*** empty log message ***
[emacs.git] / src / term.c
blob20c7be33e384e47588e71697266432f215ea0325
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 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
24 #include <config.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/file.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
35 #if HAVE_TERMIOS_H
36 #include <termios.h> /* For TIOCNOTTY. */
37 #endif
39 #include <signal.h>
40 #include <stdarg.h>
42 #include "lisp.h"
43 #include "termchar.h"
44 #include "termopts.h"
45 #include "charset.h"
46 #include "coding.h"
47 #include "keyboard.h"
48 #include "frame.h"
49 #include "disptab.h"
50 #include "termhooks.h"
51 #include "dispextern.h"
52 #include "window.h"
53 #include "keymap.h"
54 #include "blockinput.h"
55 #include "syssignal.h"
56 #include "systty.h"
57 #include "intervals.h"
59 /* For now, don't try to include termcap.h. On some systems,
60 configure finds a non-standard termcap.h that the main build
61 won't find. */
63 #if defined HAVE_TERMCAP_H && 0
64 #include <termcap.h>
65 #else
66 extern void tputs P_ ((const char *, int, int (*)(int)));
67 extern int tgetent P_ ((char *, const char *));
68 extern int tgetflag P_ ((char *id));
69 extern int tgetnum P_ ((char *id));
70 #endif
72 #include "cm.h"
73 #ifdef HAVE_X_WINDOWS
74 #include "xterm.h"
75 #endif
76 #ifdef MAC_OS
77 #include "macterm.h"
78 #endif
80 #ifndef O_RDWR
81 #define O_RDWR 2
82 #endif
84 #ifndef O_NOCTTY
85 #define O_NOCTTY 0
86 #endif
88 /* The name of the default console device. */
89 #ifdef WINDOWSNT
90 #define DEV_TTY "CONOUT$"
91 #else
92 #define DEV_TTY "/dev/tty"
93 #endif
95 static void tty_set_scroll_region P_ ((struct frame *f, int start, int stop));
96 static void turn_on_face P_ ((struct frame *, int face_id));
97 static void turn_off_face P_ ((struct frame *, int face_id));
98 static void tty_show_cursor P_ ((struct tty_display_info *));
99 static void tty_hide_cursor P_ ((struct tty_display_info *));
100 static void tty_background_highlight P_ ((struct tty_display_info *tty));
101 static void clear_tty_hooks P_ ((struct terminal *terminal));
102 static void set_tty_hooks P_ ((struct terminal *terminal));
103 static void dissociate_if_controlling_tty P_ ((int fd));
104 static void delete_tty P_ ((struct terminal *));
106 #define OUTPUT(tty, a) \
107 emacs_tputs ((tty), a, \
108 (int) (FRAME_LINES (XFRAME (selected_frame)) \
109 - curY (tty)), \
110 cmputc)
112 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
113 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
115 #define OUTPUT_IF(tty, a) \
116 do { \
117 if (a) \
118 emacs_tputs ((tty), a, \
119 (int) (FRAME_LINES (XFRAME (selected_frame)) \
120 - curY (tty) ), \
121 cmputc); \
122 } while (0)
124 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
126 /* If true, use "vs", otherwise use "ve" to make the cursor visible. */
128 static int visible_cursor;
130 /* Display space properties */
132 extern Lisp_Object Qspace, QCalign_to, QCwidth;
134 /* Functions to call after suspending a tty. */
135 Lisp_Object Vsuspend_tty_functions;
137 /* Functions to call after resuming a tty. */
138 Lisp_Object Vresume_tty_functions;
140 /* Chain of all tty device parameters. */
141 struct tty_display_info *tty_list;
143 /* Nonzero means no need to redraw the entire frame on resuming a
144 suspended Emacs. This is useful on terminals with multiple
145 pages, where one page is used for Emacs and another for all
146 else. */
147 int no_redraw_on_reenter;
149 /* Meaning of bits in no_color_video. Each bit set means that the
150 corresponding attribute cannot be combined with colors. */
152 enum no_color_bit
154 NC_STANDOUT = 1 << 0,
155 NC_UNDERLINE = 1 << 1,
156 NC_REVERSE = 1 << 2,
157 NC_BLINK = 1 << 3,
158 NC_DIM = 1 << 4,
159 NC_BOLD = 1 << 5,
160 NC_INVIS = 1 << 6,
161 NC_PROTECT = 1 << 7,
162 NC_ALT_CHARSET = 1 << 8
165 /* internal state */
167 /* The largest frame width in any call to calculate_costs. */
169 int max_frame_cols;
171 /* The largest frame height in any call to calculate_costs. */
173 int max_frame_lines;
175 /* Non-zero if we have dropped our controlling tty and therefore
176 should not open a frame on stdout. */
177 static int no_controlling_tty;
179 /* Provided for lisp packages. */
181 static int system_uses_terminfo;
183 char *tparam ();
185 extern char *tgetstr ();
188 #ifdef HAVE_GPM
189 #include <sys/fcntl.h>
190 #include "buffer.h"
192 static void term_clear_mouse_face ();
193 static void term_mouse_highlight (struct frame *f, int x, int y);
195 /* The device for which we have enabled gpm support (or NULL). */
196 struct tty_display_info *gpm_tty = NULL;
198 /* These variables describe the range of text currently shown in its
199 mouse-face, together with the window they apply to. As long as
200 the mouse stays within this range, we need not redraw anything on
201 its account. Rows and columns are glyph matrix positions in
202 MOUSE_FACE_WINDOW. */
203 static int mouse_face_beg_row, mouse_face_beg_col;
204 static int mouse_face_end_row, mouse_face_end_col;
205 static int mouse_face_past_end;
206 static Lisp_Object mouse_face_window;
207 static int mouse_face_face_id;
209 static int pos_x, pos_y;
210 static int last_mouse_x, last_mouse_y;
211 #endif /* HAVE_GPM */
213 /* Ring the bell on a tty. */
215 static void
216 tty_ring_bell (struct frame *f)
218 struct tty_display_info *tty = FRAME_TTY (f);
220 if (tty->output)
222 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
223 ? tty->TS_visible_bell
224 : tty->TS_bell));
225 fflush (tty->output);
229 /* Set up termcap modes for Emacs. */
231 void
232 tty_set_terminal_modes (struct terminal *terminal)
234 struct tty_display_info *tty = terminal->display_info.tty;
236 if (tty->output)
238 if (tty->TS_termcap_modes)
239 OUTPUT (tty, tty->TS_termcap_modes);
240 else
242 /* Output enough newlines to scroll all the old screen contents
243 off the screen, so it won't be overwritten and lost. */
244 int i;
245 current_tty = tty;
246 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
247 cmputc ('\n');
250 OUTPUT_IF (tty, tty->TS_termcap_modes);
251 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
252 OUTPUT_IF (tty, tty->TS_keypad_mode);
253 losecursor (tty);
254 fflush (tty->output);
258 /* Reset termcap modes before exiting Emacs. */
260 void
261 tty_reset_terminal_modes (struct terminal *terminal)
263 struct tty_display_info *tty = terminal->display_info.tty;
265 if (tty->output)
267 tty_turn_off_highlight (tty);
268 tty_turn_off_insert (tty);
269 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
270 OUTPUT_IF (tty, tty->TS_cursor_normal);
271 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
272 OUTPUT_IF (tty, tty->TS_orig_pair);
273 /* Output raw CR so kernel can track the cursor hpos. */
274 current_tty = tty;
275 cmputc ('\r');
276 fflush (tty->output);
280 /* Flag the end of a display update on a termcap terminal. */
282 static void
283 tty_update_end (struct frame *f)
285 struct tty_display_info *tty = FRAME_TTY (f);
287 if (!XWINDOW (selected_window)->cursor_off_p)
288 tty_show_cursor (tty);
289 tty_turn_off_insert (tty);
290 tty_background_highlight (tty);
293 /* The implementation of set_terminal_window for termcap frames. */
295 static void
296 tty_set_terminal_window (struct frame *f, int size)
298 struct tty_display_info *tty = FRAME_TTY (f);
300 tty->specified_window = size ? size : FRAME_LINES (f);
301 if (FRAME_SCROLL_REGION_OK (f))
302 tty_set_scroll_region (f, 0, tty->specified_window);
305 static void
306 tty_set_scroll_region (struct frame *f, int start, int stop)
308 char *buf;
309 struct tty_display_info *tty = FRAME_TTY (f);
311 if (tty->TS_set_scroll_region)
312 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1);
313 else if (tty->TS_set_scroll_region_1)
314 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
315 FRAME_LINES (f), start,
316 FRAME_LINES (f) - stop,
317 FRAME_LINES (f));
318 else
319 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
321 OUTPUT (tty, buf);
322 xfree (buf);
323 losecursor (tty);
327 static void
328 tty_turn_on_insert (struct tty_display_info *tty)
330 if (!tty->insert_mode)
331 OUTPUT (tty, tty->TS_insert_mode);
332 tty->insert_mode = 1;
335 void
336 tty_turn_off_insert (struct tty_display_info *tty)
338 if (tty->insert_mode)
339 OUTPUT (tty, tty->TS_end_insert_mode);
340 tty->insert_mode = 0;
343 /* Handle highlighting. */
345 void
346 tty_turn_off_highlight (struct tty_display_info *tty)
348 if (tty->standout_mode)
349 OUTPUT_IF (tty, tty->TS_end_standout_mode);
350 tty->standout_mode = 0;
353 static void
354 tty_turn_on_highlight (struct tty_display_info *tty)
356 if (!tty->standout_mode)
357 OUTPUT_IF (tty, tty->TS_standout_mode);
358 tty->standout_mode = 1;
361 static void
362 tty_toggle_highlight (struct tty_display_info *tty)
364 if (tty->standout_mode)
365 tty_turn_off_highlight (tty);
366 else
367 tty_turn_on_highlight (tty);
371 /* Make cursor invisible. */
373 static void
374 tty_hide_cursor (struct tty_display_info *tty)
376 if (tty->cursor_hidden == 0)
378 tty->cursor_hidden = 1;
379 OUTPUT_IF (tty, tty->TS_cursor_invisible);
384 /* Ensure that cursor is visible. */
386 static void
387 tty_show_cursor (struct tty_display_info *tty)
389 if (tty->cursor_hidden)
391 tty->cursor_hidden = 0;
392 OUTPUT_IF (tty, tty->TS_cursor_normal);
393 if (visible_cursor)
394 OUTPUT_IF (tty, tty->TS_cursor_visible);
399 /* Set standout mode to the state it should be in for
400 empty space inside windows. What this is,
401 depends on the user option inverse-video. */
403 static void
404 tty_background_highlight (struct tty_display_info *tty)
406 if (inverse_video)
407 tty_turn_on_highlight (tty);
408 else
409 tty_turn_off_highlight (tty);
412 /* Set standout mode to the mode specified for the text to be output. */
414 static void
415 tty_highlight_if_desired (struct tty_display_info *tty)
417 if (inverse_video)
418 tty_turn_on_highlight (tty);
419 else
420 tty_turn_off_highlight (tty);
424 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
425 frame-relative coordinates. */
427 static void
428 tty_cursor_to (struct frame *f, int vpos, int hpos)
430 struct tty_display_info *tty = FRAME_TTY (f);
432 /* Detect the case where we are called from reset_sys_modes
433 and the costs have never been calculated. Do nothing. */
434 if (! tty->costs_set)
435 return;
437 if (curY (tty) == vpos
438 && curX (tty) == hpos)
439 return;
440 if (!tty->TF_standout_motion)
441 tty_background_highlight (tty);
442 if (!tty->TF_insmode_motion)
443 tty_turn_off_insert (tty);
444 cmgoto (tty, vpos, hpos);
447 /* Similar but don't take any account of the wasted characters. */
449 static void
450 tty_raw_cursor_to (struct frame *f, int row, int col)
452 struct tty_display_info *tty = FRAME_TTY (f);
454 if (curY (tty) == row
455 && curX (tty) == col)
456 return;
457 if (!tty->TF_standout_motion)
458 tty_background_highlight (tty);
459 if (!tty->TF_insmode_motion)
460 tty_turn_off_insert (tty);
461 cmgoto (tty, row, col);
464 /* Erase operations */
466 /* Clear from cursor to end of frame on a termcap device. */
468 static void
469 tty_clear_to_end (struct frame *f)
471 register int i;
472 struct tty_display_info *tty = FRAME_TTY (f);
474 if (tty->TS_clr_to_bottom)
476 tty_background_highlight (tty);
477 OUTPUT (tty, tty->TS_clr_to_bottom);
479 else
481 for (i = curY (tty); i < FRAME_LINES (f); i++)
483 cursor_to (f, i, 0);
484 clear_end_of_line (f, FRAME_COLS (f));
489 /* Clear an entire termcap frame. */
491 static void
492 tty_clear_frame (struct frame *f)
494 struct tty_display_info *tty = FRAME_TTY (f);
496 if (tty->TS_clr_frame)
498 tty_background_highlight (tty);
499 OUTPUT (tty, tty->TS_clr_frame);
500 cmat (tty, 0, 0);
502 else
504 cursor_to (f, 0, 0);
505 clear_to_end (f);
509 /* An implementation of clear_end_of_line for termcap frames.
511 Note that the cursor may be moved, on terminals lacking a `ce' string. */
513 static void
514 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
516 register int i;
517 struct tty_display_info *tty = FRAME_TTY (f);
519 /* Detect the case where we are called from reset_sys_modes
520 and the costs have never been calculated. Do nothing. */
521 if (! tty->costs_set)
522 return;
524 if (curX (tty) >= first_unused_hpos)
525 return;
526 tty_background_highlight (tty);
527 if (tty->TS_clr_line)
529 OUTPUT1 (tty, tty->TS_clr_line);
531 else
532 { /* have to do it the hard way */
533 tty_turn_off_insert (tty);
535 /* Do not write in last row last col with Auto-wrap on. */
536 if (AutoWrap (tty)
537 && curY (tty) == FrameRows (tty) - 1
538 && first_unused_hpos == FrameCols (tty))
539 first_unused_hpos--;
541 for (i = curX (tty); i < first_unused_hpos; i++)
543 if (tty->termscript)
544 fputc (' ', tty->termscript);
545 fputc (' ', tty->output);
547 cmplus (tty, first_unused_hpos - curX (tty));
551 /* Buffer to store the source and result of code conversion for terminal. */
552 static unsigned char *encode_terminal_buf;
553 /* Allocated size of the above buffer. */
554 static int encode_terminal_bufsize;
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 register GLYPH g;
568 unsigned char *buf;
569 int nchars, nbytes, required;
570 register int tlen = GLYPH_TABLE_LENGTH;
571 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
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. */
576 required = MAX_MULTIBYTE_LENGTH * src_len;
577 if (encode_terminal_bufsize < required)
579 if (encode_terminal_bufsize == 0)
580 encode_terminal_buf = xmalloc (required);
581 else
582 encode_terminal_buf = xrealloc (encode_terminal_buf, required);
583 encode_terminal_bufsize = required;
586 buf = encode_terminal_buf;
587 nchars = 0;
588 while (src < src_end)
590 /* We must skip glyphs to be padded for a wide character. */
591 if (! CHAR_GLYPH_PADDING_P (*src))
593 g = GLYPH_FROM_CHAR_GLYPH (src[0]);
595 if (g < 0 || g >= tlen)
597 /* This glyph doesn't has an entry in Vglyph_table. */
598 if (CHAR_VALID_P (src->u.ch, 0))
599 buf += CHAR_STRING (src->u.ch, buf);
600 else
601 *buf++ = SPACEGLYPH;
602 nchars++;
604 else
606 /* This glyph has an entry in Vglyph_table,
607 so process any alias before testing for simpleness. */
608 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
610 if (GLYPH_SIMPLE_P (tbase, tlen, g))
612 int c = FAST_GLYPH_CHAR (g);
614 if (CHAR_VALID_P (c, 0))
615 buf += CHAR_STRING (c, buf);
616 else
617 *buf++ = SPACEGLYPH;
618 nchars++;
620 else
622 /* We have a string in Vglyph_table. */
623 Lisp_Object string;
625 string = tbase[g];
626 if (! STRING_MULTIBYTE (string))
627 string = string_to_multibyte (string);
628 nbytes = buf - encode_terminal_buf;
629 if (encode_terminal_bufsize < nbytes + SBYTES (string))
631 encode_terminal_bufsize = nbytes + SBYTES (string);
632 encode_terminal_buf = xrealloc (encode_terminal_buf,
633 encode_terminal_bufsize);
634 buf = encode_terminal_buf + nbytes;
636 bcopy (SDATA (string), buf, SBYTES (string));
637 buf += SBYTES (string);
638 nchars += SCHARS (string);
642 src++;
645 nbytes = buf - encode_terminal_buf;
646 coding->src_multibyte = 1;
647 coding->dst_multibyte = 0;
648 if (SYMBOLP (coding->pre_write_conversion)
649 && ! NILP (Ffboundp (coding->pre_write_conversion)))
651 run_pre_write_conversin_on_c_str (&encode_terminal_buf,
652 &encode_terminal_bufsize,
653 nchars, nbytes, coding);
654 nchars = coding->produced_char;
655 nbytes = coding->produced;
657 required = nbytes + encoding_buffer_size (coding, nbytes);
658 if (encode_terminal_bufsize < required)
660 encode_terminal_bufsize = required;
661 encode_terminal_buf = xrealloc (encode_terminal_buf, required);
664 encode_coding (coding, encode_terminal_buf, encode_terminal_buf + nbytes,
665 nbytes, encode_terminal_bufsize - nbytes);
666 return encode_terminal_buf + nbytes;
670 /* An implementation of write_glyphs for termcap frames. */
672 static void
673 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
675 unsigned char *conversion_buffer;
676 struct coding_system *coding;
678 struct tty_display_info *tty = FRAME_TTY (f);
680 tty_turn_off_insert (tty);
681 tty_hide_cursor (tty);
683 /* Don't dare write in last column of bottom line, if Auto-Wrap,
684 since that would scroll the whole frame on some terminals. */
686 if (AutoWrap (tty)
687 && curY (tty) + 1 == FRAME_LINES (f)
688 && (curX (tty) + len) == FRAME_COLS (f))
689 len --;
690 if (len <= 0)
691 return;
693 cmplus (tty, len);
695 /* If terminal_coding does any conversion, use it, otherwise use
696 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
697 because it always return 1 if the member src_multibyte is 1. */
698 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
699 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
700 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
701 the tail. */
702 coding->mode &= ~CODING_MODE_LAST_BLOCK;
704 while (len > 0)
706 /* Identify a run of glyphs with the same face. */
707 int face_id = string->face_id;
708 int n;
710 for (n = 1; n < len; ++n)
711 if (string[n].face_id != face_id)
712 break;
714 /* Turn appearance modes of the face of the run on. */
715 tty_highlight_if_desired (tty);
716 turn_on_face (f, face_id);
718 if (n == len)
719 /* This is the last run. */
720 coding->mode |= CODING_MODE_LAST_BLOCK;
721 conversion_buffer = encode_terminal_code (string, n, coding);
722 if (coding->produced > 0)
724 BLOCK_INPUT;
725 fwrite (conversion_buffer, 1, coding->produced, tty->output);
726 if (ferror (tty->output))
727 clearerr (tty->output);
728 if (tty->termscript)
729 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
730 UNBLOCK_INPUT;
732 len -= n;
733 string += n;
735 /* Turn appearance modes off. */
736 turn_off_face (f, face_id);
737 tty_turn_off_highlight (tty);
740 cmcheckmagic (tty);
743 #ifdef HAVE_GPM /* Only used by GPM code. */
745 static void
746 tty_write_glyphs_with_face (f, string, len, face_id)
747 register struct frame *f;
748 register struct glyph *string;
749 register int len, face_id;
751 unsigned char *conversion_buffer;
752 struct coding_system *coding;
754 struct tty_display_info *tty = FRAME_TTY (f);
756 tty_turn_off_insert (tty);
757 tty_hide_cursor (tty);
759 /* Don't dare write in last column of bottom line, if Auto-Wrap,
760 since that would scroll the whole frame on some terminals. */
762 if (AutoWrap (tty)
763 && curY (tty) + 1 == FRAME_LINES (f)
764 && (curX (tty) + len) == FRAME_COLS (f))
765 len --;
766 if (len <= 0)
767 return;
769 cmplus (tty, len);
771 /* If terminal_coding does any conversion, use it, otherwise use
772 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
773 because it always return 1 if the member src_multibyte is 1. */
774 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
775 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
776 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
777 the tail. */
778 coding->mode &= ~CODING_MODE_LAST_BLOCK;
780 /* Turn appearance modes of the face. */
781 tty_highlight_if_desired (tty);
782 turn_on_face (f, face_id);
784 coding->mode |= CODING_MODE_LAST_BLOCK;
785 conversion_buffer = encode_terminal_code (string, len, coding);
786 if (coding->produced > 0)
788 BLOCK_INPUT;
789 fwrite (conversion_buffer, 1, coding->produced, tty->output);
790 if (ferror (tty->output))
791 clearerr (tty->output);
792 if (tty->termscript)
793 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
794 UNBLOCK_INPUT;
797 /* Turn appearance modes off. */
798 turn_off_face (f, face_id);
799 tty_turn_off_highlight (tty);
801 cmcheckmagic (tty);
803 #endif
805 /* An implementation of insert_glyphs for termcap frames. */
807 static void
808 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
810 char *buf;
811 struct glyph *glyph = NULL;
812 unsigned char *conversion_buffer;
813 unsigned char space[1];
814 struct coding_system *coding;
816 struct tty_display_info *tty = FRAME_TTY (f);
818 if (tty->TS_ins_multi_chars)
820 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len);
821 OUTPUT1 (tty, buf);
822 xfree (buf);
823 if (start)
824 write_glyphs (f, start, len);
825 return;
828 tty_turn_on_insert (tty);
829 cmplus (tty, len);
831 if (! start)
832 space[0] = SPACEGLYPH;
834 /* If terminal_coding does any conversion, use it, otherwise use
835 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
836 because it always return 1 if the member src_multibyte is 1. */
837 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
838 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
839 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
840 the tail. */
841 coding->mode &= ~CODING_MODE_LAST_BLOCK;
843 while (len-- > 0)
845 OUTPUT1_IF (tty, tty->TS_ins_char);
846 if (!start)
848 conversion_buffer = space;
849 coding->produced = 1;
851 else
853 tty_highlight_if_desired (tty);
854 turn_on_face (f, start->face_id);
855 glyph = start;
856 ++start;
857 /* We must open sufficient space for a character which
858 occupies more than one column. */
859 while (len && CHAR_GLYPH_PADDING_P (*start))
861 OUTPUT1_IF (tty, tty->TS_ins_char);
862 start++, len--;
865 if (len <= 0)
866 /* This is the last glyph. */
867 coding->mode |= CODING_MODE_LAST_BLOCK;
869 conversion_buffer = encode_terminal_code (glyph, 1, coding);
872 if (coding->produced > 0)
874 BLOCK_INPUT;
875 fwrite (conversion_buffer, 1, coding->produced, tty->output);
876 if (ferror (tty->output))
877 clearerr (tty->output);
878 if (tty->termscript)
879 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
880 UNBLOCK_INPUT;
883 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
884 if (start)
886 turn_off_face (f, glyph->face_id);
887 tty_turn_off_highlight (tty);
891 cmcheckmagic (tty);
894 /* An implementation of delete_glyphs for termcap frames. */
896 static void
897 tty_delete_glyphs (struct frame *f, int n)
899 char *buf;
900 register int i;
902 struct tty_display_info *tty = FRAME_TTY (f);
904 if (tty->delete_in_insert_mode)
906 tty_turn_on_insert (tty);
908 else
910 tty_turn_off_insert (tty);
911 OUTPUT_IF (tty, tty->TS_delete_mode);
914 if (tty->TS_del_multi_chars)
916 buf = tparam (tty->TS_del_multi_chars, 0, 0, n);
917 OUTPUT1 (tty, buf);
918 xfree (buf);
920 else
921 for (i = 0; i < n; i++)
922 OUTPUT1 (tty, tty->TS_del_char);
923 if (!tty->delete_in_insert_mode)
924 OUTPUT_IF (tty, tty->TS_end_delete_mode);
927 /* An implementation of ins_del_lines for termcap frames. */
929 static void
930 tty_ins_del_lines (struct frame *f, int vpos, int n)
932 struct tty_display_info *tty = FRAME_TTY (f);
933 char *multi = n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
934 char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
935 char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
937 register int i = n > 0 ? n : -n;
938 register char *buf;
940 /* If the lines below the insertion are being pushed
941 into the end of the window, this is the same as clearing;
942 and we know the lines are already clear, since the matching
943 deletion has already been done. So can ignore this. */
944 /* If the lines below the deletion are blank lines coming
945 out of the end of the window, don't bother,
946 as there will be a matching inslines later that will flush them. */
947 if (FRAME_SCROLL_REGION_OK (f)
948 && vpos + i >= tty->specified_window)
949 return;
950 if (!FRAME_MEMORY_BELOW_FRAME (f)
951 && vpos + i >= FRAME_LINES (f))
952 return;
954 if (multi)
956 raw_cursor_to (f, vpos, 0);
957 tty_background_highlight (tty);
958 buf = tparam (multi, 0, 0, i);
959 OUTPUT (tty, buf);
960 xfree (buf);
962 else if (single)
964 raw_cursor_to (f, vpos, 0);
965 tty_background_highlight (tty);
966 while (--i >= 0)
967 OUTPUT (tty, single);
968 if (tty->TF_teleray)
969 curX (tty) = 0;
971 else
973 tty_set_scroll_region (f, vpos, tty->specified_window);
974 if (n < 0)
975 raw_cursor_to (f, tty->specified_window - 1, 0);
976 else
977 raw_cursor_to (f, vpos, 0);
978 tty_background_highlight (tty);
979 while (--i >= 0)
980 OUTPUTL (tty, scroll, tty->specified_window - vpos);
981 tty_set_scroll_region (f, 0, tty->specified_window);
984 if (!FRAME_SCROLL_REGION_OK (f)
985 && FRAME_MEMORY_BELOW_FRAME (f)
986 && n < 0)
988 cursor_to (f, FRAME_LINES (f) + n, 0);
989 clear_to_end (f);
993 /* Compute cost of sending "str", in characters,
994 not counting any line-dependent padding. */
997 string_cost (char *str)
999 cost = 0;
1000 if (str)
1001 tputs (str, 0, evalcost);
1002 return cost;
1005 /* Compute cost of sending "str", in characters,
1006 counting any line-dependent padding at one line. */
1008 static int
1009 string_cost_one_line (char *str)
1011 cost = 0;
1012 if (str)
1013 tputs (str, 1, evalcost);
1014 return cost;
1017 /* Compute per line amount of line-dependent padding,
1018 in tenths of characters. */
1021 per_line_cost (char *str)
1023 cost = 0;
1024 if (str)
1025 tputs (str, 0, evalcost);
1026 cost = - cost;
1027 if (str)
1028 tputs (str, 10, evalcost);
1029 return cost;
1032 #ifndef old
1033 /* char_ins_del_cost[n] is cost of inserting N characters.
1034 char_ins_del_cost[-n] is cost of deleting N characters.
1035 The length of this vector is based on max_frame_cols. */
1037 int *char_ins_del_vector;
1039 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1040 #endif
1042 /* ARGSUSED */
1043 static void
1044 calculate_ins_del_char_costs (struct frame *f)
1046 struct tty_display_info *tty = FRAME_TTY (f);
1047 int ins_startup_cost, del_startup_cost;
1048 int ins_cost_per_char, del_cost_per_char;
1049 register int i;
1050 register int *p;
1052 if (tty->TS_ins_multi_chars)
1054 ins_cost_per_char = 0;
1055 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1057 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1058 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1060 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1061 + string_cost (tty->TS_end_insert_mode))) / 100;
1062 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1063 + string_cost_one_line (tty->TS_pad_inserted_char));
1065 else
1067 ins_startup_cost = 9999;
1068 ins_cost_per_char = 0;
1071 if (tty->TS_del_multi_chars)
1073 del_cost_per_char = 0;
1074 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1076 else if (tty->TS_del_char)
1078 del_startup_cost = (string_cost (tty->TS_delete_mode)
1079 + string_cost (tty->TS_end_delete_mode));
1080 if (tty->delete_in_insert_mode)
1081 del_startup_cost /= 2;
1082 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1084 else
1086 del_startup_cost = 9999;
1087 del_cost_per_char = 0;
1090 /* Delete costs are at negative offsets */
1091 p = &char_ins_del_cost (f)[0];
1092 for (i = FRAME_COLS (f); --i >= 0;)
1093 *--p = (del_startup_cost += del_cost_per_char);
1095 /* Doing nothing is free */
1096 p = &char_ins_del_cost (f)[0];
1097 *p++ = 0;
1099 /* Insert costs are at positive offsets */
1100 for (i = FRAME_COLS (f); --i >= 0;)
1101 *p++ = (ins_startup_cost += ins_cost_per_char);
1104 void
1105 calculate_costs (struct frame *frame)
1107 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1109 if (FRAME_TERMCAP_P (frame))
1111 struct tty_display_info *tty = FRAME_TTY (frame);
1112 register char *f = (tty->TS_set_scroll_region
1113 ? tty->TS_set_scroll_region
1114 : tty->TS_set_scroll_region_1);
1116 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1118 tty->costs_set = 1;
1120 /* These variables are only used for terminal stuff. They are
1121 allocated once for the terminal frame of X-windows emacs, but not
1122 used afterwards.
1124 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1125 X turns off char_ins_del_ok. */
1127 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1128 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1130 if (char_ins_del_vector != 0)
1131 char_ins_del_vector
1132 = (int *) xrealloc (char_ins_del_vector,
1133 (sizeof (int)
1134 + 2 * max_frame_cols * sizeof (int)));
1135 else
1136 char_ins_del_vector
1137 = (int *) xmalloc (sizeof (int)
1138 + 2 * max_frame_cols * sizeof (int));
1140 bzero (char_ins_del_vector, (sizeof (int)
1141 + 2 * max_frame_cols * sizeof (int)));
1144 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1145 do_line_insertion_deletion_costs (frame,
1146 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1147 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1148 f, f, 1);
1149 else
1150 do_line_insertion_deletion_costs (frame,
1151 tty->TS_ins_line, tty->TS_ins_multi_lines,
1152 tty->TS_del_line, tty->TS_del_multi_lines,
1153 0, 0, 1);
1155 calculate_ins_del_char_costs (frame);
1157 /* Don't use TS_repeat if its padding is worse than sending the chars */
1158 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1159 tty->RPov = string_cost (tty->TS_repeat);
1160 else
1161 tty->RPov = FRAME_COLS (frame) * 2;
1163 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1167 struct fkey_table {
1168 char *cap, *name;
1171 /* Termcap capability names that correspond directly to X keysyms.
1172 Some of these (marked "terminfo") aren't supplied by old-style
1173 (Berkeley) termcap entries. They're listed in X keysym order;
1174 except we put the keypad keys first, so that if they clash with
1175 other keys (as on the IBM PC keyboard) they get overridden.
1178 static struct fkey_table keys[] =
1180 {"kh", "home"}, /* termcap */
1181 {"kl", "left"}, /* termcap */
1182 {"ku", "up"}, /* termcap */
1183 {"kr", "right"}, /* termcap */
1184 {"kd", "down"}, /* termcap */
1185 {"%8", "prior"}, /* terminfo */
1186 {"%5", "next"}, /* terminfo */
1187 {"@7", "end"}, /* terminfo */
1188 {"@1", "begin"}, /* terminfo */
1189 {"*6", "select"}, /* terminfo */
1190 {"%9", "print"}, /* terminfo */
1191 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1193 * "insert" --- see below
1195 {"&8", "undo"}, /* terminfo */
1196 {"%0", "redo"}, /* terminfo */
1197 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1198 {"@0", "find"}, /* terminfo */
1199 {"@2", "cancel"}, /* terminfo */
1200 {"%1", "help"}, /* terminfo */
1202 * "break" goes here, but can't be reliably intercepted with termcap
1204 {"&4", "reset"}, /* terminfo --- actually `restart' */
1206 * "system" and "user" --- no termcaps
1208 {"kE", "clearline"}, /* terminfo */
1209 {"kA", "insertline"}, /* terminfo */
1210 {"kL", "deleteline"}, /* terminfo */
1211 {"kI", "insertchar"}, /* terminfo */
1212 {"kD", "deletechar"}, /* terminfo */
1213 {"kB", "backtab"}, /* terminfo */
1215 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1217 {"@8", "kp-enter"}, /* terminfo */
1219 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1220 * "kp-multiply", "kp-add", "kp-separator",
1221 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1222 * --- no termcaps for any of these.
1224 {"K4", "kp-1"}, /* terminfo */
1226 * "kp-2" --- no termcap
1228 {"K5", "kp-3"}, /* terminfo */
1230 * "kp-4" --- no termcap
1232 {"K2", "kp-5"}, /* terminfo */
1234 * "kp-6" --- no termcap
1236 {"K1", "kp-7"}, /* terminfo */
1238 * "kp-8" --- no termcap
1240 {"K3", "kp-9"}, /* terminfo */
1242 * "kp-equal" --- no termcap
1244 {"k1", "f1"},
1245 {"k2", "f2"},
1246 {"k3", "f3"},
1247 {"k4", "f4"},
1248 {"k5", "f5"},
1249 {"k6", "f6"},
1250 {"k7", "f7"},
1251 {"k8", "f8"},
1252 {"k9", "f9"},
1254 {"&0", "S-cancel"}, /*shifted cancel key*/
1255 {"&9", "S-begin"}, /*shifted begin key*/
1256 {"*0", "S-find"}, /*shifted find key*/
1257 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1258 {"*4", "S-delete"}, /*shifted delete-character key*/
1259 {"*7", "S-end"}, /*shifted end key*/
1260 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1261 {"#1", "S-help"}, /*shifted help key*/
1262 {"#2", "S-home"}, /*shifted home key*/
1263 {"#3", "S-insert"}, /*shifted insert-character key*/
1264 {"#4", "S-left"}, /*shifted left-arrow key*/
1265 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1266 {"%c", "S-next"}, /*shifted next key*/
1267 {"%e", "S-prior"}, /*shifted previous key*/
1268 {"%f", "S-print"}, /*shifted print key*/
1269 {"%g", "S-redo"}, /*shifted redo key*/
1270 {"%i", "S-right"}, /*shifted right-arrow key*/
1271 {"!3", "S-undo"} /*shifted undo key*/
1274 static char **term_get_fkeys_address;
1275 static KBOARD *term_get_fkeys_kboard;
1276 static Lisp_Object term_get_fkeys_1 ();
1278 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1279 This function scans the termcap function key sequence entries, and
1280 adds entries to Vinput_decode_map for each function key it finds. */
1282 static void
1283 term_get_fkeys (address, kboard)
1284 char **address;
1285 KBOARD *kboard;
1287 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1288 errors during the call. The only errors should be from Fdefine_key
1289 when given a key sequence containing an invalid prefix key. If the
1290 termcap defines function keys which use a prefix that is already bound
1291 to a command by the default bindings, we should silently ignore that
1292 function key specification, rather than giving the user an error and
1293 refusing to run at all on such a terminal. */
1295 extern Lisp_Object Fidentity ();
1296 term_get_fkeys_address = address;
1297 term_get_fkeys_kboard = kboard;
1298 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1301 static Lisp_Object
1302 term_get_fkeys_1 ()
1304 int i;
1306 char **address = term_get_fkeys_address;
1307 KBOARD *kboard = term_get_fkeys_kboard;
1309 /* This can happen if CANNOT_DUMP or with strange options. */
1310 if (!KEYMAPP (kboard->Vinput_decode_map))
1311 kboard->Vinput_decode_map = Fmake_sparse_keymap (Qnil);
1313 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1315 char *sequence = tgetstr (keys[i].cap, address);
1316 if (sequence)
1317 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
1318 Fmake_vector (make_number (1),
1319 intern (keys[i].name)));
1322 /* The uses of the "k0" capability are inconsistent; sometimes it
1323 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1324 We will attempt to politely accommodate both systems by testing for
1325 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1328 char *k_semi = tgetstr ("k;", address);
1329 char *k0 = tgetstr ("k0", address);
1330 char *k0_name = "f10";
1332 if (k_semi)
1334 if (k0)
1335 /* Define f0 first, so that f10 takes precedence in case the
1336 key sequences happens to be the same. */
1337 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
1338 Fmake_vector (make_number (1), intern ("f0")));
1339 Fdefine_key (kboard->Vinput_decode_map, build_string (k_semi),
1340 Fmake_vector (make_number (1), intern ("f10")));
1342 else if (k0)
1343 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
1344 Fmake_vector (make_number (1), intern (k0_name)));
1347 /* Set up cookies for numbered function keys above f10. */
1349 char fcap[3], fkey[4];
1351 fcap[0] = 'F'; fcap[2] = '\0';
1352 for (i = 11; i < 64; i++)
1354 if (i <= 19)
1355 fcap[1] = '1' + i - 11;
1356 else if (i <= 45)
1357 fcap[1] = 'A' + i - 20;
1358 else
1359 fcap[1] = 'a' + i - 46;
1362 char *sequence = tgetstr (fcap, address);
1363 if (sequence)
1365 sprintf (fkey, "f%d", i);
1366 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
1367 Fmake_vector (make_number (1),
1368 intern (fkey)));
1375 * Various mappings to try and get a better fit.
1378 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1379 if (!tgetstr (cap1, address)) \
1381 char *sequence = tgetstr (cap2, address); \
1382 if (sequence) \
1383 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence), \
1384 Fmake_vector (make_number (1), \
1385 intern (sym))); \
1388 /* if there's no key_next keycap, map key_npage to `next' keysym */
1389 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1390 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1391 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1392 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1393 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1394 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1395 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1397 /* IBM has their own non-standard dialect of terminfo.
1398 If the standard name isn't found, try the IBM name. */
1399 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1400 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1401 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1402 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1403 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1404 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1405 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1406 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1407 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1408 #undef CONDITIONAL_REASSIGN
1411 return Qnil;
1415 /***********************************************************************
1416 Character Display Information
1417 ***********************************************************************/
1419 /* Avoid name clash with functions defined in xterm.c */
1420 #ifdef static
1421 #define append_glyph append_glyph_term
1422 #define produce_stretch_glyph produce_stretch_glyph_term
1423 #endif
1425 static void append_glyph P_ ((struct it *));
1426 static void produce_stretch_glyph P_ ((struct it *));
1429 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1430 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1431 the character for which to produce glyphs; IT->face_id contains the
1432 character's face. Padding glyphs are appended if IT->c has a
1433 IT->pixel_width > 1. */
1435 static void
1436 append_glyph (it)
1437 struct it *it;
1439 struct glyph *glyph, *end;
1440 int i;
1442 xassert (it->glyph_row);
1443 glyph = (it->glyph_row->glyphs[it->area]
1444 + it->glyph_row->used[it->area]);
1445 end = it->glyph_row->glyphs[1 + it->area];
1447 for (i = 0;
1448 i < it->pixel_width && glyph < end;
1449 ++i)
1451 glyph->type = CHAR_GLYPH;
1452 glyph->pixel_width = 1;
1453 glyph->u.ch = it->char_to_display;
1454 glyph->face_id = it->face_id;
1455 glyph->padding_p = i > 0;
1456 glyph->charpos = CHARPOS (it->position);
1457 glyph->object = it->object;
1459 ++it->glyph_row->used[it->area];
1460 ++glyph;
1465 /* Produce glyphs for the display element described by IT. *IT
1466 specifies what we want to produce a glyph for (character, image, ...),
1467 and where in the glyph matrix we currently are (glyph row and hpos).
1468 produce_glyphs fills in output fields of *IT with information such as the
1469 pixel width and height of a character, and maybe output actual glyphs at
1470 the same time if IT->glyph_row is non-null. See the explanation of
1471 struct display_iterator in dispextern.h for an overview.
1473 produce_glyphs also stores the result of glyph width, ascent
1474 etc. computations in *IT.
1476 IT->glyph_row may be null, in which case produce_glyphs does not
1477 actually fill in the glyphs. This is used in the move_* functions
1478 in xdisp.c for text width and height computations.
1480 Callers usually don't call produce_glyphs directly;
1481 instead they use the macro PRODUCE_GLYPHS. */
1483 void
1484 produce_glyphs (it)
1485 struct it *it;
1487 /* If a hook is installed, let it do the work. */
1488 xassert (it->what == IT_CHARACTER
1489 || it->what == IT_COMPOSITION
1490 || it->what == IT_STRETCH);
1492 if (it->what == IT_STRETCH)
1494 produce_stretch_glyph (it);
1495 goto done;
1498 /* Nothing but characters are supported on terminal frames. For a
1499 composition sequence, it->c is the first character of the
1500 sequence. */
1501 xassert (it->what == IT_CHARACTER
1502 || it->what == IT_COMPOSITION);
1504 /* Maybe translate single-byte characters to multibyte. */
1505 it->char_to_display = it->c;
1507 if (it->c >= 040 && it->c < 0177)
1509 it->pixel_width = it->nglyphs = 1;
1510 if (it->glyph_row)
1511 append_glyph (it);
1513 else if (it->c == '\n')
1514 it->pixel_width = it->nglyphs = 0;
1515 else if (it->c == '\t')
1517 int absolute_x = (it->current_x
1518 + it->continuation_lines_width);
1519 int next_tab_x
1520 = (((1 + absolute_x + it->tab_width - 1)
1521 / it->tab_width)
1522 * it->tab_width);
1523 int nspaces;
1525 /* If part of the TAB has been displayed on the previous line
1526 which is continued now, continuation_lines_width will have
1527 been incremented already by the part that fitted on the
1528 continued line. So, we will get the right number of spaces
1529 here. */
1530 nspaces = next_tab_x - absolute_x;
1532 if (it->glyph_row)
1534 int n = nspaces;
1536 it->char_to_display = ' ';
1537 it->pixel_width = it->len = 1;
1539 while (n--)
1540 append_glyph (it);
1543 it->pixel_width = nspaces;
1544 it->nglyphs = nspaces;
1546 else if (SINGLE_BYTE_CHAR_P (it->c))
1548 if (unibyte_display_via_language_environment
1549 && (it->c >= 0240
1550 || !NILP (Vnonascii_translation_table)))
1552 int charset;
1554 it->char_to_display = unibyte_char_to_multibyte (it->c);
1555 charset = CHAR_CHARSET (it->char_to_display);
1556 it->pixel_width = CHARSET_WIDTH (charset);
1557 it->nglyphs = it->pixel_width;
1558 if (it->glyph_row)
1559 append_glyph (it);
1561 else
1563 /* Coming here means that it->c is from display table, thus we
1564 must send the code as is to the terminal. Although there's
1565 no way to know how many columns it occupies on a screen, it
1566 is a good assumption that a single byte code has 1-column
1567 width. */
1568 it->pixel_width = it->nglyphs = 1;
1569 if (it->glyph_row)
1570 append_glyph (it);
1573 else
1575 /* A multi-byte character. The display width is fixed for all
1576 characters of the set. Some of the glyphs may have to be
1577 ignored because they are already displayed in a continued
1578 line. */
1579 int charset = CHAR_CHARSET (it->c);
1581 it->pixel_width = CHARSET_WIDTH (charset);
1582 it->nglyphs = it->pixel_width;
1584 if (it->glyph_row)
1585 append_glyph (it);
1588 done:
1589 /* Advance current_x by the pixel width as a convenience for
1590 the caller. */
1591 if (it->area == TEXT_AREA)
1592 it->current_x += it->pixel_width;
1593 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1594 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1598 /* Produce a stretch glyph for iterator IT. IT->object is the value
1599 of the glyph property displayed. The value must be a list
1600 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1601 being recognized:
1603 1. `:width WIDTH' specifies that the space should be WIDTH *
1604 canonical char width wide. WIDTH may be an integer or floating
1605 point number.
1607 2. `:align-to HPOS' specifies that the space should be wide enough
1608 to reach HPOS, a value in canonical character units. */
1610 static void
1611 produce_stretch_glyph (it)
1612 struct it *it;
1614 /* (space :width WIDTH ...) */
1615 Lisp_Object prop, plist;
1616 int width = 0, align_to = -1;
1617 int zero_width_ok_p = 0;
1618 double tem;
1620 /* List should start with `space'. */
1621 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1622 plist = XCDR (it->object);
1624 /* Compute the width of the stretch. */
1625 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1626 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1628 /* Absolute width `:width WIDTH' specified and valid. */
1629 zero_width_ok_p = 1;
1630 width = (int)(tem + 0.5);
1632 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1633 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1635 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1636 align_to = (align_to < 0
1638 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1639 else if (align_to < 0)
1640 align_to = window_box_left_offset (it->w, TEXT_AREA);
1641 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1642 zero_width_ok_p = 1;
1644 else
1645 /* Nothing specified -> width defaults to canonical char width. */
1646 width = FRAME_COLUMN_WIDTH (it->f);
1648 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1649 width = 1;
1651 if (width > 0 && it->glyph_row)
1653 Lisp_Object o_object = it->object;
1654 Lisp_Object object = it->stack[it->sp - 1].string;
1655 int n = width;
1657 if (!STRINGP (object))
1658 object = it->w->buffer;
1659 it->object = object;
1660 it->char_to_display = ' ';
1661 it->pixel_width = it->len = 1;
1662 while (n--)
1663 append_glyph (it);
1664 it->object = o_object;
1666 it->pixel_width = width;
1667 it->nglyphs = width;
1671 /* Get information about special display element WHAT in an
1672 environment described by IT. WHAT is one of IT_TRUNCATION or
1673 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1674 non-null glyph_row member. This function ensures that fields like
1675 face_id, c, len of IT are left untouched. */
1677 void
1678 produce_special_glyphs (it, what)
1679 struct it *it;
1680 enum display_element_type what;
1682 struct it temp_it;
1683 GLYPH glyph;
1685 temp_it = *it;
1686 temp_it.dp = NULL;
1687 temp_it.what = IT_CHARACTER;
1688 temp_it.len = 1;
1689 temp_it.object = make_number (0);
1690 bzero (&temp_it.current, sizeof temp_it.current);
1692 if (what == IT_CONTINUATION)
1694 /* Continuation glyph. */
1695 if (it->dp
1696 && INTEGERP (DISP_CONTINUE_GLYPH (it->dp))
1697 && GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (it->dp))))
1699 glyph = XINT (DISP_CONTINUE_GLYPH (it->dp));
1700 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
1702 else
1703 glyph = '\\';
1705 else if (what == IT_TRUNCATION)
1707 /* Truncation glyph. */
1708 if (it->dp
1709 && INTEGERP (DISP_TRUNC_GLYPH (it->dp))
1710 && GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (it->dp))))
1712 glyph = XINT (DISP_TRUNC_GLYPH (it->dp));
1713 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
1715 else
1716 glyph = '$';
1718 else
1719 abort ();
1721 temp_it.c = FAST_GLYPH_CHAR (glyph);
1722 temp_it.face_id = FAST_GLYPH_FACE (glyph);
1723 temp_it.len = CHAR_BYTES (temp_it.c);
1725 produce_glyphs (&temp_it);
1726 it->pixel_width = temp_it.pixel_width;
1727 it->nglyphs = temp_it.pixel_width;
1732 /***********************************************************************
1733 Faces
1734 ***********************************************************************/
1736 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1737 one of the enumerators from enum no_color_bit, or a bit set built
1738 from them. Some display attributes may not be used together with
1739 color; the termcap capability `NC' specifies which ones. */
1741 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1742 (tty->TN_max_colors > 0 \
1743 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1744 : 1)
1746 /* Turn appearances of face FACE_ID on tty frame F on.
1747 FACE_ID is a realized face ID number, in the face cache. */
1749 static void
1750 turn_on_face (f, face_id)
1751 struct frame *f;
1752 int face_id;
1754 struct face *face = FACE_FROM_ID (f, face_id);
1755 long fg = face->foreground;
1756 long bg = face->background;
1757 struct tty_display_info *tty = FRAME_TTY (f);
1759 /* Do this first because TS_end_standout_mode may be the same
1760 as TS_exit_attribute_mode, which turns all appearances off. */
1761 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1763 if (tty->TN_max_colors > 0)
1765 if (fg >= 0 && bg >= 0)
1767 /* If the terminal supports colors, we can set them
1768 below without using reverse video. The face's fg
1769 and bg colors are set as they should appear on
1770 the screen, i.e. they take the inverse-video'ness
1771 of the face already into account. */
1773 else if (inverse_video)
1775 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1776 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1777 tty_toggle_highlight (tty);
1779 else
1781 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1782 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1783 tty_toggle_highlight (tty);
1786 else
1788 /* If we can't display colors, use reverse video
1789 if the face specifies that. */
1790 if (inverse_video)
1792 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1793 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1794 tty_toggle_highlight (tty);
1796 else
1798 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1799 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1800 tty_toggle_highlight (tty);
1805 if (face->tty_bold_p)
1807 if (MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1808 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
1810 else if (face->tty_dim_p)
1811 if (MAY_USE_WITH_COLORS_P (tty, NC_DIM))
1812 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
1814 /* Alternate charset and blinking not yet used. */
1815 if (face->tty_alt_charset_p
1816 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
1817 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
1819 if (face->tty_blinking_p
1820 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
1821 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
1823 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
1824 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
1826 if (tty->TN_max_colors > 0)
1828 char *ts, *p;
1830 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
1831 if (fg >= 0 && ts)
1833 p = tparam (ts, NULL, 0, (int) fg);
1834 OUTPUT (tty, p);
1835 xfree (p);
1838 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
1839 if (bg >= 0 && ts)
1841 p = tparam (ts, NULL, 0, (int) bg);
1842 OUTPUT (tty, p);
1843 xfree (p);
1849 /* Turn off appearances of face FACE_ID on tty frame F. */
1851 static void
1852 turn_off_face (f, face_id)
1853 struct frame *f;
1854 int face_id;
1856 struct face *face = FACE_FROM_ID (f, face_id);
1857 struct tty_display_info *tty = FRAME_TTY (f);
1859 xassert (face != NULL);
1861 if (tty->TS_exit_attribute_mode)
1863 /* Capability "me" will turn off appearance modes double-bright,
1864 half-bright, reverse-video, standout, underline. It may or
1865 may not turn off alt-char-mode. */
1866 if (face->tty_bold_p
1867 || face->tty_dim_p
1868 || face->tty_reverse_p
1869 || face->tty_alt_charset_p
1870 || face->tty_blinking_p
1871 || face->tty_underline_p)
1873 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
1874 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
1875 tty->standout_mode = 0;
1878 if (face->tty_alt_charset_p)
1879 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
1881 else
1883 /* If we don't have "me" we can only have those appearances
1884 that have exit sequences defined. */
1885 if (face->tty_alt_charset_p)
1886 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
1888 if (face->tty_underline_p)
1889 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
1892 /* Switch back to default colors. */
1893 if (tty->TN_max_colors > 0
1894 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
1895 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
1896 || (face->background != FACE_TTY_DEFAULT_COLOR
1897 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
1898 OUTPUT1_IF (tty, tty->TS_orig_pair);
1902 /* Return non-zero if the terminal on frame F supports all of the
1903 capabilities in CAPS simultaneously, with foreground and background
1904 colors FG and BG. */
1907 tty_capable_p (tty, caps, fg, bg)
1908 struct tty_display_info *tty;
1909 unsigned caps;
1910 unsigned long fg, bg;
1912 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
1913 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
1914 return 0;
1916 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
1917 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
1918 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
1919 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
1920 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
1921 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
1923 /* We can do it! */
1924 return 1;
1927 /* Return non-zero if the terminal is capable to display colors. */
1929 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
1930 0, 1, 0,
1931 doc: /* Return non-nil if the tty device TERMINAL can display colors.
1933 TERMINAL can be a terminal id, a frame or nil (meaning the selected
1934 frame's terminal). This function always returns nil if TERMINAL
1935 is not on a tty device. */)
1936 (terminal)
1937 Lisp_Object terminal;
1939 struct terminal *t = get_tty_terminal (terminal, 0);
1940 if (!t)
1941 return Qnil;
1942 else
1943 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
1946 /* Return the number of supported colors. */
1947 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
1948 Stty_display_color_cells, 0, 1, 0,
1949 doc: /* Return the number of colors supported by the tty device TERMINAL.
1951 TERMINAL can be a terminal id, a frame or nil (meaning the selected
1952 frame's terminal). This function always returns 0 if TERMINAL
1953 is not on a tty device. */)
1954 (terminal)
1955 Lisp_Object terminal;
1957 struct terminal *t = get_tty_terminal (terminal, 0);
1958 if (!t)
1959 return make_number (0);
1960 else
1961 return make_number (t->display_info.tty->TN_max_colors);
1964 #ifndef WINDOWSNT
1966 /* Declare here rather than in the function, as in the rest of Emacs,
1967 to work around an HPUX compiler bug (?). See
1968 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
1969 static int default_max_colors;
1970 static int default_max_pairs;
1971 static int default_no_color_video;
1972 static char *default_orig_pair;
1973 static char *default_set_foreground;
1974 static char *default_set_background;
1976 /* Save or restore the default color-related capabilities of this
1977 terminal. */
1978 static void
1979 tty_default_color_capabilities (struct tty_display_info *tty, int save)
1982 if (save)
1984 if (default_orig_pair)
1985 xfree (default_orig_pair);
1986 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
1988 if (default_set_foreground)
1989 xfree (default_set_foreground);
1990 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
1991 : NULL;
1993 if (default_set_background)
1994 xfree (default_set_background);
1995 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
1996 : NULL;
1998 default_max_colors = tty->TN_max_colors;
1999 default_max_pairs = tty->TN_max_pairs;
2000 default_no_color_video = tty->TN_no_color_video;
2002 else
2004 tty->TS_orig_pair = default_orig_pair;
2005 tty->TS_set_foreground = default_set_foreground;
2006 tty->TS_set_background = default_set_background;
2007 tty->TN_max_colors = default_max_colors;
2008 tty->TN_max_pairs = default_max_pairs;
2009 tty->TN_no_color_video = default_no_color_video;
2013 /* Setup one of the standard tty color schemes according to MODE.
2014 MODE's value is generally the number of colors which we want to
2015 support; zero means set up for the default capabilities, the ones
2016 we saw at init_tty time; -1 means turn off color support. */
2017 static void
2018 tty_setup_colors (struct tty_display_info *tty, int mode)
2020 /* Canonicalize all negative values of MODE. */
2021 if (mode < -1)
2022 mode = -1;
2024 switch (mode)
2026 case -1: /* no colors at all */
2027 tty->TN_max_colors = 0;
2028 tty->TN_max_pairs = 0;
2029 tty->TN_no_color_video = 0;
2030 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2031 break;
2032 case 0: /* default colors, if any */
2033 default:
2034 tty_default_color_capabilities (tty, 0);
2035 break;
2036 case 8: /* 8 standard ANSI colors */
2037 tty->TS_orig_pair = "\033[0m";
2038 #ifdef TERMINFO
2039 tty->TS_set_foreground = "\033[3%p1%dm";
2040 tty->TS_set_background = "\033[4%p1%dm";
2041 #else
2042 tty->TS_set_foreground = "\033[3%dm";
2043 tty->TS_set_background = "\033[4%dm";
2044 #endif
2045 tty->TN_max_colors = 8;
2046 tty->TN_max_pairs = 64;
2047 tty->TN_no_color_video = 0;
2048 break;
2052 void
2053 set_tty_color_mode (f, val)
2054 struct frame *f;
2055 Lisp_Object val;
2057 Lisp_Object color_mode_spec, current_mode_spec;
2058 Lisp_Object color_mode, current_mode;
2059 int mode, old_mode;
2060 extern Lisp_Object Qtty_color_mode;
2061 Lisp_Object tty_color_mode_alist;
2063 tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
2064 Qnil);
2066 if (INTEGERP (val))
2067 color_mode = val;
2068 else
2070 if (NILP (tty_color_mode_alist))
2071 color_mode_spec = Qnil;
2072 else
2073 color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
2075 if (CONSP (color_mode_spec))
2076 color_mode = XCDR (color_mode_spec);
2077 else
2078 color_mode = Qnil;
2081 current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
2083 if (CONSP (current_mode_spec))
2084 current_mode = XCDR (current_mode_spec);
2085 else
2086 current_mode = Qnil;
2087 if (INTEGERP (color_mode))
2088 mode = XINT (color_mode);
2089 else
2090 mode = 0; /* meaning default */
2091 if (INTEGERP (current_mode))
2092 old_mode = XINT (current_mode);
2093 else
2094 old_mode = 0;
2096 if (mode != old_mode)
2098 tty_setup_colors (FRAME_TTY (f), mode);
2099 /* This recomputes all the faces given the new color
2100 definitions. */
2101 call0 (intern ("tty-set-up-initial-frame-faces"));
2102 redraw_frame (f);
2106 #endif /* !WINDOWSNT */
2110 /* Return the tty display object specified by TERMINAL. */
2112 struct terminal *
2113 get_tty_terminal (Lisp_Object terminal, int throw)
2115 struct terminal *t = get_terminal (terminal, throw);
2117 if (t && t->type != output_termcap)
2119 if (throw)
2120 error ("Device %d is not a termcap terminal device", t->id);
2121 else
2122 return NULL;
2125 return t;
2128 /* Return an active termcap device that uses the tty device with the
2129 given name.
2131 This function ignores suspended devices.
2133 Returns NULL if the named terminal device is not opened. */
2135 struct terminal *
2136 get_named_tty (name)
2137 char *name;
2139 struct terminal *t;
2141 if (!name)
2142 abort ();
2144 for (t = terminal_list; t; t = t->next_terminal)
2146 if (t->type == output_termcap
2147 && !strcmp (t->display_info.tty->name, name)
2148 && TERMINAL_ACTIVE_P (t))
2149 return t;
2152 return 0;
2156 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2157 doc: /* Return the type of the tty device that TERMINAL uses.
2158 Returns nil if TERMINAL is not on a tty device.
2160 TERMINAL can be a terminal id, a frame or nil (meaning the selected
2161 frame's terminal). */)
2162 (terminal)
2163 Lisp_Object terminal;
2165 struct terminal *t = get_terminal (terminal, 1);
2167 if (t->type != output_termcap)
2168 return Qnil;
2170 if (t->display_info.tty->type)
2171 return build_string (t->display_info.tty->type);
2172 else
2173 return Qnil;
2176 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2177 doc: /* Return non-nil if TERMINAL is on the controlling tty of the Emacs process.
2179 TERMINAL can be a terminal id, a frame or nil (meaning the selected
2180 frame's terminal). This function always returns nil if TERMINAL
2181 is not on a tty device. */)
2182 (terminal)
2183 Lisp_Object terminal;
2185 struct terminal *t = get_terminal (terminal, 1);
2187 if (t->type != output_termcap || strcmp (t->display_info.tty->name, DEV_TTY))
2188 return Qnil;
2189 else
2190 return Qt;
2193 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2194 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2195 This is used to override the terminfo data, for certain terminals that
2196 do not really do underlining, but say that they do. This function has
2197 no effect if used on a non-tty terminal.
2199 TERMINAL can be a terminal id, a frame or nil (meaning the selected
2200 frame's terminal). This function always returns nil if TERMINAL
2201 is not on a tty device. */)
2202 (terminal)
2203 Lisp_Object terminal;
2205 struct terminal *t = get_terminal (terminal, 1);
2207 if (t->type == output_termcap)
2208 t->display_info.tty->TS_enter_underline_mode = 0;
2209 return Qnil;
2214 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2215 doc: /* Suspend the terminal device TTY.
2217 The device is restored to its default state, and Emacs ceases all
2218 access to the tty device. Frames that use the device are not deleted,
2219 but input is not read from them and if they change, their display is
2220 not updated.
2222 TTY may be a terminal id, a frame, or nil for the terminal device of
2223 the currently selected frame.
2225 This function runs `suspend-tty-functions' after suspending the
2226 device. The functions are run with one arg, the id of the suspended
2227 terminal device.
2229 `suspend-tty' does nothing if it is called on a device that is already
2230 suspended.
2232 A suspended tty may be resumed by calling `resume-tty' on it. */)
2233 (tty)
2234 Lisp_Object tty;
2236 struct terminal *t = get_tty_terminal (tty, 1);
2237 FILE *f;
2239 if (!t)
2240 error ("Unknown tty device");
2242 f = t->display_info.tty->input;
2244 if (f)
2246 /* First run `suspend-tty-functions' and then clean up the tty
2247 state because `suspend-tty-functions' might need to change
2248 the tty state. */
2249 if (!NILP (Vrun_hooks))
2251 Lisp_Object args[2];
2252 args[0] = intern ("suspend-tty-functions");
2253 XSETTERMINAL (args[1], t);
2254 Frun_hook_with_args (2, args);
2257 reset_sys_modes (t->display_info.tty);
2259 delete_keyboard_wait_descriptor (fileno (f));
2261 fclose (f);
2262 if (f != t->display_info.tty->output)
2263 fclose (t->display_info.tty->output);
2265 t->display_info.tty->input = 0;
2266 t->display_info.tty->output = 0;
2268 if (FRAMEP (t->display_info.tty->top_frame))
2269 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2273 /* Clear display hooks to prevent further output. */
2274 clear_tty_hooks (t);
2276 return Qnil;
2279 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2280 doc: /* Resume the previously suspended terminal device TTY.
2281 The terminal is opened and reinitialized. Frames that are on the
2282 suspended terminal are revived.
2284 It is an error to resume a terminal while another terminal is active
2285 on the same device.
2287 This function runs `resume-tty-functions' after resuming the terminal.
2288 The functions are run with one arg, the id of the resumed terminal
2289 device.
2291 `resume-tty' does nothing if it is called on a device that is not
2292 suspended.
2294 TTY may be a terminal id, a frame, or nil for the terminal device of
2295 the currently selected frame. */)
2296 (tty)
2297 Lisp_Object tty;
2299 struct terminal *t = get_tty_terminal (tty, 1);
2300 int fd;
2302 if (!t)
2303 error ("Unknown tty device");
2305 if (!t->display_info.tty->input)
2307 if (get_named_tty (t->display_info.tty->name))
2308 error ("Cannot resume display while another display is active on the same device");
2310 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2312 if (fd == -1)
2313 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2315 if (strcmp (t->display_info.tty->name, DEV_TTY))
2316 dissociate_if_controlling_tty (fd);
2318 t->display_info.tty->output = fdopen (fd, "w+");
2319 t->display_info.tty->input = t->display_info.tty->output;
2321 add_keyboard_wait_descriptor (fd);
2323 if (FRAMEP (t->display_info.tty->top_frame))
2324 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2326 init_sys_modes (t->display_info.tty);
2328 /* Run `resume-tty-functions'. */
2329 if (!NILP (Vrun_hooks))
2331 Lisp_Object args[2];
2332 args[0] = intern ("resume-tty-functions");
2333 XSETTERMINAL (args[1], t);
2334 Frun_hook_with_args (2, args);
2338 set_tty_hooks (t);
2340 return Qnil;
2344 /***********************************************************************
2345 Mouse
2346 ***********************************************************************/
2348 #ifdef HAVE_GPM
2349 void
2350 term_mouse_moveto (int x, int y)
2352 /* TODO: how to set mouse position?
2353 const char *name;
2354 int fd;
2355 name = (const char *) ttyname (0);
2356 fd = open (name, O_WRONLY);
2357 SOME_FUNCTION (x, y, fd);
2358 close (fd);
2359 last_mouse_x = x;
2360 last_mouse_y = y; */
2363 static void
2364 term_show_mouse_face (enum draw_glyphs_face draw)
2366 struct window *w = XWINDOW (mouse_face_window);
2367 int save_x, save_y;
2368 int i;
2370 struct frame *f = XFRAME (w->frame);
2371 struct tty_display_info *tty = FRAME_TTY (f);
2373 if (/* If window is in the process of being destroyed, don't bother
2374 to do anything. */
2375 w->current_matrix != NULL
2376 /* Recognize when we are called to operate on rows that don't exist
2377 anymore. This can happen when a window is split. */
2378 && mouse_face_end_row < w->current_matrix->nrows)
2380 /* write_glyphs writes at cursor position, so we need to
2381 temporarily move cursor coordinates to the beginning of
2382 the highlight region. */
2384 /* Save current cursor co-ordinates */
2385 save_y = curY (tty);
2386 save_x = curX (tty);
2388 /* Note that mouse_face_beg_row etc. are window relative. */
2389 for (i = mouse_face_beg_row; i <= mouse_face_end_row; i++)
2391 int start_hpos, end_hpos, nglyphs;
2392 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
2394 /* Don't do anything if row doesn't have valid contents. */
2395 if (!row->enabled_p)
2396 continue;
2398 /* For all but the first row, the highlight starts at column 0. */
2399 if (i == mouse_face_beg_row)
2400 start_hpos = mouse_face_beg_col;
2401 else
2402 start_hpos = 0;
2404 if (i == mouse_face_end_row)
2405 end_hpos = mouse_face_end_col;
2406 else
2408 end_hpos = row->used[TEXT_AREA];
2409 if (draw == DRAW_NORMAL_TEXT)
2410 row->fill_line_p = 1; /* Clear to end of line */
2413 if (end_hpos <= start_hpos)
2414 continue;
2415 /* Record that some glyphs of this row are displayed in
2416 mouse-face. */
2417 row->mouse_face_p = draw > 0;
2419 nglyphs = end_hpos - start_hpos;
2421 if (end_hpos >= row->used[TEXT_AREA])
2422 nglyphs = row->used[TEXT_AREA] - start_hpos;
2424 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2425 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos
2426 + WINDOW_LEFT_EDGE_X (w);
2428 cursor_to (f, pos_y, pos_x);
2430 if (draw == DRAW_MOUSE_FACE)
2432 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2433 nglyphs, mouse_face_face_id);
2435 else /* draw == DRAW_NORMAL_TEXT */
2436 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
2438 cursor_to (f, save_y, save_x);
2442 static void
2443 term_clear_mouse_face ()
2445 if (!NILP (mouse_face_window))
2446 term_show_mouse_face (DRAW_NORMAL_TEXT);
2448 mouse_face_beg_row = mouse_face_beg_col = -1;
2449 mouse_face_end_row = mouse_face_end_col = -1;
2450 mouse_face_window = Qnil;
2453 /* Find the glyph matrix position of buffer position POS in window W.
2454 *HPOS and *VPOS are set to the positions found. W's current glyphs
2455 must be up to date. If POS is above window start return (0, 0).
2456 If POS is after end of W, return end of last line in W.
2457 - taken from msdos.c */
2458 static int
2459 fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
2461 int i, lastcol, line_start_position, maybe_next_line_p = 0;
2462 int yb = window_text_bottom_y (w);
2463 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0), *best_row = row;
2465 while (row->y < yb)
2467 if (row->used[TEXT_AREA])
2468 line_start_position = row->glyphs[TEXT_AREA]->charpos;
2469 else
2470 line_start_position = 0;
2472 if (line_start_position > pos)
2473 break;
2474 /* If the position sought is the end of the buffer,
2475 don't include the blank lines at the bottom of the window. */
2476 else if (line_start_position == pos
2477 && pos == BUF_ZV (XBUFFER (w->buffer)))
2479 maybe_next_line_p = 1;
2480 break;
2482 else if (line_start_position > 0)
2483 best_row = row;
2485 /* Don't overstep the last matrix row, lest we get into the
2486 never-never land... */
2487 if (row->y + 1 >= yb)
2488 break;
2490 ++row;
2493 /* Find the right column within BEST_ROW. */
2494 lastcol = 0;
2495 row = best_row;
2496 for (i = 0; i < row->used[TEXT_AREA]; i++)
2498 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
2499 int charpos;
2501 charpos = glyph->charpos;
2502 if (charpos == pos)
2504 *hpos = i;
2505 *vpos = row->y;
2506 return 1;
2508 else if (charpos > pos)
2509 break;
2510 else if (charpos > 0)
2511 lastcol = i;
2514 /* If we're looking for the end of the buffer,
2515 and we didn't find it in the line we scanned,
2516 use the start of the following line. */
2517 if (maybe_next_line_p)
2519 ++row;
2520 lastcol = 0;
2523 *vpos = row->y;
2524 *hpos = lastcol + 1;
2525 return 0;
2528 static void
2529 term_mouse_highlight (struct frame *f, int x, int y)
2531 enum window_part part;
2532 Lisp_Object window;
2533 struct window *w;
2534 struct buffer *b;
2536 if (NILP (Vmouse_highlight)
2537 || !f->glyphs_initialized_p)
2538 return;
2540 /* Which window is that in? */
2541 window = window_from_coordinates (f, x, y, &part, &x, &y, 0);
2543 /* Not on a window -> return. */
2544 if (!WINDOWP (window))
2545 return;
2547 if (!EQ (window, mouse_face_window))
2548 term_clear_mouse_face ();
2550 w = XWINDOW (window);
2552 /* Are we in a window whose display is up to date?
2553 And verify the buffer's text has not changed. */
2554 b = XBUFFER (w->buffer);
2555 if (part == ON_TEXT
2556 && EQ (w->window_end_valid, w->buffer)
2557 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
2558 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
2560 int pos, i, nrows = w->current_matrix->nrows;
2561 struct glyph_row *row;
2562 struct glyph *glyph;
2564 /* Find the glyph under X/Y. */
2565 glyph = NULL;
2566 if (y >= 0 && y < nrows)
2568 row = MATRIX_ROW (w->current_matrix, y);
2569 /* Give up if some row before the one we are looking for is
2570 not enabled. */
2571 for (i = 0; i <= y; i++)
2572 if (!MATRIX_ROW (w->current_matrix, i)->enabled_p)
2573 break;
2574 if (i > y /* all rows upto and including the one at Y are enabled */
2575 && row->displays_text_p
2576 && x < window_box_width (w, TEXT_AREA))
2578 glyph = row->glyphs[TEXT_AREA];
2579 if (x >= row->used[TEXT_AREA])
2580 glyph = NULL;
2581 else
2583 glyph += x;
2584 if (!BUFFERP (glyph->object))
2585 glyph = NULL;
2590 /* Clear mouse face if X/Y not over text. */
2591 if (glyph == NULL)
2593 term_clear_mouse_face ();
2594 return;
2597 if (!BUFFERP (glyph->object))
2598 abort ();
2599 pos = glyph->charpos;
2601 /* Check for mouse-face. */
2603 extern Lisp_Object Qmouse_face;
2604 Lisp_Object mouse_face, overlay, position, *overlay_vec;
2605 int noverlays, obegv, ozv;
2606 struct buffer *obuf;
2608 /* If we get an out-of-range value, return now; avoid an error. */
2609 if (pos > BUF_Z (b))
2610 return;
2612 /* Make the window's buffer temporarily current for
2613 overlays_at and compute_char_face. */
2614 obuf = current_buffer;
2615 current_buffer = b;
2616 obegv = BEGV;
2617 ozv = ZV;
2618 BEGV = BEG;
2619 ZV = Z;
2621 /* Is this char mouse-active? */
2622 XSETINT (position, pos);
2624 /* Put all the overlays we want in a vector in overlay_vec. */
2625 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
2626 /* Sort overlays into increasing priority order. */
2627 noverlays = sort_overlays (overlay_vec, noverlays, w);
2629 /* Check mouse-face highlighting. */
2630 if (!(EQ (window, mouse_face_window)
2631 && y >= mouse_face_beg_row
2632 && y <= mouse_face_end_row
2633 && (y > mouse_face_beg_row
2634 || x >= mouse_face_beg_col)
2635 && (y < mouse_face_end_row
2636 || x < mouse_face_end_col
2637 || mouse_face_past_end)))
2639 /* Clear the display of the old active region, if any. */
2640 term_clear_mouse_face ();
2642 /* Find the highest priority overlay that has a mouse-face
2643 property. */
2644 overlay = Qnil;
2645 for (i = noverlays - 1; i >= 0; --i)
2647 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
2648 if (!NILP (mouse_face))
2650 overlay = overlay_vec[i];
2651 break;
2655 /* If no overlay applies, get a text property. */
2656 if (NILP (overlay))
2657 mouse_face = Fget_text_property (position, Qmouse_face,
2658 w->buffer);
2660 /* Handle the overlay case. */
2661 if (!NILP (overlay))
2663 /* Find the range of text around this char that
2664 should be active. */
2665 Lisp_Object before, after;
2666 int ignore;
2669 before = Foverlay_start (overlay);
2670 after = Foverlay_end (overlay);
2671 /* Record this as the current active region. */
2672 fast_find_position (w, XFASTINT (before),
2673 &mouse_face_beg_col,
2674 &mouse_face_beg_row);
2676 mouse_face_past_end
2677 = !fast_find_position (w, XFASTINT (after),
2678 &mouse_face_end_col,
2679 &mouse_face_end_row);
2680 mouse_face_window = window;
2682 mouse_face_face_id
2683 = face_at_buffer_position (w, pos, 0, 0,
2684 &ignore, pos + 1, 1);
2686 /* Display it as active. */
2687 term_show_mouse_face (DRAW_MOUSE_FACE);
2689 /* Handle the text property case. */
2690 else if (!NILP (mouse_face))
2692 /* Find the range of text around this char that
2693 should be active. */
2694 Lisp_Object before, after, beginning, end;
2695 int ignore;
2697 beginning = Fmarker_position (w->start);
2698 XSETINT (end, (BUF_Z (b) - XFASTINT (w->window_end_pos)));
2699 before
2700 = Fprevious_single_property_change (make_number (pos + 1),
2701 Qmouse_face,
2702 w->buffer, beginning);
2703 after
2704 = Fnext_single_property_change (position, Qmouse_face,
2705 w->buffer, end);
2707 /* Record this as the current active region. */
2708 fast_find_position (w, XFASTINT (before),
2709 &mouse_face_beg_col,
2710 &mouse_face_beg_row);
2711 mouse_face_past_end
2712 = !fast_find_position (w, XFASTINT (after),
2713 &mouse_face_end_col,
2714 &mouse_face_end_row);
2715 mouse_face_window = window;
2717 mouse_face_face_id
2718 = face_at_buffer_position (w, pos, 0, 0,
2719 &ignore, pos + 1, 1);
2721 /* Display it as active. */
2722 term_show_mouse_face (DRAW_MOUSE_FACE);
2726 /* Look for a `help-echo' property. */
2728 Lisp_Object help;
2729 extern Lisp_Object Qhelp_echo;
2731 /* Check overlays first. */
2732 help = Qnil;
2733 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
2735 overlay = overlay_vec[i];
2736 help = Foverlay_get (overlay, Qhelp_echo);
2739 if (!NILP (help))
2741 help_echo_string = help;
2742 help_echo_window = window;
2743 help_echo_object = overlay;
2744 help_echo_pos = pos;
2746 /* Try text properties. */
2747 else if (NILP (help)
2748 && ((STRINGP (glyph->object)
2749 && glyph->charpos >= 0
2750 && glyph->charpos < SCHARS (glyph->object))
2751 || (BUFFERP (glyph->object)
2752 && glyph->charpos >= BEGV
2753 && glyph->charpos < ZV)))
2755 help = Fget_text_property (make_number (glyph->charpos),
2756 Qhelp_echo, glyph->object);
2757 if (!NILP (help))
2759 help_echo_string = help;
2760 help_echo_window = window;
2761 help_echo_object = glyph->object;
2762 help_echo_pos = glyph->charpos;
2767 BEGV = obegv;
2768 ZV = ozv;
2769 current_buffer = obuf;
2774 static int
2775 term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2777 /* Has the mouse moved off the glyph it was on at the last sighting? */
2778 if (event->x != last_mouse_x || event->y != last_mouse_y)
2780 frame->mouse_moved = 1;
2781 term_mouse_highlight (frame, event->x, event->y);
2782 /* Remember which glyph we're now on. */
2783 last_mouse_x = event->x;
2784 last_mouse_y = event->y;
2785 return 1;
2787 return 0;
2790 /* Return the current position of the mouse.
2792 Set *f to the frame the mouse is in, or zero if the mouse is in no
2793 Emacs frame. If it is set to zero, all the other arguments are
2794 garbage.
2796 Set *bar_window to Qnil, and *x and *y to the column and
2797 row of the character cell the mouse is over.
2799 Set *time to the time the mouse was at the returned position.
2801 This clears mouse_moved until the next motion
2802 event arrives. */
2803 static void
2804 term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2805 enum scroll_bar_part *part, Lisp_Object *x,
2806 Lisp_Object *y, unsigned long *time)
2808 struct timeval now;
2810 *fp = SELECTED_FRAME ();
2811 (*fp)->mouse_moved = 0;
2813 *bar_window = Qnil;
2814 *part = 0;
2816 XSETINT (*x, last_mouse_x);
2817 XSETINT (*y, last_mouse_y);
2818 gettimeofday(&now, 0);
2819 *time = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2822 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2824 If the event is a button press, then note that we have grabbed
2825 the mouse. */
2827 static Lisp_Object
2828 term_mouse_click (struct input_event *result, Gpm_Event *event,
2829 struct frame *f)
2831 struct timeval now;
2832 int i, j;
2834 result->kind = GPM_CLICK_EVENT;
2835 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2837 if (event->buttons & j) {
2838 result->code = i; /* button number */
2839 break;
2842 gettimeofday(&now, 0);
2843 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2845 if (event->type & GPM_UP)
2846 result->modifiers = up_modifier;
2847 else if (event->type & GPM_DOWN)
2848 result->modifiers = down_modifier;
2849 else
2850 result->modifiers = 0;
2852 if (event->type & GPM_SINGLE)
2853 result->modifiers |= click_modifier;
2855 if (event->type & GPM_DOUBLE)
2856 result->modifiers |= double_modifier;
2858 if (event->type & GPM_TRIPLE)
2859 result->modifiers |= triple_modifier;
2861 if (event->type & GPM_DRAG)
2862 result->modifiers |= drag_modifier;
2864 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
2866 /* 1 << KG_SHIFT */
2867 if (event->modifiers & (1 << 0))
2868 result->modifiers |= shift_modifier;
2870 /* 1 << KG_CTRL */
2871 if (event->modifiers & (1 << 2))
2872 result->modifiers |= ctrl_modifier;
2874 /* 1 << KG_ALT || KG_ALTGR */
2875 if (event->modifiers & (1 << 3)
2876 || event->modifiers & (1 << 1))
2877 result->modifiers |= meta_modifier;
2880 XSETINT (result->x, event->x);
2881 XSETINT (result->y, event->y);
2882 XSETFRAME (result->frame_or_window, f);
2883 result->arg = Qnil;
2884 return Qnil;
2887 int
2888 handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
2890 struct frame *f = XFRAME (tty->top_frame);
2891 struct input_event ie;
2892 int do_help = 0;
2893 int count = 0;
2895 EVENT_INIT (ie);
2896 ie.kind = NO_EVENT;
2897 ie.arg = Qnil;
2899 if (event->type & (GPM_MOVE | GPM_DRAG)) {
2900 previous_help_echo_string = help_echo_string;
2901 help_echo_string = Qnil;
2903 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
2905 if (!term_mouse_movement (f, event))
2906 help_echo_string = previous_help_echo_string;
2908 /* If the contents of the global variable help_echo_string
2909 has changed, generate a HELP_EVENT. */
2910 if (!NILP (help_echo_string)
2911 || !NILP (previous_help_echo_string))
2912 do_help = 1;
2914 goto done;
2916 else {
2917 f->mouse_moved = 0;
2918 term_mouse_click (&ie, event, f);
2921 done:
2922 if (ie.kind != NO_EVENT)
2924 kbd_buffer_store_event_hold (&ie, hold_quit);
2925 count++;
2928 if (do_help
2929 && !(hold_quit && hold_quit->kind != NO_EVENT))
2931 Lisp_Object frame;
2933 if (f)
2934 XSETFRAME (frame, f);
2935 else
2936 frame = Qnil;
2938 gen_help_event (help_echo_string, frame, help_echo_window,
2939 help_echo_object, help_echo_pos);
2940 count++;
2943 return count;
2946 DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
2947 0, 0, 0,
2948 doc: /* Open a connection to Gpm.
2949 Gpm-mouse can only be activated for one tty at a time. */)
2952 struct frame *f = SELECTED_FRAME ();
2953 struct tty_display_info *tty
2954 = ((f)->output_method == output_termcap
2955 ? (f)->terminal->display_info.tty : NULL);
2956 Gpm_Connect connection;
2958 if (!tty)
2959 error ("Gpm-mouse only works in the GNU/Linux console");
2960 if (gpm_tty == tty)
2961 return Qnil; /* Already activated, nothing to do. */
2962 if (gpm_tty)
2963 error ("Gpm-mouse can only be activated for one tty at a time");
2965 connection.eventMask = ~0;
2966 connection.defaultMask = ~GPM_HARD;
2967 connection.maxMod = ~0;
2968 connection.minMod = 0;
2969 gpm_zerobased = 1;
2971 if (Gpm_Open (&connection, 0) < 0)
2972 error ("Gpm-mouse failed to connect to the gpm daemon");
2973 else
2975 gpm_tty = tty;
2976 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2977 to generate SIGIOs. Apparently we need to call reset_sys_modes
2978 before calling init_sys_modes. */
2979 reset_sys_modes (tty);
2980 init_sys_modes (tty);
2981 add_gpm_wait_descriptor (gpm_fd);
2982 return Qnil;
2986 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
2987 0, 0, 0,
2988 doc: /* Close a connection to Gpm. */)
2991 struct frame *f = SELECTED_FRAME ();
2992 struct tty_display_info *tty
2993 = ((f)->output_method == output_termcap
2994 ? (f)->terminal->display_info.tty : NULL);
2996 if (!tty || gpm_tty != tty)
2997 return Qnil; /* Not activated on this terminal, nothing to do. */
2999 if (gpm_fd >= 0)
3000 delete_gpm_wait_descriptor (gpm_fd);
3001 while (Gpm_Close()); /* close all the stack */
3002 gpm_tty = NULL;
3003 return Qnil;
3005 #endif /* HAVE_GPM */
3008 /***********************************************************************
3009 Initialization
3010 ***********************************************************************/
3012 /* Initialize the tty-dependent part of frame F. The frame must
3013 already have its device initialized. */
3015 void
3016 create_tty_output (struct frame *f)
3018 struct tty_output *t;
3020 if (! FRAME_TERMCAP_P (f))
3021 abort ();
3023 t = xmalloc (sizeof (struct tty_output));
3024 bzero (t, sizeof (struct tty_output));
3026 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
3028 f->output_data.tty = t;
3031 /* Delete the tty-dependent part of frame F. */
3033 static void
3034 delete_tty_output (struct frame *f)
3036 if (! FRAME_TERMCAP_P (f))
3037 abort ();
3039 xfree (f->output_data.tty);
3043 /* Reset the hooks in TERMINAL. */
3045 static void
3046 clear_tty_hooks (struct terminal *terminal)
3048 terminal->rif = 0;
3049 terminal->cursor_to_hook = 0;
3050 terminal->raw_cursor_to_hook = 0;
3051 terminal->clear_to_end_hook = 0;
3052 terminal->clear_frame_hook = 0;
3053 terminal->clear_end_of_line_hook = 0;
3054 terminal->ins_del_lines_hook = 0;
3055 terminal->insert_glyphs_hook = 0;
3056 terminal->write_glyphs_hook = 0;
3057 terminal->delete_glyphs_hook = 0;
3058 terminal->ring_bell_hook = 0;
3059 terminal->reset_terminal_modes_hook = 0;
3060 terminal->set_terminal_modes_hook = 0;
3061 terminal->update_begin_hook = 0;
3062 terminal->update_end_hook = 0;
3063 terminal->set_terminal_window_hook = 0;
3064 terminal->mouse_position_hook = 0;
3065 terminal->frame_rehighlight_hook = 0;
3066 terminal->frame_raise_lower_hook = 0;
3067 terminal->fullscreen_hook = 0;
3068 terminal->set_vertical_scroll_bar_hook = 0;
3069 terminal->condemn_scroll_bars_hook = 0;
3070 terminal->redeem_scroll_bar_hook = 0;
3071 terminal->judge_scroll_bars_hook = 0;
3072 terminal->read_socket_hook = 0;
3073 terminal->frame_up_to_date_hook = 0;
3075 /* Leave these two set, or suspended frames are not deleted
3076 correctly. */
3077 terminal->delete_frame_hook = &delete_tty_output;
3078 terminal->delete_terminal_hook = &delete_tty;
3081 /* Initialize hooks in TERMINAL with the values needed for a tty. */
3083 static void
3084 set_tty_hooks (struct terminal *terminal)
3086 terminal->rif = 0; /* ttys don't support window-based redisplay. */
3088 terminal->cursor_to_hook = &tty_cursor_to;
3089 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
3091 terminal->clear_to_end_hook = &tty_clear_to_end;
3092 terminal->clear_frame_hook = &tty_clear_frame;
3093 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
3095 terminal->ins_del_lines_hook = &tty_ins_del_lines;
3097 terminal->insert_glyphs_hook = &tty_insert_glyphs;
3098 terminal->write_glyphs_hook = &tty_write_glyphs;
3099 terminal->delete_glyphs_hook = &tty_delete_glyphs;
3101 terminal->ring_bell_hook = &tty_ring_bell;
3103 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
3104 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
3105 terminal->update_begin_hook = 0; /* Not needed. */
3106 terminal->update_end_hook = &tty_update_end;
3107 terminal->set_terminal_window_hook = &tty_set_terminal_window;
3109 terminal->mouse_position_hook = 0; /* Not needed. */
3110 terminal->frame_rehighlight_hook = 0; /* Not needed. */
3111 terminal->frame_raise_lower_hook = 0; /* Not needed. */
3113 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
3114 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
3115 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
3116 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
3118 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
3119 terminal->frame_up_to_date_hook = 0; /* Not needed. */
3121 terminal->delete_frame_hook = &delete_tty_output;
3122 terminal->delete_terminal_hook = &delete_tty;
3125 /* Drop the controlling terminal if fd is the same device. */
3126 static void
3127 dissociate_if_controlling_tty (int fd)
3129 #ifndef WINDOWSNT
3130 int pgid;
3131 EMACS_GET_TTY_PGRP (fd, &pgid); /* If tcgetpgrp succeeds, fd is the ctty. */
3132 if (pgid != -1)
3134 #if defined (USG) && !defined (BSD_PGRPS)
3135 setpgrp ();
3136 no_controlling_tty = 1;
3137 #elif defined (CYGWIN)
3138 setsid ();
3139 no_controlling_tty = 1;
3140 #else
3141 #ifdef TIOCNOTTY /* Try BSD ioctls. */
3142 sigblock (sigmask (SIGTTOU));
3143 fd = emacs_open (DEV_TTY, O_RDWR, 0);
3144 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
3146 no_controlling_tty = 1;
3148 if (fd != -1)
3149 emacs_close (fd);
3150 sigunblock (sigmask (SIGTTOU));
3151 #else
3152 /* Unknown system. */
3153 croak ();
3154 #endif /* ! TIOCNOTTY */
3155 #endif /* ! USG */
3157 #endif /* !WINDOWSNT */
3160 static void maybe_fatal();
3162 /* Create a termcap display on the tty device with the given name and
3163 type.
3165 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3166 Otherwise NAME should be a path to the tty device file,
3167 e.g. "/dev/pts/7".
3169 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3171 If MUST_SUCCEED is true, then all errors are fatal. */
3173 struct terminal *
3174 init_tty (char *name, char *terminal_type, int must_succeed)
3176 char *area = NULL;
3177 char **address = &area;
3178 char *buffer = NULL;
3179 int buffer_size = 4096;
3180 register char *p = NULL;
3181 int status;
3182 struct tty_display_info *tty = NULL;
3183 struct terminal *terminal = NULL;
3184 int ctty = 0; /* 1 if asked to open controlling tty. */
3186 if (!terminal_type)
3187 maybe_fatal (must_succeed, 0, 0,
3188 "Unknown terminal type",
3189 "Unknown terminal type");
3191 if (name == NULL)
3192 name = DEV_TTY;
3193 if (!strcmp (name, DEV_TTY))
3194 ctty = 1;
3196 /* If we already have a terminal on the given device, use that. If
3197 all such terminals are suspended, create a new one instead. */
3198 /* XXX Perhaps this should be made explicit by having init_tty
3199 always create a new terminal and separating terminal and frame
3200 creation on Lisp level. */
3201 terminal = get_named_tty (name);
3202 if (terminal)
3203 return terminal;
3205 terminal = create_terminal ();
3206 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
3207 bzero (tty, sizeof (struct tty_display_info));
3208 tty->next = tty_list;
3209 tty_list = tty;
3211 terminal->type = output_termcap;
3212 terminal->display_info.tty = tty;
3213 tty->terminal = terminal;
3215 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3216 Wcm_clear (tty);
3218 #ifndef WINDOWSNT
3219 set_tty_hooks (terminal);
3222 int fd;
3223 FILE *file;
3225 #ifdef O_IGNORE_CTTY
3226 if (!ctty)
3227 /* Open the terminal device. Don't recognize it as our
3228 controlling terminal, and don't make it the controlling tty
3229 if we don't have one at the moment. */
3230 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
3231 else
3232 #else
3233 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3234 defined on Hurd. On other systems, we need to explicitly
3235 dissociate ourselves from the controlling tty when we want to
3236 open a frame on the same terminal. */
3237 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
3238 #endif /* O_IGNORE_CTTY */
3240 if (fd < 0)
3241 maybe_fatal (must_succeed, buffer, terminal,
3242 "Could not open file: %s",
3243 "Could not open file: %s",
3244 name);
3245 if (!isatty (fd))
3247 close (fd);
3248 maybe_fatal (must_succeed, buffer, terminal,
3249 "Not a tty device: %s",
3250 "Not a tty device: %s",
3251 name);
3254 #ifndef O_IGNORE_CTTY
3255 if (!ctty)
3256 dissociate_if_controlling_tty (fd);
3257 #endif
3259 file = fdopen (fd, "w+");
3260 tty->name = xstrdup (name);
3261 terminal->name = xstrdup (name);
3262 tty->input = file;
3263 tty->output = file;
3266 tty->type = xstrdup (terminal_type);
3268 add_keyboard_wait_descriptor (fileno (tty->input));
3270 #endif
3272 encode_terminal_bufsize = 0;
3274 #ifdef HAVE_GPM
3275 terminal->mouse_position_hook = term_mouse_position;
3276 mouse_face_window = Qnil;
3277 #endif
3279 #ifdef WINDOWSNT
3280 initialize_w32_display (terminal);
3281 /* The following two are inaccessible from w32console.c. */
3282 terminal->delete_frame_hook = &delete_tty_output;
3283 terminal->delete_terminal_hook = &delete_tty;
3285 tty->name = xstrdup (name);
3286 terminal->name = xstrdup (name);
3287 tty->type = xstrdup (terminal_type);
3289 tty->output = stdout;
3290 tty->input = stdin;
3291 add_keyboard_wait_descriptor (0);
3293 Wcm_clear (tty);
3296 struct frame *f = XFRAME (selected_frame);
3298 FrameRows (tty) = FRAME_LINES (f);
3299 FrameCols (tty) = FRAME_COLS (f);
3300 tty->specified_window = FRAME_LINES (f);
3302 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3303 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
3305 tty->delete_in_insert_mode = 1;
3307 UseTabs (tty) = 0;
3308 terminal->scroll_region_ok = 0;
3310 /* Seems to insert lines when it's not supposed to, messing up the
3311 display. In doing a trace, it didn't seem to be called much, so I
3312 don't think we're losing anything by turning it off. */
3313 terminal->line_ins_del_ok = 0;
3314 terminal->char_ins_del_ok = 1;
3316 baud_rate = 19200;
3318 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
3320 #else /* not WINDOWSNT */
3322 Wcm_clear (tty);
3324 buffer = (char *) xmalloc (buffer_size);
3326 /* On some systems, tgetent tries to access the controlling
3327 terminal. */
3328 sigblock (sigmask (SIGTTOU));
3329 status = tgetent (buffer, terminal_type);
3330 sigunblock (sigmask (SIGTTOU));
3332 if (status < 0)
3334 #ifdef TERMINFO
3335 maybe_fatal (must_succeed, buffer, terminal,
3336 "Cannot open terminfo database file",
3337 "Cannot open terminfo database file");
3338 #else
3339 maybe_fatal (must_succeed, buffer, terminal,
3340 "Cannot open termcap database file",
3341 "Cannot open termcap database file");
3342 #endif
3344 if (status == 0)
3346 #ifdef TERMINFO
3347 maybe_fatal (must_succeed, buffer, terminal,
3348 "Terminal type %s is not defined",
3349 "Terminal type %s is not defined.\n\
3350 If that is not the actual type of terminal you have,\n\
3351 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3352 `setenv TERM ...') to specify the correct type. It may be necessary\n\
3353 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3354 terminal_type);
3355 #else
3356 maybe_fatal (must_succeed, buffer, terminal,
3357 "Terminal type %s is not defined",
3358 "Terminal type %s is not defined.\n\
3359 If that is not the actual type of terminal you have,\n\
3360 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3361 `setenv TERM ...') to specify the correct type. It may be necessary\n\
3362 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3363 terminal_type);
3364 #endif
3367 #ifndef TERMINFO
3368 if (strlen (buffer) >= buffer_size)
3369 abort ();
3370 buffer_size = strlen (buffer);
3371 #endif
3372 area = (char *) xmalloc (buffer_size);
3374 tty->TS_ins_line = tgetstr ("al", address);
3375 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3376 tty->TS_bell = tgetstr ("bl", address);
3377 BackTab (tty) = tgetstr ("bt", address);
3378 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3379 tty->TS_clr_line = tgetstr ("ce", address);
3380 tty->TS_clr_frame = tgetstr ("cl", address);
3381 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3382 AbsPosition (tty) = tgetstr ("cm", address);
3383 CR (tty) = tgetstr ("cr", address);
3384 tty->TS_set_scroll_region = tgetstr ("cs", address);
3385 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
3386 RowPosition (tty) = tgetstr ("cv", address);
3387 tty->TS_del_char = tgetstr ("dc", address);
3388 tty->TS_del_multi_chars = tgetstr ("DC", address);
3389 tty->TS_del_line = tgetstr ("dl", address);
3390 tty->TS_del_multi_lines = tgetstr ("DL", address);
3391 tty->TS_delete_mode = tgetstr ("dm", address);
3392 tty->TS_end_delete_mode = tgetstr ("ed", address);
3393 tty->TS_end_insert_mode = tgetstr ("ei", address);
3394 Home (tty) = tgetstr ("ho", address);
3395 tty->TS_ins_char = tgetstr ("ic", address);
3396 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3397 tty->TS_insert_mode = tgetstr ("im", address);
3398 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3399 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3400 tty->TS_keypad_mode = tgetstr ("ks", address);
3401 LastLine (tty) = tgetstr ("ll", address);
3402 Right (tty) = tgetstr ("nd", address);
3403 Down (tty) = tgetstr ("do", address);
3404 if (!Down (tty))
3405 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
3406 #ifdef VMS
3407 /* VMS puts a carriage return before each linefeed,
3408 so it is not safe to use linefeeds. */
3409 if (Down (tty) && Down (tty)[0] == '\n' && Down (tty)[1] == '\0')
3410 Down (tty) = 0;
3411 #endif /* VMS */
3412 if (tgetflag ("bs"))
3413 Left (tty) = "\b"; /* can't possibly be longer! */
3414 else /* (Actually, "bs" is obsolete...) */
3415 Left (tty) = tgetstr ("le", address);
3416 if (!Left (tty))
3417 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
3418 tty->TS_pad_char = tgetstr ("pc", address);
3419 tty->TS_repeat = tgetstr ("rp", address);
3420 tty->TS_end_standout_mode = tgetstr ("se", address);
3421 tty->TS_fwd_scroll = tgetstr ("sf", address);
3422 tty->TS_standout_mode = tgetstr ("so", address);
3423 tty->TS_rev_scroll = tgetstr ("sr", address);
3424 tty->Wcm->cm_tab = tgetstr ("ta", address);
3425 tty->TS_end_termcap_modes = tgetstr ("te", address);
3426 tty->TS_termcap_modes = tgetstr ("ti", address);
3427 Up (tty) = tgetstr ("up", address);
3428 tty->TS_visible_bell = tgetstr ("vb", address);
3429 tty->TS_cursor_normal = tgetstr ("ve", address);
3430 tty->TS_cursor_visible = tgetstr ("vs", address);
3431 tty->TS_cursor_invisible = tgetstr ("vi", address);
3432 tty->TS_set_window = tgetstr ("wi", address);
3434 tty->TS_enter_underline_mode = tgetstr ("us", address);
3435 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3436 tty->TS_enter_bold_mode = tgetstr ("md", address);
3437 tty->TS_enter_dim_mode = tgetstr ("mh", address);
3438 tty->TS_enter_blink_mode = tgetstr ("mb", address);
3439 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3440 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3441 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3442 tty->TS_exit_attribute_mode = tgetstr ("me", address);
3444 MultiUp (tty) = tgetstr ("UP", address);
3445 MultiDown (tty) = tgetstr ("DO", address);
3446 MultiLeft (tty) = tgetstr ("LE", address);
3447 MultiRight (tty) = tgetstr ("RI", address);
3449 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
3450 color because we can't switch back to the default foreground and
3451 background. */
3452 tty->TS_orig_pair = tgetstr ("op", address);
3453 if (tty->TS_orig_pair)
3455 tty->TS_set_foreground = tgetstr ("AF", address);
3456 tty->TS_set_background = tgetstr ("AB", address);
3457 if (!tty->TS_set_foreground)
3459 /* SVr4. */
3460 tty->TS_set_foreground = tgetstr ("Sf", address);
3461 tty->TS_set_background = tgetstr ("Sb", address);
3464 tty->TN_max_colors = tgetnum ("Co");
3465 tty->TN_max_pairs = tgetnum ("pa");
3467 tty->TN_no_color_video = tgetnum ("NC");
3468 if (tty->TN_no_color_video == -1)
3469 tty->TN_no_color_video = 0;
3472 tty_default_color_capabilities (tty, 1);
3474 MagicWrap (tty) = tgetflag ("xn");
3475 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3476 the former flag imply the latter. */
3477 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
3478 terminal->memory_below_frame = tgetflag ("db");
3479 tty->TF_hazeltine = tgetflag ("hz");
3480 terminal->must_write_spaces = tgetflag ("in");
3481 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3482 tty->TF_insmode_motion = tgetflag ("mi");
3483 tty->TF_standout_motion = tgetflag ("ms");
3484 tty->TF_underscore = tgetflag ("ul");
3485 tty->TF_teleray = tgetflag ("xt");
3487 #endif /* !WINDOWSNT */
3488 #ifdef MULTI_KBOARD
3489 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3490 init_kboard (terminal->kboard);
3491 terminal->kboard->Vwindow_system = Qnil;
3492 terminal->kboard->next_kboard = all_kboards;
3493 all_kboards = terminal->kboard;
3494 terminal->kboard->reference_count++;
3495 /* Don't let the initial kboard remain current longer than necessary.
3496 That would cause problems if a file loaded on startup tries to
3497 prompt in the mini-buffer. */
3498 if (current_kboard == initial_kboard)
3499 current_kboard = terminal->kboard;
3500 #ifndef WINDOWSNT
3501 term_get_fkeys (address, terminal->kboard);
3502 #endif
3503 #endif
3505 #ifndef WINDOWSNT
3506 /* Get frame size from system, or else from termcap. */
3508 int height, width;
3509 get_tty_size (fileno (tty->input), &width, &height);
3510 FrameCols (tty) = width;
3511 FrameRows (tty) = height;
3514 if (FrameCols (tty) <= 0)
3515 FrameCols (tty) = tgetnum ("co");
3516 if (FrameRows (tty) <= 0)
3517 FrameRows (tty) = tgetnum ("li");
3519 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
3520 maybe_fatal (must_succeed, NULL, terminal,
3521 "Screen size %dx%d is too small"
3522 "Screen size %dx%d is too small",
3523 FrameCols (tty), FrameRows (tty));
3525 #if 0 /* This is not used anywhere. */
3526 tty->terminal->min_padding_speed = tgetnum ("pb");
3527 #endif
3529 TabWidth (tty) = tgetnum ("tw");
3531 #ifdef VMS
3532 /* These capabilities commonly use ^J.
3533 I don't know why, but sending them on VMS does not work;
3534 it causes following spaces to be lost, sometimes.
3535 For now, the simplest fix is to avoid using these capabilities ever. */
3536 if (Down (tty) && Down (tty)[0] == '\n')
3537 Down (tty) = 0;
3538 #endif /* VMS */
3540 if (!tty->TS_bell)
3541 tty->TS_bell = "\07";
3543 if (!tty->TS_fwd_scroll)
3544 tty->TS_fwd_scroll = Down (tty);
3546 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
3548 if (TabWidth (tty) < 0)
3549 TabWidth (tty) = 8;
3551 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3552 and newer termcap doc does not seem to say there is a default.
3553 if (!tty->Wcm->cm_tab)
3554 tty->Wcm->cm_tab = "\t";
3557 /* We don't support standout modes that use `magic cookies', so
3558 turn off any that do. */
3559 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
3561 tty->TS_standout_mode = 0;
3562 tty->TS_end_standout_mode = 0;
3564 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
3566 tty->TS_enter_underline_mode = 0;
3567 tty->TS_exit_underline_mode = 0;
3570 /* If there's no standout mode, try to use underlining instead. */
3571 if (tty->TS_standout_mode == 0)
3573 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3574 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
3577 /* If no `se' string, try using a `me' string instead.
3578 If that fails, we can't use standout mode at all. */
3579 if (tty->TS_end_standout_mode == 0)
3581 char *s = tgetstr ("me", address);
3582 if (s != 0)
3583 tty->TS_end_standout_mode = s;
3584 else
3585 tty->TS_standout_mode = 0;
3588 if (tty->TF_teleray)
3590 tty->Wcm->cm_tab = 0;
3591 /* We can't support standout mode, because it uses magic cookies. */
3592 tty->TS_standout_mode = 0;
3593 /* But that means we cannot rely on ^M to go to column zero! */
3594 CR (tty) = 0;
3595 /* LF can't be trusted either -- can alter hpos */
3596 /* if move at column 0 thru a line with TS_standout_mode */
3597 Down (tty) = 0;
3600 /* Special handling for certain terminal types known to need it */
3602 if (!strcmp (terminal_type, "supdup"))
3604 terminal->memory_below_frame = 1;
3605 tty->Wcm->cm_losewrap = 1;
3607 if (!strncmp (terminal_type, "c10", 3)
3608 || !strcmp (terminal_type, "perq"))
3610 /* Supply a makeshift :wi string.
3611 This string is not valid in general since it works only
3612 for windows starting at the upper left corner;
3613 but that is all Emacs uses.
3615 This string works only if the frame is using
3616 the top of the video memory, because addressing is memory-relative.
3617 So first check the :ti string to see if that is true.
3619 It would be simpler if the :wi string could go in the termcap
3620 entry, but it can't because it is not fully valid.
3621 If it were in the termcap entry, it would confuse other programs. */
3622 if (!tty->TS_set_window)
3624 p = tty->TS_termcap_modes;
3625 while (*p && strcmp (p, "\033v "))
3626 p++;
3627 if (*p)
3628 tty->TS_set_window = "\033v%C %C %C %C ";
3630 /* Termcap entry often fails to have :in: flag */
3631 terminal->must_write_spaces = 1;
3632 /* :ti string typically fails to have \E^G! in it */
3633 /* This limits scope of insert-char to one line. */
3634 strcpy (area, tty->TS_termcap_modes);
3635 strcat (area, "\033\007!");
3636 tty->TS_termcap_modes = area;
3637 area += strlen (area) + 1;
3638 p = AbsPosition (tty);
3639 /* Change all %+ parameters to %C, to handle
3640 values above 96 correctly for the C100. */
3641 while (*p)
3643 if (p[0] == '%' && p[1] == '+')
3644 p[1] = 'C';
3645 p++;
3649 tty->specified_window = FrameRows (tty);
3651 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3653 maybe_fatal (must_succeed, NULL, terminal,
3654 "Terminal type \"%s\" is not powerful enough to run Emacs",
3655 #ifdef VMS
3656 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3657 It lacks the ability to position the cursor.\n\
3658 If that is not the actual type of terminal you have, use either the\n\
3659 DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
3660 or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
3661 #else /* not VMS */
3662 # ifdef TERMINFO
3663 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3664 It lacks the ability to position the cursor.\n\
3665 If that is not the actual type of terminal you have,\n\
3666 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3667 `setenv TERM ...') to specify the correct type. It may be necessary\n\
3668 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3669 # else /* TERMCAP */
3670 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3671 It lacks the ability to position the cursor.\n\
3672 If that is not the actual type of terminal you have,\n\
3673 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3674 `setenv TERM ...') to specify the correct type. It may be necessary\n\
3675 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3676 # endif /* TERMINFO */
3677 #endif /*VMS */
3678 terminal_type);
3681 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
3682 maybe_fatal (must_succeed, NULL, terminal,
3683 "Could not determine the frame size",
3684 "Could not determine the frame size");
3686 tty->delete_in_insert_mode
3687 = tty->TS_delete_mode && tty->TS_insert_mode
3688 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
3690 tty->se_is_so = (tty->TS_standout_mode
3691 && tty->TS_end_standout_mode
3692 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
3694 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
3696 terminal->scroll_region_ok
3697 = (tty->Wcm->cm_abs
3698 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
3700 terminal->line_ins_del_ok
3701 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3702 && (tty->TS_del_line || tty->TS_del_multi_lines))
3703 || (terminal->scroll_region_ok
3704 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
3706 terminal->char_ins_del_ok
3707 = ((tty->TS_ins_char || tty->TS_insert_mode
3708 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3709 && (tty->TS_del_char || tty->TS_del_multi_chars));
3711 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
3713 init_baud_rate (fileno (tty->input));
3715 #ifdef AIXHFT
3716 /* The HFT system on AIX doesn't optimize for scrolling, so it's
3717 really ugly at times. */
3718 terminal->line_ins_del_ok = 0;
3719 terminal->char_ins_del_ok = 0;
3720 #endif
3722 /* Don't do this. I think termcap may still need the buffer. */
3723 /* xfree (buffer); */
3725 #endif /* not WINDOWSNT */
3727 /* Init system terminal modes (RAW or CBREAK, etc.). */
3728 init_sys_modes (tty);
3730 return terminal;
3733 /* Auxiliary error-handling function for init_tty.
3734 Free BUFFER and delete TERMINAL, then call error or fatal
3735 with str1 or str2, respectively, according to MUST_SUCCEED. */
3737 static void
3738 maybe_fatal (must_succeed, buffer, terminal, str1, str2, arg1, arg2)
3739 int must_succeed;
3740 char *buffer;
3741 struct terminal *terminal;
3742 char *str1, *str2, *arg1, *arg2;
3744 if (buffer)
3745 xfree (buffer);
3747 if (terminal)
3748 delete_tty (terminal);
3750 if (must_succeed)
3751 fatal (str2, arg1, arg2);
3752 else
3753 error (str1, arg1, arg2);
3755 abort ();
3758 void
3759 fatal (const char *str, ...)
3761 va_list ap;
3762 va_start (ap, str);
3763 fprintf (stderr, "emacs: ");
3764 vfprintf (stderr, str, ap);
3765 va_end (ap);
3766 fflush (stderr);
3767 exit (1);
3772 /* Delete the given tty terminal, closing all frames on it. */
3774 static void
3775 delete_tty (struct terminal *terminal)
3777 struct tty_display_info *tty;
3778 Lisp_Object tail, frame;
3779 int last_terminal;
3781 /* Protect against recursive calls. Fdelete_frame in
3782 delete_terminal calls us back when it deletes our last frame. */
3783 if (!terminal->name)
3784 return;
3786 if (terminal->type != output_termcap)
3787 abort ();
3789 tty = terminal->display_info.tty;
3791 last_terminal = 1;
3792 FOR_EACH_FRAME (tail, frame)
3794 struct frame *f = XFRAME (frame);
3795 if (FRAME_LIVE_P (f) && (!FRAME_TERMCAP_P (f) || FRAME_TTY (f) != tty))
3797 last_terminal = 0;
3798 break;
3801 if (last_terminal)
3802 error ("Attempt to delete the sole terminal device with live frames");
3804 if (tty == tty_list)
3805 tty_list = tty->next;
3806 else
3808 struct tty_display_info *p;
3809 for (p = tty_list; p && p->next != tty; p = p->next)
3812 if (! p)
3813 /* This should not happen. */
3814 abort ();
3816 p->next = tty->next;
3817 tty->next = 0;
3820 /* reset_sys_modes needs a valid device, so this call needs to be
3821 before delete_terminal. */
3822 reset_sys_modes (tty);
3824 delete_terminal (terminal);
3826 if (tty->name)
3827 xfree (tty->name);
3829 if (tty->type)
3830 xfree (tty->type);
3832 if (tty->input)
3834 delete_keyboard_wait_descriptor (fileno (tty->input));
3835 if (tty->input != stdin)
3836 fclose (tty->input);
3838 if (tty->output && tty->output != stdout && tty->output != tty->input)
3839 fclose (tty->output);
3840 if (tty->termscript)
3841 fclose (tty->termscript);
3843 if (tty->old_tty)
3844 xfree (tty->old_tty);
3846 if (tty->Wcm)
3847 xfree (tty->Wcm);
3849 bzero (tty, sizeof (struct tty_display_info));
3850 xfree (tty);
3855 /* Mark the pointers in the tty_display_info objects.
3856 Called by the Fgarbage_collector. */
3858 void
3859 mark_ttys (void)
3861 struct tty_display_info *tty;
3863 for (tty = tty_list; tty; tty = tty->next)
3864 mark_object (tty->top_frame);
3869 void
3870 syms_of_term ()
3872 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
3873 doc: /* Non-nil means the system uses terminfo rather than termcap.
3874 This variable can be used by terminal emulator packages. */);
3875 #ifdef TERMINFO
3876 system_uses_terminfo = 1;
3877 #else
3878 system_uses_terminfo = 0;
3879 #endif
3881 DEFVAR_LISP ("suspend-tty-functions", &Vsuspend_tty_functions,
3882 doc: /* Functions to be run after suspending a tty.
3883 The functions are run with one argument, the terminal id to be suspended.
3884 See `suspend-tty'. */);
3885 Vsuspend_tty_functions = Qnil;
3888 DEFVAR_LISP ("resume-tty-functions", &Vresume_tty_functions,
3889 doc: /* Functions to be run after resuming a tty.
3890 The functions are run with one argument, the terminal id that was revived.
3891 See `resume-tty'. */);
3892 Vresume_tty_functions = Qnil;
3894 DEFVAR_BOOL ("visible-cursor", &visible_cursor,
3895 doc: /* Non-nil means to make the cursor very visible.
3896 This only has an effect when running in a text terminal.
3897 What means \"very visible\" is up to your terminal. It may make the cursor
3898 bigger, or it may make it blink, or it may do nothing at all. */);
3899 visible_cursor = 1;
3901 defsubr (&Stty_display_color_p);
3902 defsubr (&Stty_display_color_cells);
3903 defsubr (&Stty_no_underline);
3904 defsubr (&Stty_type);
3905 defsubr (&Scontrolling_tty_p);
3906 defsubr (&Ssuspend_tty);
3907 defsubr (&Sresume_tty);
3908 #ifdef HAVE_GPM
3909 defsubr (&Sgpm_mouse_start);
3910 defsubr (&Sgpm_mouse_stop);
3912 staticpro (&mouse_face_window);
3913 #endif /* HAVE_GPM */
3918 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
3919 (do not change this comment) */