Fix bug #9402 with :align-to on TTY frames.
[emacs.git] / src / term.c
blob837ab3991527be2acd2d7f1f784673195d453038
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985-1987, 1993-1995, 1998, 2000-2011
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_BLINK = 1 << 3,
126 NC_DIM = 1 << 4,
127 NC_BOLD = 1 << 5,
128 NC_INVIS = 1 << 6,
129 NC_PROTECT = 1 << 7,
130 NC_ALT_CHARSET = 1 << 8
133 /* internal state */
135 /* The largest frame width in any call to calculate_costs. */
137 static int max_frame_cols;
139 /* Non-zero if we have dropped our controlling tty and therefore
140 should not open a frame on stdout. */
141 static int no_controlling_tty;
145 #ifdef HAVE_GPM
146 #include <sys/fcntl.h>
148 /* The device for which we have enabled gpm support (or NULL). */
149 struct tty_display_info *gpm_tty = NULL;
151 /* Last recorded mouse coordinates. */
152 static int last_mouse_x, last_mouse_y;
153 #endif /* HAVE_GPM */
155 /* Ring the bell on a tty. */
157 static void
158 tty_ring_bell (struct frame *f)
160 struct tty_display_info *tty = FRAME_TTY (f);
162 if (tty->output)
164 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
165 ? tty->TS_visible_bell
166 : tty->TS_bell));
167 fflush (tty->output);
171 /* Set up termcap modes for Emacs. */
173 static void
174 tty_set_terminal_modes (struct terminal *terminal)
176 struct tty_display_info *tty = terminal->display_info.tty;
178 if (tty->output)
180 if (tty->TS_termcap_modes)
181 OUTPUT (tty, tty->TS_termcap_modes);
182 else
184 /* Output enough newlines to scroll all the old screen contents
185 off the screen, so it won't be overwritten and lost. */
186 int i;
187 current_tty = tty;
188 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
189 cmputc ('\n');
192 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
193 OUTPUT_IF (tty, tty->TS_keypad_mode);
194 losecursor (tty);
195 fflush (tty->output);
199 /* Reset termcap modes before exiting Emacs. */
201 static void
202 tty_reset_terminal_modes (struct terminal *terminal)
204 struct tty_display_info *tty = terminal->display_info.tty;
206 if (tty->output)
208 tty_turn_off_highlight (tty);
209 tty_turn_off_insert (tty);
210 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
211 OUTPUT_IF (tty, tty->TS_cursor_normal);
212 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
213 OUTPUT_IF (tty, tty->TS_orig_pair);
214 /* Output raw CR so kernel can track the cursor hpos. */
215 current_tty = tty;
216 cmputc ('\r');
217 fflush (tty->output);
221 /* Flag the end of a display update on a termcap terminal. */
223 static void
224 tty_update_end (struct frame *f)
226 struct tty_display_info *tty = FRAME_TTY (f);
228 if (!XWINDOW (selected_window)->cursor_off_p)
229 tty_show_cursor (tty);
230 tty_turn_off_insert (tty);
231 tty_background_highlight (tty);
234 /* The implementation of set_terminal_window for termcap frames. */
236 static void
237 tty_set_terminal_window (struct frame *f, int size)
239 struct tty_display_info *tty = FRAME_TTY (f);
241 tty->specified_window = size ? size : FRAME_LINES (f);
242 if (FRAME_SCROLL_REGION_OK (f))
243 tty_set_scroll_region (f, 0, tty->specified_window);
246 static void
247 tty_set_scroll_region (struct frame *f, int start, int stop)
249 char *buf;
250 struct tty_display_info *tty = FRAME_TTY (f);
252 if (tty->TS_set_scroll_region)
253 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1, 0, 0);
254 else if (tty->TS_set_scroll_region_1)
255 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
256 FRAME_LINES (f), start,
257 FRAME_LINES (f) - stop,
258 FRAME_LINES (f));
259 else
260 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
262 OUTPUT (tty, buf);
263 xfree (buf);
264 losecursor (tty);
268 static void
269 tty_turn_on_insert (struct tty_display_info *tty)
271 if (!tty->insert_mode)
272 OUTPUT (tty, tty->TS_insert_mode);
273 tty->insert_mode = 1;
276 void
277 tty_turn_off_insert (struct tty_display_info *tty)
279 if (tty->insert_mode)
280 OUTPUT (tty, tty->TS_end_insert_mode);
281 tty->insert_mode = 0;
284 /* Handle highlighting. */
286 static void
287 tty_turn_off_highlight (struct tty_display_info *tty)
289 if (tty->standout_mode)
290 OUTPUT_IF (tty, tty->TS_end_standout_mode);
291 tty->standout_mode = 0;
294 static void
295 tty_turn_on_highlight (struct tty_display_info *tty)
297 if (!tty->standout_mode)
298 OUTPUT_IF (tty, tty->TS_standout_mode);
299 tty->standout_mode = 1;
302 static void
303 tty_toggle_highlight (struct tty_display_info *tty)
305 if (tty->standout_mode)
306 tty_turn_off_highlight (tty);
307 else
308 tty_turn_on_highlight (tty);
312 /* Make cursor invisible. */
314 static void
315 tty_hide_cursor (struct tty_display_info *tty)
317 if (tty->cursor_hidden == 0)
319 tty->cursor_hidden = 1;
320 OUTPUT_IF (tty, tty->TS_cursor_invisible);
325 /* Ensure that cursor is visible. */
327 static void
328 tty_show_cursor (struct tty_display_info *tty)
330 if (tty->cursor_hidden)
332 tty->cursor_hidden = 0;
333 OUTPUT_IF (tty, tty->TS_cursor_normal);
334 if (visible_cursor)
335 OUTPUT_IF (tty, tty->TS_cursor_visible);
340 /* Set standout mode to the state it should be in for
341 empty space inside windows. What this is,
342 depends on the user option inverse-video. */
344 static void
345 tty_background_highlight (struct tty_display_info *tty)
347 if (inverse_video)
348 tty_turn_on_highlight (tty);
349 else
350 tty_turn_off_highlight (tty);
353 /* Set standout mode to the mode specified for the text to be output. */
355 static void
356 tty_highlight_if_desired (struct tty_display_info *tty)
358 if (inverse_video)
359 tty_turn_on_highlight (tty);
360 else
361 tty_turn_off_highlight (tty);
365 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
366 frame-relative coordinates. */
368 static void
369 tty_cursor_to (struct frame *f, int vpos, int hpos)
371 struct tty_display_info *tty = FRAME_TTY (f);
373 /* Detect the case where we are called from reset_sys_modes
374 and the costs have never been calculated. Do nothing. */
375 if (! tty->costs_set)
376 return;
378 if (curY (tty) == vpos
379 && curX (tty) == hpos)
380 return;
381 if (!tty->TF_standout_motion)
382 tty_background_highlight (tty);
383 if (!tty->TF_insmode_motion)
384 tty_turn_off_insert (tty);
385 cmgoto (tty, vpos, hpos);
388 /* Similar but don't take any account of the wasted characters. */
390 static void
391 tty_raw_cursor_to (struct frame *f, int row, int col)
393 struct tty_display_info *tty = FRAME_TTY (f);
395 if (curY (tty) == row
396 && curX (tty) == col)
397 return;
398 if (!tty->TF_standout_motion)
399 tty_background_highlight (tty);
400 if (!tty->TF_insmode_motion)
401 tty_turn_off_insert (tty);
402 cmgoto (tty, row, col);
405 /* Erase operations */
407 /* Clear from cursor to end of frame on a termcap device. */
409 static void
410 tty_clear_to_end (struct frame *f)
412 register int i;
413 struct tty_display_info *tty = FRAME_TTY (f);
415 if (tty->TS_clr_to_bottom)
417 tty_background_highlight (tty);
418 OUTPUT (tty, tty->TS_clr_to_bottom);
420 else
422 for (i = curY (tty); i < FRAME_LINES (f); i++)
424 cursor_to (f, i, 0);
425 clear_end_of_line (f, FRAME_COLS (f));
430 /* Clear an entire termcap frame. */
432 static void
433 tty_clear_frame (struct frame *f)
435 struct tty_display_info *tty = FRAME_TTY (f);
437 if (tty->TS_clr_frame)
439 tty_background_highlight (tty);
440 OUTPUT (tty, tty->TS_clr_frame);
441 cmat (tty, 0, 0);
443 else
445 cursor_to (f, 0, 0);
446 clear_to_end (f);
450 /* An implementation of clear_end_of_line for termcap frames.
452 Note that the cursor may be moved, on terminals lacking a `ce' string. */
454 static void
455 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
457 register int i;
458 struct tty_display_info *tty = FRAME_TTY (f);
460 /* Detect the case where we are called from reset_sys_modes
461 and the costs have never been calculated. Do nothing. */
462 if (! tty->costs_set)
463 return;
465 if (curX (tty) >= first_unused_hpos)
466 return;
467 tty_background_highlight (tty);
468 if (tty->TS_clr_line)
470 OUTPUT1 (tty, tty->TS_clr_line);
472 else
473 { /* have to do it the hard way */
474 tty_turn_off_insert (tty);
476 /* Do not write in last row last col with Auto-wrap on. */
477 if (AutoWrap (tty)
478 && curY (tty) == FrameRows (tty) - 1
479 && first_unused_hpos == FrameCols (tty))
480 first_unused_hpos--;
482 for (i = curX (tty); i < first_unused_hpos; i++)
484 if (tty->termscript)
485 fputc (' ', tty->termscript);
486 fputc (' ', tty->output);
488 cmplus (tty, first_unused_hpos - curX (tty));
492 /* Buffers to store the source and result of code conversion for terminal. */
493 static unsigned char *encode_terminal_src;
494 static unsigned char *encode_terminal_dst;
495 /* Allocated sizes of the above buffers. */
496 static ptrdiff_t encode_terminal_src_size;
497 static ptrdiff_t encode_terminal_dst_size;
499 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
500 Set CODING->produced to the byte-length of the resulting byte
501 sequence, and return a pointer to that byte sequence. */
503 unsigned char *
504 encode_terminal_code (struct glyph *src, int src_len, struct coding_system *coding)
506 struct glyph *src_end = src + src_len;
507 unsigned char *buf;
508 ptrdiff_t nchars, nbytes, required;
509 ptrdiff_t tlen = GLYPH_TABLE_LENGTH;
510 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
511 Lisp_Object charset_list;
513 /* Allocate sufficient size of buffer to store all characters in
514 multibyte-form. But, it may be enlarged on demand if
515 Vglyph_table contains a string or a composite glyph is
516 encountered. */
517 if (min (PTRDIFF_MAX, SIZE_MAX) / MAX_MULTIBYTE_LENGTH < src_len)
518 memory_full (SIZE_MAX);
519 required = src_len;
520 required *= MAX_MULTIBYTE_LENGTH;
521 if (encode_terminal_src_size < required)
523 encode_terminal_src = xrealloc (encode_terminal_src, required);
524 encode_terminal_src_size = required;
527 charset_list = coding_charset_list (coding);
529 buf = encode_terminal_src;
530 nchars = 0;
531 while (src < src_end)
533 if (src->type == COMPOSITE_GLYPH)
535 struct composition *cmp IF_LINT (= NULL);
536 Lisp_Object gstring IF_LINT (= Qnil);
537 int i;
539 nbytes = buf - encode_terminal_src;
540 if (src->u.cmp.automatic)
542 gstring = composition_gstring_from_id (src->u.cmp.id);
543 required = src->slice.cmp.to - src->slice.cmp.from + 1;
545 else
547 cmp = composition_table[src->u.cmp.id];
548 required = cmp->glyph_len;
549 required *= MAX_MULTIBYTE_LENGTH;
552 if (encode_terminal_src_size - nbytes < required)
554 encode_terminal_src =
555 xpalloc (encode_terminal_src, &encode_terminal_src_size,
556 required - (encode_terminal_src_size - nbytes),
557 -1, 1);
558 buf = encode_terminal_src + nbytes;
561 if (src->u.cmp.automatic)
562 for (i = src->slice.cmp.from; i <= src->slice.cmp.to; i++)
564 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
565 int c = LGLYPH_CHAR (g);
567 if (! char_charset (c, charset_list, NULL))
568 c = '?';
569 buf += CHAR_STRING (c, buf);
570 nchars++;
572 else
573 for (i = 0; i < cmp->glyph_len; i++)
575 int c = COMPOSITION_GLYPH (cmp, i);
577 if (c == '\t')
578 continue;
579 if (char_charset (c, charset_list, NULL))
581 if (CHAR_WIDTH (c) == 0
582 && i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
583 /* Should be left-padded */
585 buf += CHAR_STRING (' ', buf);
586 nchars++;
589 else
590 c = '?';
591 buf += CHAR_STRING (c, buf);
592 nchars++;
595 /* We must skip glyphs to be padded for a wide character. */
596 else if (! CHAR_GLYPH_PADDING_P (*src))
598 GLYPH g;
599 int c IF_LINT (= 0);
600 Lisp_Object string;
602 string = Qnil;
603 SET_GLYPH_FROM_CHAR_GLYPH (g, src[0]);
605 if (GLYPH_INVALID_P (g) || GLYPH_SIMPLE_P (tbase, tlen, g))
607 /* This glyph doesn't have an entry in Vglyph_table. */
608 c = src->u.ch;
610 else
612 /* This glyph has an entry in Vglyph_table,
613 so process any alias before testing for simpleness. */
614 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
616 if (GLYPH_SIMPLE_P (tbase, tlen, g))
617 /* We set the multi-byte form of a character in G
618 (that should be an ASCII character) at WORKBUF. */
619 c = GLYPH_CHAR (g);
620 else
621 /* We have a string in Vglyph_table. */
622 string = tbase[GLYPH_CHAR (g)];
625 if (NILP (string))
627 nbytes = buf - encode_terminal_src;
628 if (encode_terminal_src_size - nbytes < MAX_MULTIBYTE_LENGTH)
630 encode_terminal_src =
631 xpalloc (encode_terminal_src, &encode_terminal_src_size,
632 MAX_MULTIBYTE_LENGTH, -1, 1);
633 buf = encode_terminal_src + nbytes;
635 if (CHAR_BYTE8_P (c)
636 || char_charset (c, charset_list, NULL))
638 /* Store the multibyte form of C at BUF. */
639 buf += CHAR_STRING (c, buf);
640 nchars++;
642 else
644 /* C is not encodable. */
645 *buf++ = '?';
646 nchars++;
647 while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
649 *buf++ = '?';
650 nchars++;
651 src++;
655 else
657 if (! STRING_MULTIBYTE (string))
658 string = string_to_multibyte (string);
659 nbytes = buf - encode_terminal_src;
660 if (encode_terminal_src_size - nbytes < SBYTES (string))
662 encode_terminal_src =
663 xpalloc (encode_terminal_src, &encode_terminal_src_size,
664 (SBYTES (string)
665 - (encode_terminal_src_size - nbytes)),
666 -1, 1);
667 buf = encode_terminal_src + nbytes;
669 memcpy (buf, SDATA (string), SBYTES (string));
670 buf += SBYTES (string);
671 nchars += SCHARS (string);
674 src++;
677 if (nchars == 0)
679 coding->produced = 0;
680 return NULL;
683 nbytes = buf - encode_terminal_src;
684 coding->source = encode_terminal_src;
685 if (encode_terminal_dst_size == 0)
687 encode_terminal_dst = xrealloc (encode_terminal_dst,
688 encode_terminal_src_size);
689 encode_terminal_dst_size = encode_terminal_src_size;
691 coding->destination = encode_terminal_dst;
692 coding->dst_bytes = encode_terminal_dst_size;
693 encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
694 /* coding->destination may have been reallocated. */
695 encode_terminal_dst = coding->destination;
696 encode_terminal_dst_size = coding->dst_bytes;
698 return (encode_terminal_dst);
703 /* An implementation of write_glyphs for termcap frames. */
705 static void
706 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
708 unsigned char *conversion_buffer;
709 struct coding_system *coding;
710 size_t n, stringlen;
712 struct tty_display_info *tty = FRAME_TTY (f);
714 tty_turn_off_insert (tty);
715 tty_hide_cursor (tty);
717 /* Don't dare write in last column of bottom line, if Auto-Wrap,
718 since that would scroll the whole frame on some terminals. */
720 if (AutoWrap (tty)
721 && curY (tty) + 1 == FRAME_LINES (f)
722 && (curX (tty) + len) == FRAME_COLS (f))
723 len --;
724 if (len <= 0)
725 return;
727 cmplus (tty, len);
729 /* If terminal_coding does any conversion, use it, otherwise use
730 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
731 because it always return 1 if the member src_multibyte is 1. */
732 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
733 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
734 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
735 the tail. */
736 coding->mode &= ~CODING_MODE_LAST_BLOCK;
738 for (stringlen = len; stringlen != 0; stringlen -= n)
740 /* Identify a run of glyphs with the same face. */
741 int face_id = string->face_id;
743 for (n = 1; n < stringlen; ++n)
744 if (string[n].face_id != face_id)
745 break;
747 /* Turn appearance modes of the face of the run on. */
748 tty_highlight_if_desired (tty);
749 turn_on_face (f, face_id);
751 if (n == stringlen)
752 /* This is the last run. */
753 coding->mode |= CODING_MODE_LAST_BLOCK;
754 conversion_buffer = encode_terminal_code (string, n, coding);
755 if (coding->produced > 0)
757 BLOCK_INPUT;
758 fwrite (conversion_buffer, 1, coding->produced, tty->output);
759 if (ferror (tty->output))
760 clearerr (tty->output);
761 if (tty->termscript)
762 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
763 UNBLOCK_INPUT;
765 string += n;
767 /* Turn appearance modes off. */
768 turn_off_face (f, face_id);
769 tty_turn_off_highlight (tty);
772 cmcheckmagic (tty);
775 #ifdef HAVE_GPM /* Only used by GPM code. */
777 static void
778 tty_write_glyphs_with_face (register struct frame *f, register struct glyph *string,
779 register int len, register int face_id)
781 unsigned char *conversion_buffer;
782 struct coding_system *coding;
784 struct tty_display_info *tty = FRAME_TTY (f);
786 tty_turn_off_insert (tty);
787 tty_hide_cursor (tty);
789 /* Don't dare write in last column of bottom line, if Auto-Wrap,
790 since that would scroll the whole frame on some terminals. */
792 if (AutoWrap (tty)
793 && curY (tty) + 1 == FRAME_LINES (f)
794 && (curX (tty) + len) == FRAME_COLS (f))
795 len --;
796 if (len <= 0)
797 return;
799 cmplus (tty, len);
801 /* If terminal_coding does any conversion, use it, otherwise use
802 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
803 because it always return 1 if the member src_multibyte is 1. */
804 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
805 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
806 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
807 the tail. */
808 coding->mode &= ~CODING_MODE_LAST_BLOCK;
810 /* Turn appearance modes of the face. */
811 tty_highlight_if_desired (tty);
812 turn_on_face (f, face_id);
814 coding->mode |= CODING_MODE_LAST_BLOCK;
815 conversion_buffer = encode_terminal_code (string, len, coding);
816 if (coding->produced > 0)
818 BLOCK_INPUT;
819 fwrite (conversion_buffer, 1, coding->produced, tty->output);
820 if (ferror (tty->output))
821 clearerr (tty->output);
822 if (tty->termscript)
823 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
824 UNBLOCK_INPUT;
827 /* Turn appearance modes off. */
828 turn_off_face (f, face_id);
829 tty_turn_off_highlight (tty);
831 cmcheckmagic (tty);
833 #endif
835 /* An implementation of insert_glyphs for termcap frames. */
837 static void
838 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
840 char *buf;
841 struct glyph *glyph = NULL;
842 unsigned char *conversion_buffer;
843 unsigned char space[1];
844 struct coding_system *coding;
846 struct tty_display_info *tty = FRAME_TTY (f);
848 if (tty->TS_ins_multi_chars)
850 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len, 0, 0, 0);
851 OUTPUT1 (tty, buf);
852 xfree (buf);
853 if (start)
854 write_glyphs (f, start, len);
855 return;
858 tty_turn_on_insert (tty);
859 cmplus (tty, len);
861 if (! start)
862 space[0] = SPACEGLYPH;
864 /* If terminal_coding does any conversion, use it, otherwise use
865 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
866 because it always return 1 if the member src_multibyte is 1. */
867 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
868 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
869 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
870 the tail. */
871 coding->mode &= ~CODING_MODE_LAST_BLOCK;
873 while (len-- > 0)
875 OUTPUT1_IF (tty, tty->TS_ins_char);
876 if (!start)
878 conversion_buffer = space;
879 coding->produced = 1;
881 else
883 tty_highlight_if_desired (tty);
884 turn_on_face (f, start->face_id);
885 glyph = start;
886 ++start;
887 /* We must open sufficient space for a character which
888 occupies more than one column. */
889 while (len && CHAR_GLYPH_PADDING_P (*start))
891 OUTPUT1_IF (tty, tty->TS_ins_char);
892 start++, len--;
895 if (len <= 0)
896 /* This is the last glyph. */
897 coding->mode |= CODING_MODE_LAST_BLOCK;
899 conversion_buffer = encode_terminal_code (glyph, 1, coding);
902 if (coding->produced > 0)
904 BLOCK_INPUT;
905 fwrite (conversion_buffer, 1, coding->produced, tty->output);
906 if (ferror (tty->output))
907 clearerr (tty->output);
908 if (tty->termscript)
909 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
910 UNBLOCK_INPUT;
913 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
914 if (start)
916 turn_off_face (f, glyph->face_id);
917 tty_turn_off_highlight (tty);
921 cmcheckmagic (tty);
924 /* An implementation of delete_glyphs for termcap frames. */
926 static void
927 tty_delete_glyphs (struct frame *f, int n)
929 char *buf;
930 register int i;
932 struct tty_display_info *tty = FRAME_TTY (f);
934 if (tty->delete_in_insert_mode)
936 tty_turn_on_insert (tty);
938 else
940 tty_turn_off_insert (tty);
941 OUTPUT_IF (tty, tty->TS_delete_mode);
944 if (tty->TS_del_multi_chars)
946 buf = tparam (tty->TS_del_multi_chars, 0, 0, n, 0, 0, 0);
947 OUTPUT1 (tty, buf);
948 xfree (buf);
950 else
951 for (i = 0; i < n; i++)
952 OUTPUT1 (tty, tty->TS_del_char);
953 if (!tty->delete_in_insert_mode)
954 OUTPUT_IF (tty, tty->TS_end_delete_mode);
957 /* An implementation of ins_del_lines for termcap frames. */
959 static void
960 tty_ins_del_lines (struct frame *f, int vpos, int n)
962 struct tty_display_info *tty = FRAME_TTY (f);
963 const char *multi =
964 n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
965 const char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
966 const char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
968 register int i = n > 0 ? n : -n;
969 register char *buf;
971 /* If the lines below the insertion are being pushed
972 into the end of the window, this is the same as clearing;
973 and we know the lines are already clear, since the matching
974 deletion has already been done. So can ignore this. */
975 /* If the lines below the deletion are blank lines coming
976 out of the end of the window, don't bother,
977 as there will be a matching inslines later that will flush them. */
978 if (FRAME_SCROLL_REGION_OK (f)
979 && vpos + i >= tty->specified_window)
980 return;
981 if (!FRAME_MEMORY_BELOW_FRAME (f)
982 && vpos + i >= FRAME_LINES (f))
983 return;
985 if (multi)
987 raw_cursor_to (f, vpos, 0);
988 tty_background_highlight (tty);
989 buf = tparam (multi, 0, 0, i, 0, 0, 0);
990 OUTPUT (tty, buf);
991 xfree (buf);
993 else if (single)
995 raw_cursor_to (f, vpos, 0);
996 tty_background_highlight (tty);
997 while (--i >= 0)
998 OUTPUT (tty, single);
999 if (tty->TF_teleray)
1000 curX (tty) = 0;
1002 else
1004 tty_set_scroll_region (f, vpos, tty->specified_window);
1005 if (n < 0)
1006 raw_cursor_to (f, tty->specified_window - 1, 0);
1007 else
1008 raw_cursor_to (f, vpos, 0);
1009 tty_background_highlight (tty);
1010 while (--i >= 0)
1011 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1012 tty_set_scroll_region (f, 0, tty->specified_window);
1015 if (!FRAME_SCROLL_REGION_OK (f)
1016 && FRAME_MEMORY_BELOW_FRAME (f)
1017 && n < 0)
1019 cursor_to (f, FRAME_LINES (f) + n, 0);
1020 clear_to_end (f);
1024 /* Compute cost of sending "str", in characters,
1025 not counting any line-dependent padding. */
1028 string_cost (const char *str)
1030 cost = 0;
1031 if (str)
1032 tputs (str, 0, evalcost);
1033 return cost;
1036 /* Compute cost of sending "str", in characters,
1037 counting any line-dependent padding at one line. */
1039 static int
1040 string_cost_one_line (const char *str)
1042 cost = 0;
1043 if (str)
1044 tputs (str, 1, evalcost);
1045 return cost;
1048 /* Compute per line amount of line-dependent padding,
1049 in tenths of characters. */
1052 per_line_cost (const char *str)
1054 cost = 0;
1055 if (str)
1056 tputs (str, 0, evalcost);
1057 cost = - cost;
1058 if (str)
1059 tputs (str, 10, evalcost);
1060 return cost;
1063 /* char_ins_del_cost[n] is cost of inserting N characters.
1064 char_ins_del_cost[-n] is cost of deleting N characters.
1065 The length of this vector is based on max_frame_cols. */
1067 int *char_ins_del_vector;
1069 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1071 /* ARGSUSED */
1072 static void
1073 calculate_ins_del_char_costs (struct frame *f)
1075 struct tty_display_info *tty = FRAME_TTY (f);
1076 int ins_startup_cost, del_startup_cost;
1077 int ins_cost_per_char, del_cost_per_char;
1078 register int i;
1079 register int *p;
1081 if (tty->TS_ins_multi_chars)
1083 ins_cost_per_char = 0;
1084 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1086 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1087 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1089 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1090 + string_cost (tty->TS_end_insert_mode))) / 100;
1091 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1092 + string_cost_one_line (tty->TS_pad_inserted_char));
1094 else
1096 ins_startup_cost = 9999;
1097 ins_cost_per_char = 0;
1100 if (tty->TS_del_multi_chars)
1102 del_cost_per_char = 0;
1103 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1105 else if (tty->TS_del_char)
1107 del_startup_cost = (string_cost (tty->TS_delete_mode)
1108 + string_cost (tty->TS_end_delete_mode));
1109 if (tty->delete_in_insert_mode)
1110 del_startup_cost /= 2;
1111 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1113 else
1115 del_startup_cost = 9999;
1116 del_cost_per_char = 0;
1119 /* Delete costs are at negative offsets */
1120 p = &char_ins_del_cost (f)[0];
1121 for (i = FRAME_COLS (f); --i >= 0;)
1122 *--p = (del_startup_cost += del_cost_per_char);
1124 /* Doing nothing is free */
1125 p = &char_ins_del_cost (f)[0];
1126 *p++ = 0;
1128 /* Insert costs are at positive offsets */
1129 for (i = FRAME_COLS (f); --i >= 0;)
1130 *p++ = (ins_startup_cost += ins_cost_per_char);
1133 void
1134 calculate_costs (struct frame *frame)
1136 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1138 if (FRAME_TERMCAP_P (frame))
1140 struct tty_display_info *tty = FRAME_TTY (frame);
1141 register const char *f = (tty->TS_set_scroll_region
1142 ? tty->TS_set_scroll_region
1143 : tty->TS_set_scroll_region_1);
1145 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1147 tty->costs_set = 1;
1149 /* These variables are only used for terminal stuff. They are
1150 allocated once for the terminal frame of X-windows emacs, but not
1151 used afterwards.
1153 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1154 X turns off char_ins_del_ok. */
1156 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1157 if ((min (PTRDIFF_MAX, SIZE_MAX) / sizeof (int) - 1) / 2
1158 < max_frame_cols)
1159 memory_full (SIZE_MAX);
1161 char_ins_del_vector =
1162 xrealloc (char_ins_del_vector,
1163 (sizeof (int) + 2 * sizeof (int) * max_frame_cols));
1165 memset (char_ins_del_vector, 0,
1166 (sizeof (int) + 2 * sizeof (int) * max_frame_cols));
1169 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1170 do_line_insertion_deletion_costs (frame,
1171 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1172 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1173 f, f, 1);
1174 else
1175 do_line_insertion_deletion_costs (frame,
1176 tty->TS_ins_line, tty->TS_ins_multi_lines,
1177 tty->TS_del_line, tty->TS_del_multi_lines,
1178 0, 0, 1);
1180 calculate_ins_del_char_costs (frame);
1182 /* Don't use TS_repeat if its padding is worse than sending the chars */
1183 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1184 tty->RPov = string_cost (tty->TS_repeat);
1185 else
1186 tty->RPov = FRAME_COLS (frame) * 2;
1188 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1192 struct fkey_table {
1193 const char *cap, *name;
1196 /* Termcap capability names that correspond directly to X keysyms.
1197 Some of these (marked "terminfo") aren't supplied by old-style
1198 (Berkeley) termcap entries. They're listed in X keysym order;
1199 except we put the keypad keys first, so that if they clash with
1200 other keys (as on the IBM PC keyboard) they get overridden.
1203 static const struct fkey_table keys[] =
1205 {"kh", "home"}, /* termcap */
1206 {"kl", "left"}, /* termcap */
1207 {"ku", "up"}, /* termcap */
1208 {"kr", "right"}, /* termcap */
1209 {"kd", "down"}, /* termcap */
1210 {"%8", "prior"}, /* terminfo */
1211 {"%5", "next"}, /* terminfo */
1212 {"@7", "end"}, /* terminfo */
1213 {"@1", "begin"}, /* terminfo */
1214 {"*6", "select"}, /* terminfo */
1215 {"%9", "print"}, /* terminfo */
1216 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1218 * "insert" --- see below
1220 {"&8", "undo"}, /* terminfo */
1221 {"%0", "redo"}, /* terminfo */
1222 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1223 {"@0", "find"}, /* terminfo */
1224 {"@2", "cancel"}, /* terminfo */
1225 {"%1", "help"}, /* terminfo */
1227 * "break" goes here, but can't be reliably intercepted with termcap
1229 {"&4", "reset"}, /* terminfo --- actually `restart' */
1231 * "system" and "user" --- no termcaps
1233 {"kE", "clearline"}, /* terminfo */
1234 {"kA", "insertline"}, /* terminfo */
1235 {"kL", "deleteline"}, /* terminfo */
1236 {"kI", "insertchar"}, /* terminfo */
1237 {"kD", "deletechar"}, /* terminfo */
1238 {"kB", "backtab"}, /* terminfo */
1240 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1242 {"@8", "kp-enter"}, /* terminfo */
1244 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1245 * "kp-multiply", "kp-add", "kp-separator",
1246 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1247 * --- no termcaps for any of these.
1249 {"K4", "kp-1"}, /* terminfo */
1251 * "kp-2" --- no termcap
1253 {"K5", "kp-3"}, /* terminfo */
1255 * "kp-4" --- no termcap
1257 {"K2", "kp-5"}, /* terminfo */
1259 * "kp-6" --- no termcap
1261 {"K1", "kp-7"}, /* terminfo */
1263 * "kp-8" --- no termcap
1265 {"K3", "kp-9"}, /* terminfo */
1267 * "kp-equal" --- no termcap
1269 {"k1", "f1"},
1270 {"k2", "f2"},
1271 {"k3", "f3"},
1272 {"k4", "f4"},
1273 {"k5", "f5"},
1274 {"k6", "f6"},
1275 {"k7", "f7"},
1276 {"k8", "f8"},
1277 {"k9", "f9"},
1279 {"&0", "S-cancel"}, /*shifted cancel key*/
1280 {"&9", "S-begin"}, /*shifted begin key*/
1281 {"*0", "S-find"}, /*shifted find key*/
1282 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1283 {"*4", "S-delete"}, /*shifted delete-character key*/
1284 {"*7", "S-end"}, /*shifted end key*/
1285 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1286 {"#1", "S-help"}, /*shifted help key*/
1287 {"#2", "S-home"}, /*shifted home key*/
1288 {"#3", "S-insert"}, /*shifted insert-character key*/
1289 {"#4", "S-left"}, /*shifted left-arrow key*/
1290 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1291 {"%c", "S-next"}, /*shifted next key*/
1292 {"%e", "S-prior"}, /*shifted previous key*/
1293 {"%f", "S-print"}, /*shifted print key*/
1294 {"%g", "S-redo"}, /*shifted redo key*/
1295 {"%i", "S-right"}, /*shifted right-arrow key*/
1296 {"!3", "S-undo"} /*shifted undo key*/
1299 #ifndef DOS_NT
1300 static char **term_get_fkeys_address;
1301 static KBOARD *term_get_fkeys_kboard;
1302 static Lisp_Object term_get_fkeys_1 (void);
1304 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1305 This function scans the termcap function key sequence entries, and
1306 adds entries to Vinput_decode_map for each function key it finds. */
1308 static void
1309 term_get_fkeys (char **address, KBOARD *kboard)
1311 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1312 errors during the call. The only errors should be from Fdefine_key
1313 when given a key sequence containing an invalid prefix key. If the
1314 termcap defines function keys which use a prefix that is already bound
1315 to a command by the default bindings, we should silently ignore that
1316 function key specification, rather than giving the user an error and
1317 refusing to run at all on such a terminal. */
1319 term_get_fkeys_address = address;
1320 term_get_fkeys_kboard = kboard;
1321 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1324 static Lisp_Object
1325 term_get_fkeys_1 (void)
1327 int i;
1329 char **address = term_get_fkeys_address;
1330 KBOARD *kboard = term_get_fkeys_kboard;
1332 /* This can happen if CANNOT_DUMP or with strange options. */
1333 if (!KEYMAPP (KVAR (kboard, Vinput_decode_map)))
1334 KVAR (kboard, Vinput_decode_map) = Fmake_sparse_keymap (Qnil);
1336 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1338 char *sequence = tgetstr (keys[i].cap, address);
1339 if (sequence)
1340 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence),
1341 Fmake_vector (make_number (1),
1342 intern (keys[i].name)));
1345 /* The uses of the "k0" capability are inconsistent; sometimes it
1346 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1347 We will attempt to politely accommodate both systems by testing for
1348 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1351 const char *k_semi = tgetstr ("k;", address);
1352 const char *k0 = tgetstr ("k0", address);
1353 const char *k0_name = "f10";
1355 if (k_semi)
1357 if (k0)
1358 /* Define f0 first, so that f10 takes precedence in case the
1359 key sequences happens to be the same. */
1360 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k0),
1361 Fmake_vector (make_number (1), intern ("f0")));
1362 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k_semi),
1363 Fmake_vector (make_number (1), intern ("f10")));
1365 else if (k0)
1366 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k0),
1367 Fmake_vector (make_number (1), intern (k0_name)));
1370 /* Set up cookies for numbered function keys above f10. */
1372 char fcap[3], fkey[4];
1374 fcap[0] = 'F'; fcap[2] = '\0';
1375 for (i = 11; i < 64; i++)
1377 if (i <= 19)
1378 fcap[1] = '1' + i - 11;
1379 else if (i <= 45)
1380 fcap[1] = 'A' + i - 20;
1381 else
1382 fcap[1] = 'a' + i - 46;
1385 char *sequence = tgetstr (fcap, address);
1386 if (sequence)
1388 sprintf (fkey, "f%d", i);
1389 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence),
1390 Fmake_vector (make_number (1),
1391 intern (fkey)));
1398 * Various mappings to try and get a better fit.
1401 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1402 if (!tgetstr (cap1, address)) \
1404 char *sequence = tgetstr (cap2, address); \
1405 if (sequence) \
1406 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence), \
1407 Fmake_vector (make_number (1), \
1408 intern (sym))); \
1411 /* if there's no key_next keycap, map key_npage to `next' keysym */
1412 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1413 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1414 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1415 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1416 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1417 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1418 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1420 /* IBM has their own non-standard dialect of terminfo.
1421 If the standard name isn't found, try the IBM name. */
1422 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1423 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1424 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1425 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1426 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1427 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1428 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1429 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1430 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1431 #undef CONDITIONAL_REASSIGN
1434 return Qnil;
1436 #endif /* not DOS_NT */
1439 /***********************************************************************
1440 Character Display Information
1441 ***********************************************************************/
1442 static void append_glyph (struct it *);
1443 static void append_composite_glyph (struct it *);
1444 static void produce_composite_glyph (struct it *);
1445 static void append_glyphless_glyph (struct it *, int, const char *);
1446 static void produce_glyphless_glyph (struct it *, int, Lisp_Object);
1448 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1449 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1450 the character for which to produce glyphs; IT->face_id contains the
1451 character's face. Padding glyphs are appended if IT->c has a
1452 IT->pixel_width > 1. */
1454 static void
1455 append_glyph (struct it *it)
1457 struct glyph *glyph, *end;
1458 int i;
1460 xassert (it->glyph_row);
1461 glyph = (it->glyph_row->glyphs[it->area]
1462 + it->glyph_row->used[it->area]);
1463 end = it->glyph_row->glyphs[1 + it->area];
1465 /* If the glyph row is reversed, we need to prepend the glyph rather
1466 than append it. */
1467 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1469 struct glyph *g;
1470 int move_by = it->pixel_width;
1472 /* Make room for the new glyphs. */
1473 if (move_by > end - glyph) /* don't overstep end of this area */
1474 move_by = end - glyph;
1475 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1476 g[move_by] = *g;
1477 glyph = it->glyph_row->glyphs[it->area];
1478 end = glyph + move_by;
1481 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1482 right, even in the REVERSED_P case, since (a) all of its u.ch are
1483 identical, and (b) the PADDING_P flag needs to be set for the
1484 leftmost one, because we write to the terminal left-to-right. */
1485 for (i = 0;
1486 i < it->pixel_width && glyph < end;
1487 ++i)
1489 glyph->type = CHAR_GLYPH;
1490 glyph->pixel_width = 1;
1491 glyph->u.ch = it->char_to_display;
1492 glyph->face_id = it->face_id;
1493 glyph->padding_p = i > 0;
1494 glyph->charpos = CHARPOS (it->position);
1495 glyph->object = it->object;
1496 if (it->bidi_p)
1498 glyph->resolved_level = it->bidi_it.resolved_level;
1499 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1500 abort ();
1501 glyph->bidi_type = it->bidi_it.type;
1503 else
1505 glyph->resolved_level = 0;
1506 glyph->bidi_type = UNKNOWN_BT;
1509 ++it->glyph_row->used[it->area];
1510 ++glyph;
1514 /* For external use. */
1515 void
1516 tty_append_glyph (struct it *it)
1518 append_glyph (it);
1522 /* Produce glyphs for the display element described by IT. *IT
1523 specifies what we want to produce a glyph for (character, image, ...),
1524 and where in the glyph matrix we currently are (glyph row and hpos).
1525 produce_glyphs fills in output fields of *IT with information such as the
1526 pixel width and height of a character, and maybe output actual glyphs at
1527 the same time if IT->glyph_row is non-null. For an overview, see
1528 the explanation in dispextern.h, before the definition of the
1529 display_element_type enumeration.
1531 produce_glyphs also stores the result of glyph width, ascent
1532 etc. computations in *IT.
1534 IT->glyph_row may be null, in which case produce_glyphs does not
1535 actually fill in the glyphs. This is used in the move_* functions
1536 in xdisp.c for text width and height computations.
1538 Callers usually don't call produce_glyphs directly;
1539 instead they use the macro PRODUCE_GLYPHS. */
1541 void
1542 produce_glyphs (struct it *it)
1544 /* If a hook is installed, let it do the work. */
1546 /* Nothing but characters are supported on terminal frames. */
1547 xassert (it->what == IT_CHARACTER
1548 || it->what == IT_COMPOSITION
1549 || it->what == IT_STRETCH
1550 || it->what == IT_GLYPHLESS);
1552 if (it->what == IT_STRETCH)
1554 produce_stretch_glyph (it);
1555 goto done;
1558 if (it->what == IT_COMPOSITION)
1560 produce_composite_glyph (it);
1561 goto done;
1564 if (it->what == IT_GLYPHLESS)
1566 produce_glyphless_glyph (it, 0, Qnil);
1567 goto done;
1570 if (it->char_to_display >= 040 && it->char_to_display < 0177)
1572 it->pixel_width = it->nglyphs = 1;
1573 if (it->glyph_row)
1574 append_glyph (it);
1576 else if (it->char_to_display == '\n')
1577 it->pixel_width = it->nglyphs = 0;
1578 else if (it->char_to_display == '\t')
1580 int absolute_x = (it->current_x
1581 + it->continuation_lines_width);
1582 int next_tab_x
1583 = (((1 + absolute_x + it->tab_width - 1)
1584 / it->tab_width)
1585 * it->tab_width);
1586 int nspaces;
1588 /* If part of the TAB has been displayed on the previous line
1589 which is continued now, continuation_lines_width will have
1590 been incremented already by the part that fitted on the
1591 continued line. So, we will get the right number of spaces
1592 here. */
1593 nspaces = next_tab_x - absolute_x;
1595 if (it->glyph_row)
1597 int n = nspaces;
1599 it->char_to_display = ' ';
1600 it->pixel_width = it->len = 1;
1602 while (n--)
1603 append_glyph (it);
1606 it->pixel_width = nspaces;
1607 it->nglyphs = nspaces;
1609 else if (CHAR_BYTE8_P (it->char_to_display))
1611 /* Coming here means that we must send the raw 8-bit byte as is
1612 to the terminal. Although there's no way to know how many
1613 columns it occupies on a screen, it is a good assumption that
1614 a single byte code has 1-column width. */
1615 it->pixel_width = it->nglyphs = 1;
1616 if (it->glyph_row)
1617 append_glyph (it);
1619 else
1621 Lisp_Object charset_list = FRAME_TERMINAL (it->f)->charset_list;
1623 if (char_charset (it->char_to_display, charset_list, NULL))
1625 it->pixel_width = CHAR_WIDTH (it->char_to_display);
1626 it->nglyphs = it->pixel_width;
1627 if (it->glyph_row)
1628 append_glyph (it);
1630 else
1632 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
1634 xassert (it->what == IT_GLYPHLESS);
1635 produce_glyphless_glyph (it, 1, acronym);
1639 done:
1640 /* Advance current_x by the pixel width as a convenience for
1641 the caller. */
1642 if (it->area == TEXT_AREA)
1643 it->current_x += it->pixel_width;
1644 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1645 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1648 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1649 Called from produce_composite_glyph for terminal frames if
1650 IT->glyph_row != NULL. IT->face_id contains the character's
1651 face. */
1653 static void
1654 append_composite_glyph (struct it *it)
1656 struct glyph *glyph;
1658 xassert (it->glyph_row);
1659 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1660 if (glyph < it->glyph_row->glyphs[1 + it->area])
1662 /* If the glyph row is reversed, we need to prepend the glyph
1663 rather than append it. */
1664 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1666 struct glyph *g;
1668 /* Make room for the new glyph. */
1669 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1670 g[1] = *g;
1671 glyph = it->glyph_row->glyphs[it->area];
1673 glyph->type = COMPOSITE_GLYPH;
1674 glyph->pixel_width = it->pixel_width;
1675 glyph->u.cmp.id = it->cmp_it.id;
1676 if (it->cmp_it.ch < 0)
1678 glyph->u.cmp.automatic = 0;
1679 glyph->u.cmp.id = it->cmp_it.id;
1681 else
1683 glyph->u.cmp.automatic = 1;
1684 glyph->u.cmp.id = it->cmp_it.id;
1685 glyph->slice.cmp.from = it->cmp_it.from;
1686 glyph->slice.cmp.to = it->cmp_it.to - 1;
1689 glyph->face_id = it->face_id;
1690 glyph->padding_p = 0;
1691 glyph->charpos = CHARPOS (it->position);
1692 glyph->object = it->object;
1693 if (it->bidi_p)
1695 glyph->resolved_level = it->bidi_it.resolved_level;
1696 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1697 abort ();
1698 glyph->bidi_type = it->bidi_it.type;
1700 else
1702 glyph->resolved_level = 0;
1703 glyph->bidi_type = UNKNOWN_BT;
1706 ++it->glyph_row->used[it->area];
1707 ++glyph;
1712 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1713 the composition. We simply produces components of the composition
1714 assuming that the terminal has a capability to layout/render it
1715 correctly. */
1717 static void
1718 produce_composite_glyph (struct it *it)
1720 if (it->cmp_it.ch < 0)
1722 struct composition *cmp = composition_table[it->cmp_it.id];
1724 it->pixel_width = cmp->width;
1726 else
1728 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
1730 it->pixel_width = composition_gstring_width (gstring, it->cmp_it.from,
1731 it->cmp_it.to, NULL);
1733 it->nglyphs = 1;
1734 if (it->glyph_row)
1735 append_composite_glyph (it);
1739 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
1740 is a face ID to be used for the glyph. What is actually appended
1741 are glyphs of type CHAR_GLYPH whose characters are in STR (which
1742 comes from it->nglyphs bytes). */
1744 static void
1745 append_glyphless_glyph (struct it *it, int face_id, const char *str)
1747 struct glyph *glyph, *end;
1748 int i;
1750 xassert (it->glyph_row);
1751 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1752 end = it->glyph_row->glyphs[1 + it->area];
1754 /* If the glyph row is reversed, we need to prepend the glyph rather
1755 than append it. */
1756 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1758 struct glyph *g;
1759 int move_by = it->pixel_width;
1761 /* Make room for the new glyphs. */
1762 if (move_by > end - glyph) /* don't overstep end of this area */
1763 move_by = end - glyph;
1764 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1765 g[move_by] = *g;
1766 glyph = it->glyph_row->glyphs[it->area];
1767 end = glyph + move_by;
1770 if (glyph >= end)
1771 return;
1772 glyph->type = CHAR_GLYPH;
1773 glyph->pixel_width = 1;
1774 glyph->face_id = face_id;
1775 glyph->padding_p = 0;
1776 glyph->charpos = CHARPOS (it->position);
1777 glyph->object = it->object;
1778 if (it->bidi_p)
1780 glyph->resolved_level = it->bidi_it.resolved_level;
1781 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1782 abort ();
1783 glyph->bidi_type = it->bidi_it.type;
1785 else
1787 glyph->resolved_level = 0;
1788 glyph->bidi_type = UNKNOWN_BT;
1791 /* BIDI Note: we put the glyphs of characters left to right, even in
1792 the REVERSED_P case because we write to the terminal
1793 left-to-right. */
1794 for (i = 0; i < it->nglyphs && glyph < end; ++i)
1796 if (i > 0)
1797 glyph[0] = glyph[-1];
1798 glyph->u.ch = str[i];
1799 ++it->glyph_row->used[it->area];
1800 ++glyph;
1804 /* Produce glyphs for a glyphless character for iterator IT.
1805 IT->glyphless_method specifies which method to use for displaying
1806 the character. See the description of enum
1807 glyphless_display_method in dispextern.h for the details.
1809 FOR_NO_FONT is nonzero if and only if this is for a character that
1810 is not supproted by the coding system of the terminal. ACRONYM, if
1811 non-nil, is an acronym string for the character.
1813 The glyphs actually produced are of type CHAR_GLYPH. */
1815 static void
1816 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
1818 int face_id;
1819 int len;
1820 char buf[9];
1821 char const *str = " ";
1823 /* Get a face ID for the glyph by utilizing a cache (the same way as
1824 done for `escape-glyph' in get_next_display_element). */
1825 if (it->f == last_glyphless_glyph_frame
1826 && it->face_id == last_glyphless_glyph_face_id)
1828 face_id = last_glyphless_glyph_merged_face_id;
1830 else
1832 /* Merge the `glyphless-char' face into the current face. */
1833 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
1834 last_glyphless_glyph_frame = it->f;
1835 last_glyphless_glyph_face_id = it->face_id;
1836 last_glyphless_glyph_merged_face_id = face_id;
1839 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
1841 /* As there's no way to produce a thin space, we produce a space
1842 of canonical width. */
1843 len = 1;
1845 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
1847 len = CHAR_WIDTH (it->c);
1848 if (len == 0)
1849 len = 1;
1850 else if (len > 4)
1851 len = 4;
1852 sprintf (buf, "[%.*s]", len, str);
1853 len += 2;
1854 str = buf;
1856 else
1858 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
1860 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
1861 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
1862 if (CONSP (acronym))
1863 acronym = XCDR (acronym);
1864 buf[0] = '[';
1865 str = STRINGP (acronym) ? SSDATA (acronym) : "";
1866 for (len = 0; len < 6 && str[len] && ASCII_BYTE_P (str[len]); len++)
1867 buf[1 + len] = str[len];
1868 buf[1 + len] = ']';
1869 len += 2;
1871 else
1873 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
1874 len = (it->c < 0x10000 ? sprintf (buf, "\\u%04X", it->c)
1875 : it->c <= MAX_UNICODE_CHAR ? sprintf (buf, "\\U%06X", it->c)
1876 : sprintf (buf, "\\x%06X", it->c));
1878 str = buf;
1881 it->pixel_width = len;
1882 it->nglyphs = len;
1883 if (it->glyph_row)
1884 append_glyphless_glyph (it, face_id, str);
1888 /* Get information about special display element WHAT in an
1889 environment described by IT. WHAT is one of IT_TRUNCATION or
1890 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1891 non-null glyph_row member. This function ensures that fields like
1892 face_id, c, len of IT are left untouched. */
1894 void
1895 produce_special_glyphs (struct it *it, enum display_element_type what)
1897 struct it temp_it;
1898 Lisp_Object gc;
1899 GLYPH glyph;
1901 temp_it = *it;
1902 temp_it.dp = NULL;
1903 temp_it.what = IT_CHARACTER;
1904 temp_it.len = 1;
1905 temp_it.object = make_number (0);
1906 memset (&temp_it.current, 0, sizeof temp_it.current);
1908 if (what == IT_CONTINUATION)
1910 /* Continuation glyph. For R2L lines, we mirror it by hand. */
1911 if (it->bidi_it.paragraph_dir == R2L)
1912 SET_GLYPH_FROM_CHAR (glyph, '/');
1913 else
1914 SET_GLYPH_FROM_CHAR (glyph, '\\');
1915 if (it->dp
1916 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc))
1917 && GLYPH_CODE_CHAR_VALID_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))
1930 && GLYPH_CODE_CHAR_VALID_P (gc))
1932 /* FIXME: Should we mirror GC for R2L lines? */
1933 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1934 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1937 else
1938 abort ();
1940 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
1941 temp_it.face_id = GLYPH_FACE (glyph);
1942 temp_it.len = CHAR_BYTES (temp_it.c);
1944 produce_glyphs (&temp_it);
1945 it->pixel_width = temp_it.pixel_width;
1946 it->nglyphs = temp_it.pixel_width;
1951 /***********************************************************************
1952 Faces
1953 ***********************************************************************/
1955 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1956 one of the enumerators from enum no_color_bit, or a bit set built
1957 from them. Some display attributes may not be used together with
1958 color; the termcap capability `NC' specifies which ones. */
1960 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1961 (tty->TN_max_colors > 0 \
1962 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1963 : 1)
1965 /* Turn appearances of face FACE_ID on tty frame F on.
1966 FACE_ID is a realized face ID number, in the face cache. */
1968 static void
1969 turn_on_face (struct frame *f, int face_id)
1971 struct face *face = FACE_FROM_ID (f, face_id);
1972 long fg = face->foreground;
1973 long bg = face->background;
1974 struct tty_display_info *tty = FRAME_TTY (f);
1976 /* Do this first because TS_end_standout_mode may be the same
1977 as TS_exit_attribute_mode, which turns all appearances off. */
1978 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1980 if (tty->TN_max_colors > 0)
1982 if (fg >= 0 && bg >= 0)
1984 /* If the terminal supports colors, we can set them
1985 below without using reverse video. The face's fg
1986 and bg colors are set as they should appear on
1987 the screen, i.e. they take the inverse-video'ness
1988 of the face already into account. */
1990 else if (inverse_video)
1992 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1993 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1994 tty_toggle_highlight (tty);
1996 else
1998 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1999 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2000 tty_toggle_highlight (tty);
2003 else
2005 /* If we can't display colors, use reverse video
2006 if the face specifies that. */
2007 if (inverse_video)
2009 if (fg == FACE_TTY_DEFAULT_FG_COLOR
2010 || bg == FACE_TTY_DEFAULT_BG_COLOR)
2011 tty_toggle_highlight (tty);
2013 else
2015 if (fg == FACE_TTY_DEFAULT_BG_COLOR
2016 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2017 tty_toggle_highlight (tty);
2022 if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
2023 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
2025 if (face->tty_dim_p && MAY_USE_WITH_COLORS_P (tty, NC_DIM))
2026 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
2028 /* Alternate charset and blinking not yet used. */
2029 if (face->tty_alt_charset_p
2030 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
2031 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
2033 if (face->tty_blinking_p
2034 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
2035 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
2037 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
2038 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
2040 if (tty->TN_max_colors > 0)
2042 const char *ts;
2043 char *p;
2045 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
2046 if (fg >= 0 && ts)
2048 p = tparam (ts, NULL, 0, (int) fg, 0, 0, 0);
2049 OUTPUT (tty, p);
2050 xfree (p);
2053 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
2054 if (bg >= 0 && ts)
2056 p = tparam (ts, NULL, 0, (int) bg, 0, 0, 0);
2057 OUTPUT (tty, p);
2058 xfree (p);
2064 /* Turn off appearances of face FACE_ID on tty frame F. */
2066 static void
2067 turn_off_face (struct frame *f, int face_id)
2069 struct face *face = FACE_FROM_ID (f, face_id);
2070 struct tty_display_info *tty = FRAME_TTY (f);
2072 xassert (face != NULL);
2074 if (tty->TS_exit_attribute_mode)
2076 /* Capability "me" will turn off appearance modes double-bright,
2077 half-bright, reverse-video, standout, underline. It may or
2078 may not turn off alt-char-mode. */
2079 if (face->tty_bold_p
2080 || face->tty_dim_p
2081 || face->tty_reverse_p
2082 || face->tty_alt_charset_p
2083 || face->tty_blinking_p
2084 || face->tty_underline_p)
2086 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
2087 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
2088 tty->standout_mode = 0;
2091 if (face->tty_alt_charset_p)
2092 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2094 else
2096 /* If we don't have "me" we can only have those appearances
2097 that have exit sequences defined. */
2098 if (face->tty_alt_charset_p)
2099 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2101 if (face->tty_underline_p)
2102 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
2105 /* Switch back to default colors. */
2106 if (tty->TN_max_colors > 0
2107 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2108 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2109 || (face->background != FACE_TTY_DEFAULT_COLOR
2110 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
2111 OUTPUT1_IF (tty, tty->TS_orig_pair);
2115 /* Return non-zero if the terminal on frame F supports all of the
2116 capabilities in CAPS simultaneously, with foreground and background
2117 colors FG and BG. */
2120 tty_capable_p (struct tty_display_info *tty, unsigned int caps,
2121 unsigned long fg, unsigned long bg)
2123 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2124 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2125 return 0;
2127 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2128 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2129 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2130 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
2131 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
2132 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
2134 /* We can do it! */
2135 return 1;
2138 /* Return non-zero if the terminal is capable to display colors. */
2140 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2141 0, 1, 0,
2142 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2144 TERMINAL can be a terminal object, a frame, or nil (meaning the
2145 selected frame's terminal). This function always returns nil if
2146 TERMINAL does not refer to a text-only terminal. */)
2147 (Lisp_Object terminal)
2149 struct terminal *t = get_tty_terminal (terminal, 0);
2150 if (!t)
2151 return Qnil;
2152 else
2153 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
2156 /* Return the number of supported colors. */
2157 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2158 Stty_display_color_cells, 0, 1, 0,
2159 doc: /* Return the number of colors supported by the tty device TERMINAL.
2161 TERMINAL can be a terminal object, a frame, or nil (meaning the
2162 selected frame's terminal). This function always returns 0 if
2163 TERMINAL does not refer to a text-only terminal. */)
2164 (Lisp_Object terminal)
2166 struct terminal *t = get_tty_terminal (terminal, 0);
2167 if (!t)
2168 return make_number (0);
2169 else
2170 return make_number (t->display_info.tty->TN_max_colors);
2173 #ifndef DOS_NT
2175 /* Declare here rather than in the function, as in the rest of Emacs,
2176 to work around an HPUX compiler bug (?). See
2177 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2178 static int default_max_colors;
2179 static int default_max_pairs;
2180 static int default_no_color_video;
2181 static char *default_orig_pair;
2182 static char *default_set_foreground;
2183 static char *default_set_background;
2185 /* Save or restore the default color-related capabilities of this
2186 terminal. */
2187 static void
2188 tty_default_color_capabilities (struct tty_display_info *tty, int save)
2191 if (save)
2193 xfree (default_orig_pair);
2194 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
2196 xfree (default_set_foreground);
2197 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
2198 : NULL;
2200 xfree (default_set_background);
2201 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
2202 : NULL;
2204 default_max_colors = tty->TN_max_colors;
2205 default_max_pairs = tty->TN_max_pairs;
2206 default_no_color_video = tty->TN_no_color_video;
2208 else
2210 tty->TS_orig_pair = default_orig_pair;
2211 tty->TS_set_foreground = default_set_foreground;
2212 tty->TS_set_background = default_set_background;
2213 tty->TN_max_colors = default_max_colors;
2214 tty->TN_max_pairs = default_max_pairs;
2215 tty->TN_no_color_video = default_no_color_video;
2219 /* Setup one of the standard tty color schemes according to MODE.
2220 MODE's value is generally the number of colors which we want to
2221 support; zero means set up for the default capabilities, the ones
2222 we saw at init_tty time; -1 means turn off color support. */
2223 static void
2224 tty_setup_colors (struct tty_display_info *tty, int mode)
2226 /* Canonicalize all negative values of MODE. */
2227 if (mode < -1)
2228 mode = -1;
2230 switch (mode)
2232 case -1: /* no colors at all */
2233 tty->TN_max_colors = 0;
2234 tty->TN_max_pairs = 0;
2235 tty->TN_no_color_video = 0;
2236 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2237 break;
2238 case 0: /* default colors, if any */
2239 default:
2240 tty_default_color_capabilities (tty, 0);
2241 break;
2242 case 8: /* 8 standard ANSI colors */
2243 tty->TS_orig_pair = "\033[0m";
2244 #ifdef TERMINFO
2245 tty->TS_set_foreground = "\033[3%p1%dm";
2246 tty->TS_set_background = "\033[4%p1%dm";
2247 #else
2248 tty->TS_set_foreground = "\033[3%dm";
2249 tty->TS_set_background = "\033[4%dm";
2250 #endif
2251 tty->TN_max_colors = 8;
2252 tty->TN_max_pairs = 64;
2253 tty->TN_no_color_video = 0;
2254 break;
2258 void
2259 set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
2261 Lisp_Object tem, val;
2262 Lisp_Object color_mode;
2263 int mode;
2264 Lisp_Object tty_color_mode_alist
2265 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
2267 tem = assq_no_quit (Qtty_color_mode, f->param_alist);
2268 val = CONSP (tem) ? XCDR (tem) : Qnil;
2270 if (INTEGERP (val))
2271 color_mode = val;
2272 else if (SYMBOLP (tty_color_mode_alist))
2274 tem = Fassq (val, Fsymbol_value (tty_color_mode_alist));
2275 color_mode = CONSP (tem) ? XCDR (tem) : Qnil;
2277 else
2278 color_mode = Qnil;
2280 mode = INTEGERP (color_mode) ? XINT (color_mode) : 0;
2282 if (mode != tty->previous_color_mode)
2284 Lisp_Object funsym = intern ("tty-set-up-initial-frame-faces");
2285 tty->previous_color_mode = mode;
2286 tty_setup_colors (tty , mode);
2287 /* This recomputes all the faces given the new color definitions. */
2288 safe_call (1, &funsym);
2292 #endif /* !DOS_NT */
2296 /* Return the tty display object specified by TERMINAL. */
2298 static struct terminal *
2299 get_tty_terminal (Lisp_Object terminal, int throw)
2301 struct terminal *t = get_terminal (terminal, throw);
2303 if (t && t->type != output_termcap && t->type != output_msdos_raw)
2305 if (throw)
2306 error ("Device %d is not a termcap terminal device", t->id);
2307 else
2308 return NULL;
2311 return t;
2314 /* Return an active termcap device that uses the tty device with the
2315 given name.
2317 This function ignores suspended devices.
2319 Returns NULL if the named terminal device is not opened. */
2321 struct terminal *
2322 get_named_tty (const char *name)
2324 struct terminal *t;
2326 if (!name)
2327 abort ();
2329 for (t = terminal_list; t; t = t->next_terminal)
2331 if ((t->type == output_termcap || t->type == output_msdos_raw)
2332 && !strcmp (t->display_info.tty->name, name)
2333 && TERMINAL_ACTIVE_P (t))
2334 return t;
2337 return 0;
2341 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2342 doc: /* Return the type of the tty device that TERMINAL uses.
2343 Returns nil if TERMINAL is not on a tty device.
2345 TERMINAL can be a terminal object, a frame, or nil (meaning the
2346 selected frame's terminal). */)
2347 (Lisp_Object terminal)
2349 struct terminal *t = get_terminal (terminal, 1);
2351 if (t->type != output_termcap && t->type != output_msdos_raw)
2352 return Qnil;
2354 if (t->display_info.tty->type)
2355 return build_string (t->display_info.tty->type);
2356 else
2357 return Qnil;
2360 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2361 doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2363 TERMINAL can be a terminal object, a frame, or nil (meaning the
2364 selected frame's terminal). This function always returns nil if
2365 TERMINAL is not on a tty device. */)
2366 (Lisp_Object terminal)
2368 struct terminal *t = get_terminal (terminal, 1);
2370 if ((t->type != output_termcap && t->type != output_msdos_raw)
2371 || strcmp (t->display_info.tty->name, DEV_TTY) != 0)
2372 return Qnil;
2373 else
2374 return Qt;
2377 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2378 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2379 This is used to override the terminfo data, for certain terminals that
2380 do not really do underlining, but say that they do. This function has
2381 no effect if used on a non-tty terminal.
2383 TERMINAL can be a terminal object, a frame or nil (meaning the
2384 selected frame's terminal). This function always returns nil if
2385 TERMINAL does not refer to a text-only terminal. */)
2386 (Lisp_Object terminal)
2388 struct terminal *t = get_terminal (terminal, 1);
2390 if (t->type == output_termcap)
2391 t->display_info.tty->TS_enter_underline_mode = 0;
2392 return Qnil;
2397 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2398 doc: /* Suspend the terminal device TTY.
2400 The device is restored to its default state, and Emacs ceases all
2401 access to the tty device. Frames that use the device are not deleted,
2402 but input is not read from them and if they change, their display is
2403 not updated.
2405 TTY may be a terminal object, a frame, or nil for the terminal device
2406 of the currently selected frame.
2408 This function runs `suspend-tty-functions' after suspending the
2409 device. The functions are run with one arg, the id of the suspended
2410 terminal device.
2412 `suspend-tty' does nothing if it is called on a device that is already
2413 suspended.
2415 A suspended tty may be resumed by calling `resume-tty' on it. */)
2416 (Lisp_Object tty)
2418 struct terminal *t = get_tty_terminal (tty, 1);
2419 FILE *f;
2421 if (!t)
2422 error ("Unknown tty device");
2424 f = t->display_info.tty->input;
2426 if (f)
2428 /* First run `suspend-tty-functions' and then clean up the tty
2429 state because `suspend-tty-functions' might need to change
2430 the tty state. */
2431 Lisp_Object args[2];
2432 args[0] = intern ("suspend-tty-functions");
2433 XSETTERMINAL (args[1], t);
2434 Frun_hook_with_args (2, args);
2436 reset_sys_modes (t->display_info.tty);
2437 delete_keyboard_wait_descriptor (fileno (f));
2439 #ifndef MSDOS
2440 fclose (f);
2441 if (f != t->display_info.tty->output)
2442 fclose (t->display_info.tty->output);
2443 #endif
2445 t->display_info.tty->input = 0;
2446 t->display_info.tty->output = 0;
2448 if (FRAMEP (t->display_info.tty->top_frame))
2449 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2453 /* Clear display hooks to prevent further output. */
2454 clear_tty_hooks (t);
2456 return Qnil;
2459 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2460 doc: /* Resume the previously suspended terminal device TTY.
2461 The terminal is opened and reinitialized. Frames that are on the
2462 suspended terminal are revived.
2464 It is an error to resume a terminal while another terminal is active
2465 on the same device.
2467 This function runs `resume-tty-functions' after resuming the terminal.
2468 The functions are run with one arg, the id of the resumed terminal
2469 device.
2471 `resume-tty' does nothing if it is called on a device that is not
2472 suspended.
2474 TTY may be a terminal object, a frame, or nil (meaning the selected
2475 frame's terminal). */)
2476 (Lisp_Object tty)
2478 struct terminal *t = get_tty_terminal (tty, 1);
2479 int fd;
2481 if (!t)
2482 error ("Unknown tty device");
2484 if (!t->display_info.tty->input)
2486 if (get_named_tty (t->display_info.tty->name))
2487 error ("Cannot resume display while another display is active on the same device");
2489 #ifdef MSDOS
2490 t->display_info.tty->output = stdout;
2491 t->display_info.tty->input = stdin;
2492 #else /* !MSDOS */
2493 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2495 if (fd == -1)
2496 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2498 if (strcmp (t->display_info.tty->name, DEV_TTY))
2499 dissociate_if_controlling_tty (fd);
2501 t->display_info.tty->output = fdopen (fd, "w+");
2502 t->display_info.tty->input = t->display_info.tty->output;
2503 #endif
2505 add_keyboard_wait_descriptor (fd);
2507 if (FRAMEP (t->display_info.tty->top_frame))
2509 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2510 int width, height;
2511 int old_height = FRAME_COLS (f);
2512 int old_width = FRAME_LINES (f);
2514 /* Check if terminal/window size has changed while the frame
2515 was suspended. */
2516 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2517 if (width != old_width || height != old_height)
2518 change_frame_size (f, height, width, 0, 0, 0);
2519 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2522 set_tty_hooks (t);
2523 init_sys_modes (t->display_info.tty);
2526 /* Run `resume-tty-functions'. */
2527 Lisp_Object args[2];
2528 args[0] = intern ("resume-tty-functions");
2529 XSETTERMINAL (args[1], t);
2530 Frun_hook_with_args (2, args);
2534 set_tty_hooks (t);
2536 return Qnil;
2540 /***********************************************************************
2541 Mouse
2542 ***********************************************************************/
2544 #ifdef HAVE_GPM
2546 #ifndef HAVE_WINDOW_SYSTEM
2547 void
2548 term_mouse_moveto (int x, int y)
2550 /* TODO: how to set mouse position?
2551 const char *name;
2552 int fd;
2553 name = (const char *) ttyname (0);
2554 fd = open (name, O_WRONLY);
2555 SOME_FUNCTION (x, y, fd);
2556 close (fd);
2557 last_mouse_x = x;
2558 last_mouse_y = y; */
2560 #endif /* HAVE_WINDOW_SYSTEM */
2562 /* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2563 void
2564 tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row,
2565 int start_hpos, int end_hpos,
2566 enum draw_glyphs_face draw)
2568 int nglyphs = end_hpos - start_hpos;
2569 struct frame *f = XFRAME (WINDOW_FRAME (w));
2570 struct tty_display_info *tty = FRAME_TTY (f);
2571 int face_id = tty->mouse_highlight.mouse_face_face_id;
2572 int save_x, save_y, pos_x, pos_y;
2574 if (end_hpos >= row->used[TEXT_AREA])
2575 nglyphs = row->used[TEXT_AREA] - start_hpos;
2577 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2578 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos + WINDOW_LEFT_EDGE_X (w);
2580 /* Save current cursor co-ordinates. */
2581 save_y = curY (tty);
2582 save_x = curX (tty);
2583 cursor_to (f, pos_y, pos_x);
2585 if (draw == DRAW_MOUSE_FACE)
2586 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2587 nglyphs, face_id);
2588 else if (draw == DRAW_NORMAL_TEXT)
2589 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
2591 cursor_to (f, save_y, save_x);
2594 static int
2595 term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2597 /* Has the mouse moved off the glyph it was on at the last sighting? */
2598 if (event->x != last_mouse_x || event->y != last_mouse_y)
2600 frame->mouse_moved = 1;
2601 note_mouse_highlight (frame, event->x, event->y);
2602 /* Remember which glyph we're now on. */
2603 last_mouse_x = event->x;
2604 last_mouse_y = event->y;
2605 return 1;
2607 return 0;
2610 /* Return the current position of the mouse.
2612 Set *f to the frame the mouse is in, or zero if the mouse is in no
2613 Emacs frame. If it is set to zero, all the other arguments are
2614 garbage.
2616 Set *bar_window to Qnil, and *x and *y to the column and
2617 row of the character cell the mouse is over.
2619 Set *timeptr to the time the mouse was at the returned position.
2621 This clears mouse_moved until the next motion
2622 event arrives. */
2623 static void
2624 term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2625 enum scroll_bar_part *part, Lisp_Object *x,
2626 Lisp_Object *y, Time *timeptr)
2628 struct timeval now;
2629 Time sec, usec;
2631 *fp = SELECTED_FRAME ();
2632 (*fp)->mouse_moved = 0;
2634 *bar_window = Qnil;
2635 *part = 0;
2637 XSETINT (*x, last_mouse_x);
2638 XSETINT (*y, last_mouse_y);
2639 gettimeofday(&now, 0);
2640 sec = now.tv_sec;
2641 usec = now.tv_usec;
2642 *timeptr = (sec * 1000) + (usec / 1000);
2645 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2647 If the event is a button press, then note that we have grabbed
2648 the mouse. */
2650 static Lisp_Object
2651 term_mouse_click (struct input_event *result, Gpm_Event *event,
2652 struct frame *f)
2654 struct timeval now;
2655 int i, j;
2657 result->kind = GPM_CLICK_EVENT;
2658 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2660 if (event->buttons & j) {
2661 result->code = i; /* button number */
2662 break;
2665 gettimeofday(&now, 0);
2666 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2668 if (event->type & GPM_UP)
2669 result->modifiers = up_modifier;
2670 else if (event->type & GPM_DOWN)
2671 result->modifiers = down_modifier;
2672 else
2673 result->modifiers = 0;
2675 if (event->type & GPM_SINGLE)
2676 result->modifiers |= click_modifier;
2678 if (event->type & GPM_DOUBLE)
2679 result->modifiers |= double_modifier;
2681 if (event->type & GPM_TRIPLE)
2682 result->modifiers |= triple_modifier;
2684 if (event->type & GPM_DRAG)
2685 result->modifiers |= drag_modifier;
2687 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
2689 /* 1 << KG_SHIFT */
2690 if (event->modifiers & (1 << 0))
2691 result->modifiers |= shift_modifier;
2693 /* 1 << KG_CTRL */
2694 if (event->modifiers & (1 << 2))
2695 result->modifiers |= ctrl_modifier;
2697 /* 1 << KG_ALT || KG_ALTGR */
2698 if (event->modifiers & (1 << 3)
2699 || event->modifiers & (1 << 1))
2700 result->modifiers |= meta_modifier;
2703 XSETINT (result->x, event->x);
2704 XSETINT (result->y, event->y);
2705 XSETFRAME (result->frame_or_window, f);
2706 result->arg = Qnil;
2707 return Qnil;
2711 handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
2713 struct frame *f = XFRAME (tty->top_frame);
2714 struct input_event ie;
2715 int do_help = 0;
2716 int count = 0;
2718 EVENT_INIT (ie);
2719 ie.kind = NO_EVENT;
2720 ie.arg = Qnil;
2722 if (event->type & (GPM_MOVE | GPM_DRAG)) {
2723 previous_help_echo_string = help_echo_string;
2724 help_echo_string = Qnil;
2726 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
2728 if (!term_mouse_movement (f, event))
2729 help_echo_string = previous_help_echo_string;
2731 /* If the contents of the global variable help_echo_string
2732 has changed, generate a HELP_EVENT. */
2733 if (!NILP (help_echo_string)
2734 || !NILP (previous_help_echo_string))
2735 do_help = 1;
2737 goto done;
2739 else {
2740 f->mouse_moved = 0;
2741 term_mouse_click (&ie, event, f);
2744 done:
2745 if (ie.kind != NO_EVENT)
2747 kbd_buffer_store_event_hold (&ie, hold_quit);
2748 count++;
2751 if (do_help
2752 && !(hold_quit && hold_quit->kind != NO_EVENT))
2754 Lisp_Object frame;
2756 if (f)
2757 XSETFRAME (frame, f);
2758 else
2759 frame = Qnil;
2761 gen_help_event (help_echo_string, frame, help_echo_window,
2762 help_echo_object, help_echo_pos);
2763 count++;
2766 return count;
2769 DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
2770 0, 0, 0,
2771 doc: /* Open a connection to Gpm.
2772 Gpm-mouse can only be activated for one tty at a time. */)
2773 (void)
2775 struct frame *f = SELECTED_FRAME ();
2776 struct tty_display_info *tty
2777 = ((f)->output_method == output_termcap
2778 ? (f)->terminal->display_info.tty : NULL);
2779 Gpm_Connect connection;
2781 if (!tty)
2782 error ("Gpm-mouse only works in the GNU/Linux console");
2783 if (gpm_tty == tty)
2784 return Qnil; /* Already activated, nothing to do. */
2785 if (gpm_tty)
2786 error ("Gpm-mouse can only be activated for one tty at a time");
2788 connection.eventMask = ~0;
2789 connection.defaultMask = ~GPM_HARD;
2790 connection.maxMod = ~0;
2791 connection.minMod = 0;
2792 gpm_zerobased = 1;
2794 if (Gpm_Open (&connection, 0) < 0)
2795 error ("Gpm-mouse failed to connect to the gpm daemon");
2796 else
2798 gpm_tty = tty;
2799 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2800 to generate SIGIOs. Apparently we need to call reset_sys_modes
2801 before calling init_sys_modes. */
2802 reset_sys_modes (tty);
2803 init_sys_modes (tty);
2804 add_gpm_wait_descriptor (gpm_fd);
2805 return Qnil;
2809 void
2810 close_gpm (int fd)
2812 if (fd >= 0)
2813 delete_gpm_wait_descriptor (fd);
2814 while (Gpm_Close()); /* close all the stack */
2815 gpm_tty = NULL;
2818 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
2819 0, 0, 0,
2820 doc: /* Close a connection to Gpm. */)
2821 (void)
2823 struct frame *f = SELECTED_FRAME ();
2824 struct tty_display_info *tty
2825 = ((f)->output_method == output_termcap
2826 ? (f)->terminal->display_info.tty : NULL);
2828 if (!tty || gpm_tty != tty)
2829 return Qnil; /* Not activated on this terminal, nothing to do. */
2831 close_gpm (gpm_fd);
2832 return Qnil;
2834 #endif /* HAVE_GPM */
2837 #ifndef MSDOS
2838 /***********************************************************************
2839 Initialization
2840 ***********************************************************************/
2842 /* Initialize the tty-dependent part of frame F. The frame must
2843 already have its device initialized. */
2845 void
2846 create_tty_output (struct frame *f)
2848 struct tty_output *t;
2850 if (! FRAME_TERMCAP_P (f))
2851 abort ();
2853 t = xmalloc (sizeof (struct tty_output));
2854 memset (t, 0, sizeof (struct tty_output));
2856 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
2858 f->output_data.tty = t;
2861 /* Delete frame F's face cache, and its tty-dependent part. */
2863 static void
2864 tty_free_frame_resources (struct frame *f)
2866 if (! FRAME_TERMCAP_P (f))
2867 abort ();
2869 if (FRAME_FACE_CACHE (f))
2870 free_frame_faces (f);
2872 xfree (f->output_data.tty);
2875 #else /* MSDOS */
2877 /* Delete frame F's face cache. */
2879 static void
2880 tty_free_frame_resources (struct frame *f)
2882 if (! FRAME_TERMCAP_P (f) && ! FRAME_MSDOS_P (f))
2883 abort ();
2885 if (FRAME_FACE_CACHE (f))
2886 free_frame_faces (f);
2888 #endif /* MSDOS */
2890 /* Reset the hooks in TERMINAL. */
2892 static void
2893 clear_tty_hooks (struct terminal *terminal)
2895 terminal->rif = 0;
2896 terminal->cursor_to_hook = 0;
2897 terminal->raw_cursor_to_hook = 0;
2898 terminal->clear_to_end_hook = 0;
2899 terminal->clear_frame_hook = 0;
2900 terminal->clear_end_of_line_hook = 0;
2901 terminal->ins_del_lines_hook = 0;
2902 terminal->insert_glyphs_hook = 0;
2903 terminal->write_glyphs_hook = 0;
2904 terminal->delete_glyphs_hook = 0;
2905 terminal->ring_bell_hook = 0;
2906 terminal->reset_terminal_modes_hook = 0;
2907 terminal->set_terminal_modes_hook = 0;
2908 terminal->update_begin_hook = 0;
2909 terminal->update_end_hook = 0;
2910 terminal->set_terminal_window_hook = 0;
2911 terminal->mouse_position_hook = 0;
2912 terminal->frame_rehighlight_hook = 0;
2913 terminal->frame_raise_lower_hook = 0;
2914 terminal->fullscreen_hook = 0;
2915 terminal->set_vertical_scroll_bar_hook = 0;
2916 terminal->condemn_scroll_bars_hook = 0;
2917 terminal->redeem_scroll_bar_hook = 0;
2918 terminal->judge_scroll_bars_hook = 0;
2919 terminal->read_socket_hook = 0;
2920 terminal->frame_up_to_date_hook = 0;
2922 /* Leave these two set, or suspended frames are not deleted
2923 correctly. */
2924 terminal->delete_frame_hook = &tty_free_frame_resources;
2925 terminal->delete_terminal_hook = &delete_tty;
2928 /* Initialize hooks in TERMINAL with the values needed for a tty. */
2930 static void
2931 set_tty_hooks (struct terminal *terminal)
2933 terminal->rif = 0; /* ttys don't support window-based redisplay. */
2935 terminal->cursor_to_hook = &tty_cursor_to;
2936 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
2938 terminal->clear_to_end_hook = &tty_clear_to_end;
2939 terminal->clear_frame_hook = &tty_clear_frame;
2940 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
2942 terminal->ins_del_lines_hook = &tty_ins_del_lines;
2944 terminal->insert_glyphs_hook = &tty_insert_glyphs;
2945 terminal->write_glyphs_hook = &tty_write_glyphs;
2946 terminal->delete_glyphs_hook = &tty_delete_glyphs;
2948 terminal->ring_bell_hook = &tty_ring_bell;
2950 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
2951 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
2952 terminal->update_begin_hook = 0; /* Not needed. */
2953 terminal->update_end_hook = &tty_update_end;
2954 terminal->set_terminal_window_hook = &tty_set_terminal_window;
2956 terminal->mouse_position_hook = 0; /* Not needed. */
2957 terminal->frame_rehighlight_hook = 0; /* Not needed. */
2958 terminal->frame_raise_lower_hook = 0; /* Not needed. */
2960 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
2961 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
2962 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
2963 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
2965 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
2966 terminal->frame_up_to_date_hook = 0; /* Not needed. */
2968 terminal->delete_frame_hook = &tty_free_frame_resources;
2969 terminal->delete_terminal_hook = &delete_tty;
2972 /* Drop the controlling terminal if fd is the same device. */
2973 static void
2974 dissociate_if_controlling_tty (int fd)
2976 #ifndef DOS_NT
2977 int pgid = tcgetpgrp (fd); /* If tcgetpgrp succeeds, fd is the ctty. */
2978 if (pgid != -1)
2980 #if defined (USG5)
2981 setpgrp ();
2982 no_controlling_tty = 1;
2983 #elif defined (CYGWIN)
2984 setsid ();
2985 no_controlling_tty = 1;
2986 #else
2987 #ifdef TIOCNOTTY /* Try BSD ioctls. */
2988 sigblock (sigmask (SIGTTOU));
2989 fd = emacs_open (DEV_TTY, O_RDWR, 0);
2990 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
2992 no_controlling_tty = 1;
2994 if (fd != -1)
2995 emacs_close (fd);
2996 sigunblock (sigmask (SIGTTOU));
2997 #else
2998 /* Unknown system. */
2999 croak ();
3000 #endif /* ! TIOCNOTTY */
3001 #endif /* ! USG */
3003 #endif /* !DOS_NT */
3006 /* Create a termcap display on the tty device with the given name and
3007 type.
3009 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3010 Otherwise NAME should be a path to the tty device file,
3011 e.g. "/dev/pts/7".
3013 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3015 If MUST_SUCCEED is true, then all errors are fatal. */
3017 struct terminal *
3018 init_tty (const char *name, const char *terminal_type, int must_succeed)
3020 char *area = NULL;
3021 char **address = &area;
3022 int buffer_size = 4096;
3023 int status;
3024 struct tty_display_info *tty = NULL;
3025 struct terminal *terminal = NULL;
3026 int ctty = 0; /* 1 if asked to open controlling tty. */
3028 if (!terminal_type)
3029 maybe_fatal (must_succeed, 0,
3030 "Unknown terminal type",
3031 "Unknown terminal type");
3033 if (name == NULL)
3034 name = DEV_TTY;
3035 if (!strcmp (name, DEV_TTY))
3036 ctty = 1;
3038 /* If we already have a terminal on the given device, use that. If
3039 all such terminals are suspended, create a new one instead. */
3040 /* XXX Perhaps this should be made explicit by having init_tty
3041 always create a new terminal and separating terminal and frame
3042 creation on Lisp level. */
3043 terminal = get_named_tty (name);
3044 if (terminal)
3045 return terminal;
3047 terminal = create_terminal ();
3048 #ifdef MSDOS
3049 if (been_here > 0)
3050 maybe_fatal (0, 0, "Attempt to create another terminal %s", "",
3051 name, "");
3052 been_here = 1;
3053 tty = &the_only_display_info;
3054 #else
3055 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
3056 #endif
3057 memset (tty, 0, sizeof (struct tty_display_info));
3058 tty->next = tty_list;
3059 tty_list = tty;
3061 terminal->type = output_termcap;
3062 terminal->display_info.tty = tty;
3063 tty->terminal = terminal;
3065 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3066 Wcm_clear (tty);
3068 encode_terminal_src_size = 0;
3069 encode_terminal_dst_size = 0;
3072 #ifndef DOS_NT
3073 set_tty_hooks (terminal);
3076 int fd;
3077 FILE *file;
3079 #ifdef O_IGNORE_CTTY
3080 if (!ctty)
3081 /* Open the terminal device. Don't recognize it as our
3082 controlling terminal, and don't make it the controlling tty
3083 if we don't have one at the moment. */
3084 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
3085 else
3086 #endif /* O_IGNORE_CTTY */
3087 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3088 defined on Hurd. On other systems, we need to explicitly
3089 dissociate ourselves from the controlling tty when we want to
3090 open a frame on the same terminal. */
3091 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
3093 tty->name = xstrdup (name);
3094 terminal->name = xstrdup (name);
3096 if (fd < 0)
3097 maybe_fatal (must_succeed, terminal,
3098 "Could not open file: %s",
3099 "Could not open file: %s",
3100 name);
3101 if (!isatty (fd))
3103 close (fd);
3104 maybe_fatal (must_succeed, terminal,
3105 "Not a tty device: %s",
3106 "Not a tty device: %s",
3107 name);
3110 #ifndef O_IGNORE_CTTY
3111 if (!ctty)
3112 dissociate_if_controlling_tty (fd);
3113 #endif
3115 file = fdopen (fd, "w+");
3116 tty->input = file;
3117 tty->output = file;
3120 tty->type = xstrdup (terminal_type);
3122 add_keyboard_wait_descriptor (fileno (tty->input));
3124 Wcm_clear (tty);
3126 tty->termcap_term_buffer = (char *) xmalloc (buffer_size);
3128 /* On some systems, tgetent tries to access the controlling
3129 terminal. */
3130 sigblock (sigmask (SIGTTOU));
3131 status = tgetent (tty->termcap_term_buffer, terminal_type);
3132 sigunblock (sigmask (SIGTTOU));
3134 if (status < 0)
3136 #ifdef TERMINFO
3137 maybe_fatal (must_succeed, terminal,
3138 "Cannot open terminfo database file",
3139 "Cannot open terminfo database file");
3140 #else
3141 maybe_fatal (must_succeed, terminal,
3142 "Cannot open termcap database file",
3143 "Cannot open termcap database file");
3144 #endif
3146 if (status == 0)
3148 maybe_fatal (must_succeed, terminal,
3149 "Terminal type %s is not defined",
3150 "Terminal type %s is not defined.\n\
3151 If that is not the actual type of terminal you have,\n\
3152 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3153 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3154 #ifdef TERMINFO
3155 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3156 #else
3157 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3158 #endif
3159 terminal_type);
3162 #ifndef TERMINFO
3163 if (strlen (tty->termcap_term_buffer) >= buffer_size)
3164 abort ();
3165 buffer_size = strlen (tty->termcap_term_buffer);
3166 #endif
3167 tty->termcap_strings_buffer = area = (char *) xmalloc (buffer_size);
3168 tty->TS_ins_line = tgetstr ("al", address);
3169 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3170 tty->TS_bell = tgetstr ("bl", address);
3171 BackTab (tty) = tgetstr ("bt", address);
3172 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3173 tty->TS_clr_line = tgetstr ("ce", address);
3174 tty->TS_clr_frame = tgetstr ("cl", address);
3175 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3176 AbsPosition (tty) = tgetstr ("cm", address);
3177 CR (tty) = tgetstr ("cr", address);
3178 tty->TS_set_scroll_region = tgetstr ("cs", address);
3179 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
3180 RowPosition (tty) = tgetstr ("cv", address);
3181 tty->TS_del_char = tgetstr ("dc", address);
3182 tty->TS_del_multi_chars = tgetstr ("DC", address);
3183 tty->TS_del_line = tgetstr ("dl", address);
3184 tty->TS_del_multi_lines = tgetstr ("DL", address);
3185 tty->TS_delete_mode = tgetstr ("dm", address);
3186 tty->TS_end_delete_mode = tgetstr ("ed", address);
3187 tty->TS_end_insert_mode = tgetstr ("ei", address);
3188 Home (tty) = tgetstr ("ho", address);
3189 tty->TS_ins_char = tgetstr ("ic", address);
3190 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3191 tty->TS_insert_mode = tgetstr ("im", address);
3192 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3193 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3194 tty->TS_keypad_mode = tgetstr ("ks", address);
3195 LastLine (tty) = tgetstr ("ll", address);
3196 Right (tty) = tgetstr ("nd", address);
3197 Down (tty) = tgetstr ("do", address);
3198 if (!Down (tty))
3199 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
3200 if (tgetflag ("bs"))
3201 Left (tty) = "\b"; /* can't possibly be longer! */
3202 else /* (Actually, "bs" is obsolete...) */
3203 Left (tty) = tgetstr ("le", address);
3204 if (!Left (tty))
3205 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
3206 tty->TS_pad_char = tgetstr ("pc", address);
3207 tty->TS_repeat = tgetstr ("rp", address);
3208 tty->TS_end_standout_mode = tgetstr ("se", address);
3209 tty->TS_fwd_scroll = tgetstr ("sf", address);
3210 tty->TS_standout_mode = tgetstr ("so", address);
3211 tty->TS_rev_scroll = tgetstr ("sr", address);
3212 tty->Wcm->cm_tab = tgetstr ("ta", address);
3213 tty->TS_end_termcap_modes = tgetstr ("te", address);
3214 tty->TS_termcap_modes = tgetstr ("ti", address);
3215 Up (tty) = tgetstr ("up", address);
3216 tty->TS_visible_bell = tgetstr ("vb", address);
3217 tty->TS_cursor_normal = tgetstr ("ve", address);
3218 tty->TS_cursor_visible = tgetstr ("vs", address);
3219 tty->TS_cursor_invisible = tgetstr ("vi", address);
3220 tty->TS_set_window = tgetstr ("wi", address);
3222 tty->TS_enter_underline_mode = tgetstr ("us", address);
3223 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3224 tty->TS_enter_bold_mode = tgetstr ("md", address);
3225 tty->TS_enter_dim_mode = tgetstr ("mh", address);
3226 tty->TS_enter_blink_mode = tgetstr ("mb", address);
3227 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3228 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3229 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3230 tty->TS_exit_attribute_mode = tgetstr ("me", address);
3232 MultiUp (tty) = tgetstr ("UP", address);
3233 MultiDown (tty) = tgetstr ("DO", address);
3234 MultiLeft (tty) = tgetstr ("LE", address);
3235 MultiRight (tty) = tgetstr ("RI", address);
3237 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
3238 color because we can't switch back to the default foreground and
3239 background. */
3240 tty->TS_orig_pair = tgetstr ("op", address);
3241 if (tty->TS_orig_pair)
3243 tty->TS_set_foreground = tgetstr ("AF", address);
3244 tty->TS_set_background = tgetstr ("AB", address);
3245 if (!tty->TS_set_foreground)
3247 /* SVr4. */
3248 tty->TS_set_foreground = tgetstr ("Sf", address);
3249 tty->TS_set_background = tgetstr ("Sb", address);
3252 tty->TN_max_colors = tgetnum ("Co");
3253 tty->TN_max_pairs = tgetnum ("pa");
3255 tty->TN_no_color_video = tgetnum ("NC");
3256 if (tty->TN_no_color_video == -1)
3257 tty->TN_no_color_video = 0;
3260 tty_default_color_capabilities (tty, 1);
3262 MagicWrap (tty) = tgetflag ("xn");
3263 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3264 the former flag imply the latter. */
3265 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
3266 terminal->memory_below_frame = tgetflag ("db");
3267 tty->TF_hazeltine = tgetflag ("hz");
3268 terminal->must_write_spaces = tgetflag ("in");
3269 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3270 tty->TF_insmode_motion = tgetflag ("mi");
3271 tty->TF_standout_motion = tgetflag ("ms");
3272 tty->TF_underscore = tgetflag ("ul");
3273 tty->TF_teleray = tgetflag ("xt");
3275 #else /* DOS_NT */
3276 #ifdef WINDOWSNT
3278 struct frame *f = XFRAME (selected_frame);
3280 initialize_w32_display (terminal);
3282 FrameRows (tty) = FRAME_LINES (f);
3283 FrameCols (tty) = FRAME_COLS (f);
3284 tty->specified_window = FRAME_LINES (f);
3286 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3287 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
3288 terminal->char_ins_del_ok = 1;
3289 baud_rate = 19200;
3291 #else /* MSDOS */
3293 int height, width;
3294 if (strcmp (terminal_type, "internal") == 0)
3295 terminal->type = output_msdos_raw;
3296 initialize_msdos_display (terminal);
3298 get_tty_size (fileno (tty->input), &width, &height);
3299 FrameCols (tty) = width;
3300 FrameRows (tty) = height;
3301 terminal->char_ins_del_ok = 0;
3302 init_baud_rate (fileno (tty->input));
3304 #endif /* MSDOS */
3305 tty->output = stdout;
3306 tty->input = stdin;
3307 /* The following two are inaccessible from w32console.c. */
3308 terminal->delete_frame_hook = &tty_free_frame_resources;
3309 terminal->delete_terminal_hook = &delete_tty;
3311 tty->name = xstrdup (name);
3312 terminal->name = xstrdup (name);
3313 tty->type = xstrdup (terminal_type);
3315 add_keyboard_wait_descriptor (0);
3317 tty->delete_in_insert_mode = 1;
3319 UseTabs (tty) = 0;
3320 terminal->scroll_region_ok = 0;
3322 /* Seems to insert lines when it's not supposed to, messing up the
3323 display. In doing a trace, it didn't seem to be called much, so I
3324 don't think we're losing anything by turning it off. */
3325 terminal->line_ins_del_ok = 0;
3327 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
3328 #endif /* DOS_NT */
3330 #ifdef HAVE_GPM
3331 terminal->mouse_position_hook = term_mouse_position;
3332 tty->mouse_highlight.mouse_face_window = Qnil;
3333 #endif
3335 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3336 init_kboard (terminal->kboard);
3337 KVAR (terminal->kboard, Vwindow_system) = Qnil;
3338 terminal->kboard->next_kboard = all_kboards;
3339 all_kboards = terminal->kboard;
3340 terminal->kboard->reference_count++;
3341 /* Don't let the initial kboard remain current longer than necessary.
3342 That would cause problems if a file loaded on startup tries to
3343 prompt in the mini-buffer. */
3344 if (current_kboard == initial_kboard)
3345 current_kboard = terminal->kboard;
3346 #ifndef DOS_NT
3347 term_get_fkeys (address, terminal->kboard);
3349 /* Get frame size from system, or else from termcap. */
3351 int height, width;
3352 get_tty_size (fileno (tty->input), &width, &height);
3353 FrameCols (tty) = width;
3354 FrameRows (tty) = height;
3357 if (FrameCols (tty) <= 0)
3358 FrameCols (tty) = tgetnum ("co");
3359 if (FrameRows (tty) <= 0)
3360 FrameRows (tty) = tgetnum ("li");
3362 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
3363 maybe_fatal (must_succeed, terminal,
3364 "Screen size %dx%d is too small",
3365 "Screen size %dx%d is too small",
3366 FrameCols (tty), FrameRows (tty));
3368 TabWidth (tty) = tgetnum ("tw");
3370 if (!tty->TS_bell)
3371 tty->TS_bell = "\07";
3373 if (!tty->TS_fwd_scroll)
3374 tty->TS_fwd_scroll = Down (tty);
3376 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
3378 if (TabWidth (tty) < 0)
3379 TabWidth (tty) = 8;
3381 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3382 and newer termcap doc does not seem to say there is a default.
3383 if (!tty->Wcm->cm_tab)
3384 tty->Wcm->cm_tab = "\t";
3387 /* We don't support standout modes that use `magic cookies', so
3388 turn off any that do. */
3389 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
3391 tty->TS_standout_mode = 0;
3392 tty->TS_end_standout_mode = 0;
3394 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
3396 tty->TS_enter_underline_mode = 0;
3397 tty->TS_exit_underline_mode = 0;
3400 /* If there's no standout mode, try to use underlining instead. */
3401 if (tty->TS_standout_mode == 0)
3403 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3404 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
3407 /* If no `se' string, try using a `me' string instead.
3408 If that fails, we can't use standout mode at all. */
3409 if (tty->TS_end_standout_mode == 0)
3411 char *s = tgetstr ("me", address);
3412 if (s != 0)
3413 tty->TS_end_standout_mode = s;
3414 else
3415 tty->TS_standout_mode = 0;
3418 if (tty->TF_teleray)
3420 tty->Wcm->cm_tab = 0;
3421 /* We can't support standout mode, because it uses magic cookies. */
3422 tty->TS_standout_mode = 0;
3423 /* But that means we cannot rely on ^M to go to column zero! */
3424 CR (tty) = 0;
3425 /* LF can't be trusted either -- can alter hpos */
3426 /* if move at column 0 thru a line with TS_standout_mode */
3427 Down (tty) = 0;
3430 tty->specified_window = FrameRows (tty);
3432 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3434 maybe_fatal (must_succeed, terminal,
3435 "Terminal type \"%s\" is not powerful enough to run Emacs",
3436 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3437 It lacks the ability to position the cursor.\n\
3438 If that is not the actual type of terminal you have,\n\
3439 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3440 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3441 # ifdef TERMINFO
3442 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3443 # else /* TERMCAP */
3444 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3445 # endif /* TERMINFO */
3446 terminal_type);
3449 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
3450 maybe_fatal (must_succeed, terminal,
3451 "Could not determine the frame size",
3452 "Could not determine the frame size");
3454 tty->delete_in_insert_mode
3455 = tty->TS_delete_mode && tty->TS_insert_mode
3456 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
3458 tty->se_is_so = (tty->TS_standout_mode
3459 && tty->TS_end_standout_mode
3460 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
3462 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
3464 terminal->scroll_region_ok
3465 = (tty->Wcm->cm_abs
3466 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
3468 terminal->line_ins_del_ok
3469 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3470 && (tty->TS_del_line || tty->TS_del_multi_lines))
3471 || (terminal->scroll_region_ok
3472 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
3474 terminal->char_ins_del_ok
3475 = ((tty->TS_ins_char || tty->TS_insert_mode
3476 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3477 && (tty->TS_del_char || tty->TS_del_multi_chars));
3479 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
3481 init_baud_rate (fileno (tty->input));
3483 #endif /* not DOS_NT */
3485 /* Init system terminal modes (RAW or CBREAK, etc.). */
3486 init_sys_modes (tty);
3488 return terminal;
3492 static void
3493 vfatal (const char *str, va_list ap)
3495 fprintf (stderr, "emacs: ");
3496 vfprintf (stderr, str, ap);
3497 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
3498 fprintf (stderr, "\n");
3499 fflush (stderr);
3500 exit (1);
3504 /* Auxiliary error-handling function for init_tty.
3505 Delete TERMINAL, then call error or fatal with str1 or str2,
3506 respectively, according to whether MUST_SUCCEED is zero or not. */
3508 static void
3509 maybe_fatal (int must_succeed, struct terminal *terminal,
3510 const char *str1, const char *str2, ...)
3512 va_list ap;
3513 va_start (ap, str2);
3514 if (terminal)
3515 delete_tty (terminal);
3517 if (must_succeed)
3518 vfatal (str2, ap);
3519 else
3520 verror (str1, ap);
3522 va_end (ap);
3523 abort ();
3526 void
3527 fatal (const char *str, ...)
3529 va_list ap;
3530 va_start (ap, str);
3531 vfatal (str, ap);
3532 va_end (ap);
3537 /* Delete the given tty terminal, closing all frames on it. */
3539 static void
3540 delete_tty (struct terminal *terminal)
3542 struct tty_display_info *tty;
3544 /* Protect against recursive calls. delete_frame in
3545 delete_terminal calls us back when it deletes our last frame. */
3546 if (!terminal->name)
3547 return;
3549 if (terminal->type != output_termcap)
3550 abort ();
3552 tty = terminal->display_info.tty;
3554 if (tty == tty_list)
3555 tty_list = tty->next;
3556 else
3558 struct tty_display_info *p;
3559 for (p = tty_list; p && p->next != tty; p = p->next)
3562 if (! p)
3563 /* This should not happen. */
3564 abort ();
3566 p->next = tty->next;
3567 tty->next = 0;
3570 /* reset_sys_modes needs a valid device, so this call needs to be
3571 before delete_terminal. */
3572 reset_sys_modes (tty);
3574 delete_terminal (terminal);
3576 xfree (tty->name);
3577 xfree (tty->type);
3579 if (tty->input)
3581 delete_keyboard_wait_descriptor (fileno (tty->input));
3582 if (tty->input != stdin)
3583 fclose (tty->input);
3585 if (tty->output && tty->output != stdout && tty->output != tty->input)
3586 fclose (tty->output);
3587 if (tty->termscript)
3588 fclose (tty->termscript);
3590 xfree (tty->old_tty);
3591 xfree (tty->Wcm);
3592 xfree (tty->termcap_strings_buffer);
3593 xfree (tty->termcap_term_buffer);
3595 memset (tty, 0, sizeof (struct tty_display_info));
3596 xfree (tty);
3601 /* Mark the pointers in the tty_display_info objects.
3602 Called by the Fgarbage_collector. */
3604 void
3605 mark_ttys (void)
3607 struct tty_display_info *tty;
3609 for (tty = tty_list; tty; tty = tty->next)
3610 mark_object (tty->top_frame);
3615 void
3616 syms_of_term (void)
3618 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo,
3619 doc: /* Non-nil means the system uses terminfo rather than termcap.
3620 This variable can be used by terminal emulator packages. */);
3621 #ifdef TERMINFO
3622 system_uses_terminfo = 1;
3623 #else
3624 system_uses_terminfo = 0;
3625 #endif
3627 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions,
3628 doc: /* Functions to be run after suspending a tty.
3629 The functions are run with one argument, the terminal object to be suspended.
3630 See `suspend-tty'. */);
3631 Vsuspend_tty_functions = Qnil;
3634 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions,
3635 doc: /* Functions to be run after resuming a tty.
3636 The functions are run with one argument, the terminal object that was revived.
3637 See `resume-tty'. */);
3638 Vresume_tty_functions = Qnil;
3640 DEFVAR_BOOL ("visible-cursor", visible_cursor,
3641 doc: /* Non-nil means to make the cursor very visible.
3642 This only has an effect when running in a text terminal.
3643 What means \"very visible\" is up to your terminal. It may make the cursor
3644 bigger, or it may make it blink, or it may do nothing at all. */);
3645 visible_cursor = 1;
3647 defsubr (&Stty_display_color_p);
3648 defsubr (&Stty_display_color_cells);
3649 defsubr (&Stty_no_underline);
3650 defsubr (&Stty_type);
3651 defsubr (&Scontrolling_tty_p);
3652 defsubr (&Ssuspend_tty);
3653 defsubr (&Sresume_tty);
3654 #ifdef HAVE_GPM
3655 defsubr (&Sgpm_mouse_start);
3656 defsubr (&Sgpm_mouse_stop);
3657 #endif /* HAVE_GPM */
3659 #ifndef DOS_NT
3660 default_orig_pair = NULL;
3661 default_set_foreground = NULL;
3662 default_set_background = NULL;
3663 #endif /* !DOS_NT */
3665 encode_terminal_src = NULL;
3666 encode_terminal_dst = NULL;