* doc/misc/url.texi (Disk Caching): Tweak previous change.
[emacs.git] / src / term.c
blob2deca1014e80e3115fbfce09308223a8de783d1a
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1998, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
23 #include <config.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/file.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
36 #endif
38 #include <signal.h>
39 #include <stdarg.h>
40 #include <setjmp.h>
42 #include "lisp.h"
43 #include "termchar.h"
44 #include "termopts.h"
45 #include "buffer.h"
46 #include "character.h"
47 #include "charset.h"
48 #include "coding.h"
49 #include "composite.h"
50 #include "keyboard.h"
51 #include "frame.h"
52 #include "disptab.h"
53 #include "termhooks.h"
54 #include "dispextern.h"
55 #include "window.h"
56 #include "keymap.h"
57 #include "blockinput.h"
58 #include "syssignal.h"
59 #include "systty.h"
60 #include "intervals.h"
61 #ifdef MSDOS
62 #include "msdos.h"
63 static int been_here = -1;
64 #endif
66 /* For now, don't try to include termcap.h. On some systems,
67 configure finds a non-standard termcap.h that the main build
68 won't find. */
69 extern void tputs (const char *, int, int (*)(int));
70 extern int tgetent (char *, const char *);
71 extern int tgetflag (char *id);
72 extern int tgetnum (char *id);
74 #include "cm.h"
75 #ifdef HAVE_X_WINDOWS
76 #include "xterm.h"
77 #endif
79 #ifndef O_RDWR
80 #define O_RDWR 2
81 #endif
83 #ifndef O_NOCTTY
84 #define O_NOCTTY 0
85 #endif
87 /* The name of the default console device. */
88 #ifdef WINDOWSNT
89 #define DEV_TTY "CONOUT$"
90 #else
91 #define DEV_TTY "/dev/tty"
92 #endif
94 static void tty_set_scroll_region (struct frame *f, int start, int stop);
95 static void turn_on_face (struct frame *, int face_id);
96 static void turn_off_face (struct frame *, int face_id);
97 static void tty_show_cursor (struct tty_display_info *);
98 static void tty_hide_cursor (struct tty_display_info *);
99 static void tty_background_highlight (struct tty_display_info *tty);
100 static void clear_tty_hooks (struct terminal *terminal);
101 static void set_tty_hooks (struct terminal *terminal);
102 static void dissociate_if_controlling_tty (int fd);
103 static void delete_tty (struct terminal *);
104 static void maybe_fatal (int must_succeed, struct terminal *terminal,
105 const char *str1, const char *str2, ...) NO_RETURN;
106 static void vfatal (const char *str, va_list ap) NO_RETURN;
109 #define OUTPUT(tty, a) \
110 emacs_tputs ((tty), a, \
111 (int) (FRAME_LINES (XFRAME (selected_frame)) \
112 - curY (tty)), \
113 cmputc)
115 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
116 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
118 #define OUTPUT_IF(tty, a) \
119 do { \
120 if (a) \
121 emacs_tputs ((tty), a, \
122 (int) (FRAME_LINES (XFRAME (selected_frame)) \
123 - curY (tty) ), \
124 cmputc); \
125 } while (0)
127 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
129 /* If true, use "vs", otherwise use "ve" to make the cursor visible. */
131 static int visible_cursor;
133 /* Display space properties */
135 /* Functions to call after suspending a tty. */
136 Lisp_Object Vsuspend_tty_functions;
138 /* Functions to call after resuming a tty. */
139 Lisp_Object Vresume_tty_functions;
141 /* Chain of all tty device parameters. */
142 struct tty_display_info *tty_list;
144 /* Nonzero means no need to redraw the entire frame on resuming a
145 suspended Emacs. This is useful on terminals with multiple
146 pages, where one page is used for Emacs and another for all
147 else. */
148 int no_redraw_on_reenter;
150 /* Meaning of bits in no_color_video. Each bit set means that the
151 corresponding attribute cannot be combined with colors. */
153 enum no_color_bit
155 NC_STANDOUT = 1 << 0,
156 NC_UNDERLINE = 1 << 1,
157 NC_REVERSE = 1 << 2,
158 NC_BLINK = 1 << 3,
159 NC_DIM = 1 << 4,
160 NC_BOLD = 1 << 5,
161 NC_INVIS = 1 << 6,
162 NC_PROTECT = 1 << 7,
163 NC_ALT_CHARSET = 1 << 8
166 /* internal state */
168 /* The largest frame width in any call to calculate_costs. */
170 int max_frame_cols;
172 /* The largest frame height in any call to calculate_costs. */
174 int max_frame_lines;
176 /* Non-zero if we have dropped our controlling tty and therefore
177 should not open a frame on stdout. */
178 static int no_controlling_tty;
180 /* Provided for lisp packages. */
182 static int system_uses_terminfo;
184 char *tparam (char *, char *, int, int, ...);
186 extern char *tgetstr (char *, char **);
189 #ifdef HAVE_GPM
190 #include <sys/fcntl.h>
192 static void term_clear_mouse_face (void);
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, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
251 OUTPUT_IF (tty, tty->TS_keypad_mode);
252 losecursor (tty);
253 fflush (tty->output);
257 /* Reset termcap modes before exiting Emacs. */
259 void
260 tty_reset_terminal_modes (struct terminal *terminal)
262 struct tty_display_info *tty = terminal->display_info.tty;
264 if (tty->output)
266 tty_turn_off_highlight (tty);
267 tty_turn_off_insert (tty);
268 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
269 OUTPUT_IF (tty, tty->TS_cursor_normal);
270 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
271 OUTPUT_IF (tty, tty->TS_orig_pair);
272 /* Output raw CR so kernel can track the cursor hpos. */
273 current_tty = tty;
274 cmputc ('\r');
275 fflush (tty->output);
279 /* Flag the end of a display update on a termcap terminal. */
281 static void
282 tty_update_end (struct frame *f)
284 struct tty_display_info *tty = FRAME_TTY (f);
286 if (!XWINDOW (selected_window)->cursor_off_p)
287 tty_show_cursor (tty);
288 tty_turn_off_insert (tty);
289 tty_background_highlight (tty);
292 /* The implementation of set_terminal_window for termcap frames. */
294 static void
295 tty_set_terminal_window (struct frame *f, int size)
297 struct tty_display_info *tty = FRAME_TTY (f);
299 tty->specified_window = size ? size : FRAME_LINES (f);
300 if (FRAME_SCROLL_REGION_OK (f))
301 tty_set_scroll_region (f, 0, tty->specified_window);
304 static void
305 tty_set_scroll_region (struct frame *f, int start, int stop)
307 char *buf;
308 struct tty_display_info *tty = FRAME_TTY (f);
310 if (tty->TS_set_scroll_region)
311 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1);
312 else if (tty->TS_set_scroll_region_1)
313 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
314 FRAME_LINES (f), start,
315 FRAME_LINES (f) - stop,
316 FRAME_LINES (f));
317 else
318 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
320 OUTPUT (tty, buf);
321 xfree (buf);
322 losecursor (tty);
326 static void
327 tty_turn_on_insert (struct tty_display_info *tty)
329 if (!tty->insert_mode)
330 OUTPUT (tty, tty->TS_insert_mode);
331 tty->insert_mode = 1;
334 void
335 tty_turn_off_insert (struct tty_display_info *tty)
337 if (tty->insert_mode)
338 OUTPUT (tty, tty->TS_end_insert_mode);
339 tty->insert_mode = 0;
342 /* Handle highlighting. */
344 void
345 tty_turn_off_highlight (struct tty_display_info *tty)
347 if (tty->standout_mode)
348 OUTPUT_IF (tty, tty->TS_end_standout_mode);
349 tty->standout_mode = 0;
352 static void
353 tty_turn_on_highlight (struct tty_display_info *tty)
355 if (!tty->standout_mode)
356 OUTPUT_IF (tty, tty->TS_standout_mode);
357 tty->standout_mode = 1;
360 static void
361 tty_toggle_highlight (struct tty_display_info *tty)
363 if (tty->standout_mode)
364 tty_turn_off_highlight (tty);
365 else
366 tty_turn_on_highlight (tty);
370 /* Make cursor invisible. */
372 static void
373 tty_hide_cursor (struct tty_display_info *tty)
375 if (tty->cursor_hidden == 0)
377 tty->cursor_hidden = 1;
378 OUTPUT_IF (tty, tty->TS_cursor_invisible);
383 /* Ensure that cursor is visible. */
385 static void
386 tty_show_cursor (struct tty_display_info *tty)
388 if (tty->cursor_hidden)
390 tty->cursor_hidden = 0;
391 OUTPUT_IF (tty, tty->TS_cursor_normal);
392 if (visible_cursor)
393 OUTPUT_IF (tty, tty->TS_cursor_visible);
398 /* Set standout mode to the state it should be in for
399 empty space inside windows. What this is,
400 depends on the user option inverse-video. */
402 static void
403 tty_background_highlight (struct tty_display_info *tty)
405 if (inverse_video)
406 tty_turn_on_highlight (tty);
407 else
408 tty_turn_off_highlight (tty);
411 /* Set standout mode to the mode specified for the text to be output. */
413 static void
414 tty_highlight_if_desired (struct tty_display_info *tty)
416 if (inverse_video)
417 tty_turn_on_highlight (tty);
418 else
419 tty_turn_off_highlight (tty);
423 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
424 frame-relative coordinates. */
426 static void
427 tty_cursor_to (struct frame *f, int vpos, int hpos)
429 struct tty_display_info *tty = FRAME_TTY (f);
431 /* Detect the case where we are called from reset_sys_modes
432 and the costs have never been calculated. Do nothing. */
433 if (! tty->costs_set)
434 return;
436 if (curY (tty) == vpos
437 && curX (tty) == hpos)
438 return;
439 if (!tty->TF_standout_motion)
440 tty_background_highlight (tty);
441 if (!tty->TF_insmode_motion)
442 tty_turn_off_insert (tty);
443 cmgoto (tty, vpos, hpos);
446 /* Similar but don't take any account of the wasted characters. */
448 static void
449 tty_raw_cursor_to (struct frame *f, int row, int col)
451 struct tty_display_info *tty = FRAME_TTY (f);
453 if (curY (tty) == row
454 && curX (tty) == col)
455 return;
456 if (!tty->TF_standout_motion)
457 tty_background_highlight (tty);
458 if (!tty->TF_insmode_motion)
459 tty_turn_off_insert (tty);
460 cmgoto (tty, row, col);
463 /* Erase operations */
465 /* Clear from cursor to end of frame on a termcap device. */
467 static void
468 tty_clear_to_end (struct frame *f)
470 register int i;
471 struct tty_display_info *tty = FRAME_TTY (f);
473 if (tty->TS_clr_to_bottom)
475 tty_background_highlight (tty);
476 OUTPUT (tty, tty->TS_clr_to_bottom);
478 else
480 for (i = curY (tty); i < FRAME_LINES (f); i++)
482 cursor_to (f, i, 0);
483 clear_end_of_line (f, FRAME_COLS (f));
488 /* Clear an entire termcap frame. */
490 static void
491 tty_clear_frame (struct frame *f)
493 struct tty_display_info *tty = FRAME_TTY (f);
495 if (tty->TS_clr_frame)
497 tty_background_highlight (tty);
498 OUTPUT (tty, tty->TS_clr_frame);
499 cmat (tty, 0, 0);
501 else
503 cursor_to (f, 0, 0);
504 clear_to_end (f);
508 /* An implementation of clear_end_of_line for termcap frames.
510 Note that the cursor may be moved, on terminals lacking a `ce' string. */
512 static void
513 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
515 register int i;
516 struct tty_display_info *tty = FRAME_TTY (f);
518 /* Detect the case where we are called from reset_sys_modes
519 and the costs have never been calculated. Do nothing. */
520 if (! tty->costs_set)
521 return;
523 if (curX (tty) >= first_unused_hpos)
524 return;
525 tty_background_highlight (tty);
526 if (tty->TS_clr_line)
528 OUTPUT1 (tty, tty->TS_clr_line);
530 else
531 { /* have to do it the hard way */
532 tty_turn_off_insert (tty);
534 /* Do not write in last row last col with Auto-wrap on. */
535 if (AutoWrap (tty)
536 && curY (tty) == FrameRows (tty) - 1
537 && first_unused_hpos == FrameCols (tty))
538 first_unused_hpos--;
540 for (i = curX (tty); i < first_unused_hpos; i++)
542 if (tty->termscript)
543 fputc (' ', tty->termscript);
544 fputc (' ', tty->output);
546 cmplus (tty, first_unused_hpos - curX (tty));
550 /* Buffers to store the source and result of code conversion for terminal. */
551 static unsigned char *encode_terminal_src;
552 static unsigned char *encode_terminal_dst;
553 /* Allocated sizes of the above buffers. */
554 static int encode_terminal_src_size;
555 static int encode_terminal_dst_size;
557 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
558 Set CODING->produced to the byte-length of the resulting byte
559 sequence, and return a pointer to that byte sequence. */
561 unsigned char *
562 encode_terminal_code (struct glyph *src, int src_len, struct coding_system *coding)
564 struct glyph *src_end = src + src_len;
565 unsigned char *buf;
566 int nchars, nbytes, required;
567 register int tlen = GLYPH_TABLE_LENGTH;
568 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
569 Lisp_Object charset_list;
571 /* Allocate sufficient size of buffer to store all characters in
572 multibyte-form. But, it may be enlarged on demand if
573 Vglyph_table contains a string or a composite glyph is
574 encountered. */
575 required = MAX_MULTIBYTE_LENGTH * src_len;
576 if (encode_terminal_src_size < required)
578 if (encode_terminal_src)
579 encode_terminal_src = xrealloc (encode_terminal_src, required);
580 else
581 encode_terminal_src = xmalloc (required);
582 encode_terminal_src_size = required;
585 charset_list = coding_charset_list (coding);
587 buf = encode_terminal_src;
588 nchars = 0;
589 while (src < src_end)
591 if (src->type == COMPOSITE_GLYPH)
593 struct composition *cmp;
594 Lisp_Object gstring;
595 int i;
597 nbytes = buf - encode_terminal_src;
598 if (src->u.cmp.automatic)
600 gstring = composition_gstring_from_id (src->u.cmp.id);
601 required = src->u.cmp.to + 1 - src->u.cmp.from;
603 else
605 cmp = composition_table[src->u.cmp.id];
606 required = MAX_MULTIBYTE_LENGTH * cmp->glyph_len;
609 if (encode_terminal_src_size < nbytes + required)
611 encode_terminal_src_size = nbytes + required;
612 encode_terminal_src = xrealloc (encode_terminal_src,
613 encode_terminal_src_size);
614 buf = encode_terminal_src + nbytes;
617 if (src->u.cmp.automatic)
618 for (i = src->u.cmp.from; i <= src->u.cmp.to; i++)
620 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
621 int c = LGLYPH_CHAR (g);
623 if (! char_charset (c, charset_list, NULL))
624 c = '?';
625 buf += CHAR_STRING (c, buf);
626 nchars++;
628 else
629 for (i = 0; i < cmp->glyph_len; i++)
631 int c = COMPOSITION_GLYPH (cmp, i);
633 if (c == '\t')
634 continue;
635 if (char_charset (c, charset_list, NULL))
637 if (CHAR_WIDTH (c) == 0
638 && i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
639 /* Should be left-padded */
641 buf += CHAR_STRING (' ', buf);
642 nchars++;
645 else
646 c = '?';
647 buf += CHAR_STRING (c, buf);
648 nchars++;
651 /* We must skip glyphs to be padded for a wide character. */
652 else if (! CHAR_GLYPH_PADDING_P (*src))
654 GLYPH g;
655 int c;
656 Lisp_Object string;
658 string = Qnil;
659 SET_GLYPH_FROM_CHAR_GLYPH (g, src[0]);
661 if (GLYPH_INVALID_P (g) || GLYPH_SIMPLE_P (tbase, tlen, g))
663 /* This glyph doesn't have an entry in Vglyph_table. */
664 c = src->u.ch;
666 else
668 /* This glyph has an entry in Vglyph_table,
669 so process any alias before testing for simpleness. */
670 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
672 if (GLYPH_SIMPLE_P (tbase, tlen, g))
673 /* We set the multi-byte form of a character in G
674 (that should be an ASCII character) at WORKBUF. */
675 c = GLYPH_CHAR (g);
676 else
677 /* We have a string in Vglyph_table. */
678 string = tbase[GLYPH_CHAR (g)];
681 if (NILP (string))
683 nbytes = buf - encode_terminal_src;
684 if (encode_terminal_src_size < nbytes + MAX_MULTIBYTE_LENGTH)
686 encode_terminal_src_size = nbytes + MAX_MULTIBYTE_LENGTH;
687 encode_terminal_src = xrealloc (encode_terminal_src,
688 encode_terminal_src_size);
689 buf = encode_terminal_src + nbytes;
691 if (CHAR_BYTE8_P (c)
692 || char_charset (c, charset_list, NULL))
694 /* Store the multibyte form of C at BUF. */
695 buf += CHAR_STRING (c, buf);
696 nchars++;
698 else
700 /* C is not encodable. */
701 *buf++ = '?';
702 nchars++;
703 while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
705 *buf++ = '?';
706 nchars++;
707 src++;
711 else
713 unsigned char *p = SDATA (string);
715 if (! STRING_MULTIBYTE (string))
716 string = string_to_multibyte (string);
717 nbytes = buf - encode_terminal_src;
718 if (encode_terminal_src_size < nbytes + SBYTES (string))
720 encode_terminal_src_size = nbytes + SBYTES (string);
721 encode_terminal_src = xrealloc (encode_terminal_src,
722 encode_terminal_src_size);
723 buf = encode_terminal_src + nbytes;
725 memcpy (buf, SDATA (string), SBYTES (string));
726 buf += SBYTES (string);
727 nchars += SCHARS (string);
730 src++;
733 if (nchars == 0)
735 coding->produced = 0;
736 return NULL;
739 nbytes = buf - encode_terminal_src;
740 coding->source = encode_terminal_src;
741 if (encode_terminal_dst_size == 0)
743 encode_terminal_dst_size = encode_terminal_src_size;
744 if (encode_terminal_dst)
745 encode_terminal_dst = xrealloc (encode_terminal_dst,
746 encode_terminal_dst_size);
747 else
748 encode_terminal_dst = xmalloc (encode_terminal_dst_size);
750 coding->destination = encode_terminal_dst;
751 coding->dst_bytes = encode_terminal_dst_size;
752 encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
753 /* coding->destination may have been reallocated. */
754 encode_terminal_dst = coding->destination;
755 encode_terminal_dst_size = coding->dst_bytes;
757 return (encode_terminal_dst);
762 /* An implementation of write_glyphs for termcap frames. */
764 static void
765 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
767 unsigned char *conversion_buffer;
768 struct coding_system *coding;
770 struct tty_display_info *tty = FRAME_TTY (f);
772 tty_turn_off_insert (tty);
773 tty_hide_cursor (tty);
775 /* Don't dare write in last column of bottom line, if Auto-Wrap,
776 since that would scroll the whole frame on some terminals. */
778 if (AutoWrap (tty)
779 && curY (tty) + 1 == FRAME_LINES (f)
780 && (curX (tty) + len) == FRAME_COLS (f))
781 len --;
782 if (len <= 0)
783 return;
785 cmplus (tty, len);
787 /* If terminal_coding does any conversion, use it, otherwise use
788 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
789 because it always return 1 if the member src_multibyte is 1. */
790 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
791 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
792 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
793 the tail. */
794 coding->mode &= ~CODING_MODE_LAST_BLOCK;
796 while (len > 0)
798 /* Identify a run of glyphs with the same face. */
799 int face_id = string->face_id;
800 int n;
802 for (n = 1; n < len; ++n)
803 if (string[n].face_id != face_id)
804 break;
806 /* Turn appearance modes of the face of the run on. */
807 tty_highlight_if_desired (tty);
808 turn_on_face (f, face_id);
810 if (n == len)
811 /* This is the last run. */
812 coding->mode |= CODING_MODE_LAST_BLOCK;
813 conversion_buffer = encode_terminal_code (string, n, coding);
814 if (coding->produced > 0)
816 BLOCK_INPUT;
817 fwrite (conversion_buffer, 1, coding->produced, tty->output);
818 if (ferror (tty->output))
819 clearerr (tty->output);
820 if (tty->termscript)
821 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
822 UNBLOCK_INPUT;
824 len -= n;
825 string += n;
827 /* Turn appearance modes off. */
828 turn_off_face (f, face_id);
829 tty_turn_off_highlight (tty);
832 cmcheckmagic (tty);
835 #ifdef HAVE_GPM /* Only used by GPM code. */
837 static void
838 tty_write_glyphs_with_face (register struct frame *f, register struct glyph *string,
839 register int len, register int face_id)
841 unsigned char *conversion_buffer;
842 struct coding_system *coding;
844 struct tty_display_info *tty = FRAME_TTY (f);
846 tty_turn_off_insert (tty);
847 tty_hide_cursor (tty);
849 /* Don't dare write in last column of bottom line, if Auto-Wrap,
850 since that would scroll the whole frame on some terminals. */
852 if (AutoWrap (tty)
853 && curY (tty) + 1 == FRAME_LINES (f)
854 && (curX (tty) + len) == FRAME_COLS (f))
855 len --;
856 if (len <= 0)
857 return;
859 cmplus (tty, len);
861 /* If terminal_coding does any conversion, use it, otherwise use
862 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
863 because it always return 1 if the member src_multibyte is 1. */
864 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
865 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
866 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
867 the tail. */
868 coding->mode &= ~CODING_MODE_LAST_BLOCK;
870 /* Turn appearance modes of the face. */
871 tty_highlight_if_desired (tty);
872 turn_on_face (f, face_id);
874 coding->mode |= CODING_MODE_LAST_BLOCK;
875 conversion_buffer = encode_terminal_code (string, len, coding);
876 if (coding->produced > 0)
878 BLOCK_INPUT;
879 fwrite (conversion_buffer, 1, coding->produced, tty->output);
880 if (ferror (tty->output))
881 clearerr (tty->output);
882 if (tty->termscript)
883 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
884 UNBLOCK_INPUT;
887 /* Turn appearance modes off. */
888 turn_off_face (f, face_id);
889 tty_turn_off_highlight (tty);
891 cmcheckmagic (tty);
893 #endif
895 /* An implementation of insert_glyphs for termcap frames. */
897 static void
898 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
900 char *buf;
901 struct glyph *glyph = NULL;
902 unsigned char *conversion_buffer;
903 unsigned char space[1];
904 struct coding_system *coding;
906 struct tty_display_info *tty = FRAME_TTY (f);
908 if (tty->TS_ins_multi_chars)
910 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len);
911 OUTPUT1 (tty, buf);
912 xfree (buf);
913 if (start)
914 write_glyphs (f, start, len);
915 return;
918 tty_turn_on_insert (tty);
919 cmplus (tty, len);
921 if (! start)
922 space[0] = SPACEGLYPH;
924 /* If terminal_coding does any conversion, use it, otherwise use
925 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
926 because it always return 1 if the member src_multibyte is 1. */
927 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
928 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
929 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
930 the tail. */
931 coding->mode &= ~CODING_MODE_LAST_BLOCK;
933 while (len-- > 0)
935 OUTPUT1_IF (tty, tty->TS_ins_char);
936 if (!start)
938 conversion_buffer = space;
939 coding->produced = 1;
941 else
943 tty_highlight_if_desired (tty);
944 turn_on_face (f, start->face_id);
945 glyph = start;
946 ++start;
947 /* We must open sufficient space for a character which
948 occupies more than one column. */
949 while (len && CHAR_GLYPH_PADDING_P (*start))
951 OUTPUT1_IF (tty, tty->TS_ins_char);
952 start++, len--;
955 if (len <= 0)
956 /* This is the last glyph. */
957 coding->mode |= CODING_MODE_LAST_BLOCK;
959 conversion_buffer = encode_terminal_code (glyph, 1, coding);
962 if (coding->produced > 0)
964 BLOCK_INPUT;
965 fwrite (conversion_buffer, 1, coding->produced, tty->output);
966 if (ferror (tty->output))
967 clearerr (tty->output);
968 if (tty->termscript)
969 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
970 UNBLOCK_INPUT;
973 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
974 if (start)
976 turn_off_face (f, glyph->face_id);
977 tty_turn_off_highlight (tty);
981 cmcheckmagic (tty);
984 /* An implementation of delete_glyphs for termcap frames. */
986 static void
987 tty_delete_glyphs (struct frame *f, int n)
989 char *buf;
990 register int i;
992 struct tty_display_info *tty = FRAME_TTY (f);
994 if (tty->delete_in_insert_mode)
996 tty_turn_on_insert (tty);
998 else
1000 tty_turn_off_insert (tty);
1001 OUTPUT_IF (tty, tty->TS_delete_mode);
1004 if (tty->TS_del_multi_chars)
1006 buf = tparam (tty->TS_del_multi_chars, 0, 0, n);
1007 OUTPUT1 (tty, buf);
1008 xfree (buf);
1010 else
1011 for (i = 0; i < n; i++)
1012 OUTPUT1 (tty, tty->TS_del_char);
1013 if (!tty->delete_in_insert_mode)
1014 OUTPUT_IF (tty, tty->TS_end_delete_mode);
1017 /* An implementation of ins_del_lines for termcap frames. */
1019 static void
1020 tty_ins_del_lines (struct frame *f, int vpos, int n)
1022 struct tty_display_info *tty = FRAME_TTY (f);
1023 char *multi = n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
1024 char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
1025 char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
1027 register int i = n > 0 ? n : -n;
1028 register char *buf;
1030 /* If the lines below the insertion are being pushed
1031 into the end of the window, this is the same as clearing;
1032 and we know the lines are already clear, since the matching
1033 deletion has already been done. So can ignore this. */
1034 /* If the lines below the deletion are blank lines coming
1035 out of the end of the window, don't bother,
1036 as there will be a matching inslines later that will flush them. */
1037 if (FRAME_SCROLL_REGION_OK (f)
1038 && vpos + i >= tty->specified_window)
1039 return;
1040 if (!FRAME_MEMORY_BELOW_FRAME (f)
1041 && vpos + i >= FRAME_LINES (f))
1042 return;
1044 if (multi)
1046 raw_cursor_to (f, vpos, 0);
1047 tty_background_highlight (tty);
1048 buf = tparam (multi, 0, 0, i);
1049 OUTPUT (tty, buf);
1050 xfree (buf);
1052 else if (single)
1054 raw_cursor_to (f, vpos, 0);
1055 tty_background_highlight (tty);
1056 while (--i >= 0)
1057 OUTPUT (tty, single);
1058 if (tty->TF_teleray)
1059 curX (tty) = 0;
1061 else
1063 tty_set_scroll_region (f, vpos, tty->specified_window);
1064 if (n < 0)
1065 raw_cursor_to (f, tty->specified_window - 1, 0);
1066 else
1067 raw_cursor_to (f, vpos, 0);
1068 tty_background_highlight (tty);
1069 while (--i >= 0)
1070 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1071 tty_set_scroll_region (f, 0, tty->specified_window);
1074 if (!FRAME_SCROLL_REGION_OK (f)
1075 && FRAME_MEMORY_BELOW_FRAME (f)
1076 && n < 0)
1078 cursor_to (f, FRAME_LINES (f) + n, 0);
1079 clear_to_end (f);
1083 /* Compute cost of sending "str", in characters,
1084 not counting any line-dependent padding. */
1087 string_cost (const char *str)
1089 cost = 0;
1090 if (str)
1091 tputs (str, 0, evalcost);
1092 return cost;
1095 /* Compute cost of sending "str", in characters,
1096 counting any line-dependent padding at one line. */
1098 static int
1099 string_cost_one_line (const char *str)
1101 cost = 0;
1102 if (str)
1103 tputs (str, 1, evalcost);
1104 return cost;
1107 /* Compute per line amount of line-dependent padding,
1108 in tenths of characters. */
1111 per_line_cost (const char *str)
1113 cost = 0;
1114 if (str)
1115 tputs (str, 0, evalcost);
1116 cost = - cost;
1117 if (str)
1118 tputs (str, 10, evalcost);
1119 return cost;
1122 /* char_ins_del_cost[n] is cost of inserting N characters.
1123 char_ins_del_cost[-n] is cost of deleting N characters.
1124 The length of this vector is based on max_frame_cols. */
1126 int *char_ins_del_vector;
1128 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1130 /* ARGSUSED */
1131 static void
1132 calculate_ins_del_char_costs (struct frame *f)
1134 struct tty_display_info *tty = FRAME_TTY (f);
1135 int ins_startup_cost, del_startup_cost;
1136 int ins_cost_per_char, del_cost_per_char;
1137 register int i;
1138 register int *p;
1140 if (tty->TS_ins_multi_chars)
1142 ins_cost_per_char = 0;
1143 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1145 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1146 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1148 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1149 + string_cost (tty->TS_end_insert_mode))) / 100;
1150 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1151 + string_cost_one_line (tty->TS_pad_inserted_char));
1153 else
1155 ins_startup_cost = 9999;
1156 ins_cost_per_char = 0;
1159 if (tty->TS_del_multi_chars)
1161 del_cost_per_char = 0;
1162 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1164 else if (tty->TS_del_char)
1166 del_startup_cost = (string_cost (tty->TS_delete_mode)
1167 + string_cost (tty->TS_end_delete_mode));
1168 if (tty->delete_in_insert_mode)
1169 del_startup_cost /= 2;
1170 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1172 else
1174 del_startup_cost = 9999;
1175 del_cost_per_char = 0;
1178 /* Delete costs are at negative offsets */
1179 p = &char_ins_del_cost (f)[0];
1180 for (i = FRAME_COLS (f); --i >= 0;)
1181 *--p = (del_startup_cost += del_cost_per_char);
1183 /* Doing nothing is free */
1184 p = &char_ins_del_cost (f)[0];
1185 *p++ = 0;
1187 /* Insert costs are at positive offsets */
1188 for (i = FRAME_COLS (f); --i >= 0;)
1189 *p++ = (ins_startup_cost += ins_cost_per_char);
1192 void
1193 calculate_costs (struct frame *frame)
1195 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1197 if (FRAME_TERMCAP_P (frame))
1199 struct tty_display_info *tty = FRAME_TTY (frame);
1200 register char *f = (tty->TS_set_scroll_region
1201 ? tty->TS_set_scroll_region
1202 : tty->TS_set_scroll_region_1);
1204 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1206 tty->costs_set = 1;
1208 /* These variables are only used for terminal stuff. They are
1209 allocated once for the terminal frame of X-windows emacs, but not
1210 used afterwards.
1212 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1213 X turns off char_ins_del_ok. */
1215 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1216 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1218 if (char_ins_del_vector != 0)
1219 char_ins_del_vector
1220 = (int *) xrealloc (char_ins_del_vector,
1221 (sizeof (int)
1222 + 2 * max_frame_cols * sizeof (int)));
1223 else
1224 char_ins_del_vector
1225 = (int *) xmalloc (sizeof (int)
1226 + 2 * max_frame_cols * sizeof (int));
1228 memset (char_ins_del_vector, 0,
1229 (sizeof (int) + 2 * max_frame_cols * sizeof (int)));
1232 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1233 do_line_insertion_deletion_costs (frame,
1234 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1235 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1236 f, f, 1);
1237 else
1238 do_line_insertion_deletion_costs (frame,
1239 tty->TS_ins_line, tty->TS_ins_multi_lines,
1240 tty->TS_del_line, tty->TS_del_multi_lines,
1241 0, 0, 1);
1243 calculate_ins_del_char_costs (frame);
1245 /* Don't use TS_repeat if its padding is worse than sending the chars */
1246 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1247 tty->RPov = string_cost (tty->TS_repeat);
1248 else
1249 tty->RPov = FRAME_COLS (frame) * 2;
1251 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1255 struct fkey_table {
1256 char *cap, *name;
1259 /* Termcap capability names that correspond directly to X keysyms.
1260 Some of these (marked "terminfo") aren't supplied by old-style
1261 (Berkeley) termcap entries. They're listed in X keysym order;
1262 except we put the keypad keys first, so that if they clash with
1263 other keys (as on the IBM PC keyboard) they get overridden.
1266 static const struct fkey_table keys[] =
1268 {"kh", "home"}, /* termcap */
1269 {"kl", "left"}, /* termcap */
1270 {"ku", "up"}, /* termcap */
1271 {"kr", "right"}, /* termcap */
1272 {"kd", "down"}, /* termcap */
1273 {"%8", "prior"}, /* terminfo */
1274 {"%5", "next"}, /* terminfo */
1275 {"@7", "end"}, /* terminfo */
1276 {"@1", "begin"}, /* terminfo */
1277 {"*6", "select"}, /* terminfo */
1278 {"%9", "print"}, /* terminfo */
1279 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1281 * "insert" --- see below
1283 {"&8", "undo"}, /* terminfo */
1284 {"%0", "redo"}, /* terminfo */
1285 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1286 {"@0", "find"}, /* terminfo */
1287 {"@2", "cancel"}, /* terminfo */
1288 {"%1", "help"}, /* terminfo */
1290 * "break" goes here, but can't be reliably intercepted with termcap
1292 {"&4", "reset"}, /* terminfo --- actually `restart' */
1294 * "system" and "user" --- no termcaps
1296 {"kE", "clearline"}, /* terminfo */
1297 {"kA", "insertline"}, /* terminfo */
1298 {"kL", "deleteline"}, /* terminfo */
1299 {"kI", "insertchar"}, /* terminfo */
1300 {"kD", "deletechar"}, /* terminfo */
1301 {"kB", "backtab"}, /* terminfo */
1303 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1305 {"@8", "kp-enter"}, /* terminfo */
1307 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1308 * "kp-multiply", "kp-add", "kp-separator",
1309 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1310 * --- no termcaps for any of these.
1312 {"K4", "kp-1"}, /* terminfo */
1314 * "kp-2" --- no termcap
1316 {"K5", "kp-3"}, /* terminfo */
1318 * "kp-4" --- no termcap
1320 {"K2", "kp-5"}, /* terminfo */
1322 * "kp-6" --- no termcap
1324 {"K1", "kp-7"}, /* terminfo */
1326 * "kp-8" --- no termcap
1328 {"K3", "kp-9"}, /* terminfo */
1330 * "kp-equal" --- no termcap
1332 {"k1", "f1"},
1333 {"k2", "f2"},
1334 {"k3", "f3"},
1335 {"k4", "f4"},
1336 {"k5", "f5"},
1337 {"k6", "f6"},
1338 {"k7", "f7"},
1339 {"k8", "f8"},
1340 {"k9", "f9"},
1342 {"&0", "S-cancel"}, /*shifted cancel key*/
1343 {"&9", "S-begin"}, /*shifted begin key*/
1344 {"*0", "S-find"}, /*shifted find key*/
1345 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1346 {"*4", "S-delete"}, /*shifted delete-character key*/
1347 {"*7", "S-end"}, /*shifted end key*/
1348 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1349 {"#1", "S-help"}, /*shifted help key*/
1350 {"#2", "S-home"}, /*shifted home key*/
1351 {"#3", "S-insert"}, /*shifted insert-character key*/
1352 {"#4", "S-left"}, /*shifted left-arrow key*/
1353 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1354 {"%c", "S-next"}, /*shifted next key*/
1355 {"%e", "S-prior"}, /*shifted previous key*/
1356 {"%f", "S-print"}, /*shifted print key*/
1357 {"%g", "S-redo"}, /*shifted redo key*/
1358 {"%i", "S-right"}, /*shifted right-arrow key*/
1359 {"!3", "S-undo"} /*shifted undo key*/
1362 #ifndef DOS_NT
1363 static char **term_get_fkeys_address;
1364 static KBOARD *term_get_fkeys_kboard;
1365 static Lisp_Object term_get_fkeys_1 (void);
1367 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1368 This function scans the termcap function key sequence entries, and
1369 adds entries to Vinput_decode_map for each function key it finds. */
1371 static void
1372 term_get_fkeys (char **address, KBOARD *kboard)
1374 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1375 errors during the call. The only errors should be from Fdefine_key
1376 when given a key sequence containing an invalid prefix key. If the
1377 termcap defines function keys which use a prefix that is already bound
1378 to a command by the default bindings, we should silently ignore that
1379 function key specification, rather than giving the user an error and
1380 refusing to run at all on such a terminal. */
1382 term_get_fkeys_address = address;
1383 term_get_fkeys_kboard = kboard;
1384 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1387 static Lisp_Object
1388 term_get_fkeys_1 (void)
1390 int i;
1392 char **address = term_get_fkeys_address;
1393 KBOARD *kboard = term_get_fkeys_kboard;
1395 /* This can happen if CANNOT_DUMP or with strange options. */
1396 if (!KEYMAPP (kboard->Vinput_decode_map))
1397 kboard->Vinput_decode_map = Fmake_sparse_keymap (Qnil);
1399 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1401 char *sequence = tgetstr (keys[i].cap, address);
1402 if (sequence)
1403 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
1404 Fmake_vector (make_number (1),
1405 intern (keys[i].name)));
1408 /* The uses of the "k0" capability are inconsistent; sometimes it
1409 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1410 We will attempt to politely accommodate both systems by testing for
1411 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1414 char *k_semi = tgetstr ("k;", address);
1415 char *k0 = tgetstr ("k0", address);
1416 char *k0_name = "f10";
1418 if (k_semi)
1420 if (k0)
1421 /* Define f0 first, so that f10 takes precedence in case the
1422 key sequences happens to be the same. */
1423 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
1424 Fmake_vector (make_number (1), intern ("f0")));
1425 Fdefine_key (kboard->Vinput_decode_map, build_string (k_semi),
1426 Fmake_vector (make_number (1), intern ("f10")));
1428 else if (k0)
1429 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
1430 Fmake_vector (make_number (1), intern (k0_name)));
1433 /* Set up cookies for numbered function keys above f10. */
1435 char fcap[3], fkey[4];
1437 fcap[0] = 'F'; fcap[2] = '\0';
1438 for (i = 11; i < 64; i++)
1440 if (i <= 19)
1441 fcap[1] = '1' + i - 11;
1442 else if (i <= 45)
1443 fcap[1] = 'A' + i - 20;
1444 else
1445 fcap[1] = 'a' + i - 46;
1448 char *sequence = tgetstr (fcap, address);
1449 if (sequence)
1451 sprintf (fkey, "f%d", i);
1452 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
1453 Fmake_vector (make_number (1),
1454 intern (fkey)));
1461 * Various mappings to try and get a better fit.
1464 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1465 if (!tgetstr (cap1, address)) \
1467 char *sequence = tgetstr (cap2, address); \
1468 if (sequence) \
1469 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence), \
1470 Fmake_vector (make_number (1), \
1471 intern (sym))); \
1474 /* if there's no key_next keycap, map key_npage to `next' keysym */
1475 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1476 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1477 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1478 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1479 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1480 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1481 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1483 /* IBM has their own non-standard dialect of terminfo.
1484 If the standard name isn't found, try the IBM name. */
1485 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1486 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1487 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1488 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1489 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1490 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1491 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1492 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1493 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1494 #undef CONDITIONAL_REASSIGN
1497 return Qnil;
1499 #endif /* not DOS_NT */
1502 /***********************************************************************
1503 Character Display Information
1504 ***********************************************************************/
1505 static void append_glyph (struct it *);
1506 static void produce_stretch_glyph (struct it *);
1507 static void append_composite_glyph (struct it *);
1508 static void produce_composite_glyph (struct it *);
1510 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1511 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1512 the character for which to produce glyphs; IT->face_id contains the
1513 character's face. Padding glyphs are appended if IT->c has a
1514 IT->pixel_width > 1. */
1516 static void
1517 append_glyph (struct it *it)
1519 struct glyph *glyph, *end;
1520 int i;
1522 xassert (it->glyph_row);
1523 glyph = (it->glyph_row->glyphs[it->area]
1524 + it->glyph_row->used[it->area]);
1525 end = it->glyph_row->glyphs[1 + it->area];
1527 /* If the glyph row is reversed, we need to prepend the glyph rather
1528 than append it. */
1529 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1531 struct glyph *g;
1532 int move_by = it->pixel_width;
1534 /* Make room for the new glyphs. */
1535 if (move_by > end - glyph) /* don't overstep end of this area */
1536 move_by = end - glyph;
1537 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1538 g[move_by] = *g;
1539 glyph = it->glyph_row->glyphs[it->area];
1540 end = glyph + move_by;
1543 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1544 right, even in the REVERSED_P case, since (a) all of its u.ch are
1545 identical, and (b) the PADDING_P flag needs to be set for the
1546 leftmost one, because we write to the terminal left-to-right. */
1547 for (i = 0;
1548 i < it->pixel_width && glyph < end;
1549 ++i)
1551 glyph->type = CHAR_GLYPH;
1552 glyph->pixel_width = 1;
1553 glyph->u.ch = it->char_to_display;
1554 glyph->face_id = it->face_id;
1555 glyph->padding_p = i > 0;
1556 glyph->charpos = CHARPOS (it->position);
1557 glyph->object = it->object;
1558 if (it->bidi_p)
1560 glyph->resolved_level = it->bidi_it.resolved_level;
1561 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1562 abort ();
1563 glyph->bidi_type = it->bidi_it.type;
1565 else
1567 glyph->resolved_level = 0;
1568 glyph->bidi_type = UNKNOWN_BT;
1571 ++it->glyph_row->used[it->area];
1572 ++glyph;
1576 /* Produce glyphs for the display element described by IT. *IT
1577 specifies what we want to produce a glyph for (character, image, ...),
1578 and where in the glyph matrix we currently are (glyph row and hpos).
1579 produce_glyphs fills in output fields of *IT with information such as the
1580 pixel width and height of a character, and maybe output actual glyphs at
1581 the same time if IT->glyph_row is non-null. For an overview, see
1582 the explanation in dispextern.h, before the definition of the
1583 display_element_type enumeration.
1585 produce_glyphs also stores the result of glyph width, ascent
1586 etc. computations in *IT.
1588 IT->glyph_row may be null, in which case produce_glyphs does not
1589 actually fill in the glyphs. This is used in the move_* functions
1590 in xdisp.c for text width and height computations.
1592 Callers usually don't call produce_glyphs directly;
1593 instead they use the macro PRODUCE_GLYPHS. */
1595 void
1596 produce_glyphs (struct it *it)
1598 /* If a hook is installed, let it do the work. */
1600 /* Nothing but characters are supported on terminal frames. */
1601 xassert (it->what == IT_CHARACTER
1602 || it->what == IT_COMPOSITION
1603 || it->what == IT_STRETCH);
1605 if (it->what == IT_STRETCH)
1607 produce_stretch_glyph (it);
1608 goto done;
1611 if (it->what == IT_COMPOSITION)
1613 produce_composite_glyph (it);
1614 goto done;
1617 if (it->char_to_display >= 040 && it->char_to_display < 0177)
1619 it->pixel_width = it->nglyphs = 1;
1620 if (it->glyph_row)
1621 append_glyph (it);
1623 else if (it->char_to_display == '\n')
1624 it->pixel_width = it->nglyphs = 0;
1625 else if (it->char_to_display == '\t')
1627 int absolute_x = (it->current_x
1628 + it->continuation_lines_width);
1629 int next_tab_x
1630 = (((1 + absolute_x + it->tab_width - 1)
1631 / it->tab_width)
1632 * it->tab_width);
1633 int nspaces;
1635 /* If part of the TAB has been displayed on the previous line
1636 which is continued now, continuation_lines_width will have
1637 been incremented already by the part that fitted on the
1638 continued line. So, we will get the right number of spaces
1639 here. */
1640 nspaces = next_tab_x - absolute_x;
1642 if (it->glyph_row)
1644 int n = nspaces;
1646 it->char_to_display = ' ';
1647 it->pixel_width = it->len = 1;
1649 while (n--)
1650 append_glyph (it);
1653 it->pixel_width = nspaces;
1654 it->nglyphs = nspaces;
1656 else if (CHAR_BYTE8_P (it->char_to_display))
1658 /* Coming here means that we must send the raw 8-bit byte as is
1659 to the terminal. Although there's no way to know how many
1660 columns it occupies on a screen, it is a good assumption that
1661 a single byte code has 1-column width. */
1662 it->pixel_width = it->nglyphs = 1;
1663 if (it->glyph_row)
1664 append_glyph (it);
1666 else
1668 it->pixel_width = CHAR_WIDTH (it->char_to_display);
1669 it->nglyphs = it->pixel_width;
1671 if (it->glyph_row)
1672 append_glyph (it);
1675 done:
1676 /* Advance current_x by the pixel width as a convenience for
1677 the caller. */
1678 if (it->area == TEXT_AREA)
1679 it->current_x += it->pixel_width;
1680 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1681 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1685 /* Produce a stretch glyph for iterator IT. IT->object is the value
1686 of the glyph property displayed. The value must be a list
1687 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1688 being recognized:
1690 1. `:width WIDTH' specifies that the space should be WIDTH *
1691 canonical char width wide. WIDTH may be an integer or floating
1692 point number.
1694 2. `:align-to HPOS' specifies that the space should be wide enough
1695 to reach HPOS, a value in canonical character units. */
1697 static void
1698 produce_stretch_glyph (struct it *it)
1700 /* (space :width WIDTH ...) */
1701 Lisp_Object prop, plist;
1702 int width = 0, align_to = -1;
1703 int zero_width_ok_p = 0;
1704 double tem;
1706 /* List should start with `space'. */
1707 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1708 plist = XCDR (it->object);
1710 /* Compute the width of the stretch. */
1711 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1712 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1714 /* Absolute width `:width WIDTH' specified and valid. */
1715 zero_width_ok_p = 1;
1716 width = (int)(tem + 0.5);
1718 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1719 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1721 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1722 align_to = (align_to < 0
1724 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1725 else if (align_to < 0)
1726 align_to = window_box_left_offset (it->w, TEXT_AREA);
1727 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1728 zero_width_ok_p = 1;
1730 else
1731 /* Nothing specified -> width defaults to canonical char width. */
1732 width = FRAME_COLUMN_WIDTH (it->f);
1734 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1735 width = 1;
1737 if (width > 0 && it->line_wrap != TRUNCATE
1738 && it->current_x + width > it->last_visible_x)
1739 width = it->last_visible_x - it->current_x - 1;
1741 if (width > 0 && it->glyph_row)
1743 Lisp_Object o_object = it->object;
1744 Lisp_Object object = it->stack[it->sp - 1].string;
1745 int n = width;
1747 if (!STRINGP (object))
1748 object = it->w->buffer;
1749 it->object = object;
1750 it->char_to_display = ' ';
1751 it->pixel_width = it->len = 1;
1752 while (n--)
1753 append_glyph (it);
1754 it->object = o_object;
1756 it->pixel_width = width;
1757 it->nglyphs = width;
1761 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1762 Called from produce_composite_glyph for terminal frames if
1763 IT->glyph_row != NULL. IT->face_id contains the character's
1764 face. */
1766 static void
1767 append_composite_glyph (struct it *it)
1769 struct glyph *glyph;
1771 xassert (it->glyph_row);
1772 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1773 if (glyph < it->glyph_row->glyphs[1 + it->area])
1775 /* If the glyph row is reversed, we need to prepend the glyph
1776 rather than append it. */
1777 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1779 struct glyph *g;
1781 /* Make room for the new glyph. */
1782 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1783 g[1] = *g;
1784 glyph = it->glyph_row->glyphs[it->area];
1786 glyph->type = COMPOSITE_GLYPH;
1787 glyph->pixel_width = it->pixel_width;
1788 glyph->u.cmp.id = it->cmp_it.id;
1789 if (it->cmp_it.ch < 0)
1791 glyph->u.cmp.automatic = 0;
1792 glyph->u.cmp.id = it->cmp_it.id;
1794 else
1796 glyph->u.cmp.automatic = 1;
1797 glyph->u.cmp.id = it->cmp_it.id;
1798 glyph->u.cmp.from = it->cmp_it.from;
1799 glyph->u.cmp.to = it->cmp_it.to - 1;
1802 glyph->face_id = it->face_id;
1803 glyph->padding_p = 0;
1804 glyph->charpos = CHARPOS (it->position);
1805 glyph->object = it->object;
1806 if (it->bidi_p)
1808 glyph->resolved_level = it->bidi_it.resolved_level;
1809 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1810 abort ();
1811 glyph->bidi_type = it->bidi_it.type;
1813 else
1815 glyph->resolved_level = 0;
1816 glyph->bidi_type = UNKNOWN_BT;
1819 ++it->glyph_row->used[it->area];
1820 ++glyph;
1825 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1826 the composition. We simply produces components of the composition
1827 assuming that the terminal has a capability to layout/render it
1828 correctly. */
1830 static void
1831 produce_composite_glyph (struct it *it)
1833 if (it->cmp_it.ch < 0)
1835 struct composition *cmp = composition_table[it->cmp_it.id];
1837 it->pixel_width = cmp->width;
1839 else
1841 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
1843 it->pixel_width = composition_gstring_width (gstring, it->cmp_it.from,
1844 it->cmp_it.to, NULL);
1846 it->nglyphs = 1;
1847 if (it->glyph_row)
1848 append_composite_glyph (it);
1852 /* Get information about special display element WHAT in an
1853 environment described by IT. WHAT is one of IT_TRUNCATION or
1854 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1855 non-null glyph_row member. This function ensures that fields like
1856 face_id, c, len of IT are left untouched. */
1858 void
1859 produce_special_glyphs (struct it *it, enum display_element_type what)
1861 struct it temp_it;
1862 Lisp_Object gc;
1863 GLYPH glyph;
1865 temp_it = *it;
1866 temp_it.dp = NULL;
1867 temp_it.what = IT_CHARACTER;
1868 temp_it.len = 1;
1869 temp_it.object = make_number (0);
1870 memset (&temp_it.current, 0, sizeof temp_it.current);
1872 if (what == IT_CONTINUATION)
1874 /* Continuation glyph. For R2L lines, we mirror it by hand. */
1875 if (it->bidi_it.paragraph_dir == R2L)
1876 SET_GLYPH_FROM_CHAR (glyph, '/');
1877 else
1878 SET_GLYPH_FROM_CHAR (glyph, '\\');
1879 if (it->dp
1880 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc))
1881 && GLYPH_CODE_CHAR_VALID_P (gc))
1883 /* FIXME: Should we mirror GC for R2L lines? */
1884 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1885 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1888 else if (what == IT_TRUNCATION)
1890 /* Truncation glyph. */
1891 SET_GLYPH_FROM_CHAR (glyph, '$');
1892 if (it->dp
1893 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc))
1894 && GLYPH_CODE_CHAR_VALID_P (gc))
1896 /* FIXME: Should we mirror GC for R2L lines? */
1897 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1898 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1901 else
1902 abort ();
1904 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
1905 temp_it.face_id = GLYPH_FACE (glyph);
1906 temp_it.len = CHAR_BYTES (temp_it.c);
1908 produce_glyphs (&temp_it);
1909 it->pixel_width = temp_it.pixel_width;
1910 it->nglyphs = temp_it.pixel_width;
1915 /***********************************************************************
1916 Faces
1917 ***********************************************************************/
1919 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1920 one of the enumerators from enum no_color_bit, or a bit set built
1921 from them. Some display attributes may not be used together with
1922 color; the termcap capability `NC' specifies which ones. */
1924 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1925 (tty->TN_max_colors > 0 \
1926 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1927 : 1)
1929 /* Turn appearances of face FACE_ID on tty frame F on.
1930 FACE_ID is a realized face ID number, in the face cache. */
1932 static void
1933 turn_on_face (struct frame *f, int face_id)
1935 struct face *face = FACE_FROM_ID (f, face_id);
1936 long fg = face->foreground;
1937 long bg = face->background;
1938 struct tty_display_info *tty = FRAME_TTY (f);
1940 /* Do this first because TS_end_standout_mode may be the same
1941 as TS_exit_attribute_mode, which turns all appearances off. */
1942 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1944 if (tty->TN_max_colors > 0)
1946 if (fg >= 0 && bg >= 0)
1948 /* If the terminal supports colors, we can set them
1949 below without using reverse video. The face's fg
1950 and bg colors are set as they should appear on
1951 the screen, i.e. they take the inverse-video'ness
1952 of the face already into account. */
1954 else if (inverse_video)
1956 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1957 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1958 tty_toggle_highlight (tty);
1960 else
1962 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1963 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1964 tty_toggle_highlight (tty);
1967 else
1969 /* If we can't display colors, use reverse video
1970 if the face specifies that. */
1971 if (inverse_video)
1973 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1974 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1975 tty_toggle_highlight (tty);
1977 else
1979 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1980 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1981 tty_toggle_highlight (tty);
1986 if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1987 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
1989 if (face->tty_dim_p && MAY_USE_WITH_COLORS_P (tty, NC_DIM))
1990 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
1992 /* Alternate charset and blinking not yet used. */
1993 if (face->tty_alt_charset_p
1994 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
1995 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
1997 if (face->tty_blinking_p
1998 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
1999 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
2001 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
2002 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
2004 if (tty->TN_max_colors > 0)
2006 char *ts, *p;
2008 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
2009 if (fg >= 0 && ts)
2011 p = tparam (ts, NULL, 0, (int) fg);
2012 OUTPUT (tty, p);
2013 xfree (p);
2016 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
2017 if (bg >= 0 && ts)
2019 p = tparam (ts, NULL, 0, (int) bg);
2020 OUTPUT (tty, p);
2021 xfree (p);
2027 /* Turn off appearances of face FACE_ID on tty frame F. */
2029 static void
2030 turn_off_face (struct frame *f, int face_id)
2032 struct face *face = FACE_FROM_ID (f, face_id);
2033 struct tty_display_info *tty = FRAME_TTY (f);
2035 xassert (face != NULL);
2037 if (tty->TS_exit_attribute_mode)
2039 /* Capability "me" will turn off appearance modes double-bright,
2040 half-bright, reverse-video, standout, underline. It may or
2041 may not turn off alt-char-mode. */
2042 if (face->tty_bold_p
2043 || face->tty_dim_p
2044 || face->tty_reverse_p
2045 || face->tty_alt_charset_p
2046 || face->tty_blinking_p
2047 || face->tty_underline_p)
2049 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
2050 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
2051 tty->standout_mode = 0;
2054 if (face->tty_alt_charset_p)
2055 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2057 else
2059 /* If we don't have "me" we can only have those appearances
2060 that have exit sequences defined. */
2061 if (face->tty_alt_charset_p)
2062 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2064 if (face->tty_underline_p)
2065 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
2068 /* Switch back to default colors. */
2069 if (tty->TN_max_colors > 0
2070 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2071 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2072 || (face->background != FACE_TTY_DEFAULT_COLOR
2073 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
2074 OUTPUT1_IF (tty, tty->TS_orig_pair);
2078 /* Return non-zero if the terminal on frame F supports all of the
2079 capabilities in CAPS simultaneously, with foreground and background
2080 colors FG and BG. */
2083 tty_capable_p (struct tty_display_info *tty, unsigned int caps,
2084 unsigned long fg, unsigned long bg)
2086 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2087 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2088 return 0;
2090 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2091 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2092 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2093 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
2094 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
2095 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
2097 /* We can do it! */
2098 return 1;
2101 /* Return non-zero if the terminal is capable to display colors. */
2103 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2104 0, 1, 0,
2105 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2107 TERMINAL can be a terminal object, a frame, or nil (meaning the
2108 selected frame's terminal). This function always returns nil if
2109 TERMINAL does not refer to a text-only terminal. */)
2110 (Lisp_Object terminal)
2112 struct terminal *t = get_tty_terminal (terminal, 0);
2113 if (!t)
2114 return Qnil;
2115 else
2116 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
2119 /* Return the number of supported colors. */
2120 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2121 Stty_display_color_cells, 0, 1, 0,
2122 doc: /* Return the number of colors supported by the tty device TERMINAL.
2124 TERMINAL can be a terminal object, a frame, or nil (meaning the
2125 selected frame's terminal). This function always returns 0 if
2126 TERMINAL does not refer to a text-only terminal. */)
2127 (Lisp_Object terminal)
2129 struct terminal *t = get_tty_terminal (terminal, 0);
2130 if (!t)
2131 return make_number (0);
2132 else
2133 return make_number (t->display_info.tty->TN_max_colors);
2136 #ifndef DOS_NT
2138 /* Declare here rather than in the function, as in the rest of Emacs,
2139 to work around an HPUX compiler bug (?). See
2140 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2141 static int default_max_colors;
2142 static int default_max_pairs;
2143 static int default_no_color_video;
2144 static char *default_orig_pair;
2145 static char *default_set_foreground;
2146 static char *default_set_background;
2148 /* Save or restore the default color-related capabilities of this
2149 terminal. */
2150 static void
2151 tty_default_color_capabilities (struct tty_display_info *tty, int save)
2154 if (save)
2156 xfree (default_orig_pair);
2157 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
2159 xfree (default_set_foreground);
2160 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
2161 : NULL;
2163 xfree (default_set_background);
2164 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
2165 : NULL;
2167 default_max_colors = tty->TN_max_colors;
2168 default_max_pairs = tty->TN_max_pairs;
2169 default_no_color_video = tty->TN_no_color_video;
2171 else
2173 tty->TS_orig_pair = default_orig_pair;
2174 tty->TS_set_foreground = default_set_foreground;
2175 tty->TS_set_background = default_set_background;
2176 tty->TN_max_colors = default_max_colors;
2177 tty->TN_max_pairs = default_max_pairs;
2178 tty->TN_no_color_video = default_no_color_video;
2182 /* Setup one of the standard tty color schemes according to MODE.
2183 MODE's value is generally the number of colors which we want to
2184 support; zero means set up for the default capabilities, the ones
2185 we saw at init_tty time; -1 means turn off color support. */
2186 static void
2187 tty_setup_colors (struct tty_display_info *tty, int mode)
2189 /* Canonicalize all negative values of MODE. */
2190 if (mode < -1)
2191 mode = -1;
2193 switch (mode)
2195 case -1: /* no colors at all */
2196 tty->TN_max_colors = 0;
2197 tty->TN_max_pairs = 0;
2198 tty->TN_no_color_video = 0;
2199 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2200 break;
2201 case 0: /* default colors, if any */
2202 default:
2203 tty_default_color_capabilities (tty, 0);
2204 break;
2205 case 8: /* 8 standard ANSI colors */
2206 tty->TS_orig_pair = "\033[0m";
2207 #ifdef TERMINFO
2208 tty->TS_set_foreground = "\033[3%p1%dm";
2209 tty->TS_set_background = "\033[4%p1%dm";
2210 #else
2211 tty->TS_set_foreground = "\033[3%dm";
2212 tty->TS_set_background = "\033[4%dm";
2213 #endif
2214 tty->TN_max_colors = 8;
2215 tty->TN_max_pairs = 64;
2216 tty->TN_no_color_video = 0;
2217 break;
2221 void
2222 set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
2224 Lisp_Object tem, val;
2225 Lisp_Object color_mode;
2226 int mode;
2227 Lisp_Object tty_color_mode_alist
2228 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
2230 tem = assq_no_quit (Qtty_color_mode, f->param_alist);
2231 val = CONSP (tem) ? XCDR (tem) : Qnil;
2233 if (INTEGERP (val))
2234 color_mode = val;
2235 else if (SYMBOLP (tty_color_mode_alist))
2237 tem = Fassq (val, Fsymbol_value (tty_color_mode_alist));
2238 color_mode = CONSP (tem) ? XCDR (tem) : Qnil;
2240 else
2241 color_mode = Qnil;
2243 mode = INTEGERP (color_mode) ? XINT (color_mode) : 0;
2245 if (mode != tty->previous_color_mode)
2247 Lisp_Object funsym = intern ("tty-set-up-initial-frame-faces");
2248 tty->previous_color_mode = mode;
2249 tty_setup_colors (tty , mode);
2250 /* This recomputes all the faces given the new color definitions. */
2251 safe_call (1, &funsym);
2255 #endif /* !DOS_NT */
2259 /* Return the tty display object specified by TERMINAL. */
2261 struct terminal *
2262 get_tty_terminal (Lisp_Object terminal, int throw)
2264 struct terminal *t = get_terminal (terminal, throw);
2266 if (t && t->type != output_termcap && t->type != output_msdos_raw)
2268 if (throw)
2269 error ("Device %d is not a termcap terminal device", t->id);
2270 else
2271 return NULL;
2274 return t;
2277 /* Return an active termcap device that uses the tty device with the
2278 given name.
2280 This function ignores suspended devices.
2282 Returns NULL if the named terminal device is not opened. */
2284 struct terminal *
2285 get_named_tty (const char *name)
2287 struct terminal *t;
2289 if (!name)
2290 abort ();
2292 for (t = terminal_list; t; t = t->next_terminal)
2294 if ((t->type == output_termcap || t->type == output_msdos_raw)
2295 && !strcmp (t->display_info.tty->name, name)
2296 && TERMINAL_ACTIVE_P (t))
2297 return t;
2300 return 0;
2304 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2305 doc: /* Return the type of the tty device that TERMINAL uses.
2306 Returns nil if TERMINAL is not on a tty device.
2308 TERMINAL can be a terminal object, a frame, or nil (meaning the
2309 selected frame's terminal). */)
2310 (Lisp_Object terminal)
2312 struct terminal *t = get_terminal (terminal, 1);
2314 if (t->type != output_termcap && t->type != output_msdos_raw)
2315 return Qnil;
2317 if (t->display_info.tty->type)
2318 return build_string (t->display_info.tty->type);
2319 else
2320 return Qnil;
2323 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2324 doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2326 TERMINAL can be a terminal object, a frame, or nil (meaning the
2327 selected frame's terminal). This function always returns nil if
2328 TERMINAL is not on a tty device. */)
2329 (Lisp_Object terminal)
2331 struct terminal *t = get_terminal (terminal, 1);
2333 if ((t->type != output_termcap && t->type != output_msdos_raw)
2334 || strcmp (t->display_info.tty->name, DEV_TTY) != 0)
2335 return Qnil;
2336 else
2337 return Qt;
2340 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2341 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2342 This is used to override the terminfo data, for certain terminals that
2343 do not really do underlining, but say that they do. This function has
2344 no effect if used on a non-tty terminal.
2346 TERMINAL can be a terminal object, a frame or nil (meaning the
2347 selected frame's terminal). This function always returns nil if
2348 TERMINAL does not refer to a text-only terminal. */)
2349 (Lisp_Object terminal)
2351 struct terminal *t = get_terminal (terminal, 1);
2353 if (t->type == output_termcap)
2354 t->display_info.tty->TS_enter_underline_mode = 0;
2355 return Qnil;
2360 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2361 doc: /* Suspend the terminal device TTY.
2363 The device is restored to its default state, and Emacs ceases all
2364 access to the tty device. Frames that use the device are not deleted,
2365 but input is not read from them and if they change, their display is
2366 not updated.
2368 TTY may be a terminal object, a frame, or nil for the terminal device
2369 of the currently selected frame.
2371 This function runs `suspend-tty-functions' after suspending the
2372 device. The functions are run with one arg, the id of the suspended
2373 terminal device.
2375 `suspend-tty' does nothing if it is called on a device that is already
2376 suspended.
2378 A suspended tty may be resumed by calling `resume-tty' on it. */)
2379 (Lisp_Object tty)
2381 struct terminal *t = get_tty_terminal (tty, 1);
2382 FILE *f;
2384 if (!t)
2385 error ("Unknown tty device");
2387 f = t->display_info.tty->input;
2389 if (f)
2391 /* First run `suspend-tty-functions' and then clean up the tty
2392 state because `suspend-tty-functions' might need to change
2393 the tty state. */
2394 if (!NILP (Vrun_hooks))
2396 Lisp_Object args[2];
2397 args[0] = intern ("suspend-tty-functions");
2398 XSETTERMINAL (args[1], t);
2399 Frun_hook_with_args (2, args);
2402 reset_sys_modes (t->display_info.tty);
2403 delete_keyboard_wait_descriptor (fileno (f));
2405 #ifndef MSDOS
2406 fclose (f);
2407 if (f != t->display_info.tty->output)
2408 fclose (t->display_info.tty->output);
2409 #endif
2411 t->display_info.tty->input = 0;
2412 t->display_info.tty->output = 0;
2414 if (FRAMEP (t->display_info.tty->top_frame))
2415 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2419 /* Clear display hooks to prevent further output. */
2420 clear_tty_hooks (t);
2422 return Qnil;
2425 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2426 doc: /* Resume the previously suspended terminal device TTY.
2427 The terminal is opened and reinitialized. Frames that are on the
2428 suspended terminal are revived.
2430 It is an error to resume a terminal while another terminal is active
2431 on the same device.
2433 This function runs `resume-tty-functions' after resuming the terminal.
2434 The functions are run with one arg, the id of the resumed terminal
2435 device.
2437 `resume-tty' does nothing if it is called on a device that is not
2438 suspended.
2440 TTY may be a terminal object, a frame, or nil (meaning the selected
2441 frame's terminal). */)
2442 (Lisp_Object tty)
2444 struct terminal *t = get_tty_terminal (tty, 1);
2445 int fd;
2447 if (!t)
2448 error ("Unknown tty device");
2450 if (!t->display_info.tty->input)
2452 if (get_named_tty (t->display_info.tty->name))
2453 error ("Cannot resume display while another display is active on the same device");
2455 #ifdef MSDOS
2456 t->display_info.tty->output = stdout;
2457 t->display_info.tty->input = stdin;
2458 #else /* !MSDOS */
2459 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2461 if (fd == -1)
2462 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2464 if (strcmp (t->display_info.tty->name, DEV_TTY))
2465 dissociate_if_controlling_tty (fd);
2467 t->display_info.tty->output = fdopen (fd, "w+");
2468 t->display_info.tty->input = t->display_info.tty->output;
2469 #endif
2471 add_keyboard_wait_descriptor (fd);
2473 if (FRAMEP (t->display_info.tty->top_frame))
2475 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2476 int width, height;
2477 int old_height = FRAME_COLS (f);
2478 int old_width = FRAME_LINES (f);
2480 /* Check if terminal/window size has changed while the frame
2481 was suspended. */
2482 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2483 if (width != old_width || height != old_height)
2484 change_frame_size (f, height, width, 0, 0, 0);
2485 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2488 init_sys_modes (t->display_info.tty);
2490 /* Run `resume-tty-functions'. */
2491 if (!NILP (Vrun_hooks))
2493 Lisp_Object args[2];
2494 args[0] = intern ("resume-tty-functions");
2495 XSETTERMINAL (args[1], t);
2496 Frun_hook_with_args (2, args);
2500 set_tty_hooks (t);
2502 return Qnil;
2506 /***********************************************************************
2507 Mouse
2508 ***********************************************************************/
2510 #ifdef HAVE_GPM
2511 void
2512 term_mouse_moveto (int x, int y)
2514 /* TODO: how to set mouse position?
2515 const char *name;
2516 int fd;
2517 name = (const char *) ttyname (0);
2518 fd = open (name, O_WRONLY);
2519 SOME_FUNCTION (x, y, fd);
2520 close (fd);
2521 last_mouse_x = x;
2522 last_mouse_y = y; */
2525 static void
2526 term_show_mouse_face (enum draw_glyphs_face draw)
2528 struct window *w = XWINDOW (mouse_face_window);
2529 int save_x, save_y;
2530 int i;
2532 struct frame *f = XFRAME (w->frame);
2533 struct tty_display_info *tty = FRAME_TTY (f);
2535 if (/* If window is in the process of being destroyed, don't bother
2536 to do anything. */
2537 w->current_matrix != NULL
2538 /* Recognize when we are called to operate on rows that don't exist
2539 anymore. This can happen when a window is split. */
2540 && mouse_face_end_row < w->current_matrix->nrows)
2542 /* write_glyphs writes at cursor position, so we need to
2543 temporarily move cursor coordinates to the beginning of
2544 the highlight region. */
2546 /* Save current cursor co-ordinates */
2547 save_y = curY (tty);
2548 save_x = curX (tty);
2550 /* Note that mouse_face_beg_row etc. are window relative. */
2551 for (i = mouse_face_beg_row; i <= mouse_face_end_row; i++)
2553 int start_hpos, end_hpos, nglyphs;
2554 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
2556 /* Don't do anything if row doesn't have valid contents. */
2557 if (!row->enabled_p)
2558 continue;
2560 /* For all but the first row, the highlight starts at column 0. */
2561 if (i == mouse_face_beg_row)
2562 start_hpos = mouse_face_beg_col;
2563 else
2564 start_hpos = 0;
2566 if (i == mouse_face_end_row)
2567 end_hpos = mouse_face_end_col;
2568 else
2570 end_hpos = row->used[TEXT_AREA];
2571 if (draw == DRAW_NORMAL_TEXT)
2572 row->fill_line_p = 1; /* Clear to end of line */
2575 if (end_hpos <= start_hpos)
2576 continue;
2577 /* Record that some glyphs of this row are displayed in
2578 mouse-face. */
2579 row->mouse_face_p = draw > 0;
2581 nglyphs = end_hpos - start_hpos;
2583 if (end_hpos >= row->used[TEXT_AREA])
2584 nglyphs = row->used[TEXT_AREA] - start_hpos;
2586 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2587 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos
2588 + WINDOW_LEFT_EDGE_X (w);
2590 cursor_to (f, pos_y, pos_x);
2592 if (draw == DRAW_MOUSE_FACE)
2594 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2595 nglyphs, mouse_face_face_id);
2597 else /* draw == DRAW_NORMAL_TEXT */
2598 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
2600 cursor_to (f, save_y, save_x);
2604 static void
2605 term_clear_mouse_face (void)
2607 if (!NILP (mouse_face_window))
2608 term_show_mouse_face (DRAW_NORMAL_TEXT);
2610 mouse_face_beg_row = mouse_face_beg_col = -1;
2611 mouse_face_end_row = mouse_face_end_col = -1;
2612 mouse_face_window = Qnil;
2615 /* Find the glyph matrix position of buffer position POS in window W.
2616 *HPOS and *VPOS are set to the positions found. W's current glyphs
2617 must be up to date. If POS is above window start return (0, 0).
2618 If POS is after end of W, return end of last line in W.
2619 - taken from msdos.c */
2620 static int
2621 fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
2623 int i, lastcol, line_start_position, maybe_next_line_p = 0;
2624 int yb = window_text_bottom_y (w);
2625 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0), *best_row = row;
2627 while (row->y < yb)
2629 if (row->used[TEXT_AREA])
2630 line_start_position = row->glyphs[TEXT_AREA]->charpos;
2631 else
2632 line_start_position = 0;
2634 if (line_start_position > pos)
2635 break;
2636 /* If the position sought is the end of the buffer,
2637 don't include the blank lines at the bottom of the window. */
2638 else if (line_start_position == pos
2639 && pos == BUF_ZV (XBUFFER (w->buffer)))
2641 maybe_next_line_p = 1;
2642 break;
2644 else if (line_start_position > 0)
2645 best_row = row;
2647 /* Don't overstep the last matrix row, lest we get into the
2648 never-never land... */
2649 if (row->y + 1 >= yb)
2650 break;
2652 ++row;
2655 /* Find the right column within BEST_ROW. */
2656 lastcol = 0;
2657 row = best_row;
2658 for (i = 0; i < row->used[TEXT_AREA]; i++)
2660 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
2661 int charpos;
2663 charpos = glyph->charpos;
2664 if (charpos == pos)
2666 *hpos = i;
2667 *vpos = row->y;
2668 return 1;
2670 else if (charpos > pos)
2671 break;
2672 else if (charpos > 0)
2673 lastcol = i;
2676 /* If we're looking for the end of the buffer,
2677 and we didn't find it in the line we scanned,
2678 use the start of the following line. */
2679 if (maybe_next_line_p)
2681 ++row;
2682 lastcol = 0;
2685 *vpos = row->y;
2686 *hpos = lastcol + 1;
2687 return 0;
2690 static void
2691 term_mouse_highlight (struct frame *f, int x, int y)
2693 enum window_part part;
2694 Lisp_Object window;
2695 struct window *w;
2696 struct buffer *b;
2698 if (NILP (Vmouse_highlight)
2699 || !f->glyphs_initialized_p)
2700 return;
2702 /* Which window is that in? */
2703 window = window_from_coordinates (f, x, y, &part, &x, &y, 0);
2705 /* Not on a window -> return. */
2706 if (!WINDOWP (window))
2707 return;
2709 if (!EQ (window, mouse_face_window))
2710 term_clear_mouse_face ();
2712 w = XWINDOW (window);
2714 /* Are we in a window whose display is up to date?
2715 And verify the buffer's text has not changed. */
2716 b = XBUFFER (w->buffer);
2717 if (part == ON_TEXT
2718 && EQ (w->window_end_valid, w->buffer)
2719 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
2720 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
2722 int pos, i, nrows = w->current_matrix->nrows;
2723 struct glyph_row *row;
2724 struct glyph *glyph;
2726 /* Find the glyph under X/Y. */
2727 glyph = NULL;
2728 if (y >= 0 && y < nrows)
2730 row = MATRIX_ROW (w->current_matrix, y);
2731 /* Give up if some row before the one we are looking for is
2732 not enabled. */
2733 for (i = 0; i <= y; i++)
2734 if (!MATRIX_ROW (w->current_matrix, i)->enabled_p)
2735 break;
2736 if (i > y /* all rows upto and including the one at Y are enabled */
2737 && row->displays_text_p
2738 && x < window_box_width (w, TEXT_AREA))
2740 glyph = row->glyphs[TEXT_AREA];
2741 if (x >= row->used[TEXT_AREA])
2742 glyph = NULL;
2743 else
2745 glyph += x;
2746 if (!BUFFERP (glyph->object))
2747 glyph = NULL;
2752 /* Clear mouse face if X/Y not over text. */
2753 if (glyph == NULL)
2755 term_clear_mouse_face ();
2756 return;
2759 if (!BUFFERP (glyph->object))
2760 abort ();
2761 pos = glyph->charpos;
2763 /* Check for mouse-face. */
2765 Lisp_Object mouse_face, overlay, position, *overlay_vec;
2766 int noverlays, obegv, ozv;
2767 struct buffer *obuf;
2769 /* If we get an out-of-range value, return now; avoid an error. */
2770 if (pos > BUF_Z (b))
2771 return;
2773 /* Make the window's buffer temporarily current for
2774 overlays_at and compute_char_face. */
2775 obuf = current_buffer;
2776 current_buffer = b;
2777 obegv = BEGV;
2778 ozv = ZV;
2779 BEGV = BEG;
2780 ZV = Z;
2782 /* Is this char mouse-active? */
2783 XSETINT (position, pos);
2785 /* Put all the overlays we want in a vector in overlay_vec. */
2786 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
2787 /* Sort overlays into increasing priority order. */
2788 noverlays = sort_overlays (overlay_vec, noverlays, w);
2790 /* Check mouse-face highlighting. */
2791 if (!(EQ (window, mouse_face_window)
2792 && y >= mouse_face_beg_row
2793 && y <= mouse_face_end_row
2794 && (y > mouse_face_beg_row
2795 || x >= mouse_face_beg_col)
2796 && (y < mouse_face_end_row
2797 || x < mouse_face_end_col
2798 || mouse_face_past_end)))
2800 /* Clear the display of the old active region, if any. */
2801 term_clear_mouse_face ();
2803 /* Find the highest priority overlay that has a mouse-face
2804 property. */
2805 overlay = Qnil;
2806 for (i = noverlays - 1; i >= 0; --i)
2808 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
2809 if (!NILP (mouse_face))
2811 overlay = overlay_vec[i];
2812 break;
2816 /* If no overlay applies, get a text property. */
2817 if (NILP (overlay))
2818 mouse_face = Fget_text_property (position, Qmouse_face,
2819 w->buffer);
2821 /* Handle the overlay case. */
2822 if (!NILP (overlay))
2824 /* Find the range of text around this char that
2825 should be active. */
2826 Lisp_Object before, after;
2827 EMACS_INT ignore;
2830 before = Foverlay_start (overlay);
2831 after = Foverlay_end (overlay);
2832 /* Record this as the current active region. */
2833 fast_find_position (w, XFASTINT (before),
2834 &mouse_face_beg_col,
2835 &mouse_face_beg_row);
2837 mouse_face_past_end
2838 = !fast_find_position (w, XFASTINT (after),
2839 &mouse_face_end_col,
2840 &mouse_face_end_row);
2841 mouse_face_window = window;
2843 mouse_face_face_id
2844 = face_at_buffer_position (w, pos, 0, 0,
2845 &ignore, pos + 1, 1, -1);
2847 /* Display it as active. */
2848 term_show_mouse_face (DRAW_MOUSE_FACE);
2850 /* Handle the text property case. */
2851 else if (!NILP (mouse_face))
2853 /* Find the range of text around this char that
2854 should be active. */
2855 Lisp_Object before, after, beginning, end;
2856 EMACS_INT ignore;
2858 beginning = Fmarker_position (w->start);
2859 XSETINT (end, (BUF_Z (b) - XFASTINT (w->window_end_pos)));
2860 before
2861 = Fprevious_single_property_change (make_number (pos + 1),
2862 Qmouse_face,
2863 w->buffer, beginning);
2864 after
2865 = Fnext_single_property_change (position, Qmouse_face,
2866 w->buffer, end);
2868 /* Record this as the current active region. */
2869 fast_find_position (w, XFASTINT (before),
2870 &mouse_face_beg_col,
2871 &mouse_face_beg_row);
2872 mouse_face_past_end
2873 = !fast_find_position (w, XFASTINT (after),
2874 &mouse_face_end_col,
2875 &mouse_face_end_row);
2876 mouse_face_window = window;
2878 mouse_face_face_id
2879 = face_at_buffer_position (w, pos, 0, 0,
2880 &ignore, pos + 1, 1, -1);
2882 /* Display it as active. */
2883 term_show_mouse_face (DRAW_MOUSE_FACE);
2887 /* Look for a `help-echo' property. */
2889 Lisp_Object help;
2891 /* Check overlays first. */
2892 help = Qnil;
2893 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
2895 overlay = overlay_vec[i];
2896 help = Foverlay_get (overlay, Qhelp_echo);
2899 if (!NILP (help))
2901 help_echo_string = help;
2902 help_echo_window = window;
2903 help_echo_object = overlay;
2904 help_echo_pos = pos;
2906 /* Try text properties. */
2907 else if (NILP (help)
2908 && ((STRINGP (glyph->object)
2909 && glyph->charpos >= 0
2910 && glyph->charpos < SCHARS (glyph->object))
2911 || (BUFFERP (glyph->object)
2912 && glyph->charpos >= BEGV
2913 && glyph->charpos < ZV)))
2915 help = Fget_text_property (make_number (glyph->charpos),
2916 Qhelp_echo, glyph->object);
2917 if (!NILP (help))
2919 help_echo_string = help;
2920 help_echo_window = window;
2921 help_echo_object = glyph->object;
2922 help_echo_pos = glyph->charpos;
2927 BEGV = obegv;
2928 ZV = ozv;
2929 current_buffer = obuf;
2934 static int
2935 term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2937 /* Has the mouse moved off the glyph it was on at the last sighting? */
2938 if (event->x != last_mouse_x || event->y != last_mouse_y)
2940 frame->mouse_moved = 1;
2941 term_mouse_highlight (frame, event->x, event->y);
2942 /* Remember which glyph we're now on. */
2943 last_mouse_x = event->x;
2944 last_mouse_y = event->y;
2945 return 1;
2947 return 0;
2950 /* Return the current position of the mouse.
2952 Set *f to the frame the mouse is in, or zero if the mouse is in no
2953 Emacs frame. If it is set to zero, all the other arguments are
2954 garbage.
2956 Set *bar_window to Qnil, and *x and *y to the column and
2957 row of the character cell the mouse is over.
2959 Set *time to the time the mouse was at the returned position.
2961 This clears mouse_moved until the next motion
2962 event arrives. */
2963 static void
2964 term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2965 enum scroll_bar_part *part, Lisp_Object *x,
2966 Lisp_Object *y, unsigned long *time)
2968 struct timeval now;
2970 *fp = SELECTED_FRAME ();
2971 (*fp)->mouse_moved = 0;
2973 *bar_window = Qnil;
2974 *part = 0;
2976 XSETINT (*x, last_mouse_x);
2977 XSETINT (*y, last_mouse_y);
2978 gettimeofday(&now, 0);
2979 *time = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2982 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2984 If the event is a button press, then note that we have grabbed
2985 the mouse. */
2987 static Lisp_Object
2988 term_mouse_click (struct input_event *result, Gpm_Event *event,
2989 struct frame *f)
2991 struct timeval now;
2992 int i, j;
2994 result->kind = GPM_CLICK_EVENT;
2995 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2997 if (event->buttons & j) {
2998 result->code = i; /* button number */
2999 break;
3002 gettimeofday(&now, 0);
3003 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
3005 if (event->type & GPM_UP)
3006 result->modifiers = up_modifier;
3007 else if (event->type & GPM_DOWN)
3008 result->modifiers = down_modifier;
3009 else
3010 result->modifiers = 0;
3012 if (event->type & GPM_SINGLE)
3013 result->modifiers |= click_modifier;
3015 if (event->type & GPM_DOUBLE)
3016 result->modifiers |= double_modifier;
3018 if (event->type & GPM_TRIPLE)
3019 result->modifiers |= triple_modifier;
3021 if (event->type & GPM_DRAG)
3022 result->modifiers |= drag_modifier;
3024 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
3026 /* 1 << KG_SHIFT */
3027 if (event->modifiers & (1 << 0))
3028 result->modifiers |= shift_modifier;
3030 /* 1 << KG_CTRL */
3031 if (event->modifiers & (1 << 2))
3032 result->modifiers |= ctrl_modifier;
3034 /* 1 << KG_ALT || KG_ALTGR */
3035 if (event->modifiers & (1 << 3)
3036 || event->modifiers & (1 << 1))
3037 result->modifiers |= meta_modifier;
3040 XSETINT (result->x, event->x);
3041 XSETINT (result->y, event->y);
3042 XSETFRAME (result->frame_or_window, f);
3043 result->arg = Qnil;
3044 return Qnil;
3048 handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
3050 struct frame *f = XFRAME (tty->top_frame);
3051 struct input_event ie;
3052 int do_help = 0;
3053 int count = 0;
3055 EVENT_INIT (ie);
3056 ie.kind = NO_EVENT;
3057 ie.arg = Qnil;
3059 if (event->type & (GPM_MOVE | GPM_DRAG)) {
3060 previous_help_echo_string = help_echo_string;
3061 help_echo_string = Qnil;
3063 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
3065 if (!term_mouse_movement (f, event))
3066 help_echo_string = previous_help_echo_string;
3068 /* If the contents of the global variable help_echo_string
3069 has changed, generate a HELP_EVENT. */
3070 if (!NILP (help_echo_string)
3071 || !NILP (previous_help_echo_string))
3072 do_help = 1;
3074 goto done;
3076 else {
3077 f->mouse_moved = 0;
3078 term_mouse_click (&ie, event, f);
3081 done:
3082 if (ie.kind != NO_EVENT)
3084 kbd_buffer_store_event_hold (&ie, hold_quit);
3085 count++;
3088 if (do_help
3089 && !(hold_quit && hold_quit->kind != NO_EVENT))
3091 Lisp_Object frame;
3093 if (f)
3094 XSETFRAME (frame, f);
3095 else
3096 frame = Qnil;
3098 gen_help_event (help_echo_string, frame, help_echo_window,
3099 help_echo_object, help_echo_pos);
3100 count++;
3103 return count;
3106 DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
3107 0, 0, 0,
3108 doc: /* Open a connection to Gpm.
3109 Gpm-mouse can only be activated for one tty at a time. */)
3110 (void)
3112 struct frame *f = SELECTED_FRAME ();
3113 struct tty_display_info *tty
3114 = ((f)->output_method == output_termcap
3115 ? (f)->terminal->display_info.tty : NULL);
3116 Gpm_Connect connection;
3118 if (!tty)
3119 error ("Gpm-mouse only works in the GNU/Linux console");
3120 if (gpm_tty == tty)
3121 return Qnil; /* Already activated, nothing to do. */
3122 if (gpm_tty)
3123 error ("Gpm-mouse can only be activated for one tty at a time");
3125 connection.eventMask = ~0;
3126 connection.defaultMask = ~GPM_HARD;
3127 connection.maxMod = ~0;
3128 connection.minMod = 0;
3129 gpm_zerobased = 1;
3131 if (Gpm_Open (&connection, 0) < 0)
3132 error ("Gpm-mouse failed to connect to the gpm daemon");
3133 else
3135 gpm_tty = tty;
3136 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
3137 to generate SIGIOs. Apparently we need to call reset_sys_modes
3138 before calling init_sys_modes. */
3139 reset_sys_modes (tty);
3140 init_sys_modes (tty);
3141 add_gpm_wait_descriptor (gpm_fd);
3142 return Qnil;
3146 void
3147 close_gpm (int fd)
3149 if (fd >= 0)
3150 delete_gpm_wait_descriptor (fd);
3151 while (Gpm_Close()); /* close all the stack */
3152 gpm_tty = NULL;
3155 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
3156 0, 0, 0,
3157 doc: /* Close a connection to Gpm. */)
3158 (void)
3160 struct frame *f = SELECTED_FRAME ();
3161 struct tty_display_info *tty
3162 = ((f)->output_method == output_termcap
3163 ? (f)->terminal->display_info.tty : NULL);
3165 if (!tty || gpm_tty != tty)
3166 return Qnil; /* Not activated on this terminal, nothing to do. */
3168 close_gpm (gpm_fd);
3169 return Qnil;
3171 #endif /* HAVE_GPM */
3174 #ifndef MSDOS
3175 /***********************************************************************
3176 Initialization
3177 ***********************************************************************/
3179 /* Initialize the tty-dependent part of frame F. The frame must
3180 already have its device initialized. */
3182 void
3183 create_tty_output (struct frame *f)
3185 struct tty_output *t;
3187 if (! FRAME_TERMCAP_P (f))
3188 abort ();
3190 t = xmalloc (sizeof (struct tty_output));
3191 memset (t, 0, sizeof (struct tty_output));
3193 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
3195 f->output_data.tty = t;
3198 /* Delete frame F's face cache, and its tty-dependent part. */
3200 static void
3201 tty_free_frame_resources (struct frame *f)
3203 if (! FRAME_TERMCAP_P (f))
3204 abort ();
3206 if (FRAME_FACE_CACHE (f))
3207 free_frame_faces (f);
3209 xfree (f->output_data.tty);
3212 #else /* MSDOS */
3214 /* Delete frame F's face cache. */
3216 static void
3217 tty_free_frame_resources (struct frame *f)
3219 if (! FRAME_TERMCAP_P (f) && ! FRAME_MSDOS_P (f))
3220 abort ();
3222 if (FRAME_FACE_CACHE (f))
3223 free_frame_faces (f);
3225 #endif /* MSDOS */
3227 /* Reset the hooks in TERMINAL. */
3229 static void
3230 clear_tty_hooks (struct terminal *terminal)
3232 terminal->rif = 0;
3233 terminal->cursor_to_hook = 0;
3234 terminal->raw_cursor_to_hook = 0;
3235 terminal->clear_to_end_hook = 0;
3236 terminal->clear_frame_hook = 0;
3237 terminal->clear_end_of_line_hook = 0;
3238 terminal->ins_del_lines_hook = 0;
3239 terminal->insert_glyphs_hook = 0;
3240 terminal->write_glyphs_hook = 0;
3241 terminal->delete_glyphs_hook = 0;
3242 terminal->ring_bell_hook = 0;
3243 terminal->reset_terminal_modes_hook = 0;
3244 terminal->set_terminal_modes_hook = 0;
3245 terminal->update_begin_hook = 0;
3246 terminal->update_end_hook = 0;
3247 terminal->set_terminal_window_hook = 0;
3248 terminal->mouse_position_hook = 0;
3249 terminal->frame_rehighlight_hook = 0;
3250 terminal->frame_raise_lower_hook = 0;
3251 terminal->fullscreen_hook = 0;
3252 terminal->set_vertical_scroll_bar_hook = 0;
3253 terminal->condemn_scroll_bars_hook = 0;
3254 terminal->redeem_scroll_bar_hook = 0;
3255 terminal->judge_scroll_bars_hook = 0;
3256 terminal->read_socket_hook = 0;
3257 terminal->frame_up_to_date_hook = 0;
3259 /* Leave these two set, or suspended frames are not deleted
3260 correctly. */
3261 terminal->delete_frame_hook = &tty_free_frame_resources;
3262 terminal->delete_terminal_hook = &delete_tty;
3265 /* Initialize hooks in TERMINAL with the values needed for a tty. */
3267 static void
3268 set_tty_hooks (struct terminal *terminal)
3270 terminal->rif = 0; /* ttys don't support window-based redisplay. */
3272 terminal->cursor_to_hook = &tty_cursor_to;
3273 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
3275 terminal->clear_to_end_hook = &tty_clear_to_end;
3276 terminal->clear_frame_hook = &tty_clear_frame;
3277 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
3279 terminal->ins_del_lines_hook = &tty_ins_del_lines;
3281 terminal->insert_glyphs_hook = &tty_insert_glyphs;
3282 terminal->write_glyphs_hook = &tty_write_glyphs;
3283 terminal->delete_glyphs_hook = &tty_delete_glyphs;
3285 terminal->ring_bell_hook = &tty_ring_bell;
3287 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
3288 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
3289 terminal->update_begin_hook = 0; /* Not needed. */
3290 terminal->update_end_hook = &tty_update_end;
3291 terminal->set_terminal_window_hook = &tty_set_terminal_window;
3293 terminal->mouse_position_hook = 0; /* Not needed. */
3294 terminal->frame_rehighlight_hook = 0; /* Not needed. */
3295 terminal->frame_raise_lower_hook = 0; /* Not needed. */
3297 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
3298 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
3299 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
3300 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
3302 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
3303 terminal->frame_up_to_date_hook = 0; /* Not needed. */
3305 terminal->delete_frame_hook = &tty_free_frame_resources;
3306 terminal->delete_terminal_hook = &delete_tty;
3309 /* Drop the controlling terminal if fd is the same device. */
3310 static void
3311 dissociate_if_controlling_tty (int fd)
3313 #ifndef DOS_NT
3314 int pgid;
3315 EMACS_GET_TTY_PGRP (fd, &pgid); /* If tcgetpgrp succeeds, fd is the ctty. */
3316 if (pgid != -1)
3318 #if defined (USG5)
3319 setpgrp ();
3320 no_controlling_tty = 1;
3321 #elif defined (CYGWIN)
3322 setsid ();
3323 no_controlling_tty = 1;
3324 #else
3325 #ifdef TIOCNOTTY /* Try BSD ioctls. */
3326 sigblock (sigmask (SIGTTOU));
3327 fd = emacs_open (DEV_TTY, O_RDWR, 0);
3328 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
3330 no_controlling_tty = 1;
3332 if (fd != -1)
3333 emacs_close (fd);
3334 sigunblock (sigmask (SIGTTOU));
3335 #else
3336 /* Unknown system. */
3337 croak ();
3338 #endif /* ! TIOCNOTTY */
3339 #endif /* ! USG */
3341 #endif /* !DOS_NT */
3344 /* Create a termcap display on the tty device with the given name and
3345 type.
3347 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3348 Otherwise NAME should be a path to the tty device file,
3349 e.g. "/dev/pts/7".
3351 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3353 If MUST_SUCCEED is true, then all errors are fatal. */
3355 struct terminal *
3356 init_tty (const char *name, const char *terminal_type, int must_succeed)
3358 char *area = NULL;
3359 char **address = &area;
3360 int buffer_size = 4096;
3361 register char *p = NULL;
3362 int status;
3363 struct tty_display_info *tty = NULL;
3364 struct terminal *terminal = NULL;
3365 int ctty = 0; /* 1 if asked to open controlling tty. */
3367 if (!terminal_type)
3368 maybe_fatal (must_succeed, 0,
3369 "Unknown terminal type",
3370 "Unknown terminal type");
3372 if (name == NULL)
3373 name = DEV_TTY;
3374 if (!strcmp (name, DEV_TTY))
3375 ctty = 1;
3377 /* If we already have a terminal on the given device, use that. If
3378 all such terminals are suspended, create a new one instead. */
3379 /* XXX Perhaps this should be made explicit by having init_tty
3380 always create a new terminal and separating terminal and frame
3381 creation on Lisp level. */
3382 terminal = get_named_tty (name);
3383 if (terminal)
3384 return terminal;
3386 terminal = create_terminal ();
3387 #ifdef MSDOS
3388 if (been_here > 0)
3389 maybe_fatal (1, 0, "Attempt to create another terminal %s", "",
3390 name, "");
3391 been_here = 1;
3392 tty = &the_only_display_info;
3393 #else
3394 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
3395 #endif
3396 memset (tty, 0, sizeof (struct tty_display_info));
3397 tty->next = tty_list;
3398 tty_list = tty;
3400 terminal->type = output_termcap;
3401 terminal->display_info.tty = tty;
3402 tty->terminal = terminal;
3404 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3405 Wcm_clear (tty);
3407 #ifndef DOS_NT
3408 set_tty_hooks (terminal);
3411 int fd;
3412 FILE *file;
3414 #ifdef O_IGNORE_CTTY
3415 if (!ctty)
3416 /* Open the terminal device. Don't recognize it as our
3417 controlling terminal, and don't make it the controlling tty
3418 if we don't have one at the moment. */
3419 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
3420 else
3421 #else
3422 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3423 defined on Hurd. On other systems, we need to explicitly
3424 dissociate ourselves from the controlling tty when we want to
3425 open a frame on the same terminal. */
3426 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
3427 #endif /* O_IGNORE_CTTY */
3429 tty->name = xstrdup (name);
3430 terminal->name = xstrdup (name);
3432 if (fd < 0)
3433 maybe_fatal (must_succeed, terminal,
3434 "Could not open file: %s",
3435 "Could not open file: %s",
3436 name);
3437 if (!isatty (fd))
3439 close (fd);
3440 maybe_fatal (must_succeed, terminal,
3441 "Not a tty device: %s",
3442 "Not a tty device: %s",
3443 name);
3446 #ifndef O_IGNORE_CTTY
3447 if (!ctty)
3448 dissociate_if_controlling_tty (fd);
3449 #endif
3451 file = fdopen (fd, "w+");
3452 tty->input = file;
3453 tty->output = file;
3456 tty->type = xstrdup (terminal_type);
3458 add_keyboard_wait_descriptor (fileno (tty->input));
3460 #endif /* !DOS_NT */
3462 encode_terminal_src_size = 0;
3463 encode_terminal_dst_size = 0;
3465 #ifdef HAVE_GPM
3466 terminal->mouse_position_hook = term_mouse_position;
3467 mouse_face_window = Qnil;
3468 #endif
3470 #ifdef DOS_NT
3471 #ifdef WINDOWSNT
3472 initialize_w32_display (terminal);
3473 #else /* MSDOS */
3474 if (strcmp (terminal_type, "internal") == 0)
3475 terminal->type = output_msdos_raw;
3476 initialize_msdos_display (terminal);
3477 #endif /* MSDOS */
3478 tty->output = stdout;
3479 tty->input = stdin;
3480 /* The following two are inaccessible from w32console.c. */
3481 terminal->delete_frame_hook = &tty_free_frame_resources;
3482 terminal->delete_terminal_hook = &delete_tty;
3484 tty->name = xstrdup (name);
3485 terminal->name = xstrdup (name);
3486 tty->type = xstrdup (terminal_type);
3488 add_keyboard_wait_descriptor (0);
3490 Wcm_clear (tty);
3492 #ifdef WINDOWSNT
3494 struct frame *f = XFRAME (selected_frame);
3496 FrameRows (tty) = FRAME_LINES (f);
3497 FrameCols (tty) = FRAME_COLS (f);
3498 tty->specified_window = FRAME_LINES (f);
3500 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3501 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
3503 #else /* MSDOS */
3505 int height, width;
3506 get_tty_size (fileno (tty->input), &width, &height);
3507 FrameCols (tty) = width;
3508 FrameRows (tty) = height;
3510 #endif /* MSDOS */
3511 tty->delete_in_insert_mode = 1;
3513 UseTabs (tty) = 0;
3514 terminal->scroll_region_ok = 0;
3516 /* Seems to insert lines when it's not supposed to, messing up the
3517 display. In doing a trace, it didn't seem to be called much, so I
3518 don't think we're losing anything by turning it off. */
3519 terminal->line_ins_del_ok = 0;
3520 #ifdef WINDOWSNT
3521 terminal->char_ins_del_ok = 1;
3522 baud_rate = 19200;
3523 #else /* MSDOS */
3524 terminal->char_ins_del_ok = 0;
3525 init_baud_rate (fileno (tty->input));
3526 #endif /* MSDOS */
3528 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
3530 #else /* not DOS_NT */
3532 Wcm_clear (tty);
3534 tty->termcap_term_buffer = (char *) xmalloc (buffer_size);
3536 /* On some systems, tgetent tries to access the controlling
3537 terminal. */
3538 sigblock (sigmask (SIGTTOU));
3539 status = tgetent (tty->termcap_term_buffer, terminal_type);
3540 sigunblock (sigmask (SIGTTOU));
3542 if (status < 0)
3544 #ifdef TERMINFO
3545 maybe_fatal (must_succeed, terminal,
3546 "Cannot open terminfo database file",
3547 "Cannot open terminfo database file");
3548 #else
3549 maybe_fatal (must_succeed, terminal,
3550 "Cannot open termcap database file",
3551 "Cannot open termcap database file");
3552 #endif
3554 if (status == 0)
3556 maybe_fatal (must_succeed, terminal,
3557 "Terminal type %s is not defined",
3558 "Terminal type %s is not defined.\n\
3559 If that is not the actual type of terminal you have,\n\
3560 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3561 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3562 #ifdef TERMINFO
3563 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3564 #else
3565 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3566 #endif
3567 terminal_type);
3570 #ifndef TERMINFO
3571 if (strlen (tty->termcap_term_buffer) >= buffer_size)
3572 abort ();
3573 buffer_size = strlen (tty->termcap_term_buffer);
3574 #endif
3575 tty->termcap_strings_buffer = area = (char *) xmalloc (buffer_size);
3576 tty->TS_ins_line = tgetstr ("al", address);
3577 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3578 tty->TS_bell = tgetstr ("bl", address);
3579 BackTab (tty) = tgetstr ("bt", address);
3580 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3581 tty->TS_clr_line = tgetstr ("ce", address);
3582 tty->TS_clr_frame = tgetstr ("cl", address);
3583 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3584 AbsPosition (tty) = tgetstr ("cm", address);
3585 CR (tty) = tgetstr ("cr", address);
3586 tty->TS_set_scroll_region = tgetstr ("cs", address);
3587 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
3588 RowPosition (tty) = tgetstr ("cv", address);
3589 tty->TS_del_char = tgetstr ("dc", address);
3590 tty->TS_del_multi_chars = tgetstr ("DC", address);
3591 tty->TS_del_line = tgetstr ("dl", address);
3592 tty->TS_del_multi_lines = tgetstr ("DL", address);
3593 tty->TS_delete_mode = tgetstr ("dm", address);
3594 tty->TS_end_delete_mode = tgetstr ("ed", address);
3595 tty->TS_end_insert_mode = tgetstr ("ei", address);
3596 Home (tty) = tgetstr ("ho", address);
3597 tty->TS_ins_char = tgetstr ("ic", address);
3598 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3599 tty->TS_insert_mode = tgetstr ("im", address);
3600 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3601 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3602 tty->TS_keypad_mode = tgetstr ("ks", address);
3603 LastLine (tty) = tgetstr ("ll", address);
3604 Right (tty) = tgetstr ("nd", address);
3605 Down (tty) = tgetstr ("do", address);
3606 if (!Down (tty))
3607 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
3608 if (tgetflag ("bs"))
3609 Left (tty) = "\b"; /* can't possibly be longer! */
3610 else /* (Actually, "bs" is obsolete...) */
3611 Left (tty) = tgetstr ("le", address);
3612 if (!Left (tty))
3613 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
3614 tty->TS_pad_char = tgetstr ("pc", address);
3615 tty->TS_repeat = tgetstr ("rp", address);
3616 tty->TS_end_standout_mode = tgetstr ("se", address);
3617 tty->TS_fwd_scroll = tgetstr ("sf", address);
3618 tty->TS_standout_mode = tgetstr ("so", address);
3619 tty->TS_rev_scroll = tgetstr ("sr", address);
3620 tty->Wcm->cm_tab = tgetstr ("ta", address);
3621 tty->TS_end_termcap_modes = tgetstr ("te", address);
3622 tty->TS_termcap_modes = tgetstr ("ti", address);
3623 Up (tty) = tgetstr ("up", address);
3624 tty->TS_visible_bell = tgetstr ("vb", address);
3625 tty->TS_cursor_normal = tgetstr ("ve", address);
3626 tty->TS_cursor_visible = tgetstr ("vs", address);
3627 tty->TS_cursor_invisible = tgetstr ("vi", address);
3628 tty->TS_set_window = tgetstr ("wi", address);
3630 tty->TS_enter_underline_mode = tgetstr ("us", address);
3631 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3632 tty->TS_enter_bold_mode = tgetstr ("md", address);
3633 tty->TS_enter_dim_mode = tgetstr ("mh", address);
3634 tty->TS_enter_blink_mode = tgetstr ("mb", address);
3635 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3636 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3637 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3638 tty->TS_exit_attribute_mode = tgetstr ("me", address);
3640 MultiUp (tty) = tgetstr ("UP", address);
3641 MultiDown (tty) = tgetstr ("DO", address);
3642 MultiLeft (tty) = tgetstr ("LE", address);
3643 MultiRight (tty) = tgetstr ("RI", address);
3645 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
3646 color because we can't switch back to the default foreground and
3647 background. */
3648 tty->TS_orig_pair = tgetstr ("op", address);
3649 if (tty->TS_orig_pair)
3651 tty->TS_set_foreground = tgetstr ("AF", address);
3652 tty->TS_set_background = tgetstr ("AB", address);
3653 if (!tty->TS_set_foreground)
3655 /* SVr4. */
3656 tty->TS_set_foreground = tgetstr ("Sf", address);
3657 tty->TS_set_background = tgetstr ("Sb", address);
3660 tty->TN_max_colors = tgetnum ("Co");
3661 tty->TN_max_pairs = tgetnum ("pa");
3663 tty->TN_no_color_video = tgetnum ("NC");
3664 if (tty->TN_no_color_video == -1)
3665 tty->TN_no_color_video = 0;
3668 tty_default_color_capabilities (tty, 1);
3670 MagicWrap (tty) = tgetflag ("xn");
3671 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3672 the former flag imply the latter. */
3673 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
3674 terminal->memory_below_frame = tgetflag ("db");
3675 tty->TF_hazeltine = tgetflag ("hz");
3676 terminal->must_write_spaces = tgetflag ("in");
3677 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3678 tty->TF_insmode_motion = tgetflag ("mi");
3679 tty->TF_standout_motion = tgetflag ("ms");
3680 tty->TF_underscore = tgetflag ("ul");
3681 tty->TF_teleray = tgetflag ("xt");
3683 #endif /* !DOS_NT */
3684 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3685 init_kboard (terminal->kboard);
3686 terminal->kboard->Vwindow_system = Qnil;
3687 terminal->kboard->next_kboard = all_kboards;
3688 all_kboards = terminal->kboard;
3689 terminal->kboard->reference_count++;
3690 /* Don't let the initial kboard remain current longer than necessary.
3691 That would cause problems if a file loaded on startup tries to
3692 prompt in the mini-buffer. */
3693 if (current_kboard == initial_kboard)
3694 current_kboard = terminal->kboard;
3695 #ifndef DOS_NT
3696 term_get_fkeys (address, terminal->kboard);
3698 /* Get frame size from system, or else from termcap. */
3700 int height, width;
3701 get_tty_size (fileno (tty->input), &width, &height);
3702 FrameCols (tty) = width;
3703 FrameRows (tty) = height;
3706 if (FrameCols (tty) <= 0)
3707 FrameCols (tty) = tgetnum ("co");
3708 if (FrameRows (tty) <= 0)
3709 FrameRows (tty) = tgetnum ("li");
3711 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
3712 maybe_fatal (must_succeed, terminal,
3713 "Screen size %dx%d is too small",
3714 "Screen size %dx%d is too small",
3715 FrameCols (tty), FrameRows (tty));
3717 TabWidth (tty) = tgetnum ("tw");
3719 if (!tty->TS_bell)
3720 tty->TS_bell = "\07";
3722 if (!tty->TS_fwd_scroll)
3723 tty->TS_fwd_scroll = Down (tty);
3725 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
3727 if (TabWidth (tty) < 0)
3728 TabWidth (tty) = 8;
3730 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3731 and newer termcap doc does not seem to say there is a default.
3732 if (!tty->Wcm->cm_tab)
3733 tty->Wcm->cm_tab = "\t";
3736 /* We don't support standout modes that use `magic cookies', so
3737 turn off any that do. */
3738 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
3740 tty->TS_standout_mode = 0;
3741 tty->TS_end_standout_mode = 0;
3743 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
3745 tty->TS_enter_underline_mode = 0;
3746 tty->TS_exit_underline_mode = 0;
3749 /* If there's no standout mode, try to use underlining instead. */
3750 if (tty->TS_standout_mode == 0)
3752 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3753 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
3756 /* If no `se' string, try using a `me' string instead.
3757 If that fails, we can't use standout mode at all. */
3758 if (tty->TS_end_standout_mode == 0)
3760 char *s = tgetstr ("me", address);
3761 if (s != 0)
3762 tty->TS_end_standout_mode = s;
3763 else
3764 tty->TS_standout_mode = 0;
3767 if (tty->TF_teleray)
3769 tty->Wcm->cm_tab = 0;
3770 /* We can't support standout mode, because it uses magic cookies. */
3771 tty->TS_standout_mode = 0;
3772 /* But that means we cannot rely on ^M to go to column zero! */
3773 CR (tty) = 0;
3774 /* LF can't be trusted either -- can alter hpos */
3775 /* if move at column 0 thru a line with TS_standout_mode */
3776 Down (tty) = 0;
3779 /* Special handling for certain terminal types known to need it */
3781 if (!strcmp (terminal_type, "supdup"))
3783 terminal->memory_below_frame = 1;
3784 tty->Wcm->cm_losewrap = 1;
3786 if (!strncmp (terminal_type, "c10", 3)
3787 || !strcmp (terminal_type, "perq"))
3789 /* Supply a makeshift :wi string.
3790 This string is not valid in general since it works only
3791 for windows starting at the upper left corner;
3792 but that is all Emacs uses.
3794 This string works only if the frame is using
3795 the top of the video memory, because addressing is memory-relative.
3796 So first check the :ti string to see if that is true.
3798 It would be simpler if the :wi string could go in the termcap
3799 entry, but it can't because it is not fully valid.
3800 If it were in the termcap entry, it would confuse other programs. */
3801 if (!tty->TS_set_window)
3803 p = tty->TS_termcap_modes;
3804 while (*p && strcmp (p, "\033v "))
3805 p++;
3806 if (*p)
3807 tty->TS_set_window = "\033v%C %C %C %C ";
3809 /* Termcap entry often fails to have :in: flag */
3810 terminal->must_write_spaces = 1;
3811 /* :ti string typically fails to have \E^G! in it */
3812 /* This limits scope of insert-char to one line. */
3813 strcpy (area, tty->TS_termcap_modes);
3814 strcat (area, "\033\007!");
3815 tty->TS_termcap_modes = area;
3816 area += strlen (area) + 1;
3817 p = AbsPosition (tty);
3818 /* Change all %+ parameters to %C, to handle
3819 values above 96 correctly for the C100. */
3820 while (*p)
3822 if (p[0] == '%' && p[1] == '+')
3823 p[1] = 'C';
3824 p++;
3828 tty->specified_window = FrameRows (tty);
3830 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3832 maybe_fatal (must_succeed, terminal,
3833 "Terminal type \"%s\" is not powerful enough to run Emacs",
3834 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3835 It lacks the ability to position the cursor.\n\
3836 If that is not the actual type of terminal you have,\n\
3837 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3838 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3839 # ifdef TERMINFO
3840 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3841 # else /* TERMCAP */
3842 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3843 # endif /* TERMINFO */
3844 terminal_type);
3847 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
3848 maybe_fatal (must_succeed, terminal,
3849 "Could not determine the frame size",
3850 "Could not determine the frame size");
3852 tty->delete_in_insert_mode
3853 = tty->TS_delete_mode && tty->TS_insert_mode
3854 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
3856 tty->se_is_so = (tty->TS_standout_mode
3857 && tty->TS_end_standout_mode
3858 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
3860 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
3862 terminal->scroll_region_ok
3863 = (tty->Wcm->cm_abs
3864 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
3866 terminal->line_ins_del_ok
3867 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3868 && (tty->TS_del_line || tty->TS_del_multi_lines))
3869 || (terminal->scroll_region_ok
3870 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
3872 terminal->char_ins_del_ok
3873 = ((tty->TS_ins_char || tty->TS_insert_mode
3874 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3875 && (tty->TS_del_char || tty->TS_del_multi_chars));
3877 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
3879 init_baud_rate (fileno (tty->input));
3881 #endif /* not DOS_NT */
3883 /* Init system terminal modes (RAW or CBREAK, etc.). */
3884 init_sys_modes (tty);
3886 return terminal;
3890 static void
3891 vfatal (const char *str, va_list ap)
3893 fprintf (stderr, "emacs: ");
3894 vfprintf (stderr, str, ap);
3895 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
3896 fprintf (stderr, "\n");
3897 va_end (ap);
3898 fflush (stderr);
3899 exit (1);
3903 /* Auxiliary error-handling function for init_tty.
3904 Delete TERMINAL, then call error or fatal with str1 or str2,
3905 respectively, according to MUST_SUCCEED. */
3907 static void
3908 maybe_fatal (int must_succeed, struct terminal *terminal,
3909 const char *str1, const char *str2, ...)
3911 va_list ap;
3912 va_start (ap, str2);
3913 if (terminal)
3914 delete_tty (terminal);
3916 if (must_succeed)
3917 vfatal (str2, ap);
3918 else
3919 verror (str1, ap);
3921 va_end (ap);
3922 abort ();
3925 void
3926 fatal (const char *str, ...)
3928 va_list ap;
3929 va_start (ap, str);
3930 vfatal (str, ap);
3931 va_end (ap);
3936 /* Delete the given tty terminal, closing all frames on it. */
3938 static void
3939 delete_tty (struct terminal *terminal)
3941 struct tty_display_info *tty;
3943 /* Protect against recursive calls. delete_frame in
3944 delete_terminal calls us back when it deletes our last frame. */
3945 if (!terminal->name)
3946 return;
3948 if (terminal->type != output_termcap)
3949 abort ();
3951 tty = terminal->display_info.tty;
3953 if (tty == tty_list)
3954 tty_list = tty->next;
3955 else
3957 struct tty_display_info *p;
3958 for (p = tty_list; p && p->next != tty; p = p->next)
3961 if (! p)
3962 /* This should not happen. */
3963 abort ();
3965 p->next = tty->next;
3966 tty->next = 0;
3969 /* reset_sys_modes needs a valid device, so this call needs to be
3970 before delete_terminal. */
3971 reset_sys_modes (tty);
3973 delete_terminal (terminal);
3975 xfree (tty->name);
3976 xfree (tty->type);
3978 if (tty->input)
3980 delete_keyboard_wait_descriptor (fileno (tty->input));
3981 if (tty->input != stdin)
3982 fclose (tty->input);
3984 if (tty->output && tty->output != stdout && tty->output != tty->input)
3985 fclose (tty->output);
3986 if (tty->termscript)
3987 fclose (tty->termscript);
3989 xfree (tty->old_tty);
3990 xfree (tty->Wcm);
3991 xfree (tty->termcap_strings_buffer);
3992 xfree (tty->termcap_term_buffer);
3994 memset (tty, 0, sizeof (struct tty_display_info));
3995 xfree (tty);
4000 /* Mark the pointers in the tty_display_info objects.
4001 Called by the Fgarbage_collector. */
4003 void
4004 mark_ttys (void)
4006 struct tty_display_info *tty;
4008 for (tty = tty_list; tty; tty = tty->next)
4009 mark_object (tty->top_frame);
4014 void
4015 syms_of_term (void)
4017 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
4018 doc: /* Non-nil means the system uses terminfo rather than termcap.
4019 This variable can be used by terminal emulator packages. */);
4020 #ifdef TERMINFO
4021 system_uses_terminfo = 1;
4022 #else
4023 system_uses_terminfo = 0;
4024 #endif
4026 DEFVAR_LISP ("suspend-tty-functions", &Vsuspend_tty_functions,
4027 doc: /* Functions to be run after suspending a tty.
4028 The functions are run with one argument, the terminal object to be suspended.
4029 See `suspend-tty'. */);
4030 Vsuspend_tty_functions = Qnil;
4033 DEFVAR_LISP ("resume-tty-functions", &Vresume_tty_functions,
4034 doc: /* Functions to be run after resuming a tty.
4035 The functions are run with one argument, the terminal object that was revived.
4036 See `resume-tty'. */);
4037 Vresume_tty_functions = Qnil;
4039 DEFVAR_BOOL ("visible-cursor", &visible_cursor,
4040 doc: /* Non-nil means to make the cursor very visible.
4041 This only has an effect when running in a text terminal.
4042 What means \"very visible\" is up to your terminal. It may make the cursor
4043 bigger, or it may make it blink, or it may do nothing at all. */);
4044 visible_cursor = 1;
4046 defsubr (&Stty_display_color_p);
4047 defsubr (&Stty_display_color_cells);
4048 defsubr (&Stty_no_underline);
4049 defsubr (&Stty_type);
4050 defsubr (&Scontrolling_tty_p);
4051 defsubr (&Ssuspend_tty);
4052 defsubr (&Sresume_tty);
4053 #ifdef HAVE_GPM
4054 defsubr (&Sgpm_mouse_start);
4055 defsubr (&Sgpm_mouse_stop);
4057 staticpro (&mouse_face_window);
4058 #endif /* HAVE_GPM */
4060 #ifndef DOS_NT
4061 default_orig_pair = NULL;
4062 default_set_foreground = NULL;
4063 default_set_background = NULL;
4064 #endif /* !DOS_NT */
4066 encode_terminal_src = NULL;
4067 encode_terminal_dst = NULL;
4072 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
4073 (do not change this comment) */