Update and split ChangeLogs.
[emacs.git] / src / term.c
blob39c9592e28f0a0ae42639a6e946207c0802f5da0
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 <stdarg.h>
30 #include <setjmp.h>
32 #include "lisp.h"
33 #include "termchar.h"
34 #include "termopts.h"
35 #include "tparam.h"
36 #include "buffer.h"
37 #include "character.h"
38 #include "charset.h"
39 #include "coding.h"
40 #include "composite.h"
41 #include "keyboard.h"
42 #include "frame.h"
43 #include "disptab.h"
44 #include "termhooks.h"
45 #include "dispextern.h"
46 #include "window.h"
47 #include "keymap.h"
48 #include "blockinput.h"
49 #include "syssignal.h"
50 #include "systty.h"
51 #include "intervals.h"
52 #ifdef MSDOS
53 #include "msdos.h"
54 static int been_here = -1;
55 #endif
57 #include "cm.h"
58 #ifdef HAVE_X_WINDOWS
59 #include "xterm.h"
60 #endif
62 #ifndef O_RDWR
63 #define O_RDWR 2
64 #endif
66 #ifndef O_NOCTTY
67 #define O_NOCTTY 0
68 #endif
70 /* The name of the default console device. */
71 #ifdef WINDOWSNT
72 #define DEV_TTY "CONOUT$"
73 #else
74 #define DEV_TTY "/dev/tty"
75 #endif
77 static void tty_set_scroll_region (struct frame *f, int start, int stop);
78 static void turn_on_face (struct frame *, int face_id);
79 static void turn_off_face (struct frame *, int face_id);
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 void clear_tty_hooks (struct terminal *terminal);
84 static void set_tty_hooks (struct terminal *terminal);
85 static void dissociate_if_controlling_tty (int fd);
86 static void delete_tty (struct terminal *);
87 static void maybe_fatal (int must_succeed, struct terminal *terminal,
88 const char *str1, const char *str2, ...)
89 NO_RETURN ATTRIBUTE_FORMAT_PRINTF (4, 5);
90 static void vfatal (const char *str, va_list ap)
91 NO_RETURN ATTRIBUTE_FORMAT_PRINTF (1, 0);
94 #define OUTPUT(tty, a) \
95 emacs_tputs ((tty), a, \
96 (int) (FRAME_LINES (XFRAME (selected_frame)) \
97 - curY (tty)), \
98 cmputc)
100 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
101 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
103 #define OUTPUT_IF(tty, a) \
104 do { \
105 if (a) \
106 OUTPUT (tty, a); \
107 } while (0)
109 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
111 /* Display space properties */
113 /* Chain of all tty device parameters. */
114 struct tty_display_info *tty_list;
116 /* Meaning of bits in no_color_video. Each bit set means that the
117 corresponding attribute cannot be combined with colors. */
119 enum no_color_bit
121 NC_STANDOUT = 1 << 0,
122 NC_UNDERLINE = 1 << 1,
123 NC_REVERSE = 1 << 2,
124 NC_BLINK = 1 << 3,
125 NC_DIM = 1 << 4,
126 NC_BOLD = 1 << 5,
127 NC_INVIS = 1 << 6,
128 NC_PROTECT = 1 << 7,
129 NC_ALT_CHARSET = 1 << 8
132 /* internal state */
134 /* The largest frame width in any call to calculate_costs. */
136 int max_frame_cols;
138 /* The largest frame height in any call to calculate_costs. */
140 int max_frame_lines;
142 /* Non-zero if we have dropped our controlling tty and therefore
143 should not open a frame on stdout. */
144 static int no_controlling_tty;
148 #ifdef HAVE_GPM
149 #include <sys/fcntl.h>
151 /* The device for which we have enabled gpm support (or NULL). */
152 struct tty_display_info *gpm_tty = NULL;
154 /* Last recorded mouse coordinates. */
155 static int last_mouse_x, last_mouse_y;
156 #endif /* HAVE_GPM */
158 /* Ring the bell on a tty. */
160 static void
161 tty_ring_bell (struct frame *f)
163 struct tty_display_info *tty = FRAME_TTY (f);
165 if (tty->output)
167 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
168 ? tty->TS_visible_bell
169 : tty->TS_bell));
170 fflush (tty->output);
174 /* Set up termcap modes for Emacs. */
176 void
177 tty_set_terminal_modes (struct terminal *terminal)
179 struct tty_display_info *tty = terminal->display_info.tty;
181 if (tty->output)
183 if (tty->TS_termcap_modes)
184 OUTPUT (tty, tty->TS_termcap_modes);
185 else
187 /* Output enough newlines to scroll all the old screen contents
188 off the screen, so it won't be overwritten and lost. */
189 int i;
190 current_tty = tty;
191 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
192 cmputc ('\n');
195 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
196 OUTPUT_IF (tty, tty->TS_keypad_mode);
197 losecursor (tty);
198 fflush (tty->output);
202 /* Reset termcap modes before exiting Emacs. */
204 void
205 tty_reset_terminal_modes (struct terminal *terminal)
207 struct tty_display_info *tty = terminal->display_info.tty;
209 if (tty->output)
211 tty_turn_off_highlight (tty);
212 tty_turn_off_insert (tty);
213 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
214 OUTPUT_IF (tty, tty->TS_cursor_normal);
215 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
216 OUTPUT_IF (tty, tty->TS_orig_pair);
217 /* Output raw CR so kernel can track the cursor hpos. */
218 current_tty = tty;
219 cmputc ('\r');
220 fflush (tty->output);
224 /* Flag the end of a display update on a termcap terminal. */
226 static void
227 tty_update_end (struct frame *f)
229 struct tty_display_info *tty = FRAME_TTY (f);
231 if (!XWINDOW (selected_window)->cursor_off_p)
232 tty_show_cursor (tty);
233 tty_turn_off_insert (tty);
234 tty_background_highlight (tty);
237 /* The implementation of set_terminal_window for termcap frames. */
239 static void
240 tty_set_terminal_window (struct frame *f, int size)
242 struct tty_display_info *tty = FRAME_TTY (f);
244 tty->specified_window = size ? size : FRAME_LINES (f);
245 if (FRAME_SCROLL_REGION_OK (f))
246 tty_set_scroll_region (f, 0, tty->specified_window);
249 static void
250 tty_set_scroll_region (struct frame *f, int start, int stop)
252 char *buf;
253 struct tty_display_info *tty = FRAME_TTY (f);
255 if (tty->TS_set_scroll_region)
256 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1, 0, 0);
257 else if (tty->TS_set_scroll_region_1)
258 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
259 FRAME_LINES (f), start,
260 FRAME_LINES (f) - stop,
261 FRAME_LINES (f));
262 else
263 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
265 OUTPUT (tty, buf);
266 xfree (buf);
267 losecursor (tty);
271 static void
272 tty_turn_on_insert (struct tty_display_info *tty)
274 if (!tty->insert_mode)
275 OUTPUT (tty, tty->TS_insert_mode);
276 tty->insert_mode = 1;
279 void
280 tty_turn_off_insert (struct tty_display_info *tty)
282 if (tty->insert_mode)
283 OUTPUT (tty, tty->TS_end_insert_mode);
284 tty->insert_mode = 0;
287 /* Handle highlighting. */
289 void
290 tty_turn_off_highlight (struct tty_display_info *tty)
292 if (tty->standout_mode)
293 OUTPUT_IF (tty, tty->TS_end_standout_mode);
294 tty->standout_mode = 0;
297 static void
298 tty_turn_on_highlight (struct tty_display_info *tty)
300 if (!tty->standout_mode)
301 OUTPUT_IF (tty, tty->TS_standout_mode);
302 tty->standout_mode = 1;
305 static void
306 tty_toggle_highlight (struct tty_display_info *tty)
308 if (tty->standout_mode)
309 tty_turn_off_highlight (tty);
310 else
311 tty_turn_on_highlight (tty);
315 /* Make cursor invisible. */
317 static void
318 tty_hide_cursor (struct tty_display_info *tty)
320 if (tty->cursor_hidden == 0)
322 tty->cursor_hidden = 1;
323 OUTPUT_IF (tty, tty->TS_cursor_invisible);
328 /* Ensure that cursor is visible. */
330 static void
331 tty_show_cursor (struct tty_display_info *tty)
333 if (tty->cursor_hidden)
335 tty->cursor_hidden = 0;
336 OUTPUT_IF (tty, tty->TS_cursor_normal);
337 if (visible_cursor)
338 OUTPUT_IF (tty, tty->TS_cursor_visible);
343 /* Set standout mode to the state it should be in for
344 empty space inside windows. What this is,
345 depends on the user option inverse-video. */
347 static void
348 tty_background_highlight (struct tty_display_info *tty)
350 if (inverse_video)
351 tty_turn_on_highlight (tty);
352 else
353 tty_turn_off_highlight (tty);
356 /* Set standout mode to the mode specified for the text to be output. */
358 static void
359 tty_highlight_if_desired (struct tty_display_info *tty)
361 if (inverse_video)
362 tty_turn_on_highlight (tty);
363 else
364 tty_turn_off_highlight (tty);
368 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
369 frame-relative coordinates. */
371 static void
372 tty_cursor_to (struct frame *f, int vpos, int hpos)
374 struct tty_display_info *tty = FRAME_TTY (f);
376 /* Detect the case where we are called from reset_sys_modes
377 and the costs have never been calculated. Do nothing. */
378 if (! tty->costs_set)
379 return;
381 if (curY (tty) == vpos
382 && curX (tty) == hpos)
383 return;
384 if (!tty->TF_standout_motion)
385 tty_background_highlight (tty);
386 if (!tty->TF_insmode_motion)
387 tty_turn_off_insert (tty);
388 cmgoto (tty, vpos, hpos);
391 /* Similar but don't take any account of the wasted characters. */
393 static void
394 tty_raw_cursor_to (struct frame *f, int row, int col)
396 struct tty_display_info *tty = FRAME_TTY (f);
398 if (curY (tty) == row
399 && curX (tty) == col)
400 return;
401 if (!tty->TF_standout_motion)
402 tty_background_highlight (tty);
403 if (!tty->TF_insmode_motion)
404 tty_turn_off_insert (tty);
405 cmgoto (tty, row, col);
408 /* Erase operations */
410 /* Clear from cursor to end of frame on a termcap device. */
412 static void
413 tty_clear_to_end (struct frame *f)
415 register int i;
416 struct tty_display_info *tty = FRAME_TTY (f);
418 if (tty->TS_clr_to_bottom)
420 tty_background_highlight (tty);
421 OUTPUT (tty, tty->TS_clr_to_bottom);
423 else
425 for (i = curY (tty); i < FRAME_LINES (f); i++)
427 cursor_to (f, i, 0);
428 clear_end_of_line (f, FRAME_COLS (f));
433 /* Clear an entire termcap frame. */
435 static void
436 tty_clear_frame (struct frame *f)
438 struct tty_display_info *tty = FRAME_TTY (f);
440 if (tty->TS_clr_frame)
442 tty_background_highlight (tty);
443 OUTPUT (tty, tty->TS_clr_frame);
444 cmat (tty, 0, 0);
446 else
448 cursor_to (f, 0, 0);
449 clear_to_end (f);
453 /* An implementation of clear_end_of_line for termcap frames.
455 Note that the cursor may be moved, on terminals lacking a `ce' string. */
457 static void
458 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
460 register int i;
461 struct tty_display_info *tty = FRAME_TTY (f);
463 /* Detect the case where we are called from reset_sys_modes
464 and the costs have never been calculated. Do nothing. */
465 if (! tty->costs_set)
466 return;
468 if (curX (tty) >= first_unused_hpos)
469 return;
470 tty_background_highlight (tty);
471 if (tty->TS_clr_line)
473 OUTPUT1 (tty, tty->TS_clr_line);
475 else
476 { /* have to do it the hard way */
477 tty_turn_off_insert (tty);
479 /* Do not write in last row last col with Auto-wrap on. */
480 if (AutoWrap (tty)
481 && curY (tty) == FrameRows (tty) - 1
482 && first_unused_hpos == FrameCols (tty))
483 first_unused_hpos--;
485 for (i = curX (tty); i < first_unused_hpos; i++)
487 if (tty->termscript)
488 fputc (' ', tty->termscript);
489 fputc (' ', tty->output);
491 cmplus (tty, first_unused_hpos - curX (tty));
495 /* Buffers to store the source and result of code conversion for terminal. */
496 static unsigned char *encode_terminal_src;
497 static unsigned char *encode_terminal_dst;
498 /* Allocated sizes of the above buffers. */
499 static int encode_terminal_src_size;
500 static int encode_terminal_dst_size;
502 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
503 Set CODING->produced to the byte-length of the resulting byte
504 sequence, and return a pointer to that byte sequence. */
506 unsigned char *
507 encode_terminal_code (struct glyph *src, int src_len, struct coding_system *coding)
509 struct glyph *src_end = src + src_len;
510 unsigned char *buf;
511 int nchars, nbytes, required;
512 register int tlen = GLYPH_TABLE_LENGTH;
513 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
514 Lisp_Object charset_list;
516 /* Allocate sufficient size of buffer to store all characters in
517 multibyte-form. But, it may be enlarged on demand if
518 Vglyph_table contains a string or a composite glyph is
519 encountered. */
520 required = MAX_MULTIBYTE_LENGTH * src_len;
521 if (encode_terminal_src_size < required)
523 if (encode_terminal_src)
524 encode_terminal_src = xrealloc (encode_terminal_src, required);
525 else
526 encode_terminal_src = xmalloc (required);
527 encode_terminal_src_size = required;
530 charset_list = coding_charset_list (coding);
532 buf = encode_terminal_src;
533 nchars = 0;
534 while (src < src_end)
536 if (src->type == COMPOSITE_GLYPH)
538 struct composition *cmp IF_LINT (= NULL);
539 Lisp_Object gstring IF_LINT (= Qnil);
540 int i;
542 nbytes = buf - encode_terminal_src;
543 if (src->u.cmp.automatic)
545 gstring = composition_gstring_from_id (src->u.cmp.id);
546 required = src->slice.cmp.to + 1 - src->slice.cmp.from;
548 else
550 cmp = composition_table[src->u.cmp.id];
551 required = MAX_MULTIBYTE_LENGTH * cmp->glyph_len;
554 if (encode_terminal_src_size < nbytes + required)
556 encode_terminal_src_size = nbytes + required;
557 encode_terminal_src = xrealloc (encode_terminal_src,
558 encode_terminal_src_size);
559 buf = encode_terminal_src + nbytes;
562 if (src->u.cmp.automatic)
563 for (i = src->slice.cmp.from; i <= src->slice.cmp.to; i++)
565 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
566 int c = LGLYPH_CHAR (g);
568 if (! char_charset (c, charset_list, NULL))
569 c = '?';
570 buf += CHAR_STRING (c, buf);
571 nchars++;
573 else
574 for (i = 0; i < cmp->glyph_len; i++)
576 int c = COMPOSITION_GLYPH (cmp, i);
578 if (c == '\t')
579 continue;
580 if (char_charset (c, charset_list, NULL))
582 if (CHAR_WIDTH (c) == 0
583 && i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
584 /* Should be left-padded */
586 buf += CHAR_STRING (' ', buf);
587 nchars++;
590 else
591 c = '?';
592 buf += CHAR_STRING (c, buf);
593 nchars++;
596 /* We must skip glyphs to be padded for a wide character. */
597 else if (! CHAR_GLYPH_PADDING_P (*src))
599 GLYPH g;
600 int c IF_LINT (= 0);
601 Lisp_Object string;
603 string = Qnil;
604 SET_GLYPH_FROM_CHAR_GLYPH (g, src[0]);
606 if (GLYPH_INVALID_P (g) || GLYPH_SIMPLE_P (tbase, tlen, g))
608 /* This glyph doesn't have an entry in Vglyph_table. */
609 c = src->u.ch;
611 else
613 /* This glyph has an entry in Vglyph_table,
614 so process any alias before testing for simpleness. */
615 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
617 if (GLYPH_SIMPLE_P (tbase, tlen, g))
618 /* We set the multi-byte form of a character in G
619 (that should be an ASCII character) at WORKBUF. */
620 c = GLYPH_CHAR (g);
621 else
622 /* We have a string in Vglyph_table. */
623 string = tbase[GLYPH_CHAR (g)];
626 if (NILP (string))
628 nbytes = buf - encode_terminal_src;
629 if (encode_terminal_src_size < nbytes + MAX_MULTIBYTE_LENGTH)
631 encode_terminal_src_size = nbytes + MAX_MULTIBYTE_LENGTH;
632 encode_terminal_src = xrealloc (encode_terminal_src,
633 encode_terminal_src_size);
634 buf = encode_terminal_src + nbytes;
636 if (CHAR_BYTE8_P (c)
637 || char_charset (c, charset_list, NULL))
639 /* Store the multibyte form of C at BUF. */
640 buf += CHAR_STRING (c, buf);
641 nchars++;
643 else
645 /* C is not encodable. */
646 *buf++ = '?';
647 nchars++;
648 while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
650 *buf++ = '?';
651 nchars++;
652 src++;
656 else
658 if (! STRING_MULTIBYTE (string))
659 string = string_to_multibyte (string);
660 nbytes = buf - encode_terminal_src;
661 if (encode_terminal_src_size < nbytes + SBYTES (string))
663 encode_terminal_src_size = nbytes + SBYTES (string);
664 encode_terminal_src = xrealloc (encode_terminal_src,
665 encode_terminal_src_size);
666 buf = encode_terminal_src + nbytes;
668 memcpy (buf, SDATA (string), SBYTES (string));
669 buf += SBYTES (string);
670 nchars += SCHARS (string);
673 src++;
676 if (nchars == 0)
678 coding->produced = 0;
679 return NULL;
682 nbytes = buf - encode_terminal_src;
683 coding->source = encode_terminal_src;
684 if (encode_terminal_dst_size == 0)
686 encode_terminal_dst_size = encode_terminal_src_size;
687 if (encode_terminal_dst)
688 encode_terminal_dst = xrealloc (encode_terminal_dst,
689 encode_terminal_dst_size);
690 else
691 encode_terminal_dst = xmalloc (encode_terminal_dst_size);
693 coding->destination = encode_terminal_dst;
694 coding->dst_bytes = encode_terminal_dst_size;
695 encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
696 /* coding->destination may have been reallocated. */
697 encode_terminal_dst = coding->destination;
698 encode_terminal_dst_size = coding->dst_bytes;
700 return (encode_terminal_dst);
705 /* An implementation of write_glyphs for termcap frames. */
707 static void
708 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
710 unsigned char *conversion_buffer;
711 struct coding_system *coding;
712 size_t n, stringlen;
714 struct tty_display_info *tty = FRAME_TTY (f);
716 tty_turn_off_insert (tty);
717 tty_hide_cursor (tty);
719 /* Don't dare write in last column of bottom line, if Auto-Wrap,
720 since that would scroll the whole frame on some terminals. */
722 if (AutoWrap (tty)
723 && curY (tty) + 1 == FRAME_LINES (f)
724 && (curX (tty) + len) == FRAME_COLS (f))
725 len --;
726 if (len <= 0)
727 return;
729 cmplus (tty, len);
731 /* If terminal_coding does any conversion, use it, otherwise use
732 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
733 because it always return 1 if the member src_multibyte is 1. */
734 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
735 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
736 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
737 the tail. */
738 coding->mode &= ~CODING_MODE_LAST_BLOCK;
740 for (stringlen = len; stringlen != 0; stringlen -= n)
742 /* Identify a run of glyphs with the same face. */
743 int face_id = string->face_id;
745 for (n = 1; n < stringlen; ++n)
746 if (string[n].face_id != face_id)
747 break;
749 /* Turn appearance modes of the face of the run on. */
750 tty_highlight_if_desired (tty);
751 turn_on_face (f, face_id);
753 if (n == stringlen)
754 /* This is the last run. */
755 coding->mode |= CODING_MODE_LAST_BLOCK;
756 conversion_buffer = encode_terminal_code (string, n, coding);
757 if (coding->produced > 0)
759 BLOCK_INPUT;
760 fwrite (conversion_buffer, 1, coding->produced, tty->output);
761 if (ferror (tty->output))
762 clearerr (tty->output);
763 if (tty->termscript)
764 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
765 UNBLOCK_INPUT;
767 string += n;
769 /* Turn appearance modes off. */
770 turn_off_face (f, face_id);
771 tty_turn_off_highlight (tty);
774 cmcheckmagic (tty);
777 #ifdef HAVE_GPM /* Only used by GPM code. */
779 static void
780 tty_write_glyphs_with_face (register struct frame *f, register struct glyph *string,
781 register int len, register int face_id)
783 unsigned char *conversion_buffer;
784 struct coding_system *coding;
786 struct tty_display_info *tty = FRAME_TTY (f);
788 tty_turn_off_insert (tty);
789 tty_hide_cursor (tty);
791 /* Don't dare write in last column of bottom line, if Auto-Wrap,
792 since that would scroll the whole frame on some terminals. */
794 if (AutoWrap (tty)
795 && curY (tty) + 1 == FRAME_LINES (f)
796 && (curX (tty) + len) == FRAME_COLS (f))
797 len --;
798 if (len <= 0)
799 return;
801 cmplus (tty, len);
803 /* If terminal_coding does any conversion, use it, otherwise use
804 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
805 because it always return 1 if the member src_multibyte is 1. */
806 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
807 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
808 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
809 the tail. */
810 coding->mode &= ~CODING_MODE_LAST_BLOCK;
812 /* Turn appearance modes of the face. */
813 tty_highlight_if_desired (tty);
814 turn_on_face (f, face_id);
816 coding->mode |= CODING_MODE_LAST_BLOCK;
817 conversion_buffer = encode_terminal_code (string, len, coding);
818 if (coding->produced > 0)
820 BLOCK_INPUT;
821 fwrite (conversion_buffer, 1, coding->produced, tty->output);
822 if (ferror (tty->output))
823 clearerr (tty->output);
824 if (tty->termscript)
825 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
826 UNBLOCK_INPUT;
829 /* Turn appearance modes off. */
830 turn_off_face (f, face_id);
831 tty_turn_off_highlight (tty);
833 cmcheckmagic (tty);
835 #endif
837 /* An implementation of insert_glyphs for termcap frames. */
839 static void
840 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
842 char *buf;
843 struct glyph *glyph = NULL;
844 unsigned char *conversion_buffer;
845 unsigned char space[1];
846 struct coding_system *coding;
848 struct tty_display_info *tty = FRAME_TTY (f);
850 if (tty->TS_ins_multi_chars)
852 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len, 0, 0, 0);
853 OUTPUT1 (tty, buf);
854 xfree (buf);
855 if (start)
856 write_glyphs (f, start, len);
857 return;
860 tty_turn_on_insert (tty);
861 cmplus (tty, len);
863 if (! start)
864 space[0] = SPACEGLYPH;
866 /* If terminal_coding does any conversion, use it, otherwise use
867 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
868 because it always return 1 if the member src_multibyte is 1. */
869 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
870 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
871 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
872 the tail. */
873 coding->mode &= ~CODING_MODE_LAST_BLOCK;
875 while (len-- > 0)
877 OUTPUT1_IF (tty, tty->TS_ins_char);
878 if (!start)
880 conversion_buffer = space;
881 coding->produced = 1;
883 else
885 tty_highlight_if_desired (tty);
886 turn_on_face (f, start->face_id);
887 glyph = start;
888 ++start;
889 /* We must open sufficient space for a character which
890 occupies more than one column. */
891 while (len && CHAR_GLYPH_PADDING_P (*start))
893 OUTPUT1_IF (tty, tty->TS_ins_char);
894 start++, len--;
897 if (len <= 0)
898 /* This is the last glyph. */
899 coding->mode |= CODING_MODE_LAST_BLOCK;
901 conversion_buffer = encode_terminal_code (glyph, 1, coding);
904 if (coding->produced > 0)
906 BLOCK_INPUT;
907 fwrite (conversion_buffer, 1, coding->produced, tty->output);
908 if (ferror (tty->output))
909 clearerr (tty->output);
910 if (tty->termscript)
911 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
912 UNBLOCK_INPUT;
915 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
916 if (start)
918 turn_off_face (f, glyph->face_id);
919 tty_turn_off_highlight (tty);
923 cmcheckmagic (tty);
926 /* An implementation of delete_glyphs for termcap frames. */
928 static void
929 tty_delete_glyphs (struct frame *f, int n)
931 char *buf;
932 register int i;
934 struct tty_display_info *tty = FRAME_TTY (f);
936 if (tty->delete_in_insert_mode)
938 tty_turn_on_insert (tty);
940 else
942 tty_turn_off_insert (tty);
943 OUTPUT_IF (tty, tty->TS_delete_mode);
946 if (tty->TS_del_multi_chars)
948 buf = tparam (tty->TS_del_multi_chars, 0, 0, n, 0, 0, 0);
949 OUTPUT1 (tty, buf);
950 xfree (buf);
952 else
953 for (i = 0; i < n; i++)
954 OUTPUT1 (tty, tty->TS_del_char);
955 if (!tty->delete_in_insert_mode)
956 OUTPUT_IF (tty, tty->TS_end_delete_mode);
959 /* An implementation of ins_del_lines for termcap frames. */
961 static void
962 tty_ins_del_lines (struct frame *f, int vpos, int n)
964 struct tty_display_info *tty = FRAME_TTY (f);
965 const char *multi =
966 n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
967 const char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
968 const char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
970 register int i = n > 0 ? n : -n;
971 register char *buf;
973 /* If the lines below the insertion are being pushed
974 into the end of the window, this is the same as clearing;
975 and we know the lines are already clear, since the matching
976 deletion has already been done. So can ignore this. */
977 /* If the lines below the deletion are blank lines coming
978 out of the end of the window, don't bother,
979 as there will be a matching inslines later that will flush them. */
980 if (FRAME_SCROLL_REGION_OK (f)
981 && vpos + i >= tty->specified_window)
982 return;
983 if (!FRAME_MEMORY_BELOW_FRAME (f)
984 && vpos + i >= FRAME_LINES (f))
985 return;
987 if (multi)
989 raw_cursor_to (f, vpos, 0);
990 tty_background_highlight (tty);
991 buf = tparam (multi, 0, 0, i, 0, 0, 0);
992 OUTPUT (tty, buf);
993 xfree (buf);
995 else if (single)
997 raw_cursor_to (f, vpos, 0);
998 tty_background_highlight (tty);
999 while (--i >= 0)
1000 OUTPUT (tty, single);
1001 if (tty->TF_teleray)
1002 curX (tty) = 0;
1004 else
1006 tty_set_scroll_region (f, vpos, tty->specified_window);
1007 if (n < 0)
1008 raw_cursor_to (f, tty->specified_window - 1, 0);
1009 else
1010 raw_cursor_to (f, vpos, 0);
1011 tty_background_highlight (tty);
1012 while (--i >= 0)
1013 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1014 tty_set_scroll_region (f, 0, tty->specified_window);
1017 if (!FRAME_SCROLL_REGION_OK (f)
1018 && FRAME_MEMORY_BELOW_FRAME (f)
1019 && n < 0)
1021 cursor_to (f, FRAME_LINES (f) + n, 0);
1022 clear_to_end (f);
1026 /* Compute cost of sending "str", in characters,
1027 not counting any line-dependent padding. */
1030 string_cost (const char *str)
1032 cost = 0;
1033 if (str)
1034 tputs (str, 0, evalcost);
1035 return cost;
1038 /* Compute cost of sending "str", in characters,
1039 counting any line-dependent padding at one line. */
1041 static int
1042 string_cost_one_line (const char *str)
1044 cost = 0;
1045 if (str)
1046 tputs (str, 1, evalcost);
1047 return cost;
1050 /* Compute per line amount of line-dependent padding,
1051 in tenths of characters. */
1054 per_line_cost (const char *str)
1056 cost = 0;
1057 if (str)
1058 tputs (str, 0, evalcost);
1059 cost = - cost;
1060 if (str)
1061 tputs (str, 10, evalcost);
1062 return cost;
1065 /* char_ins_del_cost[n] is cost of inserting N characters.
1066 char_ins_del_cost[-n] is cost of deleting N characters.
1067 The length of this vector is based on max_frame_cols. */
1069 int *char_ins_del_vector;
1071 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1073 /* ARGSUSED */
1074 static void
1075 calculate_ins_del_char_costs (struct frame *f)
1077 struct tty_display_info *tty = FRAME_TTY (f);
1078 int ins_startup_cost, del_startup_cost;
1079 int ins_cost_per_char, del_cost_per_char;
1080 register int i;
1081 register int *p;
1083 if (tty->TS_ins_multi_chars)
1085 ins_cost_per_char = 0;
1086 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1088 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1089 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1091 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1092 + string_cost (tty->TS_end_insert_mode))) / 100;
1093 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1094 + string_cost_one_line (tty->TS_pad_inserted_char));
1096 else
1098 ins_startup_cost = 9999;
1099 ins_cost_per_char = 0;
1102 if (tty->TS_del_multi_chars)
1104 del_cost_per_char = 0;
1105 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1107 else if (tty->TS_del_char)
1109 del_startup_cost = (string_cost (tty->TS_delete_mode)
1110 + string_cost (tty->TS_end_delete_mode));
1111 if (tty->delete_in_insert_mode)
1112 del_startup_cost /= 2;
1113 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1115 else
1117 del_startup_cost = 9999;
1118 del_cost_per_char = 0;
1121 /* Delete costs are at negative offsets */
1122 p = &char_ins_del_cost (f)[0];
1123 for (i = FRAME_COLS (f); --i >= 0;)
1124 *--p = (del_startup_cost += del_cost_per_char);
1126 /* Doing nothing is free */
1127 p = &char_ins_del_cost (f)[0];
1128 *p++ = 0;
1130 /* Insert costs are at positive offsets */
1131 for (i = FRAME_COLS (f); --i >= 0;)
1132 *p++ = (ins_startup_cost += ins_cost_per_char);
1135 void
1136 calculate_costs (struct frame *frame)
1138 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1140 if (FRAME_TERMCAP_P (frame))
1142 struct tty_display_info *tty = FRAME_TTY (frame);
1143 register const char *f = (tty->TS_set_scroll_region
1144 ? tty->TS_set_scroll_region
1145 : tty->TS_set_scroll_region_1);
1147 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1149 tty->costs_set = 1;
1151 /* These variables are only used for terminal stuff. They are
1152 allocated once for the terminal frame of X-windows emacs, but not
1153 used afterwards.
1155 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1156 X turns off char_ins_del_ok. */
1158 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1159 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1161 if (char_ins_del_vector != 0)
1162 char_ins_del_vector
1163 = (int *) xrealloc (char_ins_del_vector,
1164 (sizeof (int)
1165 + 2 * max_frame_cols * sizeof (int)));
1166 else
1167 char_ins_del_vector
1168 = (int *) xmalloc (sizeof (int)
1169 + 2 * max_frame_cols * sizeof (int));
1171 memset (char_ins_del_vector, 0,
1172 (sizeof (int) + 2 * max_frame_cols * sizeof (int)));
1175 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1176 do_line_insertion_deletion_costs (frame,
1177 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1178 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1179 f, f, 1);
1180 else
1181 do_line_insertion_deletion_costs (frame,
1182 tty->TS_ins_line, tty->TS_ins_multi_lines,
1183 tty->TS_del_line, tty->TS_del_multi_lines,
1184 0, 0, 1);
1186 calculate_ins_del_char_costs (frame);
1188 /* Don't use TS_repeat if its padding is worse than sending the chars */
1189 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1190 tty->RPov = string_cost (tty->TS_repeat);
1191 else
1192 tty->RPov = FRAME_COLS (frame) * 2;
1194 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1198 struct fkey_table {
1199 const char *cap, *name;
1202 /* Termcap capability names that correspond directly to X keysyms.
1203 Some of these (marked "terminfo") aren't supplied by old-style
1204 (Berkeley) termcap entries. They're listed in X keysym order;
1205 except we put the keypad keys first, so that if they clash with
1206 other keys (as on the IBM PC keyboard) they get overridden.
1209 static const struct fkey_table keys[] =
1211 {"kh", "home"}, /* termcap */
1212 {"kl", "left"}, /* termcap */
1213 {"ku", "up"}, /* termcap */
1214 {"kr", "right"}, /* termcap */
1215 {"kd", "down"}, /* termcap */
1216 {"%8", "prior"}, /* terminfo */
1217 {"%5", "next"}, /* terminfo */
1218 {"@7", "end"}, /* terminfo */
1219 {"@1", "begin"}, /* terminfo */
1220 {"*6", "select"}, /* terminfo */
1221 {"%9", "print"}, /* terminfo */
1222 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1224 * "insert" --- see below
1226 {"&8", "undo"}, /* terminfo */
1227 {"%0", "redo"}, /* terminfo */
1228 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1229 {"@0", "find"}, /* terminfo */
1230 {"@2", "cancel"}, /* terminfo */
1231 {"%1", "help"}, /* terminfo */
1233 * "break" goes here, but can't be reliably intercepted with termcap
1235 {"&4", "reset"}, /* terminfo --- actually `restart' */
1237 * "system" and "user" --- no termcaps
1239 {"kE", "clearline"}, /* terminfo */
1240 {"kA", "insertline"}, /* terminfo */
1241 {"kL", "deleteline"}, /* terminfo */
1242 {"kI", "insertchar"}, /* terminfo */
1243 {"kD", "deletechar"}, /* terminfo */
1244 {"kB", "backtab"}, /* terminfo */
1246 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1248 {"@8", "kp-enter"}, /* terminfo */
1250 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1251 * "kp-multiply", "kp-add", "kp-separator",
1252 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1253 * --- no termcaps for any of these.
1255 {"K4", "kp-1"}, /* terminfo */
1257 * "kp-2" --- no termcap
1259 {"K5", "kp-3"}, /* terminfo */
1261 * "kp-4" --- no termcap
1263 {"K2", "kp-5"}, /* terminfo */
1265 * "kp-6" --- no termcap
1267 {"K1", "kp-7"}, /* terminfo */
1269 * "kp-8" --- no termcap
1271 {"K3", "kp-9"}, /* terminfo */
1273 * "kp-equal" --- no termcap
1275 {"k1", "f1"},
1276 {"k2", "f2"},
1277 {"k3", "f3"},
1278 {"k4", "f4"},
1279 {"k5", "f5"},
1280 {"k6", "f6"},
1281 {"k7", "f7"},
1282 {"k8", "f8"},
1283 {"k9", "f9"},
1285 {"&0", "S-cancel"}, /*shifted cancel key*/
1286 {"&9", "S-begin"}, /*shifted begin key*/
1287 {"*0", "S-find"}, /*shifted find key*/
1288 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1289 {"*4", "S-delete"}, /*shifted delete-character key*/
1290 {"*7", "S-end"}, /*shifted end key*/
1291 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1292 {"#1", "S-help"}, /*shifted help key*/
1293 {"#2", "S-home"}, /*shifted home key*/
1294 {"#3", "S-insert"}, /*shifted insert-character key*/
1295 {"#4", "S-left"}, /*shifted left-arrow key*/
1296 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1297 {"%c", "S-next"}, /*shifted next key*/
1298 {"%e", "S-prior"}, /*shifted previous key*/
1299 {"%f", "S-print"}, /*shifted print key*/
1300 {"%g", "S-redo"}, /*shifted redo key*/
1301 {"%i", "S-right"}, /*shifted right-arrow key*/
1302 {"!3", "S-undo"} /*shifted undo key*/
1305 #ifndef DOS_NT
1306 static char **term_get_fkeys_address;
1307 static KBOARD *term_get_fkeys_kboard;
1308 static Lisp_Object term_get_fkeys_1 (void);
1310 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1311 This function scans the termcap function key sequence entries, and
1312 adds entries to Vinput_decode_map for each function key it finds. */
1314 static void
1315 term_get_fkeys (char **address, KBOARD *kboard)
1317 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1318 errors during the call. The only errors should be from Fdefine_key
1319 when given a key sequence containing an invalid prefix key. If the
1320 termcap defines function keys which use a prefix that is already bound
1321 to a command by the default bindings, we should silently ignore that
1322 function key specification, rather than giving the user an error and
1323 refusing to run at all on such a terminal. */
1325 term_get_fkeys_address = address;
1326 term_get_fkeys_kboard = kboard;
1327 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1330 static Lisp_Object
1331 term_get_fkeys_1 (void)
1333 int i;
1335 char **address = term_get_fkeys_address;
1336 KBOARD *kboard = term_get_fkeys_kboard;
1338 /* This can happen if CANNOT_DUMP or with strange options. */
1339 if (!KEYMAPP (KVAR (kboard, Vinput_decode_map)))
1340 KVAR (kboard, Vinput_decode_map) = Fmake_sparse_keymap (Qnil);
1342 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1344 char *sequence = tgetstr (keys[i].cap, address);
1345 if (sequence)
1346 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence),
1347 Fmake_vector (make_number (1),
1348 intern (keys[i].name)));
1351 /* The uses of the "k0" capability are inconsistent; sometimes it
1352 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1353 We will attempt to politely accommodate both systems by testing for
1354 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1357 const char *k_semi = tgetstr ("k;", address);
1358 const char *k0 = tgetstr ("k0", address);
1359 const char *k0_name = "f10";
1361 if (k_semi)
1363 if (k0)
1364 /* Define f0 first, so that f10 takes precedence in case the
1365 key sequences happens to be the same. */
1366 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k0),
1367 Fmake_vector (make_number (1), intern ("f0")));
1368 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k_semi),
1369 Fmake_vector (make_number (1), intern ("f10")));
1371 else if (k0)
1372 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k0),
1373 Fmake_vector (make_number (1), intern (k0_name)));
1376 /* Set up cookies for numbered function keys above f10. */
1378 char fcap[3], fkey[4];
1380 fcap[0] = 'F'; fcap[2] = '\0';
1381 for (i = 11; i < 64; i++)
1383 if (i <= 19)
1384 fcap[1] = '1' + i - 11;
1385 else if (i <= 45)
1386 fcap[1] = 'A' + i - 20;
1387 else
1388 fcap[1] = 'a' + i - 46;
1391 char *sequence = tgetstr (fcap, address);
1392 if (sequence)
1394 sprintf (fkey, "f%d", i);
1395 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence),
1396 Fmake_vector (make_number (1),
1397 intern (fkey)));
1404 * Various mappings to try and get a better fit.
1407 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1408 if (!tgetstr (cap1, address)) \
1410 char *sequence = tgetstr (cap2, address); \
1411 if (sequence) \
1412 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence), \
1413 Fmake_vector (make_number (1), \
1414 intern (sym))); \
1417 /* if there's no key_next keycap, map key_npage to `next' keysym */
1418 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1419 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1420 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1421 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1422 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1423 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1424 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1426 /* IBM has their own non-standard dialect of terminfo.
1427 If the standard name isn't found, try the IBM name. */
1428 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1429 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1430 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1431 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1432 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1433 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1434 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1435 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1436 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1437 #undef CONDITIONAL_REASSIGN
1440 return Qnil;
1442 #endif /* not DOS_NT */
1445 /***********************************************************************
1446 Character Display Information
1447 ***********************************************************************/
1448 static void append_glyph (struct it *);
1449 static void produce_stretch_glyph (struct it *);
1450 static void append_composite_glyph (struct it *);
1451 static void produce_composite_glyph (struct it *);
1452 static void append_glyphless_glyph (struct it *, int, const char *);
1453 static void produce_glyphless_glyph (struct it *, int, Lisp_Object);
1455 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1456 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1457 the character for which to produce glyphs; IT->face_id contains the
1458 character's face. Padding glyphs are appended if IT->c has a
1459 IT->pixel_width > 1. */
1461 static void
1462 append_glyph (struct it *it)
1464 struct glyph *glyph, *end;
1465 int i;
1467 xassert (it->glyph_row);
1468 glyph = (it->glyph_row->glyphs[it->area]
1469 + it->glyph_row->used[it->area]);
1470 end = it->glyph_row->glyphs[1 + it->area];
1472 /* If the glyph row is reversed, we need to prepend the glyph rather
1473 than append it. */
1474 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1476 struct glyph *g;
1477 int move_by = it->pixel_width;
1479 /* Make room for the new glyphs. */
1480 if (move_by > end - glyph) /* don't overstep end of this area */
1481 move_by = end - glyph;
1482 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1483 g[move_by] = *g;
1484 glyph = it->glyph_row->glyphs[it->area];
1485 end = glyph + move_by;
1488 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1489 right, even in the REVERSED_P case, since (a) all of its u.ch are
1490 identical, and (b) the PADDING_P flag needs to be set for the
1491 leftmost one, because we write to the terminal left-to-right. */
1492 for (i = 0;
1493 i < it->pixel_width && glyph < end;
1494 ++i)
1496 glyph->type = CHAR_GLYPH;
1497 glyph->pixel_width = 1;
1498 glyph->u.ch = it->char_to_display;
1499 glyph->face_id = it->face_id;
1500 glyph->padding_p = i > 0;
1501 glyph->charpos = CHARPOS (it->position);
1502 glyph->object = it->object;
1503 if (it->bidi_p)
1505 glyph->resolved_level = it->bidi_it.resolved_level;
1506 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1507 abort ();
1508 glyph->bidi_type = it->bidi_it.type;
1510 else
1512 glyph->resolved_level = 0;
1513 glyph->bidi_type = UNKNOWN_BT;
1516 ++it->glyph_row->used[it->area];
1517 ++glyph;
1521 /* Produce glyphs for the display element described by IT. *IT
1522 specifies what we want to produce a glyph for (character, image, ...),
1523 and where in the glyph matrix we currently are (glyph row and hpos).
1524 produce_glyphs fills in output fields of *IT with information such as the
1525 pixel width and height of a character, and maybe output actual glyphs at
1526 the same time if IT->glyph_row is non-null. For an overview, see
1527 the explanation in dispextern.h, before the definition of the
1528 display_element_type enumeration.
1530 produce_glyphs also stores the result of glyph width, ascent
1531 etc. computations in *IT.
1533 IT->glyph_row may be null, in which case produce_glyphs does not
1534 actually fill in the glyphs. This is used in the move_* functions
1535 in xdisp.c for text width and height computations.
1537 Callers usually don't call produce_glyphs directly;
1538 instead they use the macro PRODUCE_GLYPHS. */
1540 void
1541 produce_glyphs (struct it *it)
1543 /* If a hook is installed, let it do the work. */
1545 /* Nothing but characters are supported on terminal frames. */
1546 xassert (it->what == IT_CHARACTER
1547 || it->what == IT_COMPOSITION
1548 || it->what == IT_STRETCH);
1550 if (it->what == IT_STRETCH)
1552 produce_stretch_glyph (it);
1553 goto done;
1556 if (it->what == IT_COMPOSITION)
1558 produce_composite_glyph (it);
1559 goto done;
1562 if (it->what == IT_GLYPHLESS)
1564 produce_glyphless_glyph (it, 0, Qnil);
1565 goto done;
1568 if (it->char_to_display >= 040 && it->char_to_display < 0177)
1570 it->pixel_width = it->nglyphs = 1;
1571 if (it->glyph_row)
1572 append_glyph (it);
1574 else if (it->char_to_display == '\n')
1575 it->pixel_width = it->nglyphs = 0;
1576 else if (it->char_to_display == '\t')
1578 int absolute_x = (it->current_x
1579 + it->continuation_lines_width);
1580 int next_tab_x
1581 = (((1 + absolute_x + it->tab_width - 1)
1582 / it->tab_width)
1583 * it->tab_width);
1584 int nspaces;
1586 /* If part of the TAB has been displayed on the previous line
1587 which is continued now, continuation_lines_width will have
1588 been incremented already by the part that fitted on the
1589 continued line. So, we will get the right number of spaces
1590 here. */
1591 nspaces = next_tab_x - absolute_x;
1593 if (it->glyph_row)
1595 int n = nspaces;
1597 it->char_to_display = ' ';
1598 it->pixel_width = it->len = 1;
1600 while (n--)
1601 append_glyph (it);
1604 it->pixel_width = nspaces;
1605 it->nglyphs = nspaces;
1607 else if (CHAR_BYTE8_P (it->char_to_display))
1609 /* Coming here means that we must send the raw 8-bit byte as is
1610 to the terminal. Although there's no way to know how many
1611 columns it occupies on a screen, it is a good assumption that
1612 a single byte code has 1-column width. */
1613 it->pixel_width = it->nglyphs = 1;
1614 if (it->glyph_row)
1615 append_glyph (it);
1617 else
1619 Lisp_Object charset_list = FRAME_TERMINAL (it->f)->charset_list;
1621 if (char_charset (it->char_to_display, charset_list, NULL))
1623 it->pixel_width = CHAR_WIDTH (it->char_to_display);
1624 it->nglyphs = it->pixel_width;
1625 if (it->glyph_row)
1626 append_glyph (it);
1628 else
1630 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
1632 xassert (it->what == IT_GLYPHLESS);
1633 produce_glyphless_glyph (it, 1, acronym);
1637 done:
1638 /* Advance current_x by the pixel width as a convenience for
1639 the caller. */
1640 if (it->area == TEXT_AREA)
1641 it->current_x += it->pixel_width;
1642 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1643 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1647 /* Produce a stretch glyph for iterator IT. IT->object is the value
1648 of the glyph property displayed. The value must be a list
1649 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1650 being recognized:
1652 1. `:width WIDTH' specifies that the space should be WIDTH *
1653 canonical char width wide. WIDTH may be an integer or floating
1654 point number.
1656 2. `:align-to HPOS' specifies that the space should be wide enough
1657 to reach HPOS, a value in canonical character units. */
1659 static void
1660 produce_stretch_glyph (struct it *it)
1662 /* (space :width WIDTH ...) */
1663 Lisp_Object prop, plist;
1664 int width = 0, align_to = -1;
1665 int zero_width_ok_p = 0;
1666 double tem;
1668 /* List should start with `space'. */
1669 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1670 plist = XCDR (it->object);
1672 /* Compute the width of the stretch. */
1673 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1674 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1676 /* Absolute width `:width WIDTH' specified and valid. */
1677 zero_width_ok_p = 1;
1678 width = (int)(tem + 0.5);
1680 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1681 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1683 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1684 align_to = (align_to < 0
1686 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1687 else if (align_to < 0)
1688 align_to = window_box_left_offset (it->w, TEXT_AREA);
1689 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1690 zero_width_ok_p = 1;
1692 else
1693 /* Nothing specified -> width defaults to canonical char width. */
1694 width = FRAME_COLUMN_WIDTH (it->f);
1696 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1697 width = 1;
1699 if (width > 0 && it->line_wrap != TRUNCATE
1700 && it->current_x + width > it->last_visible_x)
1701 width = it->last_visible_x - it->current_x - 1;
1703 if (width > 0 && it->glyph_row)
1705 Lisp_Object o_object = it->object;
1706 Lisp_Object object = it->stack[it->sp - 1].string;
1707 int n = width;
1709 if (!STRINGP (object))
1710 object = it->w->buffer;
1711 it->object = object;
1712 it->char_to_display = ' ';
1713 it->pixel_width = it->len = 1;
1714 while (n--)
1715 append_glyph (it);
1716 it->object = o_object;
1718 it->pixel_width = width;
1719 it->nglyphs = width;
1723 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1724 Called from produce_composite_glyph for terminal frames if
1725 IT->glyph_row != NULL. IT->face_id contains the character's
1726 face. */
1728 static void
1729 append_composite_glyph (struct it *it)
1731 struct glyph *glyph;
1733 xassert (it->glyph_row);
1734 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1735 if (glyph < it->glyph_row->glyphs[1 + it->area])
1737 /* If the glyph row is reversed, we need to prepend the glyph
1738 rather than append it. */
1739 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1741 struct glyph *g;
1743 /* Make room for the new glyph. */
1744 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1745 g[1] = *g;
1746 glyph = it->glyph_row->glyphs[it->area];
1748 glyph->type = COMPOSITE_GLYPH;
1749 glyph->pixel_width = it->pixel_width;
1750 glyph->u.cmp.id = it->cmp_it.id;
1751 if (it->cmp_it.ch < 0)
1753 glyph->u.cmp.automatic = 0;
1754 glyph->u.cmp.id = it->cmp_it.id;
1756 else
1758 glyph->u.cmp.automatic = 1;
1759 glyph->u.cmp.id = it->cmp_it.id;
1760 glyph->slice.cmp.from = it->cmp_it.from;
1761 glyph->slice.cmp.to = it->cmp_it.to - 1;
1764 glyph->face_id = it->face_id;
1765 glyph->padding_p = 0;
1766 glyph->charpos = CHARPOS (it->position);
1767 glyph->object = it->object;
1768 if (it->bidi_p)
1770 glyph->resolved_level = it->bidi_it.resolved_level;
1771 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1772 abort ();
1773 glyph->bidi_type = it->bidi_it.type;
1775 else
1777 glyph->resolved_level = 0;
1778 glyph->bidi_type = UNKNOWN_BT;
1781 ++it->glyph_row->used[it->area];
1782 ++glyph;
1787 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1788 the composition. We simply produces components of the composition
1789 assuming that the terminal has a capability to layout/render it
1790 correctly. */
1792 static void
1793 produce_composite_glyph (struct it *it)
1795 if (it->cmp_it.ch < 0)
1797 struct composition *cmp = composition_table[it->cmp_it.id];
1799 it->pixel_width = cmp->width;
1801 else
1803 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
1805 it->pixel_width = composition_gstring_width (gstring, it->cmp_it.from,
1806 it->cmp_it.to, NULL);
1808 it->nglyphs = 1;
1809 if (it->glyph_row)
1810 append_composite_glyph (it);
1814 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
1815 is a face ID to be used for the glyph. What is actually appended
1816 are glyphs of type CHAR_GLYPH whose characters are in STR (which
1817 comes from it->nglyphs bytes). */
1819 static void
1820 append_glyphless_glyph (struct it *it, int face_id, const char *str)
1822 struct glyph *glyph, *end;
1823 int i;
1825 xassert (it->glyph_row);
1826 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1827 end = it->glyph_row->glyphs[1 + it->area];
1829 /* If the glyph row is reversed, we need to prepend the glyph rather
1830 than append it. */
1831 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1833 struct glyph *g;
1834 int move_by = it->pixel_width;
1836 /* Make room for the new glyphs. */
1837 if (move_by > end - glyph) /* don't overstep end of this area */
1838 move_by = end - glyph;
1839 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1840 g[move_by] = *g;
1841 glyph = it->glyph_row->glyphs[it->area];
1842 end = glyph + move_by;
1845 if (glyph >= end)
1846 return;
1847 glyph->type = CHAR_GLYPH;
1848 glyph->pixel_width = 1;
1849 glyph->face_id = face_id;
1850 glyph->padding_p = 0;
1851 glyph->charpos = CHARPOS (it->position);
1852 glyph->object = it->object;
1853 if (it->bidi_p)
1855 glyph->resolved_level = it->bidi_it.resolved_level;
1856 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1857 abort ();
1858 glyph->bidi_type = it->bidi_it.type;
1860 else
1862 glyph->resolved_level = 0;
1863 glyph->bidi_type = UNKNOWN_BT;
1866 /* BIDI Note: we put the glyphs of characters left to right, even in
1867 the REVERSED_P case because we write to the terminal
1868 left-to-right. */
1869 for (i = 0; i < it->nglyphs && glyph < end; ++i)
1871 if (i > 0)
1872 glyph[0] = glyph[-1];
1873 glyph->u.ch = str[i];
1874 ++it->glyph_row->used[it->area];
1875 ++glyph;
1879 /* Produce glyphs for a glyphless character for iterator IT.
1880 IT->glyphless_method specifies which method to use for displaying
1881 the character. See the description of enum
1882 glyphless_display_method in dispextern.h for the details.
1884 FOR_NO_FONT is nonzero if and only if this is for a character that
1885 is not supproted by the coding system of the terminal. ACRONYM, if
1886 non-nil, is an acronym string for the character.
1888 The glyphs actually produced are of type CHAR_GLYPH. */
1890 static void
1891 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
1893 int face_id;
1894 int len;
1895 char buf[9];
1896 char const *str = " ";
1898 /* Get a face ID for the glyph by utilizing a cache (the same way as
1899 done for `escape-glyph' in get_next_display_element). */
1900 if (it->f == last_glyphless_glyph_frame
1901 && it->face_id == last_glyphless_glyph_face_id)
1903 face_id = last_glyphless_glyph_merged_face_id;
1905 else
1907 /* Merge the `glyphless-char' face into the current face. */
1908 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
1909 last_glyphless_glyph_frame = it->f;
1910 last_glyphless_glyph_face_id = it->face_id;
1911 last_glyphless_glyph_merged_face_id = face_id;
1914 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
1916 /* As there's no way to produce a thin space, we produce a space
1917 of canonical width. */
1918 len = 1;
1920 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
1922 len = CHAR_WIDTH (it->c);
1923 if (len == 0)
1924 len = 1;
1925 else if (len > 4)
1926 len = 4;
1927 sprintf (buf, "[%.*s]", len, str);
1928 len += 2;
1929 str = buf;
1931 else
1933 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
1935 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
1936 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
1937 buf[0] = '[';
1938 str = STRINGP (acronym) ? SSDATA (acronym) : "";
1939 for (len = 0; len < 6 && str[len] && ASCII_BYTE_P (str[len]); len++)
1940 buf[1 + len] = str[len];
1941 buf[1 + len] = ']';
1942 len += 2;
1944 else
1946 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
1947 len = (it->c < 0x10000 ? sprintf (buf, "\\u%04X", it->c)
1948 : it->c <= MAX_UNICODE_CHAR ? sprintf (buf, "\\U%06X", it->c)
1949 : sprintf (buf, "\\x%06X", it->c));
1951 str = buf;
1954 it->pixel_width = len;
1955 it->nglyphs = len;
1956 if (it->glyph_row)
1957 append_glyphless_glyph (it, face_id, str);
1961 /* Get information about special display element WHAT in an
1962 environment described by IT. WHAT is one of IT_TRUNCATION or
1963 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1964 non-null glyph_row member. This function ensures that fields like
1965 face_id, c, len of IT are left untouched. */
1967 void
1968 produce_special_glyphs (struct it *it, enum display_element_type what)
1970 struct it temp_it;
1971 Lisp_Object gc;
1972 GLYPH glyph;
1974 temp_it = *it;
1975 temp_it.dp = NULL;
1976 temp_it.what = IT_CHARACTER;
1977 temp_it.len = 1;
1978 temp_it.object = make_number (0);
1979 memset (&temp_it.current, 0, sizeof temp_it.current);
1981 if (what == IT_CONTINUATION)
1983 /* Continuation glyph. For R2L lines, we mirror it by hand. */
1984 if (it->bidi_it.paragraph_dir == R2L)
1985 SET_GLYPH_FROM_CHAR (glyph, '/');
1986 else
1987 SET_GLYPH_FROM_CHAR (glyph, '\\');
1988 if (it->dp
1989 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc))
1990 && GLYPH_CODE_CHAR_VALID_P (gc))
1992 /* FIXME: Should we mirror GC for R2L lines? */
1993 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1994 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1997 else if (what == IT_TRUNCATION)
1999 /* Truncation glyph. */
2000 SET_GLYPH_FROM_CHAR (glyph, '$');
2001 if (it->dp
2002 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc))
2003 && GLYPH_CODE_CHAR_VALID_P (gc))
2005 /* FIXME: Should we mirror GC for R2L lines? */
2006 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
2007 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
2010 else
2011 abort ();
2013 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
2014 temp_it.face_id = GLYPH_FACE (glyph);
2015 temp_it.len = CHAR_BYTES (temp_it.c);
2017 produce_glyphs (&temp_it);
2018 it->pixel_width = temp_it.pixel_width;
2019 it->nglyphs = temp_it.pixel_width;
2024 /***********************************************************************
2025 Faces
2026 ***********************************************************************/
2028 /* Value is non-zero if attribute ATTR may be used. ATTR should be
2029 one of the enumerators from enum no_color_bit, or a bit set built
2030 from them. Some display attributes may not be used together with
2031 color; the termcap capability `NC' specifies which ones. */
2033 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
2034 (tty->TN_max_colors > 0 \
2035 ? (tty->TN_no_color_video & (ATTR)) == 0 \
2036 : 1)
2038 /* Turn appearances of face FACE_ID on tty frame F on.
2039 FACE_ID is a realized face ID number, in the face cache. */
2041 static void
2042 turn_on_face (struct frame *f, int face_id)
2044 struct face *face = FACE_FROM_ID (f, face_id);
2045 long fg = face->foreground;
2046 long bg = face->background;
2047 struct tty_display_info *tty = FRAME_TTY (f);
2049 /* Do this first because TS_end_standout_mode may be the same
2050 as TS_exit_attribute_mode, which turns all appearances off. */
2051 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
2053 if (tty->TN_max_colors > 0)
2055 if (fg >= 0 && bg >= 0)
2057 /* If the terminal supports colors, we can set them
2058 below without using reverse video. The face's fg
2059 and bg colors are set as they should appear on
2060 the screen, i.e. they take the inverse-video'ness
2061 of the face already into account. */
2063 else if (inverse_video)
2065 if (fg == FACE_TTY_DEFAULT_FG_COLOR
2066 || bg == FACE_TTY_DEFAULT_BG_COLOR)
2067 tty_toggle_highlight (tty);
2069 else
2071 if (fg == FACE_TTY_DEFAULT_BG_COLOR
2072 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2073 tty_toggle_highlight (tty);
2076 else
2078 /* If we can't display colors, use reverse video
2079 if the face specifies that. */
2080 if (inverse_video)
2082 if (fg == FACE_TTY_DEFAULT_FG_COLOR
2083 || bg == FACE_TTY_DEFAULT_BG_COLOR)
2084 tty_toggle_highlight (tty);
2086 else
2088 if (fg == FACE_TTY_DEFAULT_BG_COLOR
2089 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2090 tty_toggle_highlight (tty);
2095 if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
2096 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
2098 if (face->tty_dim_p && MAY_USE_WITH_COLORS_P (tty, NC_DIM))
2099 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
2101 /* Alternate charset and blinking not yet used. */
2102 if (face->tty_alt_charset_p
2103 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
2104 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
2106 if (face->tty_blinking_p
2107 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
2108 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
2110 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
2111 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
2113 if (tty->TN_max_colors > 0)
2115 const char *ts;
2116 char *p;
2118 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
2119 if (fg >= 0 && ts)
2121 p = tparam (ts, NULL, 0, (int) fg, 0, 0, 0);
2122 OUTPUT (tty, p);
2123 xfree (p);
2126 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
2127 if (bg >= 0 && ts)
2129 p = tparam (ts, NULL, 0, (int) bg, 0, 0, 0);
2130 OUTPUT (tty, p);
2131 xfree (p);
2137 /* Turn off appearances of face FACE_ID on tty frame F. */
2139 static void
2140 turn_off_face (struct frame *f, int face_id)
2142 struct face *face = FACE_FROM_ID (f, face_id);
2143 struct tty_display_info *tty = FRAME_TTY (f);
2145 xassert (face != NULL);
2147 if (tty->TS_exit_attribute_mode)
2149 /* Capability "me" will turn off appearance modes double-bright,
2150 half-bright, reverse-video, standout, underline. It may or
2151 may not turn off alt-char-mode. */
2152 if (face->tty_bold_p
2153 || face->tty_dim_p
2154 || face->tty_reverse_p
2155 || face->tty_alt_charset_p
2156 || face->tty_blinking_p
2157 || face->tty_underline_p)
2159 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
2160 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
2161 tty->standout_mode = 0;
2164 if (face->tty_alt_charset_p)
2165 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2167 else
2169 /* If we don't have "me" we can only have those appearances
2170 that have exit sequences defined. */
2171 if (face->tty_alt_charset_p)
2172 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2174 if (face->tty_underline_p)
2175 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
2178 /* Switch back to default colors. */
2179 if (tty->TN_max_colors > 0
2180 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2181 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2182 || (face->background != FACE_TTY_DEFAULT_COLOR
2183 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
2184 OUTPUT1_IF (tty, tty->TS_orig_pair);
2188 /* Return non-zero if the terminal on frame F supports all of the
2189 capabilities in CAPS simultaneously, with foreground and background
2190 colors FG and BG. */
2193 tty_capable_p (struct tty_display_info *tty, unsigned int caps,
2194 unsigned long fg, unsigned long bg)
2196 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2197 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2198 return 0;
2200 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2201 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2202 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2203 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
2204 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
2205 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
2207 /* We can do it! */
2208 return 1;
2211 /* Return non-zero if the terminal is capable to display colors. */
2213 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2214 0, 1, 0,
2215 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2217 TERMINAL can be a terminal object, a frame, or nil (meaning the
2218 selected frame's terminal). This function always returns nil if
2219 TERMINAL does not refer to a text-only terminal. */)
2220 (Lisp_Object terminal)
2222 struct terminal *t = get_tty_terminal (terminal, 0);
2223 if (!t)
2224 return Qnil;
2225 else
2226 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
2229 /* Return the number of supported colors. */
2230 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2231 Stty_display_color_cells, 0, 1, 0,
2232 doc: /* Return the number of colors supported by the tty device TERMINAL.
2234 TERMINAL can be a terminal object, a frame, or nil (meaning the
2235 selected frame's terminal). This function always returns 0 if
2236 TERMINAL does not refer to a text-only terminal. */)
2237 (Lisp_Object terminal)
2239 struct terminal *t = get_tty_terminal (terminal, 0);
2240 if (!t)
2241 return make_number (0);
2242 else
2243 return make_number (t->display_info.tty->TN_max_colors);
2246 #ifndef DOS_NT
2248 /* Declare here rather than in the function, as in the rest of Emacs,
2249 to work around an HPUX compiler bug (?). See
2250 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2251 static int default_max_colors;
2252 static int default_max_pairs;
2253 static int default_no_color_video;
2254 static char *default_orig_pair;
2255 static char *default_set_foreground;
2256 static char *default_set_background;
2258 /* Save or restore the default color-related capabilities of this
2259 terminal. */
2260 static void
2261 tty_default_color_capabilities (struct tty_display_info *tty, int save)
2264 if (save)
2266 xfree (default_orig_pair);
2267 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
2269 xfree (default_set_foreground);
2270 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
2271 : NULL;
2273 xfree (default_set_background);
2274 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
2275 : NULL;
2277 default_max_colors = tty->TN_max_colors;
2278 default_max_pairs = tty->TN_max_pairs;
2279 default_no_color_video = tty->TN_no_color_video;
2281 else
2283 tty->TS_orig_pair = default_orig_pair;
2284 tty->TS_set_foreground = default_set_foreground;
2285 tty->TS_set_background = default_set_background;
2286 tty->TN_max_colors = default_max_colors;
2287 tty->TN_max_pairs = default_max_pairs;
2288 tty->TN_no_color_video = default_no_color_video;
2292 /* Setup one of the standard tty color schemes according to MODE.
2293 MODE's value is generally the number of colors which we want to
2294 support; zero means set up for the default capabilities, the ones
2295 we saw at init_tty time; -1 means turn off color support. */
2296 static void
2297 tty_setup_colors (struct tty_display_info *tty, int mode)
2299 /* Canonicalize all negative values of MODE. */
2300 if (mode < -1)
2301 mode = -1;
2303 switch (mode)
2305 case -1: /* no colors at all */
2306 tty->TN_max_colors = 0;
2307 tty->TN_max_pairs = 0;
2308 tty->TN_no_color_video = 0;
2309 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2310 break;
2311 case 0: /* default colors, if any */
2312 default:
2313 tty_default_color_capabilities (tty, 0);
2314 break;
2315 case 8: /* 8 standard ANSI colors */
2316 tty->TS_orig_pair = "\033[0m";
2317 #ifdef TERMINFO
2318 tty->TS_set_foreground = "\033[3%p1%dm";
2319 tty->TS_set_background = "\033[4%p1%dm";
2320 #else
2321 tty->TS_set_foreground = "\033[3%dm";
2322 tty->TS_set_background = "\033[4%dm";
2323 #endif
2324 tty->TN_max_colors = 8;
2325 tty->TN_max_pairs = 64;
2326 tty->TN_no_color_video = 0;
2327 break;
2331 void
2332 set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
2334 Lisp_Object tem, val;
2335 Lisp_Object color_mode;
2336 int mode;
2337 Lisp_Object tty_color_mode_alist
2338 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
2340 tem = assq_no_quit (Qtty_color_mode, f->param_alist);
2341 val = CONSP (tem) ? XCDR (tem) : Qnil;
2343 if (INTEGERP (val))
2344 color_mode = val;
2345 else if (SYMBOLP (tty_color_mode_alist))
2347 tem = Fassq (val, Fsymbol_value (tty_color_mode_alist));
2348 color_mode = CONSP (tem) ? XCDR (tem) : Qnil;
2350 else
2351 color_mode = Qnil;
2353 mode = INTEGERP (color_mode) ? XINT (color_mode) : 0;
2355 if (mode != tty->previous_color_mode)
2357 Lisp_Object funsym = intern ("tty-set-up-initial-frame-faces");
2358 tty->previous_color_mode = mode;
2359 tty_setup_colors (tty , mode);
2360 /* This recomputes all the faces given the new color definitions. */
2361 safe_call (1, &funsym);
2365 #endif /* !DOS_NT */
2369 /* Return the tty display object specified by TERMINAL. */
2371 struct terminal *
2372 get_tty_terminal (Lisp_Object terminal, int throw)
2374 struct terminal *t = get_terminal (terminal, throw);
2376 if (t && t->type != output_termcap && t->type != output_msdos_raw)
2378 if (throw)
2379 error ("Device %d is not a termcap terminal device", t->id);
2380 else
2381 return NULL;
2384 return t;
2387 /* Return an active termcap device that uses the tty device with the
2388 given name.
2390 This function ignores suspended devices.
2392 Returns NULL if the named terminal device is not opened. */
2394 struct terminal *
2395 get_named_tty (const char *name)
2397 struct terminal *t;
2399 if (!name)
2400 abort ();
2402 for (t = terminal_list; t; t = t->next_terminal)
2404 if ((t->type == output_termcap || t->type == output_msdos_raw)
2405 && !strcmp (t->display_info.tty->name, name)
2406 && TERMINAL_ACTIVE_P (t))
2407 return t;
2410 return 0;
2414 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2415 doc: /* Return the type of the tty device that TERMINAL uses.
2416 Returns nil if TERMINAL is not on a tty device.
2418 TERMINAL can be a terminal object, a frame, or nil (meaning the
2419 selected frame's terminal). */)
2420 (Lisp_Object terminal)
2422 struct terminal *t = get_terminal (terminal, 1);
2424 if (t->type != output_termcap && t->type != output_msdos_raw)
2425 return Qnil;
2427 if (t->display_info.tty->type)
2428 return build_string (t->display_info.tty->type);
2429 else
2430 return Qnil;
2433 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2434 doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2436 TERMINAL can be a terminal object, a frame, or nil (meaning the
2437 selected frame's terminal). This function always returns nil if
2438 TERMINAL is not on a tty device. */)
2439 (Lisp_Object terminal)
2441 struct terminal *t = get_terminal (terminal, 1);
2443 if ((t->type != output_termcap && t->type != output_msdos_raw)
2444 || strcmp (t->display_info.tty->name, DEV_TTY) != 0)
2445 return Qnil;
2446 else
2447 return Qt;
2450 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2451 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2452 This is used to override the terminfo data, for certain terminals that
2453 do not really do underlining, but say that they do. This function has
2454 no effect if used on a non-tty terminal.
2456 TERMINAL can be a terminal object, a frame or nil (meaning the
2457 selected frame's terminal). This function always returns nil if
2458 TERMINAL does not refer to a text-only terminal. */)
2459 (Lisp_Object terminal)
2461 struct terminal *t = get_terminal (terminal, 1);
2463 if (t->type == output_termcap)
2464 t->display_info.tty->TS_enter_underline_mode = 0;
2465 return Qnil;
2470 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2471 doc: /* Suspend the terminal device TTY.
2473 The device is restored to its default state, and Emacs ceases all
2474 access to the tty device. Frames that use the device are not deleted,
2475 but input is not read from them and if they change, their display is
2476 not updated.
2478 TTY may be a terminal object, a frame, or nil for the terminal device
2479 of the currently selected frame.
2481 This function runs `suspend-tty-functions' after suspending the
2482 device. The functions are run with one arg, the id of the suspended
2483 terminal device.
2485 `suspend-tty' does nothing if it is called on a device that is already
2486 suspended.
2488 A suspended tty may be resumed by calling `resume-tty' on it. */)
2489 (Lisp_Object tty)
2491 struct terminal *t = get_tty_terminal (tty, 1);
2492 FILE *f;
2494 if (!t)
2495 error ("Unknown tty device");
2497 f = t->display_info.tty->input;
2499 if (f)
2501 /* First run `suspend-tty-functions' and then clean up the tty
2502 state because `suspend-tty-functions' might need to change
2503 the tty state. */
2504 Lisp_Object args[2];
2505 args[0] = intern ("suspend-tty-functions");
2506 XSETTERMINAL (args[1], t);
2507 Frun_hook_with_args (2, args);
2509 reset_sys_modes (t->display_info.tty);
2510 delete_keyboard_wait_descriptor (fileno (f));
2512 #ifndef MSDOS
2513 fclose (f);
2514 if (f != t->display_info.tty->output)
2515 fclose (t->display_info.tty->output);
2516 #endif
2518 t->display_info.tty->input = 0;
2519 t->display_info.tty->output = 0;
2521 if (FRAMEP (t->display_info.tty->top_frame))
2522 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2526 /* Clear display hooks to prevent further output. */
2527 clear_tty_hooks (t);
2529 return Qnil;
2532 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2533 doc: /* Resume the previously suspended terminal device TTY.
2534 The terminal is opened and reinitialized. Frames that are on the
2535 suspended terminal are revived.
2537 It is an error to resume a terminal while another terminal is active
2538 on the same device.
2540 This function runs `resume-tty-functions' after resuming the terminal.
2541 The functions are run with one arg, the id of the resumed terminal
2542 device.
2544 `resume-tty' does nothing if it is called on a device that is not
2545 suspended.
2547 TTY may be a terminal object, a frame, or nil (meaning the selected
2548 frame's terminal). */)
2549 (Lisp_Object tty)
2551 struct terminal *t = get_tty_terminal (tty, 1);
2552 int fd;
2554 if (!t)
2555 error ("Unknown tty device");
2557 if (!t->display_info.tty->input)
2559 if (get_named_tty (t->display_info.tty->name))
2560 error ("Cannot resume display while another display is active on the same device");
2562 #ifdef MSDOS
2563 t->display_info.tty->output = stdout;
2564 t->display_info.tty->input = stdin;
2565 #else /* !MSDOS */
2566 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2568 if (fd == -1)
2569 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2571 if (strcmp (t->display_info.tty->name, DEV_TTY))
2572 dissociate_if_controlling_tty (fd);
2574 t->display_info.tty->output = fdopen (fd, "w+");
2575 t->display_info.tty->input = t->display_info.tty->output;
2576 #endif
2578 add_keyboard_wait_descriptor (fd);
2580 if (FRAMEP (t->display_info.tty->top_frame))
2582 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2583 int width, height;
2584 int old_height = FRAME_COLS (f);
2585 int old_width = FRAME_LINES (f);
2587 /* Check if terminal/window size has changed while the frame
2588 was suspended. */
2589 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2590 if (width != old_width || height != old_height)
2591 change_frame_size (f, height, width, 0, 0, 0);
2592 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2595 init_sys_modes (t->display_info.tty);
2598 /* Run `resume-tty-functions'. */
2599 Lisp_Object args[2];
2600 args[0] = intern ("resume-tty-functions");
2601 XSETTERMINAL (args[1], t);
2602 Frun_hook_with_args (2, args);
2606 set_tty_hooks (t);
2608 return Qnil;
2612 /***********************************************************************
2613 Mouse
2614 ***********************************************************************/
2616 #ifdef HAVE_GPM
2617 void
2618 term_mouse_moveto (int x, int y)
2620 /* TODO: how to set mouse position?
2621 const char *name;
2622 int fd;
2623 name = (const char *) ttyname (0);
2624 fd = open (name, O_WRONLY);
2625 SOME_FUNCTION (x, y, fd);
2626 close (fd);
2627 last_mouse_x = x;
2628 last_mouse_y = y; */
2631 /* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2632 void
2633 tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row,
2634 int start_hpos, int end_hpos,
2635 enum draw_glyphs_face draw)
2637 int nglyphs = end_hpos - start_hpos;
2638 struct frame *f = XFRAME (WINDOW_FRAME (w));
2639 struct tty_display_info *tty = FRAME_TTY (f);
2640 int face_id = tty->mouse_highlight.mouse_face_face_id;
2641 int save_x, save_y, pos_x, pos_y;
2643 if (end_hpos >= row->used[TEXT_AREA])
2644 nglyphs = row->used[TEXT_AREA] - start_hpos;
2646 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2647 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos + WINDOW_LEFT_EDGE_X (w);
2649 /* Save current cursor co-ordinates. */
2650 save_y = curY (tty);
2651 save_x = curX (tty);
2652 cursor_to (f, pos_y, pos_x);
2654 if (draw == DRAW_MOUSE_FACE)
2655 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2656 nglyphs, face_id);
2657 else if (draw == DRAW_NORMAL_TEXT)
2658 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
2660 cursor_to (f, save_y, save_x);
2663 static int
2664 term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2666 /* Has the mouse moved off the glyph it was on at the last sighting? */
2667 if (event->x != last_mouse_x || event->y != last_mouse_y)
2669 frame->mouse_moved = 1;
2670 note_mouse_highlight (frame, event->x, event->y);
2671 /* Remember which glyph we're now on. */
2672 last_mouse_x = event->x;
2673 last_mouse_y = event->y;
2674 return 1;
2676 return 0;
2679 /* Return the current position of the mouse.
2681 Set *f to the frame the mouse is in, or zero if the mouse is in no
2682 Emacs frame. If it is set to zero, all the other arguments are
2683 garbage.
2685 Set *bar_window to Qnil, and *x and *y to the column and
2686 row of the character cell the mouse is over.
2688 Set *timeptr to the time the mouse was at the returned position.
2690 This clears mouse_moved until the next motion
2691 event arrives. */
2692 static void
2693 term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2694 enum scroll_bar_part *part, Lisp_Object *x,
2695 Lisp_Object *y, unsigned long *timeptr)
2697 struct timeval now;
2699 *fp = SELECTED_FRAME ();
2700 (*fp)->mouse_moved = 0;
2702 *bar_window = Qnil;
2703 *part = 0;
2705 XSETINT (*x, last_mouse_x);
2706 XSETINT (*y, last_mouse_y);
2707 gettimeofday(&now, 0);
2708 *timeptr = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2711 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2713 If the event is a button press, then note that we have grabbed
2714 the mouse. */
2716 static Lisp_Object
2717 term_mouse_click (struct input_event *result, Gpm_Event *event,
2718 struct frame *f)
2720 struct timeval now;
2721 int i, j;
2723 result->kind = GPM_CLICK_EVENT;
2724 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2726 if (event->buttons & j) {
2727 result->code = i; /* button number */
2728 break;
2731 gettimeofday(&now, 0);
2732 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2734 if (event->type & GPM_UP)
2735 result->modifiers = up_modifier;
2736 else if (event->type & GPM_DOWN)
2737 result->modifiers = down_modifier;
2738 else
2739 result->modifiers = 0;
2741 if (event->type & GPM_SINGLE)
2742 result->modifiers |= click_modifier;
2744 if (event->type & GPM_DOUBLE)
2745 result->modifiers |= double_modifier;
2747 if (event->type & GPM_TRIPLE)
2748 result->modifiers |= triple_modifier;
2750 if (event->type & GPM_DRAG)
2751 result->modifiers |= drag_modifier;
2753 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
2755 /* 1 << KG_SHIFT */
2756 if (event->modifiers & (1 << 0))
2757 result->modifiers |= shift_modifier;
2759 /* 1 << KG_CTRL */
2760 if (event->modifiers & (1 << 2))
2761 result->modifiers |= ctrl_modifier;
2763 /* 1 << KG_ALT || KG_ALTGR */
2764 if (event->modifiers & (1 << 3)
2765 || event->modifiers & (1 << 1))
2766 result->modifiers |= meta_modifier;
2769 XSETINT (result->x, event->x);
2770 XSETINT (result->y, event->y);
2771 XSETFRAME (result->frame_or_window, f);
2772 result->arg = Qnil;
2773 return Qnil;
2777 handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
2779 struct frame *f = XFRAME (tty->top_frame);
2780 struct input_event ie;
2781 int do_help = 0;
2782 int count = 0;
2784 EVENT_INIT (ie);
2785 ie.kind = NO_EVENT;
2786 ie.arg = Qnil;
2788 if (event->type & (GPM_MOVE | GPM_DRAG)) {
2789 previous_help_echo_string = help_echo_string;
2790 help_echo_string = Qnil;
2792 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
2794 if (!term_mouse_movement (f, event))
2795 help_echo_string = previous_help_echo_string;
2797 /* If the contents of the global variable help_echo_string
2798 has changed, generate a HELP_EVENT. */
2799 if (!NILP (help_echo_string)
2800 || !NILP (previous_help_echo_string))
2801 do_help = 1;
2803 goto done;
2805 else {
2806 f->mouse_moved = 0;
2807 term_mouse_click (&ie, event, f);
2810 done:
2811 if (ie.kind != NO_EVENT)
2813 kbd_buffer_store_event_hold (&ie, hold_quit);
2814 count++;
2817 if (do_help
2818 && !(hold_quit && hold_quit->kind != NO_EVENT))
2820 Lisp_Object frame;
2822 if (f)
2823 XSETFRAME (frame, f);
2824 else
2825 frame = Qnil;
2827 gen_help_event (help_echo_string, frame, help_echo_window,
2828 help_echo_object, help_echo_pos);
2829 count++;
2832 return count;
2835 DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
2836 0, 0, 0,
2837 doc: /* Open a connection to Gpm.
2838 Gpm-mouse can only be activated for one tty at a time. */)
2839 (void)
2841 struct frame *f = SELECTED_FRAME ();
2842 struct tty_display_info *tty
2843 = ((f)->output_method == output_termcap
2844 ? (f)->terminal->display_info.tty : NULL);
2845 Gpm_Connect connection;
2847 if (!tty)
2848 error ("Gpm-mouse only works in the GNU/Linux console");
2849 if (gpm_tty == tty)
2850 return Qnil; /* Already activated, nothing to do. */
2851 if (gpm_tty)
2852 error ("Gpm-mouse can only be activated for one tty at a time");
2854 connection.eventMask = ~0;
2855 connection.defaultMask = ~GPM_HARD;
2856 connection.maxMod = ~0;
2857 connection.minMod = 0;
2858 gpm_zerobased = 1;
2860 if (Gpm_Open (&connection, 0) < 0)
2861 error ("Gpm-mouse failed to connect to the gpm daemon");
2862 else
2864 gpm_tty = tty;
2865 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2866 to generate SIGIOs. Apparently we need to call reset_sys_modes
2867 before calling init_sys_modes. */
2868 reset_sys_modes (tty);
2869 init_sys_modes (tty);
2870 add_gpm_wait_descriptor (gpm_fd);
2871 return Qnil;
2875 void
2876 close_gpm (int fd)
2878 if (fd >= 0)
2879 delete_gpm_wait_descriptor (fd);
2880 while (Gpm_Close()); /* close all the stack */
2881 gpm_tty = NULL;
2884 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
2885 0, 0, 0,
2886 doc: /* Close a connection to Gpm. */)
2887 (void)
2889 struct frame *f = SELECTED_FRAME ();
2890 struct tty_display_info *tty
2891 = ((f)->output_method == output_termcap
2892 ? (f)->terminal->display_info.tty : NULL);
2894 if (!tty || gpm_tty != tty)
2895 return Qnil; /* Not activated on this terminal, nothing to do. */
2897 close_gpm (gpm_fd);
2898 return Qnil;
2900 #endif /* HAVE_GPM */
2903 #ifndef MSDOS
2904 /***********************************************************************
2905 Initialization
2906 ***********************************************************************/
2908 /* Initialize the tty-dependent part of frame F. The frame must
2909 already have its device initialized. */
2911 void
2912 create_tty_output (struct frame *f)
2914 struct tty_output *t;
2916 if (! FRAME_TERMCAP_P (f))
2917 abort ();
2919 t = xmalloc (sizeof (struct tty_output));
2920 memset (t, 0, sizeof (struct tty_output));
2922 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
2924 f->output_data.tty = t;
2927 /* Delete frame F's face cache, and its tty-dependent part. */
2929 static void
2930 tty_free_frame_resources (struct frame *f)
2932 if (! FRAME_TERMCAP_P (f))
2933 abort ();
2935 if (FRAME_FACE_CACHE (f))
2936 free_frame_faces (f);
2938 xfree (f->output_data.tty);
2941 #else /* MSDOS */
2943 /* Delete frame F's face cache. */
2945 static void
2946 tty_free_frame_resources (struct frame *f)
2948 if (! FRAME_TERMCAP_P (f) && ! FRAME_MSDOS_P (f))
2949 abort ();
2951 if (FRAME_FACE_CACHE (f))
2952 free_frame_faces (f);
2954 #endif /* MSDOS */
2956 /* Reset the hooks in TERMINAL. */
2958 static void
2959 clear_tty_hooks (struct terminal *terminal)
2961 terminal->rif = 0;
2962 terminal->cursor_to_hook = 0;
2963 terminal->raw_cursor_to_hook = 0;
2964 terminal->clear_to_end_hook = 0;
2965 terminal->clear_frame_hook = 0;
2966 terminal->clear_end_of_line_hook = 0;
2967 terminal->ins_del_lines_hook = 0;
2968 terminal->insert_glyphs_hook = 0;
2969 terminal->write_glyphs_hook = 0;
2970 terminal->delete_glyphs_hook = 0;
2971 terminal->ring_bell_hook = 0;
2972 terminal->reset_terminal_modes_hook = 0;
2973 terminal->set_terminal_modes_hook = 0;
2974 terminal->update_begin_hook = 0;
2975 terminal->update_end_hook = 0;
2976 terminal->set_terminal_window_hook = 0;
2977 terminal->mouse_position_hook = 0;
2978 terminal->frame_rehighlight_hook = 0;
2979 terminal->frame_raise_lower_hook = 0;
2980 terminal->fullscreen_hook = 0;
2981 terminal->set_vertical_scroll_bar_hook = 0;
2982 terminal->condemn_scroll_bars_hook = 0;
2983 terminal->redeem_scroll_bar_hook = 0;
2984 terminal->judge_scroll_bars_hook = 0;
2985 terminal->read_socket_hook = 0;
2986 terminal->frame_up_to_date_hook = 0;
2988 /* Leave these two set, or suspended frames are not deleted
2989 correctly. */
2990 terminal->delete_frame_hook = &tty_free_frame_resources;
2991 terminal->delete_terminal_hook = &delete_tty;
2994 /* Initialize hooks in TERMINAL with the values needed for a tty. */
2996 static void
2997 set_tty_hooks (struct terminal *terminal)
2999 terminal->rif = 0; /* ttys don't support window-based redisplay. */
3001 terminal->cursor_to_hook = &tty_cursor_to;
3002 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
3004 terminal->clear_to_end_hook = &tty_clear_to_end;
3005 terminal->clear_frame_hook = &tty_clear_frame;
3006 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
3008 terminal->ins_del_lines_hook = &tty_ins_del_lines;
3010 terminal->insert_glyphs_hook = &tty_insert_glyphs;
3011 terminal->write_glyphs_hook = &tty_write_glyphs;
3012 terminal->delete_glyphs_hook = &tty_delete_glyphs;
3014 terminal->ring_bell_hook = &tty_ring_bell;
3016 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
3017 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
3018 terminal->update_begin_hook = 0; /* Not needed. */
3019 terminal->update_end_hook = &tty_update_end;
3020 terminal->set_terminal_window_hook = &tty_set_terminal_window;
3022 terminal->mouse_position_hook = 0; /* Not needed. */
3023 terminal->frame_rehighlight_hook = 0; /* Not needed. */
3024 terminal->frame_raise_lower_hook = 0; /* Not needed. */
3026 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
3027 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
3028 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
3029 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
3031 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
3032 terminal->frame_up_to_date_hook = 0; /* Not needed. */
3034 terminal->delete_frame_hook = &tty_free_frame_resources;
3035 terminal->delete_terminal_hook = &delete_tty;
3038 /* Drop the controlling terminal if fd is the same device. */
3039 static void
3040 dissociate_if_controlling_tty (int fd)
3042 #ifndef DOS_NT
3043 int pgid = tcgetpgrp (fd); /* If tcgetpgrp succeeds, fd is the ctty. */
3044 if (pgid != -1)
3046 #if defined (USG5)
3047 setpgrp ();
3048 no_controlling_tty = 1;
3049 #elif defined (CYGWIN)
3050 setsid ();
3051 no_controlling_tty = 1;
3052 #else
3053 #ifdef TIOCNOTTY /* Try BSD ioctls. */
3054 sigblock (sigmask (SIGTTOU));
3055 fd = emacs_open (DEV_TTY, O_RDWR, 0);
3056 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
3058 no_controlling_tty = 1;
3060 if (fd != -1)
3061 emacs_close (fd);
3062 sigunblock (sigmask (SIGTTOU));
3063 #else
3064 /* Unknown system. */
3065 croak ();
3066 #endif /* ! TIOCNOTTY */
3067 #endif /* ! USG */
3069 #endif /* !DOS_NT */
3072 /* Create a termcap display on the tty device with the given name and
3073 type.
3075 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3076 Otherwise NAME should be a path to the tty device file,
3077 e.g. "/dev/pts/7".
3079 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3081 If MUST_SUCCEED is true, then all errors are fatal. */
3083 struct terminal *
3084 init_tty (const char *name, const char *terminal_type, int must_succeed)
3086 char *area = NULL;
3087 char **address = &area;
3088 int buffer_size = 4096;
3089 register char *p = NULL;
3090 int status;
3091 struct tty_display_info *tty = NULL;
3092 struct terminal *terminal = NULL;
3093 int ctty = 0; /* 1 if asked to open controlling tty. */
3095 if (!terminal_type)
3096 maybe_fatal (must_succeed, 0,
3097 "Unknown terminal type",
3098 "Unknown terminal type");
3100 if (name == NULL)
3101 name = DEV_TTY;
3102 if (!strcmp (name, DEV_TTY))
3103 ctty = 1;
3105 /* If we already have a terminal on the given device, use that. If
3106 all such terminals are suspended, create a new one instead. */
3107 /* XXX Perhaps this should be made explicit by having init_tty
3108 always create a new terminal and separating terminal and frame
3109 creation on Lisp level. */
3110 terminal = get_named_tty (name);
3111 if (terminal)
3112 return terminal;
3114 terminal = create_terminal ();
3115 #ifdef MSDOS
3116 if (been_here > 0)
3117 maybe_fatal (1, 0, "Attempt to create another terminal %s", "",
3118 name, "");
3119 been_here = 1;
3120 tty = &the_only_display_info;
3121 #else
3122 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
3123 #endif
3124 memset (tty, 0, sizeof (struct tty_display_info));
3125 tty->next = tty_list;
3126 tty_list = tty;
3128 terminal->type = output_termcap;
3129 terminal->display_info.tty = tty;
3130 tty->terminal = terminal;
3132 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3133 Wcm_clear (tty);
3135 encode_terminal_src_size = 0;
3136 encode_terminal_dst_size = 0;
3138 #ifdef HAVE_GPM
3139 terminal->mouse_position_hook = term_mouse_position;
3140 tty->mouse_highlight.mouse_face_window = Qnil;
3141 #endif
3144 #ifndef DOS_NT
3145 set_tty_hooks (terminal);
3148 int fd;
3149 FILE *file;
3151 #ifdef O_IGNORE_CTTY
3152 if (!ctty)
3153 /* Open the terminal device. Don't recognize it as our
3154 controlling terminal, and don't make it the controlling tty
3155 if we don't have one at the moment. */
3156 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
3157 else
3158 #else
3159 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3160 defined on Hurd. On other systems, we need to explicitly
3161 dissociate ourselves from the controlling tty when we want to
3162 open a frame on the same terminal. */
3163 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
3164 #endif /* O_IGNORE_CTTY */
3166 tty->name = xstrdup (name);
3167 terminal->name = xstrdup (name);
3169 if (fd < 0)
3170 maybe_fatal (must_succeed, terminal,
3171 "Could not open file: %s",
3172 "Could not open file: %s",
3173 name);
3174 if (!isatty (fd))
3176 close (fd);
3177 maybe_fatal (must_succeed, terminal,
3178 "Not a tty device: %s",
3179 "Not a tty device: %s",
3180 name);
3183 #ifndef O_IGNORE_CTTY
3184 if (!ctty)
3185 dissociate_if_controlling_tty (fd);
3186 #endif
3188 file = fdopen (fd, "w+");
3189 tty->input = file;
3190 tty->output = file;
3193 tty->type = xstrdup (terminal_type);
3195 add_keyboard_wait_descriptor (fileno (tty->input));
3197 Wcm_clear (tty);
3199 tty->termcap_term_buffer = (char *) xmalloc (buffer_size);
3201 /* On some systems, tgetent tries to access the controlling
3202 terminal. */
3203 sigblock (sigmask (SIGTTOU));
3204 status = tgetent (tty->termcap_term_buffer, terminal_type);
3205 sigunblock (sigmask (SIGTTOU));
3207 if (status < 0)
3209 #ifdef TERMINFO
3210 maybe_fatal (must_succeed, terminal,
3211 "Cannot open terminfo database file",
3212 "Cannot open terminfo database file");
3213 #else
3214 maybe_fatal (must_succeed, terminal,
3215 "Cannot open termcap database file",
3216 "Cannot open termcap database file");
3217 #endif
3219 if (status == 0)
3221 maybe_fatal (must_succeed, terminal,
3222 "Terminal type %s is not defined",
3223 "Terminal type %s is not defined.\n\
3224 If that is not the actual type of terminal you have,\n\
3225 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3226 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3227 #ifdef TERMINFO
3228 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3229 #else
3230 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3231 #endif
3232 terminal_type);
3235 #ifndef TERMINFO
3236 if (strlen (tty->termcap_term_buffer) >= buffer_size)
3237 abort ();
3238 buffer_size = strlen (tty->termcap_term_buffer);
3239 #endif
3240 tty->termcap_strings_buffer = area = (char *) xmalloc (buffer_size);
3241 tty->TS_ins_line = tgetstr ("al", address);
3242 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3243 tty->TS_bell = tgetstr ("bl", address);
3244 BackTab (tty) = tgetstr ("bt", address);
3245 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3246 tty->TS_clr_line = tgetstr ("ce", address);
3247 tty->TS_clr_frame = tgetstr ("cl", address);
3248 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3249 AbsPosition (tty) = tgetstr ("cm", address);
3250 CR (tty) = tgetstr ("cr", address);
3251 tty->TS_set_scroll_region = tgetstr ("cs", address);
3252 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
3253 RowPosition (tty) = tgetstr ("cv", address);
3254 tty->TS_del_char = tgetstr ("dc", address);
3255 tty->TS_del_multi_chars = tgetstr ("DC", address);
3256 tty->TS_del_line = tgetstr ("dl", address);
3257 tty->TS_del_multi_lines = tgetstr ("DL", address);
3258 tty->TS_delete_mode = tgetstr ("dm", address);
3259 tty->TS_end_delete_mode = tgetstr ("ed", address);
3260 tty->TS_end_insert_mode = tgetstr ("ei", address);
3261 Home (tty) = tgetstr ("ho", address);
3262 tty->TS_ins_char = tgetstr ("ic", address);
3263 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3264 tty->TS_insert_mode = tgetstr ("im", address);
3265 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3266 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3267 tty->TS_keypad_mode = tgetstr ("ks", address);
3268 LastLine (tty) = tgetstr ("ll", address);
3269 Right (tty) = tgetstr ("nd", address);
3270 Down (tty) = tgetstr ("do", address);
3271 if (!Down (tty))
3272 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
3273 if (tgetflag ("bs"))
3274 Left (tty) = "\b"; /* can't possibly be longer! */
3275 else /* (Actually, "bs" is obsolete...) */
3276 Left (tty) = tgetstr ("le", address);
3277 if (!Left (tty))
3278 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
3279 tty->TS_pad_char = tgetstr ("pc", address);
3280 tty->TS_repeat = tgetstr ("rp", address);
3281 tty->TS_end_standout_mode = tgetstr ("se", address);
3282 tty->TS_fwd_scroll = tgetstr ("sf", address);
3283 tty->TS_standout_mode = tgetstr ("so", address);
3284 tty->TS_rev_scroll = tgetstr ("sr", address);
3285 tty->Wcm->cm_tab = tgetstr ("ta", address);
3286 tty->TS_end_termcap_modes = tgetstr ("te", address);
3287 tty->TS_termcap_modes = tgetstr ("ti", address);
3288 Up (tty) = tgetstr ("up", address);
3289 tty->TS_visible_bell = tgetstr ("vb", address);
3290 tty->TS_cursor_normal = tgetstr ("ve", address);
3291 tty->TS_cursor_visible = tgetstr ("vs", address);
3292 tty->TS_cursor_invisible = tgetstr ("vi", address);
3293 tty->TS_set_window = tgetstr ("wi", address);
3295 tty->TS_enter_underline_mode = tgetstr ("us", address);
3296 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3297 tty->TS_enter_bold_mode = tgetstr ("md", address);
3298 tty->TS_enter_dim_mode = tgetstr ("mh", address);
3299 tty->TS_enter_blink_mode = tgetstr ("mb", address);
3300 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3301 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3302 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3303 tty->TS_exit_attribute_mode = tgetstr ("me", address);
3305 MultiUp (tty) = tgetstr ("UP", address);
3306 MultiDown (tty) = tgetstr ("DO", address);
3307 MultiLeft (tty) = tgetstr ("LE", address);
3308 MultiRight (tty) = tgetstr ("RI", address);
3310 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
3311 color because we can't switch back to the default foreground and
3312 background. */
3313 tty->TS_orig_pair = tgetstr ("op", address);
3314 if (tty->TS_orig_pair)
3316 tty->TS_set_foreground = tgetstr ("AF", address);
3317 tty->TS_set_background = tgetstr ("AB", address);
3318 if (!tty->TS_set_foreground)
3320 /* SVr4. */
3321 tty->TS_set_foreground = tgetstr ("Sf", address);
3322 tty->TS_set_background = tgetstr ("Sb", address);
3325 tty->TN_max_colors = tgetnum ("Co");
3326 tty->TN_max_pairs = tgetnum ("pa");
3328 tty->TN_no_color_video = tgetnum ("NC");
3329 if (tty->TN_no_color_video == -1)
3330 tty->TN_no_color_video = 0;
3333 tty_default_color_capabilities (tty, 1);
3335 MagicWrap (tty) = tgetflag ("xn");
3336 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3337 the former flag imply the latter. */
3338 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
3339 terminal->memory_below_frame = tgetflag ("db");
3340 tty->TF_hazeltine = tgetflag ("hz");
3341 terminal->must_write_spaces = tgetflag ("in");
3342 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3343 tty->TF_insmode_motion = tgetflag ("mi");
3344 tty->TF_standout_motion = tgetflag ("ms");
3345 tty->TF_underscore = tgetflag ("ul");
3346 tty->TF_teleray = tgetflag ("xt");
3348 #else /* DOS_NT */
3349 #ifdef WINDOWSNT
3351 struct frame *f = XFRAME (selected_frame);
3353 initialize_w32_display (terminal);
3355 FrameRows (tty) = FRAME_LINES (f);
3356 FrameCols (tty) = FRAME_COLS (f);
3357 tty->specified_window = FRAME_LINES (f);
3359 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3360 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
3361 terminal->char_ins_del_ok = 1;
3362 baud_rate = 19200;
3364 #else /* MSDOS */
3366 int height, width;
3367 if (strcmp (terminal_type, "internal") == 0)
3368 terminal->type = output_msdos_raw;
3369 initialize_msdos_display (terminal);
3371 get_tty_size (fileno (tty->input), &width, &height);
3372 FrameCols (tty) = width;
3373 FrameRows (tty) = height;
3374 terminal->char_ins_del_ok = 0;
3375 init_baud_rate (fileno (tty->input));
3377 #endif /* MSDOS */
3378 tty->output = stdout;
3379 tty->input = stdin;
3380 /* The following two are inaccessible from w32console.c. */
3381 terminal->delete_frame_hook = &tty_free_frame_resources;
3382 terminal->delete_terminal_hook = &delete_tty;
3384 tty->name = xstrdup (name);
3385 terminal->name = xstrdup (name);
3386 tty->type = xstrdup (terminal_type);
3388 add_keyboard_wait_descriptor (0);
3390 tty->delete_in_insert_mode = 1;
3392 UseTabs (tty) = 0;
3393 terminal->scroll_region_ok = 0;
3395 /* Seems to insert lines when it's not supposed to, messing up the
3396 display. In doing a trace, it didn't seem to be called much, so I
3397 don't think we're losing anything by turning it off. */
3398 terminal->line_ins_del_ok = 0;
3400 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
3401 #endif /* DOS_NT */
3403 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3404 init_kboard (terminal->kboard);
3405 KVAR (terminal->kboard, Vwindow_system) = Qnil;
3406 terminal->kboard->next_kboard = all_kboards;
3407 all_kboards = terminal->kboard;
3408 terminal->kboard->reference_count++;
3409 /* Don't let the initial kboard remain current longer than necessary.
3410 That would cause problems if a file loaded on startup tries to
3411 prompt in the mini-buffer. */
3412 if (current_kboard == initial_kboard)
3413 current_kboard = terminal->kboard;
3414 #ifndef DOS_NT
3415 term_get_fkeys (address, terminal->kboard);
3417 /* Get frame size from system, or else from termcap. */
3419 int height, width;
3420 get_tty_size (fileno (tty->input), &width, &height);
3421 FrameCols (tty) = width;
3422 FrameRows (tty) = height;
3425 if (FrameCols (tty) <= 0)
3426 FrameCols (tty) = tgetnum ("co");
3427 if (FrameRows (tty) <= 0)
3428 FrameRows (tty) = tgetnum ("li");
3430 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
3431 maybe_fatal (must_succeed, terminal,
3432 "Screen size %dx%d is too small",
3433 "Screen size %dx%d is too small",
3434 FrameCols (tty), FrameRows (tty));
3436 TabWidth (tty) = tgetnum ("tw");
3438 if (!tty->TS_bell)
3439 tty->TS_bell = "\07";
3441 if (!tty->TS_fwd_scroll)
3442 tty->TS_fwd_scroll = Down (tty);
3444 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
3446 if (TabWidth (tty) < 0)
3447 TabWidth (tty) = 8;
3449 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3450 and newer termcap doc does not seem to say there is a default.
3451 if (!tty->Wcm->cm_tab)
3452 tty->Wcm->cm_tab = "\t";
3455 /* We don't support standout modes that use `magic cookies', so
3456 turn off any that do. */
3457 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
3459 tty->TS_standout_mode = 0;
3460 tty->TS_end_standout_mode = 0;
3462 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
3464 tty->TS_enter_underline_mode = 0;
3465 tty->TS_exit_underline_mode = 0;
3468 /* If there's no standout mode, try to use underlining instead. */
3469 if (tty->TS_standout_mode == 0)
3471 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3472 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
3475 /* If no `se' string, try using a `me' string instead.
3476 If that fails, we can't use standout mode at all. */
3477 if (tty->TS_end_standout_mode == 0)
3479 char *s = tgetstr ("me", address);
3480 if (s != 0)
3481 tty->TS_end_standout_mode = s;
3482 else
3483 tty->TS_standout_mode = 0;
3486 if (tty->TF_teleray)
3488 tty->Wcm->cm_tab = 0;
3489 /* We can't support standout mode, because it uses magic cookies. */
3490 tty->TS_standout_mode = 0;
3491 /* But that means we cannot rely on ^M to go to column zero! */
3492 CR (tty) = 0;
3493 /* LF can't be trusted either -- can alter hpos */
3494 /* if move at column 0 thru a line with TS_standout_mode */
3495 Down (tty) = 0;
3498 /* Special handling for certain terminal types known to need it */
3500 if (!strcmp (terminal_type, "supdup"))
3502 terminal->memory_below_frame = 1;
3503 tty->Wcm->cm_losewrap = 1;
3505 if (!strncmp (terminal_type, "c10", 3)
3506 || !strcmp (terminal_type, "perq"))
3508 /* Supply a makeshift :wi string.
3509 This string is not valid in general since it works only
3510 for windows starting at the upper left corner;
3511 but that is all Emacs uses.
3513 This string works only if the frame is using
3514 the top of the video memory, because addressing is memory-relative.
3515 So first check the :ti string to see if that is true.
3517 It would be simpler if the :wi string could go in the termcap
3518 entry, but it can't because it is not fully valid.
3519 If it were in the termcap entry, it would confuse other programs. */
3520 if (!tty->TS_set_window)
3522 const char *m = tty->TS_termcap_modes;
3523 while (*m && strcmp (m, "\033v "))
3524 m++;
3525 if (*m)
3526 tty->TS_set_window = "\033v%C %C %C %C ";
3528 /* Termcap entry often fails to have :in: flag */
3529 terminal->must_write_spaces = 1;
3530 /* :ti string typically fails to have \E^G! in it */
3531 /* This limits scope of insert-char to one line. */
3532 strcpy (area, tty->TS_termcap_modes);
3533 strcat (area, "\033\007!");
3534 tty->TS_termcap_modes = area;
3535 area += strlen (area) + 1;
3536 p = AbsPosition (tty);
3537 /* Change all %+ parameters to %C, to handle
3538 values above 96 correctly for the C100. */
3539 while (*p)
3541 if (p[0] == '%' && p[1] == '+')
3542 p[1] = 'C';
3543 p++;
3547 tty->specified_window = FrameRows (tty);
3549 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3551 maybe_fatal (must_succeed, terminal,
3552 "Terminal type \"%s\" is not powerful enough to run Emacs",
3553 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3554 It lacks the ability to position the cursor.\n\
3555 If that is not the actual type of terminal you have,\n\
3556 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3557 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3558 # ifdef TERMINFO
3559 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3560 # else /* TERMCAP */
3561 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3562 # endif /* TERMINFO */
3563 terminal_type);
3566 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
3567 maybe_fatal (must_succeed, terminal,
3568 "Could not determine the frame size",
3569 "Could not determine the frame size");
3571 tty->delete_in_insert_mode
3572 = tty->TS_delete_mode && tty->TS_insert_mode
3573 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
3575 tty->se_is_so = (tty->TS_standout_mode
3576 && tty->TS_end_standout_mode
3577 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
3579 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
3581 terminal->scroll_region_ok
3582 = (tty->Wcm->cm_abs
3583 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
3585 terminal->line_ins_del_ok
3586 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3587 && (tty->TS_del_line || tty->TS_del_multi_lines))
3588 || (terminal->scroll_region_ok
3589 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
3591 terminal->char_ins_del_ok
3592 = ((tty->TS_ins_char || tty->TS_insert_mode
3593 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3594 && (tty->TS_del_char || tty->TS_del_multi_chars));
3596 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
3598 init_baud_rate (fileno (tty->input));
3600 #endif /* not DOS_NT */
3602 /* Init system terminal modes (RAW or CBREAK, etc.). */
3603 init_sys_modes (tty);
3605 return terminal;
3609 static void
3610 vfatal (const char *str, va_list ap)
3612 fprintf (stderr, "emacs: ");
3613 vfprintf (stderr, str, ap);
3614 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
3615 fprintf (stderr, "\n");
3616 va_end (ap);
3617 fflush (stderr);
3618 exit (1);
3622 /* Auxiliary error-handling function for init_tty.
3623 Delete TERMINAL, then call error or fatal with str1 or str2,
3624 respectively, according to MUST_SUCCEED. */
3626 static void
3627 maybe_fatal (int must_succeed, struct terminal *terminal,
3628 const char *str1, const char *str2, ...)
3630 va_list ap;
3631 va_start (ap, str2);
3632 if (terminal)
3633 delete_tty (terminal);
3635 if (must_succeed)
3636 vfatal (str2, ap);
3637 else
3638 verror (str1, ap);
3640 va_end (ap);
3641 abort ();
3644 void
3645 fatal (const char *str, ...)
3647 va_list ap;
3648 va_start (ap, str);
3649 vfatal (str, ap);
3650 va_end (ap);
3655 /* Delete the given tty terminal, closing all frames on it. */
3657 static void
3658 delete_tty (struct terminal *terminal)
3660 struct tty_display_info *tty;
3662 /* Protect against recursive calls. delete_frame in
3663 delete_terminal calls us back when it deletes our last frame. */
3664 if (!terminal->name)
3665 return;
3667 if (terminal->type != output_termcap)
3668 abort ();
3670 tty = terminal->display_info.tty;
3672 if (tty == tty_list)
3673 tty_list = tty->next;
3674 else
3676 struct tty_display_info *p;
3677 for (p = tty_list; p && p->next != tty; p = p->next)
3680 if (! p)
3681 /* This should not happen. */
3682 abort ();
3684 p->next = tty->next;
3685 tty->next = 0;
3688 /* reset_sys_modes needs a valid device, so this call needs to be
3689 before delete_terminal. */
3690 reset_sys_modes (tty);
3692 delete_terminal (terminal);
3694 xfree (tty->name);
3695 xfree (tty->type);
3697 if (tty->input)
3699 delete_keyboard_wait_descriptor (fileno (tty->input));
3700 if (tty->input != stdin)
3701 fclose (tty->input);
3703 if (tty->output && tty->output != stdout && tty->output != tty->input)
3704 fclose (tty->output);
3705 if (tty->termscript)
3706 fclose (tty->termscript);
3708 xfree (tty->old_tty);
3709 xfree (tty->Wcm);
3710 xfree (tty->termcap_strings_buffer);
3711 xfree (tty->termcap_term_buffer);
3713 memset (tty, 0, sizeof (struct tty_display_info));
3714 xfree (tty);
3719 /* Mark the pointers in the tty_display_info objects.
3720 Called by the Fgarbage_collector. */
3722 void
3723 mark_ttys (void)
3725 struct tty_display_info *tty;
3727 for (tty = tty_list; tty; tty = tty->next)
3728 mark_object (tty->top_frame);
3733 void
3734 syms_of_term (void)
3736 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo,
3737 doc: /* Non-nil means the system uses terminfo rather than termcap.
3738 This variable can be used by terminal emulator packages. */);
3739 #ifdef TERMINFO
3740 system_uses_terminfo = 1;
3741 #else
3742 system_uses_terminfo = 0;
3743 #endif
3745 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions,
3746 doc: /* Functions to be run after suspending a tty.
3747 The functions are run with one argument, the terminal object to be suspended.
3748 See `suspend-tty'. */);
3749 Vsuspend_tty_functions = Qnil;
3752 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions,
3753 doc: /* Functions to be run after resuming a tty.
3754 The functions are run with one argument, the terminal object that was revived.
3755 See `resume-tty'. */);
3756 Vresume_tty_functions = Qnil;
3758 DEFVAR_BOOL ("visible-cursor", visible_cursor,
3759 doc: /* Non-nil means to make the cursor very visible.
3760 This only has an effect when running in a text terminal.
3761 What means \"very visible\" is up to your terminal. It may make the cursor
3762 bigger, or it may make it blink, or it may do nothing at all. */);
3763 visible_cursor = 1;
3765 defsubr (&Stty_display_color_p);
3766 defsubr (&Stty_display_color_cells);
3767 defsubr (&Stty_no_underline);
3768 defsubr (&Stty_type);
3769 defsubr (&Scontrolling_tty_p);
3770 defsubr (&Ssuspend_tty);
3771 defsubr (&Sresume_tty);
3772 #ifdef HAVE_GPM
3773 defsubr (&Sgpm_mouse_start);
3774 defsubr (&Sgpm_mouse_stop);
3775 #endif /* HAVE_GPM */
3777 #ifndef DOS_NT
3778 default_orig_pair = NULL;
3779 default_set_foreground = NULL;
3780 default_set_background = NULL;
3781 #endif /* !DOS_NT */
3783 encode_terminal_src = NULL;
3784 encode_terminal_dst = NULL;