Avoid the access to NULL memory while gc marks.
[emacs.git] / src / term.c
blobf08b3787f74be36bcb9bbb6d683f9a5ef8051ca9
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1998, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
23 #include <config.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/file.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
34 #if HAVE_TERMIOS_H
35 #include <termios.h> /* For TIOCNOTTY. */
36 #endif
38 #include <signal.h>
39 #include <stdarg.h>
40 #include <setjmp.h>
42 #include "lisp.h"
43 #include "termchar.h"
44 #include "termopts.h"
45 #include "buffer.h"
46 #include "character.h"
47 #include "charset.h"
48 #include "coding.h"
49 #include "composite.h"
50 #include "keyboard.h"
51 #include "frame.h"
52 #include "disptab.h"
53 #include "termhooks.h"
54 #include "dispextern.h"
55 #include "window.h"
56 #include "keymap.h"
57 #include "blockinput.h"
58 #include "syssignal.h"
59 #include "systty.h"
60 #include "intervals.h"
61 #ifdef MSDOS
62 #include "msdos.h"
63 static int been_here = -1;
64 #endif
66 /* For now, don't try to include termcap.h. On some systems,
67 configure finds a non-standard termcap.h that the main build
68 won't find. */
69 extern void tputs P_ ((const char *, int, int (*)(int)));
70 extern int tgetent P_ ((char *, const char *));
71 extern int tgetflag P_ ((char *id));
72 extern int tgetnum P_ ((char *id));
74 #include "cm.h"
75 #ifdef HAVE_X_WINDOWS
76 #include "xterm.h"
77 #endif
79 #ifndef O_RDWR
80 #define O_RDWR 2
81 #endif
83 #ifndef O_NOCTTY
84 #define O_NOCTTY 0
85 #endif
87 /* The name of the default console device. */
88 #ifdef WINDOWSNT
89 #define DEV_TTY "CONOUT$"
90 #else
91 #define DEV_TTY "/dev/tty"
92 #endif
94 static void tty_set_scroll_region P_ ((struct frame *f, int start, int stop));
95 static void turn_on_face P_ ((struct frame *, int face_id));
96 static void turn_off_face P_ ((struct frame *, int face_id));
97 static void tty_show_cursor P_ ((struct tty_display_info *));
98 static void tty_hide_cursor P_ ((struct tty_display_info *));
99 static void tty_background_highlight P_ ((struct tty_display_info *tty));
100 static void clear_tty_hooks P_ ((struct terminal *terminal));
101 static void set_tty_hooks P_ ((struct terminal *terminal));
102 static void dissociate_if_controlling_tty P_ ((int fd));
103 static void delete_tty P_ ((struct terminal *));
105 #define OUTPUT(tty, a) \
106 emacs_tputs ((tty), a, \
107 (int) (FRAME_LINES (XFRAME (selected_frame)) \
108 - curY (tty)), \
109 cmputc)
111 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
112 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
114 #define OUTPUT_IF(tty, a) \
115 do { \
116 if (a) \
117 emacs_tputs ((tty), a, \
118 (int) (FRAME_LINES (XFRAME (selected_frame)) \
119 - curY (tty) ), \
120 cmputc); \
121 } while (0)
123 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
125 /* If true, use "vs", otherwise use "ve" to make the cursor visible. */
127 static int visible_cursor;
129 /* Display space properties */
131 extern Lisp_Object Qspace, QCalign_to, QCwidth;
133 /* Functions to call after suspending a tty. */
134 Lisp_Object impl_Vsuspend_tty_functions;
136 /* Functions to call after resuming a tty. */
137 Lisp_Object impl_Vresume_tty_functions;
139 /* Chain of all tty device parameters. */
140 struct tty_display_info *tty_list;
142 /* Nonzero means no need to redraw the entire frame on resuming a
143 suspended Emacs. This is useful on terminals with multiple
144 pages, where one page is used for Emacs and another for all
145 else. */
146 int no_redraw_on_reenter;
148 /* Meaning of bits in no_color_video. Each bit set means that the
149 corresponding attribute cannot be combined with colors. */
151 enum no_color_bit
153 NC_STANDOUT = 1 << 0,
154 NC_UNDERLINE = 1 << 1,
155 NC_REVERSE = 1 << 2,
156 NC_BLINK = 1 << 3,
157 NC_DIM = 1 << 4,
158 NC_BOLD = 1 << 5,
159 NC_INVIS = 1 << 6,
160 NC_PROTECT = 1 << 7,
161 NC_ALT_CHARSET = 1 << 8
164 /* internal state */
166 /* The largest frame width in any call to calculate_costs. */
168 int max_frame_cols;
170 /* The largest frame height in any call to calculate_costs. */
172 int max_frame_lines;
174 /* Non-zero if we have dropped our controlling tty and therefore
175 should not open a frame on stdout. */
176 static int no_controlling_tty;
178 /* Provided for lisp packages. */
180 static int system_uses_terminfo;
182 char *tparam ();
184 extern char *tgetstr ();
187 #ifdef HAVE_GPM
188 #include <sys/fcntl.h>
190 static void term_clear_mouse_face ();
191 static void term_mouse_highlight (struct frame *f, int x, int y);
193 /* The device for which we have enabled gpm support (or NULL). */
194 struct tty_display_info *gpm_tty = NULL;
196 /* These variables describe the range of text currently shown in its
197 mouse-face, together with the window they apply to. As long as
198 the mouse stays within this range, we need not redraw anything on
199 its account. Rows and columns are glyph matrix positions in
200 MOUSE_FACE_WINDOW. */
201 static int mouse_face_beg_row, mouse_face_beg_col;
202 static int mouse_face_end_row, mouse_face_end_col;
203 static int mouse_face_past_end;
204 static Lisp_Object mouse_face_window;
205 static int mouse_face_face_id;
207 static int pos_x, pos_y;
208 static int last_mouse_x, last_mouse_y;
209 #endif /* HAVE_GPM */
211 /* Ring the bell on a tty. */
213 static void
214 tty_ring_bell (struct frame *f)
216 struct tty_display_info *tty = FRAME_TTY (f);
218 if (tty->output)
220 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
221 ? tty->TS_visible_bell
222 : tty->TS_bell));
223 fflush (tty->output);
227 /* Set up termcap modes for Emacs. */
229 void
230 tty_set_terminal_modes (struct terminal *terminal)
232 struct tty_display_info *tty = terminal->display_info.tty;
234 if (tty->output)
236 if (tty->TS_termcap_modes)
237 OUTPUT (tty, tty->TS_termcap_modes);
238 else
240 /* Output enough newlines to scroll all the old screen contents
241 off the screen, so it won't be overwritten and lost. */
242 int i;
243 current_tty = tty;
244 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
245 cmputc ('\n');
248 OUTPUT_IF (tty, tty->TS_termcap_modes);
249 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
250 OUTPUT_IF (tty, tty->TS_keypad_mode);
251 losecursor (tty);
252 fflush (tty->output);
256 /* Reset termcap modes before exiting Emacs. */
258 void
259 tty_reset_terminal_modes (struct terminal *terminal)
261 struct tty_display_info *tty = terminal->display_info.tty;
263 if (tty->output)
265 tty_turn_off_highlight (tty);
266 tty_turn_off_insert (tty);
267 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
268 OUTPUT_IF (tty, tty->TS_cursor_normal);
269 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
270 OUTPUT_IF (tty, tty->TS_orig_pair);
271 /* Output raw CR so kernel can track the cursor hpos. */
272 current_tty = tty;
273 cmputc ('\r');
274 fflush (tty->output);
278 /* Flag the end of a display update on a termcap terminal. */
280 static void
281 tty_update_end (struct frame *f)
283 struct tty_display_info *tty = FRAME_TTY (f);
285 if (!XWINDOW (selected_window)->cursor_off_p)
286 tty_show_cursor (tty);
287 tty_turn_off_insert (tty);
288 tty_background_highlight (tty);
291 /* The implementation of set_terminal_window for termcap frames. */
293 static void
294 tty_set_terminal_window (struct frame *f, int size)
296 struct tty_display_info *tty = FRAME_TTY (f);
298 tty->specified_window = size ? size : FRAME_LINES (f);
299 if (FRAME_SCROLL_REGION_OK (f))
300 tty_set_scroll_region (f, 0, tty->specified_window);
303 static void
304 tty_set_scroll_region (struct frame *f, int start, int stop)
306 char *buf;
307 struct tty_display_info *tty = FRAME_TTY (f);
309 if (tty->TS_set_scroll_region)
310 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1);
311 else if (tty->TS_set_scroll_region_1)
312 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
313 FRAME_LINES (f), start,
314 FRAME_LINES (f) - stop,
315 FRAME_LINES (f));
316 else
317 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
319 OUTPUT (tty, buf);
320 xfree (buf);
321 losecursor (tty);
325 static void
326 tty_turn_on_insert (struct tty_display_info *tty)
328 if (!tty->insert_mode)
329 OUTPUT (tty, tty->TS_insert_mode);
330 tty->insert_mode = 1;
333 void
334 tty_turn_off_insert (struct tty_display_info *tty)
336 if (tty->insert_mode)
337 OUTPUT (tty, tty->TS_end_insert_mode);
338 tty->insert_mode = 0;
341 /* Handle highlighting. */
343 void
344 tty_turn_off_highlight (struct tty_display_info *tty)
346 if (tty->standout_mode)
347 OUTPUT_IF (tty, tty->TS_end_standout_mode);
348 tty->standout_mode = 0;
351 static void
352 tty_turn_on_highlight (struct tty_display_info *tty)
354 if (!tty->standout_mode)
355 OUTPUT_IF (tty, tty->TS_standout_mode);
356 tty->standout_mode = 1;
359 static void
360 tty_toggle_highlight (struct tty_display_info *tty)
362 if (tty->standout_mode)
363 tty_turn_off_highlight (tty);
364 else
365 tty_turn_on_highlight (tty);
369 /* Make cursor invisible. */
371 static void
372 tty_hide_cursor (struct tty_display_info *tty)
374 if (tty->cursor_hidden == 0)
376 tty->cursor_hidden = 1;
377 OUTPUT_IF (tty, tty->TS_cursor_invisible);
382 /* Ensure that cursor is visible. */
384 static void
385 tty_show_cursor (struct tty_display_info *tty)
387 if (tty->cursor_hidden)
389 tty->cursor_hidden = 0;
390 OUTPUT_IF (tty, tty->TS_cursor_normal);
391 if (visible_cursor)
392 OUTPUT_IF (tty, tty->TS_cursor_visible);
397 /* Set standout mode to the state it should be in for
398 empty space inside windows. What this is,
399 depends on the user option inverse-video. */
401 static void
402 tty_background_highlight (struct tty_display_info *tty)
404 if (inverse_video)
405 tty_turn_on_highlight (tty);
406 else
407 tty_turn_off_highlight (tty);
410 /* Set standout mode to the mode specified for the text to be output. */
412 static void
413 tty_highlight_if_desired (struct tty_display_info *tty)
415 if (inverse_video)
416 tty_turn_on_highlight (tty);
417 else
418 tty_turn_off_highlight (tty);
422 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
423 frame-relative coordinates. */
425 static void
426 tty_cursor_to (struct frame *f, int vpos, int hpos)
428 struct tty_display_info *tty = FRAME_TTY (f);
430 /* Detect the case where we are called from reset_sys_modes
431 and the costs have never been calculated. Do nothing. */
432 if (! tty->costs_set)
433 return;
435 if (curY (tty) == vpos
436 && curX (tty) == hpos)
437 return;
438 if (!tty->TF_standout_motion)
439 tty_background_highlight (tty);
440 if (!tty->TF_insmode_motion)
441 tty_turn_off_insert (tty);
442 cmgoto (tty, vpos, hpos);
445 /* Similar but don't take any account of the wasted characters. */
447 static void
448 tty_raw_cursor_to (struct frame *f, int row, int col)
450 struct tty_display_info *tty = FRAME_TTY (f);
452 if (curY (tty) == row
453 && curX (tty) == col)
454 return;
455 if (!tty->TF_standout_motion)
456 tty_background_highlight (tty);
457 if (!tty->TF_insmode_motion)
458 tty_turn_off_insert (tty);
459 cmgoto (tty, row, col);
462 /* Erase operations */
464 /* Clear from cursor to end of frame on a termcap device. */
466 static void
467 tty_clear_to_end (struct frame *f)
469 register int i;
470 struct tty_display_info *tty = FRAME_TTY (f);
472 if (tty->TS_clr_to_bottom)
474 tty_background_highlight (tty);
475 OUTPUT (tty, tty->TS_clr_to_bottom);
477 else
479 for (i = curY (tty); i < FRAME_LINES (f); i++)
481 cursor_to (f, i, 0);
482 clear_end_of_line (f, FRAME_COLS (f));
487 /* Clear an entire termcap frame. */
489 static void
490 tty_clear_frame (struct frame *f)
492 struct tty_display_info *tty = FRAME_TTY (f);
494 if (tty->TS_clr_frame)
496 tty_background_highlight (tty);
497 OUTPUT (tty, tty->TS_clr_frame);
498 cmat (tty, 0, 0);
500 else
502 cursor_to (f, 0, 0);
503 clear_to_end (f);
507 /* An implementation of clear_end_of_line for termcap frames.
509 Note that the cursor may be moved, on terminals lacking a `ce' string. */
511 static void
512 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
514 register int i;
515 struct tty_display_info *tty = FRAME_TTY (f);
517 /* Detect the case where we are called from reset_sys_modes
518 and the costs have never been calculated. Do nothing. */
519 if (! tty->costs_set)
520 return;
522 if (curX (tty) >= first_unused_hpos)
523 return;
524 tty_background_highlight (tty);
525 if (tty->TS_clr_line)
527 OUTPUT1 (tty, tty->TS_clr_line);
529 else
530 { /* have to do it the hard way */
531 tty_turn_off_insert (tty);
533 /* Do not write in last row last col with Auto-wrap on. */
534 if (AutoWrap (tty)
535 && curY (tty) == FrameRows (tty) - 1
536 && first_unused_hpos == FrameCols (tty))
537 first_unused_hpos--;
539 for (i = curX (tty); i < first_unused_hpos; i++)
541 if (tty->termscript)
542 fputc (' ', tty->termscript);
543 fputc (' ', tty->output);
545 cmplus (tty, first_unused_hpos - curX (tty));
549 /* Buffers to store the source and result of code conversion for terminal. */
550 static unsigned char *encode_terminal_src;
551 static unsigned char *encode_terminal_dst;
552 /* Allocated sizes of the above buffers. */
553 static int encode_terminal_src_size;
554 static int encode_terminal_dst_size;
556 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
557 Set CODING->produced to the byte-length of the resulting byte
558 sequence, and return a pointer to that byte sequence. */
560 unsigned char *
561 encode_terminal_code (src, src_len, coding)
562 struct glyph *src;
563 int src_len;
564 struct coding_system *coding;
566 struct glyph *src_end = src + src_len;
567 unsigned char *buf;
568 int nchars, nbytes, required;
569 register int tlen = GLYPH_TABLE_LENGTH;
570 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
571 Lisp_Object charset_list;
573 /* Allocate sufficient size of buffer to store all characters in
574 multibyte-form. But, it may be enlarged on demand if
575 Vglyph_table contains a string or a composite glyph is
576 encountered. */
577 required = MAX_MULTIBYTE_LENGTH * src_len;
578 if (encode_terminal_src_size < required)
580 if (encode_terminal_src)
581 encode_terminal_src = xrealloc (encode_terminal_src, required);
582 else
583 encode_terminal_src = xmalloc (required);
584 encode_terminal_src_size = required;
587 charset_list = coding_charset_list (coding);
589 buf = encode_terminal_src;
590 nchars = 0;
591 while (src < src_end)
593 if (src->type == COMPOSITE_GLYPH)
595 struct composition *cmp;
596 Lisp_Object gstring;
597 int i;
599 nbytes = buf - encode_terminal_src;
600 if (src->u.cmp.automatic)
602 gstring = composition_gstring_from_id (src->u.cmp.id);
603 required = src->u.cmp.to + 1 - src->u.cmp.from;
605 else
607 cmp = composition_table[src->u.cmp.id];
608 required = MAX_MULTIBYTE_LENGTH * cmp->glyph_len;
611 if (encode_terminal_src_size < nbytes + required)
613 encode_terminal_src_size = nbytes + required;
614 encode_terminal_src = xrealloc (encode_terminal_src,
615 encode_terminal_src_size);
616 buf = encode_terminal_src + nbytes;
619 if (src->u.cmp.automatic)
620 for (i = src->u.cmp.from; i <= src->u.cmp.to; i++)
622 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
623 int c = LGLYPH_CHAR (g);
625 if (! char_charset (c, charset_list, NULL))
626 c = '?';
627 buf += CHAR_STRING (c, buf);
628 nchars++;
630 else
631 for (i = 0; i < cmp->glyph_len; i++)
633 int c = COMPOSITION_GLYPH (cmp, i);
635 if (c == '\t')
636 continue;
637 if (char_charset (c, charset_list, NULL))
639 if (CHAR_WIDTH (c) == 0
640 && i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
641 /* Should be left-padded */
643 buf += CHAR_STRING (' ', buf);
644 nchars++;
647 else
648 c = '?';
649 buf += CHAR_STRING (c, buf);
650 nchars++;
653 /* We must skip glyphs to be padded for a wide character. */
654 else if (! CHAR_GLYPH_PADDING_P (*src))
656 GLYPH g;
657 int c;
658 Lisp_Object string;
660 string = Qnil;
661 SET_GLYPH_FROM_CHAR_GLYPH (g, src[0]);
663 if (GLYPH_INVALID_P (g) || GLYPH_SIMPLE_P (tbase, tlen, g))
665 /* This glyph doesn't have an entry in Vglyph_table. */
666 c = src->u.ch;
668 else
670 /* This glyph has an entry in Vglyph_table,
671 so process any alias before testing for simpleness. */
672 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
674 if (GLYPH_SIMPLE_P (tbase, tlen, g))
675 /* We set the multi-byte form of a character in G
676 (that should be an ASCII character) at WORKBUF. */
677 c = GLYPH_CHAR (g);
678 else
679 /* We have a string in Vglyph_table. */
680 string = tbase[GLYPH_CHAR (g)];
683 if (NILP (string))
685 nbytes = buf - encode_terminal_src;
686 if (encode_terminal_src_size < nbytes + MAX_MULTIBYTE_LENGTH)
688 encode_terminal_src_size = nbytes + MAX_MULTIBYTE_LENGTH;
689 encode_terminal_src = xrealloc (encode_terminal_src,
690 encode_terminal_src_size);
691 buf = encode_terminal_src + nbytes;
693 if (char_charset (c, charset_list, NULL))
695 /* Store the multibyte form of C at BUF. */
696 buf += CHAR_STRING (c, buf);
697 nchars++;
699 else
701 /* C is not encodable. */
702 *buf++ = '?';
703 nchars++;
704 while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
706 *buf++ = '?';
707 nchars++;
708 src++;
712 else
714 unsigned char *p = SDATA (string), *pend = p + SBYTES (string);
716 if (! STRING_MULTIBYTE (string))
717 string = string_to_multibyte (string);
718 nbytes = buf - encode_terminal_src;
719 if (encode_terminal_src_size < nbytes + SBYTES (string))
721 encode_terminal_src_size = nbytes + SBYTES (string);
722 encode_terminal_src = xrealloc (encode_terminal_src,
723 encode_terminal_src_size);
724 buf = encode_terminal_src + nbytes;
726 bcopy (SDATA (string), buf, SBYTES (string));
727 buf += SBYTES (string);
728 nchars += SCHARS (string);
731 src++;
734 if (nchars == 0)
736 coding->produced = 0;
737 return NULL;
740 nbytes = buf - encode_terminal_src;
741 coding->source = encode_terminal_src;
742 if (encode_terminal_dst_size == 0)
744 encode_terminal_dst_size = encode_terminal_src_size;
745 if (encode_terminal_dst)
746 encode_terminal_dst = xrealloc (encode_terminal_dst,
747 encode_terminal_dst_size);
748 else
749 encode_terminal_dst = xmalloc (encode_terminal_dst_size);
751 coding->destination = encode_terminal_dst;
752 coding->dst_bytes = encode_terminal_dst_size;
753 encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
754 /* coding->destination may have been reallocated. */
755 encode_terminal_dst = coding->destination;
756 encode_terminal_dst_size = coding->dst_bytes;
758 return (encode_terminal_dst);
763 /* An implementation of write_glyphs for termcap frames. */
765 static void
766 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
768 unsigned char *conversion_buffer;
769 struct coding_system *coding;
771 struct tty_display_info *tty = FRAME_TTY (f);
773 tty_turn_off_insert (tty);
774 tty_hide_cursor (tty);
776 /* Don't dare write in last column of bottom line, if Auto-Wrap,
777 since that would scroll the whole frame on some terminals. */
779 if (AutoWrap (tty)
780 && curY (tty) + 1 == FRAME_LINES (f)
781 && (curX (tty) + len) == FRAME_COLS (f))
782 len --;
783 if (len <= 0)
784 return;
786 cmplus (tty, len);
788 /* If terminal_coding does any conversion, use it, otherwise use
789 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
790 because it always return 1 if the member src_multibyte is 1. */
791 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
792 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
793 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
794 the tail. */
795 coding->mode &= ~CODING_MODE_LAST_BLOCK;
797 while (len > 0)
799 /* Identify a run of glyphs with the same face. */
800 int face_id = string->face_id;
801 int n;
803 for (n = 1; n < len; ++n)
804 if (string[n].face_id != face_id)
805 break;
807 /* Turn appearance modes of the face of the run on. */
808 tty_highlight_if_desired (tty);
809 turn_on_face (f, face_id);
811 if (n == len)
812 /* This is the last run. */
813 coding->mode |= CODING_MODE_LAST_BLOCK;
814 conversion_buffer = encode_terminal_code (string, n, coding);
815 if (coding->produced > 0)
817 BLOCK_INPUT;
818 fwrite (conversion_buffer, 1, coding->produced, tty->output);
819 if (ferror (tty->output))
820 clearerr (tty->output);
821 if (tty->termscript)
822 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
823 UNBLOCK_INPUT;
825 len -= n;
826 string += n;
828 /* Turn appearance modes off. */
829 turn_off_face (f, face_id);
830 tty_turn_off_highlight (tty);
833 cmcheckmagic (tty);
836 #ifdef HAVE_GPM /* Only used by GPM code. */
838 static void
839 tty_write_glyphs_with_face (f, string, len, face_id)
840 register struct frame *f;
841 register struct glyph *string;
842 register int len, face_id;
844 unsigned char *conversion_buffer;
845 struct coding_system *coding;
847 struct tty_display_info *tty = FRAME_TTY (f);
849 tty_turn_off_insert (tty);
850 tty_hide_cursor (tty);
852 /* Don't dare write in last column of bottom line, if Auto-Wrap,
853 since that would scroll the whole frame on some terminals. */
855 if (AutoWrap (tty)
856 && curY (tty) + 1 == FRAME_LINES (f)
857 && (curX (tty) + len) == FRAME_COLS (f))
858 len --;
859 if (len <= 0)
860 return;
862 cmplus (tty, len);
864 /* If terminal_coding does any conversion, use it, otherwise use
865 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
866 because it always return 1 if the member src_multibyte is 1. */
867 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
868 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
869 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
870 the tail. */
871 coding->mode &= ~CODING_MODE_LAST_BLOCK;
873 /* Turn appearance modes of the face. */
874 tty_highlight_if_desired (tty);
875 turn_on_face (f, face_id);
877 coding->mode |= CODING_MODE_LAST_BLOCK;
878 conversion_buffer = encode_terminal_code (string, len, coding);
879 if (coding->produced > 0)
881 BLOCK_INPUT;
882 fwrite (conversion_buffer, 1, coding->produced, tty->output);
883 if (ferror (tty->output))
884 clearerr (tty->output);
885 if (tty->termscript)
886 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
887 UNBLOCK_INPUT;
890 /* Turn appearance modes off. */
891 turn_off_face (f, face_id);
892 tty_turn_off_highlight (tty);
894 cmcheckmagic (tty);
896 #endif
898 /* An implementation of insert_glyphs for termcap frames. */
900 static void
901 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
903 char *buf;
904 struct glyph *glyph = NULL;
905 unsigned char *conversion_buffer;
906 unsigned char space[1];
907 struct coding_system *coding;
909 struct tty_display_info *tty = FRAME_TTY (f);
911 if (tty->TS_ins_multi_chars)
913 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len);
914 OUTPUT1 (tty, buf);
915 xfree (buf);
916 if (start)
917 write_glyphs (f, start, len);
918 return;
921 tty_turn_on_insert (tty);
922 cmplus (tty, len);
924 if (! start)
925 space[0] = SPACEGLYPH;
927 /* If terminal_coding does any conversion, use it, otherwise use
928 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
929 because it always return 1 if the member src_multibyte is 1. */
930 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
931 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
932 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
933 the tail. */
934 coding->mode &= ~CODING_MODE_LAST_BLOCK;
936 while (len-- > 0)
938 OUTPUT1_IF (tty, tty->TS_ins_char);
939 if (!start)
941 conversion_buffer = space;
942 coding->produced = 1;
944 else
946 tty_highlight_if_desired (tty);
947 turn_on_face (f, start->face_id);
948 glyph = start;
949 ++start;
950 /* We must open sufficient space for a character which
951 occupies more than one column. */
952 while (len && CHAR_GLYPH_PADDING_P (*start))
954 OUTPUT1_IF (tty, tty->TS_ins_char);
955 start++, len--;
958 if (len <= 0)
959 /* This is the last glyph. */
960 coding->mode |= CODING_MODE_LAST_BLOCK;
962 conversion_buffer = encode_terminal_code (glyph, 1, coding);
965 if (coding->produced > 0)
967 BLOCK_INPUT;
968 fwrite (conversion_buffer, 1, coding->produced, tty->output);
969 if (ferror (tty->output))
970 clearerr (tty->output);
971 if (tty->termscript)
972 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
973 UNBLOCK_INPUT;
976 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
977 if (start)
979 turn_off_face (f, glyph->face_id);
980 tty_turn_off_highlight (tty);
984 cmcheckmagic (tty);
987 /* An implementation of delete_glyphs for termcap frames. */
989 static void
990 tty_delete_glyphs (struct frame *f, int n)
992 char *buf;
993 register int i;
995 struct tty_display_info *tty = FRAME_TTY (f);
997 if (tty->delete_in_insert_mode)
999 tty_turn_on_insert (tty);
1001 else
1003 tty_turn_off_insert (tty);
1004 OUTPUT_IF (tty, tty->TS_delete_mode);
1007 if (tty->TS_del_multi_chars)
1009 buf = tparam (tty->TS_del_multi_chars, 0, 0, n);
1010 OUTPUT1 (tty, buf);
1011 xfree (buf);
1013 else
1014 for (i = 0; i < n; i++)
1015 OUTPUT1 (tty, tty->TS_del_char);
1016 if (!tty->delete_in_insert_mode)
1017 OUTPUT_IF (tty, tty->TS_end_delete_mode);
1020 /* An implementation of ins_del_lines for termcap frames. */
1022 static void
1023 tty_ins_del_lines (struct frame *f, int vpos, int n)
1025 struct tty_display_info *tty = FRAME_TTY (f);
1026 char *multi = n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
1027 char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
1028 char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
1030 register int i = n > 0 ? n : -n;
1031 register char *buf;
1033 /* If the lines below the insertion are being pushed
1034 into the end of the window, this is the same as clearing;
1035 and we know the lines are already clear, since the matching
1036 deletion has already been done. So can ignore this. */
1037 /* If the lines below the deletion are blank lines coming
1038 out of the end of the window, don't bother,
1039 as there will be a matching inslines later that will flush them. */
1040 if (FRAME_SCROLL_REGION_OK (f)
1041 && vpos + i >= tty->specified_window)
1042 return;
1043 if (!FRAME_MEMORY_BELOW_FRAME (f)
1044 && vpos + i >= FRAME_LINES (f))
1045 return;
1047 if (multi)
1049 raw_cursor_to (f, vpos, 0);
1050 tty_background_highlight (tty);
1051 buf = tparam (multi, 0, 0, i);
1052 OUTPUT (tty, buf);
1053 xfree (buf);
1055 else if (single)
1057 raw_cursor_to (f, vpos, 0);
1058 tty_background_highlight (tty);
1059 while (--i >= 0)
1060 OUTPUT (tty, single);
1061 if (tty->TF_teleray)
1062 curX (tty) = 0;
1064 else
1066 tty_set_scroll_region (f, vpos, tty->specified_window);
1067 if (n < 0)
1068 raw_cursor_to (f, tty->specified_window - 1, 0);
1069 else
1070 raw_cursor_to (f, vpos, 0);
1071 tty_background_highlight (tty);
1072 while (--i >= 0)
1073 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1074 tty_set_scroll_region (f, 0, tty->specified_window);
1077 if (!FRAME_SCROLL_REGION_OK (f)
1078 && FRAME_MEMORY_BELOW_FRAME (f)
1079 && n < 0)
1081 cursor_to (f, FRAME_LINES (f) + n, 0);
1082 clear_to_end (f);
1086 /* Compute cost of sending "str", in characters,
1087 not counting any line-dependent padding. */
1090 string_cost (char *str)
1092 cost = 0;
1093 if (str)
1094 tputs (str, 0, evalcost);
1095 return cost;
1098 /* Compute cost of sending "str", in characters,
1099 counting any line-dependent padding at one line. */
1101 static int
1102 string_cost_one_line (char *str)
1104 cost = 0;
1105 if (str)
1106 tputs (str, 1, evalcost);
1107 return cost;
1110 /* Compute per line amount of line-dependent padding,
1111 in tenths of characters. */
1114 per_line_cost (char *str)
1116 cost = 0;
1117 if (str)
1118 tputs (str, 0, evalcost);
1119 cost = - cost;
1120 if (str)
1121 tputs (str, 10, evalcost);
1122 return cost;
1125 #ifndef old
1126 /* char_ins_del_cost[n] is cost of inserting N characters.
1127 char_ins_del_cost[-n] is cost of deleting N characters.
1128 The length of this vector is based on max_frame_cols. */
1130 int *char_ins_del_vector;
1132 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1133 #endif
1135 /* ARGSUSED */
1136 static void
1137 calculate_ins_del_char_costs (struct frame *f)
1139 struct tty_display_info *tty = FRAME_TTY (f);
1140 int ins_startup_cost, del_startup_cost;
1141 int ins_cost_per_char, del_cost_per_char;
1142 register int i;
1143 register int *p;
1145 if (tty->TS_ins_multi_chars)
1147 ins_cost_per_char = 0;
1148 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1150 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1151 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1153 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1154 + string_cost (tty->TS_end_insert_mode))) / 100;
1155 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1156 + string_cost_one_line (tty->TS_pad_inserted_char));
1158 else
1160 ins_startup_cost = 9999;
1161 ins_cost_per_char = 0;
1164 if (tty->TS_del_multi_chars)
1166 del_cost_per_char = 0;
1167 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1169 else if (tty->TS_del_char)
1171 del_startup_cost = (string_cost (tty->TS_delete_mode)
1172 + string_cost (tty->TS_end_delete_mode));
1173 if (tty->delete_in_insert_mode)
1174 del_startup_cost /= 2;
1175 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1177 else
1179 del_startup_cost = 9999;
1180 del_cost_per_char = 0;
1183 /* Delete costs are at negative offsets */
1184 p = &char_ins_del_cost (f)[0];
1185 for (i = FRAME_COLS (f); --i >= 0;)
1186 *--p = (del_startup_cost += del_cost_per_char);
1188 /* Doing nothing is free */
1189 p = &char_ins_del_cost (f)[0];
1190 *p++ = 0;
1192 /* Insert costs are at positive offsets */
1193 for (i = FRAME_COLS (f); --i >= 0;)
1194 *p++ = (ins_startup_cost += ins_cost_per_char);
1197 void
1198 calculate_costs (struct frame *frame)
1200 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1202 if (FRAME_TERMCAP_P (frame))
1204 struct tty_display_info *tty = FRAME_TTY (frame);
1205 register char *f = (tty->TS_set_scroll_region
1206 ? tty->TS_set_scroll_region
1207 : tty->TS_set_scroll_region_1);
1209 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1211 tty->costs_set = 1;
1213 /* These variables are only used for terminal stuff. They are
1214 allocated once for the terminal frame of X-windows emacs, but not
1215 used afterwards.
1217 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1218 X turns off char_ins_del_ok. */
1220 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1221 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1223 if (char_ins_del_vector != 0)
1224 char_ins_del_vector
1225 = (int *) xrealloc (char_ins_del_vector,
1226 (sizeof (int)
1227 + 2 * max_frame_cols * sizeof (int)));
1228 else
1229 char_ins_del_vector
1230 = (int *) xmalloc (sizeof (int)
1231 + 2 * max_frame_cols * sizeof (int));
1233 bzero (char_ins_del_vector, (sizeof (int)
1234 + 2 * max_frame_cols * sizeof (int)));
1237 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1238 do_line_insertion_deletion_costs (frame,
1239 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1240 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1241 f, f, 1);
1242 else
1243 do_line_insertion_deletion_costs (frame,
1244 tty->TS_ins_line, tty->TS_ins_multi_lines,
1245 tty->TS_del_line, tty->TS_del_multi_lines,
1246 0, 0, 1);
1248 calculate_ins_del_char_costs (frame);
1250 /* Don't use TS_repeat if its padding is worse than sending the chars */
1251 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1252 tty->RPov = string_cost (tty->TS_repeat);
1253 else
1254 tty->RPov = FRAME_COLS (frame) * 2;
1256 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1260 struct fkey_table {
1261 char *cap, *name;
1264 /* Termcap capability names that correspond directly to X keysyms.
1265 Some of these (marked "terminfo") aren't supplied by old-style
1266 (Berkeley) termcap entries. They're listed in X keysym order;
1267 except we put the keypad keys first, so that if they clash with
1268 other keys (as on the IBM PC keyboard) they get overridden.
1271 static struct fkey_table keys[] =
1273 {"kh", "home"}, /* termcap */
1274 {"kl", "left"}, /* termcap */
1275 {"ku", "up"}, /* termcap */
1276 {"kr", "right"}, /* termcap */
1277 {"kd", "down"}, /* termcap */
1278 {"%8", "prior"}, /* terminfo */
1279 {"%5", "next"}, /* terminfo */
1280 {"@7", "end"}, /* terminfo */
1281 {"@1", "begin"}, /* terminfo */
1282 {"*6", "select"}, /* terminfo */
1283 {"%9", "print"}, /* terminfo */
1284 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1286 * "insert" --- see below
1288 {"&8", "undo"}, /* terminfo */
1289 {"%0", "redo"}, /* terminfo */
1290 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1291 {"@0", "find"}, /* terminfo */
1292 {"@2", "cancel"}, /* terminfo */
1293 {"%1", "help"}, /* terminfo */
1295 * "break" goes here, but can't be reliably intercepted with termcap
1297 {"&4", "reset"}, /* terminfo --- actually `restart' */
1299 * "system" and "user" --- no termcaps
1301 {"kE", "clearline"}, /* terminfo */
1302 {"kA", "insertline"}, /* terminfo */
1303 {"kL", "deleteline"}, /* terminfo */
1304 {"kI", "insertchar"}, /* terminfo */
1305 {"kD", "deletechar"}, /* terminfo */
1306 {"kB", "backtab"}, /* terminfo */
1308 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1310 {"@8", "kp-enter"}, /* terminfo */
1312 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1313 * "kp-multiply", "kp-add", "kp-separator",
1314 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1315 * --- no termcaps for any of these.
1317 {"K4", "kp-1"}, /* terminfo */
1319 * "kp-2" --- no termcap
1321 {"K5", "kp-3"}, /* terminfo */
1323 * "kp-4" --- no termcap
1325 {"K2", "kp-5"}, /* terminfo */
1327 * "kp-6" --- no termcap
1329 {"K1", "kp-7"}, /* terminfo */
1331 * "kp-8" --- no termcap
1333 {"K3", "kp-9"}, /* terminfo */
1335 * "kp-equal" --- no termcap
1337 {"k1", "f1"},
1338 {"k2", "f2"},
1339 {"k3", "f3"},
1340 {"k4", "f4"},
1341 {"k5", "f5"},
1342 {"k6", "f6"},
1343 {"k7", "f7"},
1344 {"k8", "f8"},
1345 {"k9", "f9"},
1347 {"&0", "S-cancel"}, /*shifted cancel key*/
1348 {"&9", "S-begin"}, /*shifted begin key*/
1349 {"*0", "S-find"}, /*shifted find key*/
1350 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1351 {"*4", "S-delete"}, /*shifted delete-character key*/
1352 {"*7", "S-end"}, /*shifted end key*/
1353 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1354 {"#1", "S-help"}, /*shifted help key*/
1355 {"#2", "S-home"}, /*shifted home key*/
1356 {"#3", "S-insert"}, /*shifted insert-character key*/
1357 {"#4", "S-left"}, /*shifted left-arrow key*/
1358 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1359 {"%c", "S-next"}, /*shifted next key*/
1360 {"%e", "S-prior"}, /*shifted previous key*/
1361 {"%f", "S-print"}, /*shifted print key*/
1362 {"%g", "S-redo"}, /*shifted redo key*/
1363 {"%i", "S-right"}, /*shifted right-arrow key*/
1364 {"!3", "S-undo"} /*shifted undo key*/
1367 static char **term_get_fkeys_address;
1368 static KBOARD *term_get_fkeys_kboard;
1369 static Lisp_Object term_get_fkeys_1 ();
1371 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1372 This function scans the termcap function key sequence entries, and
1373 adds entries to Vinput_decode_map for each function key it finds. */
1375 static void
1376 term_get_fkeys (address, kboard)
1377 char **address;
1378 KBOARD *kboard;
1380 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1381 errors during the call. The only errors should be from Fdefine_key
1382 when given a key sequence containing an invalid prefix key. If the
1383 termcap defines function keys which use a prefix that is already bound
1384 to a command by the default bindings, we should silently ignore that
1385 function key specification, rather than giving the user an error and
1386 refusing to run at all on such a terminal. */
1388 extern Lisp_Object Fidentity ();
1389 term_get_fkeys_address = address;
1390 term_get_fkeys_kboard = kboard;
1391 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1394 static Lisp_Object
1395 term_get_fkeys_1 ()
1397 int i;
1399 char **address = term_get_fkeys_address;
1400 KBOARD *kboard = term_get_fkeys_kboard;
1402 /* This can happen if CANNOT_DUMP or with strange options. */
1403 if (!KEYMAPP (kboard->Vinput_decode_map))
1404 kboard->Vinput_decode_map = Fmake_sparse_keymap (Qnil);
1406 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1408 char *sequence = tgetstr (keys[i].cap, address);
1409 if (sequence)
1410 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
1411 Fmake_vector (make_number (1),
1412 intern (keys[i].name)));
1415 /* The uses of the "k0" capability are inconsistent; sometimes it
1416 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1417 We will attempt to politely accommodate both systems by testing for
1418 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1421 char *k_semi = tgetstr ("k;", address);
1422 char *k0 = tgetstr ("k0", address);
1423 char *k0_name = "f10";
1425 if (k_semi)
1427 if (k0)
1428 /* Define f0 first, so that f10 takes precedence in case the
1429 key sequences happens to be the same. */
1430 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
1431 Fmake_vector (make_number (1), intern ("f0")));
1432 Fdefine_key (kboard->Vinput_decode_map, build_string (k_semi),
1433 Fmake_vector (make_number (1), intern ("f10")));
1435 else if (k0)
1436 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
1437 Fmake_vector (make_number (1), intern (k0_name)));
1440 /* Set up cookies for numbered function keys above f10. */
1442 char fcap[3], fkey[4];
1444 fcap[0] = 'F'; fcap[2] = '\0';
1445 for (i = 11; i < 64; i++)
1447 if (i <= 19)
1448 fcap[1] = '1' + i - 11;
1449 else if (i <= 45)
1450 fcap[1] = 'A' + i - 20;
1451 else
1452 fcap[1] = 'a' + i - 46;
1455 char *sequence = tgetstr (fcap, address);
1456 if (sequence)
1458 sprintf (fkey, "f%d", i);
1459 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
1460 Fmake_vector (make_number (1),
1461 intern (fkey)));
1468 * Various mappings to try and get a better fit.
1471 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1472 if (!tgetstr (cap1, address)) \
1474 char *sequence = tgetstr (cap2, address); \
1475 if (sequence) \
1476 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence), \
1477 Fmake_vector (make_number (1), \
1478 intern (sym))); \
1481 /* if there's no key_next keycap, map key_npage to `next' keysym */
1482 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1483 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1484 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1485 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1486 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1487 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1488 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1490 /* IBM has their own non-standard dialect of terminfo.
1491 If the standard name isn't found, try the IBM name. */
1492 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1493 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1494 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1495 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1496 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1497 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1498 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1499 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1500 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1501 #undef CONDITIONAL_REASSIGN
1504 return Qnil;
1508 /***********************************************************************
1509 Character Display Information
1510 ***********************************************************************/
1512 /* Avoid name clash with functions defined in xterm.c */
1513 #ifdef static
1514 #define append_glyph append_glyph_term
1515 #define produce_stretch_glyph produce_stretch_glyph_term
1516 #define append_composite_glyph append_composite_glyph_term
1517 #define produce_composite_glyph produce_composite_glyph_term
1518 #endif
1520 static void append_glyph P_ ((struct it *));
1521 static void produce_stretch_glyph P_ ((struct it *));
1522 static void append_composite_glyph P_ ((struct it *));
1523 static void produce_composite_glyph P_ ((struct it *));
1525 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1526 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1527 the character for which to produce glyphs; IT->face_id contains the
1528 character's face. Padding glyphs are appended if IT->c has a
1529 IT->pixel_width > 1. */
1531 static void
1532 append_glyph (it)
1533 struct it *it;
1535 struct glyph *glyph, *end;
1536 int i;
1538 xassert (it->glyph_row);
1539 glyph = (it->glyph_row->glyphs[it->area]
1540 + it->glyph_row->used[it->area]);
1541 end = it->glyph_row->glyphs[1 + it->area];
1543 for (i = 0;
1544 i < it->pixel_width && glyph < end;
1545 ++i)
1547 glyph->type = CHAR_GLYPH;
1548 glyph->pixel_width = 1;
1549 glyph->u.ch = it->char_to_display;
1550 glyph->face_id = it->face_id;
1551 glyph->padding_p = i > 0;
1552 glyph->charpos = CHARPOS (it->position);
1553 glyph->object = it->object;
1555 ++it->glyph_row->used[it->area];
1556 ++glyph;
1561 /* Produce glyphs for the display element described by IT. *IT
1562 specifies what we want to produce a glyph for (character, image, ...),
1563 and where in the glyph matrix we currently are (glyph row and hpos).
1564 produce_glyphs fills in output fields of *IT with information such as the
1565 pixel width and height of a character, and maybe output actual glyphs at
1566 the same time if IT->glyph_row is non-null. For an overview, see
1567 the explanation in dispextern.h, before the definition of the
1568 display_element_type enumeration.
1570 produce_glyphs also stores the result of glyph width, ascent
1571 etc. computations in *IT.
1573 IT->glyph_row may be null, in which case produce_glyphs does not
1574 actually fill in the glyphs. This is used in the move_* functions
1575 in xdisp.c for text width and height computations.
1577 Callers usually don't call produce_glyphs directly;
1578 instead they use the macro PRODUCE_GLYPHS. */
1580 void
1581 produce_glyphs (it)
1582 struct it *it;
1584 /* If a hook is installed, let it do the work. */
1586 /* Nothing but characters are supported on terminal frames. */
1587 xassert (it->what == IT_CHARACTER
1588 || it->what == IT_COMPOSITION
1589 || it->what == IT_STRETCH);
1591 if (it->what == IT_STRETCH)
1593 produce_stretch_glyph (it);
1594 goto done;
1597 if (it->what == IT_COMPOSITION)
1599 produce_composite_glyph (it);
1600 goto done;
1603 /* Maybe translate single-byte characters to multibyte. */
1604 it->char_to_display = it->c;
1606 if (it->c >= 040 && it->c < 0177)
1608 it->pixel_width = it->nglyphs = 1;
1609 if (it->glyph_row)
1610 append_glyph (it);
1612 else if (it->c == '\n')
1613 it->pixel_width = it->nglyphs = 0;
1614 else if (it->c == '\t')
1616 int absolute_x = (it->current_x
1617 + it->continuation_lines_width);
1618 int next_tab_x
1619 = (((1 + absolute_x + it->tab_width - 1)
1620 / it->tab_width)
1621 * it->tab_width);
1622 int nspaces;
1624 /* If part of the TAB has been displayed on the previous line
1625 which is continued now, continuation_lines_width will have
1626 been incremented already by the part that fitted on the
1627 continued line. So, we will get the right number of spaces
1628 here. */
1629 nspaces = next_tab_x - absolute_x;
1631 if (it->glyph_row)
1633 int n = nspaces;
1635 it->char_to_display = ' ';
1636 it->pixel_width = it->len = 1;
1638 while (n--)
1639 append_glyph (it);
1642 it->pixel_width = nspaces;
1643 it->nglyphs = nspaces;
1645 else if (CHAR_BYTE8_P (it->c))
1647 if (unibyte_display_via_language_environment
1648 && (it->c >= 0240))
1650 it->char_to_display = BYTE8_TO_CHAR (it->c);
1651 it->pixel_width = CHAR_WIDTH (it->char_to_display);
1652 it->nglyphs = it->pixel_width;
1653 if (it->glyph_row)
1654 append_glyph (it);
1656 else
1658 /* Coming here means that it->c is from display table, thus
1659 we must send the raw 8-bit byte as is to the terminal.
1660 Although there's no way to know how many columns it
1661 occupies on a screen, it is a good assumption that a
1662 single byte code has 1-column width. */
1663 it->pixel_width = it->nglyphs = 1;
1664 if (it->glyph_row)
1665 append_glyph (it);
1668 else
1670 it->pixel_width = CHAR_WIDTH (it->c);
1671 it->nglyphs = it->pixel_width;
1673 if (it->glyph_row)
1674 append_glyph (it);
1677 done:
1678 /* Advance current_x by the pixel width as a convenience for
1679 the caller. */
1680 if (it->area == TEXT_AREA)
1681 it->current_x += it->pixel_width;
1682 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1683 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1687 /* Produce a stretch glyph for iterator IT. IT->object is the value
1688 of the glyph property displayed. The value must be a list
1689 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1690 being recognized:
1692 1. `:width WIDTH' specifies that the space should be WIDTH *
1693 canonical char width wide. WIDTH may be an integer or floating
1694 point number.
1696 2. `:align-to HPOS' specifies that the space should be wide enough
1697 to reach HPOS, a value in canonical character units. */
1699 static void
1700 produce_stretch_glyph (it)
1701 struct it *it;
1703 /* (space :width WIDTH ...) */
1704 Lisp_Object prop, plist;
1705 int width = 0, align_to = -1;
1706 int zero_width_ok_p = 0;
1707 double tem;
1709 /* List should start with `space'. */
1710 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1711 plist = XCDR (it->object);
1713 /* Compute the width of the stretch. */
1714 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1715 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1717 /* Absolute width `:width WIDTH' specified and valid. */
1718 zero_width_ok_p = 1;
1719 width = (int)(tem + 0.5);
1721 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1722 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1724 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1725 align_to = (align_to < 0
1727 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1728 else if (align_to < 0)
1729 align_to = window_box_left_offset (it->w, TEXT_AREA);
1730 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1731 zero_width_ok_p = 1;
1733 else
1734 /* Nothing specified -> width defaults to canonical char width. */
1735 width = FRAME_COLUMN_WIDTH (it->f);
1737 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1738 width = 1;
1740 if (width > 0 && it->line_wrap != TRUNCATE
1741 && it->current_x + width > it->last_visible_x)
1742 width = it->last_visible_x - it->current_x - 1;
1744 if (width > 0 && it->glyph_row)
1746 Lisp_Object o_object = it->object;
1747 Lisp_Object object = it->stack[it->sp - 1].string;
1748 int n = width;
1750 if (!STRINGP (object))
1751 object = it->w->buffer;
1752 it->object = object;
1753 it->char_to_display = ' ';
1754 it->pixel_width = it->len = 1;
1755 while (n--)
1756 append_glyph (it);
1757 it->object = o_object;
1759 it->pixel_width = width;
1760 it->nglyphs = width;
1764 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1765 Called from produce_composite_glyph for terminal frames if
1766 IT->glyph_row != NULL. IT->face_id contains the character's
1767 face. */
1769 static void
1770 append_composite_glyph (it)
1771 struct it *it;
1773 struct glyph *glyph;
1775 xassert (it->glyph_row);
1776 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1777 if (glyph < it->glyph_row->glyphs[1 + it->area])
1779 glyph->type = COMPOSITE_GLYPH;
1780 glyph->pixel_width = it->pixel_width;
1781 glyph->u.cmp.id = it->cmp_it.id;
1782 if (it->cmp_it.ch < 0)
1784 glyph->u.cmp.automatic = 0;
1785 glyph->u.cmp.id = it->cmp_it.id;
1787 else
1789 glyph->u.cmp.automatic = 1;
1790 glyph->u.cmp.id = it->cmp_it.id;
1791 glyph->u.cmp.from = it->cmp_it.from;
1792 glyph->u.cmp.to = it->cmp_it.to - 1;
1795 glyph->face_id = it->face_id;
1796 glyph->padding_p = 0;
1797 glyph->charpos = CHARPOS (it->position);
1798 glyph->object = it->object;
1800 ++it->glyph_row->used[it->area];
1801 ++glyph;
1806 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1807 the composition. We simply produces components of the composition
1808 assuming that the terminal has a capability to layout/render it
1809 correctly. */
1811 static void
1812 produce_composite_glyph (it)
1813 struct it *it;
1815 int c;
1817 if (it->cmp_it.ch < 0)
1819 struct composition *cmp = composition_table[it->cmp_it.id];
1821 it->pixel_width = cmp->width;
1823 else
1825 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
1827 it->pixel_width = composition_gstring_width (gstring, it->cmp_it.from,
1828 it->cmp_it.to, NULL);
1830 it->nglyphs = 1;
1831 if (it->glyph_row)
1832 append_composite_glyph (it);
1836 /* Get information about special display element WHAT in an
1837 environment described by IT. WHAT is one of IT_TRUNCATION or
1838 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1839 non-null glyph_row member. This function ensures that fields like
1840 face_id, c, len of IT are left untouched. */
1842 void
1843 produce_special_glyphs (it, what)
1844 struct it *it;
1845 enum display_element_type what;
1847 struct it temp_it;
1848 Lisp_Object gc;
1849 GLYPH glyph;
1851 temp_it = *it;
1852 temp_it.dp = NULL;
1853 temp_it.what = IT_CHARACTER;
1854 temp_it.len = 1;
1855 temp_it.object = make_number (0);
1856 bzero (&temp_it.current, sizeof temp_it.current);
1858 if (what == IT_CONTINUATION)
1860 /* Continuation glyph. */
1861 SET_GLYPH_FROM_CHAR (glyph, '\\');
1862 if (it->dp
1863 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc))
1864 && GLYPH_CODE_CHAR_VALID_P (gc))
1866 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1867 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1870 else if (what == IT_TRUNCATION)
1872 /* Truncation glyph. */
1873 SET_GLYPH_FROM_CHAR (glyph, '$');
1874 if (it->dp
1875 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc))
1876 && GLYPH_CODE_CHAR_VALID_P (gc))
1878 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1879 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1882 else
1883 abort ();
1885 temp_it.c = GLYPH_CHAR (glyph);
1886 temp_it.face_id = GLYPH_FACE (glyph);
1887 temp_it.len = CHAR_BYTES (temp_it.c);
1889 produce_glyphs (&temp_it);
1890 it->pixel_width = temp_it.pixel_width;
1891 it->nglyphs = temp_it.pixel_width;
1896 /***********************************************************************
1897 Faces
1898 ***********************************************************************/
1900 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1901 one of the enumerators from enum no_color_bit, or a bit set built
1902 from them. Some display attributes may not be used together with
1903 color; the termcap capability `NC' specifies which ones. */
1905 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1906 (tty->TN_max_colors > 0 \
1907 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1908 : 1)
1910 /* Turn appearances of face FACE_ID on tty frame F on.
1911 FACE_ID is a realized face ID number, in the face cache. */
1913 static void
1914 turn_on_face (f, face_id)
1915 struct frame *f;
1916 int face_id;
1918 struct face *face = FACE_FROM_ID (f, face_id);
1919 long fg = face->foreground;
1920 long bg = face->background;
1921 struct tty_display_info *tty = FRAME_TTY (f);
1923 /* Do this first because TS_end_standout_mode may be the same
1924 as TS_exit_attribute_mode, which turns all appearances off. */
1925 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1927 if (tty->TN_max_colors > 0)
1929 if (fg >= 0 && bg >= 0)
1931 /* If the terminal supports colors, we can set them
1932 below without using reverse video. The face's fg
1933 and bg colors are set as they should appear on
1934 the screen, i.e. they take the inverse-video'ness
1935 of the face already into account. */
1937 else if (inverse_video)
1939 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1940 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1941 tty_toggle_highlight (tty);
1943 else
1945 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1946 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1947 tty_toggle_highlight (tty);
1950 else
1952 /* If we can't display colors, use reverse video
1953 if the face specifies that. */
1954 if (inverse_video)
1956 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1957 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1958 tty_toggle_highlight (tty);
1960 else
1962 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1963 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1964 tty_toggle_highlight (tty);
1969 if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1970 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
1972 if (face->tty_dim_p && MAY_USE_WITH_COLORS_P (tty, NC_DIM))
1973 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
1975 /* Alternate charset and blinking not yet used. */
1976 if (face->tty_alt_charset_p
1977 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
1978 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
1980 if (face->tty_blinking_p
1981 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
1982 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
1984 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
1985 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
1987 if (tty->TN_max_colors > 0)
1989 char *ts, *p;
1991 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
1992 if (fg >= 0 && ts)
1994 p = tparam (ts, NULL, 0, (int) fg);
1995 OUTPUT (tty, p);
1996 xfree (p);
1999 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
2000 if (bg >= 0 && ts)
2002 p = tparam (ts, NULL, 0, (int) bg);
2003 OUTPUT (tty, p);
2004 xfree (p);
2010 /* Turn off appearances of face FACE_ID on tty frame F. */
2012 static void
2013 turn_off_face (f, face_id)
2014 struct frame *f;
2015 int face_id;
2017 struct face *face = FACE_FROM_ID (f, face_id);
2018 struct tty_display_info *tty = FRAME_TTY (f);
2020 xassert (face != NULL);
2022 if (tty->TS_exit_attribute_mode)
2024 /* Capability "me" will turn off appearance modes double-bright,
2025 half-bright, reverse-video, standout, underline. It may or
2026 may not turn off alt-char-mode. */
2027 if (face->tty_bold_p
2028 || face->tty_dim_p
2029 || face->tty_reverse_p
2030 || face->tty_alt_charset_p
2031 || face->tty_blinking_p
2032 || face->tty_underline_p)
2034 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
2035 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
2036 tty->standout_mode = 0;
2039 if (face->tty_alt_charset_p)
2040 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2042 else
2044 /* If we don't have "me" we can only have those appearances
2045 that have exit sequences defined. */
2046 if (face->tty_alt_charset_p)
2047 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2049 if (face->tty_underline_p)
2050 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
2053 /* Switch back to default colors. */
2054 if (tty->TN_max_colors > 0
2055 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2056 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2057 || (face->background != FACE_TTY_DEFAULT_COLOR
2058 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
2059 OUTPUT1_IF (tty, tty->TS_orig_pair);
2063 /* Return non-zero if the terminal on frame F supports all of the
2064 capabilities in CAPS simultaneously, with foreground and background
2065 colors FG and BG. */
2068 tty_capable_p (tty, caps, fg, bg)
2069 struct tty_display_info *tty;
2070 unsigned caps;
2071 unsigned long fg, bg;
2073 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2074 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2075 return 0;
2077 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2078 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2079 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2080 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
2081 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
2082 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
2084 /* We can do it! */
2085 return 1;
2088 /* Return non-zero if the terminal is capable to display colors. */
2090 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2091 0, 1, 0,
2092 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2094 TERMINAL can be a terminal object, a frame, or nil (meaning the
2095 selected frame's terminal). This function always returns nil if
2096 TERMINAL does not refer to a text-only terminal. */)
2097 (terminal)
2098 Lisp_Object terminal;
2100 struct terminal *t = get_tty_terminal (terminal, 0);
2101 if (!t)
2102 return Qnil;
2103 else
2104 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
2107 /* Return the number of supported colors. */
2108 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2109 Stty_display_color_cells, 0, 1, 0,
2110 doc: /* Return the number of colors supported by the tty device TERMINAL.
2112 TERMINAL can be a terminal object, a frame, or nil (meaning the
2113 selected frame's terminal). This function always returns 0 if
2114 TERMINAL does not refer to a text-only terminal. */)
2115 (terminal)
2116 Lisp_Object terminal;
2118 struct terminal *t = get_tty_terminal (terminal, 0);
2119 if (!t)
2120 return make_number (0);
2121 else
2122 return make_number (t->display_info.tty->TN_max_colors);
2125 #ifndef DOS_NT
2127 /* Declare here rather than in the function, as in the rest of Emacs,
2128 to work around an HPUX compiler bug (?). See
2129 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2130 static int default_max_colors;
2131 static int default_max_pairs;
2132 static int default_no_color_video;
2133 static char *default_orig_pair;
2134 static char *default_set_foreground;
2135 static char *default_set_background;
2137 /* Save or restore the default color-related capabilities of this
2138 terminal. */
2139 static void
2140 tty_default_color_capabilities (struct tty_display_info *tty, int save)
2143 if (save)
2145 xfree (default_orig_pair);
2146 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
2148 xfree (default_set_foreground);
2149 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
2150 : NULL;
2152 xfree (default_set_background);
2153 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
2154 : NULL;
2156 default_max_colors = tty->TN_max_colors;
2157 default_max_pairs = tty->TN_max_pairs;
2158 default_no_color_video = tty->TN_no_color_video;
2160 else
2162 tty->TS_orig_pair = default_orig_pair;
2163 tty->TS_set_foreground = default_set_foreground;
2164 tty->TS_set_background = default_set_background;
2165 tty->TN_max_colors = default_max_colors;
2166 tty->TN_max_pairs = default_max_pairs;
2167 tty->TN_no_color_video = default_no_color_video;
2171 /* Setup one of the standard tty color schemes according to MODE.
2172 MODE's value is generally the number of colors which we want to
2173 support; zero means set up for the default capabilities, the ones
2174 we saw at init_tty time; -1 means turn off color support. */
2175 static void
2176 tty_setup_colors (struct tty_display_info *tty, int mode)
2178 /* Canonicalize all negative values of MODE. */
2179 if (mode < -1)
2180 mode = -1;
2182 switch (mode)
2184 case -1: /* no colors at all */
2185 tty->TN_max_colors = 0;
2186 tty->TN_max_pairs = 0;
2187 tty->TN_no_color_video = 0;
2188 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2189 break;
2190 case 0: /* default colors, if any */
2191 default:
2192 tty_default_color_capabilities (tty, 0);
2193 break;
2194 case 8: /* 8 standard ANSI colors */
2195 tty->TS_orig_pair = "\033[0m";
2196 #ifdef TERMINFO
2197 tty->TS_set_foreground = "\033[3%p1%dm";
2198 tty->TS_set_background = "\033[4%p1%dm";
2199 #else
2200 tty->TS_set_foreground = "\033[3%dm";
2201 tty->TS_set_background = "\033[4%dm";
2202 #endif
2203 tty->TN_max_colors = 8;
2204 tty->TN_max_pairs = 64;
2205 tty->TN_no_color_video = 0;
2206 break;
2210 void
2211 set_tty_color_mode (tty, f)
2212 struct tty_display_info *tty;
2213 struct frame *f;
2215 Lisp_Object tem, val, color_mode_spec;
2216 Lisp_Object color_mode;
2217 int mode;
2218 extern Lisp_Object Qtty_color_mode;
2219 Lisp_Object tty_color_mode_alist
2220 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
2222 tem = assq_no_quit (Qtty_color_mode, f->param_alist);
2223 val = CONSP (tem) ? XCDR (tem) : Qnil;
2225 if (INTEGERP (val))
2226 color_mode = val;
2227 else
2229 tem = (NILP (tty_color_mode_alist) ? Qnil
2230 : Fassq (val, XSYMBOL (tty_color_mode_alist)->value));
2231 color_mode = CONSP (tem) ? XCDR (tem) : Qnil;
2234 mode = INTEGERP (color_mode) ? XINT (color_mode) : 0;
2236 if (mode != tty->previous_color_mode)
2238 Lisp_Object funsym = intern ("tty-set-up-initial-frame-faces");
2239 tty->previous_color_mode = mode;
2240 tty_setup_colors (tty , mode);
2241 /* This recomputes all the faces given the new color definitions. */
2242 safe_call (1, &funsym);
2246 #endif /* !DOS_NT */
2250 /* Return the tty display object specified by TERMINAL. */
2252 struct terminal *
2253 get_tty_terminal (Lisp_Object terminal, int throw)
2255 struct terminal *t = get_terminal (terminal, throw);
2257 if (t && t->type != output_termcap && t->type != output_msdos_raw)
2259 if (throw)
2260 error ("Device %d is not a termcap terminal device", t->id);
2261 else
2262 return NULL;
2265 return t;
2268 /* Return an active termcap device that uses the tty device with the
2269 given name.
2271 This function ignores suspended devices.
2273 Returns NULL if the named terminal device is not opened. */
2275 struct terminal *
2276 get_named_tty (name)
2277 char *name;
2279 struct terminal *t;
2281 if (!name)
2282 abort ();
2284 for (t = terminal_list; t; t = t->next_terminal)
2286 if ((t->type == output_termcap || t->type == output_msdos_raw)
2287 && !strcmp (t->display_info.tty->name, name)
2288 && TERMINAL_ACTIVE_P (t))
2289 return t;
2292 return 0;
2296 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2297 doc: /* Return the type of the tty device that TERMINAL uses.
2298 Returns nil if TERMINAL is not on a tty device.
2300 TERMINAL can be a terminal object, a frame, or nil (meaning the
2301 selected frame's terminal). */)
2302 (terminal)
2303 Lisp_Object terminal;
2305 struct terminal *t = get_terminal (terminal, 1);
2307 if (t->type != output_termcap && t->type != output_msdos_raw)
2308 return Qnil;
2310 if (t->display_info.tty->type)
2311 return build_string (t->display_info.tty->type);
2312 else
2313 return Qnil;
2316 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2317 doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2319 TERMINAL can be a terminal object, a frame, or nil (meaning the
2320 selected frame's terminal). This function always returns nil if
2321 TERMINAL is not on a tty device. */)
2322 (terminal)
2323 Lisp_Object terminal;
2325 struct terminal *t = get_terminal (terminal, 1);
2327 if ((t->type != output_termcap && t->type != output_msdos_raw)
2328 || strcmp (t->display_info.tty->name, DEV_TTY) != 0)
2329 return Qnil;
2330 else
2331 return Qt;
2334 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2335 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2336 This is used to override the terminfo data, for certain terminals that
2337 do not really do underlining, but say that they do. This function has
2338 no effect if used on a non-tty terminal.
2340 TERMINAL can be a terminal object, a frame or nil (meaning the
2341 selected frame's terminal). This function always returns nil if
2342 TERMINAL does not refer to a text-only terminal. */)
2343 (terminal)
2344 Lisp_Object terminal;
2346 struct terminal *t = get_terminal (terminal, 1);
2348 if (t->type == output_termcap)
2349 t->display_info.tty->TS_enter_underline_mode = 0;
2350 return Qnil;
2355 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2356 doc: /* Suspend the terminal device TTY.
2358 The device is restored to its default state, and Emacs ceases all
2359 access to the tty device. Frames that use the device are not deleted,
2360 but input is not read from them and if they change, their display is
2361 not updated.
2363 TTY may be a terminal object, a frame, or nil for the terminal device
2364 of the currently selected frame.
2366 This function runs `suspend-tty-functions' after suspending the
2367 device. The functions are run with one arg, the id of the suspended
2368 terminal device.
2370 `suspend-tty' does nothing if it is called on a device that is already
2371 suspended.
2373 A suspended tty may be resumed by calling `resume-tty' on it. */)
2374 (tty)
2375 Lisp_Object tty;
2377 struct terminal *t = get_tty_terminal (tty, 1);
2378 FILE *f;
2380 if (!t)
2381 error ("Unknown tty device");
2383 f = t->display_info.tty->input;
2385 if (f)
2387 /* First run `suspend-tty-functions' and then clean up the tty
2388 state because `suspend-tty-functions' might need to change
2389 the tty state. */
2390 if (!NILP (Vrun_hooks))
2392 Lisp_Object args[2];
2393 args[0] = intern ("suspend-tty-functions");
2394 XSETTERMINAL (args[1], t);
2395 Frun_hook_with_args (2, args);
2398 reset_sys_modes (t->display_info.tty);
2400 #ifdef subprocesses
2401 delete_keyboard_wait_descriptor (fileno (f));
2402 #endif
2404 #ifndef MSDOS
2405 fclose (f);
2406 if (f != t->display_info.tty->output)
2407 fclose (t->display_info.tty->output);
2408 #endif
2410 t->display_info.tty->input = 0;
2411 t->display_info.tty->output = 0;
2413 if (FRAMEP (t->display_info.tty->top_frame))
2414 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2418 /* Clear display hooks to prevent further output. */
2419 clear_tty_hooks (t);
2421 return Qnil;
2424 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2425 doc: /* Resume the previously suspended terminal device TTY.
2426 The terminal is opened and reinitialized. Frames that are on the
2427 suspended terminal are revived.
2429 It is an error to resume a terminal while another terminal is active
2430 on the same device.
2432 This function runs `resume-tty-functions' after resuming the terminal.
2433 The functions are run with one arg, the id of the resumed terminal
2434 device.
2436 `resume-tty' does nothing if it is called on a device that is not
2437 suspended.
2439 TTY may be a terminal object, a frame, or nil (meaning the selected
2440 frame's terminal). */)
2441 (tty)
2442 Lisp_Object tty;
2444 struct terminal *t = get_tty_terminal (tty, 1);
2445 int fd;
2447 if (!t)
2448 error ("Unknown tty device");
2450 if (!t->display_info.tty->input)
2452 if (get_named_tty (t->display_info.tty->name))
2453 error ("Cannot resume display while another display is active on the same device");
2455 #ifdef MSDOS
2456 t->display_info.tty->output = stdout;
2457 t->display_info.tty->input = stdin;
2458 #else /* !MSDOS */
2459 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2461 if (fd == -1)
2462 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2464 if (strcmp (t->display_info.tty->name, DEV_TTY))
2465 dissociate_if_controlling_tty (fd);
2467 t->display_info.tty->output = fdopen (fd, "w+");
2468 t->display_info.tty->input = t->display_info.tty->output;
2469 #endif
2471 #ifdef subprocesses
2472 add_keyboard_wait_descriptor (fd);
2473 #endif
2475 if (FRAMEP (t->display_info.tty->top_frame))
2477 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2478 int width, height;
2479 int old_height = FRAME_COLS (f);
2480 int old_width = FRAME_LINES (f);
2482 /* Check if terminal/window size has changed while the frame
2483 was suspended. */
2484 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2485 if (width != old_width || height != old_height)
2486 change_frame_size (f, height, width, 0, 0, 0);
2487 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2490 init_sys_modes (t->display_info.tty);
2492 /* Run `resume-tty-functions'. */
2493 if (!NILP (Vrun_hooks))
2495 Lisp_Object args[2];
2496 args[0] = intern ("resume-tty-functions");
2497 XSETTERMINAL (args[1], t);
2498 Frun_hook_with_args (2, args);
2502 set_tty_hooks (t);
2504 return Qnil;
2508 /***********************************************************************
2509 Mouse
2510 ***********************************************************************/
2512 #ifdef HAVE_GPM
2513 void
2514 term_mouse_moveto (int x, int y)
2516 /* TODO: how to set mouse position?
2517 const char *name;
2518 int fd;
2519 name = (const char *) ttyname (0);
2520 fd = open (name, O_WRONLY);
2521 SOME_FUNCTION (x, y, fd);
2522 close (fd);
2523 last_mouse_x = x;
2524 last_mouse_y = y; */
2527 static void
2528 term_show_mouse_face (enum draw_glyphs_face draw)
2530 struct window *w = XWINDOW (mouse_face_window);
2531 int save_x, save_y;
2532 int i;
2534 struct frame *f = XFRAME (w->frame);
2535 struct tty_display_info *tty = FRAME_TTY (f);
2537 if (/* If window is in the process of being destroyed, don't bother
2538 to do anything. */
2539 w->current_matrix != NULL
2540 /* Recognize when we are called to operate on rows that don't exist
2541 anymore. This can happen when a window is split. */
2542 && mouse_face_end_row < w->current_matrix->nrows)
2544 /* write_glyphs writes at cursor position, so we need to
2545 temporarily move cursor coordinates to the beginning of
2546 the highlight region. */
2548 /* Save current cursor co-ordinates */
2549 save_y = curY (tty);
2550 save_x = curX (tty);
2552 /* Note that mouse_face_beg_row etc. are window relative. */
2553 for (i = mouse_face_beg_row; i <= mouse_face_end_row; i++)
2555 int start_hpos, end_hpos, nglyphs;
2556 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
2558 /* Don't do anything if row doesn't have valid contents. */
2559 if (!row->enabled_p)
2560 continue;
2562 /* For all but the first row, the highlight starts at column 0. */
2563 if (i == mouse_face_beg_row)
2564 start_hpos = mouse_face_beg_col;
2565 else
2566 start_hpos = 0;
2568 if (i == mouse_face_end_row)
2569 end_hpos = mouse_face_end_col;
2570 else
2572 end_hpos = row->used[TEXT_AREA];
2573 if (draw == DRAW_NORMAL_TEXT)
2574 row->fill_line_p = 1; /* Clear to end of line */
2577 if (end_hpos <= start_hpos)
2578 continue;
2579 /* Record that some glyphs of this row are displayed in
2580 mouse-face. */
2581 row->mouse_face_p = draw > 0;
2583 nglyphs = end_hpos - start_hpos;
2585 if (end_hpos >= row->used[TEXT_AREA])
2586 nglyphs = row->used[TEXT_AREA] - start_hpos;
2588 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2589 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos
2590 + WINDOW_LEFT_EDGE_X (w);
2592 cursor_to (f, pos_y, pos_x);
2594 if (draw == DRAW_MOUSE_FACE)
2596 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2597 nglyphs, mouse_face_face_id);
2599 else /* draw == DRAW_NORMAL_TEXT */
2600 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
2602 cursor_to (f, save_y, save_x);
2606 static void
2607 term_clear_mouse_face ()
2609 if (!NILP (mouse_face_window))
2610 term_show_mouse_face (DRAW_NORMAL_TEXT);
2612 mouse_face_beg_row = mouse_face_beg_col = -1;
2613 mouse_face_end_row = mouse_face_end_col = -1;
2614 mouse_face_window = Qnil;
2617 /* Find the glyph matrix position of buffer position POS in window W.
2618 *HPOS and *VPOS are set to the positions found. W's current glyphs
2619 must be up to date. If POS is above window start return (0, 0).
2620 If POS is after end of W, return end of last line in W.
2621 - taken from msdos.c */
2622 static int
2623 fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
2625 int i, lastcol, line_start_position, maybe_next_line_p = 0;
2626 int yb = window_text_bottom_y (w);
2627 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0), *best_row = row;
2629 while (row->y < yb)
2631 if (row->used[TEXT_AREA])
2632 line_start_position = row->glyphs[TEXT_AREA]->charpos;
2633 else
2634 line_start_position = 0;
2636 if (line_start_position > pos)
2637 break;
2638 /* If the position sought is the end of the buffer,
2639 don't include the blank lines at the bottom of the window. */
2640 else if (line_start_position == pos
2641 && pos == BUF_ZV (XBUFFER (w->buffer)))
2643 maybe_next_line_p = 1;
2644 break;
2646 else if (line_start_position > 0)
2647 best_row = row;
2649 /* Don't overstep the last matrix row, lest we get into the
2650 never-never land... */
2651 if (row->y + 1 >= yb)
2652 break;
2654 ++row;
2657 /* Find the right column within BEST_ROW. */
2658 lastcol = 0;
2659 row = best_row;
2660 for (i = 0; i < row->used[TEXT_AREA]; i++)
2662 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
2663 int charpos;
2665 charpos = glyph->charpos;
2666 if (charpos == pos)
2668 *hpos = i;
2669 *vpos = row->y;
2670 return 1;
2672 else if (charpos > pos)
2673 break;
2674 else if (charpos > 0)
2675 lastcol = i;
2678 /* If we're looking for the end of the buffer,
2679 and we didn't find it in the line we scanned,
2680 use the start of the following line. */
2681 if (maybe_next_line_p)
2683 ++row;
2684 lastcol = 0;
2687 *vpos = row->y;
2688 *hpos = lastcol + 1;
2689 return 0;
2692 static void
2693 term_mouse_highlight (struct frame *f, int x, int y)
2695 enum window_part part;
2696 Lisp_Object window;
2697 struct window *w;
2698 struct buffer *b;
2700 if (NILP (Vmouse_highlight)
2701 || !f->glyphs_initialized_p)
2702 return;
2704 /* Which window is that in? */
2705 window = window_from_coordinates (f, x, y, &part, &x, &y, 0);
2707 /* Not on a window -> return. */
2708 if (!WINDOWP (window))
2709 return;
2711 if (!EQ (window, mouse_face_window))
2712 term_clear_mouse_face ();
2714 w = XWINDOW (window);
2716 /* Are we in a window whose display is up to date?
2717 And verify the buffer's text has not changed. */
2718 b = XBUFFER (w->buffer);
2719 if (part == ON_TEXT
2720 && EQ (w->window_end_valid, w->buffer)
2721 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
2722 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
2724 int pos, i, nrows = w->current_matrix->nrows;
2725 struct glyph_row *row;
2726 struct glyph *glyph;
2728 /* Find the glyph under X/Y. */
2729 glyph = NULL;
2730 if (y >= 0 && y < nrows)
2732 row = MATRIX_ROW (w->current_matrix, y);
2733 /* Give up if some row before the one we are looking for is
2734 not enabled. */
2735 for (i = 0; i <= y; i++)
2736 if (!MATRIX_ROW (w->current_matrix, i)->enabled_p)
2737 break;
2738 if (i > y /* all rows upto and including the one at Y are enabled */
2739 && row->displays_text_p
2740 && x < window_box_width (w, TEXT_AREA))
2742 glyph = row->glyphs[TEXT_AREA];
2743 if (x >= row->used[TEXT_AREA])
2744 glyph = NULL;
2745 else
2747 glyph += x;
2748 if (!BUFFERP (glyph->object))
2749 glyph = NULL;
2754 /* Clear mouse face if X/Y not over text. */
2755 if (glyph == NULL)
2757 term_clear_mouse_face ();
2758 return;
2761 if (!BUFFERP (glyph->object))
2762 abort ();
2763 pos = glyph->charpos;
2765 /* Check for mouse-face. */
2767 extern Lisp_Object Qmouse_face;
2768 Lisp_Object mouse_face, overlay, position, *overlay_vec;
2769 int noverlays, obegv, ozv;
2770 struct buffer *obuf;
2772 /* If we get an out-of-range value, return now; avoid an error. */
2773 if (pos > BUF_Z (b))
2774 return;
2776 /* Make the window's buffer temporarily current for
2777 overlays_at and compute_char_face. */
2778 obuf = current_buffer;
2779 current_buffer = b;
2780 obegv = BEGV;
2781 ozv = ZV;
2782 BEGV = BEG;
2783 ZV = Z;
2785 /* Is this char mouse-active? */
2786 XSETINT (position, pos);
2788 /* Put all the overlays we want in a vector in overlay_vec. */
2789 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
2790 /* Sort overlays into increasing priority order. */
2791 noverlays = sort_overlays (overlay_vec, noverlays, w);
2793 /* Check mouse-face highlighting. */
2794 if (!(EQ (window, mouse_face_window)
2795 && y >= mouse_face_beg_row
2796 && y <= mouse_face_end_row
2797 && (y > mouse_face_beg_row
2798 || x >= mouse_face_beg_col)
2799 && (y < mouse_face_end_row
2800 || x < mouse_face_end_col
2801 || mouse_face_past_end)))
2803 /* Clear the display of the old active region, if any. */
2804 term_clear_mouse_face ();
2806 /* Find the highest priority overlay that has a mouse-face
2807 property. */
2808 overlay = Qnil;
2809 for (i = noverlays - 1; i >= 0; --i)
2811 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
2812 if (!NILP (mouse_face))
2814 overlay = overlay_vec[i];
2815 break;
2819 /* If no overlay applies, get a text property. */
2820 if (NILP (overlay))
2821 mouse_face = Fget_text_property (position, Qmouse_face,
2822 w->buffer);
2824 /* Handle the overlay case. */
2825 if (!NILP (overlay))
2827 /* Find the range of text around this char that
2828 should be active. */
2829 Lisp_Object before, after;
2830 EMACS_INT ignore;
2833 before = Foverlay_start (overlay);
2834 after = Foverlay_end (overlay);
2835 /* Record this as the current active region. */
2836 fast_find_position (w, XFASTINT (before),
2837 &mouse_face_beg_col,
2838 &mouse_face_beg_row);
2840 mouse_face_past_end
2841 = !fast_find_position (w, XFASTINT (after),
2842 &mouse_face_end_col,
2843 &mouse_face_end_row);
2844 mouse_face_window = window;
2846 mouse_face_face_id
2847 = face_at_buffer_position (w, pos, 0, 0,
2848 &ignore, pos + 1, 1, -1);
2850 /* Display it as active. */
2851 term_show_mouse_face (DRAW_MOUSE_FACE);
2853 /* Handle the text property case. */
2854 else if (!NILP (mouse_face))
2856 /* Find the range of text around this char that
2857 should be active. */
2858 Lisp_Object before, after, beginning, end;
2859 EMACS_INT ignore;
2861 beginning = Fmarker_position (w->start);
2862 XSETINT (end, (BUF_Z (b) - XFASTINT (w->window_end_pos)));
2863 before
2864 = Fprevious_single_property_change (make_number (pos + 1),
2865 Qmouse_face,
2866 w->buffer, beginning);
2867 after
2868 = Fnext_single_property_change (position, Qmouse_face,
2869 w->buffer, end);
2871 /* Record this as the current active region. */
2872 fast_find_position (w, XFASTINT (before),
2873 &mouse_face_beg_col,
2874 &mouse_face_beg_row);
2875 mouse_face_past_end
2876 = !fast_find_position (w, XFASTINT (after),
2877 &mouse_face_end_col,
2878 &mouse_face_end_row);
2879 mouse_face_window = window;
2881 mouse_face_face_id
2882 = face_at_buffer_position (w, pos, 0, 0,
2883 &ignore, pos + 1, 1, -1);
2885 /* Display it as active. */
2886 term_show_mouse_face (DRAW_MOUSE_FACE);
2890 /* Look for a `help-echo' property. */
2892 Lisp_Object help;
2893 extern Lisp_Object Qhelp_echo;
2895 /* Check overlays first. */
2896 help = Qnil;
2897 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
2899 overlay = overlay_vec[i];
2900 help = Foverlay_get (overlay, Qhelp_echo);
2903 if (!NILP (help))
2905 help_echo_string = help;
2906 help_echo_window = window;
2907 help_echo_object = overlay;
2908 help_echo_pos = pos;
2910 /* Try text properties. */
2911 else if (NILP (help)
2912 && ((STRINGP (glyph->object)
2913 && glyph->charpos >= 0
2914 && glyph->charpos < SCHARS (glyph->object))
2915 || (BUFFERP (glyph->object)
2916 && glyph->charpos >= BEGV
2917 && glyph->charpos < ZV)))
2919 help = Fget_text_property (make_number (glyph->charpos),
2920 Qhelp_echo, glyph->object);
2921 if (!NILP (help))
2923 help_echo_string = help;
2924 help_echo_window = window;
2925 help_echo_object = glyph->object;
2926 help_echo_pos = glyph->charpos;
2931 BEGV = obegv;
2932 ZV = ozv;
2933 current_buffer = obuf;
2938 static int
2939 term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2941 /* Has the mouse moved off the glyph it was on at the last sighting? */
2942 if (event->x != last_mouse_x || event->y != last_mouse_y)
2944 frame->mouse_moved = 1;
2945 term_mouse_highlight (frame, event->x, event->y);
2946 /* Remember which glyph we're now on. */
2947 last_mouse_x = event->x;
2948 last_mouse_y = event->y;
2949 return 1;
2951 return 0;
2954 /* Return the current position of the mouse.
2956 Set *f to the frame the mouse is in, or zero if the mouse is in no
2957 Emacs frame. If it is set to zero, all the other arguments are
2958 garbage.
2960 Set *bar_window to Qnil, and *x and *y to the column and
2961 row of the character cell the mouse is over.
2963 Set *time to the time the mouse was at the returned position.
2965 This clears mouse_moved until the next motion
2966 event arrives. */
2967 static void
2968 term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2969 enum scroll_bar_part *part, Lisp_Object *x,
2970 Lisp_Object *y, unsigned long *time)
2972 struct timeval now;
2974 *fp = SELECTED_FRAME ();
2975 (*fp)->mouse_moved = 0;
2977 *bar_window = Qnil;
2978 *part = 0;
2980 XSETINT (*x, last_mouse_x);
2981 XSETINT (*y, last_mouse_y);
2982 gettimeofday(&now, 0);
2983 *time = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2986 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2988 If the event is a button press, then note that we have grabbed
2989 the mouse. */
2991 static Lisp_Object
2992 term_mouse_click (struct input_event *result, Gpm_Event *event,
2993 struct frame *f)
2995 struct timeval now;
2996 int i, j;
2998 result->kind = GPM_CLICK_EVENT;
2999 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
3001 if (event->buttons & j) {
3002 result->code = i; /* button number */
3003 break;
3006 gettimeofday(&now, 0);
3007 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
3009 if (event->type & GPM_UP)
3010 result->modifiers = up_modifier;
3011 else if (event->type & GPM_DOWN)
3012 result->modifiers = down_modifier;
3013 else
3014 result->modifiers = 0;
3016 if (event->type & GPM_SINGLE)
3017 result->modifiers |= click_modifier;
3019 if (event->type & GPM_DOUBLE)
3020 result->modifiers |= double_modifier;
3022 if (event->type & GPM_TRIPLE)
3023 result->modifiers |= triple_modifier;
3025 if (event->type & GPM_DRAG)
3026 result->modifiers |= drag_modifier;
3028 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
3030 /* 1 << KG_SHIFT */
3031 if (event->modifiers & (1 << 0))
3032 result->modifiers |= shift_modifier;
3034 /* 1 << KG_CTRL */
3035 if (event->modifiers & (1 << 2))
3036 result->modifiers |= ctrl_modifier;
3038 /* 1 << KG_ALT || KG_ALTGR */
3039 if (event->modifiers & (1 << 3)
3040 || event->modifiers & (1 << 1))
3041 result->modifiers |= meta_modifier;
3044 XSETINT (result->x, event->x);
3045 XSETINT (result->y, event->y);
3046 XSETFRAME (result->frame_or_window, f);
3047 result->arg = Qnil;
3048 return Qnil;
3052 handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
3054 struct frame *f = XFRAME (tty->top_frame);
3055 struct input_event ie;
3056 int do_help = 0;
3057 int count = 0;
3059 EVENT_INIT (ie);
3060 ie.kind = NO_EVENT;
3061 ie.arg = Qnil;
3063 if (event->type & (GPM_MOVE | GPM_DRAG)) {
3064 previous_help_echo_string = help_echo_string;
3065 help_echo_string = Qnil;
3067 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
3069 if (!term_mouse_movement (f, event))
3070 help_echo_string = previous_help_echo_string;
3072 /* If the contents of the global variable help_echo_string
3073 has changed, generate a HELP_EVENT. */
3074 if (!NILP (help_echo_string)
3075 || !NILP (previous_help_echo_string))
3076 do_help = 1;
3078 goto done;
3080 else {
3081 f->mouse_moved = 0;
3082 term_mouse_click (&ie, event, f);
3085 done:
3086 if (ie.kind != NO_EVENT)
3088 kbd_buffer_store_event_hold (&ie, hold_quit);
3089 count++;
3092 if (do_help
3093 && !(hold_quit && hold_quit->kind != NO_EVENT))
3095 Lisp_Object frame;
3097 if (f)
3098 XSETFRAME (frame, f);
3099 else
3100 frame = Qnil;
3102 gen_help_event (help_echo_string, frame, help_echo_window,
3103 help_echo_object, help_echo_pos);
3104 count++;
3107 return count;
3110 DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
3111 0, 0, 0,
3112 doc: /* Open a connection to Gpm.
3113 Gpm-mouse can only be activated for one tty at a time. */)
3116 struct frame *f = SELECTED_FRAME ();
3117 struct tty_display_info *tty
3118 = ((f)->output_method == output_termcap
3119 ? (f)->terminal->display_info.tty : NULL);
3120 Gpm_Connect connection;
3122 if (!tty)
3123 error ("Gpm-mouse only works in the GNU/Linux console");
3124 if (gpm_tty == tty)
3125 return Qnil; /* Already activated, nothing to do. */
3126 if (gpm_tty)
3127 error ("Gpm-mouse can only be activated for one tty at a time");
3129 connection.eventMask = ~0;
3130 connection.defaultMask = ~GPM_HARD;
3131 connection.maxMod = ~0;
3132 connection.minMod = 0;
3133 gpm_zerobased = 1;
3135 if (Gpm_Open (&connection, 0) < 0)
3136 error ("Gpm-mouse failed to connect to the gpm daemon");
3137 else
3139 gpm_tty = tty;
3140 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
3141 to generate SIGIOs. Apparently we need to call reset_sys_modes
3142 before calling init_sys_modes. */
3143 reset_sys_modes (tty);
3144 init_sys_modes (tty);
3145 add_gpm_wait_descriptor (gpm_fd);
3146 return Qnil;
3150 void
3151 close_gpm (int fd)
3153 if (fd >= 0)
3154 delete_gpm_wait_descriptor (fd);
3155 while (Gpm_Close()); /* close all the stack */
3156 gpm_tty = NULL;
3159 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
3160 0, 0, 0,
3161 doc: /* Close a connection to Gpm. */)
3164 struct frame *f = SELECTED_FRAME ();
3165 struct tty_display_info *tty
3166 = ((f)->output_method == output_termcap
3167 ? (f)->terminal->display_info.tty : NULL);
3169 if (!tty || gpm_tty != tty)
3170 return Qnil; /* Not activated on this terminal, nothing to do. */
3172 close_gpm (gpm_fd);
3173 return Qnil;
3175 #endif /* HAVE_GPM */
3178 #ifndef MSDOS
3179 /***********************************************************************
3180 Initialization
3181 ***********************************************************************/
3183 /* Initialize the tty-dependent part of frame F. The frame must
3184 already have its device initialized. */
3186 void
3187 create_tty_output (struct frame *f)
3189 struct tty_output *t;
3191 if (! FRAME_TERMCAP_P (f))
3192 abort ();
3194 t = xmalloc (sizeof (struct tty_output));
3195 bzero (t, sizeof (struct tty_output));
3197 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
3199 f->output_data.tty = t;
3202 /* Delete frame F's face cache, and its tty-dependent part. */
3204 static void
3205 tty_free_frame_resources (struct frame *f)
3207 if (! FRAME_TERMCAP_P (f))
3208 abort ();
3210 if (FRAME_FACE_CACHE (f))
3211 free_frame_faces (f);
3213 xfree (f->output_data.tty);
3216 #else /* MSDOS */
3218 /* Delete frame F's face cache. */
3220 static void
3221 tty_free_frame_resources (struct frame *f)
3223 if (! FRAME_TERMCAP_P (f) && ! FRAME_MSDOS_P (f))
3224 abort ();
3226 if (FRAME_FACE_CACHE (f))
3227 free_frame_faces (f);
3229 #endif /* MSDOS */
3231 /* Reset the hooks in TERMINAL. */
3233 static void
3234 clear_tty_hooks (struct terminal *terminal)
3236 terminal->rif = 0;
3237 terminal->cursor_to_hook = 0;
3238 terminal->raw_cursor_to_hook = 0;
3239 terminal->clear_to_end_hook = 0;
3240 terminal->clear_frame_hook = 0;
3241 terminal->clear_end_of_line_hook = 0;
3242 terminal->ins_del_lines_hook = 0;
3243 terminal->insert_glyphs_hook = 0;
3244 terminal->write_glyphs_hook = 0;
3245 terminal->delete_glyphs_hook = 0;
3246 terminal->ring_bell_hook = 0;
3247 terminal->reset_terminal_modes_hook = 0;
3248 terminal->set_terminal_modes_hook = 0;
3249 terminal->update_begin_hook = 0;
3250 terminal->update_end_hook = 0;
3251 terminal->set_terminal_window_hook = 0;
3252 terminal->mouse_position_hook = 0;
3253 terminal->frame_rehighlight_hook = 0;
3254 terminal->frame_raise_lower_hook = 0;
3255 terminal->fullscreen_hook = 0;
3256 terminal->set_vertical_scroll_bar_hook = 0;
3257 terminal->condemn_scroll_bars_hook = 0;
3258 terminal->redeem_scroll_bar_hook = 0;
3259 terminal->judge_scroll_bars_hook = 0;
3260 terminal->read_socket_hook = 0;
3261 terminal->frame_up_to_date_hook = 0;
3263 /* Leave these two set, or suspended frames are not deleted
3264 correctly. */
3265 terminal->delete_frame_hook = &tty_free_frame_resources;
3266 terminal->delete_terminal_hook = &delete_tty;
3269 /* Initialize hooks in TERMINAL with the values needed for a tty. */
3271 static void
3272 set_tty_hooks (struct terminal *terminal)
3274 terminal->rif = 0; /* ttys don't support window-based redisplay. */
3276 terminal->cursor_to_hook = &tty_cursor_to;
3277 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
3279 terminal->clear_to_end_hook = &tty_clear_to_end;
3280 terminal->clear_frame_hook = &tty_clear_frame;
3281 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
3283 terminal->ins_del_lines_hook = &tty_ins_del_lines;
3285 terminal->insert_glyphs_hook = &tty_insert_glyphs;
3286 terminal->write_glyphs_hook = &tty_write_glyphs;
3287 terminal->delete_glyphs_hook = &tty_delete_glyphs;
3289 terminal->ring_bell_hook = &tty_ring_bell;
3291 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
3292 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
3293 terminal->update_begin_hook = 0; /* Not needed. */
3294 terminal->update_end_hook = &tty_update_end;
3295 terminal->set_terminal_window_hook = &tty_set_terminal_window;
3297 terminal->mouse_position_hook = 0; /* Not needed. */
3298 terminal->frame_rehighlight_hook = 0; /* Not needed. */
3299 terminal->frame_raise_lower_hook = 0; /* Not needed. */
3301 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
3302 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
3303 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
3304 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
3306 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
3307 terminal->frame_up_to_date_hook = 0; /* Not needed. */
3309 terminal->delete_frame_hook = &tty_free_frame_resources;
3310 terminal->delete_terminal_hook = &delete_tty;
3313 /* Drop the controlling terminal if fd is the same device. */
3314 static void
3315 dissociate_if_controlling_tty (int fd)
3317 #ifndef DOS_NT
3318 int pgid;
3319 EMACS_GET_TTY_PGRP (fd, &pgid); /* If tcgetpgrp succeeds, fd is the ctty. */
3320 if (pgid != -1)
3322 #if defined (USG) && !defined (BSD_PGRPS)
3323 setpgrp ();
3324 no_controlling_tty = 1;
3325 #elif defined (CYGWIN)
3326 setsid ();
3327 no_controlling_tty = 1;
3328 #else
3329 #ifdef TIOCNOTTY /* Try BSD ioctls. */
3330 sigblock (sigmask (SIGTTOU));
3331 fd = emacs_open (DEV_TTY, O_RDWR, 0);
3332 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
3334 no_controlling_tty = 1;
3336 if (fd != -1)
3337 emacs_close (fd);
3338 sigunblock (sigmask (SIGTTOU));
3339 #else
3340 /* Unknown system. */
3341 croak ();
3342 #endif /* ! TIOCNOTTY */
3343 #endif /* ! USG */
3345 #endif /* !DOS_NT */
3348 static void maybe_fatal();
3350 /* Create a termcap display on the tty device with the given name and
3351 type.
3353 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3354 Otherwise NAME should be a path to the tty device file,
3355 e.g. "/dev/pts/7".
3357 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3359 If MUST_SUCCEED is true, then all errors are fatal. */
3361 struct terminal *
3362 init_tty (char *name, char *terminal_type, int must_succeed)
3364 char *area = NULL;
3365 char **address = &area;
3366 int buffer_size = 4096;
3367 register char *p = NULL;
3368 int status;
3369 struct tty_display_info *tty = NULL;
3370 struct terminal *terminal = NULL;
3371 int ctty = 0; /* 1 if asked to open controlling tty. */
3373 if (!terminal_type)
3374 maybe_fatal (must_succeed, 0,
3375 "Unknown terminal type",
3376 "Unknown terminal type");
3378 if (name == NULL)
3379 name = DEV_TTY;
3380 if (!strcmp (name, DEV_TTY))
3381 ctty = 1;
3383 /* If we already have a terminal on the given device, use that. If
3384 all such terminals are suspended, create a new one instead. */
3385 /* XXX Perhaps this should be made explicit by having init_tty
3386 always create a new terminal and separating terminal and frame
3387 creation on Lisp level. */
3388 terminal = get_named_tty (name);
3389 if (terminal)
3390 return terminal;
3392 terminal = create_terminal ();
3393 #ifdef MSDOS
3394 if (been_here > 0)
3395 maybe_fatal (1, 0, "Attempt to create another terminal %s", "",
3396 name, "");
3397 been_here = 1;
3398 tty = &the_only_display_info;
3399 #else
3400 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
3401 #endif
3402 bzero (tty, sizeof (struct tty_display_info));
3403 tty->next = tty_list;
3404 tty_list = tty;
3406 terminal->type = output_termcap;
3407 terminal->display_info.tty = tty;
3408 tty->terminal = terminal;
3410 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3411 Wcm_clear (tty);
3413 #ifndef DOS_NT
3414 set_tty_hooks (terminal);
3417 int fd;
3418 FILE *file;
3420 #ifdef O_IGNORE_CTTY
3421 if (!ctty)
3422 /* Open the terminal device. Don't recognize it as our
3423 controlling terminal, and don't make it the controlling tty
3424 if we don't have one at the moment. */
3425 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
3426 else
3427 #else
3428 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3429 defined on Hurd. On other systems, we need to explicitly
3430 dissociate ourselves from the controlling tty when we want to
3431 open a frame on the same terminal. */
3432 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
3433 #endif /* O_IGNORE_CTTY */
3435 tty->name = xstrdup (name);
3436 terminal->name = xstrdup (name);
3438 if (fd < 0)
3439 maybe_fatal (must_succeed, terminal,
3440 "Could not open file: %s",
3441 "Could not open file: %s",
3442 name);
3443 if (!isatty (fd))
3445 close (fd);
3446 maybe_fatal (must_succeed, terminal,
3447 "Not a tty device: %s",
3448 "Not a tty device: %s",
3449 name);
3452 #ifndef O_IGNORE_CTTY
3453 if (!ctty)
3454 dissociate_if_controlling_tty (fd);
3455 #endif
3457 file = fdopen (fd, "w+");
3458 tty->input = file;
3459 tty->output = file;
3462 tty->type = xstrdup (terminal_type);
3464 add_keyboard_wait_descriptor (fileno (tty->input));
3466 #endif /* !DOS_NT */
3468 encode_terminal_src_size = 0;
3469 encode_terminal_dst_size = 0;
3471 #ifdef HAVE_GPM
3472 terminal->mouse_position_hook = term_mouse_position;
3473 mouse_face_window = Qnil;
3474 #endif
3476 #ifdef DOS_NT
3477 #ifdef WINDOWSNT
3478 initialize_w32_display (terminal);
3479 #else /* MSDOS */
3480 if (strcmp (terminal_type, "internal") == 0)
3481 terminal->type = output_msdos_raw;
3482 initialize_msdos_display (terminal);
3483 #endif /* MSDOS */
3484 tty->output = stdout;
3485 tty->input = stdin;
3486 /* The following two are inaccessible from w32console.c. */
3487 terminal->delete_frame_hook = &tty_free_frame_resources;
3488 terminal->delete_terminal_hook = &delete_tty;
3490 tty->name = xstrdup (name);
3491 terminal->name = xstrdup (name);
3492 tty->type = xstrdup (terminal_type);
3494 #ifdef subprocesses
3495 add_keyboard_wait_descriptor (0);
3496 #endif
3498 Wcm_clear (tty);
3500 #ifdef WINDOWSNT
3502 struct frame *f = XFRAME (selected_frame);
3504 FrameRows (tty) = FRAME_LINES (f);
3505 FrameCols (tty) = FRAME_COLS (f);
3506 tty->specified_window = FRAME_LINES (f);
3508 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3509 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
3511 #else /* MSDOS */
3513 int height, width;
3514 get_tty_size (fileno (tty->input), &width, &height);
3515 FrameCols (tty) = width;
3516 FrameRows (tty) = height;
3518 #endif /* MSDOS */
3519 tty->delete_in_insert_mode = 1;
3521 UseTabs (tty) = 0;
3522 terminal->scroll_region_ok = 0;
3524 /* Seems to insert lines when it's not supposed to, messing up the
3525 display. In doing a trace, it didn't seem to be called much, so I
3526 don't think we're losing anything by turning it off. */
3527 terminal->line_ins_del_ok = 0;
3528 #ifdef WINDOWSNT
3529 terminal->char_ins_del_ok = 1;
3530 baud_rate = 19200;
3531 #else /* MSDOS */
3532 terminal->char_ins_del_ok = 0;
3533 init_baud_rate (fileno (tty->input));
3534 #endif /* MSDOS */
3536 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
3538 #else /* not DOS_NT */
3540 Wcm_clear (tty);
3542 tty->termcap_term_buffer = (char *) xmalloc (buffer_size);
3544 /* On some systems, tgetent tries to access the controlling
3545 terminal. */
3546 sigblock (sigmask (SIGTTOU));
3547 status = tgetent (tty->termcap_term_buffer, terminal_type);
3548 sigunblock (sigmask (SIGTTOU));
3550 if (status < 0)
3552 #ifdef TERMINFO
3553 maybe_fatal (must_succeed, terminal,
3554 "Cannot open terminfo database file",
3555 "Cannot open terminfo database file");
3556 #else
3557 maybe_fatal (must_succeed, terminal,
3558 "Cannot open termcap database file",
3559 "Cannot open termcap database file");
3560 #endif
3562 if (status == 0)
3564 #ifdef TERMINFO
3565 maybe_fatal (must_succeed, terminal,
3566 "Terminal type %s is not defined",
3567 "Terminal type %s is not defined.\n\
3568 If that is not the actual type of terminal you have,\n\
3569 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3570 `setenv TERM ...') to specify the correct type. It may be necessary\n\
3571 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3572 terminal_type);
3573 #else
3574 maybe_fatal (must_succeed, terminal,
3575 "Terminal type %s is not defined",
3576 "Terminal type %s is not defined.\n\
3577 If that is not the actual type of terminal you have,\n\
3578 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3579 `setenv TERM ...') to specify the correct type. It may be necessary\n\
3580 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3581 terminal_type);
3582 #endif
3585 #ifndef TERMINFO
3586 if (strlen (tty->termcap_term_buffer) >= buffer_size)
3587 abort ();
3588 buffer_size = strlen (tty->termcap_term_buffer);
3589 #endif
3590 tty->termcap_strings_buffer = area = (char *) xmalloc (buffer_size);
3591 tty->TS_ins_line = tgetstr ("al", address);
3592 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3593 tty->TS_bell = tgetstr ("bl", address);
3594 BackTab (tty) = tgetstr ("bt", address);
3595 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3596 tty->TS_clr_line = tgetstr ("ce", address);
3597 tty->TS_clr_frame = tgetstr ("cl", address);
3598 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3599 AbsPosition (tty) = tgetstr ("cm", address);
3600 CR (tty) = tgetstr ("cr", address);
3601 tty->TS_set_scroll_region = tgetstr ("cs", address);
3602 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
3603 RowPosition (tty) = tgetstr ("cv", address);
3604 tty->TS_del_char = tgetstr ("dc", address);
3605 tty->TS_del_multi_chars = tgetstr ("DC", address);
3606 tty->TS_del_line = tgetstr ("dl", address);
3607 tty->TS_del_multi_lines = tgetstr ("DL", address);
3608 tty->TS_delete_mode = tgetstr ("dm", address);
3609 tty->TS_end_delete_mode = tgetstr ("ed", address);
3610 tty->TS_end_insert_mode = tgetstr ("ei", address);
3611 Home (tty) = tgetstr ("ho", address);
3612 tty->TS_ins_char = tgetstr ("ic", address);
3613 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3614 tty->TS_insert_mode = tgetstr ("im", address);
3615 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3616 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3617 tty->TS_keypad_mode = tgetstr ("ks", address);
3618 LastLine (tty) = tgetstr ("ll", address);
3619 Right (tty) = tgetstr ("nd", address);
3620 Down (tty) = tgetstr ("do", address);
3621 if (!Down (tty))
3622 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
3623 if (tgetflag ("bs"))
3624 Left (tty) = "\b"; /* can't possibly be longer! */
3625 else /* (Actually, "bs" is obsolete...) */
3626 Left (tty) = tgetstr ("le", address);
3627 if (!Left (tty))
3628 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
3629 tty->TS_pad_char = tgetstr ("pc", address);
3630 tty->TS_repeat = tgetstr ("rp", address);
3631 tty->TS_end_standout_mode = tgetstr ("se", address);
3632 tty->TS_fwd_scroll = tgetstr ("sf", address);
3633 tty->TS_standout_mode = tgetstr ("so", address);
3634 tty->TS_rev_scroll = tgetstr ("sr", address);
3635 tty->Wcm->cm_tab = tgetstr ("ta", address);
3636 tty->TS_end_termcap_modes = tgetstr ("te", address);
3637 tty->TS_termcap_modes = tgetstr ("ti", address);
3638 Up (tty) = tgetstr ("up", address);
3639 tty->TS_visible_bell = tgetstr ("vb", address);
3640 tty->TS_cursor_normal = tgetstr ("ve", address);
3641 tty->TS_cursor_visible = tgetstr ("vs", address);
3642 tty->TS_cursor_invisible = tgetstr ("vi", address);
3643 tty->TS_set_window = tgetstr ("wi", address);
3645 tty->TS_enter_underline_mode = tgetstr ("us", address);
3646 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3647 tty->TS_enter_bold_mode = tgetstr ("md", address);
3648 tty->TS_enter_dim_mode = tgetstr ("mh", address);
3649 tty->TS_enter_blink_mode = tgetstr ("mb", address);
3650 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3651 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3652 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3653 tty->TS_exit_attribute_mode = tgetstr ("me", address);
3655 MultiUp (tty) = tgetstr ("UP", address);
3656 MultiDown (tty) = tgetstr ("DO", address);
3657 MultiLeft (tty) = tgetstr ("LE", address);
3658 MultiRight (tty) = tgetstr ("RI", address);
3660 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
3661 color because we can't switch back to the default foreground and
3662 background. */
3663 tty->TS_orig_pair = tgetstr ("op", address);
3664 if (tty->TS_orig_pair)
3666 tty->TS_set_foreground = tgetstr ("AF", address);
3667 tty->TS_set_background = tgetstr ("AB", address);
3668 if (!tty->TS_set_foreground)
3670 /* SVr4. */
3671 tty->TS_set_foreground = tgetstr ("Sf", address);
3672 tty->TS_set_background = tgetstr ("Sb", address);
3675 tty->TN_max_colors = tgetnum ("Co");
3676 tty->TN_max_pairs = tgetnum ("pa");
3678 tty->TN_no_color_video = tgetnum ("NC");
3679 if (tty->TN_no_color_video == -1)
3680 tty->TN_no_color_video = 0;
3683 tty_default_color_capabilities (tty, 1);
3685 MagicWrap (tty) = tgetflag ("xn");
3686 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3687 the former flag imply the latter. */
3688 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
3689 terminal->memory_below_frame = tgetflag ("db");
3690 tty->TF_hazeltine = tgetflag ("hz");
3691 terminal->must_write_spaces = tgetflag ("in");
3692 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3693 tty->TF_insmode_motion = tgetflag ("mi");
3694 tty->TF_standout_motion = tgetflag ("ms");
3695 tty->TF_underscore = tgetflag ("ul");
3696 tty->TF_teleray = tgetflag ("xt");
3698 #endif /* !DOS_NT */
3699 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3700 init_kboard (terminal->kboard);
3701 terminal->kboard->Vwindow_system = Qnil;
3702 terminal->kboard->next_kboard = all_kboards;
3703 all_kboards = terminal->kboard;
3704 terminal->kboard->reference_count++;
3705 /* Don't let the initial kboard remain current longer than necessary.
3706 That would cause problems if a file loaded on startup tries to
3707 prompt in the mini-buffer. */
3708 if (current_kboard == initial_kboard)
3709 current_kboard = terminal->kboard;
3710 #ifndef DOS_NT
3711 term_get_fkeys (address, terminal->kboard);
3713 /* Get frame size from system, or else from termcap. */
3715 int height, width;
3716 get_tty_size (fileno (tty->input), &width, &height);
3717 FrameCols (tty) = width;
3718 FrameRows (tty) = height;
3721 if (FrameCols (tty) <= 0)
3722 FrameCols (tty) = tgetnum ("co");
3723 if (FrameRows (tty) <= 0)
3724 FrameRows (tty) = tgetnum ("li");
3726 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
3727 maybe_fatal (must_succeed, terminal,
3728 "Screen size %dx%d is too small"
3729 "Screen size %dx%d is too small",
3730 FrameCols (tty), FrameRows (tty));
3732 TabWidth (tty) = tgetnum ("tw");
3734 if (!tty->TS_bell)
3735 tty->TS_bell = "\07";
3737 if (!tty->TS_fwd_scroll)
3738 tty->TS_fwd_scroll = Down (tty);
3740 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
3742 if (TabWidth (tty) < 0)
3743 TabWidth (tty) = 8;
3745 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3746 and newer termcap doc does not seem to say there is a default.
3747 if (!tty->Wcm->cm_tab)
3748 tty->Wcm->cm_tab = "\t";
3751 /* We don't support standout modes that use `magic cookies', so
3752 turn off any that do. */
3753 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
3755 tty->TS_standout_mode = 0;
3756 tty->TS_end_standout_mode = 0;
3758 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
3760 tty->TS_enter_underline_mode = 0;
3761 tty->TS_exit_underline_mode = 0;
3764 /* If there's no standout mode, try to use underlining instead. */
3765 if (tty->TS_standout_mode == 0)
3767 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3768 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
3771 /* If no `se' string, try using a `me' string instead.
3772 If that fails, we can't use standout mode at all. */
3773 if (tty->TS_end_standout_mode == 0)
3775 char *s = tgetstr ("me", address);
3776 if (s != 0)
3777 tty->TS_end_standout_mode = s;
3778 else
3779 tty->TS_standout_mode = 0;
3782 if (tty->TF_teleray)
3784 tty->Wcm->cm_tab = 0;
3785 /* We can't support standout mode, because it uses magic cookies. */
3786 tty->TS_standout_mode = 0;
3787 /* But that means we cannot rely on ^M to go to column zero! */
3788 CR (tty) = 0;
3789 /* LF can't be trusted either -- can alter hpos */
3790 /* if move at column 0 thru a line with TS_standout_mode */
3791 Down (tty) = 0;
3794 /* Special handling for certain terminal types known to need it */
3796 if (!strcmp (terminal_type, "supdup"))
3798 terminal->memory_below_frame = 1;
3799 tty->Wcm->cm_losewrap = 1;
3801 if (!strncmp (terminal_type, "c10", 3)
3802 || !strcmp (terminal_type, "perq"))
3804 /* Supply a makeshift :wi string.
3805 This string is not valid in general since it works only
3806 for windows starting at the upper left corner;
3807 but that is all Emacs uses.
3809 This string works only if the frame is using
3810 the top of the video memory, because addressing is memory-relative.
3811 So first check the :ti string to see if that is true.
3813 It would be simpler if the :wi string could go in the termcap
3814 entry, but it can't because it is not fully valid.
3815 If it were in the termcap entry, it would confuse other programs. */
3816 if (!tty->TS_set_window)
3818 p = tty->TS_termcap_modes;
3819 while (*p && strcmp (p, "\033v "))
3820 p++;
3821 if (*p)
3822 tty->TS_set_window = "\033v%C %C %C %C ";
3824 /* Termcap entry often fails to have :in: flag */
3825 terminal->must_write_spaces = 1;
3826 /* :ti string typically fails to have \E^G! in it */
3827 /* This limits scope of insert-char to one line. */
3828 strcpy (area, tty->TS_termcap_modes);
3829 strcat (area, "\033\007!");
3830 tty->TS_termcap_modes = area;
3831 area += strlen (area) + 1;
3832 p = AbsPosition (tty);
3833 /* Change all %+ parameters to %C, to handle
3834 values above 96 correctly for the C100. */
3835 while (*p)
3837 if (p[0] == '%' && p[1] == '+')
3838 p[1] = 'C';
3839 p++;
3843 tty->specified_window = FrameRows (tty);
3845 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3847 maybe_fatal (must_succeed, terminal,
3848 "Terminal type \"%s\" is not powerful enough to run Emacs",
3849 # ifdef TERMINFO
3850 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3851 It lacks the ability to position the cursor.\n\
3852 If that is not the actual type of terminal you have,\n\
3853 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3854 `setenv TERM ...') to specify the correct type. It may be necessary\n\
3855 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3856 # else /* TERMCAP */
3857 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3858 It lacks the ability to position the cursor.\n\
3859 If that is not the actual type of terminal you have,\n\
3860 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3861 `setenv TERM ...') to specify the correct type. It may be necessary\n\
3862 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3863 # endif /* TERMINFO */
3864 terminal_type);
3867 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
3868 maybe_fatal (must_succeed, terminal,
3869 "Could not determine the frame size",
3870 "Could not determine the frame size");
3872 tty->delete_in_insert_mode
3873 = tty->TS_delete_mode && tty->TS_insert_mode
3874 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
3876 tty->se_is_so = (tty->TS_standout_mode
3877 && tty->TS_end_standout_mode
3878 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
3880 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
3882 terminal->scroll_region_ok
3883 = (tty->Wcm->cm_abs
3884 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
3886 terminal->line_ins_del_ok
3887 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3888 && (tty->TS_del_line || tty->TS_del_multi_lines))
3889 || (terminal->scroll_region_ok
3890 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
3892 terminal->char_ins_del_ok
3893 = ((tty->TS_ins_char || tty->TS_insert_mode
3894 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3895 && (tty->TS_del_char || tty->TS_del_multi_chars));
3897 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
3899 init_baud_rate (fileno (tty->input));
3901 #endif /* not DOS_NT */
3903 /* Init system terminal modes (RAW or CBREAK, etc.). */
3904 init_sys_modes (tty);
3906 return terminal;
3909 /* Auxiliary error-handling function for init_tty.
3910 Delete TERMINAL, then call error or fatal with str1 or str2,
3911 respectively, according to MUST_SUCCEED. */
3913 static void
3914 maybe_fatal (must_succeed, terminal, str1, str2, arg1, arg2)
3915 int must_succeed;
3916 struct terminal *terminal;
3917 char *str1, *str2, *arg1, *arg2;
3919 if (terminal)
3920 delete_tty (terminal);
3922 if (must_succeed)
3923 fatal (str2, arg1, arg2);
3924 else
3925 error (str1, arg1, arg2);
3927 abort ();
3930 void
3931 fatal (const char *str, ...)
3933 va_list ap;
3934 va_start (ap, str);
3935 fprintf (stderr, "emacs: ");
3936 vfprintf (stderr, str, ap);
3937 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
3938 fprintf (stderr, "\n");
3939 va_end (ap);
3940 fflush (stderr);
3941 exit (1);
3946 /* Delete the given tty terminal, closing all frames on it. */
3948 static void
3949 delete_tty (struct terminal *terminal)
3951 struct tty_display_info *tty;
3953 /* Protect against recursive calls. delete_frame in
3954 delete_terminal calls us back when it deletes our last frame. */
3955 if (!terminal->name)
3956 return;
3958 if (terminal->type != output_termcap)
3959 abort ();
3961 tty = terminal->display_info.tty;
3963 if (tty == tty_list)
3964 tty_list = tty->next;
3965 else
3967 struct tty_display_info *p;
3968 for (p = tty_list; p && p->next != tty; p = p->next)
3971 if (! p)
3972 /* This should not happen. */
3973 abort ();
3975 p->next = tty->next;
3976 tty->next = 0;
3979 /* reset_sys_modes needs a valid device, so this call needs to be
3980 before delete_terminal. */
3981 reset_sys_modes (tty);
3983 delete_terminal (terminal);
3985 xfree (tty->name);
3986 xfree (tty->type);
3988 if (tty->input)
3990 #ifdef subprocesses
3991 delete_keyboard_wait_descriptor (fileno (tty->input));
3992 #endif
3993 if (tty->input != stdin)
3994 fclose (tty->input);
3996 if (tty->output && tty->output != stdout && tty->output != tty->input)
3997 fclose (tty->output);
3998 if (tty->termscript)
3999 fclose (tty->termscript);
4001 xfree (tty->old_tty);
4002 xfree (tty->Wcm);
4003 xfree (tty->termcap_strings_buffer);
4004 xfree (tty->termcap_term_buffer);
4006 bzero (tty, sizeof (struct tty_display_info));
4007 xfree (tty);
4012 /* Mark the pointers in the tty_display_info objects.
4013 Called by the Fgarbage_collector. */
4015 void
4016 mark_ttys (void)
4018 struct tty_display_info *tty;
4020 for (tty = tty_list; tty; tty = tty->next)
4021 mark_object (tty->top_frame);
4026 void
4027 syms_of_term ()
4029 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
4030 doc: /* Non-nil means the system uses terminfo rather than termcap.
4031 This variable can be used by terminal emulator packages. */);
4032 #ifdef TERMINFO
4033 system_uses_terminfo = 1;
4034 #else
4035 system_uses_terminfo = 0;
4036 #endif
4038 DEFVAR_LISP ("suspend-tty-functions", &Vsuspend_tty_functions,
4039 doc: /* Functions to be run after suspending a tty.
4040 The functions are run with one argument, the terminal object to be suspended.
4041 See `suspend-tty'. */);
4042 Vsuspend_tty_functions = Qnil;
4045 DEFVAR_LISP ("resume-tty-functions", &Vresume_tty_functions,
4046 doc: /* Functions to be run after resuming a tty.
4047 The functions are run with one argument, the terminal object that was revived.
4048 See `resume-tty'. */);
4049 Vresume_tty_functions = Qnil;
4051 DEFVAR_BOOL ("visible-cursor", &visible_cursor,
4052 doc: /* Non-nil means to make the cursor very visible.
4053 This only has an effect when running in a text terminal.
4054 What means \"very visible\" is up to your terminal. It may make the cursor
4055 bigger, or it may make it blink, or it may do nothing at all. */);
4056 visible_cursor = 1;
4058 defsubr (&Stty_display_color_p);
4059 defsubr (&Stty_display_color_cells);
4060 defsubr (&Stty_no_underline);
4061 defsubr (&Stty_type);
4062 defsubr (&Scontrolling_tty_p);
4063 defsubr (&Ssuspend_tty);
4064 defsubr (&Sresume_tty);
4065 #ifdef HAVE_GPM
4066 defsubr (&Sgpm_mouse_start);
4067 defsubr (&Sgpm_mouse_stop);
4069 staticpro (&mouse_face_window);
4070 #endif /* HAVE_GPM */
4072 #ifndef DOS_NT
4073 default_orig_pair = NULL;
4074 default_set_foreground = NULL;
4075 default_set_background = NULL;
4076 #endif /* !DOS_NT */
4078 encode_terminal_src = NULL;
4079 encode_terminal_dst = NULL;
4084 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
4085 (do not change this comment) */