Merge from emacs-23; up to 2010-06-12T17:12:15Z!cyd@stupidchicken.com.
[emacs.git] / src / term.c
blob9205719b5f4c3b198694bebf65a770202be5c7be
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985-1987, 1993-1995, 1998, 2000-2011
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <sys/file.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <setjmp.h>
31 #include "lisp.h"
32 #include "termchar.h"
33 #include "termopts.h"
34 #include "tparam.h"
35 #include "buffer.h"
36 #include "character.h"
37 #include "charset.h"
38 #include "coding.h"
39 #include "composite.h"
40 #include "keyboard.h"
41 #include "frame.h"
42 #include "disptab.h"
43 #include "termhooks.h"
44 #include "dispextern.h"
45 #include "window.h"
46 #include "keymap.h"
47 #include "blockinput.h"
48 #include "syssignal.h"
49 #include "systty.h"
50 #include "intervals.h"
51 #ifdef MSDOS
52 #include "msdos.h"
53 static int been_here = -1;
54 #endif
56 #include "cm.h"
57 #ifdef HAVE_X_WINDOWS
58 #include "xterm.h"
59 #endif
61 #ifndef O_RDWR
62 #define O_RDWR 2
63 #endif
65 #ifndef O_NOCTTY
66 #define O_NOCTTY 0
67 #endif
69 /* The name of the default console device. */
70 #ifdef WINDOWSNT
71 #define DEV_TTY "CONOUT$"
72 #else
73 #define DEV_TTY "/dev/tty"
74 #endif
76 static void tty_set_scroll_region (struct frame *f, int start, int stop);
77 static void turn_on_face (struct frame *, int face_id);
78 static void turn_off_face (struct frame *, int face_id);
79 static void tty_turn_off_highlight (struct tty_display_info *);
80 static void tty_show_cursor (struct tty_display_info *);
81 static void tty_hide_cursor (struct tty_display_info *);
82 static void tty_background_highlight (struct tty_display_info *tty);
83 static struct terminal *get_tty_terminal (Lisp_Object, int);
84 static void clear_tty_hooks (struct terminal *terminal);
85 static void set_tty_hooks (struct terminal *terminal);
86 static void dissociate_if_controlling_tty (int fd);
87 static void delete_tty (struct terminal *);
88 static void maybe_fatal (int must_succeed, struct terminal *terminal,
89 const char *str1, const char *str2, ...)
90 NO_RETURN ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
91 static void vfatal (const char *str, va_list ap)
92 NO_RETURN ATTRIBUTE_FORMAT_PRINTF (1, 0);
95 #define OUTPUT(tty, a) \
96 emacs_tputs ((tty), a, \
97 (int) (FRAME_LINES (XFRAME (selected_frame)) \
98 - curY (tty)), \
99 cmputc)
101 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
102 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
104 #define OUTPUT_IF(tty, a) \
105 do { \
106 if (a) \
107 OUTPUT (tty, a); \
108 } while (0)
110 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
112 /* Display space properties */
114 /* Chain of all tty device parameters. */
115 struct tty_display_info *tty_list;
117 /* Meaning of bits in no_color_video. Each bit set means that the
118 corresponding attribute cannot be combined with colors. */
120 enum no_color_bit
122 NC_STANDOUT = 1 << 0,
123 NC_UNDERLINE = 1 << 1,
124 NC_REVERSE = 1 << 2,
125 NC_BLINK = 1 << 3,
126 NC_DIM = 1 << 4,
127 NC_BOLD = 1 << 5,
128 NC_INVIS = 1 << 6,
129 NC_PROTECT = 1 << 7,
130 NC_ALT_CHARSET = 1 << 8
133 /* internal state */
135 /* The largest frame width in any call to calculate_costs. */
137 static int max_frame_cols;
139 /* The largest frame height in any call to calculate_costs. */
141 static int max_frame_lines;
143 /* Non-zero if we have dropped our controlling tty and therefore
144 should not open a frame on stdout. */
145 static int no_controlling_tty;
149 #ifdef HAVE_GPM
150 #include <sys/fcntl.h>
152 /* The device for which we have enabled gpm support (or NULL). */
153 struct tty_display_info *gpm_tty = NULL;
155 /* Last recorded mouse coordinates. */
156 static int last_mouse_x, last_mouse_y;
157 #endif /* HAVE_GPM */
159 /* Ring the bell on a tty. */
161 static void
162 tty_ring_bell (struct frame *f)
164 struct tty_display_info *tty = FRAME_TTY (f);
166 if (tty->output)
168 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
169 ? tty->TS_visible_bell
170 : tty->TS_bell));
171 fflush (tty->output);
175 /* Set up termcap modes for Emacs. */
177 static void
178 tty_set_terminal_modes (struct terminal *terminal)
180 struct tty_display_info *tty = terminal->display_info.tty;
182 if (tty->output)
184 if (tty->TS_termcap_modes)
185 OUTPUT (tty, tty->TS_termcap_modes);
186 else
188 /* Output enough newlines to scroll all the old screen contents
189 off the screen, so it won't be overwritten and lost. */
190 int i;
191 current_tty = tty;
192 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
193 cmputc ('\n');
196 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
197 OUTPUT_IF (tty, tty->TS_keypad_mode);
198 losecursor (tty);
199 fflush (tty->output);
203 /* Reset termcap modes before exiting Emacs. */
205 static void
206 tty_reset_terminal_modes (struct terminal *terminal)
208 struct tty_display_info *tty = terminal->display_info.tty;
210 if (tty->output)
212 tty_turn_off_highlight (tty);
213 tty_turn_off_insert (tty);
214 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
215 OUTPUT_IF (tty, tty->TS_cursor_normal);
216 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
217 OUTPUT_IF (tty, tty->TS_orig_pair);
218 /* Output raw CR so kernel can track the cursor hpos. */
219 current_tty = tty;
220 cmputc ('\r');
221 fflush (tty->output);
225 /* Flag the end of a display update on a termcap terminal. */
227 static void
228 tty_update_end (struct frame *f)
230 struct tty_display_info *tty = FRAME_TTY (f);
232 if (!XWINDOW (selected_window)->cursor_off_p)
233 tty_show_cursor (tty);
234 tty_turn_off_insert (tty);
235 tty_background_highlight (tty);
238 /* The implementation of set_terminal_window for termcap frames. */
240 static void
241 tty_set_terminal_window (struct frame *f, int size)
243 struct tty_display_info *tty = FRAME_TTY (f);
245 tty->specified_window = size ? size : FRAME_LINES (f);
246 if (FRAME_SCROLL_REGION_OK (f))
247 tty_set_scroll_region (f, 0, tty->specified_window);
250 static void
251 tty_set_scroll_region (struct frame *f, int start, int stop)
253 char *buf;
254 struct tty_display_info *tty = FRAME_TTY (f);
256 if (tty->TS_set_scroll_region)
257 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1, 0, 0);
258 else if (tty->TS_set_scroll_region_1)
259 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
260 FRAME_LINES (f), start,
261 FRAME_LINES (f) - stop,
262 FRAME_LINES (f));
263 else
264 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
266 OUTPUT (tty, buf);
267 xfree (buf);
268 losecursor (tty);
272 static void
273 tty_turn_on_insert (struct tty_display_info *tty)
275 if (!tty->insert_mode)
276 OUTPUT (tty, tty->TS_insert_mode);
277 tty->insert_mode = 1;
280 void
281 tty_turn_off_insert (struct tty_display_info *tty)
283 if (tty->insert_mode)
284 OUTPUT (tty, tty->TS_end_insert_mode);
285 tty->insert_mode = 0;
288 /* Handle highlighting. */
290 static void
291 tty_turn_off_highlight (struct tty_display_info *tty)
293 if (tty->standout_mode)
294 OUTPUT_IF (tty, tty->TS_end_standout_mode);
295 tty->standout_mode = 0;
298 static void
299 tty_turn_on_highlight (struct tty_display_info *tty)
301 if (!tty->standout_mode)
302 OUTPUT_IF (tty, tty->TS_standout_mode);
303 tty->standout_mode = 1;
306 static void
307 tty_toggle_highlight (struct tty_display_info *tty)
309 if (tty->standout_mode)
310 tty_turn_off_highlight (tty);
311 else
312 tty_turn_on_highlight (tty);
316 /* Make cursor invisible. */
318 static void
319 tty_hide_cursor (struct tty_display_info *tty)
321 if (tty->cursor_hidden == 0)
323 tty->cursor_hidden = 1;
324 OUTPUT_IF (tty, tty->TS_cursor_invisible);
329 /* Ensure that cursor is visible. */
331 static void
332 tty_show_cursor (struct tty_display_info *tty)
334 if (tty->cursor_hidden)
336 tty->cursor_hidden = 0;
337 OUTPUT_IF (tty, tty->TS_cursor_normal);
338 if (visible_cursor)
339 OUTPUT_IF (tty, tty->TS_cursor_visible);
344 /* Set standout mode to the state it should be in for
345 empty space inside windows. What this is,
346 depends on the user option inverse-video. */
348 static void
349 tty_background_highlight (struct tty_display_info *tty)
351 if (inverse_video)
352 tty_turn_on_highlight (tty);
353 else
354 tty_turn_off_highlight (tty);
357 /* Set standout mode to the mode specified for the text to be output. */
359 static void
360 tty_highlight_if_desired (struct tty_display_info *tty)
362 if (inverse_video)
363 tty_turn_on_highlight (tty);
364 else
365 tty_turn_off_highlight (tty);
369 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
370 frame-relative coordinates. */
372 static void
373 tty_cursor_to (struct frame *f, int vpos, int hpos)
375 struct tty_display_info *tty = FRAME_TTY (f);
377 /* Detect the case where we are called from reset_sys_modes
378 and the costs have never been calculated. Do nothing. */
379 if (! tty->costs_set)
380 return;
382 if (curY (tty) == vpos
383 && curX (tty) == hpos)
384 return;
385 if (!tty->TF_standout_motion)
386 tty_background_highlight (tty);
387 if (!tty->TF_insmode_motion)
388 tty_turn_off_insert (tty);
389 cmgoto (tty, vpos, hpos);
392 /* Similar but don't take any account of the wasted characters. */
394 static void
395 tty_raw_cursor_to (struct frame *f, int row, int col)
397 struct tty_display_info *tty = FRAME_TTY (f);
399 if (curY (tty) == row
400 && curX (tty) == col)
401 return;
402 if (!tty->TF_standout_motion)
403 tty_background_highlight (tty);
404 if (!tty->TF_insmode_motion)
405 tty_turn_off_insert (tty);
406 cmgoto (tty, row, col);
409 /* Erase operations */
411 /* Clear from cursor to end of frame on a termcap device. */
413 static void
414 tty_clear_to_end (struct frame *f)
416 register int i;
417 struct tty_display_info *tty = FRAME_TTY (f);
419 if (tty->TS_clr_to_bottom)
421 tty_background_highlight (tty);
422 OUTPUT (tty, tty->TS_clr_to_bottom);
424 else
426 for (i = curY (tty); i < FRAME_LINES (f); i++)
428 cursor_to (f, i, 0);
429 clear_end_of_line (f, FRAME_COLS (f));
434 /* Clear an entire termcap frame. */
436 static void
437 tty_clear_frame (struct frame *f)
439 struct tty_display_info *tty = FRAME_TTY (f);
441 if (tty->TS_clr_frame)
443 tty_background_highlight (tty);
444 OUTPUT (tty, tty->TS_clr_frame);
445 cmat (tty, 0, 0);
447 else
449 cursor_to (f, 0, 0);
450 clear_to_end (f);
454 /* An implementation of clear_end_of_line for termcap frames.
456 Note that the cursor may be moved, on terminals lacking a `ce' string. */
458 static void
459 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
461 register int i;
462 struct tty_display_info *tty = FRAME_TTY (f);
464 /* Detect the case where we are called from reset_sys_modes
465 and the costs have never been calculated. Do nothing. */
466 if (! tty->costs_set)
467 return;
469 if (curX (tty) >= first_unused_hpos)
470 return;
471 tty_background_highlight (tty);
472 if (tty->TS_clr_line)
474 OUTPUT1 (tty, tty->TS_clr_line);
476 else
477 { /* have to do it the hard way */
478 tty_turn_off_insert (tty);
480 /* Do not write in last row last col with Auto-wrap on. */
481 if (AutoWrap (tty)
482 && curY (tty) == FrameRows (tty) - 1
483 && first_unused_hpos == FrameCols (tty))
484 first_unused_hpos--;
486 for (i = curX (tty); i < first_unused_hpos; i++)
488 if (tty->termscript)
489 fputc (' ', tty->termscript);
490 fputc (' ', tty->output);
492 cmplus (tty, first_unused_hpos - curX (tty));
496 /* Buffers to store the source and result of code conversion for terminal. */
497 static unsigned char *encode_terminal_src;
498 static unsigned char *encode_terminal_dst;
499 /* Allocated sizes of the above buffers. */
500 static int encode_terminal_src_size;
501 static int encode_terminal_dst_size;
503 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
504 Set CODING->produced to the byte-length of the resulting byte
505 sequence, and return a pointer to that byte sequence. */
507 unsigned char *
508 encode_terminal_code (struct glyph *src, int src_len, struct coding_system *coding)
510 struct glyph *src_end = src + src_len;
511 unsigned char *buf;
512 int nchars, nbytes, required;
513 register int tlen = GLYPH_TABLE_LENGTH;
514 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
515 Lisp_Object charset_list;
517 /* Allocate sufficient size of buffer to store all characters in
518 multibyte-form. But, it may be enlarged on demand if
519 Vglyph_table contains a string or a composite glyph is
520 encountered. */
521 required = MAX_MULTIBYTE_LENGTH * src_len;
522 if (encode_terminal_src_size < required)
524 if (encode_terminal_src)
525 encode_terminal_src = xrealloc (encode_terminal_src, required);
526 else
527 encode_terminal_src = xmalloc (required);
528 encode_terminal_src_size = required;
531 charset_list = coding_charset_list (coding);
533 buf = encode_terminal_src;
534 nchars = 0;
535 while (src < src_end)
537 if (src->type == COMPOSITE_GLYPH)
539 struct composition *cmp IF_LINT (= NULL);
540 Lisp_Object gstring IF_LINT (= Qnil);
541 int i;
543 nbytes = buf - encode_terminal_src;
544 if (src->u.cmp.automatic)
546 gstring = composition_gstring_from_id (src->u.cmp.id);
547 required = src->slice.cmp.to + 1 - src->slice.cmp.from;
549 else
551 cmp = composition_table[src->u.cmp.id];
552 required = MAX_MULTIBYTE_LENGTH * cmp->glyph_len;
555 if (encode_terminal_src_size < nbytes + required)
557 encode_terminal_src_size = nbytes + required;
558 encode_terminal_src = xrealloc (encode_terminal_src,
559 encode_terminal_src_size);
560 buf = encode_terminal_src + nbytes;
563 if (src->u.cmp.automatic)
564 for (i = src->slice.cmp.from; i <= src->slice.cmp.to; i++)
566 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
567 int c = LGLYPH_CHAR (g);
569 if (! char_charset (c, charset_list, NULL))
570 c = '?';
571 buf += CHAR_STRING (c, buf);
572 nchars++;
574 else
575 for (i = 0; i < cmp->glyph_len; i++)
577 int c = COMPOSITION_GLYPH (cmp, i);
579 if (c == '\t')
580 continue;
581 if (char_charset (c, charset_list, NULL))
583 if (CHAR_WIDTH (c) == 0
584 && i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
585 /* Should be left-padded */
587 buf += CHAR_STRING (' ', buf);
588 nchars++;
591 else
592 c = '?';
593 buf += CHAR_STRING (c, buf);
594 nchars++;
597 /* We must skip glyphs to be padded for a wide character. */
598 else if (! CHAR_GLYPH_PADDING_P (*src))
600 GLYPH g;
601 int c IF_LINT (= 0);
602 Lisp_Object string;
604 string = Qnil;
605 SET_GLYPH_FROM_CHAR_GLYPH (g, src[0]);
607 if (GLYPH_INVALID_P (g) || GLYPH_SIMPLE_P (tbase, tlen, g))
609 /* This glyph doesn't have an entry in Vglyph_table. */
610 c = src->u.ch;
612 else
614 /* This glyph has an entry in Vglyph_table,
615 so process any alias before testing for simpleness. */
616 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
618 if (GLYPH_SIMPLE_P (tbase, tlen, g))
619 /* We set the multi-byte form of a character in G
620 (that should be an ASCII character) at WORKBUF. */
621 c = GLYPH_CHAR (g);
622 else
623 /* We have a string in Vglyph_table. */
624 string = tbase[GLYPH_CHAR (g)];
627 if (NILP (string))
629 nbytes = buf - encode_terminal_src;
630 if (encode_terminal_src_size < nbytes + MAX_MULTIBYTE_LENGTH)
632 encode_terminal_src_size = nbytes + MAX_MULTIBYTE_LENGTH;
633 encode_terminal_src = xrealloc (encode_terminal_src,
634 encode_terminal_src_size);
635 buf = encode_terminal_src + nbytes;
637 if (CHAR_BYTE8_P (c)
638 || char_charset (c, charset_list, NULL))
640 /* Store the multibyte form of C at BUF. */
641 buf += CHAR_STRING (c, buf);
642 nchars++;
644 else
646 /* C is not encodable. */
647 *buf++ = '?';
648 nchars++;
649 while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
651 *buf++ = '?';
652 nchars++;
653 src++;
657 else
659 if (! STRING_MULTIBYTE (string))
660 string = string_to_multibyte (string);
661 nbytes = buf - encode_terminal_src;
662 if (encode_terminal_src_size < nbytes + SBYTES (string))
664 encode_terminal_src_size = nbytes + SBYTES (string);
665 encode_terminal_src = xrealloc (encode_terminal_src,
666 encode_terminal_src_size);
667 buf = encode_terminal_src + nbytes;
669 memcpy (buf, SDATA (string), SBYTES (string));
670 buf += SBYTES (string);
671 nchars += SCHARS (string);
674 src++;
677 if (nchars == 0)
679 coding->produced = 0;
680 return NULL;
683 nbytes = buf - encode_terminal_src;
684 coding->source = encode_terminal_src;
685 if (encode_terminal_dst_size == 0)
687 encode_terminal_dst_size = encode_terminal_src_size;
688 if (encode_terminal_dst)
689 encode_terminal_dst = xrealloc (encode_terminal_dst,
690 encode_terminal_dst_size);
691 else
692 encode_terminal_dst = xmalloc (encode_terminal_dst_size);
694 coding->destination = encode_terminal_dst;
695 coding->dst_bytes = encode_terminal_dst_size;
696 encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
697 /* coding->destination may have been reallocated. */
698 encode_terminal_dst = coding->destination;
699 encode_terminal_dst_size = coding->dst_bytes;
701 return (encode_terminal_dst);
706 /* An implementation of write_glyphs for termcap frames. */
708 static void
709 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
711 unsigned char *conversion_buffer;
712 struct coding_system *coding;
713 size_t n, stringlen;
715 struct tty_display_info *tty = FRAME_TTY (f);
717 tty_turn_off_insert (tty);
718 tty_hide_cursor (tty);
720 /* Don't dare write in last column of bottom line, if Auto-Wrap,
721 since that would scroll the whole frame on some terminals. */
723 if (AutoWrap (tty)
724 && curY (tty) + 1 == FRAME_LINES (f)
725 && (curX (tty) + len) == FRAME_COLS (f))
726 len --;
727 if (len <= 0)
728 return;
730 cmplus (tty, len);
732 /* If terminal_coding does any conversion, use it, otherwise use
733 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
734 because it always return 1 if the member src_multibyte is 1. */
735 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
736 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
737 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
738 the tail. */
739 coding->mode &= ~CODING_MODE_LAST_BLOCK;
741 for (stringlen = len; stringlen != 0; stringlen -= n)
743 /* Identify a run of glyphs with the same face. */
744 int face_id = string->face_id;
746 for (n = 1; n < stringlen; ++n)
747 if (string[n].face_id != face_id)
748 break;
750 /* Turn appearance modes of the face of the run on. */
751 tty_highlight_if_desired (tty);
752 turn_on_face (f, face_id);
754 if (n == stringlen)
755 /* This is the last run. */
756 coding->mode |= CODING_MODE_LAST_BLOCK;
757 conversion_buffer = encode_terminal_code (string, n, coding);
758 if (coding->produced > 0)
760 BLOCK_INPUT;
761 fwrite (conversion_buffer, 1, coding->produced, tty->output);
762 if (ferror (tty->output))
763 clearerr (tty->output);
764 if (tty->termscript)
765 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
766 UNBLOCK_INPUT;
768 string += n;
770 /* Turn appearance modes off. */
771 turn_off_face (f, face_id);
772 tty_turn_off_highlight (tty);
775 cmcheckmagic (tty);
778 #ifdef HAVE_GPM /* Only used by GPM code. */
780 static void
781 tty_write_glyphs_with_face (register struct frame *f, register struct glyph *string,
782 register int len, register int face_id)
784 unsigned char *conversion_buffer;
785 struct coding_system *coding;
787 struct tty_display_info *tty = FRAME_TTY (f);
789 tty_turn_off_insert (tty);
790 tty_hide_cursor (tty);
792 /* Don't dare write in last column of bottom line, if Auto-Wrap,
793 since that would scroll the whole frame on some terminals. */
795 if (AutoWrap (tty)
796 && curY (tty) + 1 == FRAME_LINES (f)
797 && (curX (tty) + len) == FRAME_COLS (f))
798 len --;
799 if (len <= 0)
800 return;
802 cmplus (tty, len);
804 /* If terminal_coding does any conversion, use it, otherwise use
805 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
806 because it always return 1 if the member src_multibyte is 1. */
807 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
808 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
809 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
810 the tail. */
811 coding->mode &= ~CODING_MODE_LAST_BLOCK;
813 /* Turn appearance modes of the face. */
814 tty_highlight_if_desired (tty);
815 turn_on_face (f, face_id);
817 coding->mode |= CODING_MODE_LAST_BLOCK;
818 conversion_buffer = encode_terminal_code (string, len, coding);
819 if (coding->produced > 0)
821 BLOCK_INPUT;
822 fwrite (conversion_buffer, 1, coding->produced, tty->output);
823 if (ferror (tty->output))
824 clearerr (tty->output);
825 if (tty->termscript)
826 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
827 UNBLOCK_INPUT;
830 /* Turn appearance modes off. */
831 turn_off_face (f, face_id);
832 tty_turn_off_highlight (tty);
834 cmcheckmagic (tty);
836 #endif
838 /* An implementation of insert_glyphs for termcap frames. */
840 static void
841 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
843 char *buf;
844 struct glyph *glyph = NULL;
845 unsigned char *conversion_buffer;
846 unsigned char space[1];
847 struct coding_system *coding;
849 struct tty_display_info *tty = FRAME_TTY (f);
851 if (tty->TS_ins_multi_chars)
853 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len, 0, 0, 0);
854 OUTPUT1 (tty, buf);
855 xfree (buf);
856 if (start)
857 write_glyphs (f, start, len);
858 return;
861 tty_turn_on_insert (tty);
862 cmplus (tty, len);
864 if (! start)
865 space[0] = SPACEGLYPH;
867 /* If terminal_coding does any conversion, use it, otherwise use
868 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
869 because it always return 1 if the member src_multibyte is 1. */
870 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
871 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
872 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
873 the tail. */
874 coding->mode &= ~CODING_MODE_LAST_BLOCK;
876 while (len-- > 0)
878 OUTPUT1_IF (tty, tty->TS_ins_char);
879 if (!start)
881 conversion_buffer = space;
882 coding->produced = 1;
884 else
886 tty_highlight_if_desired (tty);
887 turn_on_face (f, start->face_id);
888 glyph = start;
889 ++start;
890 /* We must open sufficient space for a character which
891 occupies more than one column. */
892 while (len && CHAR_GLYPH_PADDING_P (*start))
894 OUTPUT1_IF (tty, tty->TS_ins_char);
895 start++, len--;
898 if (len <= 0)
899 /* This is the last glyph. */
900 coding->mode |= CODING_MODE_LAST_BLOCK;
902 conversion_buffer = encode_terminal_code (glyph, 1, coding);
905 if (coding->produced > 0)
907 BLOCK_INPUT;
908 fwrite (conversion_buffer, 1, coding->produced, tty->output);
909 if (ferror (tty->output))
910 clearerr (tty->output);
911 if (tty->termscript)
912 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
913 UNBLOCK_INPUT;
916 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
917 if (start)
919 turn_off_face (f, glyph->face_id);
920 tty_turn_off_highlight (tty);
924 cmcheckmagic (tty);
927 /* An implementation of delete_glyphs for termcap frames. */
929 static void
930 tty_delete_glyphs (struct frame *f, int n)
932 char *buf;
933 register int i;
935 struct tty_display_info *tty = FRAME_TTY (f);
937 if (tty->delete_in_insert_mode)
939 tty_turn_on_insert (tty);
941 else
943 tty_turn_off_insert (tty);
944 OUTPUT_IF (tty, tty->TS_delete_mode);
947 if (tty->TS_del_multi_chars)
949 buf = tparam (tty->TS_del_multi_chars, 0, 0, n, 0, 0, 0);
950 OUTPUT1 (tty, buf);
951 xfree (buf);
953 else
954 for (i = 0; i < n; i++)
955 OUTPUT1 (tty, tty->TS_del_char);
956 if (!tty->delete_in_insert_mode)
957 OUTPUT_IF (tty, tty->TS_end_delete_mode);
960 /* An implementation of ins_del_lines for termcap frames. */
962 static void
963 tty_ins_del_lines (struct frame *f, int vpos, int n)
965 struct tty_display_info *tty = FRAME_TTY (f);
966 const char *multi =
967 n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
968 const char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
969 const char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
971 register int i = n > 0 ? n : -n;
972 register char *buf;
974 /* If the lines below the insertion are being pushed
975 into the end of the window, this is the same as clearing;
976 and we know the lines are already clear, since the matching
977 deletion has already been done. So can ignore this. */
978 /* If the lines below the deletion are blank lines coming
979 out of the end of the window, don't bother,
980 as there will be a matching inslines later that will flush them. */
981 if (FRAME_SCROLL_REGION_OK (f)
982 && vpos + i >= tty->specified_window)
983 return;
984 if (!FRAME_MEMORY_BELOW_FRAME (f)
985 && vpos + i >= FRAME_LINES (f))
986 return;
988 if (multi)
990 raw_cursor_to (f, vpos, 0);
991 tty_background_highlight (tty);
992 buf = tparam (multi, 0, 0, i, 0, 0, 0);
993 OUTPUT (tty, buf);
994 xfree (buf);
996 else if (single)
998 raw_cursor_to (f, vpos, 0);
999 tty_background_highlight (tty);
1000 while (--i >= 0)
1001 OUTPUT (tty, single);
1002 if (tty->TF_teleray)
1003 curX (tty) = 0;
1005 else
1007 tty_set_scroll_region (f, vpos, tty->specified_window);
1008 if (n < 0)
1009 raw_cursor_to (f, tty->specified_window - 1, 0);
1010 else
1011 raw_cursor_to (f, vpos, 0);
1012 tty_background_highlight (tty);
1013 while (--i >= 0)
1014 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1015 tty_set_scroll_region (f, 0, tty->specified_window);
1018 if (!FRAME_SCROLL_REGION_OK (f)
1019 && FRAME_MEMORY_BELOW_FRAME (f)
1020 && n < 0)
1022 cursor_to (f, FRAME_LINES (f) + n, 0);
1023 clear_to_end (f);
1027 /* Compute cost of sending "str", in characters,
1028 not counting any line-dependent padding. */
1031 string_cost (const char *str)
1033 cost = 0;
1034 if (str)
1035 tputs (str, 0, evalcost);
1036 return cost;
1039 /* Compute cost of sending "str", in characters,
1040 counting any line-dependent padding at one line. */
1042 static int
1043 string_cost_one_line (const char *str)
1045 cost = 0;
1046 if (str)
1047 tputs (str, 1, evalcost);
1048 return cost;
1051 /* Compute per line amount of line-dependent padding,
1052 in tenths of characters. */
1055 per_line_cost (const char *str)
1057 cost = 0;
1058 if (str)
1059 tputs (str, 0, evalcost);
1060 cost = - cost;
1061 if (str)
1062 tputs (str, 10, evalcost);
1063 return cost;
1066 /* char_ins_del_cost[n] is cost of inserting N characters.
1067 char_ins_del_cost[-n] is cost of deleting N characters.
1068 The length of this vector is based on max_frame_cols. */
1070 int *char_ins_del_vector;
1072 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1074 /* ARGSUSED */
1075 static void
1076 calculate_ins_del_char_costs (struct frame *f)
1078 struct tty_display_info *tty = FRAME_TTY (f);
1079 int ins_startup_cost, del_startup_cost;
1080 int ins_cost_per_char, del_cost_per_char;
1081 register int i;
1082 register int *p;
1084 if (tty->TS_ins_multi_chars)
1086 ins_cost_per_char = 0;
1087 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1089 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1090 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1092 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1093 + string_cost (tty->TS_end_insert_mode))) / 100;
1094 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1095 + string_cost_one_line (tty->TS_pad_inserted_char));
1097 else
1099 ins_startup_cost = 9999;
1100 ins_cost_per_char = 0;
1103 if (tty->TS_del_multi_chars)
1105 del_cost_per_char = 0;
1106 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1108 else if (tty->TS_del_char)
1110 del_startup_cost = (string_cost (tty->TS_delete_mode)
1111 + string_cost (tty->TS_end_delete_mode));
1112 if (tty->delete_in_insert_mode)
1113 del_startup_cost /= 2;
1114 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1116 else
1118 del_startup_cost = 9999;
1119 del_cost_per_char = 0;
1122 /* Delete costs are at negative offsets */
1123 p = &char_ins_del_cost (f)[0];
1124 for (i = FRAME_COLS (f); --i >= 0;)
1125 *--p = (del_startup_cost += del_cost_per_char);
1127 /* Doing nothing is free */
1128 p = &char_ins_del_cost (f)[0];
1129 *p++ = 0;
1131 /* Insert costs are at positive offsets */
1132 for (i = FRAME_COLS (f); --i >= 0;)
1133 *p++ = (ins_startup_cost += ins_cost_per_char);
1136 void
1137 calculate_costs (struct frame *frame)
1139 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1141 if (FRAME_TERMCAP_P (frame))
1143 struct tty_display_info *tty = FRAME_TTY (frame);
1144 register const char *f = (tty->TS_set_scroll_region
1145 ? tty->TS_set_scroll_region
1146 : tty->TS_set_scroll_region_1);
1148 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1150 tty->costs_set = 1;
1152 /* These variables are only used for terminal stuff. They are
1153 allocated once for the terminal frame of X-windows emacs, but not
1154 used afterwards.
1156 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1157 X turns off char_ins_del_ok. */
1159 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1160 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1162 if (char_ins_del_vector != 0)
1163 char_ins_del_vector
1164 = (int *) xrealloc (char_ins_del_vector,
1165 (sizeof (int)
1166 + 2 * max_frame_cols * sizeof (int)));
1167 else
1168 char_ins_del_vector
1169 = (int *) xmalloc (sizeof (int)
1170 + 2 * max_frame_cols * sizeof (int));
1172 memset (char_ins_del_vector, 0,
1173 (sizeof (int) + 2 * max_frame_cols * sizeof (int)));
1176 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1177 do_line_insertion_deletion_costs (frame,
1178 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1179 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1180 f, f, 1);
1181 else
1182 do_line_insertion_deletion_costs (frame,
1183 tty->TS_ins_line, tty->TS_ins_multi_lines,
1184 tty->TS_del_line, tty->TS_del_multi_lines,
1185 0, 0, 1);
1187 calculate_ins_del_char_costs (frame);
1189 /* Don't use TS_repeat if its padding is worse than sending the chars */
1190 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1191 tty->RPov = string_cost (tty->TS_repeat);
1192 else
1193 tty->RPov = FRAME_COLS (frame) * 2;
1195 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1199 struct fkey_table {
1200 const char *cap, *name;
1203 /* Termcap capability names that correspond directly to X keysyms.
1204 Some of these (marked "terminfo") aren't supplied by old-style
1205 (Berkeley) termcap entries. They're listed in X keysym order;
1206 except we put the keypad keys first, so that if they clash with
1207 other keys (as on the IBM PC keyboard) they get overridden.
1210 static const struct fkey_table keys[] =
1212 {"kh", "home"}, /* termcap */
1213 {"kl", "left"}, /* termcap */
1214 {"ku", "up"}, /* termcap */
1215 {"kr", "right"}, /* termcap */
1216 {"kd", "down"}, /* termcap */
1217 {"%8", "prior"}, /* terminfo */
1218 {"%5", "next"}, /* terminfo */
1219 {"@7", "end"}, /* terminfo */
1220 {"@1", "begin"}, /* terminfo */
1221 {"*6", "select"}, /* terminfo */
1222 {"%9", "print"}, /* terminfo */
1223 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1225 * "insert" --- see below
1227 {"&8", "undo"}, /* terminfo */
1228 {"%0", "redo"}, /* terminfo */
1229 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1230 {"@0", "find"}, /* terminfo */
1231 {"@2", "cancel"}, /* terminfo */
1232 {"%1", "help"}, /* terminfo */
1234 * "break" goes here, but can't be reliably intercepted with termcap
1236 {"&4", "reset"}, /* terminfo --- actually `restart' */
1238 * "system" and "user" --- no termcaps
1240 {"kE", "clearline"}, /* terminfo */
1241 {"kA", "insertline"}, /* terminfo */
1242 {"kL", "deleteline"}, /* terminfo */
1243 {"kI", "insertchar"}, /* terminfo */
1244 {"kD", "deletechar"}, /* terminfo */
1245 {"kB", "backtab"}, /* terminfo */
1247 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1249 {"@8", "kp-enter"}, /* terminfo */
1251 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1252 * "kp-multiply", "kp-add", "kp-separator",
1253 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1254 * --- no termcaps for any of these.
1256 {"K4", "kp-1"}, /* terminfo */
1258 * "kp-2" --- no termcap
1260 {"K5", "kp-3"}, /* terminfo */
1262 * "kp-4" --- no termcap
1264 {"K2", "kp-5"}, /* terminfo */
1266 * "kp-6" --- no termcap
1268 {"K1", "kp-7"}, /* terminfo */
1270 * "kp-8" --- no termcap
1272 {"K3", "kp-9"}, /* terminfo */
1274 * "kp-equal" --- no termcap
1276 {"k1", "f1"},
1277 {"k2", "f2"},
1278 {"k3", "f3"},
1279 {"k4", "f4"},
1280 {"k5", "f5"},
1281 {"k6", "f6"},
1282 {"k7", "f7"},
1283 {"k8", "f8"},
1284 {"k9", "f9"},
1286 {"&0", "S-cancel"}, /*shifted cancel key*/
1287 {"&9", "S-begin"}, /*shifted begin key*/
1288 {"*0", "S-find"}, /*shifted find key*/
1289 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1290 {"*4", "S-delete"}, /*shifted delete-character key*/
1291 {"*7", "S-end"}, /*shifted end key*/
1292 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1293 {"#1", "S-help"}, /*shifted help key*/
1294 {"#2", "S-home"}, /*shifted home key*/
1295 {"#3", "S-insert"}, /*shifted insert-character key*/
1296 {"#4", "S-left"}, /*shifted left-arrow key*/
1297 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1298 {"%c", "S-next"}, /*shifted next key*/
1299 {"%e", "S-prior"}, /*shifted previous key*/
1300 {"%f", "S-print"}, /*shifted print key*/
1301 {"%g", "S-redo"}, /*shifted redo key*/
1302 {"%i", "S-right"}, /*shifted right-arrow key*/
1303 {"!3", "S-undo"} /*shifted undo key*/
1306 #ifndef DOS_NT
1307 static char **term_get_fkeys_address;
1308 static KBOARD *term_get_fkeys_kboard;
1309 static Lisp_Object term_get_fkeys_1 (void);
1311 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1312 This function scans the termcap function key sequence entries, and
1313 adds entries to Vinput_decode_map for each function key it finds. */
1315 static void
1316 term_get_fkeys (char **address, KBOARD *kboard)
1318 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1319 errors during the call. The only errors should be from Fdefine_key
1320 when given a key sequence containing an invalid prefix key. If the
1321 termcap defines function keys which use a prefix that is already bound
1322 to a command by the default bindings, we should silently ignore that
1323 function key specification, rather than giving the user an error and
1324 refusing to run at all on such a terminal. */
1326 term_get_fkeys_address = address;
1327 term_get_fkeys_kboard = kboard;
1328 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1331 static Lisp_Object
1332 term_get_fkeys_1 (void)
1334 int i;
1336 char **address = term_get_fkeys_address;
1337 KBOARD *kboard = term_get_fkeys_kboard;
1339 /* This can happen if CANNOT_DUMP or with strange options. */
1340 if (!KEYMAPP (KVAR (kboard, Vinput_decode_map)))
1341 KVAR (kboard, Vinput_decode_map) = Fmake_sparse_keymap (Qnil);
1343 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1345 char *sequence = tgetstr (keys[i].cap, address);
1346 if (sequence)
1347 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence),
1348 Fmake_vector (make_number (1),
1349 intern (keys[i].name)));
1352 /* The uses of the "k0" capability are inconsistent; sometimes it
1353 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1354 We will attempt to politely accommodate both systems by testing for
1355 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1358 const char *k_semi = tgetstr ("k;", address);
1359 const char *k0 = tgetstr ("k0", address);
1360 const char *k0_name = "f10";
1362 if (k_semi)
1364 if (k0)
1365 /* Define f0 first, so that f10 takes precedence in case the
1366 key sequences happens to be the same. */
1367 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k0),
1368 Fmake_vector (make_number (1), intern ("f0")));
1369 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k_semi),
1370 Fmake_vector (make_number (1), intern ("f10")));
1372 else if (k0)
1373 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k0),
1374 Fmake_vector (make_number (1), intern (k0_name)));
1377 /* Set up cookies for numbered function keys above f10. */
1379 char fcap[3], fkey[4];
1381 fcap[0] = 'F'; fcap[2] = '\0';
1382 for (i = 11; i < 64; i++)
1384 if (i <= 19)
1385 fcap[1] = '1' + i - 11;
1386 else if (i <= 45)
1387 fcap[1] = 'A' + i - 20;
1388 else
1389 fcap[1] = 'a' + i - 46;
1392 char *sequence = tgetstr (fcap, address);
1393 if (sequence)
1395 sprintf (fkey, "f%d", i);
1396 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence),
1397 Fmake_vector (make_number (1),
1398 intern (fkey)));
1405 * Various mappings to try and get a better fit.
1408 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1409 if (!tgetstr (cap1, address)) \
1411 char *sequence = tgetstr (cap2, address); \
1412 if (sequence) \
1413 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence), \
1414 Fmake_vector (make_number (1), \
1415 intern (sym))); \
1418 /* if there's no key_next keycap, map key_npage to `next' keysym */
1419 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1420 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1421 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1422 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1423 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1424 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1425 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1427 /* IBM has their own non-standard dialect of terminfo.
1428 If the standard name isn't found, try the IBM name. */
1429 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1430 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1431 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1432 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1433 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1434 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1435 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1436 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1437 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1438 #undef CONDITIONAL_REASSIGN
1441 return Qnil;
1443 #endif /* not DOS_NT */
1446 /***********************************************************************
1447 Character Display Information
1448 ***********************************************************************/
1449 static void append_glyph (struct it *);
1450 static void produce_stretch_glyph (struct it *);
1451 static void append_composite_glyph (struct it *);
1452 static void produce_composite_glyph (struct it *);
1453 static void append_glyphless_glyph (struct it *, int, const char *);
1454 static void produce_glyphless_glyph (struct it *, int, Lisp_Object);
1456 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1457 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1458 the character for which to produce glyphs; IT->face_id contains the
1459 character's face. Padding glyphs are appended if IT->c has a
1460 IT->pixel_width > 1. */
1462 static void
1463 append_glyph (struct it *it)
1465 struct glyph *glyph, *end;
1466 int i;
1468 xassert (it->glyph_row);
1469 glyph = (it->glyph_row->glyphs[it->area]
1470 + it->glyph_row->used[it->area]);
1471 end = it->glyph_row->glyphs[1 + it->area];
1473 /* If the glyph row is reversed, we need to prepend the glyph rather
1474 than append it. */
1475 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1477 struct glyph *g;
1478 int move_by = it->pixel_width;
1480 /* Make room for the new glyphs. */
1481 if (move_by > end - glyph) /* don't overstep end of this area */
1482 move_by = end - glyph;
1483 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1484 g[move_by] = *g;
1485 glyph = it->glyph_row->glyphs[it->area];
1486 end = glyph + move_by;
1489 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1490 right, even in the REVERSED_P case, since (a) all of its u.ch are
1491 identical, and (b) the PADDING_P flag needs to be set for the
1492 leftmost one, because we write to the terminal left-to-right. */
1493 for (i = 0;
1494 i < it->pixel_width && glyph < end;
1495 ++i)
1497 glyph->type = CHAR_GLYPH;
1498 glyph->pixel_width = 1;
1499 glyph->u.ch = it->char_to_display;
1500 glyph->face_id = it->face_id;
1501 glyph->padding_p = i > 0;
1502 glyph->charpos = CHARPOS (it->position);
1503 glyph->object = it->object;
1504 if (it->bidi_p)
1506 glyph->resolved_level = it->bidi_it.resolved_level;
1507 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1508 abort ();
1509 glyph->bidi_type = it->bidi_it.type;
1511 else
1513 glyph->resolved_level = 0;
1514 glyph->bidi_type = UNKNOWN_BT;
1517 ++it->glyph_row->used[it->area];
1518 ++glyph;
1522 /* Produce glyphs for the display element described by IT. *IT
1523 specifies what we want to produce a glyph for (character, image, ...),
1524 and where in the glyph matrix we currently are (glyph row and hpos).
1525 produce_glyphs fills in output fields of *IT with information such as the
1526 pixel width and height of a character, and maybe output actual glyphs at
1527 the same time if IT->glyph_row is non-null. For an overview, see
1528 the explanation in dispextern.h, before the definition of the
1529 display_element_type enumeration.
1531 produce_glyphs also stores the result of glyph width, ascent
1532 etc. computations in *IT.
1534 IT->glyph_row may be null, in which case produce_glyphs does not
1535 actually fill in the glyphs. This is used in the move_* functions
1536 in xdisp.c for text width and height computations.
1538 Callers usually don't call produce_glyphs directly;
1539 instead they use the macro PRODUCE_GLYPHS. */
1541 void
1542 produce_glyphs (struct it *it)
1544 /* If a hook is installed, let it do the work. */
1546 /* Nothing but characters are supported on terminal frames. */
1547 xassert (it->what == IT_CHARACTER
1548 || it->what == IT_COMPOSITION
1549 || it->what == IT_STRETCH);
1551 if (it->what == IT_STRETCH)
1553 produce_stretch_glyph (it);
1554 goto done;
1557 if (it->what == IT_COMPOSITION)
1559 produce_composite_glyph (it);
1560 goto done;
1563 if (it->what == IT_GLYPHLESS)
1565 produce_glyphless_glyph (it, 0, Qnil);
1566 goto done;
1569 if (it->char_to_display >= 040 && it->char_to_display < 0177)
1571 it->pixel_width = it->nglyphs = 1;
1572 if (it->glyph_row)
1573 append_glyph (it);
1575 else if (it->char_to_display == '\n')
1576 it->pixel_width = it->nglyphs = 0;
1577 else if (it->char_to_display == '\t')
1579 int absolute_x = (it->current_x
1580 + it->continuation_lines_width);
1581 int next_tab_x
1582 = (((1 + absolute_x + it->tab_width - 1)
1583 / it->tab_width)
1584 * it->tab_width);
1585 int nspaces;
1587 /* If part of the TAB has been displayed on the previous line
1588 which is continued now, continuation_lines_width will have
1589 been incremented already by the part that fitted on the
1590 continued line. So, we will get the right number of spaces
1591 here. */
1592 nspaces = next_tab_x - absolute_x;
1594 if (it->glyph_row)
1596 int n = nspaces;
1598 it->char_to_display = ' ';
1599 it->pixel_width = it->len = 1;
1601 while (n--)
1602 append_glyph (it);
1605 it->pixel_width = nspaces;
1606 it->nglyphs = nspaces;
1608 else if (CHAR_BYTE8_P (it->char_to_display))
1610 /* Coming here means that we must send the raw 8-bit byte as is
1611 to the terminal. Although there's no way to know how many
1612 columns it occupies on a screen, it is a good assumption that
1613 a single byte code has 1-column width. */
1614 it->pixel_width = it->nglyphs = 1;
1615 if (it->glyph_row)
1616 append_glyph (it);
1618 else
1620 Lisp_Object charset_list = FRAME_TERMINAL (it->f)->charset_list;
1622 if (char_charset (it->char_to_display, charset_list, NULL))
1624 it->pixel_width = CHAR_WIDTH (it->char_to_display);
1625 it->nglyphs = it->pixel_width;
1626 if (it->glyph_row)
1627 append_glyph (it);
1629 else
1631 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
1633 xassert (it->what == IT_GLYPHLESS);
1634 produce_glyphless_glyph (it, 1, acronym);
1638 done:
1639 /* Advance current_x by the pixel width as a convenience for
1640 the caller. */
1641 if (it->area == TEXT_AREA)
1642 it->current_x += it->pixel_width;
1643 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1644 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1648 /* Produce a stretch glyph for iterator IT. IT->object is the value
1649 of the glyph property displayed. The value must be a list
1650 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1651 being recognized:
1653 1. `:width WIDTH' specifies that the space should be WIDTH *
1654 canonical char width wide. WIDTH may be an integer or floating
1655 point number.
1657 2. `:align-to HPOS' specifies that the space should be wide enough
1658 to reach HPOS, a value in canonical character units. */
1660 static void
1661 produce_stretch_glyph (struct it *it)
1663 /* (space :width WIDTH ...) */
1664 Lisp_Object prop, plist;
1665 int width = 0, align_to = -1;
1666 int zero_width_ok_p = 0;
1667 double tem;
1669 /* List should start with `space'. */
1670 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1671 plist = XCDR (it->object);
1673 /* Compute the width of the stretch. */
1674 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1675 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1677 /* Absolute width `:width WIDTH' specified and valid. */
1678 zero_width_ok_p = 1;
1679 width = (int)(tem + 0.5);
1681 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1682 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1684 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1685 align_to = (align_to < 0
1687 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1688 else if (align_to < 0)
1689 align_to = window_box_left_offset (it->w, TEXT_AREA);
1690 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1691 zero_width_ok_p = 1;
1693 else
1694 /* Nothing specified -> width defaults to canonical char width. */
1695 width = FRAME_COLUMN_WIDTH (it->f);
1697 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1698 width = 1;
1700 if (width > 0 && it->line_wrap != TRUNCATE
1701 && it->current_x + width > it->last_visible_x)
1702 width = it->last_visible_x - it->current_x - 1;
1704 if (width > 0 && it->glyph_row)
1706 Lisp_Object o_object = it->object;
1707 Lisp_Object object = it->stack[it->sp - 1].string;
1708 int n = width;
1710 if (!STRINGP (object))
1711 object = it->w->buffer;
1712 it->object = object;
1713 it->char_to_display = ' ';
1714 it->pixel_width = it->len = 1;
1715 while (n--)
1716 append_glyph (it);
1717 it->object = o_object;
1719 it->pixel_width = width;
1720 it->nglyphs = width;
1724 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1725 Called from produce_composite_glyph for terminal frames if
1726 IT->glyph_row != NULL. IT->face_id contains the character's
1727 face. */
1729 static void
1730 append_composite_glyph (struct it *it)
1732 struct glyph *glyph;
1734 xassert (it->glyph_row);
1735 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1736 if (glyph < it->glyph_row->glyphs[1 + it->area])
1738 /* If the glyph row is reversed, we need to prepend the glyph
1739 rather than append it. */
1740 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1742 struct glyph *g;
1744 /* Make room for the new glyph. */
1745 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1746 g[1] = *g;
1747 glyph = it->glyph_row->glyphs[it->area];
1749 glyph->type = COMPOSITE_GLYPH;
1750 glyph->pixel_width = it->pixel_width;
1751 glyph->u.cmp.id = it->cmp_it.id;
1752 if (it->cmp_it.ch < 0)
1754 glyph->u.cmp.automatic = 0;
1755 glyph->u.cmp.id = it->cmp_it.id;
1757 else
1759 glyph->u.cmp.automatic = 1;
1760 glyph->u.cmp.id = it->cmp_it.id;
1761 glyph->slice.cmp.from = it->cmp_it.from;
1762 glyph->slice.cmp.to = it->cmp_it.to - 1;
1765 glyph->face_id = it->face_id;
1766 glyph->padding_p = 0;
1767 glyph->charpos = CHARPOS (it->position);
1768 glyph->object = it->object;
1769 if (it->bidi_p)
1771 glyph->resolved_level = it->bidi_it.resolved_level;
1772 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1773 abort ();
1774 glyph->bidi_type = it->bidi_it.type;
1776 else
1778 glyph->resolved_level = 0;
1779 glyph->bidi_type = UNKNOWN_BT;
1782 ++it->glyph_row->used[it->area];
1783 ++glyph;
1788 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1789 the composition. We simply produces components of the composition
1790 assuming that the terminal has a capability to layout/render it
1791 correctly. */
1793 static void
1794 produce_composite_glyph (struct it *it)
1796 if (it->cmp_it.ch < 0)
1798 struct composition *cmp = composition_table[it->cmp_it.id];
1800 it->pixel_width = cmp->width;
1802 else
1804 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
1806 it->pixel_width = composition_gstring_width (gstring, it->cmp_it.from,
1807 it->cmp_it.to, NULL);
1809 it->nglyphs = 1;
1810 if (it->glyph_row)
1811 append_composite_glyph (it);
1815 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
1816 is a face ID to be used for the glyph. What is actually appended
1817 are glyphs of type CHAR_GLYPH whose characters are in STR (which
1818 comes from it->nglyphs bytes). */
1820 static void
1821 append_glyphless_glyph (struct it *it, int face_id, const char *str)
1823 struct glyph *glyph, *end;
1824 int i;
1826 xassert (it->glyph_row);
1827 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1828 end = it->glyph_row->glyphs[1 + it->area];
1830 /* If the glyph row is reversed, we need to prepend the glyph rather
1831 than append it. */
1832 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1834 struct glyph *g;
1835 int move_by = it->pixel_width;
1837 /* Make room for the new glyphs. */
1838 if (move_by > end - glyph) /* don't overstep end of this area */
1839 move_by = end - glyph;
1840 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1841 g[move_by] = *g;
1842 glyph = it->glyph_row->glyphs[it->area];
1843 end = glyph + move_by;
1846 if (glyph >= end)
1847 return;
1848 glyph->type = CHAR_GLYPH;
1849 glyph->pixel_width = 1;
1850 glyph->face_id = face_id;
1851 glyph->padding_p = 0;
1852 glyph->charpos = CHARPOS (it->position);
1853 glyph->object = it->object;
1854 if (it->bidi_p)
1856 glyph->resolved_level = it->bidi_it.resolved_level;
1857 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1858 abort ();
1859 glyph->bidi_type = it->bidi_it.type;
1861 else
1863 glyph->resolved_level = 0;
1864 glyph->bidi_type = UNKNOWN_BT;
1867 /* BIDI Note: we put the glyphs of characters left to right, even in
1868 the REVERSED_P case because we write to the terminal
1869 left-to-right. */
1870 for (i = 0; i < it->nglyphs && glyph < end; ++i)
1872 if (i > 0)
1873 glyph[0] = glyph[-1];
1874 glyph->u.ch = str[i];
1875 ++it->glyph_row->used[it->area];
1876 ++glyph;
1880 /* Produce glyphs for a glyphless character for iterator IT.
1881 IT->glyphless_method specifies which method to use for displaying
1882 the character. See the description of enum
1883 glyphless_display_method in dispextern.h for the details.
1885 FOR_NO_FONT is nonzero if and only if this is for a character that
1886 is not supproted by the coding system of the terminal. ACRONYM, if
1887 non-nil, is an acronym string for the character.
1889 The glyphs actually produced are of type CHAR_GLYPH. */
1891 static void
1892 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
1894 int face_id;
1895 int len;
1896 char buf[9];
1897 char const *str = " ";
1899 /* Get a face ID for the glyph by utilizing a cache (the same way as
1900 done for `escape-glyph' in get_next_display_element). */
1901 if (it->f == last_glyphless_glyph_frame
1902 && it->face_id == last_glyphless_glyph_face_id)
1904 face_id = last_glyphless_glyph_merged_face_id;
1906 else
1908 /* Merge the `glyphless-char' face into the current face. */
1909 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
1910 last_glyphless_glyph_frame = it->f;
1911 last_glyphless_glyph_face_id = it->face_id;
1912 last_glyphless_glyph_merged_face_id = face_id;
1915 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
1917 /* As there's no way to produce a thin space, we produce a space
1918 of canonical width. */
1919 len = 1;
1921 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
1923 len = CHAR_WIDTH (it->c);
1924 if (len == 0)
1925 len = 1;
1926 else if (len > 4)
1927 len = 4;
1928 sprintf (buf, "[%.*s]", len, str);
1929 len += 2;
1930 str = buf;
1932 else
1934 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
1936 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
1937 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
1938 if (CONSP (acronym))
1939 acronym = XCDR (acronym);
1940 buf[0] = '[';
1941 str = STRINGP (acronym) ? SSDATA (acronym) : "";
1942 for (len = 0; len < 6 && str[len] && ASCII_BYTE_P (str[len]); len++)
1943 buf[1 + len] = str[len];
1944 buf[1 + len] = ']';
1945 len += 2;
1947 else
1949 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
1950 len = (it->c < 0x10000 ? sprintf (buf, "\\u%04X", it->c)
1951 : it->c <= MAX_UNICODE_CHAR ? sprintf (buf, "\\U%06X", it->c)
1952 : sprintf (buf, "\\x%06X", it->c));
1954 str = buf;
1957 it->pixel_width = len;
1958 it->nglyphs = len;
1959 if (it->glyph_row)
1960 append_glyphless_glyph (it, face_id, str);
1964 /* Get information about special display element WHAT in an
1965 environment described by IT. WHAT is one of IT_TRUNCATION or
1966 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1967 non-null glyph_row member. This function ensures that fields like
1968 face_id, c, len of IT are left untouched. */
1970 void
1971 produce_special_glyphs (struct it *it, enum display_element_type what)
1973 struct it temp_it;
1974 Lisp_Object gc;
1975 GLYPH glyph;
1977 temp_it = *it;
1978 temp_it.dp = NULL;
1979 temp_it.what = IT_CHARACTER;
1980 temp_it.len = 1;
1981 temp_it.object = make_number (0);
1982 memset (&temp_it.current, 0, sizeof temp_it.current);
1984 if (what == IT_CONTINUATION)
1986 /* Continuation glyph. For R2L lines, we mirror it by hand. */
1987 if (it->bidi_it.paragraph_dir == R2L)
1988 SET_GLYPH_FROM_CHAR (glyph, '/');
1989 else
1990 SET_GLYPH_FROM_CHAR (glyph, '\\');
1991 if (it->dp
1992 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc))
1993 && GLYPH_CODE_CHAR_VALID_P (gc))
1995 /* FIXME: Should we mirror GC for R2L lines? */
1996 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1997 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
2000 else if (what == IT_TRUNCATION)
2002 /* Truncation glyph. */
2003 SET_GLYPH_FROM_CHAR (glyph, '$');
2004 if (it->dp
2005 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc))
2006 && GLYPH_CODE_CHAR_VALID_P (gc))
2008 /* FIXME: Should we mirror GC for R2L lines? */
2009 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
2010 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
2013 else
2014 abort ();
2016 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
2017 temp_it.face_id = GLYPH_FACE (glyph);
2018 temp_it.len = CHAR_BYTES (temp_it.c);
2020 produce_glyphs (&temp_it);
2021 it->pixel_width = temp_it.pixel_width;
2022 it->nglyphs = temp_it.pixel_width;
2027 /***********************************************************************
2028 Faces
2029 ***********************************************************************/
2031 /* Value is non-zero if attribute ATTR may be used. ATTR should be
2032 one of the enumerators from enum no_color_bit, or a bit set built
2033 from them. Some display attributes may not be used together with
2034 color; the termcap capability `NC' specifies which ones. */
2036 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
2037 (tty->TN_max_colors > 0 \
2038 ? (tty->TN_no_color_video & (ATTR)) == 0 \
2039 : 1)
2041 /* Turn appearances of face FACE_ID on tty frame F on.
2042 FACE_ID is a realized face ID number, in the face cache. */
2044 static void
2045 turn_on_face (struct frame *f, int face_id)
2047 struct face *face = FACE_FROM_ID (f, face_id);
2048 long fg = face->foreground;
2049 long bg = face->background;
2050 struct tty_display_info *tty = FRAME_TTY (f);
2052 /* Do this first because TS_end_standout_mode may be the same
2053 as TS_exit_attribute_mode, which turns all appearances off. */
2054 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
2056 if (tty->TN_max_colors > 0)
2058 if (fg >= 0 && bg >= 0)
2060 /* If the terminal supports colors, we can set them
2061 below without using reverse video. The face's fg
2062 and bg colors are set as they should appear on
2063 the screen, i.e. they take the inverse-video'ness
2064 of the face already into account. */
2066 else if (inverse_video)
2068 if (fg == FACE_TTY_DEFAULT_FG_COLOR
2069 || bg == FACE_TTY_DEFAULT_BG_COLOR)
2070 tty_toggle_highlight (tty);
2072 else
2074 if (fg == FACE_TTY_DEFAULT_BG_COLOR
2075 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2076 tty_toggle_highlight (tty);
2079 else
2081 /* If we can't display colors, use reverse video
2082 if the face specifies that. */
2083 if (inverse_video)
2085 if (fg == FACE_TTY_DEFAULT_FG_COLOR
2086 || bg == FACE_TTY_DEFAULT_BG_COLOR)
2087 tty_toggle_highlight (tty);
2089 else
2091 if (fg == FACE_TTY_DEFAULT_BG_COLOR
2092 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2093 tty_toggle_highlight (tty);
2098 if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
2099 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
2101 if (face->tty_dim_p && MAY_USE_WITH_COLORS_P (tty, NC_DIM))
2102 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
2104 /* Alternate charset and blinking not yet used. */
2105 if (face->tty_alt_charset_p
2106 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
2107 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
2109 if (face->tty_blinking_p
2110 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
2111 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
2113 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
2114 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
2116 if (tty->TN_max_colors > 0)
2118 const char *ts;
2119 char *p;
2121 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
2122 if (fg >= 0 && ts)
2124 p = tparam (ts, NULL, 0, (int) fg, 0, 0, 0);
2125 OUTPUT (tty, p);
2126 xfree (p);
2129 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
2130 if (bg >= 0 && ts)
2132 p = tparam (ts, NULL, 0, (int) bg, 0, 0, 0);
2133 OUTPUT (tty, p);
2134 xfree (p);
2140 /* Turn off appearances of face FACE_ID on tty frame F. */
2142 static void
2143 turn_off_face (struct frame *f, int face_id)
2145 struct face *face = FACE_FROM_ID (f, face_id);
2146 struct tty_display_info *tty = FRAME_TTY (f);
2148 xassert (face != NULL);
2150 if (tty->TS_exit_attribute_mode)
2152 /* Capability "me" will turn off appearance modes double-bright,
2153 half-bright, reverse-video, standout, underline. It may or
2154 may not turn off alt-char-mode. */
2155 if (face->tty_bold_p
2156 || face->tty_dim_p
2157 || face->tty_reverse_p
2158 || face->tty_alt_charset_p
2159 || face->tty_blinking_p
2160 || face->tty_underline_p)
2162 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
2163 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
2164 tty->standout_mode = 0;
2167 if (face->tty_alt_charset_p)
2168 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2170 else
2172 /* If we don't have "me" we can only have those appearances
2173 that have exit sequences defined. */
2174 if (face->tty_alt_charset_p)
2175 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2177 if (face->tty_underline_p)
2178 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
2181 /* Switch back to default colors. */
2182 if (tty->TN_max_colors > 0
2183 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2184 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2185 || (face->background != FACE_TTY_DEFAULT_COLOR
2186 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
2187 OUTPUT1_IF (tty, tty->TS_orig_pair);
2191 /* Return non-zero if the terminal on frame F supports all of the
2192 capabilities in CAPS simultaneously, with foreground and background
2193 colors FG and BG. */
2196 tty_capable_p (struct tty_display_info *tty, unsigned int caps,
2197 unsigned long fg, unsigned long bg)
2199 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2200 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2201 return 0;
2203 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2204 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2205 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2206 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
2207 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
2208 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
2210 /* We can do it! */
2211 return 1;
2214 /* Return non-zero if the terminal is capable to display colors. */
2216 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2217 0, 1, 0,
2218 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2220 TERMINAL can be a terminal object, a frame, or nil (meaning the
2221 selected frame's terminal). This function always returns nil if
2222 TERMINAL does not refer to a text-only terminal. */)
2223 (Lisp_Object terminal)
2225 struct terminal *t = get_tty_terminal (terminal, 0);
2226 if (!t)
2227 return Qnil;
2228 else
2229 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
2232 /* Return the number of supported colors. */
2233 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2234 Stty_display_color_cells, 0, 1, 0,
2235 doc: /* Return the number of colors supported by the tty device TERMINAL.
2237 TERMINAL can be a terminal object, a frame, or nil (meaning the
2238 selected frame's terminal). This function always returns 0 if
2239 TERMINAL does not refer to a text-only terminal. */)
2240 (Lisp_Object terminal)
2242 struct terminal *t = get_tty_terminal (terminal, 0);
2243 if (!t)
2244 return make_number (0);
2245 else
2246 return make_number (t->display_info.tty->TN_max_colors);
2249 #ifndef DOS_NT
2251 /* Declare here rather than in the function, as in the rest of Emacs,
2252 to work around an HPUX compiler bug (?). See
2253 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2254 static int default_max_colors;
2255 static int default_max_pairs;
2256 static int default_no_color_video;
2257 static char *default_orig_pair;
2258 static char *default_set_foreground;
2259 static char *default_set_background;
2261 /* Save or restore the default color-related capabilities of this
2262 terminal. */
2263 static void
2264 tty_default_color_capabilities (struct tty_display_info *tty, int save)
2267 if (save)
2269 xfree (default_orig_pair);
2270 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
2272 xfree (default_set_foreground);
2273 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
2274 : NULL;
2276 xfree (default_set_background);
2277 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
2278 : NULL;
2280 default_max_colors = tty->TN_max_colors;
2281 default_max_pairs = tty->TN_max_pairs;
2282 default_no_color_video = tty->TN_no_color_video;
2284 else
2286 tty->TS_orig_pair = default_orig_pair;
2287 tty->TS_set_foreground = default_set_foreground;
2288 tty->TS_set_background = default_set_background;
2289 tty->TN_max_colors = default_max_colors;
2290 tty->TN_max_pairs = default_max_pairs;
2291 tty->TN_no_color_video = default_no_color_video;
2295 /* Setup one of the standard tty color schemes according to MODE.
2296 MODE's value is generally the number of colors which we want to
2297 support; zero means set up for the default capabilities, the ones
2298 we saw at init_tty time; -1 means turn off color support. */
2299 static void
2300 tty_setup_colors (struct tty_display_info *tty, int mode)
2302 /* Canonicalize all negative values of MODE. */
2303 if (mode < -1)
2304 mode = -1;
2306 switch (mode)
2308 case -1: /* no colors at all */
2309 tty->TN_max_colors = 0;
2310 tty->TN_max_pairs = 0;
2311 tty->TN_no_color_video = 0;
2312 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2313 break;
2314 case 0: /* default colors, if any */
2315 default:
2316 tty_default_color_capabilities (tty, 0);
2317 break;
2318 case 8: /* 8 standard ANSI colors */
2319 tty->TS_orig_pair = "\033[0m";
2320 #ifdef TERMINFO
2321 tty->TS_set_foreground = "\033[3%p1%dm";
2322 tty->TS_set_background = "\033[4%p1%dm";
2323 #else
2324 tty->TS_set_foreground = "\033[3%dm";
2325 tty->TS_set_background = "\033[4%dm";
2326 #endif
2327 tty->TN_max_colors = 8;
2328 tty->TN_max_pairs = 64;
2329 tty->TN_no_color_video = 0;
2330 break;
2334 void
2335 set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
2337 Lisp_Object tem, val;
2338 Lisp_Object color_mode;
2339 int mode;
2340 Lisp_Object tty_color_mode_alist
2341 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
2343 tem = assq_no_quit (Qtty_color_mode, f->param_alist);
2344 val = CONSP (tem) ? XCDR (tem) : Qnil;
2346 if (INTEGERP (val))
2347 color_mode = val;
2348 else if (SYMBOLP (tty_color_mode_alist))
2350 tem = Fassq (val, Fsymbol_value (tty_color_mode_alist));
2351 color_mode = CONSP (tem) ? XCDR (tem) : Qnil;
2353 else
2354 color_mode = Qnil;
2356 mode = INTEGERP (color_mode) ? XINT (color_mode) : 0;
2358 if (mode != tty->previous_color_mode)
2360 Lisp_Object funsym = intern ("tty-set-up-initial-frame-faces");
2361 tty->previous_color_mode = mode;
2362 tty_setup_colors (tty , mode);
2363 /* This recomputes all the faces given the new color definitions. */
2364 safe_call (1, &funsym);
2368 #endif /* !DOS_NT */
2372 /* Return the tty display object specified by TERMINAL. */
2374 static struct terminal *
2375 get_tty_terminal (Lisp_Object terminal, int throw)
2377 struct terminal *t = get_terminal (terminal, throw);
2379 if (t && t->type != output_termcap && t->type != output_msdos_raw)
2381 if (throw)
2382 error ("Device %d is not a termcap terminal device", t->id);
2383 else
2384 return NULL;
2387 return t;
2390 /* Return an active termcap device that uses the tty device with the
2391 given name.
2393 This function ignores suspended devices.
2395 Returns NULL if the named terminal device is not opened. */
2397 struct terminal *
2398 get_named_tty (const char *name)
2400 struct terminal *t;
2402 if (!name)
2403 abort ();
2405 for (t = terminal_list; t; t = t->next_terminal)
2407 if ((t->type == output_termcap || t->type == output_msdos_raw)
2408 && !strcmp (t->display_info.tty->name, name)
2409 && TERMINAL_ACTIVE_P (t))
2410 return t;
2413 return 0;
2417 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2418 doc: /* Return the type of the tty device that TERMINAL uses.
2419 Returns nil if TERMINAL is not on a tty device.
2421 TERMINAL can be a terminal object, a frame, or nil (meaning the
2422 selected frame's terminal). */)
2423 (Lisp_Object terminal)
2425 struct terminal *t = get_terminal (terminal, 1);
2427 if (t->type != output_termcap && t->type != output_msdos_raw)
2428 return Qnil;
2430 if (t->display_info.tty->type)
2431 return build_string (t->display_info.tty->type);
2432 else
2433 return Qnil;
2436 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2437 doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2439 TERMINAL can be a terminal object, a frame, or nil (meaning the
2440 selected frame's terminal). This function always returns nil if
2441 TERMINAL is not on a tty device. */)
2442 (Lisp_Object terminal)
2444 struct terminal *t = get_terminal (terminal, 1);
2446 if ((t->type != output_termcap && t->type != output_msdos_raw)
2447 || strcmp (t->display_info.tty->name, DEV_TTY) != 0)
2448 return Qnil;
2449 else
2450 return Qt;
2453 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2454 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2455 This is used to override the terminfo data, for certain terminals that
2456 do not really do underlining, but say that they do. This function has
2457 no effect if used on a non-tty terminal.
2459 TERMINAL can be a terminal object, a frame or nil (meaning the
2460 selected frame's terminal). This function always returns nil if
2461 TERMINAL does not refer to a text-only terminal. */)
2462 (Lisp_Object terminal)
2464 struct terminal *t = get_terminal (terminal, 1);
2466 if (t->type == output_termcap)
2467 t->display_info.tty->TS_enter_underline_mode = 0;
2468 return Qnil;
2473 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2474 doc: /* Suspend the terminal device TTY.
2476 The device is restored to its default state, and Emacs ceases all
2477 access to the tty device. Frames that use the device are not deleted,
2478 but input is not read from them and if they change, their display is
2479 not updated.
2481 TTY may be a terminal object, a frame, or nil for the terminal device
2482 of the currently selected frame.
2484 This function runs `suspend-tty-functions' after suspending the
2485 device. The functions are run with one arg, the id of the suspended
2486 terminal device.
2488 `suspend-tty' does nothing if it is called on a device that is already
2489 suspended.
2491 A suspended tty may be resumed by calling `resume-tty' on it. */)
2492 (Lisp_Object tty)
2494 struct terminal *t = get_tty_terminal (tty, 1);
2495 FILE *f;
2497 if (!t)
2498 error ("Unknown tty device");
2500 f = t->display_info.tty->input;
2502 if (f)
2504 /* First run `suspend-tty-functions' and then clean up the tty
2505 state because `suspend-tty-functions' might need to change
2506 the tty state. */
2507 Lisp_Object args[2];
2508 args[0] = intern ("suspend-tty-functions");
2509 XSETTERMINAL (args[1], t);
2510 Frun_hook_with_args (2, args);
2512 reset_sys_modes (t->display_info.tty);
2513 delete_keyboard_wait_descriptor (fileno (f));
2515 #ifndef MSDOS
2516 fclose (f);
2517 if (f != t->display_info.tty->output)
2518 fclose (t->display_info.tty->output);
2519 #endif
2521 t->display_info.tty->input = 0;
2522 t->display_info.tty->output = 0;
2524 if (FRAMEP (t->display_info.tty->top_frame))
2525 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2529 /* Clear display hooks to prevent further output. */
2530 clear_tty_hooks (t);
2532 return Qnil;
2535 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2536 doc: /* Resume the previously suspended terminal device TTY.
2537 The terminal is opened and reinitialized. Frames that are on the
2538 suspended terminal are revived.
2540 It is an error to resume a terminal while another terminal is active
2541 on the same device.
2543 This function runs `resume-tty-functions' after resuming the terminal.
2544 The functions are run with one arg, the id of the resumed terminal
2545 device.
2547 `resume-tty' does nothing if it is called on a device that is not
2548 suspended.
2550 TTY may be a terminal object, a frame, or nil (meaning the selected
2551 frame's terminal). */)
2552 (Lisp_Object tty)
2554 struct terminal *t = get_tty_terminal (tty, 1);
2555 int fd;
2557 if (!t)
2558 error ("Unknown tty device");
2560 if (!t->display_info.tty->input)
2562 if (get_named_tty (t->display_info.tty->name))
2563 error ("Cannot resume display while another display is active on the same device");
2565 #ifdef MSDOS
2566 t->display_info.tty->output = stdout;
2567 t->display_info.tty->input = stdin;
2568 #else /* !MSDOS */
2569 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2571 if (fd == -1)
2572 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2574 if (strcmp (t->display_info.tty->name, DEV_TTY))
2575 dissociate_if_controlling_tty (fd);
2577 t->display_info.tty->output = fdopen (fd, "w+");
2578 t->display_info.tty->input = t->display_info.tty->output;
2579 #endif
2581 add_keyboard_wait_descriptor (fd);
2583 if (FRAMEP (t->display_info.tty->top_frame))
2585 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2586 int width, height;
2587 int old_height = FRAME_COLS (f);
2588 int old_width = FRAME_LINES (f);
2590 /* Check if terminal/window size has changed while the frame
2591 was suspended. */
2592 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2593 if (width != old_width || height != old_height)
2594 change_frame_size (f, height, width, 0, 0, 0);
2595 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2598 set_tty_hooks (t);
2599 init_sys_modes (t->display_info.tty);
2602 /* Run `resume-tty-functions'. */
2603 Lisp_Object args[2];
2604 args[0] = intern ("resume-tty-functions");
2605 XSETTERMINAL (args[1], t);
2606 Frun_hook_with_args (2, args);
2610 set_tty_hooks (t);
2612 return Qnil;
2616 /***********************************************************************
2617 Mouse
2618 ***********************************************************************/
2620 #ifdef HAVE_GPM
2622 #ifndef HAVE_WINDOW_SYSTEM
2623 void
2624 term_mouse_moveto (int x, int y)
2626 /* TODO: how to set mouse position?
2627 const char *name;
2628 int fd;
2629 name = (const char *) ttyname (0);
2630 fd = open (name, O_WRONLY);
2631 SOME_FUNCTION (x, y, fd);
2632 close (fd);
2633 last_mouse_x = x;
2634 last_mouse_y = y; */
2636 #endif /* HAVE_WINDOW_SYSTEM */
2638 /* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2639 void
2640 tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row,
2641 int start_hpos, int end_hpos,
2642 enum draw_glyphs_face draw)
2644 int nglyphs = end_hpos - start_hpos;
2645 struct frame *f = XFRAME (WINDOW_FRAME (w));
2646 struct tty_display_info *tty = FRAME_TTY (f);
2647 int face_id = tty->mouse_highlight.mouse_face_face_id;
2648 int save_x, save_y, pos_x, pos_y;
2650 if (end_hpos >= row->used[TEXT_AREA])
2651 nglyphs = row->used[TEXT_AREA] - start_hpos;
2653 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2654 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos + WINDOW_LEFT_EDGE_X (w);
2656 /* Save current cursor co-ordinates. */
2657 save_y = curY (tty);
2658 save_x = curX (tty);
2659 cursor_to (f, pos_y, pos_x);
2661 if (draw == DRAW_MOUSE_FACE)
2662 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2663 nglyphs, face_id);
2664 else if (draw == DRAW_NORMAL_TEXT)
2665 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
2667 cursor_to (f, save_y, save_x);
2670 static int
2671 term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2673 /* Has the mouse moved off the glyph it was on at the last sighting? */
2674 if (event->x != last_mouse_x || event->y != last_mouse_y)
2676 frame->mouse_moved = 1;
2677 note_mouse_highlight (frame, event->x, event->y);
2678 /* Remember which glyph we're now on. */
2679 last_mouse_x = event->x;
2680 last_mouse_y = event->y;
2681 return 1;
2683 return 0;
2686 /* Return the current position of the mouse.
2688 Set *f to the frame the mouse is in, or zero if the mouse is in no
2689 Emacs frame. If it is set to zero, all the other arguments are
2690 garbage.
2692 Set *bar_window to Qnil, and *x and *y to the column and
2693 row of the character cell the mouse is over.
2695 Set *timeptr to the time the mouse was at the returned position.
2697 This clears mouse_moved until the next motion
2698 event arrives. */
2699 static void
2700 term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2701 enum scroll_bar_part *part, Lisp_Object *x,
2702 Lisp_Object *y, Time *timeptr)
2704 struct timeval now;
2705 Time sec, usec;
2707 *fp = SELECTED_FRAME ();
2708 (*fp)->mouse_moved = 0;
2710 *bar_window = Qnil;
2711 *part = 0;
2713 XSETINT (*x, last_mouse_x);
2714 XSETINT (*y, last_mouse_y);
2715 gettimeofday(&now, 0);
2716 sec = now.tv_sec;
2717 usec = now.tv_usec;
2718 *timeptr = (sec * 1000) + (usec / 1000);
2721 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2723 If the event is a button press, then note that we have grabbed
2724 the mouse. */
2726 static Lisp_Object
2727 term_mouse_click (struct input_event *result, Gpm_Event *event,
2728 struct frame *f)
2730 struct timeval now;
2731 int i, j;
2733 result->kind = GPM_CLICK_EVENT;
2734 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2736 if (event->buttons & j) {
2737 result->code = i; /* button number */
2738 break;
2741 gettimeofday(&now, 0);
2742 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2744 if (event->type & GPM_UP)
2745 result->modifiers = up_modifier;
2746 else if (event->type & GPM_DOWN)
2747 result->modifiers = down_modifier;
2748 else
2749 result->modifiers = 0;
2751 if (event->type & GPM_SINGLE)
2752 result->modifiers |= click_modifier;
2754 if (event->type & GPM_DOUBLE)
2755 result->modifiers |= double_modifier;
2757 if (event->type & GPM_TRIPLE)
2758 result->modifiers |= triple_modifier;
2760 if (event->type & GPM_DRAG)
2761 result->modifiers |= drag_modifier;
2763 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
2765 /* 1 << KG_SHIFT */
2766 if (event->modifiers & (1 << 0))
2767 result->modifiers |= shift_modifier;
2769 /* 1 << KG_CTRL */
2770 if (event->modifiers & (1 << 2))
2771 result->modifiers |= ctrl_modifier;
2773 /* 1 << KG_ALT || KG_ALTGR */
2774 if (event->modifiers & (1 << 3)
2775 || event->modifiers & (1 << 1))
2776 result->modifiers |= meta_modifier;
2779 XSETINT (result->x, event->x);
2780 XSETINT (result->y, event->y);
2781 XSETFRAME (result->frame_or_window, f);
2782 result->arg = Qnil;
2783 return Qnil;
2787 handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
2789 struct frame *f = XFRAME (tty->top_frame);
2790 struct input_event ie;
2791 int do_help = 0;
2792 int count = 0;
2794 EVENT_INIT (ie);
2795 ie.kind = NO_EVENT;
2796 ie.arg = Qnil;
2798 if (event->type & (GPM_MOVE | GPM_DRAG)) {
2799 previous_help_echo_string = help_echo_string;
2800 help_echo_string = Qnil;
2802 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
2804 if (!term_mouse_movement (f, event))
2805 help_echo_string = previous_help_echo_string;
2807 /* If the contents of the global variable help_echo_string
2808 has changed, generate a HELP_EVENT. */
2809 if (!NILP (help_echo_string)
2810 || !NILP (previous_help_echo_string))
2811 do_help = 1;
2813 goto done;
2815 else {
2816 f->mouse_moved = 0;
2817 term_mouse_click (&ie, event, f);
2820 done:
2821 if (ie.kind != NO_EVENT)
2823 kbd_buffer_store_event_hold (&ie, hold_quit);
2824 count++;
2827 if (do_help
2828 && !(hold_quit && hold_quit->kind != NO_EVENT))
2830 Lisp_Object frame;
2832 if (f)
2833 XSETFRAME (frame, f);
2834 else
2835 frame = Qnil;
2837 gen_help_event (help_echo_string, frame, help_echo_window,
2838 help_echo_object, help_echo_pos);
2839 count++;
2842 return count;
2845 DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
2846 0, 0, 0,
2847 doc: /* Open a connection to Gpm.
2848 Gpm-mouse can only be activated for one tty at a time. */)
2849 (void)
2851 struct frame *f = SELECTED_FRAME ();
2852 struct tty_display_info *tty
2853 = ((f)->output_method == output_termcap
2854 ? (f)->terminal->display_info.tty : NULL);
2855 Gpm_Connect connection;
2857 if (!tty)
2858 error ("Gpm-mouse only works in the GNU/Linux console");
2859 if (gpm_tty == tty)
2860 return Qnil; /* Already activated, nothing to do. */
2861 if (gpm_tty)
2862 error ("Gpm-mouse can only be activated for one tty at a time");
2864 connection.eventMask = ~0;
2865 connection.defaultMask = ~GPM_HARD;
2866 connection.maxMod = ~0;
2867 connection.minMod = 0;
2868 gpm_zerobased = 1;
2870 if (Gpm_Open (&connection, 0) < 0)
2871 error ("Gpm-mouse failed to connect to the gpm daemon");
2872 else
2874 gpm_tty = tty;
2875 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2876 to generate SIGIOs. Apparently we need to call reset_sys_modes
2877 before calling init_sys_modes. */
2878 reset_sys_modes (tty);
2879 init_sys_modes (tty);
2880 add_gpm_wait_descriptor (gpm_fd);
2881 return Qnil;
2885 void
2886 close_gpm (int fd)
2888 if (fd >= 0)
2889 delete_gpm_wait_descriptor (fd);
2890 while (Gpm_Close()); /* close all the stack */
2891 gpm_tty = NULL;
2894 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
2895 0, 0, 0,
2896 doc: /* Close a connection to Gpm. */)
2897 (void)
2899 struct frame *f = SELECTED_FRAME ();
2900 struct tty_display_info *tty
2901 = ((f)->output_method == output_termcap
2902 ? (f)->terminal->display_info.tty : NULL);
2904 if (!tty || gpm_tty != tty)
2905 return Qnil; /* Not activated on this terminal, nothing to do. */
2907 close_gpm (gpm_fd);
2908 return Qnil;
2910 #endif /* HAVE_GPM */
2913 #ifndef MSDOS
2914 /***********************************************************************
2915 Initialization
2916 ***********************************************************************/
2918 /* Initialize the tty-dependent part of frame F. The frame must
2919 already have its device initialized. */
2921 void
2922 create_tty_output (struct frame *f)
2924 struct tty_output *t;
2926 if (! FRAME_TERMCAP_P (f))
2927 abort ();
2929 t = xmalloc (sizeof (struct tty_output));
2930 memset (t, 0, sizeof (struct tty_output));
2932 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
2934 f->output_data.tty = t;
2937 /* Delete frame F's face cache, and its tty-dependent part. */
2939 static void
2940 tty_free_frame_resources (struct frame *f)
2942 if (! FRAME_TERMCAP_P (f))
2943 abort ();
2945 if (FRAME_FACE_CACHE (f))
2946 free_frame_faces (f);
2948 xfree (f->output_data.tty);
2951 #else /* MSDOS */
2953 /* Delete frame F's face cache. */
2955 static void
2956 tty_free_frame_resources (struct frame *f)
2958 if (! FRAME_TERMCAP_P (f) && ! FRAME_MSDOS_P (f))
2959 abort ();
2961 if (FRAME_FACE_CACHE (f))
2962 free_frame_faces (f);
2964 #endif /* MSDOS */
2966 /* Reset the hooks in TERMINAL. */
2968 static void
2969 clear_tty_hooks (struct terminal *terminal)
2971 terminal->rif = 0;
2972 terminal->cursor_to_hook = 0;
2973 terminal->raw_cursor_to_hook = 0;
2974 terminal->clear_to_end_hook = 0;
2975 terminal->clear_frame_hook = 0;
2976 terminal->clear_end_of_line_hook = 0;
2977 terminal->ins_del_lines_hook = 0;
2978 terminal->insert_glyphs_hook = 0;
2979 terminal->write_glyphs_hook = 0;
2980 terminal->delete_glyphs_hook = 0;
2981 terminal->ring_bell_hook = 0;
2982 terminal->reset_terminal_modes_hook = 0;
2983 terminal->set_terminal_modes_hook = 0;
2984 terminal->update_begin_hook = 0;
2985 terminal->update_end_hook = 0;
2986 terminal->set_terminal_window_hook = 0;
2987 terminal->mouse_position_hook = 0;
2988 terminal->frame_rehighlight_hook = 0;
2989 terminal->frame_raise_lower_hook = 0;
2990 terminal->fullscreen_hook = 0;
2991 terminal->set_vertical_scroll_bar_hook = 0;
2992 terminal->condemn_scroll_bars_hook = 0;
2993 terminal->redeem_scroll_bar_hook = 0;
2994 terminal->judge_scroll_bars_hook = 0;
2995 terminal->read_socket_hook = 0;
2996 terminal->frame_up_to_date_hook = 0;
2998 /* Leave these two set, or suspended frames are not deleted
2999 correctly. */
3000 terminal->delete_frame_hook = &tty_free_frame_resources;
3001 terminal->delete_terminal_hook = &delete_tty;
3004 /* Initialize hooks in TERMINAL with the values needed for a tty. */
3006 static void
3007 set_tty_hooks (struct terminal *terminal)
3009 terminal->rif = 0; /* ttys don't support window-based redisplay. */
3011 terminal->cursor_to_hook = &tty_cursor_to;
3012 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
3014 terminal->clear_to_end_hook = &tty_clear_to_end;
3015 terminal->clear_frame_hook = &tty_clear_frame;
3016 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
3018 terminal->ins_del_lines_hook = &tty_ins_del_lines;
3020 terminal->insert_glyphs_hook = &tty_insert_glyphs;
3021 terminal->write_glyphs_hook = &tty_write_glyphs;
3022 terminal->delete_glyphs_hook = &tty_delete_glyphs;
3024 terminal->ring_bell_hook = &tty_ring_bell;
3026 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
3027 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
3028 terminal->update_begin_hook = 0; /* Not needed. */
3029 terminal->update_end_hook = &tty_update_end;
3030 terminal->set_terminal_window_hook = &tty_set_terminal_window;
3032 terminal->mouse_position_hook = 0; /* Not needed. */
3033 terminal->frame_rehighlight_hook = 0; /* Not needed. */
3034 terminal->frame_raise_lower_hook = 0; /* Not needed. */
3036 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
3037 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
3038 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
3039 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
3041 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
3042 terminal->frame_up_to_date_hook = 0; /* Not needed. */
3044 terminal->delete_frame_hook = &tty_free_frame_resources;
3045 terminal->delete_terminal_hook = &delete_tty;
3048 /* Drop the controlling terminal if fd is the same device. */
3049 static void
3050 dissociate_if_controlling_tty (int fd)
3052 #ifndef DOS_NT
3053 int pgid = tcgetpgrp (fd); /* If tcgetpgrp succeeds, fd is the ctty. */
3054 if (pgid != -1)
3056 #if defined (USG5)
3057 setpgrp ();
3058 no_controlling_tty = 1;
3059 #elif defined (CYGWIN)
3060 setsid ();
3061 no_controlling_tty = 1;
3062 #else
3063 #ifdef TIOCNOTTY /* Try BSD ioctls. */
3064 sigblock (sigmask (SIGTTOU));
3065 fd = emacs_open (DEV_TTY, O_RDWR, 0);
3066 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
3068 no_controlling_tty = 1;
3070 if (fd != -1)
3071 emacs_close (fd);
3072 sigunblock (sigmask (SIGTTOU));
3073 #else
3074 /* Unknown system. */
3075 croak ();
3076 #endif /* ! TIOCNOTTY */
3077 #endif /* ! USG */
3079 #endif /* !DOS_NT */
3082 /* Create a termcap display on the tty device with the given name and
3083 type.
3085 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3086 Otherwise NAME should be a path to the tty device file,
3087 e.g. "/dev/pts/7".
3089 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3091 If MUST_SUCCEED is true, then all errors are fatal. */
3093 struct terminal *
3094 init_tty (const char *name, const char *terminal_type, int must_succeed)
3096 char *area = NULL;
3097 char **address = &area;
3098 int buffer_size = 4096;
3099 register char *p = NULL;
3100 int status;
3101 struct tty_display_info *tty = NULL;
3102 struct terminal *terminal = NULL;
3103 int ctty = 0; /* 1 if asked to open controlling tty. */
3105 if (!terminal_type)
3106 maybe_fatal (must_succeed, 0,
3107 "Unknown terminal type",
3108 "Unknown terminal type");
3110 if (name == NULL)
3111 name = DEV_TTY;
3112 if (!strcmp (name, DEV_TTY))
3113 ctty = 1;
3115 /* If we already have a terminal on the given device, use that. If
3116 all such terminals are suspended, create a new one instead. */
3117 /* XXX Perhaps this should be made explicit by having init_tty
3118 always create a new terminal and separating terminal and frame
3119 creation on Lisp level. */
3120 terminal = get_named_tty (name);
3121 if (terminal)
3122 return terminal;
3124 terminal = create_terminal ();
3125 #ifdef MSDOS
3126 if (been_here > 0)
3127 maybe_fatal (0, 0, "Attempt to create another terminal %s", "",
3128 name, "");
3129 been_here = 1;
3130 tty = &the_only_display_info;
3131 #else
3132 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
3133 #endif
3134 memset (tty, 0, sizeof (struct tty_display_info));
3135 tty->next = tty_list;
3136 tty_list = tty;
3138 terminal->type = output_termcap;
3139 terminal->display_info.tty = tty;
3140 tty->terminal = terminal;
3142 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3143 Wcm_clear (tty);
3145 encode_terminal_src_size = 0;
3146 encode_terminal_dst_size = 0;
3148 #ifdef HAVE_GPM
3149 terminal->mouse_position_hook = term_mouse_position;
3150 tty->mouse_highlight.mouse_face_window = Qnil;
3151 #endif
3154 #ifndef DOS_NT
3155 set_tty_hooks (terminal);
3158 int fd;
3159 FILE *file;
3161 #ifdef O_IGNORE_CTTY
3162 if (!ctty)
3163 /* Open the terminal device. Don't recognize it as our
3164 controlling terminal, and don't make it the controlling tty
3165 if we don't have one at the moment. */
3166 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
3167 else
3168 #endif /* O_IGNORE_CTTY */
3169 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3170 defined on Hurd. On other systems, we need to explicitly
3171 dissociate ourselves from the controlling tty when we want to
3172 open a frame on the same terminal. */
3173 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
3175 tty->name = xstrdup (name);
3176 terminal->name = xstrdup (name);
3178 if (fd < 0)
3179 maybe_fatal (must_succeed, terminal,
3180 "Could not open file: %s",
3181 "Could not open file: %s",
3182 name);
3183 if (!isatty (fd))
3185 close (fd);
3186 maybe_fatal (must_succeed, terminal,
3187 "Not a tty device: %s",
3188 "Not a tty device: %s",
3189 name);
3192 #ifndef O_IGNORE_CTTY
3193 if (!ctty)
3194 dissociate_if_controlling_tty (fd);
3195 #endif
3197 file = fdopen (fd, "w+");
3198 tty->input = file;
3199 tty->output = file;
3202 tty->type = xstrdup (terminal_type);
3204 add_keyboard_wait_descriptor (fileno (tty->input));
3206 Wcm_clear (tty);
3208 tty->termcap_term_buffer = (char *) xmalloc (buffer_size);
3210 /* On some systems, tgetent tries to access the controlling
3211 terminal. */
3212 sigblock (sigmask (SIGTTOU));
3213 status = tgetent (tty->termcap_term_buffer, terminal_type);
3214 sigunblock (sigmask (SIGTTOU));
3216 if (status < 0)
3218 #ifdef TERMINFO
3219 maybe_fatal (must_succeed, terminal,
3220 "Cannot open terminfo database file",
3221 "Cannot open terminfo database file");
3222 #else
3223 maybe_fatal (must_succeed, terminal,
3224 "Cannot open termcap database file",
3225 "Cannot open termcap database file");
3226 #endif
3228 if (status == 0)
3230 maybe_fatal (must_succeed, terminal,
3231 "Terminal type %s is not defined",
3232 "Terminal type %s is not defined.\n\
3233 If that is not the actual type of terminal you have,\n\
3234 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3235 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3236 #ifdef TERMINFO
3237 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3238 #else
3239 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3240 #endif
3241 terminal_type);
3244 #ifndef TERMINFO
3245 if (strlen (tty->termcap_term_buffer) >= buffer_size)
3246 abort ();
3247 buffer_size = strlen (tty->termcap_term_buffer);
3248 #endif
3249 tty->termcap_strings_buffer = area = (char *) xmalloc (buffer_size);
3250 tty->TS_ins_line = tgetstr ("al", address);
3251 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3252 tty->TS_bell = tgetstr ("bl", address);
3253 BackTab (tty) = tgetstr ("bt", address);
3254 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3255 tty->TS_clr_line = tgetstr ("ce", address);
3256 tty->TS_clr_frame = tgetstr ("cl", address);
3257 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3258 AbsPosition (tty) = tgetstr ("cm", address);
3259 CR (tty) = tgetstr ("cr", address);
3260 tty->TS_set_scroll_region = tgetstr ("cs", address);
3261 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
3262 RowPosition (tty) = tgetstr ("cv", address);
3263 tty->TS_del_char = tgetstr ("dc", address);
3264 tty->TS_del_multi_chars = tgetstr ("DC", address);
3265 tty->TS_del_line = tgetstr ("dl", address);
3266 tty->TS_del_multi_lines = tgetstr ("DL", address);
3267 tty->TS_delete_mode = tgetstr ("dm", address);
3268 tty->TS_end_delete_mode = tgetstr ("ed", address);
3269 tty->TS_end_insert_mode = tgetstr ("ei", address);
3270 Home (tty) = tgetstr ("ho", address);
3271 tty->TS_ins_char = tgetstr ("ic", address);
3272 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3273 tty->TS_insert_mode = tgetstr ("im", address);
3274 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3275 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3276 tty->TS_keypad_mode = tgetstr ("ks", address);
3277 LastLine (tty) = tgetstr ("ll", address);
3278 Right (tty) = tgetstr ("nd", address);
3279 Down (tty) = tgetstr ("do", address);
3280 if (!Down (tty))
3281 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
3282 if (tgetflag ("bs"))
3283 Left (tty) = "\b"; /* can't possibly be longer! */
3284 else /* (Actually, "bs" is obsolete...) */
3285 Left (tty) = tgetstr ("le", address);
3286 if (!Left (tty))
3287 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
3288 tty->TS_pad_char = tgetstr ("pc", address);
3289 tty->TS_repeat = tgetstr ("rp", address);
3290 tty->TS_end_standout_mode = tgetstr ("se", address);
3291 tty->TS_fwd_scroll = tgetstr ("sf", address);
3292 tty->TS_standout_mode = tgetstr ("so", address);
3293 tty->TS_rev_scroll = tgetstr ("sr", address);
3294 tty->Wcm->cm_tab = tgetstr ("ta", address);
3295 tty->TS_end_termcap_modes = tgetstr ("te", address);
3296 tty->TS_termcap_modes = tgetstr ("ti", address);
3297 Up (tty) = tgetstr ("up", address);
3298 tty->TS_visible_bell = tgetstr ("vb", address);
3299 tty->TS_cursor_normal = tgetstr ("ve", address);
3300 tty->TS_cursor_visible = tgetstr ("vs", address);
3301 tty->TS_cursor_invisible = tgetstr ("vi", address);
3302 tty->TS_set_window = tgetstr ("wi", address);
3304 tty->TS_enter_underline_mode = tgetstr ("us", address);
3305 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3306 tty->TS_enter_bold_mode = tgetstr ("md", address);
3307 tty->TS_enter_dim_mode = tgetstr ("mh", address);
3308 tty->TS_enter_blink_mode = tgetstr ("mb", address);
3309 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3310 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3311 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3312 tty->TS_exit_attribute_mode = tgetstr ("me", address);
3314 MultiUp (tty) = tgetstr ("UP", address);
3315 MultiDown (tty) = tgetstr ("DO", address);
3316 MultiLeft (tty) = tgetstr ("LE", address);
3317 MultiRight (tty) = tgetstr ("RI", address);
3319 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
3320 color because we can't switch back to the default foreground and
3321 background. */
3322 tty->TS_orig_pair = tgetstr ("op", address);
3323 if (tty->TS_orig_pair)
3325 tty->TS_set_foreground = tgetstr ("AF", address);
3326 tty->TS_set_background = tgetstr ("AB", address);
3327 if (!tty->TS_set_foreground)
3329 /* SVr4. */
3330 tty->TS_set_foreground = tgetstr ("Sf", address);
3331 tty->TS_set_background = tgetstr ("Sb", address);
3334 tty->TN_max_colors = tgetnum ("Co");
3335 tty->TN_max_pairs = tgetnum ("pa");
3337 tty->TN_no_color_video = tgetnum ("NC");
3338 if (tty->TN_no_color_video == -1)
3339 tty->TN_no_color_video = 0;
3342 tty_default_color_capabilities (tty, 1);
3344 MagicWrap (tty) = tgetflag ("xn");
3345 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3346 the former flag imply the latter. */
3347 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
3348 terminal->memory_below_frame = tgetflag ("db");
3349 tty->TF_hazeltine = tgetflag ("hz");
3350 terminal->must_write_spaces = tgetflag ("in");
3351 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3352 tty->TF_insmode_motion = tgetflag ("mi");
3353 tty->TF_standout_motion = tgetflag ("ms");
3354 tty->TF_underscore = tgetflag ("ul");
3355 tty->TF_teleray = tgetflag ("xt");
3357 #else /* DOS_NT */
3358 #ifdef WINDOWSNT
3360 struct frame *f = XFRAME (selected_frame);
3362 initialize_w32_display (terminal);
3364 FrameRows (tty) = FRAME_LINES (f);
3365 FrameCols (tty) = FRAME_COLS (f);
3366 tty->specified_window = FRAME_LINES (f);
3368 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3369 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
3370 terminal->char_ins_del_ok = 1;
3371 baud_rate = 19200;
3373 #else /* MSDOS */
3375 int height, width;
3376 if (strcmp (terminal_type, "internal") == 0)
3377 terminal->type = output_msdos_raw;
3378 initialize_msdos_display (terminal);
3380 get_tty_size (fileno (tty->input), &width, &height);
3381 FrameCols (tty) = width;
3382 FrameRows (tty) = height;
3383 terminal->char_ins_del_ok = 0;
3384 init_baud_rate (fileno (tty->input));
3386 #endif /* MSDOS */
3387 tty->output = stdout;
3388 tty->input = stdin;
3389 /* The following two are inaccessible from w32console.c. */
3390 terminal->delete_frame_hook = &tty_free_frame_resources;
3391 terminal->delete_terminal_hook = &delete_tty;
3393 tty->name = xstrdup (name);
3394 terminal->name = xstrdup (name);
3395 tty->type = xstrdup (terminal_type);
3397 add_keyboard_wait_descriptor (0);
3399 tty->delete_in_insert_mode = 1;
3401 UseTabs (tty) = 0;
3402 terminal->scroll_region_ok = 0;
3404 /* Seems to insert lines when it's not supposed to, messing up the
3405 display. In doing a trace, it didn't seem to be called much, so I
3406 don't think we're losing anything by turning it off. */
3407 terminal->line_ins_del_ok = 0;
3409 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
3410 #endif /* DOS_NT */
3412 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3413 init_kboard (terminal->kboard);
3414 KVAR (terminal->kboard, Vwindow_system) = Qnil;
3415 terminal->kboard->next_kboard = all_kboards;
3416 all_kboards = terminal->kboard;
3417 terminal->kboard->reference_count++;
3418 /* Don't let the initial kboard remain current longer than necessary.
3419 That would cause problems if a file loaded on startup tries to
3420 prompt in the mini-buffer. */
3421 if (current_kboard == initial_kboard)
3422 current_kboard = terminal->kboard;
3423 #ifndef DOS_NT
3424 term_get_fkeys (address, terminal->kboard);
3426 /* Get frame size from system, or else from termcap. */
3428 int height, width;
3429 get_tty_size (fileno (tty->input), &width, &height);
3430 FrameCols (tty) = width;
3431 FrameRows (tty) = height;
3434 if (FrameCols (tty) <= 0)
3435 FrameCols (tty) = tgetnum ("co");
3436 if (FrameRows (tty) <= 0)
3437 FrameRows (tty) = tgetnum ("li");
3439 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
3440 maybe_fatal (must_succeed, terminal,
3441 "Screen size %dx%d is too small",
3442 "Screen size %dx%d is too small",
3443 FrameCols (tty), FrameRows (tty));
3445 TabWidth (tty) = tgetnum ("tw");
3447 if (!tty->TS_bell)
3448 tty->TS_bell = "\07";
3450 if (!tty->TS_fwd_scroll)
3451 tty->TS_fwd_scroll = Down (tty);
3453 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
3455 if (TabWidth (tty) < 0)
3456 TabWidth (tty) = 8;
3458 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3459 and newer termcap doc does not seem to say there is a default.
3460 if (!tty->Wcm->cm_tab)
3461 tty->Wcm->cm_tab = "\t";
3464 /* We don't support standout modes that use `magic cookies', so
3465 turn off any that do. */
3466 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
3468 tty->TS_standout_mode = 0;
3469 tty->TS_end_standout_mode = 0;
3471 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
3473 tty->TS_enter_underline_mode = 0;
3474 tty->TS_exit_underline_mode = 0;
3477 /* If there's no standout mode, try to use underlining instead. */
3478 if (tty->TS_standout_mode == 0)
3480 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3481 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
3484 /* If no `se' string, try using a `me' string instead.
3485 If that fails, we can't use standout mode at all. */
3486 if (tty->TS_end_standout_mode == 0)
3488 char *s = tgetstr ("me", address);
3489 if (s != 0)
3490 tty->TS_end_standout_mode = s;
3491 else
3492 tty->TS_standout_mode = 0;
3495 if (tty->TF_teleray)
3497 tty->Wcm->cm_tab = 0;
3498 /* We can't support standout mode, because it uses magic cookies. */
3499 tty->TS_standout_mode = 0;
3500 /* But that means we cannot rely on ^M to go to column zero! */
3501 CR (tty) = 0;
3502 /* LF can't be trusted either -- can alter hpos */
3503 /* if move at column 0 thru a line with TS_standout_mode */
3504 Down (tty) = 0;
3507 /* Special handling for certain terminal types known to need it */
3509 if (!strcmp (terminal_type, "supdup"))
3511 terminal->memory_below_frame = 1;
3512 tty->Wcm->cm_losewrap = 1;
3514 if (!strncmp (terminal_type, "c10", 3)
3515 || !strcmp (terminal_type, "perq"))
3517 /* Supply a makeshift :wi string.
3518 This string is not valid in general since it works only
3519 for windows starting at the upper left corner;
3520 but that is all Emacs uses.
3522 This string works only if the frame is using
3523 the top of the video memory, because addressing is memory-relative.
3524 So first check the :ti string to see if that is true.
3526 It would be simpler if the :wi string could go in the termcap
3527 entry, but it can't because it is not fully valid.
3528 If it were in the termcap entry, it would confuse other programs. */
3529 if (!tty->TS_set_window)
3531 const char *m = tty->TS_termcap_modes;
3532 while (*m && strcmp (m, "\033v "))
3533 m++;
3534 if (*m)
3535 tty->TS_set_window = "\033v%C %C %C %C ";
3537 /* Termcap entry often fails to have :in: flag */
3538 terminal->must_write_spaces = 1;
3539 /* :ti string typically fails to have \E^G! in it */
3540 /* This limits scope of insert-char to one line. */
3541 strcpy (area, tty->TS_termcap_modes);
3542 strcat (area, "\033\007!");
3543 tty->TS_termcap_modes = area;
3544 area += strlen (area) + 1;
3545 p = AbsPosition (tty);
3546 /* Change all %+ parameters to %C, to handle
3547 values above 96 correctly for the C100. */
3548 while (*p)
3550 if (p[0] == '%' && p[1] == '+')
3551 p[1] = 'C';
3552 p++;
3556 tty->specified_window = FrameRows (tty);
3558 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3560 maybe_fatal (must_succeed, terminal,
3561 "Terminal type \"%s\" is not powerful enough to run Emacs",
3562 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3563 It lacks the ability to position the cursor.\n\
3564 If that is not the actual type of terminal you have,\n\
3565 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3566 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3567 # ifdef TERMINFO
3568 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3569 # else /* TERMCAP */
3570 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3571 # endif /* TERMINFO */
3572 terminal_type);
3575 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
3576 maybe_fatal (must_succeed, terminal,
3577 "Could not determine the frame size",
3578 "Could not determine the frame size");
3580 tty->delete_in_insert_mode
3581 = tty->TS_delete_mode && tty->TS_insert_mode
3582 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
3584 tty->se_is_so = (tty->TS_standout_mode
3585 && tty->TS_end_standout_mode
3586 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
3588 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
3590 terminal->scroll_region_ok
3591 = (tty->Wcm->cm_abs
3592 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
3594 terminal->line_ins_del_ok
3595 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3596 && (tty->TS_del_line || tty->TS_del_multi_lines))
3597 || (terminal->scroll_region_ok
3598 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
3600 terminal->char_ins_del_ok
3601 = ((tty->TS_ins_char || tty->TS_insert_mode
3602 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3603 && (tty->TS_del_char || tty->TS_del_multi_chars));
3605 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
3607 init_baud_rate (fileno (tty->input));
3609 #endif /* not DOS_NT */
3611 /* Init system terminal modes (RAW or CBREAK, etc.). */
3612 init_sys_modes (tty);
3614 return terminal;
3618 static void
3619 vfatal (const char *str, va_list ap)
3621 fprintf (stderr, "emacs: ");
3622 vfprintf (stderr, str, ap);
3623 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
3624 fprintf (stderr, "\n");
3625 fflush (stderr);
3626 exit (1);
3630 /* Auxiliary error-handling function for init_tty.
3631 Delete TERMINAL, then call error or fatal with str1 or str2,
3632 respectively, according to whether MUST_SUCCEED is zero or not. */
3634 static void
3635 maybe_fatal (int must_succeed, struct terminal *terminal,
3636 const char *str1, const char *str2, ...)
3638 va_list ap;
3639 va_start (ap, str2);
3640 if (terminal)
3641 delete_tty (terminal);
3643 if (must_succeed)
3644 vfatal (str2, ap);
3645 else
3646 verror (str1, ap);
3648 va_end (ap);
3649 abort ();
3652 void
3653 fatal (const char *str, ...)
3655 va_list ap;
3656 va_start (ap, str);
3657 vfatal (str, ap);
3658 va_end (ap);
3663 /* Delete the given tty terminal, closing all frames on it. */
3665 static void
3666 delete_tty (struct terminal *terminal)
3668 struct tty_display_info *tty;
3670 /* Protect against recursive calls. delete_frame in
3671 delete_terminal calls us back when it deletes our last frame. */
3672 if (!terminal->name)
3673 return;
3675 if (terminal->type != output_termcap)
3676 abort ();
3678 tty = terminal->display_info.tty;
3680 if (tty == tty_list)
3681 tty_list = tty->next;
3682 else
3684 struct tty_display_info *p;
3685 for (p = tty_list; p && p->next != tty; p = p->next)
3688 if (! p)
3689 /* This should not happen. */
3690 abort ();
3692 p->next = tty->next;
3693 tty->next = 0;
3696 /* reset_sys_modes needs a valid device, so this call needs to be
3697 before delete_terminal. */
3698 reset_sys_modes (tty);
3700 delete_terminal (terminal);
3702 xfree (tty->name);
3703 xfree (tty->type);
3705 if (tty->input)
3707 delete_keyboard_wait_descriptor (fileno (tty->input));
3708 if (tty->input != stdin)
3709 fclose (tty->input);
3711 if (tty->output && tty->output != stdout && tty->output != tty->input)
3712 fclose (tty->output);
3713 if (tty->termscript)
3714 fclose (tty->termscript);
3716 xfree (tty->old_tty);
3717 xfree (tty->Wcm);
3718 xfree (tty->termcap_strings_buffer);
3719 xfree (tty->termcap_term_buffer);
3721 memset (tty, 0, sizeof (struct tty_display_info));
3722 xfree (tty);
3727 /* Mark the pointers in the tty_display_info objects.
3728 Called by the Fgarbage_collector. */
3730 void
3731 mark_ttys (void)
3733 struct tty_display_info *tty;
3735 for (tty = tty_list; tty; tty = tty->next)
3736 mark_object (tty->top_frame);
3741 void
3742 syms_of_term (void)
3744 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo,
3745 doc: /* Non-nil means the system uses terminfo rather than termcap.
3746 This variable can be used by terminal emulator packages. */);
3747 #ifdef TERMINFO
3748 system_uses_terminfo = 1;
3749 #else
3750 system_uses_terminfo = 0;
3751 #endif
3753 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions,
3754 doc: /* Functions to be run after suspending a tty.
3755 The functions are run with one argument, the terminal object to be suspended.
3756 See `suspend-tty'. */);
3757 Vsuspend_tty_functions = Qnil;
3760 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions,
3761 doc: /* Functions to be run after resuming a tty.
3762 The functions are run with one argument, the terminal object that was revived.
3763 See `resume-tty'. */);
3764 Vresume_tty_functions = Qnil;
3766 DEFVAR_BOOL ("visible-cursor", visible_cursor,
3767 doc: /* Non-nil means to make the cursor very visible.
3768 This only has an effect when running in a text terminal.
3769 What means \"very visible\" is up to your terminal. It may make the cursor
3770 bigger, or it may make it blink, or it may do nothing at all. */);
3771 visible_cursor = 1;
3773 defsubr (&Stty_display_color_p);
3774 defsubr (&Stty_display_color_cells);
3775 defsubr (&Stty_no_underline);
3776 defsubr (&Stty_type);
3777 defsubr (&Scontrolling_tty_p);
3778 defsubr (&Ssuspend_tty);
3779 defsubr (&Sresume_tty);
3780 #ifdef HAVE_GPM
3781 defsubr (&Sgpm_mouse_start);
3782 defsubr (&Sgpm_mouse_stop);
3783 #endif /* HAVE_GPM */
3785 #ifndef DOS_NT
3786 default_orig_pair = NULL;
3787 default_set_foreground = NULL;
3788 default_set_background = NULL;
3789 #endif /* !DOS_NT */
3791 encode_terminal_src = NULL;
3792 encode_terminal_dst = NULL;