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