Add support for italic text on ttys.
[emacs.git] / src / term.c
blob8ce2efc0929e88eebd97a5715fb60c78864c06b5
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985-1987, 1993-1995, 1998, 2000-2012
3 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 3 of the License, or
10 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <sys/file.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <setjmp.h>
31 #include "lisp.h"
32 #include "termchar.h"
33 #include "termopts.h"
34 #include "tparam.h"
35 #include "buffer.h"
36 #include "character.h"
37 #include "charset.h"
38 #include "coding.h"
39 #include "composite.h"
40 #include "keyboard.h"
41 #include "frame.h"
42 #include "disptab.h"
43 #include "termhooks.h"
44 #include "dispextern.h"
45 #include "window.h"
46 #include "keymap.h"
47 #include "blockinput.h"
48 #include "syssignal.h"
49 #include "systty.h"
50 #include "intervals.h"
51 #ifdef MSDOS
52 #include "msdos.h"
53 static int been_here = -1;
54 #endif
56 #include "cm.h"
57 #ifdef HAVE_X_WINDOWS
58 #include "xterm.h"
59 #endif
61 #ifndef O_RDWR
62 #define O_RDWR 2
63 #endif
65 #ifndef O_NOCTTY
66 #define O_NOCTTY 0
67 #endif
69 /* The name of the default console device. */
70 #ifdef WINDOWSNT
71 #define DEV_TTY "CONOUT$"
72 #else
73 #define DEV_TTY "/dev/tty"
74 #endif
76 static void tty_set_scroll_region (struct frame *f, int start, int stop);
77 static void turn_on_face (struct frame *, int face_id);
78 static void turn_off_face (struct frame *, int face_id);
79 static void tty_turn_off_highlight (struct tty_display_info *);
80 static void tty_show_cursor (struct tty_display_info *);
81 static void tty_hide_cursor (struct tty_display_info *);
82 static void tty_background_highlight (struct tty_display_info *tty);
83 static struct terminal *get_tty_terminal (Lisp_Object, int);
84 static void clear_tty_hooks (struct terminal *terminal);
85 static void set_tty_hooks (struct terminal *terminal);
86 static void dissociate_if_controlling_tty (int fd);
87 static void delete_tty (struct terminal *);
88 static void maybe_fatal (int must_succeed, struct terminal *terminal,
89 const char *str1, const char *str2, ...)
90 NO_RETURN ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
91 static void vfatal (const char *str, va_list ap)
92 NO_RETURN ATTRIBUTE_FORMAT_PRINTF (1, 0);
95 #define OUTPUT(tty, a) \
96 emacs_tputs ((tty), a, \
97 (int) (FRAME_LINES (XFRAME (selected_frame)) \
98 - curY (tty)), \
99 cmputc)
101 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
102 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
104 #define OUTPUT_IF(tty, a) \
105 do { \
106 if (a) \
107 OUTPUT (tty, a); \
108 } while (0)
110 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
112 /* Display space properties */
114 /* Chain of all tty device parameters. */
115 struct tty_display_info *tty_list;
117 /* Meaning of bits in no_color_video. Each bit set means that the
118 corresponding attribute cannot be combined with colors. */
120 enum no_color_bit
122 NC_STANDOUT = 1 << 0,
123 NC_UNDERLINE = 1 << 1,
124 NC_REVERSE = 1 << 2,
125 NC_ITALIC = 1 << 3,
126 NC_DIM = 1 << 4,
127 NC_BOLD = 1 << 5,
128 NC_INVIS = 1 << 6,
129 NC_PROTECT = 1 << 7
132 /* internal state */
134 /* The largest frame width in any call to calculate_costs. */
136 static int max_frame_cols;
138 /* Non-zero if we have dropped our controlling tty and therefore
139 should not open a frame on stdout. */
140 static int no_controlling_tty;
144 #ifdef HAVE_GPM
145 #include <sys/fcntl.h>
147 /* The device for which we have enabled gpm support (or NULL). */
148 struct tty_display_info *gpm_tty = NULL;
150 /* Last recorded mouse coordinates. */
151 static int last_mouse_x, last_mouse_y;
152 #endif /* HAVE_GPM */
154 /* Ring the bell on a tty. */
156 static void
157 tty_ring_bell (struct frame *f)
159 struct tty_display_info *tty = FRAME_TTY (f);
161 if (tty->output)
163 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
164 ? tty->TS_visible_bell
165 : tty->TS_bell));
166 fflush (tty->output);
170 /* Set up termcap modes for Emacs. */
172 static void
173 tty_set_terminal_modes (struct terminal *terminal)
175 struct tty_display_info *tty = terminal->display_info.tty;
177 if (tty->output)
179 if (tty->TS_termcap_modes)
180 OUTPUT (tty, tty->TS_termcap_modes);
181 else
183 /* Output enough newlines to scroll all the old screen contents
184 off the screen, so it won't be overwritten and lost. */
185 int i;
186 current_tty = tty;
187 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
188 cmputc ('\n');
191 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
192 OUTPUT_IF (tty, tty->TS_keypad_mode);
193 losecursor (tty);
194 fflush (tty->output);
198 /* Reset termcap modes before exiting Emacs. */
200 static void
201 tty_reset_terminal_modes (struct terminal *terminal)
203 struct tty_display_info *tty = terminal->display_info.tty;
205 if (tty->output)
207 tty_turn_off_highlight (tty);
208 tty_turn_off_insert (tty);
209 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
210 OUTPUT_IF (tty, tty->TS_cursor_normal);
211 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
212 OUTPUT_IF (tty, tty->TS_orig_pair);
213 /* Output raw CR so kernel can track the cursor hpos. */
214 current_tty = tty;
215 cmputc ('\r');
216 fflush (tty->output);
220 /* Flag the end of a display update on a termcap terminal. */
222 static void
223 tty_update_end (struct frame *f)
225 struct tty_display_info *tty = FRAME_TTY (f);
227 if (!XWINDOW (selected_window)->cursor_off_p)
228 tty_show_cursor (tty);
229 tty_turn_off_insert (tty);
230 tty_background_highlight (tty);
233 /* The implementation of set_terminal_window for termcap frames. */
235 static void
236 tty_set_terminal_window (struct frame *f, int size)
238 struct tty_display_info *tty = FRAME_TTY (f);
240 tty->specified_window = size ? size : FRAME_LINES (f);
241 if (FRAME_SCROLL_REGION_OK (f))
242 tty_set_scroll_region (f, 0, tty->specified_window);
245 static void
246 tty_set_scroll_region (struct frame *f, int start, int stop)
248 char *buf;
249 struct tty_display_info *tty = FRAME_TTY (f);
251 if (tty->TS_set_scroll_region)
252 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1, 0, 0);
253 else if (tty->TS_set_scroll_region_1)
254 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
255 FRAME_LINES (f), start,
256 FRAME_LINES (f) - stop,
257 FRAME_LINES (f));
258 else
259 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
261 OUTPUT (tty, buf);
262 xfree (buf);
263 losecursor (tty);
267 static void
268 tty_turn_on_insert (struct tty_display_info *tty)
270 if (!tty->insert_mode)
271 OUTPUT (tty, tty->TS_insert_mode);
272 tty->insert_mode = 1;
275 void
276 tty_turn_off_insert (struct tty_display_info *tty)
278 if (tty->insert_mode)
279 OUTPUT (tty, tty->TS_end_insert_mode);
280 tty->insert_mode = 0;
283 /* Handle highlighting. */
285 static void
286 tty_turn_off_highlight (struct tty_display_info *tty)
288 if (tty->standout_mode)
289 OUTPUT_IF (tty, tty->TS_end_standout_mode);
290 tty->standout_mode = 0;
293 static void
294 tty_turn_on_highlight (struct tty_display_info *tty)
296 if (!tty->standout_mode)
297 OUTPUT_IF (tty, tty->TS_standout_mode);
298 tty->standout_mode = 1;
301 static void
302 tty_toggle_highlight (struct tty_display_info *tty)
304 if (tty->standout_mode)
305 tty_turn_off_highlight (tty);
306 else
307 tty_turn_on_highlight (tty);
311 /* Make cursor invisible. */
313 static void
314 tty_hide_cursor (struct tty_display_info *tty)
316 if (tty->cursor_hidden == 0)
318 tty->cursor_hidden = 1;
319 OUTPUT_IF (tty, tty->TS_cursor_invisible);
324 /* Ensure that cursor is visible. */
326 static void
327 tty_show_cursor (struct tty_display_info *tty)
329 if (tty->cursor_hidden)
331 tty->cursor_hidden = 0;
332 OUTPUT_IF (tty, tty->TS_cursor_normal);
333 if (visible_cursor)
334 OUTPUT_IF (tty, tty->TS_cursor_visible);
339 /* Set standout mode to the state it should be in for
340 empty space inside windows. What this is,
341 depends on the user option inverse-video. */
343 static void
344 tty_background_highlight (struct tty_display_info *tty)
346 if (inverse_video)
347 tty_turn_on_highlight (tty);
348 else
349 tty_turn_off_highlight (tty);
352 /* Set standout mode to the mode specified for the text to be output. */
354 static void
355 tty_highlight_if_desired (struct tty_display_info *tty)
357 if (inverse_video)
358 tty_turn_on_highlight (tty);
359 else
360 tty_turn_off_highlight (tty);
364 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
365 frame-relative coordinates. */
367 static void
368 tty_cursor_to (struct frame *f, int vpos, int hpos)
370 struct tty_display_info *tty = FRAME_TTY (f);
372 /* Detect the case where we are called from reset_sys_modes
373 and the costs have never been calculated. Do nothing. */
374 if (! tty->costs_set)
375 return;
377 if (curY (tty) == vpos
378 && curX (tty) == hpos)
379 return;
380 if (!tty->TF_standout_motion)
381 tty_background_highlight (tty);
382 if (!tty->TF_insmode_motion)
383 tty_turn_off_insert (tty);
384 cmgoto (tty, vpos, hpos);
387 /* Similar but don't take any account of the wasted characters. */
389 static void
390 tty_raw_cursor_to (struct frame *f, int row, int col)
392 struct tty_display_info *tty = FRAME_TTY (f);
394 if (curY (tty) == row
395 && curX (tty) == col)
396 return;
397 if (!tty->TF_standout_motion)
398 tty_background_highlight (tty);
399 if (!tty->TF_insmode_motion)
400 tty_turn_off_insert (tty);
401 cmgoto (tty, row, col);
404 /* Erase operations */
406 /* Clear from cursor to end of frame on a termcap device. */
408 static void
409 tty_clear_to_end (struct frame *f)
411 register int i;
412 struct tty_display_info *tty = FRAME_TTY (f);
414 if (tty->TS_clr_to_bottom)
416 tty_background_highlight (tty);
417 OUTPUT (tty, tty->TS_clr_to_bottom);
419 else
421 for (i = curY (tty); i < FRAME_LINES (f); i++)
423 cursor_to (f, i, 0);
424 clear_end_of_line (f, FRAME_COLS (f));
429 /* Clear an entire termcap frame. */
431 static void
432 tty_clear_frame (struct frame *f)
434 struct tty_display_info *tty = FRAME_TTY (f);
436 if (tty->TS_clr_frame)
438 tty_background_highlight (tty);
439 OUTPUT (tty, tty->TS_clr_frame);
440 cmat (tty, 0, 0);
442 else
444 cursor_to (f, 0, 0);
445 clear_to_end (f);
449 /* An implementation of clear_end_of_line for termcap frames.
451 Note that the cursor may be moved, on terminals lacking a `ce' string. */
453 static void
454 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
456 register int i;
457 struct tty_display_info *tty = FRAME_TTY (f);
459 /* Detect the case where we are called from reset_sys_modes
460 and the costs have never been calculated. Do nothing. */
461 if (! tty->costs_set)
462 return;
464 if (curX (tty) >= first_unused_hpos)
465 return;
466 tty_background_highlight (tty);
467 if (tty->TS_clr_line)
469 OUTPUT1 (tty, tty->TS_clr_line);
471 else
472 { /* have to do it the hard way */
473 tty_turn_off_insert (tty);
475 /* Do not write in last row last col with Auto-wrap on. */
476 if (AutoWrap (tty)
477 && curY (tty) == FrameRows (tty) - 1
478 && first_unused_hpos == FrameCols (tty))
479 first_unused_hpos--;
481 for (i = curX (tty); i < first_unused_hpos; i++)
483 if (tty->termscript)
484 fputc (' ', tty->termscript);
485 fputc (' ', tty->output);
487 cmplus (tty, first_unused_hpos - curX (tty));
491 /* Buffers to store the source and result of code conversion for terminal. */
492 static unsigned char *encode_terminal_src;
493 static unsigned char *encode_terminal_dst;
494 /* Allocated sizes of the above buffers. */
495 static ptrdiff_t encode_terminal_src_size;
496 static ptrdiff_t encode_terminal_dst_size;
498 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
499 Set CODING->produced to the byte-length of the resulting byte
500 sequence, and return a pointer to that byte sequence. */
502 unsigned char *
503 encode_terminal_code (struct glyph *src, int src_len, struct coding_system *coding)
505 struct glyph *src_end = src + src_len;
506 unsigned char *buf;
507 ptrdiff_t nchars, nbytes, required;
508 ptrdiff_t tlen = GLYPH_TABLE_LENGTH;
509 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
510 Lisp_Object charset_list;
512 /* Allocate sufficient size of buffer to store all characters in
513 multibyte-form. But, it may be enlarged on demand if
514 Vglyph_table contains a string or a composite glyph is
515 encountered. */
516 if (min (PTRDIFF_MAX, SIZE_MAX) / MAX_MULTIBYTE_LENGTH < src_len)
517 memory_full (SIZE_MAX);
518 required = src_len;
519 required *= MAX_MULTIBYTE_LENGTH;
520 if (encode_terminal_src_size < required)
522 encode_terminal_src = xrealloc (encode_terminal_src, required);
523 encode_terminal_src_size = required;
526 charset_list = coding_charset_list (coding);
528 buf = encode_terminal_src;
529 nchars = 0;
530 while (src < src_end)
532 if (src->type == COMPOSITE_GLYPH)
534 struct composition *cmp IF_LINT (= NULL);
535 Lisp_Object gstring IF_LINT (= Qnil);
536 int i;
538 nbytes = buf - encode_terminal_src;
539 if (src->u.cmp.automatic)
541 gstring = composition_gstring_from_id (src->u.cmp.id);
542 required = src->slice.cmp.to - src->slice.cmp.from + 1;
544 else
546 cmp = composition_table[src->u.cmp.id];
547 required = cmp->glyph_len;
548 required *= MAX_MULTIBYTE_LENGTH;
551 if (encode_terminal_src_size - nbytes < required)
553 encode_terminal_src =
554 xpalloc (encode_terminal_src, &encode_terminal_src_size,
555 required - (encode_terminal_src_size - nbytes),
556 -1, 1);
557 buf = encode_terminal_src + nbytes;
560 if (src->u.cmp.automatic)
561 for (i = src->slice.cmp.from; i <= src->slice.cmp.to; i++)
563 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
564 int c = LGLYPH_CHAR (g);
566 if (! char_charset (c, charset_list, NULL))
567 c = '?';
568 buf += CHAR_STRING (c, buf);
569 nchars++;
571 else
572 for (i = 0; i < cmp->glyph_len; i++)
574 int c = COMPOSITION_GLYPH (cmp, i);
576 /* TAB in a composition means display glyphs with
577 padding space on the left or right. */
578 if (c == '\t')
579 continue;
580 if (char_charset (c, charset_list, NULL))
582 if (CHAR_WIDTH (c) == 0
583 && i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
584 /* Should be left-padded */
586 buf += CHAR_STRING (' ', buf);
587 nchars++;
590 else
591 c = '?';
592 buf += CHAR_STRING (c, buf);
593 nchars++;
596 /* We must skip glyphs to be padded for a wide character. */
597 else if (! CHAR_GLYPH_PADDING_P (*src))
599 GLYPH g;
600 int c IF_LINT (= 0);
601 Lisp_Object string;
603 string = Qnil;
604 SET_GLYPH_FROM_CHAR_GLYPH (g, src[0]);
606 if (GLYPH_INVALID_P (g) || GLYPH_SIMPLE_P (tbase, tlen, g))
608 /* This glyph doesn't have an entry in Vglyph_table. */
609 c = src->u.ch;
611 else
613 /* This glyph has an entry in Vglyph_table,
614 so process any alias before testing for simpleness. */
615 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
617 if (GLYPH_SIMPLE_P (tbase, tlen, g))
618 /* We set the multi-byte form of a character in G
619 (that should be an ASCII character) at WORKBUF. */
620 c = GLYPH_CHAR (g);
621 else
622 /* We have a string in Vglyph_table. */
623 string = tbase[GLYPH_CHAR (g)];
626 if (NILP (string))
628 nbytes = buf - encode_terminal_src;
629 if (encode_terminal_src_size - nbytes < MAX_MULTIBYTE_LENGTH)
631 encode_terminal_src =
632 xpalloc (encode_terminal_src, &encode_terminal_src_size,
633 MAX_MULTIBYTE_LENGTH, -1, 1);
634 buf = encode_terminal_src + nbytes;
636 if (CHAR_BYTE8_P (c)
637 || char_charset (c, charset_list, NULL))
639 /* Store the multibyte form of C at BUF. */
640 buf += CHAR_STRING (c, buf);
641 nchars++;
643 else
645 /* C is not encodable. */
646 *buf++ = '?';
647 nchars++;
648 while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
650 *buf++ = '?';
651 nchars++;
652 src++;
656 else
658 if (! STRING_MULTIBYTE (string))
659 string = string_to_multibyte (string);
660 nbytes = buf - encode_terminal_src;
661 if (encode_terminal_src_size - nbytes < SBYTES (string))
663 encode_terminal_src =
664 xpalloc (encode_terminal_src, &encode_terminal_src_size,
665 (SBYTES (string)
666 - (encode_terminal_src_size - nbytes)),
667 -1, 1);
668 buf = encode_terminal_src + nbytes;
670 memcpy (buf, SDATA (string), SBYTES (string));
671 buf += SBYTES (string);
672 nchars += SCHARS (string);
675 src++;
678 if (nchars == 0)
680 coding->produced = 0;
681 return NULL;
684 nbytes = buf - encode_terminal_src;
685 coding->source = encode_terminal_src;
686 if (encode_terminal_dst_size == 0)
688 encode_terminal_dst = xrealloc (encode_terminal_dst,
689 encode_terminal_src_size);
690 encode_terminal_dst_size = encode_terminal_src_size;
692 coding->destination = encode_terminal_dst;
693 coding->dst_bytes = encode_terminal_dst_size;
694 encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
695 /* coding->destination may have been reallocated. */
696 encode_terminal_dst = coding->destination;
697 encode_terminal_dst_size = coding->dst_bytes;
699 return (encode_terminal_dst);
704 /* An implementation of write_glyphs for termcap frames. */
706 static void
707 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
709 unsigned char *conversion_buffer;
710 struct coding_system *coding;
711 size_t n, stringlen;
713 struct tty_display_info *tty = FRAME_TTY (f);
715 tty_turn_off_insert (tty);
716 tty_hide_cursor (tty);
718 /* Don't dare write in last column of bottom line, if Auto-Wrap,
719 since that would scroll the whole frame on some terminals. */
721 if (AutoWrap (tty)
722 && curY (tty) + 1 == FRAME_LINES (f)
723 && (curX (tty) + len) == FRAME_COLS (f))
724 len --;
725 if (len <= 0)
726 return;
728 cmplus (tty, len);
730 /* If terminal_coding does any conversion, use it, otherwise use
731 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
732 because it always return 1 if the member src_multibyte is 1. */
733 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
734 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
735 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
736 the tail. */
737 coding->mode &= ~CODING_MODE_LAST_BLOCK;
739 for (stringlen = len; stringlen != 0; stringlen -= n)
741 /* Identify a run of glyphs with the same face. */
742 int face_id = string->face_id;
744 for (n = 1; n < stringlen; ++n)
745 if (string[n].face_id != face_id)
746 break;
748 /* Turn appearance modes of the face of the run on. */
749 tty_highlight_if_desired (tty);
750 turn_on_face (f, face_id);
752 if (n == stringlen)
753 /* This is the last run. */
754 coding->mode |= CODING_MODE_LAST_BLOCK;
755 conversion_buffer = encode_terminal_code (string, n, coding);
756 if (coding->produced > 0)
758 BLOCK_INPUT;
759 fwrite (conversion_buffer, 1, coding->produced, tty->output);
760 if (ferror (tty->output))
761 clearerr (tty->output);
762 if (tty->termscript)
763 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
764 UNBLOCK_INPUT;
766 string += n;
768 /* Turn appearance modes off. */
769 turn_off_face (f, face_id);
770 tty_turn_off_highlight (tty);
773 cmcheckmagic (tty);
776 #ifdef HAVE_GPM /* Only used by GPM code. */
778 static void
779 tty_write_glyphs_with_face (register struct frame *f, register struct glyph *string,
780 register int len, register int face_id)
782 unsigned char *conversion_buffer;
783 struct coding_system *coding;
785 struct tty_display_info *tty = FRAME_TTY (f);
787 tty_turn_off_insert (tty);
788 tty_hide_cursor (tty);
790 /* Don't dare write in last column of bottom line, if Auto-Wrap,
791 since that would scroll the whole frame on some terminals. */
793 if (AutoWrap (tty)
794 && curY (tty) + 1 == FRAME_LINES (f)
795 && (curX (tty) + len) == FRAME_COLS (f))
796 len --;
797 if (len <= 0)
798 return;
800 cmplus (tty, len);
802 /* If terminal_coding does any conversion, use it, otherwise use
803 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
804 because it always return 1 if the member src_multibyte is 1. */
805 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
806 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
807 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
808 the tail. */
809 coding->mode &= ~CODING_MODE_LAST_BLOCK;
811 /* Turn appearance modes of the face. */
812 tty_highlight_if_desired (tty);
813 turn_on_face (f, face_id);
815 coding->mode |= CODING_MODE_LAST_BLOCK;
816 conversion_buffer = encode_terminal_code (string, len, coding);
817 if (coding->produced > 0)
819 BLOCK_INPUT;
820 fwrite (conversion_buffer, 1, coding->produced, tty->output);
821 if (ferror (tty->output))
822 clearerr (tty->output);
823 if (tty->termscript)
824 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
825 UNBLOCK_INPUT;
828 /* Turn appearance modes off. */
829 turn_off_face (f, face_id);
830 tty_turn_off_highlight (tty);
832 cmcheckmagic (tty);
834 #endif
836 /* An implementation of insert_glyphs for termcap frames. */
838 static void
839 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
841 char *buf;
842 struct glyph *glyph = NULL;
843 unsigned char *conversion_buffer;
844 unsigned char space[1];
845 struct coding_system *coding;
847 struct tty_display_info *tty = FRAME_TTY (f);
849 if (tty->TS_ins_multi_chars)
851 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len, 0, 0, 0);
852 OUTPUT1 (tty, buf);
853 xfree (buf);
854 if (start)
855 write_glyphs (f, start, len);
856 return;
859 tty_turn_on_insert (tty);
860 cmplus (tty, len);
862 if (! start)
863 space[0] = SPACEGLYPH;
865 /* If terminal_coding does any conversion, use it, otherwise use
866 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
867 because it always return 1 if the member src_multibyte is 1. */
868 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
869 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
870 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
871 the tail. */
872 coding->mode &= ~CODING_MODE_LAST_BLOCK;
874 while (len-- > 0)
876 OUTPUT1_IF (tty, tty->TS_ins_char);
877 if (!start)
879 conversion_buffer = space;
880 coding->produced = 1;
882 else
884 tty_highlight_if_desired (tty);
885 turn_on_face (f, start->face_id);
886 glyph = start;
887 ++start;
888 /* We must open sufficient space for a character which
889 occupies more than one column. */
890 while (len && CHAR_GLYPH_PADDING_P (*start))
892 OUTPUT1_IF (tty, tty->TS_ins_char);
893 start++, len--;
896 if (len <= 0)
897 /* This is the last glyph. */
898 coding->mode |= CODING_MODE_LAST_BLOCK;
900 conversion_buffer = encode_terminal_code (glyph, 1, coding);
903 if (coding->produced > 0)
905 BLOCK_INPUT;
906 fwrite (conversion_buffer, 1, coding->produced, tty->output);
907 if (ferror (tty->output))
908 clearerr (tty->output);
909 if (tty->termscript)
910 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
911 UNBLOCK_INPUT;
914 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
915 if (start)
917 turn_off_face (f, glyph->face_id);
918 tty_turn_off_highlight (tty);
922 cmcheckmagic (tty);
925 /* An implementation of delete_glyphs for termcap frames. */
927 static void
928 tty_delete_glyphs (struct frame *f, int n)
930 char *buf;
931 register int i;
933 struct tty_display_info *tty = FRAME_TTY (f);
935 if (tty->delete_in_insert_mode)
937 tty_turn_on_insert (tty);
939 else
941 tty_turn_off_insert (tty);
942 OUTPUT_IF (tty, tty->TS_delete_mode);
945 if (tty->TS_del_multi_chars)
947 buf = tparam (tty->TS_del_multi_chars, 0, 0, n, 0, 0, 0);
948 OUTPUT1 (tty, buf);
949 xfree (buf);
951 else
952 for (i = 0; i < n; i++)
953 OUTPUT1 (tty, tty->TS_del_char);
954 if (!tty->delete_in_insert_mode)
955 OUTPUT_IF (tty, tty->TS_end_delete_mode);
958 /* An implementation of ins_del_lines for termcap frames. */
960 static void
961 tty_ins_del_lines (struct frame *f, int vpos, int n)
963 struct tty_display_info *tty = FRAME_TTY (f);
964 const char *multi =
965 n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
966 const char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
967 const char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
969 register int i = n > 0 ? n : -n;
970 register char *buf;
972 /* If the lines below the insertion are being pushed
973 into the end of the window, this is the same as clearing;
974 and we know the lines are already clear, since the matching
975 deletion has already been done. So can ignore this. */
976 /* If the lines below the deletion are blank lines coming
977 out of the end of the window, don't bother,
978 as there will be a matching inslines later that will flush them. */
979 if (FRAME_SCROLL_REGION_OK (f)
980 && vpos + i >= tty->specified_window)
981 return;
982 if (!FRAME_MEMORY_BELOW_FRAME (f)
983 && vpos + i >= FRAME_LINES (f))
984 return;
986 if (multi)
988 raw_cursor_to (f, vpos, 0);
989 tty_background_highlight (tty);
990 buf = tparam (multi, 0, 0, i, 0, 0, 0);
991 OUTPUT (tty, buf);
992 xfree (buf);
994 else if (single)
996 raw_cursor_to (f, vpos, 0);
997 tty_background_highlight (tty);
998 while (--i >= 0)
999 OUTPUT (tty, single);
1000 if (tty->TF_teleray)
1001 curX (tty) = 0;
1003 else
1005 tty_set_scroll_region (f, vpos, tty->specified_window);
1006 if (n < 0)
1007 raw_cursor_to (f, tty->specified_window - 1, 0);
1008 else
1009 raw_cursor_to (f, vpos, 0);
1010 tty_background_highlight (tty);
1011 while (--i >= 0)
1012 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1013 tty_set_scroll_region (f, 0, tty->specified_window);
1016 if (!FRAME_SCROLL_REGION_OK (f)
1017 && FRAME_MEMORY_BELOW_FRAME (f)
1018 && n < 0)
1020 cursor_to (f, FRAME_LINES (f) + n, 0);
1021 clear_to_end (f);
1025 /* Compute cost of sending "str", in characters,
1026 not counting any line-dependent padding. */
1029 string_cost (const char *str)
1031 cost = 0;
1032 if (str)
1033 tputs (str, 0, evalcost);
1034 return cost;
1037 /* Compute cost of sending "str", in characters,
1038 counting any line-dependent padding at one line. */
1040 static int
1041 string_cost_one_line (const char *str)
1043 cost = 0;
1044 if (str)
1045 tputs (str, 1, evalcost);
1046 return cost;
1049 /* Compute per line amount of line-dependent padding,
1050 in tenths of characters. */
1053 per_line_cost (const char *str)
1055 cost = 0;
1056 if (str)
1057 tputs (str, 0, evalcost);
1058 cost = - cost;
1059 if (str)
1060 tputs (str, 10, evalcost);
1061 return cost;
1064 /* char_ins_del_cost[n] is cost of inserting N characters.
1065 char_ins_del_cost[-n] is cost of deleting N characters.
1066 The length of this vector is based on max_frame_cols. */
1068 int *char_ins_del_vector;
1070 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1072 /* ARGSUSED */
1073 static void
1074 calculate_ins_del_char_costs (struct frame *f)
1076 struct tty_display_info *tty = FRAME_TTY (f);
1077 int ins_startup_cost, del_startup_cost;
1078 int ins_cost_per_char, del_cost_per_char;
1079 register int i;
1080 register int *p;
1082 if (tty->TS_ins_multi_chars)
1084 ins_cost_per_char = 0;
1085 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1087 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1088 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1090 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1091 + string_cost (tty->TS_end_insert_mode))) / 100;
1092 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1093 + string_cost_one_line (tty->TS_pad_inserted_char));
1095 else
1097 ins_startup_cost = 9999;
1098 ins_cost_per_char = 0;
1101 if (tty->TS_del_multi_chars)
1103 del_cost_per_char = 0;
1104 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1106 else if (tty->TS_del_char)
1108 del_startup_cost = (string_cost (tty->TS_delete_mode)
1109 + string_cost (tty->TS_end_delete_mode));
1110 if (tty->delete_in_insert_mode)
1111 del_startup_cost /= 2;
1112 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1114 else
1116 del_startup_cost = 9999;
1117 del_cost_per_char = 0;
1120 /* Delete costs are at negative offsets */
1121 p = &char_ins_del_cost (f)[0];
1122 for (i = FRAME_COLS (f); --i >= 0;)
1123 *--p = (del_startup_cost += del_cost_per_char);
1125 /* Doing nothing is free */
1126 p = &char_ins_del_cost (f)[0];
1127 *p++ = 0;
1129 /* Insert costs are at positive offsets */
1130 for (i = FRAME_COLS (f); --i >= 0;)
1131 *p++ = (ins_startup_cost += ins_cost_per_char);
1134 void
1135 calculate_costs (struct frame *frame)
1137 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1139 if (FRAME_TERMCAP_P (frame))
1141 struct tty_display_info *tty = FRAME_TTY (frame);
1142 register const char *f = (tty->TS_set_scroll_region
1143 ? tty->TS_set_scroll_region
1144 : tty->TS_set_scroll_region_1);
1146 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1148 tty->costs_set = 1;
1150 /* These variables are only used for terminal stuff. They are
1151 allocated once for the terminal frame of X-windows emacs, but not
1152 used afterwards.
1154 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1155 X turns off char_ins_del_ok. */
1157 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1158 if ((min (PTRDIFF_MAX, SIZE_MAX) / sizeof (int) - 1) / 2
1159 < max_frame_cols)
1160 memory_full (SIZE_MAX);
1162 char_ins_del_vector =
1163 xrealloc (char_ins_del_vector,
1164 (sizeof (int) + 2 * sizeof (int) * max_frame_cols));
1166 memset (char_ins_del_vector, 0,
1167 (sizeof (int) + 2 * sizeof (int) * max_frame_cols));
1170 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1171 do_line_insertion_deletion_costs (frame,
1172 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1173 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1174 f, f, 1);
1175 else
1176 do_line_insertion_deletion_costs (frame,
1177 tty->TS_ins_line, tty->TS_ins_multi_lines,
1178 tty->TS_del_line, tty->TS_del_multi_lines,
1179 0, 0, 1);
1181 calculate_ins_del_char_costs (frame);
1183 /* Don't use TS_repeat if its padding is worse than sending the chars */
1184 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1185 tty->RPov = string_cost (tty->TS_repeat);
1186 else
1187 tty->RPov = FRAME_COLS (frame) * 2;
1189 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1193 struct fkey_table {
1194 const char *cap, *name;
1197 /* Termcap capability names that correspond directly to X keysyms.
1198 Some of these (marked "terminfo") aren't supplied by old-style
1199 (Berkeley) termcap entries. They're listed in X keysym order;
1200 except we put the keypad keys first, so that if they clash with
1201 other keys (as on the IBM PC keyboard) they get overridden.
1204 static const struct fkey_table keys[] =
1206 {"kh", "home"}, /* termcap */
1207 {"kl", "left"}, /* termcap */
1208 {"ku", "up"}, /* termcap */
1209 {"kr", "right"}, /* termcap */
1210 {"kd", "down"}, /* termcap */
1211 {"%8", "prior"}, /* terminfo */
1212 {"%5", "next"}, /* terminfo */
1213 {"@7", "end"}, /* terminfo */
1214 {"@1", "begin"}, /* terminfo */
1215 {"*6", "select"}, /* terminfo */
1216 {"%9", "print"}, /* terminfo */
1217 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1219 * "insert" --- see below
1221 {"&8", "undo"}, /* terminfo */
1222 {"%0", "redo"}, /* terminfo */
1223 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1224 {"@0", "find"}, /* terminfo */
1225 {"@2", "cancel"}, /* terminfo */
1226 {"%1", "help"}, /* terminfo */
1228 * "break" goes here, but can't be reliably intercepted with termcap
1230 {"&4", "reset"}, /* terminfo --- actually `restart' */
1232 * "system" and "user" --- no termcaps
1234 {"kE", "clearline"}, /* terminfo */
1235 {"kA", "insertline"}, /* terminfo */
1236 {"kL", "deleteline"}, /* terminfo */
1237 {"kI", "insertchar"}, /* terminfo */
1238 {"kD", "deletechar"}, /* terminfo */
1239 {"kB", "backtab"}, /* terminfo */
1241 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1243 {"@8", "kp-enter"}, /* terminfo */
1245 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1246 * "kp-multiply", "kp-add", "kp-separator",
1247 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1248 * --- no termcaps for any of these.
1250 {"K4", "kp-1"}, /* terminfo */
1252 * "kp-2" --- no termcap
1254 {"K5", "kp-3"}, /* terminfo */
1256 * "kp-4" --- no termcap
1258 {"K2", "kp-5"}, /* terminfo */
1260 * "kp-6" --- no termcap
1262 {"K1", "kp-7"}, /* terminfo */
1264 * "kp-8" --- no termcap
1266 {"K3", "kp-9"}, /* terminfo */
1268 * "kp-equal" --- no termcap
1270 {"k1", "f1"},
1271 {"k2", "f2"},
1272 {"k3", "f3"},
1273 {"k4", "f4"},
1274 {"k5", "f5"},
1275 {"k6", "f6"},
1276 {"k7", "f7"},
1277 {"k8", "f8"},
1278 {"k9", "f9"},
1280 {"&0", "S-cancel"}, /*shifted cancel key*/
1281 {"&9", "S-begin"}, /*shifted begin key*/
1282 {"*0", "S-find"}, /*shifted find key*/
1283 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1284 {"*4", "S-delete"}, /*shifted delete-character key*/
1285 {"*7", "S-end"}, /*shifted end key*/
1286 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1287 {"#1", "S-help"}, /*shifted help key*/
1288 {"#2", "S-home"}, /*shifted home key*/
1289 {"#3", "S-insert"}, /*shifted insert-character key*/
1290 {"#4", "S-left"}, /*shifted left-arrow key*/
1291 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1292 {"%c", "S-next"}, /*shifted next key*/
1293 {"%e", "S-prior"}, /*shifted previous key*/
1294 {"%f", "S-print"}, /*shifted print key*/
1295 {"%g", "S-redo"}, /*shifted redo key*/
1296 {"%i", "S-right"}, /*shifted right-arrow key*/
1297 {"!3", "S-undo"} /*shifted undo key*/
1300 #ifndef DOS_NT
1301 static char **term_get_fkeys_address;
1302 static KBOARD *term_get_fkeys_kboard;
1303 static Lisp_Object term_get_fkeys_1 (void);
1305 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1306 This function scans the termcap function key sequence entries, and
1307 adds entries to Vinput_decode_map for each function key it finds. */
1309 static void
1310 term_get_fkeys (char **address, KBOARD *kboard)
1312 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1313 errors during the call. The only errors should be from Fdefine_key
1314 when given a key sequence containing an invalid prefix key. If the
1315 termcap defines function keys which use a prefix that is already bound
1316 to a command by the default bindings, we should silently ignore that
1317 function key specification, rather than giving the user an error and
1318 refusing to run at all on such a terminal. */
1320 term_get_fkeys_address = address;
1321 term_get_fkeys_kboard = kboard;
1322 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1325 static Lisp_Object
1326 term_get_fkeys_1 (void)
1328 int i;
1330 char **address = term_get_fkeys_address;
1331 KBOARD *kboard = term_get_fkeys_kboard;
1333 /* This can happen if CANNOT_DUMP or with strange options. */
1334 if (!KEYMAPP (KVAR (kboard, Vinput_decode_map)))
1335 KVAR (kboard, Vinput_decode_map) = Fmake_sparse_keymap (Qnil);
1337 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1339 char *sequence = tgetstr (keys[i].cap, address);
1340 if (sequence)
1341 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence),
1342 Fmake_vector (make_number (1),
1343 intern (keys[i].name)));
1346 /* The uses of the "k0" capability are inconsistent; sometimes it
1347 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1348 We will attempt to politely accommodate both systems by testing for
1349 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1352 const char *k_semi = tgetstr ("k;", address);
1353 const char *k0 = tgetstr ("k0", address);
1354 const char *k0_name = "f10";
1356 if (k_semi)
1358 if (k0)
1359 /* Define f0 first, so that f10 takes precedence in case the
1360 key sequences happens to be the same. */
1361 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k0),
1362 Fmake_vector (make_number (1), intern ("f0")));
1363 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k_semi),
1364 Fmake_vector (make_number (1), intern ("f10")));
1366 else if (k0)
1367 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k0),
1368 Fmake_vector (make_number (1), intern (k0_name)));
1371 /* Set up cookies for numbered function keys above f10. */
1373 char fcap[3], fkey[4];
1375 fcap[0] = 'F'; fcap[2] = '\0';
1376 for (i = 11; i < 64; i++)
1378 if (i <= 19)
1379 fcap[1] = '1' + i - 11;
1380 else if (i <= 45)
1381 fcap[1] = 'A' + i - 20;
1382 else
1383 fcap[1] = 'a' + i - 46;
1386 char *sequence = tgetstr (fcap, address);
1387 if (sequence)
1389 sprintf (fkey, "f%d", i);
1390 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence),
1391 Fmake_vector (make_number (1),
1392 intern (fkey)));
1399 * Various mappings to try and get a better fit.
1402 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1403 if (!tgetstr (cap1, address)) \
1405 char *sequence = tgetstr (cap2, address); \
1406 if (sequence) \
1407 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence), \
1408 Fmake_vector (make_number (1), \
1409 intern (sym))); \
1412 /* if there's no key_next keycap, map key_npage to `next' keysym */
1413 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1414 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1415 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1416 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1417 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1418 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1419 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1421 /* IBM has their own non-standard dialect of terminfo.
1422 If the standard name isn't found, try the IBM name. */
1423 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1424 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1425 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1426 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1427 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1428 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1429 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1430 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1431 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1432 #undef CONDITIONAL_REASSIGN
1435 return Qnil;
1437 #endif /* not DOS_NT */
1440 /***********************************************************************
1441 Character Display Information
1442 ***********************************************************************/
1443 static void append_glyph (struct it *);
1444 static void append_composite_glyph (struct it *);
1445 static void produce_composite_glyph (struct it *);
1446 static void append_glyphless_glyph (struct it *, int, const char *);
1447 static void produce_glyphless_glyph (struct it *, int, Lisp_Object);
1449 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1450 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1451 the character for which to produce glyphs; IT->face_id contains the
1452 character's face. Padding glyphs are appended if IT->c has a
1453 IT->pixel_width > 1. */
1455 static void
1456 append_glyph (struct it *it)
1458 struct glyph *glyph, *end;
1459 int i;
1461 xassert (it->glyph_row);
1462 glyph = (it->glyph_row->glyphs[it->area]
1463 + it->glyph_row->used[it->area]);
1464 end = it->glyph_row->glyphs[1 + it->area];
1466 /* If the glyph row is reversed, we need to prepend the glyph rather
1467 than append it. */
1468 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1470 struct glyph *g;
1471 int move_by = it->pixel_width;
1473 /* Make room for the new glyphs. */
1474 if (move_by > end - glyph) /* don't overstep end of this area */
1475 move_by = end - glyph;
1476 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1477 g[move_by] = *g;
1478 glyph = it->glyph_row->glyphs[it->area];
1479 end = glyph + move_by;
1482 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1483 right, even in the REVERSED_P case, since (a) all of its u.ch are
1484 identical, and (b) the PADDING_P flag needs to be set for the
1485 leftmost one, because we write to the terminal left-to-right. */
1486 for (i = 0;
1487 i < it->pixel_width && glyph < end;
1488 ++i)
1490 glyph->type = CHAR_GLYPH;
1491 glyph->pixel_width = 1;
1492 glyph->u.ch = it->char_to_display;
1493 glyph->face_id = it->face_id;
1494 glyph->padding_p = i > 0;
1495 glyph->charpos = CHARPOS (it->position);
1496 glyph->object = it->object;
1497 if (it->bidi_p)
1499 glyph->resolved_level = it->bidi_it.resolved_level;
1500 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1501 abort ();
1502 glyph->bidi_type = it->bidi_it.type;
1504 else
1506 glyph->resolved_level = 0;
1507 glyph->bidi_type = UNKNOWN_BT;
1510 ++it->glyph_row->used[it->area];
1511 ++glyph;
1515 /* For external use. */
1516 void
1517 tty_append_glyph (struct it *it)
1519 append_glyph (it);
1523 /* Produce glyphs for the display element described by IT. *IT
1524 specifies what we want to produce a glyph for (character, image, ...),
1525 and where in the glyph matrix we currently are (glyph row and hpos).
1526 produce_glyphs fills in output fields of *IT with information such as the
1527 pixel width and height of a character, and maybe output actual glyphs at
1528 the same time if IT->glyph_row is non-null. For an overview, see
1529 the explanation in dispextern.h, before the definition of the
1530 display_element_type enumeration.
1532 produce_glyphs also stores the result of glyph width, ascent
1533 etc. computations in *IT.
1535 IT->glyph_row may be null, in which case produce_glyphs does not
1536 actually fill in the glyphs. This is used in the move_* functions
1537 in xdisp.c for text width and height computations.
1539 Callers usually don't call produce_glyphs directly;
1540 instead they use the macro PRODUCE_GLYPHS. */
1542 void
1543 produce_glyphs (struct it *it)
1545 /* If a hook is installed, let it do the work. */
1547 /* Nothing but characters are supported on terminal frames. */
1548 xassert (it->what == IT_CHARACTER
1549 || it->what == IT_COMPOSITION
1550 || it->what == IT_STRETCH
1551 || it->what == IT_GLYPHLESS);
1553 if (it->what == IT_STRETCH)
1555 produce_stretch_glyph (it);
1556 goto done;
1559 if (it->what == IT_COMPOSITION)
1561 produce_composite_glyph (it);
1562 goto done;
1565 if (it->what == IT_GLYPHLESS)
1567 produce_glyphless_glyph (it, 0, Qnil);
1568 goto done;
1571 if (it->char_to_display >= 040 && it->char_to_display < 0177)
1573 it->pixel_width = it->nglyphs = 1;
1574 if (it->glyph_row)
1575 append_glyph (it);
1577 else if (it->char_to_display == '\n')
1578 it->pixel_width = it->nglyphs = 0;
1579 else if (it->char_to_display == '\t')
1581 int absolute_x = (it->current_x
1582 + it->continuation_lines_width);
1583 int next_tab_x
1584 = (((1 + absolute_x + it->tab_width - 1)
1585 / it->tab_width)
1586 * it->tab_width);
1587 int nspaces;
1589 /* If part of the TAB has been displayed on the previous line
1590 which is continued now, continuation_lines_width will have
1591 been incremented already by the part that fitted on the
1592 continued line. So, we will get the right number of spaces
1593 here. */
1594 nspaces = next_tab_x - absolute_x;
1596 if (it->glyph_row)
1598 int n = nspaces;
1600 it->char_to_display = ' ';
1601 it->pixel_width = it->len = 1;
1603 while (n--)
1604 append_glyph (it);
1607 it->pixel_width = nspaces;
1608 it->nglyphs = nspaces;
1610 else if (CHAR_BYTE8_P (it->char_to_display))
1612 /* Coming here means that we must send the raw 8-bit byte as is
1613 to the terminal. Although there's no way to know how many
1614 columns it occupies on a screen, it is a good assumption that
1615 a single byte code has 1-column width. */
1616 it->pixel_width = it->nglyphs = 1;
1617 if (it->glyph_row)
1618 append_glyph (it);
1620 else
1622 Lisp_Object charset_list = FRAME_TERMINAL (it->f)->charset_list;
1624 if (char_charset (it->char_to_display, charset_list, NULL))
1626 it->pixel_width = CHAR_WIDTH (it->char_to_display);
1627 it->nglyphs = it->pixel_width;
1628 if (it->glyph_row)
1629 append_glyph (it);
1631 else
1633 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
1635 xassert (it->what == IT_GLYPHLESS);
1636 produce_glyphless_glyph (it, 1, acronym);
1640 done:
1641 /* Advance current_x by the pixel width as a convenience for
1642 the caller. */
1643 if (it->area == TEXT_AREA)
1644 it->current_x += it->pixel_width;
1645 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1646 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1649 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1650 Called from produce_composite_glyph for terminal frames if
1651 IT->glyph_row != NULL. IT->face_id contains the character's
1652 face. */
1654 static void
1655 append_composite_glyph (struct it *it)
1657 struct glyph *glyph;
1659 xassert (it->glyph_row);
1660 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1661 if (glyph < it->glyph_row->glyphs[1 + it->area])
1663 /* If the glyph row is reversed, we need to prepend the glyph
1664 rather than append it. */
1665 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1667 struct glyph *g;
1669 /* Make room for the new glyph. */
1670 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1671 g[1] = *g;
1672 glyph = it->glyph_row->glyphs[it->area];
1674 glyph->type = COMPOSITE_GLYPH;
1675 glyph->pixel_width = it->pixel_width;
1676 glyph->u.cmp.id = it->cmp_it.id;
1677 if (it->cmp_it.ch < 0)
1679 glyph->u.cmp.automatic = 0;
1680 glyph->u.cmp.id = it->cmp_it.id;
1682 else
1684 glyph->u.cmp.automatic = 1;
1685 glyph->u.cmp.id = it->cmp_it.id;
1686 glyph->slice.cmp.from = it->cmp_it.from;
1687 glyph->slice.cmp.to = it->cmp_it.to - 1;
1690 glyph->face_id = it->face_id;
1691 glyph->padding_p = 0;
1692 glyph->charpos = CHARPOS (it->position);
1693 glyph->object = it->object;
1694 if (it->bidi_p)
1696 glyph->resolved_level = it->bidi_it.resolved_level;
1697 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1698 abort ();
1699 glyph->bidi_type = it->bidi_it.type;
1701 else
1703 glyph->resolved_level = 0;
1704 glyph->bidi_type = UNKNOWN_BT;
1707 ++it->glyph_row->used[it->area];
1708 ++glyph;
1713 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1714 the composition. We simply produces components of the composition
1715 assuming that the terminal has a capability to layout/render it
1716 correctly. */
1718 static void
1719 produce_composite_glyph (struct it *it)
1721 if (it->cmp_it.ch < 0)
1723 struct composition *cmp = composition_table[it->cmp_it.id];
1725 it->pixel_width = cmp->width;
1727 else
1729 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
1731 it->pixel_width = composition_gstring_width (gstring, it->cmp_it.from,
1732 it->cmp_it.to, NULL);
1734 it->nglyphs = 1;
1735 if (it->glyph_row)
1736 append_composite_glyph (it);
1740 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
1741 is a face ID to be used for the glyph. What is actually appended
1742 are glyphs of type CHAR_GLYPH whose characters are in STR (which
1743 comes from it->nglyphs bytes). */
1745 static void
1746 append_glyphless_glyph (struct it *it, int face_id, const char *str)
1748 struct glyph *glyph, *end;
1749 int i;
1751 xassert (it->glyph_row);
1752 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1753 end = it->glyph_row->glyphs[1 + it->area];
1755 /* If the glyph row is reversed, we need to prepend the glyph rather
1756 than append it. */
1757 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1759 struct glyph *g;
1760 int move_by = it->pixel_width;
1762 /* Make room for the new glyphs. */
1763 if (move_by > end - glyph) /* don't overstep end of this area */
1764 move_by = end - glyph;
1765 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1766 g[move_by] = *g;
1767 glyph = it->glyph_row->glyphs[it->area];
1768 end = glyph + move_by;
1771 if (glyph >= end)
1772 return;
1773 glyph->type = CHAR_GLYPH;
1774 glyph->pixel_width = 1;
1775 glyph->face_id = face_id;
1776 glyph->padding_p = 0;
1777 glyph->charpos = CHARPOS (it->position);
1778 glyph->object = it->object;
1779 if (it->bidi_p)
1781 glyph->resolved_level = it->bidi_it.resolved_level;
1782 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1783 abort ();
1784 glyph->bidi_type = it->bidi_it.type;
1786 else
1788 glyph->resolved_level = 0;
1789 glyph->bidi_type = UNKNOWN_BT;
1792 /* BIDI Note: we put the glyphs of characters left to right, even in
1793 the REVERSED_P case because we write to the terminal
1794 left-to-right. */
1795 for (i = 0; i < it->nglyphs && glyph < end; ++i)
1797 if (i > 0)
1798 glyph[0] = glyph[-1];
1799 glyph->u.ch = str[i];
1800 ++it->glyph_row->used[it->area];
1801 ++glyph;
1805 /* Produce glyphs for a glyphless character for iterator IT.
1806 IT->glyphless_method specifies which method to use for displaying
1807 the character. See the description of enum
1808 glyphless_display_method in dispextern.h for the details.
1810 FOR_NO_FONT is nonzero if and only if this is for a character that
1811 is not supported by the coding system of the terminal. ACRONYM, if
1812 non-nil, is an acronym string for the character.
1814 The glyphs actually produced are of type CHAR_GLYPH. */
1816 static void
1817 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
1819 int face_id;
1820 int len;
1821 char buf[sizeof "\\x" + max (6, (sizeof it->c * CHAR_BIT + 3) / 4)];
1822 char const *str = " ";
1824 /* Get a face ID for the glyph by utilizing a cache (the same way as
1825 done for `escape-glyph' in get_next_display_element). */
1826 if (it->f == last_glyphless_glyph_frame
1827 && it->face_id == last_glyphless_glyph_face_id)
1829 face_id = last_glyphless_glyph_merged_face_id;
1831 else
1833 /* Merge the `glyphless-char' face into the current face. */
1834 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
1835 last_glyphless_glyph_frame = it->f;
1836 last_glyphless_glyph_face_id = it->face_id;
1837 last_glyphless_glyph_merged_face_id = face_id;
1840 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
1842 /* As there's no way to produce a thin space, we produce a space
1843 of canonical width. */
1844 len = 1;
1846 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
1848 len = CHAR_WIDTH (it->c);
1849 if (len == 0)
1850 len = 1;
1851 else if (len > 4)
1852 len = 4;
1853 sprintf (buf, "[%.*s]", len, str);
1854 len += 2;
1855 str = buf;
1857 else
1859 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
1861 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
1862 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
1863 if (CONSP (acronym))
1864 acronym = XCDR (acronym);
1865 buf[0] = '[';
1866 str = STRINGP (acronym) ? SSDATA (acronym) : "";
1867 for (len = 0; len < 6 && str[len] && ASCII_BYTE_P (str[len]); len++)
1868 buf[1 + len] = str[len];
1869 buf[1 + len] = ']';
1870 len += 2;
1872 else
1874 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
1875 len = (it->c < 0x10000 ? sprintf (buf, "\\u%04X", it->c)
1876 : it->c <= MAX_UNICODE_CHAR ? sprintf (buf, "\\U%06X", it->c)
1877 : sprintf (buf, "\\x%06X", it->c));
1879 str = buf;
1882 it->pixel_width = len;
1883 it->nglyphs = len;
1884 if (it->glyph_row)
1885 append_glyphless_glyph (it, face_id, str);
1889 /* Get information about special display element WHAT in an
1890 environment described by IT. WHAT is one of IT_TRUNCATION or
1891 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1892 non-null glyph_row member. This function ensures that fields like
1893 face_id, c, len of IT are left untouched. */
1895 void
1896 produce_special_glyphs (struct it *it, enum display_element_type what)
1898 struct it temp_it;
1899 Lisp_Object gc;
1900 GLYPH glyph;
1902 temp_it = *it;
1903 temp_it.dp = NULL;
1904 temp_it.what = IT_CHARACTER;
1905 temp_it.len = 1;
1906 temp_it.object = make_number (0);
1907 memset (&temp_it.current, 0, sizeof temp_it.current);
1909 if (what == IT_CONTINUATION)
1911 /* Continuation glyph. For R2L lines, we mirror it by hand. */
1912 if (it->bidi_it.paragraph_dir == R2L)
1913 SET_GLYPH_FROM_CHAR (glyph, '/');
1914 else
1915 SET_GLYPH_FROM_CHAR (glyph, '\\');
1916 if (it->dp
1917 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
1919 /* FIXME: Should we mirror GC for R2L lines? */
1920 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1921 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1924 else if (what == IT_TRUNCATION)
1926 /* Truncation glyph. */
1927 SET_GLYPH_FROM_CHAR (glyph, '$');
1928 if (it->dp
1929 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
1931 /* FIXME: Should we mirror GC for R2L lines? */
1932 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1933 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1936 else
1937 abort ();
1939 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
1940 temp_it.face_id = GLYPH_FACE (glyph);
1941 temp_it.len = CHAR_BYTES (temp_it.c);
1943 produce_glyphs (&temp_it);
1944 it->pixel_width = temp_it.pixel_width;
1945 it->nglyphs = temp_it.pixel_width;
1950 /***********************************************************************
1951 Faces
1952 ***********************************************************************/
1954 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1955 one of the enumerators from enum no_color_bit, or a bit set built
1956 from them. Some display attributes may not be used together with
1957 color; the termcap capability `NC' specifies which ones. */
1959 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1960 (tty->TN_max_colors > 0 \
1961 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1962 : 1)
1964 /* Turn appearances of face FACE_ID on tty frame F on.
1965 FACE_ID is a realized face ID number, in the face cache. */
1967 static void
1968 turn_on_face (struct frame *f, int face_id)
1970 struct face *face = FACE_FROM_ID (f, face_id);
1971 long fg = face->foreground;
1972 long bg = face->background;
1973 struct tty_display_info *tty = FRAME_TTY (f);
1975 /* Do this first because TS_end_standout_mode may be the same
1976 as TS_exit_attribute_mode, which turns all appearances off. */
1977 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1979 if (tty->TN_max_colors > 0)
1981 if (fg >= 0 && bg >= 0)
1983 /* If the terminal supports colors, we can set them
1984 below without using reverse video. The face's fg
1985 and bg colors are set as they should appear on
1986 the screen, i.e. they take the inverse-video'ness
1987 of the face already into account. */
1989 else if (inverse_video)
1991 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1992 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1993 tty_toggle_highlight (tty);
1995 else
1997 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1998 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1999 tty_toggle_highlight (tty);
2002 else
2004 /* If we can't display colors, use reverse video
2005 if the face specifies that. */
2006 if (inverse_video)
2008 if (fg == FACE_TTY_DEFAULT_FG_COLOR
2009 || bg == FACE_TTY_DEFAULT_BG_COLOR)
2010 tty_toggle_highlight (tty);
2012 else
2014 if (fg == FACE_TTY_DEFAULT_BG_COLOR
2015 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2016 tty_toggle_highlight (tty);
2021 if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
2022 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
2024 if (face->tty_italic_p && MAY_USE_WITH_COLORS_P (tty, NC_ITALIC))
2026 if (tty->TS_enter_italic_mode)
2027 OUTPUT1 (tty, tty->TS_enter_italic_mode);
2028 else
2029 /* Italics mode is unavailable on many terminals. In that
2030 case, map slant to dimmed text; we want italic text to
2031 appear different and dimming is not otherwise used. */
2032 OUTPUT1 (tty, tty->TS_enter_dim_mode);
2035 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
2036 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
2038 if (tty->TN_max_colors > 0)
2040 const char *ts;
2041 char *p;
2043 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
2044 if (fg >= 0 && ts)
2046 p = tparam (ts, NULL, 0, (int) fg, 0, 0, 0);
2047 OUTPUT (tty, p);
2048 xfree (p);
2051 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
2052 if (bg >= 0 && ts)
2054 p = tparam (ts, NULL, 0, (int) bg, 0, 0, 0);
2055 OUTPUT (tty, p);
2056 xfree (p);
2062 /* Turn off appearances of face FACE_ID on tty frame F. */
2064 static void
2065 turn_off_face (struct frame *f, int face_id)
2067 struct face *face = FACE_FROM_ID (f, face_id);
2068 struct tty_display_info *tty = FRAME_TTY (f);
2070 xassert (face != NULL);
2072 if (tty->TS_exit_attribute_mode)
2074 /* Capability "me" will turn off appearance modes double-bright,
2075 half-bright, reverse-video, standout, underline. It may or
2076 may not turn off alt-char-mode. */
2077 if (face->tty_bold_p
2078 || face->tty_italic_p
2079 || face->tty_reverse_p
2080 || face->tty_underline_p)
2082 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
2083 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
2084 tty->standout_mode = 0;
2087 else
2089 /* If we don't have "me" we can only have those appearances
2090 that have exit sequences defined. */
2091 if (face->tty_underline_p)
2092 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
2095 /* Switch back to default colors. */
2096 if (tty->TN_max_colors > 0
2097 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2098 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2099 || (face->background != FACE_TTY_DEFAULT_COLOR
2100 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
2101 OUTPUT1_IF (tty, tty->TS_orig_pair);
2105 /* Return non-zero if the terminal on frame F supports all of the
2106 capabilities in CAPS simultaneously, with foreground and background
2107 colors FG and BG. */
2110 tty_capable_p (struct tty_display_info *tty, unsigned int caps,
2111 unsigned long fg, unsigned long bg)
2113 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2114 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2115 return 0;
2117 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2118 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2119 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2120 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
2121 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ITALIC, tty->TS_enter_italic_mode, NC_ITALIC);
2123 /* We can do it! */
2124 return 1;
2127 /* Return non-zero if the terminal is capable to display colors. */
2129 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2130 0, 1, 0,
2131 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2133 TERMINAL can be a terminal object, a frame, or nil (meaning the
2134 selected frame's terminal). This function always returns nil if
2135 TERMINAL does not refer to a text-only terminal. */)
2136 (Lisp_Object terminal)
2138 struct terminal *t = get_tty_terminal (terminal, 0);
2139 if (!t)
2140 return Qnil;
2141 else
2142 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
2145 /* Return the number of supported colors. */
2146 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2147 Stty_display_color_cells, 0, 1, 0,
2148 doc: /* Return the number of colors supported by the tty device TERMINAL.
2150 TERMINAL can be a terminal object, a frame, or nil (meaning the
2151 selected frame's terminal). This function always returns 0 if
2152 TERMINAL does not refer to a text-only terminal. */)
2153 (Lisp_Object terminal)
2155 struct terminal *t = get_tty_terminal (terminal, 0);
2156 if (!t)
2157 return make_number (0);
2158 else
2159 return make_number (t->display_info.tty->TN_max_colors);
2162 #ifndef DOS_NT
2164 /* Declare here rather than in the function, as in the rest of Emacs,
2165 to work around an HPUX compiler bug (?). See
2166 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2167 static int default_max_colors;
2168 static int default_max_pairs;
2169 static int default_no_color_video;
2170 static char *default_orig_pair;
2171 static char *default_set_foreground;
2172 static char *default_set_background;
2174 /* Save or restore the default color-related capabilities of this
2175 terminal. */
2176 static void
2177 tty_default_color_capabilities (struct tty_display_info *tty, int save)
2180 if (save)
2182 xfree (default_orig_pair);
2183 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
2185 xfree (default_set_foreground);
2186 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
2187 : NULL;
2189 xfree (default_set_background);
2190 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
2191 : NULL;
2193 default_max_colors = tty->TN_max_colors;
2194 default_max_pairs = tty->TN_max_pairs;
2195 default_no_color_video = tty->TN_no_color_video;
2197 else
2199 tty->TS_orig_pair = default_orig_pair;
2200 tty->TS_set_foreground = default_set_foreground;
2201 tty->TS_set_background = default_set_background;
2202 tty->TN_max_colors = default_max_colors;
2203 tty->TN_max_pairs = default_max_pairs;
2204 tty->TN_no_color_video = default_no_color_video;
2208 /* Setup one of the standard tty color schemes according to MODE.
2209 MODE's value is generally the number of colors which we want to
2210 support; zero means set up for the default capabilities, the ones
2211 we saw at init_tty time; -1 means turn off color support. */
2212 static void
2213 tty_setup_colors (struct tty_display_info *tty, int mode)
2215 /* Canonicalize all negative values of MODE. */
2216 if (mode < -1)
2217 mode = -1;
2219 switch (mode)
2221 case -1: /* no colors at all */
2222 tty->TN_max_colors = 0;
2223 tty->TN_max_pairs = 0;
2224 tty->TN_no_color_video = 0;
2225 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2226 break;
2227 case 0: /* default colors, if any */
2228 default:
2229 tty_default_color_capabilities (tty, 0);
2230 break;
2231 case 8: /* 8 standard ANSI colors */
2232 tty->TS_orig_pair = "\033[0m";
2233 #ifdef TERMINFO
2234 tty->TS_set_foreground = "\033[3%p1%dm";
2235 tty->TS_set_background = "\033[4%p1%dm";
2236 #else
2237 tty->TS_set_foreground = "\033[3%dm";
2238 tty->TS_set_background = "\033[4%dm";
2239 #endif
2240 tty->TN_max_colors = 8;
2241 tty->TN_max_pairs = 64;
2242 tty->TN_no_color_video = 0;
2243 break;
2247 void
2248 set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
2250 Lisp_Object tem, val;
2251 Lisp_Object color_mode;
2252 int mode;
2253 Lisp_Object tty_color_mode_alist
2254 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
2256 tem = assq_no_quit (Qtty_color_mode, f->param_alist);
2257 val = CONSP (tem) ? XCDR (tem) : Qnil;
2259 if (INTEGERP (val))
2260 color_mode = val;
2261 else if (SYMBOLP (tty_color_mode_alist))
2263 tem = Fassq (val, Fsymbol_value (tty_color_mode_alist));
2264 color_mode = CONSP (tem) ? XCDR (tem) : Qnil;
2266 else
2267 color_mode = Qnil;
2269 mode = TYPE_RANGED_INTEGERP (int, color_mode) ? XINT (color_mode) : 0;
2271 if (mode != tty->previous_color_mode)
2273 Lisp_Object funsym = intern ("tty-set-up-initial-frame-faces");
2274 tty->previous_color_mode = mode;
2275 tty_setup_colors (tty , mode);
2276 /* This recomputes all the faces given the new color definitions. */
2277 safe_call (1, &funsym);
2281 #endif /* !DOS_NT */
2285 /* Return the tty display object specified by TERMINAL. */
2287 static struct terminal *
2288 get_tty_terminal (Lisp_Object terminal, int throw)
2290 struct terminal *t = get_terminal (terminal, throw);
2292 if (t && t->type != output_termcap && t->type != output_msdos_raw)
2294 if (throw)
2295 error ("Device %d is not a termcap terminal device", t->id);
2296 else
2297 return NULL;
2300 return t;
2303 /* Return an active termcap device that uses the tty device with the
2304 given name.
2306 This function ignores suspended devices.
2308 Returns NULL if the named terminal device is not opened. */
2310 struct terminal *
2311 get_named_tty (const char *name)
2313 struct terminal *t;
2315 if (!name)
2316 abort ();
2318 for (t = terminal_list; t; t = t->next_terminal)
2320 if ((t->type == output_termcap || t->type == output_msdos_raw)
2321 && !strcmp (t->display_info.tty->name, name)
2322 && TERMINAL_ACTIVE_P (t))
2323 return t;
2326 return 0;
2330 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2331 doc: /* Return the type of the tty device that TERMINAL uses.
2332 Returns nil if TERMINAL is not on a tty device.
2334 TERMINAL can be a terminal object, a frame, or nil (meaning the
2335 selected frame's terminal). */)
2336 (Lisp_Object terminal)
2338 struct terminal *t = get_terminal (terminal, 1);
2340 if (t->type != output_termcap && t->type != output_msdos_raw)
2341 return Qnil;
2343 if (t->display_info.tty->type)
2344 return build_string (t->display_info.tty->type);
2345 else
2346 return Qnil;
2349 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2350 doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2352 TERMINAL can be a terminal object, a frame, or nil (meaning the
2353 selected frame's terminal). This function always returns nil if
2354 TERMINAL is not on a tty device. */)
2355 (Lisp_Object terminal)
2357 struct terminal *t = get_terminal (terminal, 1);
2359 if ((t->type != output_termcap && t->type != output_msdos_raw)
2360 || strcmp (t->display_info.tty->name, DEV_TTY) != 0)
2361 return Qnil;
2362 else
2363 return Qt;
2366 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2367 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2368 This is used to override the terminfo data, for certain terminals that
2369 do not really do underlining, but say that they do. This function has
2370 no effect if used on a non-tty terminal.
2372 TERMINAL can be a terminal object, a frame or nil (meaning the
2373 selected frame's terminal). This function always returns nil if
2374 TERMINAL does not refer to a text-only terminal. */)
2375 (Lisp_Object terminal)
2377 struct terminal *t = get_terminal (terminal, 1);
2379 if (t->type == output_termcap)
2380 t->display_info.tty->TS_enter_underline_mode = 0;
2381 return Qnil;
2386 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2387 doc: /* Suspend the terminal device TTY.
2389 The device is restored to its default state, and Emacs ceases all
2390 access to the tty device. Frames that use the device are not deleted,
2391 but input is not read from them and if they change, their display is
2392 not updated.
2394 TTY may be a terminal object, a frame, or nil for the terminal device
2395 of the currently selected frame.
2397 This function runs `suspend-tty-functions' after suspending the
2398 device. The functions are run with one arg, the id of the suspended
2399 terminal device.
2401 `suspend-tty' does nothing if it is called on a device that is already
2402 suspended.
2404 A suspended tty may be resumed by calling `resume-tty' on it. */)
2405 (Lisp_Object tty)
2407 struct terminal *t = get_tty_terminal (tty, 1);
2408 FILE *f;
2410 if (!t)
2411 error ("Unknown tty device");
2413 f = t->display_info.tty->input;
2415 if (f)
2417 /* First run `suspend-tty-functions' and then clean up the tty
2418 state because `suspend-tty-functions' might need to change
2419 the tty state. */
2420 Lisp_Object args[2];
2421 args[0] = intern ("suspend-tty-functions");
2422 XSETTERMINAL (args[1], t);
2423 Frun_hook_with_args (2, args);
2425 reset_sys_modes (t->display_info.tty);
2426 delete_keyboard_wait_descriptor (fileno (f));
2428 #ifndef MSDOS
2429 fclose (f);
2430 if (f != t->display_info.tty->output)
2431 fclose (t->display_info.tty->output);
2432 #endif
2434 t->display_info.tty->input = 0;
2435 t->display_info.tty->output = 0;
2437 if (FRAMEP (t->display_info.tty->top_frame))
2438 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2442 /* Clear display hooks to prevent further output. */
2443 clear_tty_hooks (t);
2445 return Qnil;
2448 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2449 doc: /* Resume the previously suspended terminal device TTY.
2450 The terminal is opened and reinitialized. Frames that are on the
2451 suspended terminal are revived.
2453 It is an error to resume a terminal while another terminal is active
2454 on the same device.
2456 This function runs `resume-tty-functions' after resuming the terminal.
2457 The functions are run with one arg, the id of the resumed terminal
2458 device.
2460 `resume-tty' does nothing if it is called on a device that is not
2461 suspended.
2463 TTY may be a terminal object, a frame, or nil (meaning the selected
2464 frame's terminal). */)
2465 (Lisp_Object tty)
2467 struct terminal *t = get_tty_terminal (tty, 1);
2468 int fd;
2470 if (!t)
2471 error ("Unknown tty device");
2473 if (!t->display_info.tty->input)
2475 if (get_named_tty (t->display_info.tty->name))
2476 error ("Cannot resume display while another display is active on the same device");
2478 #ifdef MSDOS
2479 t->display_info.tty->output = stdout;
2480 t->display_info.tty->input = stdin;
2481 #else /* !MSDOS */
2482 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2484 if (fd == -1)
2485 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2487 if (strcmp (t->display_info.tty->name, DEV_TTY))
2488 dissociate_if_controlling_tty (fd);
2490 t->display_info.tty->output = fdopen (fd, "w+");
2491 t->display_info.tty->input = t->display_info.tty->output;
2492 #endif
2494 add_keyboard_wait_descriptor (fd);
2496 if (FRAMEP (t->display_info.tty->top_frame))
2498 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2499 int width, height;
2500 int old_height = FRAME_COLS (f);
2501 int old_width = FRAME_LINES (f);
2503 /* Check if terminal/window size has changed while the frame
2504 was suspended. */
2505 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2506 if (width != old_width || height != old_height)
2507 change_frame_size (f, height, width, 0, 0, 0);
2508 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2511 set_tty_hooks (t);
2512 init_sys_modes (t->display_info.tty);
2515 /* Run `resume-tty-functions'. */
2516 Lisp_Object args[2];
2517 args[0] = intern ("resume-tty-functions");
2518 XSETTERMINAL (args[1], t);
2519 Frun_hook_with_args (2, args);
2523 set_tty_hooks (t);
2525 return Qnil;
2529 /***********************************************************************
2530 Mouse
2531 ***********************************************************************/
2533 #ifdef HAVE_GPM
2535 #ifndef HAVE_WINDOW_SYSTEM
2536 void
2537 term_mouse_moveto (int x, int y)
2539 /* TODO: how to set mouse position?
2540 const char *name;
2541 int fd;
2542 name = (const char *) ttyname (0);
2543 fd = open (name, O_WRONLY);
2544 SOME_FUNCTION (x, y, fd);
2545 close (fd);
2546 last_mouse_x = x;
2547 last_mouse_y = y; */
2549 #endif /* HAVE_WINDOW_SYSTEM */
2551 /* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2552 void
2553 tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row,
2554 int start_hpos, int end_hpos,
2555 enum draw_glyphs_face draw)
2557 int nglyphs = end_hpos - start_hpos;
2558 struct frame *f = XFRAME (WINDOW_FRAME (w));
2559 struct tty_display_info *tty = FRAME_TTY (f);
2560 int face_id = tty->mouse_highlight.mouse_face_face_id;
2561 int save_x, save_y, pos_x, pos_y;
2563 if (end_hpos >= row->used[TEXT_AREA])
2564 nglyphs = row->used[TEXT_AREA] - start_hpos;
2566 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2567 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos + WINDOW_LEFT_EDGE_X (w);
2569 /* Save current cursor co-ordinates. */
2570 save_y = curY (tty);
2571 save_x = curX (tty);
2572 cursor_to (f, pos_y, pos_x);
2574 if (draw == DRAW_MOUSE_FACE)
2575 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2576 nglyphs, face_id);
2577 else if (draw == DRAW_NORMAL_TEXT)
2578 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
2580 cursor_to (f, save_y, save_x);
2583 static int
2584 term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2586 /* Has the mouse moved off the glyph it was on at the last sighting? */
2587 if (event->x != last_mouse_x || event->y != last_mouse_y)
2589 frame->mouse_moved = 1;
2590 note_mouse_highlight (frame, event->x, event->y);
2591 /* Remember which glyph we're now on. */
2592 last_mouse_x = event->x;
2593 last_mouse_y = event->y;
2594 return 1;
2596 return 0;
2599 /* Return the current position of the mouse.
2601 Set *f to the frame the mouse is in, or zero if the mouse is in no
2602 Emacs frame. If it is set to zero, all the other arguments are
2603 garbage.
2605 Set *bar_window to Qnil, and *x and *y to the column and
2606 row of the character cell the mouse is over.
2608 Set *timeptr to the time the mouse was at the returned position.
2610 This clears mouse_moved until the next motion
2611 event arrives. */
2612 static void
2613 term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2614 enum scroll_bar_part *part, Lisp_Object *x,
2615 Lisp_Object *y, Time *timeptr)
2617 struct timeval now;
2618 Time sec, usec;
2620 *fp = SELECTED_FRAME ();
2621 (*fp)->mouse_moved = 0;
2623 *bar_window = Qnil;
2624 *part = 0;
2626 XSETINT (*x, last_mouse_x);
2627 XSETINT (*y, last_mouse_y);
2628 gettimeofday(&now, 0);
2629 sec = now.tv_sec;
2630 usec = now.tv_usec;
2631 *timeptr = (sec * 1000) + (usec / 1000);
2634 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2636 If the event is a button press, then note that we have grabbed
2637 the mouse. */
2639 static Lisp_Object
2640 term_mouse_click (struct input_event *result, Gpm_Event *event,
2641 struct frame *f)
2643 struct timeval now;
2644 int i, j;
2646 result->kind = GPM_CLICK_EVENT;
2647 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2649 if (event->buttons & j) {
2650 result->code = i; /* button number */
2651 break;
2654 gettimeofday(&now, 0);
2655 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2657 if (event->type & GPM_UP)
2658 result->modifiers = up_modifier;
2659 else if (event->type & GPM_DOWN)
2660 result->modifiers = down_modifier;
2661 else
2662 result->modifiers = 0;
2664 if (event->type & GPM_SINGLE)
2665 result->modifiers |= click_modifier;
2667 if (event->type & GPM_DOUBLE)
2668 result->modifiers |= double_modifier;
2670 if (event->type & GPM_TRIPLE)
2671 result->modifiers |= triple_modifier;
2673 if (event->type & GPM_DRAG)
2674 result->modifiers |= drag_modifier;
2676 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
2678 /* 1 << KG_SHIFT */
2679 if (event->modifiers & (1 << 0))
2680 result->modifiers |= shift_modifier;
2682 /* 1 << KG_CTRL */
2683 if (event->modifiers & (1 << 2))
2684 result->modifiers |= ctrl_modifier;
2686 /* 1 << KG_ALT || KG_ALTGR */
2687 if (event->modifiers & (1 << 3)
2688 || event->modifiers & (1 << 1))
2689 result->modifiers |= meta_modifier;
2692 XSETINT (result->x, event->x);
2693 XSETINT (result->y, event->y);
2694 XSETFRAME (result->frame_or_window, f);
2695 result->arg = Qnil;
2696 return Qnil;
2700 handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
2702 struct frame *f = XFRAME (tty->top_frame);
2703 struct input_event ie;
2704 int do_help = 0;
2705 int count = 0;
2707 EVENT_INIT (ie);
2708 ie.kind = NO_EVENT;
2709 ie.arg = Qnil;
2711 if (event->type & (GPM_MOVE | GPM_DRAG)) {
2712 previous_help_echo_string = help_echo_string;
2713 help_echo_string = Qnil;
2715 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
2717 if (!term_mouse_movement (f, event))
2718 help_echo_string = previous_help_echo_string;
2720 /* If the contents of the global variable help_echo_string
2721 has changed, generate a HELP_EVENT. */
2722 if (!NILP (help_echo_string)
2723 || !NILP (previous_help_echo_string))
2724 do_help = 1;
2726 goto done;
2728 else {
2729 f->mouse_moved = 0;
2730 term_mouse_click (&ie, event, f);
2733 done:
2734 if (ie.kind != NO_EVENT)
2736 kbd_buffer_store_event_hold (&ie, hold_quit);
2737 count++;
2740 if (do_help
2741 && !(hold_quit && hold_quit->kind != NO_EVENT))
2743 Lisp_Object frame;
2745 if (f)
2746 XSETFRAME (frame, f);
2747 else
2748 frame = Qnil;
2750 gen_help_event (help_echo_string, frame, help_echo_window,
2751 help_echo_object, help_echo_pos);
2752 count++;
2755 return count;
2758 DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
2759 0, 0, 0,
2760 doc: /* Open a connection to Gpm.
2761 Gpm-mouse can only be activated for one tty at a time. */)
2762 (void)
2764 struct frame *f = SELECTED_FRAME ();
2765 struct tty_display_info *tty
2766 = ((f)->output_method == output_termcap
2767 ? (f)->terminal->display_info.tty : NULL);
2768 Gpm_Connect connection;
2770 if (!tty)
2771 error ("Gpm-mouse only works in the GNU/Linux console");
2772 if (gpm_tty == tty)
2773 return Qnil; /* Already activated, nothing to do. */
2774 if (gpm_tty)
2775 error ("Gpm-mouse can only be activated for one tty at a time");
2777 connection.eventMask = ~0;
2778 connection.defaultMask = ~GPM_HARD;
2779 connection.maxMod = ~0;
2780 connection.minMod = 0;
2781 gpm_zerobased = 1;
2783 if (Gpm_Open (&connection, 0) < 0)
2784 error ("Gpm-mouse failed to connect to the gpm daemon");
2785 else
2787 gpm_tty = tty;
2788 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2789 to generate SIGIOs. Apparently we need to call reset_sys_modes
2790 before calling init_sys_modes. */
2791 reset_sys_modes (tty);
2792 init_sys_modes (tty);
2793 add_gpm_wait_descriptor (gpm_fd);
2794 return Qnil;
2798 void
2799 close_gpm (int fd)
2801 if (fd >= 0)
2802 delete_gpm_wait_descriptor (fd);
2803 while (Gpm_Close()); /* close all the stack */
2804 gpm_tty = NULL;
2807 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
2808 0, 0, 0,
2809 doc: /* Close a connection to Gpm. */)
2810 (void)
2812 struct frame *f = SELECTED_FRAME ();
2813 struct tty_display_info *tty
2814 = ((f)->output_method == output_termcap
2815 ? (f)->terminal->display_info.tty : NULL);
2817 if (!tty || gpm_tty != tty)
2818 return Qnil; /* Not activated on this terminal, nothing to do. */
2820 close_gpm (gpm_fd);
2821 return Qnil;
2823 #endif /* HAVE_GPM */
2826 #ifndef MSDOS
2827 /***********************************************************************
2828 Initialization
2829 ***********************************************************************/
2831 /* Initialize the tty-dependent part of frame F. The frame must
2832 already have its device initialized. */
2834 void
2835 create_tty_output (struct frame *f)
2837 struct tty_output *t;
2839 if (! FRAME_TERMCAP_P (f))
2840 abort ();
2842 t = xmalloc (sizeof (struct tty_output));
2843 memset (t, 0, sizeof (struct tty_output));
2845 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
2847 f->output_data.tty = t;
2850 /* Delete frame F's face cache, and its tty-dependent part. */
2852 static void
2853 tty_free_frame_resources (struct frame *f)
2855 if (! FRAME_TERMCAP_P (f))
2856 abort ();
2858 if (FRAME_FACE_CACHE (f))
2859 free_frame_faces (f);
2861 xfree (f->output_data.tty);
2864 #else /* MSDOS */
2866 /* Delete frame F's face cache. */
2868 static void
2869 tty_free_frame_resources (struct frame *f)
2871 if (! FRAME_TERMCAP_P (f) && ! FRAME_MSDOS_P (f))
2872 abort ();
2874 if (FRAME_FACE_CACHE (f))
2875 free_frame_faces (f);
2877 #endif /* MSDOS */
2879 /* Reset the hooks in TERMINAL. */
2881 static void
2882 clear_tty_hooks (struct terminal *terminal)
2884 terminal->rif = 0;
2885 terminal->cursor_to_hook = 0;
2886 terminal->raw_cursor_to_hook = 0;
2887 terminal->clear_to_end_hook = 0;
2888 terminal->clear_frame_hook = 0;
2889 terminal->clear_end_of_line_hook = 0;
2890 terminal->ins_del_lines_hook = 0;
2891 terminal->insert_glyphs_hook = 0;
2892 terminal->write_glyphs_hook = 0;
2893 terminal->delete_glyphs_hook = 0;
2894 terminal->ring_bell_hook = 0;
2895 terminal->reset_terminal_modes_hook = 0;
2896 terminal->set_terminal_modes_hook = 0;
2897 terminal->update_begin_hook = 0;
2898 terminal->update_end_hook = 0;
2899 terminal->set_terminal_window_hook = 0;
2900 terminal->mouse_position_hook = 0;
2901 terminal->frame_rehighlight_hook = 0;
2902 terminal->frame_raise_lower_hook = 0;
2903 terminal->fullscreen_hook = 0;
2904 terminal->set_vertical_scroll_bar_hook = 0;
2905 terminal->condemn_scroll_bars_hook = 0;
2906 terminal->redeem_scroll_bar_hook = 0;
2907 terminal->judge_scroll_bars_hook = 0;
2908 terminal->read_socket_hook = 0;
2909 terminal->frame_up_to_date_hook = 0;
2911 /* Leave these two set, or suspended frames are not deleted
2912 correctly. */
2913 terminal->delete_frame_hook = &tty_free_frame_resources;
2914 terminal->delete_terminal_hook = &delete_tty;
2917 /* Initialize hooks in TERMINAL with the values needed for a tty. */
2919 static void
2920 set_tty_hooks (struct terminal *terminal)
2922 terminal->rif = 0; /* ttys don't support window-based redisplay. */
2924 terminal->cursor_to_hook = &tty_cursor_to;
2925 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
2927 terminal->clear_to_end_hook = &tty_clear_to_end;
2928 terminal->clear_frame_hook = &tty_clear_frame;
2929 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
2931 terminal->ins_del_lines_hook = &tty_ins_del_lines;
2933 terminal->insert_glyphs_hook = &tty_insert_glyphs;
2934 terminal->write_glyphs_hook = &tty_write_glyphs;
2935 terminal->delete_glyphs_hook = &tty_delete_glyphs;
2937 terminal->ring_bell_hook = &tty_ring_bell;
2939 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
2940 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
2941 terminal->update_begin_hook = 0; /* Not needed. */
2942 terminal->update_end_hook = &tty_update_end;
2943 terminal->set_terminal_window_hook = &tty_set_terminal_window;
2945 terminal->mouse_position_hook = 0; /* Not needed. */
2946 terminal->frame_rehighlight_hook = 0; /* Not needed. */
2947 terminal->frame_raise_lower_hook = 0; /* Not needed. */
2949 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
2950 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
2951 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
2952 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
2954 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
2955 terminal->frame_up_to_date_hook = 0; /* Not needed. */
2957 terminal->delete_frame_hook = &tty_free_frame_resources;
2958 terminal->delete_terminal_hook = &delete_tty;
2961 /* Drop the controlling terminal if fd is the same device. */
2962 static void
2963 dissociate_if_controlling_tty (int fd)
2965 #ifndef DOS_NT
2966 int pgid = tcgetpgrp (fd); /* If tcgetpgrp succeeds, fd is the ctty. */
2967 if (pgid != -1)
2969 #if defined (USG5)
2970 setpgrp ();
2971 no_controlling_tty = 1;
2972 #elif defined (CYGWIN)
2973 setsid ();
2974 no_controlling_tty = 1;
2975 #else
2976 #ifdef TIOCNOTTY /* Try BSD ioctls. */
2977 sigblock (sigmask (SIGTTOU));
2978 fd = emacs_open (DEV_TTY, O_RDWR, 0);
2979 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
2981 no_controlling_tty = 1;
2983 if (fd != -1)
2984 emacs_close (fd);
2985 sigunblock (sigmask (SIGTTOU));
2986 #else
2987 /* Unknown system. */
2988 croak ();
2989 #endif /* ! TIOCNOTTY */
2990 #endif /* ! USG */
2992 #endif /* !DOS_NT */
2995 /* Create a termcap display on the tty device with the given name and
2996 type.
2998 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
2999 Otherwise NAME should be a path to the tty device file,
3000 e.g. "/dev/pts/7".
3002 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3004 If MUST_SUCCEED is true, then all errors are fatal. */
3006 struct terminal *
3007 init_tty (const char *name, const char *terminal_type, int must_succeed)
3009 char *area = NULL;
3010 char **address = &area;
3011 int buffer_size = 4096;
3012 int status;
3013 struct tty_display_info *tty = NULL;
3014 struct terminal *terminal = NULL;
3015 int ctty = 0; /* 1 if asked to open controlling tty. */
3017 if (!terminal_type)
3018 maybe_fatal (must_succeed, 0,
3019 "Unknown terminal type",
3020 "Unknown terminal type");
3022 if (name == NULL)
3023 name = DEV_TTY;
3024 if (!strcmp (name, DEV_TTY))
3025 ctty = 1;
3027 /* If we already have a terminal on the given device, use that. If
3028 all such terminals are suspended, create a new one instead. */
3029 /* XXX Perhaps this should be made explicit by having init_tty
3030 always create a new terminal and separating terminal and frame
3031 creation on Lisp level. */
3032 terminal = get_named_tty (name);
3033 if (terminal)
3034 return terminal;
3036 terminal = create_terminal ();
3037 #ifdef MSDOS
3038 if (been_here > 0)
3039 maybe_fatal (0, 0, "Attempt to create another terminal %s", "",
3040 name, "");
3041 been_here = 1;
3042 tty = &the_only_display_info;
3043 #else
3044 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
3045 #endif
3046 memset (tty, 0, sizeof (struct tty_display_info));
3047 tty->next = tty_list;
3048 tty_list = tty;
3050 terminal->type = output_termcap;
3051 terminal->display_info.tty = tty;
3052 tty->terminal = terminal;
3054 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3055 Wcm_clear (tty);
3057 encode_terminal_src_size = 0;
3058 encode_terminal_dst_size = 0;
3061 #ifndef DOS_NT
3062 set_tty_hooks (terminal);
3065 int fd;
3066 FILE *file;
3068 #ifdef O_IGNORE_CTTY
3069 if (!ctty)
3070 /* Open the terminal device. Don't recognize it as our
3071 controlling terminal, and don't make it the controlling tty
3072 if we don't have one at the moment. */
3073 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
3074 else
3075 #endif /* O_IGNORE_CTTY */
3076 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3077 defined on Hurd. On other systems, we need to explicitly
3078 dissociate ourselves from the controlling tty when we want to
3079 open a frame on the same terminal. */
3080 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
3082 tty->name = xstrdup (name);
3083 terminal->name = xstrdup (name);
3085 if (fd < 0)
3086 maybe_fatal (must_succeed, terminal,
3087 "Could not open file: %s",
3088 "Could not open file: %s",
3089 name);
3090 if (!isatty (fd))
3092 close (fd);
3093 maybe_fatal (must_succeed, terminal,
3094 "Not a tty device: %s",
3095 "Not a tty device: %s",
3096 name);
3099 #ifndef O_IGNORE_CTTY
3100 if (!ctty)
3101 dissociate_if_controlling_tty (fd);
3102 #endif
3104 file = fdopen (fd, "w+");
3105 tty->input = file;
3106 tty->output = file;
3109 tty->type = xstrdup (terminal_type);
3111 add_keyboard_wait_descriptor (fileno (tty->input));
3113 Wcm_clear (tty);
3115 tty->termcap_term_buffer = (char *) xmalloc (buffer_size);
3117 /* On some systems, tgetent tries to access the controlling
3118 terminal. */
3119 sigblock (sigmask (SIGTTOU));
3120 status = tgetent (tty->termcap_term_buffer, terminal_type);
3121 sigunblock (sigmask (SIGTTOU));
3123 if (status < 0)
3125 #ifdef TERMINFO
3126 maybe_fatal (must_succeed, terminal,
3127 "Cannot open terminfo database file",
3128 "Cannot open terminfo database file");
3129 #else
3130 maybe_fatal (must_succeed, terminal,
3131 "Cannot open termcap database file",
3132 "Cannot open termcap database file");
3133 #endif
3135 if (status == 0)
3137 maybe_fatal (must_succeed, terminal,
3138 "Terminal type %s is not defined",
3139 "Terminal type %s is not defined.\n\
3140 If that is not the actual type of terminal you have,\n\
3141 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3142 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3143 #ifdef TERMINFO
3144 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3145 #else
3146 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3147 #endif
3148 terminal_type);
3151 #ifndef TERMINFO
3152 if (strlen (tty->termcap_term_buffer) >= buffer_size)
3153 abort ();
3154 buffer_size = strlen (tty->termcap_term_buffer);
3155 #endif
3156 tty->termcap_strings_buffer = area = (char *) xmalloc (buffer_size);
3157 tty->TS_ins_line = tgetstr ("al", address);
3158 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3159 tty->TS_bell = tgetstr ("bl", address);
3160 BackTab (tty) = tgetstr ("bt", address);
3161 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3162 tty->TS_clr_line = tgetstr ("ce", address);
3163 tty->TS_clr_frame = tgetstr ("cl", address);
3164 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3165 AbsPosition (tty) = tgetstr ("cm", address);
3166 CR (tty) = tgetstr ("cr", address);
3167 tty->TS_set_scroll_region = tgetstr ("cs", address);
3168 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
3169 RowPosition (tty) = tgetstr ("cv", address);
3170 tty->TS_del_char = tgetstr ("dc", address);
3171 tty->TS_del_multi_chars = tgetstr ("DC", address);
3172 tty->TS_del_line = tgetstr ("dl", address);
3173 tty->TS_del_multi_lines = tgetstr ("DL", address);
3174 tty->TS_delete_mode = tgetstr ("dm", address);
3175 tty->TS_end_delete_mode = tgetstr ("ed", address);
3176 tty->TS_end_insert_mode = tgetstr ("ei", address);
3177 Home (tty) = tgetstr ("ho", address);
3178 tty->TS_ins_char = tgetstr ("ic", address);
3179 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3180 tty->TS_insert_mode = tgetstr ("im", address);
3181 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3182 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3183 tty->TS_keypad_mode = tgetstr ("ks", address);
3184 LastLine (tty) = tgetstr ("ll", address);
3185 Right (tty) = tgetstr ("nd", address);
3186 Down (tty) = tgetstr ("do", address);
3187 if (!Down (tty))
3188 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
3189 if (tgetflag ("bs"))
3190 Left (tty) = "\b"; /* can't possibly be longer! */
3191 else /* (Actually, "bs" is obsolete...) */
3192 Left (tty) = tgetstr ("le", address);
3193 if (!Left (tty))
3194 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
3195 tty->TS_pad_char = tgetstr ("pc", address);
3196 tty->TS_repeat = tgetstr ("rp", address);
3197 tty->TS_end_standout_mode = tgetstr ("se", address);
3198 tty->TS_fwd_scroll = tgetstr ("sf", address);
3199 tty->TS_standout_mode = tgetstr ("so", address);
3200 tty->TS_rev_scroll = tgetstr ("sr", address);
3201 tty->Wcm->cm_tab = tgetstr ("ta", address);
3202 tty->TS_end_termcap_modes = tgetstr ("te", address);
3203 tty->TS_termcap_modes = tgetstr ("ti", address);
3204 Up (tty) = tgetstr ("up", address);
3205 tty->TS_visible_bell = tgetstr ("vb", address);
3206 tty->TS_cursor_normal = tgetstr ("ve", address);
3207 tty->TS_cursor_visible = tgetstr ("vs", address);
3208 tty->TS_cursor_invisible = tgetstr ("vi", address);
3209 tty->TS_set_window = tgetstr ("wi", address);
3211 tty->TS_enter_underline_mode = tgetstr ("us", address);
3212 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3213 tty->TS_enter_bold_mode = tgetstr ("md", address);
3214 tty->TS_enter_italic_mode = tgetstr ("ZH", address);
3215 tty->TS_enter_dim_mode = tgetstr ("mh", address);
3216 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3217 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3218 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3219 tty->TS_exit_attribute_mode = tgetstr ("me", address);
3221 MultiUp (tty) = tgetstr ("UP", address);
3222 MultiDown (tty) = tgetstr ("DO", address);
3223 MultiLeft (tty) = tgetstr ("LE", address);
3224 MultiRight (tty) = tgetstr ("RI", address);
3226 /* SVr4/ANSI color support. If "op" isn't available, don't support
3227 color because we can't switch back to the default foreground and
3228 background. */
3229 tty->TS_orig_pair = tgetstr ("op", address);
3230 if (tty->TS_orig_pair)
3232 tty->TS_set_foreground = tgetstr ("AF", address);
3233 tty->TS_set_background = tgetstr ("AB", address);
3234 if (!tty->TS_set_foreground)
3236 /* SVr4. */
3237 tty->TS_set_foreground = tgetstr ("Sf", address);
3238 tty->TS_set_background = tgetstr ("Sb", address);
3241 tty->TN_max_colors = tgetnum ("Co");
3242 tty->TN_max_pairs = tgetnum ("pa");
3244 tty->TN_no_color_video = tgetnum ("NC");
3245 if (tty->TN_no_color_video == -1)
3246 tty->TN_no_color_video = 0;
3249 tty_default_color_capabilities (tty, 1);
3251 MagicWrap (tty) = tgetflag ("xn");
3252 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3253 the former flag imply the latter. */
3254 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
3255 terminal->memory_below_frame = tgetflag ("db");
3256 tty->TF_hazeltine = tgetflag ("hz");
3257 terminal->must_write_spaces = tgetflag ("in");
3258 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3259 tty->TF_insmode_motion = tgetflag ("mi");
3260 tty->TF_standout_motion = tgetflag ("ms");
3261 tty->TF_underscore = tgetflag ("ul");
3262 tty->TF_teleray = tgetflag ("xt");
3264 #else /* DOS_NT */
3265 #ifdef WINDOWSNT
3267 struct frame *f = XFRAME (selected_frame);
3269 initialize_w32_display (terminal);
3271 FrameRows (tty) = FRAME_LINES (f);
3272 FrameCols (tty) = FRAME_COLS (f);
3273 tty->specified_window = FRAME_LINES (f);
3275 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3276 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
3277 terminal->char_ins_del_ok = 1;
3278 baud_rate = 19200;
3280 #else /* MSDOS */
3282 int height, width;
3283 if (strcmp (terminal_type, "internal") == 0)
3284 terminal->type = output_msdos_raw;
3285 initialize_msdos_display (terminal);
3287 get_tty_size (fileno (tty->input), &width, &height);
3288 FrameCols (tty) = width;
3289 FrameRows (tty) = height;
3290 terminal->char_ins_del_ok = 0;
3291 init_baud_rate (fileno (tty->input));
3293 #endif /* MSDOS */
3294 tty->output = stdout;
3295 tty->input = stdin;
3296 /* The following two are inaccessible from w32console.c. */
3297 terminal->delete_frame_hook = &tty_free_frame_resources;
3298 terminal->delete_terminal_hook = &delete_tty;
3300 tty->name = xstrdup (name);
3301 terminal->name = xstrdup (name);
3302 tty->type = xstrdup (terminal_type);
3304 add_keyboard_wait_descriptor (0);
3306 tty->delete_in_insert_mode = 1;
3308 UseTabs (tty) = 0;
3309 terminal->scroll_region_ok = 0;
3311 /* Seems to insert lines when it's not supposed to, messing up the
3312 display. In doing a trace, it didn't seem to be called much, so I
3313 don't think we're losing anything by turning it off. */
3314 terminal->line_ins_del_ok = 0;
3316 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
3317 #endif /* DOS_NT */
3319 #ifdef HAVE_GPM
3320 terminal->mouse_position_hook = term_mouse_position;
3321 tty->mouse_highlight.mouse_face_window = Qnil;
3322 #endif
3324 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3325 init_kboard (terminal->kboard);
3326 KVAR (terminal->kboard, Vwindow_system) = Qnil;
3327 terminal->kboard->next_kboard = all_kboards;
3328 all_kboards = terminal->kboard;
3329 terminal->kboard->reference_count++;
3330 /* Don't let the initial kboard remain current longer than necessary.
3331 That would cause problems if a file loaded on startup tries to
3332 prompt in the mini-buffer. */
3333 if (current_kboard == initial_kboard)
3334 current_kboard = terminal->kboard;
3335 #ifndef DOS_NT
3336 term_get_fkeys (address, terminal->kboard);
3338 /* Get frame size from system, or else from termcap. */
3340 int height, width;
3341 get_tty_size (fileno (tty->input), &width, &height);
3342 FrameCols (tty) = width;
3343 FrameRows (tty) = height;
3346 if (FrameCols (tty) <= 0)
3347 FrameCols (tty) = tgetnum ("co");
3348 if (FrameRows (tty) <= 0)
3349 FrameRows (tty) = tgetnum ("li");
3351 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
3352 maybe_fatal (must_succeed, terminal,
3353 "Screen size %dx%d is too small",
3354 "Screen size %dx%d is too small",
3355 FrameCols (tty), FrameRows (tty));
3357 TabWidth (tty) = tgetnum ("tw");
3359 if (!tty->TS_bell)
3360 tty->TS_bell = "\07";
3362 if (!tty->TS_fwd_scroll)
3363 tty->TS_fwd_scroll = Down (tty);
3365 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
3367 if (TabWidth (tty) < 0)
3368 TabWidth (tty) = 8;
3370 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3371 and newer termcap doc does not seem to say there is a default.
3372 if (!tty->Wcm->cm_tab)
3373 tty->Wcm->cm_tab = "\t";
3376 /* We don't support standout modes that use `magic cookies', so
3377 turn off any that do. */
3378 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
3380 tty->TS_standout_mode = 0;
3381 tty->TS_end_standout_mode = 0;
3383 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
3385 tty->TS_enter_underline_mode = 0;
3386 tty->TS_exit_underline_mode = 0;
3389 /* If there's no standout mode, try to use underlining instead. */
3390 if (tty->TS_standout_mode == 0)
3392 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3393 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
3396 /* If no `se' string, try using a `me' string instead.
3397 If that fails, we can't use standout mode at all. */
3398 if (tty->TS_end_standout_mode == 0)
3400 char *s = tgetstr ("me", address);
3401 if (s != 0)
3402 tty->TS_end_standout_mode = s;
3403 else
3404 tty->TS_standout_mode = 0;
3407 if (tty->TF_teleray)
3409 tty->Wcm->cm_tab = 0;
3410 /* We can't support standout mode, because it uses magic cookies. */
3411 tty->TS_standout_mode = 0;
3412 /* But that means we cannot rely on ^M to go to column zero! */
3413 CR (tty) = 0;
3414 /* LF can't be trusted either -- can alter hpos */
3415 /* if move at column 0 thru a line with TS_standout_mode */
3416 Down (tty) = 0;
3419 tty->specified_window = FrameRows (tty);
3421 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3423 maybe_fatal (must_succeed, terminal,
3424 "Terminal type \"%s\" is not powerful enough to run Emacs",
3425 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3426 It lacks the ability to position the cursor.\n\
3427 If that is not the actual type of terminal you have,\n\
3428 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3429 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3430 # ifdef TERMINFO
3431 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3432 # else /* TERMCAP */
3433 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3434 # endif /* TERMINFO */
3435 terminal_type);
3438 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
3439 maybe_fatal (must_succeed, terminal,
3440 "Could not determine the frame size",
3441 "Could not determine the frame size");
3443 tty->delete_in_insert_mode
3444 = tty->TS_delete_mode && tty->TS_insert_mode
3445 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
3447 tty->se_is_so = (tty->TS_standout_mode
3448 && tty->TS_end_standout_mode
3449 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
3451 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
3453 terminal->scroll_region_ok
3454 = (tty->Wcm->cm_abs
3455 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
3457 terminal->line_ins_del_ok
3458 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3459 && (tty->TS_del_line || tty->TS_del_multi_lines))
3460 || (terminal->scroll_region_ok
3461 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
3463 terminal->char_ins_del_ok
3464 = ((tty->TS_ins_char || tty->TS_insert_mode
3465 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3466 && (tty->TS_del_char || tty->TS_del_multi_chars));
3468 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
3470 init_baud_rate (fileno (tty->input));
3472 #endif /* not DOS_NT */
3474 /* Init system terminal modes (RAW or CBREAK, etc.). */
3475 init_sys_modes (tty);
3477 return terminal;
3481 static void
3482 vfatal (const char *str, va_list ap)
3484 fprintf (stderr, "emacs: ");
3485 vfprintf (stderr, str, ap);
3486 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
3487 fprintf (stderr, "\n");
3488 fflush (stderr);
3489 exit (1);
3493 /* Auxiliary error-handling function for init_tty.
3494 Delete TERMINAL, then call error or fatal with str1 or str2,
3495 respectively, according to whether MUST_SUCCEED is zero or not. */
3497 static void
3498 maybe_fatal (int must_succeed, struct terminal *terminal,
3499 const char *str1, const char *str2, ...)
3501 va_list ap;
3502 va_start (ap, str2);
3503 if (terminal)
3504 delete_tty (terminal);
3506 if (must_succeed)
3507 vfatal (str2, ap);
3508 else
3509 verror (str1, ap);
3511 va_end (ap);
3512 abort ();
3515 void
3516 fatal (const char *str, ...)
3518 va_list ap;
3519 va_start (ap, str);
3520 vfatal (str, ap);
3521 va_end (ap);
3526 /* Delete the given tty terminal, closing all frames on it. */
3528 static void
3529 delete_tty (struct terminal *terminal)
3531 struct tty_display_info *tty;
3533 /* Protect against recursive calls. delete_frame in
3534 delete_terminal calls us back when it deletes our last frame. */
3535 if (!terminal->name)
3536 return;
3538 if (terminal->type != output_termcap)
3539 abort ();
3541 tty = terminal->display_info.tty;
3543 if (tty == tty_list)
3544 tty_list = tty->next;
3545 else
3547 struct tty_display_info *p;
3548 for (p = tty_list; p && p->next != tty; p = p->next)
3551 if (! p)
3552 /* This should not happen. */
3553 abort ();
3555 p->next = tty->next;
3556 tty->next = 0;
3559 /* reset_sys_modes needs a valid device, so this call needs to be
3560 before delete_terminal. */
3561 reset_sys_modes (tty);
3563 delete_terminal (terminal);
3565 xfree (tty->name);
3566 xfree (tty->type);
3568 if (tty->input)
3570 delete_keyboard_wait_descriptor (fileno (tty->input));
3571 if (tty->input != stdin)
3572 fclose (tty->input);
3574 if (tty->output && tty->output != stdout && tty->output != tty->input)
3575 fclose (tty->output);
3576 if (tty->termscript)
3577 fclose (tty->termscript);
3579 xfree (tty->old_tty);
3580 xfree (tty->Wcm);
3581 xfree (tty->termcap_strings_buffer);
3582 xfree (tty->termcap_term_buffer);
3584 memset (tty, 0, sizeof (struct tty_display_info));
3585 xfree (tty);
3590 /* Mark the pointers in the tty_display_info objects.
3591 Called by Fgarbage_collect. */
3593 void
3594 mark_ttys (void)
3596 struct tty_display_info *tty;
3598 for (tty = tty_list; tty; tty = tty->next)
3599 mark_object (tty->top_frame);
3604 void
3605 syms_of_term (void)
3607 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo,
3608 doc: /* Non-nil means the system uses terminfo rather than termcap.
3609 This variable can be used by terminal emulator packages. */);
3610 #ifdef TERMINFO
3611 system_uses_terminfo = 1;
3612 #else
3613 system_uses_terminfo = 0;
3614 #endif
3616 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions,
3617 doc: /* Functions to be run after suspending a tty.
3618 The functions are run with one argument, the terminal object to be suspended.
3619 See `suspend-tty'. */);
3620 Vsuspend_tty_functions = Qnil;
3623 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions,
3624 doc: /* Functions to be run after resuming a tty.
3625 The functions are run with one argument, the terminal object that was revived.
3626 See `resume-tty'. */);
3627 Vresume_tty_functions = Qnil;
3629 DEFVAR_BOOL ("visible-cursor", visible_cursor,
3630 doc: /* Non-nil means to make the cursor very visible.
3631 This only has an effect when running in a text terminal.
3632 What means \"very visible\" is up to your terminal. It may make the cursor
3633 bigger, or it may make it blink, or it may do nothing at all. */);
3634 visible_cursor = 1;
3636 defsubr (&Stty_display_color_p);
3637 defsubr (&Stty_display_color_cells);
3638 defsubr (&Stty_no_underline);
3639 defsubr (&Stty_type);
3640 defsubr (&Scontrolling_tty_p);
3641 defsubr (&Ssuspend_tty);
3642 defsubr (&Sresume_tty);
3643 #ifdef HAVE_GPM
3644 defsubr (&Sgpm_mouse_start);
3645 defsubr (&Sgpm_mouse_stop);
3646 #endif /* HAVE_GPM */
3648 #ifndef DOS_NT
3649 default_orig_pair = NULL;
3650 default_set_foreground = NULL;
3651 default_set_background = NULL;
3652 #endif /* !DOS_NT */
3654 encode_terminal_src = NULL;
3655 encode_terminal_dst = NULL;