Convert DEFUNs to standard C.
[emacs.git] / src / term.c
blob53879e57e5b707fa38d4815ed488733a121c1001
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 (const char *, int, int (*)(int));
70 extern int tgetent (char *, const char *);
71 extern int tgetflag (char *id);
72 extern int tgetnum (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 (struct frame *f, int start, int stop);
95 static void turn_on_face (struct frame *, int face_id);
96 static void turn_off_face (struct frame *, int face_id);
97 static void tty_show_cursor (struct tty_display_info *);
98 static void tty_hide_cursor (struct tty_display_info *);
99 static void tty_background_highlight (struct tty_display_info *tty);
100 static void clear_tty_hooks (struct terminal *terminal);
101 static void set_tty_hooks (struct terminal *terminal);
102 static void dissociate_if_controlling_tty (int fd);
103 static void delete_tty (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 Vsuspend_tty_functions;
136 /* Functions to call after resuming a tty. */
137 Lisp_Object 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 (char *, char **);
187 #ifdef HAVE_GPM
188 #include <sys/fcntl.h>
190 static void term_clear_mouse_face (void);
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 (struct glyph *src, int src_len, struct coding_system *coding)
563 struct glyph *src_end = src + src_len;
564 unsigned char *buf;
565 int nchars, nbytes, required;
566 register int tlen = GLYPH_TABLE_LENGTH;
567 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
568 Lisp_Object charset_list;
570 /* Allocate sufficient size of buffer to store all characters in
571 multibyte-form. But, it may be enlarged on demand if
572 Vglyph_table contains a string or a composite glyph is
573 encountered. */
574 required = MAX_MULTIBYTE_LENGTH * src_len;
575 if (encode_terminal_src_size < required)
577 if (encode_terminal_src)
578 encode_terminal_src = xrealloc (encode_terminal_src, required);
579 else
580 encode_terminal_src = xmalloc (required);
581 encode_terminal_src_size = required;
584 charset_list = coding_charset_list (coding);
586 buf = encode_terminal_src;
587 nchars = 0;
588 while (src < src_end)
590 if (src->type == COMPOSITE_GLYPH)
592 struct composition *cmp;
593 Lisp_Object gstring;
594 int i;
596 nbytes = buf - encode_terminal_src;
597 if (src->u.cmp.automatic)
599 gstring = composition_gstring_from_id (src->u.cmp.id);
600 required = src->u.cmp.to + 1 - src->u.cmp.from;
602 else
604 cmp = composition_table[src->u.cmp.id];
605 required = MAX_MULTIBYTE_LENGTH * cmp->glyph_len;
608 if (encode_terminal_src_size < nbytes + required)
610 encode_terminal_src_size = nbytes + required;
611 encode_terminal_src = xrealloc (encode_terminal_src,
612 encode_terminal_src_size);
613 buf = encode_terminal_src + nbytes;
616 if (src->u.cmp.automatic)
617 for (i = src->u.cmp.from; i <= src->u.cmp.to; i++)
619 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
620 int c = LGLYPH_CHAR (g);
622 if (! char_charset (c, charset_list, NULL))
623 c = '?';
624 buf += CHAR_STRING (c, buf);
625 nchars++;
627 else
628 for (i = 0; i < cmp->glyph_len; i++)
630 int c = COMPOSITION_GLYPH (cmp, i);
632 if (c == '\t')
633 continue;
634 if (char_charset (c, charset_list, NULL))
636 if (CHAR_WIDTH (c) == 0
637 && i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
638 /* Should be left-padded */
640 buf += CHAR_STRING (' ', buf);
641 nchars++;
644 else
645 c = '?';
646 buf += CHAR_STRING (c, buf);
647 nchars++;
650 /* We must skip glyphs to be padded for a wide character. */
651 else if (! CHAR_GLYPH_PADDING_P (*src))
653 GLYPH g;
654 int c;
655 Lisp_Object string;
657 string = Qnil;
658 SET_GLYPH_FROM_CHAR_GLYPH (g, src[0]);
660 if (GLYPH_INVALID_P (g) || GLYPH_SIMPLE_P (tbase, tlen, g))
662 /* This glyph doesn't have an entry in Vglyph_table. */
663 c = src->u.ch;
665 else
667 /* This glyph has an entry in Vglyph_table,
668 so process any alias before testing for simpleness. */
669 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
671 if (GLYPH_SIMPLE_P (tbase, tlen, g))
672 /* We set the multi-byte form of a character in G
673 (that should be an ASCII character) at WORKBUF. */
674 c = GLYPH_CHAR (g);
675 else
676 /* We have a string in Vglyph_table. */
677 string = tbase[GLYPH_CHAR (g)];
680 if (NILP (string))
682 nbytes = buf - encode_terminal_src;
683 if (encode_terminal_src_size < nbytes + MAX_MULTIBYTE_LENGTH)
685 encode_terminal_src_size = nbytes + MAX_MULTIBYTE_LENGTH;
686 encode_terminal_src = xrealloc (encode_terminal_src,
687 encode_terminal_src_size);
688 buf = encode_terminal_src + nbytes;
690 if (char_charset (c, charset_list, NULL))
692 /* Store the multibyte form of C at BUF. */
693 buf += CHAR_STRING (c, buf);
694 nchars++;
696 else
698 /* C is not encodable. */
699 *buf++ = '?';
700 nchars++;
701 while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
703 *buf++ = '?';
704 nchars++;
705 src++;
709 else
711 unsigned char *p = SDATA (string), *pend = p + SBYTES (string);
713 if (! STRING_MULTIBYTE (string))
714 string = string_to_multibyte (string);
715 nbytes = buf - encode_terminal_src;
716 if (encode_terminal_src_size < nbytes + SBYTES (string))
718 encode_terminal_src_size = nbytes + SBYTES (string);
719 encode_terminal_src = xrealloc (encode_terminal_src,
720 encode_terminal_src_size);
721 buf = encode_terminal_src + nbytes;
723 memcpy (buf, SDATA (string), SBYTES (string));
724 buf += SBYTES (string);
725 nchars += SCHARS (string);
728 src++;
731 if (nchars == 0)
733 coding->produced = 0;
734 return NULL;
737 nbytes = buf - encode_terminal_src;
738 coding->source = encode_terminal_src;
739 if (encode_terminal_dst_size == 0)
741 encode_terminal_dst_size = encode_terminal_src_size;
742 if (encode_terminal_dst)
743 encode_terminal_dst = xrealloc (encode_terminal_dst,
744 encode_terminal_dst_size);
745 else
746 encode_terminal_dst = xmalloc (encode_terminal_dst_size);
748 coding->destination = encode_terminal_dst;
749 coding->dst_bytes = encode_terminal_dst_size;
750 encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
751 /* coding->destination may have been reallocated. */
752 encode_terminal_dst = coding->destination;
753 encode_terminal_dst_size = coding->dst_bytes;
755 return (encode_terminal_dst);
760 /* An implementation of write_glyphs for termcap frames. */
762 static void
763 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
765 unsigned char *conversion_buffer;
766 struct coding_system *coding;
768 struct tty_display_info *tty = FRAME_TTY (f);
770 tty_turn_off_insert (tty);
771 tty_hide_cursor (tty);
773 /* Don't dare write in last column of bottom line, if Auto-Wrap,
774 since that would scroll the whole frame on some terminals. */
776 if (AutoWrap (tty)
777 && curY (tty) + 1 == FRAME_LINES (f)
778 && (curX (tty) + len) == FRAME_COLS (f))
779 len --;
780 if (len <= 0)
781 return;
783 cmplus (tty, len);
785 /* If terminal_coding does any conversion, use it, otherwise use
786 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
787 because it always return 1 if the member src_multibyte is 1. */
788 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
789 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
790 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
791 the tail. */
792 coding->mode &= ~CODING_MODE_LAST_BLOCK;
794 while (len > 0)
796 /* Identify a run of glyphs with the same face. */
797 int face_id = string->face_id;
798 int n;
800 for (n = 1; n < len; ++n)
801 if (string[n].face_id != face_id)
802 break;
804 /* Turn appearance modes of the face of the run on. */
805 tty_highlight_if_desired (tty);
806 turn_on_face (f, face_id);
808 if (n == len)
809 /* This is the last run. */
810 coding->mode |= CODING_MODE_LAST_BLOCK;
811 conversion_buffer = encode_terminal_code (string, n, coding);
812 if (coding->produced > 0)
814 BLOCK_INPUT;
815 fwrite (conversion_buffer, 1, coding->produced, tty->output);
816 if (ferror (tty->output))
817 clearerr (tty->output);
818 if (tty->termscript)
819 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
820 UNBLOCK_INPUT;
822 len -= n;
823 string += n;
825 /* Turn appearance modes off. */
826 turn_off_face (f, face_id);
827 tty_turn_off_highlight (tty);
830 cmcheckmagic (tty);
833 #ifdef HAVE_GPM /* Only used by GPM code. */
835 static void
836 tty_write_glyphs_with_face (register struct frame *f, register struct glyph *string,
837 register int len, register int face_id)
839 unsigned char *conversion_buffer;
840 struct coding_system *coding;
842 struct tty_display_info *tty = FRAME_TTY (f);
844 tty_turn_off_insert (tty);
845 tty_hide_cursor (tty);
847 /* Don't dare write in last column of bottom line, if Auto-Wrap,
848 since that would scroll the whole frame on some terminals. */
850 if (AutoWrap (tty)
851 && curY (tty) + 1 == FRAME_LINES (f)
852 && (curX (tty) + len) == FRAME_COLS (f))
853 len --;
854 if (len <= 0)
855 return;
857 cmplus (tty, len);
859 /* If terminal_coding does any conversion, use it, otherwise use
860 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
861 because it always return 1 if the member src_multibyte is 1. */
862 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
863 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
864 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
865 the tail. */
866 coding->mode &= ~CODING_MODE_LAST_BLOCK;
868 /* Turn appearance modes of the face. */
869 tty_highlight_if_desired (tty);
870 turn_on_face (f, face_id);
872 coding->mode |= CODING_MODE_LAST_BLOCK;
873 conversion_buffer = encode_terminal_code (string, len, coding);
874 if (coding->produced > 0)
876 BLOCK_INPUT;
877 fwrite (conversion_buffer, 1, coding->produced, tty->output);
878 if (ferror (tty->output))
879 clearerr (tty->output);
880 if (tty->termscript)
881 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
882 UNBLOCK_INPUT;
885 /* Turn appearance modes off. */
886 turn_off_face (f, face_id);
887 tty_turn_off_highlight (tty);
889 cmcheckmagic (tty);
891 #endif
893 /* An implementation of insert_glyphs for termcap frames. */
895 static void
896 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
898 char *buf;
899 struct glyph *glyph = NULL;
900 unsigned char *conversion_buffer;
901 unsigned char space[1];
902 struct coding_system *coding;
904 struct tty_display_info *tty = FRAME_TTY (f);
906 if (tty->TS_ins_multi_chars)
908 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len);
909 OUTPUT1 (tty, buf);
910 xfree (buf);
911 if (start)
912 write_glyphs (f, start, len);
913 return;
916 tty_turn_on_insert (tty);
917 cmplus (tty, len);
919 if (! start)
920 space[0] = SPACEGLYPH;
922 /* If terminal_coding does any conversion, use it, otherwise use
923 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
924 because it always return 1 if the member src_multibyte is 1. */
925 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
926 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
927 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
928 the tail. */
929 coding->mode &= ~CODING_MODE_LAST_BLOCK;
931 while (len-- > 0)
933 OUTPUT1_IF (tty, tty->TS_ins_char);
934 if (!start)
936 conversion_buffer = space;
937 coding->produced = 1;
939 else
941 tty_highlight_if_desired (tty);
942 turn_on_face (f, start->face_id);
943 glyph = start;
944 ++start;
945 /* We must open sufficient space for a character which
946 occupies more than one column. */
947 while (len && CHAR_GLYPH_PADDING_P (*start))
949 OUTPUT1_IF (tty, tty->TS_ins_char);
950 start++, len--;
953 if (len <= 0)
954 /* This is the last glyph. */
955 coding->mode |= CODING_MODE_LAST_BLOCK;
957 conversion_buffer = encode_terminal_code (glyph, 1, coding);
960 if (coding->produced > 0)
962 BLOCK_INPUT;
963 fwrite (conversion_buffer, 1, coding->produced, tty->output);
964 if (ferror (tty->output))
965 clearerr (tty->output);
966 if (tty->termscript)
967 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
968 UNBLOCK_INPUT;
971 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
972 if (start)
974 turn_off_face (f, glyph->face_id);
975 tty_turn_off_highlight (tty);
979 cmcheckmagic (tty);
982 /* An implementation of delete_glyphs for termcap frames. */
984 static void
985 tty_delete_glyphs (struct frame *f, int n)
987 char *buf;
988 register int i;
990 struct tty_display_info *tty = FRAME_TTY (f);
992 if (tty->delete_in_insert_mode)
994 tty_turn_on_insert (tty);
996 else
998 tty_turn_off_insert (tty);
999 OUTPUT_IF (tty, tty->TS_delete_mode);
1002 if (tty->TS_del_multi_chars)
1004 buf = tparam (tty->TS_del_multi_chars, 0, 0, n);
1005 OUTPUT1 (tty, buf);
1006 xfree (buf);
1008 else
1009 for (i = 0; i < n; i++)
1010 OUTPUT1 (tty, tty->TS_del_char);
1011 if (!tty->delete_in_insert_mode)
1012 OUTPUT_IF (tty, tty->TS_end_delete_mode);
1015 /* An implementation of ins_del_lines for termcap frames. */
1017 static void
1018 tty_ins_del_lines (struct frame *f, int vpos, int n)
1020 struct tty_display_info *tty = FRAME_TTY (f);
1021 char *multi = n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
1022 char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
1023 char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
1025 register int i = n > 0 ? n : -n;
1026 register char *buf;
1028 /* If the lines below the insertion are being pushed
1029 into the end of the window, this is the same as clearing;
1030 and we know the lines are already clear, since the matching
1031 deletion has already been done. So can ignore this. */
1032 /* If the lines below the deletion are blank lines coming
1033 out of the end of the window, don't bother,
1034 as there will be a matching inslines later that will flush them. */
1035 if (FRAME_SCROLL_REGION_OK (f)
1036 && vpos + i >= tty->specified_window)
1037 return;
1038 if (!FRAME_MEMORY_BELOW_FRAME (f)
1039 && vpos + i >= FRAME_LINES (f))
1040 return;
1042 if (multi)
1044 raw_cursor_to (f, vpos, 0);
1045 tty_background_highlight (tty);
1046 buf = tparam (multi, 0, 0, i);
1047 OUTPUT (tty, buf);
1048 xfree (buf);
1050 else if (single)
1052 raw_cursor_to (f, vpos, 0);
1053 tty_background_highlight (tty);
1054 while (--i >= 0)
1055 OUTPUT (tty, single);
1056 if (tty->TF_teleray)
1057 curX (tty) = 0;
1059 else
1061 tty_set_scroll_region (f, vpos, tty->specified_window);
1062 if (n < 0)
1063 raw_cursor_to (f, tty->specified_window - 1, 0);
1064 else
1065 raw_cursor_to (f, vpos, 0);
1066 tty_background_highlight (tty);
1067 while (--i >= 0)
1068 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1069 tty_set_scroll_region (f, 0, tty->specified_window);
1072 if (!FRAME_SCROLL_REGION_OK (f)
1073 && FRAME_MEMORY_BELOW_FRAME (f)
1074 && n < 0)
1076 cursor_to (f, FRAME_LINES (f) + n, 0);
1077 clear_to_end (f);
1081 /* Compute cost of sending "str", in characters,
1082 not counting any line-dependent padding. */
1085 string_cost (char *str)
1087 cost = 0;
1088 if (str)
1089 tputs (str, 0, evalcost);
1090 return cost;
1093 /* Compute cost of sending "str", in characters,
1094 counting any line-dependent padding at one line. */
1096 static int
1097 string_cost_one_line (char *str)
1099 cost = 0;
1100 if (str)
1101 tputs (str, 1, evalcost);
1102 return cost;
1105 /* Compute per line amount of line-dependent padding,
1106 in tenths of characters. */
1109 per_line_cost (char *str)
1111 cost = 0;
1112 if (str)
1113 tputs (str, 0, evalcost);
1114 cost = - cost;
1115 if (str)
1116 tputs (str, 10, evalcost);
1117 return cost;
1120 #ifndef old
1121 /* char_ins_del_cost[n] is cost of inserting N characters.
1122 char_ins_del_cost[-n] is cost of deleting N characters.
1123 The length of this vector is based on max_frame_cols. */
1125 int *char_ins_del_vector;
1127 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1128 #endif
1130 /* ARGSUSED */
1131 static void
1132 calculate_ins_del_char_costs (struct frame *f)
1134 struct tty_display_info *tty = FRAME_TTY (f);
1135 int ins_startup_cost, del_startup_cost;
1136 int ins_cost_per_char, del_cost_per_char;
1137 register int i;
1138 register int *p;
1140 if (tty->TS_ins_multi_chars)
1142 ins_cost_per_char = 0;
1143 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1145 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1146 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1148 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1149 + string_cost (tty->TS_end_insert_mode))) / 100;
1150 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1151 + string_cost_one_line (tty->TS_pad_inserted_char));
1153 else
1155 ins_startup_cost = 9999;
1156 ins_cost_per_char = 0;
1159 if (tty->TS_del_multi_chars)
1161 del_cost_per_char = 0;
1162 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1164 else if (tty->TS_del_char)
1166 del_startup_cost = (string_cost (tty->TS_delete_mode)
1167 + string_cost (tty->TS_end_delete_mode));
1168 if (tty->delete_in_insert_mode)
1169 del_startup_cost /= 2;
1170 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1172 else
1174 del_startup_cost = 9999;
1175 del_cost_per_char = 0;
1178 /* Delete costs are at negative offsets */
1179 p = &char_ins_del_cost (f)[0];
1180 for (i = FRAME_COLS (f); --i >= 0;)
1181 *--p = (del_startup_cost += del_cost_per_char);
1183 /* Doing nothing is free */
1184 p = &char_ins_del_cost (f)[0];
1185 *p++ = 0;
1187 /* Insert costs are at positive offsets */
1188 for (i = FRAME_COLS (f); --i >= 0;)
1189 *p++ = (ins_startup_cost += ins_cost_per_char);
1192 void
1193 calculate_costs (struct frame *frame)
1195 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1197 if (FRAME_TERMCAP_P (frame))
1199 struct tty_display_info *tty = FRAME_TTY (frame);
1200 register char *f = (tty->TS_set_scroll_region
1201 ? tty->TS_set_scroll_region
1202 : tty->TS_set_scroll_region_1);
1204 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1206 tty->costs_set = 1;
1208 /* These variables are only used for terminal stuff. They are
1209 allocated once for the terminal frame of X-windows emacs, but not
1210 used afterwards.
1212 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1213 X turns off char_ins_del_ok. */
1215 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1216 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1218 if (char_ins_del_vector != 0)
1219 char_ins_del_vector
1220 = (int *) xrealloc (char_ins_del_vector,
1221 (sizeof (int)
1222 + 2 * max_frame_cols * sizeof (int)));
1223 else
1224 char_ins_del_vector
1225 = (int *) xmalloc (sizeof (int)
1226 + 2 * max_frame_cols * sizeof (int));
1228 memset (char_ins_del_vector, 0,
1229 (sizeof (int) + 2 * max_frame_cols * sizeof (int)));
1232 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1233 do_line_insertion_deletion_costs (frame,
1234 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1235 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1236 f, f, 1);
1237 else
1238 do_line_insertion_deletion_costs (frame,
1239 tty->TS_ins_line, tty->TS_ins_multi_lines,
1240 tty->TS_del_line, tty->TS_del_multi_lines,
1241 0, 0, 1);
1243 calculate_ins_del_char_costs (frame);
1245 /* Don't use TS_repeat if its padding is worse than sending the chars */
1246 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1247 tty->RPov = string_cost (tty->TS_repeat);
1248 else
1249 tty->RPov = FRAME_COLS (frame) * 2;
1251 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1255 struct fkey_table {
1256 char *cap, *name;
1259 /* Termcap capability names that correspond directly to X keysyms.
1260 Some of these (marked "terminfo") aren't supplied by old-style
1261 (Berkeley) termcap entries. They're listed in X keysym order;
1262 except we put the keypad keys first, so that if they clash with
1263 other keys (as on the IBM PC keyboard) they get overridden.
1266 static struct fkey_table keys[] =
1268 {"kh", "home"}, /* termcap */
1269 {"kl", "left"}, /* termcap */
1270 {"ku", "up"}, /* termcap */
1271 {"kr", "right"}, /* termcap */
1272 {"kd", "down"}, /* termcap */
1273 {"%8", "prior"}, /* terminfo */
1274 {"%5", "next"}, /* terminfo */
1275 {"@7", "end"}, /* terminfo */
1276 {"@1", "begin"}, /* terminfo */
1277 {"*6", "select"}, /* terminfo */
1278 {"%9", "print"}, /* terminfo */
1279 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1281 * "insert" --- see below
1283 {"&8", "undo"}, /* terminfo */
1284 {"%0", "redo"}, /* terminfo */
1285 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1286 {"@0", "find"}, /* terminfo */
1287 {"@2", "cancel"}, /* terminfo */
1288 {"%1", "help"}, /* terminfo */
1290 * "break" goes here, but can't be reliably intercepted with termcap
1292 {"&4", "reset"}, /* terminfo --- actually `restart' */
1294 * "system" and "user" --- no termcaps
1296 {"kE", "clearline"}, /* terminfo */
1297 {"kA", "insertline"}, /* terminfo */
1298 {"kL", "deleteline"}, /* terminfo */
1299 {"kI", "insertchar"}, /* terminfo */
1300 {"kD", "deletechar"}, /* terminfo */
1301 {"kB", "backtab"}, /* terminfo */
1303 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1305 {"@8", "kp-enter"}, /* terminfo */
1307 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1308 * "kp-multiply", "kp-add", "kp-separator",
1309 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1310 * --- no termcaps for any of these.
1312 {"K4", "kp-1"}, /* terminfo */
1314 * "kp-2" --- no termcap
1316 {"K5", "kp-3"}, /* terminfo */
1318 * "kp-4" --- no termcap
1320 {"K2", "kp-5"}, /* terminfo */
1322 * "kp-6" --- no termcap
1324 {"K1", "kp-7"}, /* terminfo */
1326 * "kp-8" --- no termcap
1328 {"K3", "kp-9"}, /* terminfo */
1330 * "kp-equal" --- no termcap
1332 {"k1", "f1"},
1333 {"k2", "f2"},
1334 {"k3", "f3"},
1335 {"k4", "f4"},
1336 {"k5", "f5"},
1337 {"k6", "f6"},
1338 {"k7", "f7"},
1339 {"k8", "f8"},
1340 {"k9", "f9"},
1342 {"&0", "S-cancel"}, /*shifted cancel key*/
1343 {"&9", "S-begin"}, /*shifted begin key*/
1344 {"*0", "S-find"}, /*shifted find key*/
1345 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1346 {"*4", "S-delete"}, /*shifted delete-character key*/
1347 {"*7", "S-end"}, /*shifted end key*/
1348 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1349 {"#1", "S-help"}, /*shifted help key*/
1350 {"#2", "S-home"}, /*shifted home key*/
1351 {"#3", "S-insert"}, /*shifted insert-character key*/
1352 {"#4", "S-left"}, /*shifted left-arrow key*/
1353 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1354 {"%c", "S-next"}, /*shifted next key*/
1355 {"%e", "S-prior"}, /*shifted previous key*/
1356 {"%f", "S-print"}, /*shifted print key*/
1357 {"%g", "S-redo"}, /*shifted redo key*/
1358 {"%i", "S-right"}, /*shifted right-arrow key*/
1359 {"!3", "S-undo"} /*shifted undo key*/
1362 static char **term_get_fkeys_address;
1363 static KBOARD *term_get_fkeys_kboard;
1364 static Lisp_Object term_get_fkeys_1 (void);
1366 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1367 This function scans the termcap function key sequence entries, and
1368 adds entries to Vinput_decode_map for each function key it finds. */
1370 static void
1371 term_get_fkeys (char **address, KBOARD *kboard)
1373 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1374 errors during the call. The only errors should be from Fdefine_key
1375 when given a key sequence containing an invalid prefix key. If the
1376 termcap defines function keys which use a prefix that is already bound
1377 to a command by the default bindings, we should silently ignore that
1378 function key specification, rather than giving the user an error and
1379 refusing to run at all on such a terminal. */
1381 extern Lisp_Object Fidentity (Lisp_Object);
1382 term_get_fkeys_address = address;
1383 term_get_fkeys_kboard = kboard;
1384 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1387 static Lisp_Object
1388 term_get_fkeys_1 (void)
1390 int i;
1392 char **address = term_get_fkeys_address;
1393 KBOARD *kboard = term_get_fkeys_kboard;
1395 /* This can happen if CANNOT_DUMP or with strange options. */
1396 if (!KEYMAPP (kboard->Vinput_decode_map))
1397 kboard->Vinput_decode_map = Fmake_sparse_keymap (Qnil);
1399 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1401 char *sequence = tgetstr (keys[i].cap, address);
1402 if (sequence)
1403 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
1404 Fmake_vector (make_number (1),
1405 intern (keys[i].name)));
1408 /* The uses of the "k0" capability are inconsistent; sometimes it
1409 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1410 We will attempt to politely accommodate both systems by testing for
1411 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1414 char *k_semi = tgetstr ("k;", address);
1415 char *k0 = tgetstr ("k0", address);
1416 char *k0_name = "f10";
1418 if (k_semi)
1420 if (k0)
1421 /* Define f0 first, so that f10 takes precedence in case the
1422 key sequences happens to be the same. */
1423 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
1424 Fmake_vector (make_number (1), intern ("f0")));
1425 Fdefine_key (kboard->Vinput_decode_map, build_string (k_semi),
1426 Fmake_vector (make_number (1), intern ("f10")));
1428 else if (k0)
1429 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
1430 Fmake_vector (make_number (1), intern (k0_name)));
1433 /* Set up cookies for numbered function keys above f10. */
1435 char fcap[3], fkey[4];
1437 fcap[0] = 'F'; fcap[2] = '\0';
1438 for (i = 11; i < 64; i++)
1440 if (i <= 19)
1441 fcap[1] = '1' + i - 11;
1442 else if (i <= 45)
1443 fcap[1] = 'A' + i - 20;
1444 else
1445 fcap[1] = 'a' + i - 46;
1448 char *sequence = tgetstr (fcap, address);
1449 if (sequence)
1451 sprintf (fkey, "f%d", i);
1452 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
1453 Fmake_vector (make_number (1),
1454 intern (fkey)));
1461 * Various mappings to try and get a better fit.
1464 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1465 if (!tgetstr (cap1, address)) \
1467 char *sequence = tgetstr (cap2, address); \
1468 if (sequence) \
1469 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence), \
1470 Fmake_vector (make_number (1), \
1471 intern (sym))); \
1474 /* if there's no key_next keycap, map key_npage to `next' keysym */
1475 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1476 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1477 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1478 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1479 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1480 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1481 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1483 /* IBM has their own non-standard dialect of terminfo.
1484 If the standard name isn't found, try the IBM name. */
1485 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1486 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1487 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1488 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1489 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1490 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1491 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1492 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1493 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1494 #undef CONDITIONAL_REASSIGN
1497 return Qnil;
1501 /***********************************************************************
1502 Character Display Information
1503 ***********************************************************************/
1505 /* Avoid name clash with functions defined in xterm.c */
1506 #ifdef static
1507 #define append_glyph append_glyph_term
1508 #define produce_stretch_glyph produce_stretch_glyph_term
1509 #define append_composite_glyph append_composite_glyph_term
1510 #define produce_composite_glyph produce_composite_glyph_term
1511 #endif
1513 static void append_glyph (struct it *);
1514 static void produce_stretch_glyph (struct it *);
1515 static void append_composite_glyph (struct it *);
1516 static void produce_composite_glyph (struct it *);
1518 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1519 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1520 the character for which to produce glyphs; IT->face_id contains the
1521 character's face. Padding glyphs are appended if IT->c has a
1522 IT->pixel_width > 1. */
1524 static void
1525 append_glyph (struct it *it)
1527 struct glyph *glyph, *end;
1528 int i;
1530 xassert (it->glyph_row);
1531 glyph = (it->glyph_row->glyphs[it->area]
1532 + it->glyph_row->used[it->area]);
1533 end = it->glyph_row->glyphs[1 + it->area];
1535 /* If the glyph row is reversed, we need to prepend the glyph rather
1536 than append it. */
1537 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1539 struct glyph *g;
1540 int move_by = it->pixel_width;
1542 /* Make room for the new glyphs. */
1543 if (move_by > end - glyph) /* don't overstep end of this area */
1544 move_by = end - glyph;
1545 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1546 g[move_by] = *g;
1547 glyph = it->glyph_row->glyphs[it->area];
1548 end = glyph + move_by;
1551 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1552 right, even in the REVERSED_P case, since (a) all of its u.ch are
1553 identical, and (b) the PADDING_P flag needs to be set for the
1554 leftmost one, because we write to the terminal left-to-right. */
1555 for (i = 0;
1556 i < it->pixel_width && glyph < end;
1557 ++i)
1559 glyph->type = CHAR_GLYPH;
1560 glyph->pixel_width = 1;
1561 glyph->u.ch = it->char_to_display;
1562 glyph->face_id = it->face_id;
1563 glyph->padding_p = i > 0;
1564 glyph->charpos = CHARPOS (it->position);
1565 glyph->object = it->object;
1566 if (it->bidi_p)
1568 glyph->resolved_level = it->bidi_it.resolved_level;
1569 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1570 abort ();
1571 glyph->bidi_type = it->bidi_it.type;
1573 else
1575 glyph->resolved_level = 0;
1576 glyph->bidi_type = UNKNOWN_BT;
1579 ++it->glyph_row->used[it->area];
1580 ++glyph;
1584 /* Produce glyphs for the display element described by IT. *IT
1585 specifies what we want to produce a glyph for (character, image, ...),
1586 and where in the glyph matrix we currently are (glyph row and hpos).
1587 produce_glyphs fills in output fields of *IT with information such as the
1588 pixel width and height of a character, and maybe output actual glyphs at
1589 the same time if IT->glyph_row is non-null. For an overview, see
1590 the explanation in dispextern.h, before the definition of the
1591 display_element_type enumeration.
1593 produce_glyphs also stores the result of glyph width, ascent
1594 etc. computations in *IT.
1596 IT->glyph_row may be null, in which case produce_glyphs does not
1597 actually fill in the glyphs. This is used in the move_* functions
1598 in xdisp.c for text width and height computations.
1600 Callers usually don't call produce_glyphs directly;
1601 instead they use the macro PRODUCE_GLYPHS. */
1603 void
1604 produce_glyphs (struct it *it)
1606 /* If a hook is installed, let it do the work. */
1608 /* Nothing but characters are supported on terminal frames. */
1609 xassert (it->what == IT_CHARACTER
1610 || it->what == IT_COMPOSITION
1611 || it->what == IT_STRETCH);
1613 if (it->what == IT_STRETCH)
1615 produce_stretch_glyph (it);
1616 goto done;
1619 if (it->what == IT_COMPOSITION)
1621 produce_composite_glyph (it);
1622 goto done;
1625 /* Maybe translate single-byte characters to multibyte. */
1626 it->char_to_display = it->c;
1628 if (it->c >= 040 && it->c < 0177)
1630 it->pixel_width = it->nglyphs = 1;
1631 if (it->glyph_row)
1632 append_glyph (it);
1634 else if (it->c == '\n')
1635 it->pixel_width = it->nglyphs = 0;
1636 else if (it->c == '\t')
1638 int absolute_x = (it->current_x
1639 + it->continuation_lines_width);
1640 int next_tab_x
1641 = (((1 + absolute_x + it->tab_width - 1)
1642 / it->tab_width)
1643 * it->tab_width);
1644 int nspaces;
1646 /* If part of the TAB has been displayed on the previous line
1647 which is continued now, continuation_lines_width will have
1648 been incremented already by the part that fitted on the
1649 continued line. So, we will get the right number of spaces
1650 here. */
1651 nspaces = next_tab_x - absolute_x;
1653 if (it->glyph_row)
1655 int n = nspaces;
1657 it->char_to_display = ' ';
1658 it->pixel_width = it->len = 1;
1660 while (n--)
1661 append_glyph (it);
1664 it->pixel_width = nspaces;
1665 it->nglyphs = nspaces;
1667 else if (CHAR_BYTE8_P (it->c))
1669 if (unibyte_display_via_language_environment
1670 && (it->c >= 0240))
1672 it->char_to_display = BYTE8_TO_CHAR (it->c);
1673 it->pixel_width = CHAR_WIDTH (it->char_to_display);
1674 it->nglyphs = it->pixel_width;
1675 if (it->glyph_row)
1676 append_glyph (it);
1678 else
1680 /* Coming here means that it->c is from display table, thus
1681 we must send the raw 8-bit byte as is to the terminal.
1682 Although there's no way to know how many columns it
1683 occupies on a screen, it is a good assumption that a
1684 single byte code has 1-column width. */
1685 it->pixel_width = it->nglyphs = 1;
1686 if (it->glyph_row)
1687 append_glyph (it);
1690 else
1692 it->pixel_width = CHAR_WIDTH (it->c);
1693 it->nglyphs = it->pixel_width;
1695 if (it->glyph_row)
1696 append_glyph (it);
1699 done:
1700 /* Advance current_x by the pixel width as a convenience for
1701 the caller. */
1702 if (it->area == TEXT_AREA)
1703 it->current_x += it->pixel_width;
1704 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1705 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1709 /* Produce a stretch glyph for iterator IT. IT->object is the value
1710 of the glyph property displayed. The value must be a list
1711 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1712 being recognized:
1714 1. `:width WIDTH' specifies that the space should be WIDTH *
1715 canonical char width wide. WIDTH may be an integer or floating
1716 point number.
1718 2. `:align-to HPOS' specifies that the space should be wide enough
1719 to reach HPOS, a value in canonical character units. */
1721 static void
1722 produce_stretch_glyph (struct it *it)
1724 /* (space :width WIDTH ...) */
1725 Lisp_Object prop, plist;
1726 int width = 0, align_to = -1;
1727 int zero_width_ok_p = 0;
1728 double tem;
1730 /* List should start with `space'. */
1731 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1732 plist = XCDR (it->object);
1734 /* Compute the width of the stretch. */
1735 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1736 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1738 /* Absolute width `:width WIDTH' specified and valid. */
1739 zero_width_ok_p = 1;
1740 width = (int)(tem + 0.5);
1742 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1743 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1745 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1746 align_to = (align_to < 0
1748 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1749 else if (align_to < 0)
1750 align_to = window_box_left_offset (it->w, TEXT_AREA);
1751 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1752 zero_width_ok_p = 1;
1754 else
1755 /* Nothing specified -> width defaults to canonical char width. */
1756 width = FRAME_COLUMN_WIDTH (it->f);
1758 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1759 width = 1;
1761 if (width > 0 && it->line_wrap != TRUNCATE
1762 && it->current_x + width > it->last_visible_x)
1763 width = it->last_visible_x - it->current_x - 1;
1765 if (width > 0 && it->glyph_row)
1767 Lisp_Object o_object = it->object;
1768 Lisp_Object object = it->stack[it->sp - 1].string;
1769 int n = width;
1771 if (!STRINGP (object))
1772 object = it->w->buffer;
1773 it->object = object;
1774 it->char_to_display = ' ';
1775 it->pixel_width = it->len = 1;
1776 while (n--)
1777 append_glyph (it);
1778 it->object = o_object;
1780 it->pixel_width = width;
1781 it->nglyphs = width;
1785 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1786 Called from produce_composite_glyph for terminal frames if
1787 IT->glyph_row != NULL. IT->face_id contains the character's
1788 face. */
1790 static void
1791 append_composite_glyph (struct it *it)
1793 struct glyph *glyph;
1795 xassert (it->glyph_row);
1796 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1797 if (glyph < it->glyph_row->glyphs[1 + it->area])
1799 /* If the glyph row is reversed, we need to prepend the glyph
1800 rather than append it. */
1801 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1803 struct glyph *g;
1805 /* Make room for the new glyph. */
1806 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1807 g[1] = *g;
1808 glyph = it->glyph_row->glyphs[it->area];
1810 glyph->type = COMPOSITE_GLYPH;
1811 glyph->pixel_width = it->pixel_width;
1812 glyph->u.cmp.id = it->cmp_it.id;
1813 if (it->cmp_it.ch < 0)
1815 glyph->u.cmp.automatic = 0;
1816 glyph->u.cmp.id = it->cmp_it.id;
1818 else
1820 glyph->u.cmp.automatic = 1;
1821 glyph->u.cmp.id = it->cmp_it.id;
1822 glyph->u.cmp.from = it->cmp_it.from;
1823 glyph->u.cmp.to = it->cmp_it.to - 1;
1826 glyph->face_id = it->face_id;
1827 glyph->padding_p = 0;
1828 glyph->charpos = CHARPOS (it->position);
1829 glyph->object = it->object;
1830 if (it->bidi_p)
1832 glyph->resolved_level = it->bidi_it.resolved_level;
1833 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1834 abort ();
1835 glyph->bidi_type = it->bidi_it.type;
1837 else
1839 glyph->resolved_level = 0;
1840 glyph->bidi_type = UNKNOWN_BT;
1843 ++it->glyph_row->used[it->area];
1844 ++glyph;
1849 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1850 the composition. We simply produces components of the composition
1851 assuming that the terminal has a capability to layout/render it
1852 correctly. */
1854 static void
1855 produce_composite_glyph (struct it *it)
1857 int c;
1859 if (it->cmp_it.ch < 0)
1861 struct composition *cmp = composition_table[it->cmp_it.id];
1863 it->pixel_width = cmp->width;
1865 else
1867 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
1869 it->pixel_width = composition_gstring_width (gstring, it->cmp_it.from,
1870 it->cmp_it.to, NULL);
1872 it->nglyphs = 1;
1873 if (it->glyph_row)
1874 append_composite_glyph (it);
1878 /* Get information about special display element WHAT in an
1879 environment described by IT. WHAT is one of IT_TRUNCATION or
1880 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1881 non-null glyph_row member. This function ensures that fields like
1882 face_id, c, len of IT are left untouched. */
1884 void
1885 produce_special_glyphs (struct it *it, enum display_element_type what)
1887 struct it temp_it;
1888 Lisp_Object gc;
1889 GLYPH glyph;
1891 temp_it = *it;
1892 temp_it.dp = NULL;
1893 temp_it.what = IT_CHARACTER;
1894 temp_it.len = 1;
1895 temp_it.object = make_number (0);
1896 memset (&temp_it.current, 0, sizeof temp_it.current);
1898 if (what == IT_CONTINUATION)
1900 /* Continuation glyph. For R2L lines, we mirror it by hand. */
1901 if (it->bidi_it.paragraph_dir == R2L)
1902 SET_GLYPH_FROM_CHAR (glyph, '/');
1903 else
1904 SET_GLYPH_FROM_CHAR (glyph, '\\');
1905 if (it->dp
1906 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc))
1907 && GLYPH_CODE_CHAR_VALID_P (gc))
1909 /* FIXME: Should we mirror GC for R2L lines? */
1910 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1911 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1914 else if (what == IT_TRUNCATION)
1916 /* Truncation glyph. */
1917 SET_GLYPH_FROM_CHAR (glyph, '$');
1918 if (it->dp
1919 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc))
1920 && GLYPH_CODE_CHAR_VALID_P (gc))
1922 /* FIXME: Should we mirror GC for R2L lines? */
1923 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1924 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1927 else
1928 abort ();
1930 temp_it.c = GLYPH_CHAR (glyph);
1931 temp_it.face_id = GLYPH_FACE (glyph);
1932 temp_it.len = CHAR_BYTES (temp_it.c);
1934 produce_glyphs (&temp_it);
1935 it->pixel_width = temp_it.pixel_width;
1936 it->nglyphs = temp_it.pixel_width;
1941 /***********************************************************************
1942 Faces
1943 ***********************************************************************/
1945 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1946 one of the enumerators from enum no_color_bit, or a bit set built
1947 from them. Some display attributes may not be used together with
1948 color; the termcap capability `NC' specifies which ones. */
1950 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1951 (tty->TN_max_colors > 0 \
1952 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1953 : 1)
1955 /* Turn appearances of face FACE_ID on tty frame F on.
1956 FACE_ID is a realized face ID number, in the face cache. */
1958 static void
1959 turn_on_face (struct frame *f, int face_id)
1961 struct face *face = FACE_FROM_ID (f, face_id);
1962 long fg = face->foreground;
1963 long bg = face->background;
1964 struct tty_display_info *tty = FRAME_TTY (f);
1966 /* Do this first because TS_end_standout_mode may be the same
1967 as TS_exit_attribute_mode, which turns all appearances off. */
1968 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1970 if (tty->TN_max_colors > 0)
1972 if (fg >= 0 && bg >= 0)
1974 /* If the terminal supports colors, we can set them
1975 below without using reverse video. The face's fg
1976 and bg colors are set as they should appear on
1977 the screen, i.e. they take the inverse-video'ness
1978 of the face already into account. */
1980 else if (inverse_video)
1982 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1983 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1984 tty_toggle_highlight (tty);
1986 else
1988 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1989 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1990 tty_toggle_highlight (tty);
1993 else
1995 /* If we can't display colors, use reverse video
1996 if the face specifies that. */
1997 if (inverse_video)
1999 if (fg == FACE_TTY_DEFAULT_FG_COLOR
2000 || bg == FACE_TTY_DEFAULT_BG_COLOR)
2001 tty_toggle_highlight (tty);
2003 else
2005 if (fg == FACE_TTY_DEFAULT_BG_COLOR
2006 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2007 tty_toggle_highlight (tty);
2012 if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
2013 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
2015 if (face->tty_dim_p && MAY_USE_WITH_COLORS_P (tty, NC_DIM))
2016 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
2018 /* Alternate charset and blinking not yet used. */
2019 if (face->tty_alt_charset_p
2020 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
2021 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
2023 if (face->tty_blinking_p
2024 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
2025 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
2027 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
2028 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
2030 if (tty->TN_max_colors > 0)
2032 char *ts, *p;
2034 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
2035 if (fg >= 0 && ts)
2037 p = tparam (ts, NULL, 0, (int) fg);
2038 OUTPUT (tty, p);
2039 xfree (p);
2042 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
2043 if (bg >= 0 && ts)
2045 p = tparam (ts, NULL, 0, (int) bg);
2046 OUTPUT (tty, p);
2047 xfree (p);
2053 /* Turn off appearances of face FACE_ID on tty frame F. */
2055 static void
2056 turn_off_face (struct frame *f, int face_id)
2058 struct face *face = FACE_FROM_ID (f, face_id);
2059 struct tty_display_info *tty = FRAME_TTY (f);
2061 xassert (face != NULL);
2063 if (tty->TS_exit_attribute_mode)
2065 /* Capability "me" will turn off appearance modes double-bright,
2066 half-bright, reverse-video, standout, underline. It may or
2067 may not turn off alt-char-mode. */
2068 if (face->tty_bold_p
2069 || face->tty_dim_p
2070 || face->tty_reverse_p
2071 || face->tty_alt_charset_p
2072 || face->tty_blinking_p
2073 || face->tty_underline_p)
2075 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
2076 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
2077 tty->standout_mode = 0;
2080 if (face->tty_alt_charset_p)
2081 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2083 else
2085 /* If we don't have "me" we can only have those appearances
2086 that have exit sequences defined. */
2087 if (face->tty_alt_charset_p)
2088 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2090 if (face->tty_underline_p)
2091 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
2094 /* Switch back to default colors. */
2095 if (tty->TN_max_colors > 0
2096 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2097 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2098 || (face->background != FACE_TTY_DEFAULT_COLOR
2099 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
2100 OUTPUT1_IF (tty, tty->TS_orig_pair);
2104 /* Return non-zero if the terminal on frame F supports all of the
2105 capabilities in CAPS simultaneously, with foreground and background
2106 colors FG and BG. */
2109 tty_capable_p (struct tty_display_info *tty, unsigned int caps,
2110 unsigned long fg, unsigned long bg)
2112 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2113 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2114 return 0;
2116 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2117 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2118 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2119 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
2120 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
2121 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
2123 /* We can do it! */
2124 return 1;
2127 /* Return non-zero if the terminal is capable to display colors. */
2129 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2130 0, 1, 0,
2131 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2133 TERMINAL can be a terminal object, a frame, or nil (meaning the
2134 selected frame's terminal). This function always returns nil if
2135 TERMINAL does not refer to a text-only terminal. */)
2136 (Lisp_Object terminal)
2138 struct terminal *t = get_tty_terminal (terminal, 0);
2139 if (!t)
2140 return Qnil;
2141 else
2142 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
2145 /* Return the number of supported colors. */
2146 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2147 Stty_display_color_cells, 0, 1, 0,
2148 doc: /* Return the number of colors supported by the tty device TERMINAL.
2150 TERMINAL can be a terminal object, a frame, or nil (meaning the
2151 selected frame's terminal). This function always returns 0 if
2152 TERMINAL does not refer to a text-only terminal. */)
2153 (Lisp_Object terminal)
2155 struct terminal *t = get_tty_terminal (terminal, 0);
2156 if (!t)
2157 return make_number (0);
2158 else
2159 return make_number (t->display_info.tty->TN_max_colors);
2162 #ifndef DOS_NT
2164 /* Declare here rather than in the function, as in the rest of Emacs,
2165 to work around an HPUX compiler bug (?). See
2166 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2167 static int default_max_colors;
2168 static int default_max_pairs;
2169 static int default_no_color_video;
2170 static char *default_orig_pair;
2171 static char *default_set_foreground;
2172 static char *default_set_background;
2174 /* Save or restore the default color-related capabilities of this
2175 terminal. */
2176 static void
2177 tty_default_color_capabilities (struct tty_display_info *tty, int save)
2180 if (save)
2182 xfree (default_orig_pair);
2183 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
2185 xfree (default_set_foreground);
2186 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
2187 : NULL;
2189 xfree (default_set_background);
2190 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
2191 : NULL;
2193 default_max_colors = tty->TN_max_colors;
2194 default_max_pairs = tty->TN_max_pairs;
2195 default_no_color_video = tty->TN_no_color_video;
2197 else
2199 tty->TS_orig_pair = default_orig_pair;
2200 tty->TS_set_foreground = default_set_foreground;
2201 tty->TS_set_background = default_set_background;
2202 tty->TN_max_colors = default_max_colors;
2203 tty->TN_max_pairs = default_max_pairs;
2204 tty->TN_no_color_video = default_no_color_video;
2208 /* Setup one of the standard tty color schemes according to MODE.
2209 MODE's value is generally the number of colors which we want to
2210 support; zero means set up for the default capabilities, the ones
2211 we saw at init_tty time; -1 means turn off color support. */
2212 static void
2213 tty_setup_colors (struct tty_display_info *tty, int mode)
2215 /* Canonicalize all negative values of MODE. */
2216 if (mode < -1)
2217 mode = -1;
2219 switch (mode)
2221 case -1: /* no colors at all */
2222 tty->TN_max_colors = 0;
2223 tty->TN_max_pairs = 0;
2224 tty->TN_no_color_video = 0;
2225 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2226 break;
2227 case 0: /* default colors, if any */
2228 default:
2229 tty_default_color_capabilities (tty, 0);
2230 break;
2231 case 8: /* 8 standard ANSI colors */
2232 tty->TS_orig_pair = "\033[0m";
2233 #ifdef TERMINFO
2234 tty->TS_set_foreground = "\033[3%p1%dm";
2235 tty->TS_set_background = "\033[4%p1%dm";
2236 #else
2237 tty->TS_set_foreground = "\033[3%dm";
2238 tty->TS_set_background = "\033[4%dm";
2239 #endif
2240 tty->TN_max_colors = 8;
2241 tty->TN_max_pairs = 64;
2242 tty->TN_no_color_video = 0;
2243 break;
2247 void
2248 set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
2250 Lisp_Object tem, val;
2251 Lisp_Object color_mode;
2252 int mode;
2253 extern Lisp_Object Qtty_color_mode;
2254 Lisp_Object tty_color_mode_alist
2255 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
2257 tem = assq_no_quit (Qtty_color_mode, f->param_alist);
2258 val = CONSP (tem) ? XCDR (tem) : Qnil;
2260 if (INTEGERP (val))
2261 color_mode = val;
2262 else if (SYMBOLP (tty_color_mode_alist))
2264 tem = Fassq (val, Fsymbol_value (tty_color_mode_alist));
2265 color_mode = CONSP (tem) ? XCDR (tem) : Qnil;
2267 else
2268 color_mode = Qnil;
2270 mode = INTEGERP (color_mode) ? XINT (color_mode) : 0;
2272 if (mode != tty->previous_color_mode)
2274 Lisp_Object funsym = intern ("tty-set-up-initial-frame-faces");
2275 tty->previous_color_mode = mode;
2276 tty_setup_colors (tty , mode);
2277 /* This recomputes all the faces given the new color definitions. */
2278 safe_call (1, &funsym);
2282 #endif /* !DOS_NT */
2286 /* Return the tty display object specified by TERMINAL. */
2288 struct terminal *
2289 get_tty_terminal (Lisp_Object terminal, int throw)
2291 struct terminal *t = get_terminal (terminal, throw);
2293 if (t && t->type != output_termcap && t->type != output_msdos_raw)
2295 if (throw)
2296 error ("Device %d is not a termcap terminal device", t->id);
2297 else
2298 return NULL;
2301 return t;
2304 /* Return an active termcap device that uses the tty device with the
2305 given name.
2307 This function ignores suspended devices.
2309 Returns NULL if the named terminal device is not opened. */
2311 struct terminal *
2312 get_named_tty (char *name)
2314 struct terminal *t;
2316 if (!name)
2317 abort ();
2319 for (t = terminal_list; t; t = t->next_terminal)
2321 if ((t->type == output_termcap || t->type == output_msdos_raw)
2322 && !strcmp (t->display_info.tty->name, name)
2323 && TERMINAL_ACTIVE_P (t))
2324 return t;
2327 return 0;
2331 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2332 doc: /* Return the type of the tty device that TERMINAL uses.
2333 Returns nil if TERMINAL is not on a tty device.
2335 TERMINAL can be a terminal object, a frame, or nil (meaning the
2336 selected frame's terminal). */)
2337 (Lisp_Object terminal)
2339 struct terminal *t = get_terminal (terminal, 1);
2341 if (t->type != output_termcap && t->type != output_msdos_raw)
2342 return Qnil;
2344 if (t->display_info.tty->type)
2345 return build_string (t->display_info.tty->type);
2346 else
2347 return Qnil;
2350 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2351 doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2353 TERMINAL can be a terminal object, a frame, or nil (meaning the
2354 selected frame's terminal). This function always returns nil if
2355 TERMINAL is not on a tty device. */)
2356 (Lisp_Object terminal)
2358 struct terminal *t = get_terminal (terminal, 1);
2360 if ((t->type != output_termcap && t->type != output_msdos_raw)
2361 || strcmp (t->display_info.tty->name, DEV_TTY) != 0)
2362 return Qnil;
2363 else
2364 return Qt;
2367 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2368 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2369 This is used to override the terminfo data, for certain terminals that
2370 do not really do underlining, but say that they do. This function has
2371 no effect if used on a non-tty terminal.
2373 TERMINAL can be a terminal object, a frame or nil (meaning the
2374 selected frame's terminal). This function always returns nil if
2375 TERMINAL does not refer to a text-only terminal. */)
2376 (Lisp_Object terminal)
2378 struct terminal *t = get_terminal (terminal, 1);
2380 if (t->type == output_termcap)
2381 t->display_info.tty->TS_enter_underline_mode = 0;
2382 return Qnil;
2387 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2388 doc: /* Suspend the terminal device TTY.
2390 The device is restored to its default state, and Emacs ceases all
2391 access to the tty device. Frames that use the device are not deleted,
2392 but input is not read from them and if they change, their display is
2393 not updated.
2395 TTY may be a terminal object, a frame, or nil for the terminal device
2396 of the currently selected frame.
2398 This function runs `suspend-tty-functions' after suspending the
2399 device. The functions are run with one arg, the id of the suspended
2400 terminal device.
2402 `suspend-tty' does nothing if it is called on a device that is already
2403 suspended.
2405 A suspended tty may be resumed by calling `resume-tty' on it. */)
2406 (Lisp_Object tty)
2408 struct terminal *t = get_tty_terminal (tty, 1);
2409 FILE *f;
2411 if (!t)
2412 error ("Unknown tty device");
2414 f = t->display_info.tty->input;
2416 if (f)
2418 /* First run `suspend-tty-functions' and then clean up the tty
2419 state because `suspend-tty-functions' might need to change
2420 the tty state. */
2421 if (!NILP (Vrun_hooks))
2423 Lisp_Object args[2];
2424 args[0] = intern ("suspend-tty-functions");
2425 XSETTERMINAL (args[1], t);
2426 Frun_hook_with_args (2, args);
2429 reset_sys_modes (t->display_info.tty);
2431 #ifdef subprocesses
2432 delete_keyboard_wait_descriptor (fileno (f));
2433 #endif
2435 #ifndef MSDOS
2436 fclose (f);
2437 if (f != t->display_info.tty->output)
2438 fclose (t->display_info.tty->output);
2439 #endif
2441 t->display_info.tty->input = 0;
2442 t->display_info.tty->output = 0;
2444 if (FRAMEP (t->display_info.tty->top_frame))
2445 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2449 /* Clear display hooks to prevent further output. */
2450 clear_tty_hooks (t);
2452 return Qnil;
2455 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2456 doc: /* Resume the previously suspended terminal device TTY.
2457 The terminal is opened and reinitialized. Frames that are on the
2458 suspended terminal are revived.
2460 It is an error to resume a terminal while another terminal is active
2461 on the same device.
2463 This function runs `resume-tty-functions' after resuming the terminal.
2464 The functions are run with one arg, the id of the resumed terminal
2465 device.
2467 `resume-tty' does nothing if it is called on a device that is not
2468 suspended.
2470 TTY may be a terminal object, a frame, or nil (meaning the selected
2471 frame's terminal). */)
2472 (Lisp_Object tty)
2474 struct terminal *t = get_tty_terminal (tty, 1);
2475 int fd;
2477 if (!t)
2478 error ("Unknown tty device");
2480 if (!t->display_info.tty->input)
2482 if (get_named_tty (t->display_info.tty->name))
2483 error ("Cannot resume display while another display is active on the same device");
2485 #ifdef MSDOS
2486 t->display_info.tty->output = stdout;
2487 t->display_info.tty->input = stdin;
2488 #else /* !MSDOS */
2489 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2491 if (fd == -1)
2492 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2494 if (strcmp (t->display_info.tty->name, DEV_TTY))
2495 dissociate_if_controlling_tty (fd);
2497 t->display_info.tty->output = fdopen (fd, "w+");
2498 t->display_info.tty->input = t->display_info.tty->output;
2499 #endif
2501 #ifdef subprocesses
2502 add_keyboard_wait_descriptor (fd);
2503 #endif
2505 if (FRAMEP (t->display_info.tty->top_frame))
2507 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2508 int width, height;
2509 int old_height = FRAME_COLS (f);
2510 int old_width = FRAME_LINES (f);
2512 /* Check if terminal/window size has changed while the frame
2513 was suspended. */
2514 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2515 if (width != old_width || height != old_height)
2516 change_frame_size (f, height, width, 0, 0, 0);
2517 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2520 init_sys_modes (t->display_info.tty);
2522 /* Run `resume-tty-functions'. */
2523 if (!NILP (Vrun_hooks))
2525 Lisp_Object args[2];
2526 args[0] = intern ("resume-tty-functions");
2527 XSETTERMINAL (args[1], t);
2528 Frun_hook_with_args (2, args);
2532 set_tty_hooks (t);
2534 return Qnil;
2538 /***********************************************************************
2539 Mouse
2540 ***********************************************************************/
2542 #ifdef HAVE_GPM
2543 void
2544 term_mouse_moveto (int x, int y)
2546 /* TODO: how to set mouse position?
2547 const char *name;
2548 int fd;
2549 name = (const char *) ttyname (0);
2550 fd = open (name, O_WRONLY);
2551 SOME_FUNCTION (x, y, fd);
2552 close (fd);
2553 last_mouse_x = x;
2554 last_mouse_y = y; */
2557 static void
2558 term_show_mouse_face (enum draw_glyphs_face draw)
2560 struct window *w = XWINDOW (mouse_face_window);
2561 int save_x, save_y;
2562 int i;
2564 struct frame *f = XFRAME (w->frame);
2565 struct tty_display_info *tty = FRAME_TTY (f);
2567 if (/* If window is in the process of being destroyed, don't bother
2568 to do anything. */
2569 w->current_matrix != NULL
2570 /* Recognize when we are called to operate on rows that don't exist
2571 anymore. This can happen when a window is split. */
2572 && mouse_face_end_row < w->current_matrix->nrows)
2574 /* write_glyphs writes at cursor position, so we need to
2575 temporarily move cursor coordinates to the beginning of
2576 the highlight region. */
2578 /* Save current cursor co-ordinates */
2579 save_y = curY (tty);
2580 save_x = curX (tty);
2582 /* Note that mouse_face_beg_row etc. are window relative. */
2583 for (i = mouse_face_beg_row; i <= mouse_face_end_row; i++)
2585 int start_hpos, end_hpos, nglyphs;
2586 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
2588 /* Don't do anything if row doesn't have valid contents. */
2589 if (!row->enabled_p)
2590 continue;
2592 /* For all but the first row, the highlight starts at column 0. */
2593 if (i == mouse_face_beg_row)
2594 start_hpos = mouse_face_beg_col;
2595 else
2596 start_hpos = 0;
2598 if (i == mouse_face_end_row)
2599 end_hpos = mouse_face_end_col;
2600 else
2602 end_hpos = row->used[TEXT_AREA];
2603 if (draw == DRAW_NORMAL_TEXT)
2604 row->fill_line_p = 1; /* Clear to end of line */
2607 if (end_hpos <= start_hpos)
2608 continue;
2609 /* Record that some glyphs of this row are displayed in
2610 mouse-face. */
2611 row->mouse_face_p = draw > 0;
2613 nglyphs = end_hpos - start_hpos;
2615 if (end_hpos >= row->used[TEXT_AREA])
2616 nglyphs = row->used[TEXT_AREA] - start_hpos;
2618 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2619 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos
2620 + WINDOW_LEFT_EDGE_X (w);
2622 cursor_to (f, pos_y, pos_x);
2624 if (draw == DRAW_MOUSE_FACE)
2626 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2627 nglyphs, mouse_face_face_id);
2629 else /* draw == DRAW_NORMAL_TEXT */
2630 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
2632 cursor_to (f, save_y, save_x);
2636 static void
2637 term_clear_mouse_face (void)
2639 if (!NILP (mouse_face_window))
2640 term_show_mouse_face (DRAW_NORMAL_TEXT);
2642 mouse_face_beg_row = mouse_face_beg_col = -1;
2643 mouse_face_end_row = mouse_face_end_col = -1;
2644 mouse_face_window = Qnil;
2647 /* Find the glyph matrix position of buffer position POS in window W.
2648 *HPOS and *VPOS are set to the positions found. W's current glyphs
2649 must be up to date. If POS is above window start return (0, 0).
2650 If POS is after end of W, return end of last line in W.
2651 - taken from msdos.c */
2652 static int
2653 fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
2655 int i, lastcol, line_start_position, maybe_next_line_p = 0;
2656 int yb = window_text_bottom_y (w);
2657 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0), *best_row = row;
2659 while (row->y < yb)
2661 if (row->used[TEXT_AREA])
2662 line_start_position = row->glyphs[TEXT_AREA]->charpos;
2663 else
2664 line_start_position = 0;
2666 if (line_start_position > pos)
2667 break;
2668 /* If the position sought is the end of the buffer,
2669 don't include the blank lines at the bottom of the window. */
2670 else if (line_start_position == pos
2671 && pos == BUF_ZV (XBUFFER (w->buffer)))
2673 maybe_next_line_p = 1;
2674 break;
2676 else if (line_start_position > 0)
2677 best_row = row;
2679 /* Don't overstep the last matrix row, lest we get into the
2680 never-never land... */
2681 if (row->y + 1 >= yb)
2682 break;
2684 ++row;
2687 /* Find the right column within BEST_ROW. */
2688 lastcol = 0;
2689 row = best_row;
2690 for (i = 0; i < row->used[TEXT_AREA]; i++)
2692 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
2693 int charpos;
2695 charpos = glyph->charpos;
2696 if (charpos == pos)
2698 *hpos = i;
2699 *vpos = row->y;
2700 return 1;
2702 else if (charpos > pos)
2703 break;
2704 else if (charpos > 0)
2705 lastcol = i;
2708 /* If we're looking for the end of the buffer,
2709 and we didn't find it in the line we scanned,
2710 use the start of the following line. */
2711 if (maybe_next_line_p)
2713 ++row;
2714 lastcol = 0;
2717 *vpos = row->y;
2718 *hpos = lastcol + 1;
2719 return 0;
2722 static void
2723 term_mouse_highlight (struct frame *f, int x, int y)
2725 enum window_part part;
2726 Lisp_Object window;
2727 struct window *w;
2728 struct buffer *b;
2730 if (NILP (Vmouse_highlight)
2731 || !f->glyphs_initialized_p)
2732 return;
2734 /* Which window is that in? */
2735 window = window_from_coordinates (f, x, y, &part, &x, &y, 0);
2737 /* Not on a window -> return. */
2738 if (!WINDOWP (window))
2739 return;
2741 if (!EQ (window, mouse_face_window))
2742 term_clear_mouse_face ();
2744 w = XWINDOW (window);
2746 /* Are we in a window whose display is up to date?
2747 And verify the buffer's text has not changed. */
2748 b = XBUFFER (w->buffer);
2749 if (part == ON_TEXT
2750 && EQ (w->window_end_valid, w->buffer)
2751 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
2752 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
2754 int pos, i, nrows = w->current_matrix->nrows;
2755 struct glyph_row *row;
2756 struct glyph *glyph;
2758 /* Find the glyph under X/Y. */
2759 glyph = NULL;
2760 if (y >= 0 && y < nrows)
2762 row = MATRIX_ROW (w->current_matrix, y);
2763 /* Give up if some row before the one we are looking for is
2764 not enabled. */
2765 for (i = 0; i <= y; i++)
2766 if (!MATRIX_ROW (w->current_matrix, i)->enabled_p)
2767 break;
2768 if (i > y /* all rows upto and including the one at Y are enabled */
2769 && row->displays_text_p
2770 && x < window_box_width (w, TEXT_AREA))
2772 glyph = row->glyphs[TEXT_AREA];
2773 if (x >= row->used[TEXT_AREA])
2774 glyph = NULL;
2775 else
2777 glyph += x;
2778 if (!BUFFERP (glyph->object))
2779 glyph = NULL;
2784 /* Clear mouse face if X/Y not over text. */
2785 if (glyph == NULL)
2787 term_clear_mouse_face ();
2788 return;
2791 if (!BUFFERP (glyph->object))
2792 abort ();
2793 pos = glyph->charpos;
2795 /* Check for mouse-face. */
2797 extern Lisp_Object Qmouse_face;
2798 Lisp_Object mouse_face, overlay, position, *overlay_vec;
2799 int noverlays, obegv, ozv;
2800 struct buffer *obuf;
2802 /* If we get an out-of-range value, return now; avoid an error. */
2803 if (pos > BUF_Z (b))
2804 return;
2806 /* Make the window's buffer temporarily current for
2807 overlays_at and compute_char_face. */
2808 obuf = current_buffer;
2809 current_buffer = b;
2810 obegv = BEGV;
2811 ozv = ZV;
2812 BEGV = BEG;
2813 ZV = Z;
2815 /* Is this char mouse-active? */
2816 XSETINT (position, pos);
2818 /* Put all the overlays we want in a vector in overlay_vec. */
2819 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
2820 /* Sort overlays into increasing priority order. */
2821 noverlays = sort_overlays (overlay_vec, noverlays, w);
2823 /* Check mouse-face highlighting. */
2824 if (!(EQ (window, mouse_face_window)
2825 && y >= mouse_face_beg_row
2826 && y <= mouse_face_end_row
2827 && (y > mouse_face_beg_row
2828 || x >= mouse_face_beg_col)
2829 && (y < mouse_face_end_row
2830 || x < mouse_face_end_col
2831 || mouse_face_past_end)))
2833 /* Clear the display of the old active region, if any. */
2834 term_clear_mouse_face ();
2836 /* Find the highest priority overlay that has a mouse-face
2837 property. */
2838 overlay = Qnil;
2839 for (i = noverlays - 1; i >= 0; --i)
2841 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
2842 if (!NILP (mouse_face))
2844 overlay = overlay_vec[i];
2845 break;
2849 /* If no overlay applies, get a text property. */
2850 if (NILP (overlay))
2851 mouse_face = Fget_text_property (position, Qmouse_face,
2852 w->buffer);
2854 /* Handle the overlay case. */
2855 if (!NILP (overlay))
2857 /* Find the range of text around this char that
2858 should be active. */
2859 Lisp_Object before, after;
2860 EMACS_INT ignore;
2863 before = Foverlay_start (overlay);
2864 after = Foverlay_end (overlay);
2865 /* Record this as the current active region. */
2866 fast_find_position (w, XFASTINT (before),
2867 &mouse_face_beg_col,
2868 &mouse_face_beg_row);
2870 mouse_face_past_end
2871 = !fast_find_position (w, XFASTINT (after),
2872 &mouse_face_end_col,
2873 &mouse_face_end_row);
2874 mouse_face_window = window;
2876 mouse_face_face_id
2877 = face_at_buffer_position (w, pos, 0, 0,
2878 &ignore, pos + 1, 1, -1);
2880 /* Display it as active. */
2881 term_show_mouse_face (DRAW_MOUSE_FACE);
2883 /* Handle the text property case. */
2884 else if (!NILP (mouse_face))
2886 /* Find the range of text around this char that
2887 should be active. */
2888 Lisp_Object before, after, beginning, end;
2889 EMACS_INT ignore;
2891 beginning = Fmarker_position (w->start);
2892 XSETINT (end, (BUF_Z (b) - XFASTINT (w->window_end_pos)));
2893 before
2894 = Fprevious_single_property_change (make_number (pos + 1),
2895 Qmouse_face,
2896 w->buffer, beginning);
2897 after
2898 = Fnext_single_property_change (position, Qmouse_face,
2899 w->buffer, end);
2901 /* Record this as the current active region. */
2902 fast_find_position (w, XFASTINT (before),
2903 &mouse_face_beg_col,
2904 &mouse_face_beg_row);
2905 mouse_face_past_end
2906 = !fast_find_position (w, XFASTINT (after),
2907 &mouse_face_end_col,
2908 &mouse_face_end_row);
2909 mouse_face_window = window;
2911 mouse_face_face_id
2912 = face_at_buffer_position (w, pos, 0, 0,
2913 &ignore, pos + 1, 1, -1);
2915 /* Display it as active. */
2916 term_show_mouse_face (DRAW_MOUSE_FACE);
2920 /* Look for a `help-echo' property. */
2922 Lisp_Object help;
2923 extern Lisp_Object Qhelp_echo;
2925 /* Check overlays first. */
2926 help = Qnil;
2927 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
2929 overlay = overlay_vec[i];
2930 help = Foverlay_get (overlay, Qhelp_echo);
2933 if (!NILP (help))
2935 help_echo_string = help;
2936 help_echo_window = window;
2937 help_echo_object = overlay;
2938 help_echo_pos = pos;
2940 /* Try text properties. */
2941 else if (NILP (help)
2942 && ((STRINGP (glyph->object)
2943 && glyph->charpos >= 0
2944 && glyph->charpos < SCHARS (glyph->object))
2945 || (BUFFERP (glyph->object)
2946 && glyph->charpos >= BEGV
2947 && glyph->charpos < ZV)))
2949 help = Fget_text_property (make_number (glyph->charpos),
2950 Qhelp_echo, glyph->object);
2951 if (!NILP (help))
2953 help_echo_string = help;
2954 help_echo_window = window;
2955 help_echo_object = glyph->object;
2956 help_echo_pos = glyph->charpos;
2961 BEGV = obegv;
2962 ZV = ozv;
2963 current_buffer = obuf;
2968 static int
2969 term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2971 /* Has the mouse moved off the glyph it was on at the last sighting? */
2972 if (event->x != last_mouse_x || event->y != last_mouse_y)
2974 frame->mouse_moved = 1;
2975 term_mouse_highlight (frame, event->x, event->y);
2976 /* Remember which glyph we're now on. */
2977 last_mouse_x = event->x;
2978 last_mouse_y = event->y;
2979 return 1;
2981 return 0;
2984 /* Return the current position of the mouse.
2986 Set *f to the frame the mouse is in, or zero if the mouse is in no
2987 Emacs frame. If it is set to zero, all the other arguments are
2988 garbage.
2990 Set *bar_window to Qnil, and *x and *y to the column and
2991 row of the character cell the mouse is over.
2993 Set *time to the time the mouse was at the returned position.
2995 This clears mouse_moved until the next motion
2996 event arrives. */
2997 static void
2998 term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2999 enum scroll_bar_part *part, Lisp_Object *x,
3000 Lisp_Object *y, unsigned long *time)
3002 struct timeval now;
3004 *fp = SELECTED_FRAME ();
3005 (*fp)->mouse_moved = 0;
3007 *bar_window = Qnil;
3008 *part = 0;
3010 XSETINT (*x, last_mouse_x);
3011 XSETINT (*y, last_mouse_y);
3012 gettimeofday(&now, 0);
3013 *time = (now.tv_sec * 1000) + (now.tv_usec / 1000);
3016 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3018 If the event is a button press, then note that we have grabbed
3019 the mouse. */
3021 static Lisp_Object
3022 term_mouse_click (struct input_event *result, Gpm_Event *event,
3023 struct frame *f)
3025 struct timeval now;
3026 int i, j;
3028 result->kind = GPM_CLICK_EVENT;
3029 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
3031 if (event->buttons & j) {
3032 result->code = i; /* button number */
3033 break;
3036 gettimeofday(&now, 0);
3037 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
3039 if (event->type & GPM_UP)
3040 result->modifiers = up_modifier;
3041 else if (event->type & GPM_DOWN)
3042 result->modifiers = down_modifier;
3043 else
3044 result->modifiers = 0;
3046 if (event->type & GPM_SINGLE)
3047 result->modifiers |= click_modifier;
3049 if (event->type & GPM_DOUBLE)
3050 result->modifiers |= double_modifier;
3052 if (event->type & GPM_TRIPLE)
3053 result->modifiers |= triple_modifier;
3055 if (event->type & GPM_DRAG)
3056 result->modifiers |= drag_modifier;
3058 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
3060 /* 1 << KG_SHIFT */
3061 if (event->modifiers & (1 << 0))
3062 result->modifiers |= shift_modifier;
3064 /* 1 << KG_CTRL */
3065 if (event->modifiers & (1 << 2))
3066 result->modifiers |= ctrl_modifier;
3068 /* 1 << KG_ALT || KG_ALTGR */
3069 if (event->modifiers & (1 << 3)
3070 || event->modifiers & (1 << 1))
3071 result->modifiers |= meta_modifier;
3074 XSETINT (result->x, event->x);
3075 XSETINT (result->y, event->y);
3076 XSETFRAME (result->frame_or_window, f);
3077 result->arg = Qnil;
3078 return Qnil;
3082 handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
3084 struct frame *f = XFRAME (tty->top_frame);
3085 struct input_event ie;
3086 int do_help = 0;
3087 int count = 0;
3089 EVENT_INIT (ie);
3090 ie.kind = NO_EVENT;
3091 ie.arg = Qnil;
3093 if (event->type & (GPM_MOVE | GPM_DRAG)) {
3094 previous_help_echo_string = help_echo_string;
3095 help_echo_string = Qnil;
3097 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
3099 if (!term_mouse_movement (f, event))
3100 help_echo_string = previous_help_echo_string;
3102 /* If the contents of the global variable help_echo_string
3103 has changed, generate a HELP_EVENT. */
3104 if (!NILP (help_echo_string)
3105 || !NILP (previous_help_echo_string))
3106 do_help = 1;
3108 goto done;
3110 else {
3111 f->mouse_moved = 0;
3112 term_mouse_click (&ie, event, f);
3115 done:
3116 if (ie.kind != NO_EVENT)
3118 kbd_buffer_store_event_hold (&ie, hold_quit);
3119 count++;
3122 if (do_help
3123 && !(hold_quit && hold_quit->kind != NO_EVENT))
3125 Lisp_Object frame;
3127 if (f)
3128 XSETFRAME (frame, f);
3129 else
3130 frame = Qnil;
3132 gen_help_event (help_echo_string, frame, help_echo_window,
3133 help_echo_object, help_echo_pos);
3134 count++;
3137 return count;
3140 DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
3141 0, 0, 0,
3142 doc: /* Open a connection to Gpm.
3143 Gpm-mouse can only be activated for one tty at a time. */)
3144 (void)
3146 struct frame *f = SELECTED_FRAME ();
3147 struct tty_display_info *tty
3148 = ((f)->output_method == output_termcap
3149 ? (f)->terminal->display_info.tty : NULL);
3150 Gpm_Connect connection;
3152 if (!tty)
3153 error ("Gpm-mouse only works in the GNU/Linux console");
3154 if (gpm_tty == tty)
3155 return Qnil; /* Already activated, nothing to do. */
3156 if (gpm_tty)
3157 error ("Gpm-mouse can only be activated for one tty at a time");
3159 connection.eventMask = ~0;
3160 connection.defaultMask = ~GPM_HARD;
3161 connection.maxMod = ~0;
3162 connection.minMod = 0;
3163 gpm_zerobased = 1;
3165 if (Gpm_Open (&connection, 0) < 0)
3166 error ("Gpm-mouse failed to connect to the gpm daemon");
3167 else
3169 gpm_tty = tty;
3170 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
3171 to generate SIGIOs. Apparently we need to call reset_sys_modes
3172 before calling init_sys_modes. */
3173 reset_sys_modes (tty);
3174 init_sys_modes (tty);
3175 add_gpm_wait_descriptor (gpm_fd);
3176 return Qnil;
3180 void
3181 close_gpm (int fd)
3183 if (fd >= 0)
3184 delete_gpm_wait_descriptor (fd);
3185 while (Gpm_Close()); /* close all the stack */
3186 gpm_tty = NULL;
3189 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
3190 0, 0, 0,
3191 doc: /* Close a connection to Gpm. */)
3192 (void)
3194 struct frame *f = SELECTED_FRAME ();
3195 struct tty_display_info *tty
3196 = ((f)->output_method == output_termcap
3197 ? (f)->terminal->display_info.tty : NULL);
3199 if (!tty || gpm_tty != tty)
3200 return Qnil; /* Not activated on this terminal, nothing to do. */
3202 close_gpm (gpm_fd);
3203 return Qnil;
3205 #endif /* HAVE_GPM */
3208 #ifndef MSDOS
3209 /***********************************************************************
3210 Initialization
3211 ***********************************************************************/
3213 /* Initialize the tty-dependent part of frame F. The frame must
3214 already have its device initialized. */
3216 void
3217 create_tty_output (struct frame *f)
3219 struct tty_output *t;
3221 if (! FRAME_TERMCAP_P (f))
3222 abort ();
3224 t = xmalloc (sizeof (struct tty_output));
3225 memset (t, 0, sizeof (struct tty_output));
3227 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
3229 f->output_data.tty = t;
3232 /* Delete frame F's face cache, and its tty-dependent part. */
3234 static void
3235 tty_free_frame_resources (struct frame *f)
3237 if (! FRAME_TERMCAP_P (f))
3238 abort ();
3240 if (FRAME_FACE_CACHE (f))
3241 free_frame_faces (f);
3243 xfree (f->output_data.tty);
3246 #else /* MSDOS */
3248 /* Delete frame F's face cache. */
3250 static void
3251 tty_free_frame_resources (struct frame *f)
3253 if (! FRAME_TERMCAP_P (f) && ! FRAME_MSDOS_P (f))
3254 abort ();
3256 if (FRAME_FACE_CACHE (f))
3257 free_frame_faces (f);
3259 #endif /* MSDOS */
3261 /* Reset the hooks in TERMINAL. */
3263 static void
3264 clear_tty_hooks (struct terminal *terminal)
3266 terminal->rif = 0;
3267 terminal->cursor_to_hook = 0;
3268 terminal->raw_cursor_to_hook = 0;
3269 terminal->clear_to_end_hook = 0;
3270 terminal->clear_frame_hook = 0;
3271 terminal->clear_end_of_line_hook = 0;
3272 terminal->ins_del_lines_hook = 0;
3273 terminal->insert_glyphs_hook = 0;
3274 terminal->write_glyphs_hook = 0;
3275 terminal->delete_glyphs_hook = 0;
3276 terminal->ring_bell_hook = 0;
3277 terminal->reset_terminal_modes_hook = 0;
3278 terminal->set_terminal_modes_hook = 0;
3279 terminal->update_begin_hook = 0;
3280 terminal->update_end_hook = 0;
3281 terminal->set_terminal_window_hook = 0;
3282 terminal->mouse_position_hook = 0;
3283 terminal->frame_rehighlight_hook = 0;
3284 terminal->frame_raise_lower_hook = 0;
3285 terminal->fullscreen_hook = 0;
3286 terminal->set_vertical_scroll_bar_hook = 0;
3287 terminal->condemn_scroll_bars_hook = 0;
3288 terminal->redeem_scroll_bar_hook = 0;
3289 terminal->judge_scroll_bars_hook = 0;
3290 terminal->read_socket_hook = 0;
3291 terminal->frame_up_to_date_hook = 0;
3293 /* Leave these two set, or suspended frames are not deleted
3294 correctly. */
3295 terminal->delete_frame_hook = &tty_free_frame_resources;
3296 terminal->delete_terminal_hook = &delete_tty;
3299 /* Initialize hooks in TERMINAL with the values needed for a tty. */
3301 static void
3302 set_tty_hooks (struct terminal *terminal)
3304 terminal->rif = 0; /* ttys don't support window-based redisplay. */
3306 terminal->cursor_to_hook = &tty_cursor_to;
3307 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
3309 terminal->clear_to_end_hook = &tty_clear_to_end;
3310 terminal->clear_frame_hook = &tty_clear_frame;
3311 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
3313 terminal->ins_del_lines_hook = &tty_ins_del_lines;
3315 terminal->insert_glyphs_hook = &tty_insert_glyphs;
3316 terminal->write_glyphs_hook = &tty_write_glyphs;
3317 terminal->delete_glyphs_hook = &tty_delete_glyphs;
3319 terminal->ring_bell_hook = &tty_ring_bell;
3321 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
3322 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
3323 terminal->update_begin_hook = 0; /* Not needed. */
3324 terminal->update_end_hook = &tty_update_end;
3325 terminal->set_terminal_window_hook = &tty_set_terminal_window;
3327 terminal->mouse_position_hook = 0; /* Not needed. */
3328 terminal->frame_rehighlight_hook = 0; /* Not needed. */
3329 terminal->frame_raise_lower_hook = 0; /* Not needed. */
3331 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
3332 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
3333 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
3334 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
3336 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
3337 terminal->frame_up_to_date_hook = 0; /* Not needed. */
3339 terminal->delete_frame_hook = &tty_free_frame_resources;
3340 terminal->delete_terminal_hook = &delete_tty;
3343 /* Drop the controlling terminal if fd is the same device. */
3344 static void
3345 dissociate_if_controlling_tty (int fd)
3347 #ifndef DOS_NT
3348 int pgid;
3349 EMACS_GET_TTY_PGRP (fd, &pgid); /* If tcgetpgrp succeeds, fd is the ctty. */
3350 if (pgid != -1)
3352 #if defined (USG)
3353 setpgrp ();
3354 no_controlling_tty = 1;
3355 #elif defined (CYGWIN)
3356 setsid ();
3357 no_controlling_tty = 1;
3358 #else
3359 #ifdef TIOCNOTTY /* Try BSD ioctls. */
3360 sigblock (sigmask (SIGTTOU));
3361 fd = emacs_open (DEV_TTY, O_RDWR, 0);
3362 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
3364 no_controlling_tty = 1;
3366 if (fd != -1)
3367 emacs_close (fd);
3368 sigunblock (sigmask (SIGTTOU));
3369 #else
3370 /* Unknown system. */
3371 croak ();
3372 #endif /* ! TIOCNOTTY */
3373 #endif /* ! USG */
3375 #endif /* !DOS_NT */
3378 static void maybe_fatal();
3380 /* Create a termcap display on the tty device with the given name and
3381 type.
3383 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3384 Otherwise NAME should be a path to the tty device file,
3385 e.g. "/dev/pts/7".
3387 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3389 If MUST_SUCCEED is true, then all errors are fatal. */
3391 struct terminal *
3392 init_tty (char *name, char *terminal_type, int must_succeed)
3394 char *area = NULL;
3395 char **address = &area;
3396 int buffer_size = 4096;
3397 register char *p = NULL;
3398 int status;
3399 struct tty_display_info *tty = NULL;
3400 struct terminal *terminal = NULL;
3401 int ctty = 0; /* 1 if asked to open controlling tty. */
3403 if (!terminal_type)
3404 maybe_fatal (must_succeed, 0,
3405 "Unknown terminal type",
3406 "Unknown terminal type");
3408 if (name == NULL)
3409 name = DEV_TTY;
3410 if (!strcmp (name, DEV_TTY))
3411 ctty = 1;
3413 /* If we already have a terminal on the given device, use that. If
3414 all such terminals are suspended, create a new one instead. */
3415 /* XXX Perhaps this should be made explicit by having init_tty
3416 always create a new terminal and separating terminal and frame
3417 creation on Lisp level. */
3418 terminal = get_named_tty (name);
3419 if (terminal)
3420 return terminal;
3422 terminal = create_terminal ();
3423 #ifdef MSDOS
3424 if (been_here > 0)
3425 maybe_fatal (1, 0, "Attempt to create another terminal %s", "",
3426 name, "");
3427 been_here = 1;
3428 tty = &the_only_display_info;
3429 #else
3430 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
3431 #endif
3432 memset (tty, 0, sizeof (struct tty_display_info));
3433 tty->next = tty_list;
3434 tty_list = tty;
3436 terminal->type = output_termcap;
3437 terminal->display_info.tty = tty;
3438 tty->terminal = terminal;
3440 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3441 Wcm_clear (tty);
3443 #ifndef DOS_NT
3444 set_tty_hooks (terminal);
3447 int fd;
3448 FILE *file;
3450 #ifdef O_IGNORE_CTTY
3451 if (!ctty)
3452 /* Open the terminal device. Don't recognize it as our
3453 controlling terminal, and don't make it the controlling tty
3454 if we don't have one at the moment. */
3455 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
3456 else
3457 #else
3458 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3459 defined on Hurd. On other systems, we need to explicitly
3460 dissociate ourselves from the controlling tty when we want to
3461 open a frame on the same terminal. */
3462 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
3463 #endif /* O_IGNORE_CTTY */
3465 tty->name = xstrdup (name);
3466 terminal->name = xstrdup (name);
3468 if (fd < 0)
3469 maybe_fatal (must_succeed, terminal,
3470 "Could not open file: %s",
3471 "Could not open file: %s",
3472 name);
3473 if (!isatty (fd))
3475 close (fd);
3476 maybe_fatal (must_succeed, terminal,
3477 "Not a tty device: %s",
3478 "Not a tty device: %s",
3479 name);
3482 #ifndef O_IGNORE_CTTY
3483 if (!ctty)
3484 dissociate_if_controlling_tty (fd);
3485 #endif
3487 file = fdopen (fd, "w+");
3488 tty->input = file;
3489 tty->output = file;
3492 tty->type = xstrdup (terminal_type);
3494 add_keyboard_wait_descriptor (fileno (tty->input));
3496 #endif /* !DOS_NT */
3498 encode_terminal_src_size = 0;
3499 encode_terminal_dst_size = 0;
3501 #ifdef HAVE_GPM
3502 terminal->mouse_position_hook = term_mouse_position;
3503 mouse_face_window = Qnil;
3504 #endif
3506 #ifdef DOS_NT
3507 #ifdef WINDOWSNT
3508 initialize_w32_display (terminal);
3509 #else /* MSDOS */
3510 if (strcmp (terminal_type, "internal") == 0)
3511 terminal->type = output_msdos_raw;
3512 initialize_msdos_display (terminal);
3513 #endif /* MSDOS */
3514 tty->output = stdout;
3515 tty->input = stdin;
3516 /* The following two are inaccessible from w32console.c. */
3517 terminal->delete_frame_hook = &tty_free_frame_resources;
3518 terminal->delete_terminal_hook = &delete_tty;
3520 tty->name = xstrdup (name);
3521 terminal->name = xstrdup (name);
3522 tty->type = xstrdup (terminal_type);
3524 #ifdef subprocesses
3525 add_keyboard_wait_descriptor (0);
3526 #endif
3528 Wcm_clear (tty);
3530 #ifdef WINDOWSNT
3532 struct frame *f = XFRAME (selected_frame);
3534 FrameRows (tty) = FRAME_LINES (f);
3535 FrameCols (tty) = FRAME_COLS (f);
3536 tty->specified_window = FRAME_LINES (f);
3538 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3539 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
3541 #else /* MSDOS */
3543 int height, width;
3544 get_tty_size (fileno (tty->input), &width, &height);
3545 FrameCols (tty) = width;
3546 FrameRows (tty) = height;
3548 #endif /* MSDOS */
3549 tty->delete_in_insert_mode = 1;
3551 UseTabs (tty) = 0;
3552 terminal->scroll_region_ok = 0;
3554 /* Seems to insert lines when it's not supposed to, messing up the
3555 display. In doing a trace, it didn't seem to be called much, so I
3556 don't think we're losing anything by turning it off. */
3557 terminal->line_ins_del_ok = 0;
3558 #ifdef WINDOWSNT
3559 terminal->char_ins_del_ok = 1;
3560 baud_rate = 19200;
3561 #else /* MSDOS */
3562 terminal->char_ins_del_ok = 0;
3563 init_baud_rate (fileno (tty->input));
3564 #endif /* MSDOS */
3566 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
3568 #else /* not DOS_NT */
3570 Wcm_clear (tty);
3572 tty->termcap_term_buffer = (char *) xmalloc (buffer_size);
3574 /* On some systems, tgetent tries to access the controlling
3575 terminal. */
3576 sigblock (sigmask (SIGTTOU));
3577 status = tgetent (tty->termcap_term_buffer, terminal_type);
3578 sigunblock (sigmask (SIGTTOU));
3580 if (status < 0)
3582 #ifdef TERMINFO
3583 maybe_fatal (must_succeed, terminal,
3584 "Cannot open terminfo database file",
3585 "Cannot open terminfo database file");
3586 #else
3587 maybe_fatal (must_succeed, terminal,
3588 "Cannot open termcap database file",
3589 "Cannot open termcap database file");
3590 #endif
3592 if (status == 0)
3594 maybe_fatal (must_succeed, terminal,
3595 "Terminal type %s is not defined",
3596 "Terminal type %s is not defined.\n\
3597 If that is not the actual type of terminal you have,\n\
3598 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3599 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3600 #ifdef TERMINFO
3601 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3602 #else
3603 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3604 #endif
3605 terminal_type);
3608 #ifndef TERMINFO
3609 if (strlen (tty->termcap_term_buffer) >= buffer_size)
3610 abort ();
3611 buffer_size = strlen (tty->termcap_term_buffer);
3612 #endif
3613 tty->termcap_strings_buffer = area = (char *) xmalloc (buffer_size);
3614 tty->TS_ins_line = tgetstr ("al", address);
3615 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3616 tty->TS_bell = tgetstr ("bl", address);
3617 BackTab (tty) = tgetstr ("bt", address);
3618 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3619 tty->TS_clr_line = tgetstr ("ce", address);
3620 tty->TS_clr_frame = tgetstr ("cl", address);
3621 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3622 AbsPosition (tty) = tgetstr ("cm", address);
3623 CR (tty) = tgetstr ("cr", address);
3624 tty->TS_set_scroll_region = tgetstr ("cs", address);
3625 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
3626 RowPosition (tty) = tgetstr ("cv", address);
3627 tty->TS_del_char = tgetstr ("dc", address);
3628 tty->TS_del_multi_chars = tgetstr ("DC", address);
3629 tty->TS_del_line = tgetstr ("dl", address);
3630 tty->TS_del_multi_lines = tgetstr ("DL", address);
3631 tty->TS_delete_mode = tgetstr ("dm", address);
3632 tty->TS_end_delete_mode = tgetstr ("ed", address);
3633 tty->TS_end_insert_mode = tgetstr ("ei", address);
3634 Home (tty) = tgetstr ("ho", address);
3635 tty->TS_ins_char = tgetstr ("ic", address);
3636 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3637 tty->TS_insert_mode = tgetstr ("im", address);
3638 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3639 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3640 tty->TS_keypad_mode = tgetstr ("ks", address);
3641 LastLine (tty) = tgetstr ("ll", address);
3642 Right (tty) = tgetstr ("nd", address);
3643 Down (tty) = tgetstr ("do", address);
3644 if (!Down (tty))
3645 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
3646 if (tgetflag ("bs"))
3647 Left (tty) = "\b"; /* can't possibly be longer! */
3648 else /* (Actually, "bs" is obsolete...) */
3649 Left (tty) = tgetstr ("le", address);
3650 if (!Left (tty))
3651 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
3652 tty->TS_pad_char = tgetstr ("pc", address);
3653 tty->TS_repeat = tgetstr ("rp", address);
3654 tty->TS_end_standout_mode = tgetstr ("se", address);
3655 tty->TS_fwd_scroll = tgetstr ("sf", address);
3656 tty->TS_standout_mode = tgetstr ("so", address);
3657 tty->TS_rev_scroll = tgetstr ("sr", address);
3658 tty->Wcm->cm_tab = tgetstr ("ta", address);
3659 tty->TS_end_termcap_modes = tgetstr ("te", address);
3660 tty->TS_termcap_modes = tgetstr ("ti", address);
3661 Up (tty) = tgetstr ("up", address);
3662 tty->TS_visible_bell = tgetstr ("vb", address);
3663 tty->TS_cursor_normal = tgetstr ("ve", address);
3664 tty->TS_cursor_visible = tgetstr ("vs", address);
3665 tty->TS_cursor_invisible = tgetstr ("vi", address);
3666 tty->TS_set_window = tgetstr ("wi", address);
3668 tty->TS_enter_underline_mode = tgetstr ("us", address);
3669 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3670 tty->TS_enter_bold_mode = tgetstr ("md", address);
3671 tty->TS_enter_dim_mode = tgetstr ("mh", address);
3672 tty->TS_enter_blink_mode = tgetstr ("mb", address);
3673 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3674 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3675 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3676 tty->TS_exit_attribute_mode = tgetstr ("me", address);
3678 MultiUp (tty) = tgetstr ("UP", address);
3679 MultiDown (tty) = tgetstr ("DO", address);
3680 MultiLeft (tty) = tgetstr ("LE", address);
3681 MultiRight (tty) = tgetstr ("RI", address);
3683 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
3684 color because we can't switch back to the default foreground and
3685 background. */
3686 tty->TS_orig_pair = tgetstr ("op", address);
3687 if (tty->TS_orig_pair)
3689 tty->TS_set_foreground = tgetstr ("AF", address);
3690 tty->TS_set_background = tgetstr ("AB", address);
3691 if (!tty->TS_set_foreground)
3693 /* SVr4. */
3694 tty->TS_set_foreground = tgetstr ("Sf", address);
3695 tty->TS_set_background = tgetstr ("Sb", address);
3698 tty->TN_max_colors = tgetnum ("Co");
3699 tty->TN_max_pairs = tgetnum ("pa");
3701 tty->TN_no_color_video = tgetnum ("NC");
3702 if (tty->TN_no_color_video == -1)
3703 tty->TN_no_color_video = 0;
3706 tty_default_color_capabilities (tty, 1);
3708 MagicWrap (tty) = tgetflag ("xn");
3709 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3710 the former flag imply the latter. */
3711 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
3712 terminal->memory_below_frame = tgetflag ("db");
3713 tty->TF_hazeltine = tgetflag ("hz");
3714 terminal->must_write_spaces = tgetflag ("in");
3715 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3716 tty->TF_insmode_motion = tgetflag ("mi");
3717 tty->TF_standout_motion = tgetflag ("ms");
3718 tty->TF_underscore = tgetflag ("ul");
3719 tty->TF_teleray = tgetflag ("xt");
3721 #endif /* !DOS_NT */
3722 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3723 init_kboard (terminal->kboard);
3724 terminal->kboard->Vwindow_system = Qnil;
3725 terminal->kboard->next_kboard = all_kboards;
3726 all_kboards = terminal->kboard;
3727 terminal->kboard->reference_count++;
3728 /* Don't let the initial kboard remain current longer than necessary.
3729 That would cause problems if a file loaded on startup tries to
3730 prompt in the mini-buffer. */
3731 if (current_kboard == initial_kboard)
3732 current_kboard = terminal->kboard;
3733 #ifndef DOS_NT
3734 term_get_fkeys (address, terminal->kboard);
3736 /* Get frame size from system, or else from termcap. */
3738 int height, width;
3739 get_tty_size (fileno (tty->input), &width, &height);
3740 FrameCols (tty) = width;
3741 FrameRows (tty) = height;
3744 if (FrameCols (tty) <= 0)
3745 FrameCols (tty) = tgetnum ("co");
3746 if (FrameRows (tty) <= 0)
3747 FrameRows (tty) = tgetnum ("li");
3749 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
3750 maybe_fatal (must_succeed, terminal,
3751 "Screen size %dx%d is too small"
3752 "Screen size %dx%d is too small",
3753 FrameCols (tty), FrameRows (tty));
3755 TabWidth (tty) = tgetnum ("tw");
3757 if (!tty->TS_bell)
3758 tty->TS_bell = "\07";
3760 if (!tty->TS_fwd_scroll)
3761 tty->TS_fwd_scroll = Down (tty);
3763 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
3765 if (TabWidth (tty) < 0)
3766 TabWidth (tty) = 8;
3768 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3769 and newer termcap doc does not seem to say there is a default.
3770 if (!tty->Wcm->cm_tab)
3771 tty->Wcm->cm_tab = "\t";
3774 /* We don't support standout modes that use `magic cookies', so
3775 turn off any that do. */
3776 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
3778 tty->TS_standout_mode = 0;
3779 tty->TS_end_standout_mode = 0;
3781 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
3783 tty->TS_enter_underline_mode = 0;
3784 tty->TS_exit_underline_mode = 0;
3787 /* If there's no standout mode, try to use underlining instead. */
3788 if (tty->TS_standout_mode == 0)
3790 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3791 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
3794 /* If no `se' string, try using a `me' string instead.
3795 If that fails, we can't use standout mode at all. */
3796 if (tty->TS_end_standout_mode == 0)
3798 char *s = tgetstr ("me", address);
3799 if (s != 0)
3800 tty->TS_end_standout_mode = s;
3801 else
3802 tty->TS_standout_mode = 0;
3805 if (tty->TF_teleray)
3807 tty->Wcm->cm_tab = 0;
3808 /* We can't support standout mode, because it uses magic cookies. */
3809 tty->TS_standout_mode = 0;
3810 /* But that means we cannot rely on ^M to go to column zero! */
3811 CR (tty) = 0;
3812 /* LF can't be trusted either -- can alter hpos */
3813 /* if move at column 0 thru a line with TS_standout_mode */
3814 Down (tty) = 0;
3817 /* Special handling for certain terminal types known to need it */
3819 if (!strcmp (terminal_type, "supdup"))
3821 terminal->memory_below_frame = 1;
3822 tty->Wcm->cm_losewrap = 1;
3824 if (!strncmp (terminal_type, "c10", 3)
3825 || !strcmp (terminal_type, "perq"))
3827 /* Supply a makeshift :wi string.
3828 This string is not valid in general since it works only
3829 for windows starting at the upper left corner;
3830 but that is all Emacs uses.
3832 This string works only if the frame is using
3833 the top of the video memory, because addressing is memory-relative.
3834 So first check the :ti string to see if that is true.
3836 It would be simpler if the :wi string could go in the termcap
3837 entry, but it can't because it is not fully valid.
3838 If it were in the termcap entry, it would confuse other programs. */
3839 if (!tty->TS_set_window)
3841 p = tty->TS_termcap_modes;
3842 while (*p && strcmp (p, "\033v "))
3843 p++;
3844 if (*p)
3845 tty->TS_set_window = "\033v%C %C %C %C ";
3847 /* Termcap entry often fails to have :in: flag */
3848 terminal->must_write_spaces = 1;
3849 /* :ti string typically fails to have \E^G! in it */
3850 /* This limits scope of insert-char to one line. */
3851 strcpy (area, tty->TS_termcap_modes);
3852 strcat (area, "\033\007!");
3853 tty->TS_termcap_modes = area;
3854 area += strlen (area) + 1;
3855 p = AbsPosition (tty);
3856 /* Change all %+ parameters to %C, to handle
3857 values above 96 correctly for the C100. */
3858 while (*p)
3860 if (p[0] == '%' && p[1] == '+')
3861 p[1] = 'C';
3862 p++;
3866 tty->specified_window = FrameRows (tty);
3868 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3870 maybe_fatal (must_succeed, terminal,
3871 "Terminal type \"%s\" is not powerful enough to run Emacs",
3872 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3873 It lacks the ability to position the cursor.\n\
3874 If that is not the actual type of terminal you have,\n\
3875 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3876 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3877 # ifdef TERMINFO
3878 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3879 # else /* TERMCAP */
3880 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3881 # endif /* TERMINFO */
3882 terminal_type);
3885 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
3886 maybe_fatal (must_succeed, terminal,
3887 "Could not determine the frame size",
3888 "Could not determine the frame size");
3890 tty->delete_in_insert_mode
3891 = tty->TS_delete_mode && tty->TS_insert_mode
3892 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
3894 tty->se_is_so = (tty->TS_standout_mode
3895 && tty->TS_end_standout_mode
3896 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
3898 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
3900 terminal->scroll_region_ok
3901 = (tty->Wcm->cm_abs
3902 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
3904 terminal->line_ins_del_ok
3905 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3906 && (tty->TS_del_line || tty->TS_del_multi_lines))
3907 || (terminal->scroll_region_ok
3908 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
3910 terminal->char_ins_del_ok
3911 = ((tty->TS_ins_char || tty->TS_insert_mode
3912 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3913 && (tty->TS_del_char || tty->TS_del_multi_chars));
3915 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
3917 init_baud_rate (fileno (tty->input));
3919 #endif /* not DOS_NT */
3921 /* Init system terminal modes (RAW or CBREAK, etc.). */
3922 init_sys_modes (tty);
3924 return terminal;
3927 /* Auxiliary error-handling function for init_tty.
3928 Delete TERMINAL, then call error or fatal with str1 or str2,
3929 respectively, according to MUST_SUCCEED. */
3931 static void
3932 maybe_fatal (must_succeed, terminal, str1, str2, arg1, arg2)
3933 int must_succeed;
3934 struct terminal *terminal;
3935 char *str1, *str2, *arg1, *arg2;
3937 if (terminal)
3938 delete_tty (terminal);
3940 if (must_succeed)
3941 fatal (str2, arg1, arg2);
3942 else
3943 error (str1, arg1, arg2);
3945 abort ();
3948 void
3949 fatal (const char *str, ...)
3951 va_list ap;
3952 va_start (ap, str);
3953 fprintf (stderr, "emacs: ");
3954 vfprintf (stderr, str, ap);
3955 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
3956 fprintf (stderr, "\n");
3957 va_end (ap);
3958 fflush (stderr);
3959 exit (1);
3964 /* Delete the given tty terminal, closing all frames on it. */
3966 static void
3967 delete_tty (struct terminal *terminal)
3969 struct tty_display_info *tty;
3971 /* Protect against recursive calls. delete_frame in
3972 delete_terminal calls us back when it deletes our last frame. */
3973 if (!terminal->name)
3974 return;
3976 if (terminal->type != output_termcap)
3977 abort ();
3979 tty = terminal->display_info.tty;
3981 if (tty == tty_list)
3982 tty_list = tty->next;
3983 else
3985 struct tty_display_info *p;
3986 for (p = tty_list; p && p->next != tty; p = p->next)
3989 if (! p)
3990 /* This should not happen. */
3991 abort ();
3993 p->next = tty->next;
3994 tty->next = 0;
3997 /* reset_sys_modes needs a valid device, so this call needs to be
3998 before delete_terminal. */
3999 reset_sys_modes (tty);
4001 delete_terminal (terminal);
4003 xfree (tty->name);
4004 xfree (tty->type);
4006 if (tty->input)
4008 #ifdef subprocesses
4009 delete_keyboard_wait_descriptor (fileno (tty->input));
4010 #endif
4011 if (tty->input != stdin)
4012 fclose (tty->input);
4014 if (tty->output && tty->output != stdout && tty->output != tty->input)
4015 fclose (tty->output);
4016 if (tty->termscript)
4017 fclose (tty->termscript);
4019 xfree (tty->old_tty);
4020 xfree (tty->Wcm);
4021 xfree (tty->termcap_strings_buffer);
4022 xfree (tty->termcap_term_buffer);
4024 memset (tty, 0, sizeof (struct tty_display_info));
4025 xfree (tty);
4030 /* Mark the pointers in the tty_display_info objects.
4031 Called by the Fgarbage_collector. */
4033 void
4034 mark_ttys (void)
4036 struct tty_display_info *tty;
4038 for (tty = tty_list; tty; tty = tty->next)
4039 mark_object (tty->top_frame);
4044 void
4045 syms_of_term (void)
4047 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
4048 doc: /* Non-nil means the system uses terminfo rather than termcap.
4049 This variable can be used by terminal emulator packages. */);
4050 #ifdef TERMINFO
4051 system_uses_terminfo = 1;
4052 #else
4053 system_uses_terminfo = 0;
4054 #endif
4056 DEFVAR_LISP ("suspend-tty-functions", &Vsuspend_tty_functions,
4057 doc: /* Functions to be run after suspending a tty.
4058 The functions are run with one argument, the terminal object to be suspended.
4059 See `suspend-tty'. */);
4060 Vsuspend_tty_functions = Qnil;
4063 DEFVAR_LISP ("resume-tty-functions", &Vresume_tty_functions,
4064 doc: /* Functions to be run after resuming a tty.
4065 The functions are run with one argument, the terminal object that was revived.
4066 See `resume-tty'. */);
4067 Vresume_tty_functions = Qnil;
4069 DEFVAR_BOOL ("visible-cursor", &visible_cursor,
4070 doc: /* Non-nil means to make the cursor very visible.
4071 This only has an effect when running in a text terminal.
4072 What means \"very visible\" is up to your terminal. It may make the cursor
4073 bigger, or it may make it blink, or it may do nothing at all. */);
4074 visible_cursor = 1;
4076 defsubr (&Stty_display_color_p);
4077 defsubr (&Stty_display_color_cells);
4078 defsubr (&Stty_no_underline);
4079 defsubr (&Stty_type);
4080 defsubr (&Scontrolling_tty_p);
4081 defsubr (&Ssuspend_tty);
4082 defsubr (&Sresume_tty);
4083 #ifdef HAVE_GPM
4084 defsubr (&Sgpm_mouse_start);
4085 defsubr (&Sgpm_mouse_stop);
4087 staticpro (&mouse_face_window);
4088 #endif /* HAVE_GPM */
4090 #ifndef DOS_NT
4091 default_orig_pair = NULL;
4092 default_set_foreground = NULL;
4093 default_set_background = NULL;
4094 #endif /* !DOS_NT */
4096 encode_terminal_src = NULL;
4097 encode_terminal_dst = NULL;
4102 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
4103 (do not change this comment) */