Do not include stdlib.h and string.h, config.h does it.
[emacs.git] / src / term.c
blobd67056d29189ab60766b3f071f62775c52d7d603
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 <errno.h>
27 #include <sys/file.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
33 #include <signal.h>
34 #include <stdarg.h>
35 #include <setjmp.h>
37 #include "lisp.h"
38 #include "termchar.h"
39 #include "termopts.h"
40 #include "buffer.h"
41 #include "character.h"
42 #include "charset.h"
43 #include "coding.h"
44 #include "composite.h"
45 #include "keyboard.h"
46 #include "frame.h"
47 #include "disptab.h"
48 #include "termhooks.h"
49 #include "dispextern.h"
50 #include "window.h"
51 #include "keymap.h"
52 #include "blockinput.h"
53 #include "syssignal.h"
54 #include "systty.h"
55 #include "intervals.h"
56 #ifdef MSDOS
57 #include "msdos.h"
58 static int been_here = -1;
59 #endif
61 /* For now, don't try to include termcap.h. On some systems,
62 configure finds a non-standard termcap.h that the main build
63 won't find. */
64 extern void tputs (const char *, int, int (*)(int));
65 extern int tgetent (char *, const char *);
66 extern int tgetflag (char *id);
67 extern int tgetnum (char *id);
69 #include "cm.h"
70 #ifdef HAVE_X_WINDOWS
71 #include "xterm.h"
72 #endif
74 #ifndef O_RDWR
75 #define O_RDWR 2
76 #endif
78 #ifndef O_NOCTTY
79 #define O_NOCTTY 0
80 #endif
82 /* The name of the default console device. */
83 #ifdef WINDOWSNT
84 #define DEV_TTY "CONOUT$"
85 #else
86 #define DEV_TTY "/dev/tty"
87 #endif
89 static void tty_set_scroll_region (struct frame *f, int start, int stop);
90 static void turn_on_face (struct frame *, int face_id);
91 static void turn_off_face (struct frame *, int face_id);
92 static void tty_show_cursor (struct tty_display_info *);
93 static void tty_hide_cursor (struct tty_display_info *);
94 static void tty_background_highlight (struct tty_display_info *tty);
95 static void clear_tty_hooks (struct terminal *terminal);
96 static void set_tty_hooks (struct terminal *terminal);
97 static void dissociate_if_controlling_tty (int fd);
98 static void delete_tty (struct terminal *);
99 static void maybe_fatal (int must_succeed, struct terminal *terminal,
100 const char *str1, const char *str2, ...) NO_RETURN;
101 static void vfatal (const char *str, va_list ap) NO_RETURN;
104 #define OUTPUT(tty, a) \
105 emacs_tputs ((tty), a, \
106 (int) (FRAME_LINES (XFRAME (selected_frame)) \
107 - curY (tty)), \
108 cmputc)
110 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
111 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
113 #define OUTPUT_IF(tty, a) \
114 do { \
115 if (a) \
116 emacs_tputs ((tty), a, \
117 (int) (FRAME_LINES (XFRAME (selected_frame)) \
118 - curY (tty) ), \
119 cmputc); \
120 } while (0)
122 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
124 /* If true, use "vs", otherwise use "ve" to make the cursor visible. */
126 static int visible_cursor;
128 /* Display space properties */
130 /* Functions to call after suspending a tty. */
131 Lisp_Object Vsuspend_tty_functions;
133 /* Functions to call after resuming a tty. */
134 Lisp_Object Vresume_tty_functions;
136 /* Chain of all tty device parameters. */
137 struct tty_display_info *tty_list;
139 /* Nonzero means no need to redraw the entire frame on resuming a
140 suspended Emacs. This is useful on terminals with multiple
141 pages, where one page is used for Emacs and another for all
142 else. */
143 int no_redraw_on_reenter;
145 /* Meaning of bits in no_color_video. Each bit set means that the
146 corresponding attribute cannot be combined with colors. */
148 enum no_color_bit
150 NC_STANDOUT = 1 << 0,
151 NC_UNDERLINE = 1 << 1,
152 NC_REVERSE = 1 << 2,
153 NC_BLINK = 1 << 3,
154 NC_DIM = 1 << 4,
155 NC_BOLD = 1 << 5,
156 NC_INVIS = 1 << 6,
157 NC_PROTECT = 1 << 7,
158 NC_ALT_CHARSET = 1 << 8
161 /* internal state */
163 /* The largest frame width in any call to calculate_costs. */
165 int max_frame_cols;
167 /* The largest frame height in any call to calculate_costs. */
169 int max_frame_lines;
171 /* Non-zero if we have dropped our controlling tty and therefore
172 should not open a frame on stdout. */
173 static int no_controlling_tty;
175 /* Provided for lisp packages. */
177 static int system_uses_terminfo;
179 char *tparam (char *, char *, int, int, ...);
181 extern char *tgetstr (char *, char **);
184 #ifdef HAVE_GPM
185 #include <sys/fcntl.h>
187 static void term_clear_mouse_face (void);
188 static void term_mouse_highlight (struct frame *f, int x, int y);
190 /* The device for which we have enabled gpm support (or NULL). */
191 struct tty_display_info *gpm_tty = NULL;
193 /* These variables describe the range of text currently shown in its
194 mouse-face, together with the window they apply to. As long as
195 the mouse stays within this range, we need not redraw anything on
196 its account. Rows and columns are glyph matrix positions in
197 MOUSE_FACE_WINDOW. */
198 static int mouse_face_beg_row, mouse_face_beg_col;
199 static int mouse_face_end_row, mouse_face_end_col;
200 static int mouse_face_past_end;
201 static Lisp_Object mouse_face_window;
202 static int mouse_face_face_id;
204 static int pos_x, pos_y;
205 static int last_mouse_x, last_mouse_y;
206 #endif /* HAVE_GPM */
208 /* Ring the bell on a tty. */
210 static void
211 tty_ring_bell (struct frame *f)
213 struct tty_display_info *tty = FRAME_TTY (f);
215 if (tty->output)
217 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
218 ? tty->TS_visible_bell
219 : tty->TS_bell));
220 fflush (tty->output);
224 /* Set up termcap modes for Emacs. */
226 void
227 tty_set_terminal_modes (struct terminal *terminal)
229 struct tty_display_info *tty = terminal->display_info.tty;
231 if (tty->output)
233 if (tty->TS_termcap_modes)
234 OUTPUT (tty, tty->TS_termcap_modes);
235 else
237 /* Output enough newlines to scroll all the old screen contents
238 off the screen, so it won't be overwritten and lost. */
239 int i;
240 current_tty = tty;
241 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
242 cmputc ('\n');
245 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
246 OUTPUT_IF (tty, tty->TS_keypad_mode);
247 losecursor (tty);
248 fflush (tty->output);
252 /* Reset termcap modes before exiting Emacs. */
254 void
255 tty_reset_terminal_modes (struct terminal *terminal)
257 struct tty_display_info *tty = terminal->display_info.tty;
259 if (tty->output)
261 tty_turn_off_highlight (tty);
262 tty_turn_off_insert (tty);
263 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
264 OUTPUT_IF (tty, tty->TS_cursor_normal);
265 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
266 OUTPUT_IF (tty, tty->TS_orig_pair);
267 /* Output raw CR so kernel can track the cursor hpos. */
268 current_tty = tty;
269 cmputc ('\r');
270 fflush (tty->output);
274 /* Flag the end of a display update on a termcap terminal. */
276 static void
277 tty_update_end (struct frame *f)
279 struct tty_display_info *tty = FRAME_TTY (f);
281 if (!XWINDOW (selected_window)->cursor_off_p)
282 tty_show_cursor (tty);
283 tty_turn_off_insert (tty);
284 tty_background_highlight (tty);
287 /* The implementation of set_terminal_window for termcap frames. */
289 static void
290 tty_set_terminal_window (struct frame *f, int size)
292 struct tty_display_info *tty = FRAME_TTY (f);
294 tty->specified_window = size ? size : FRAME_LINES (f);
295 if (FRAME_SCROLL_REGION_OK (f))
296 tty_set_scroll_region (f, 0, tty->specified_window);
299 static void
300 tty_set_scroll_region (struct frame *f, int start, int stop)
302 char *buf;
303 struct tty_display_info *tty = FRAME_TTY (f);
305 if (tty->TS_set_scroll_region)
306 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1);
307 else if (tty->TS_set_scroll_region_1)
308 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
309 FRAME_LINES (f), start,
310 FRAME_LINES (f) - stop,
311 FRAME_LINES (f));
312 else
313 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
315 OUTPUT (tty, buf);
316 xfree (buf);
317 losecursor (tty);
321 static void
322 tty_turn_on_insert (struct tty_display_info *tty)
324 if (!tty->insert_mode)
325 OUTPUT (tty, tty->TS_insert_mode);
326 tty->insert_mode = 1;
329 void
330 tty_turn_off_insert (struct tty_display_info *tty)
332 if (tty->insert_mode)
333 OUTPUT (tty, tty->TS_end_insert_mode);
334 tty->insert_mode = 0;
337 /* Handle highlighting. */
339 void
340 tty_turn_off_highlight (struct tty_display_info *tty)
342 if (tty->standout_mode)
343 OUTPUT_IF (tty, tty->TS_end_standout_mode);
344 tty->standout_mode = 0;
347 static void
348 tty_turn_on_highlight (struct tty_display_info *tty)
350 if (!tty->standout_mode)
351 OUTPUT_IF (tty, tty->TS_standout_mode);
352 tty->standout_mode = 1;
355 static void
356 tty_toggle_highlight (struct tty_display_info *tty)
358 if (tty->standout_mode)
359 tty_turn_off_highlight (tty);
360 else
361 tty_turn_on_highlight (tty);
365 /* Make cursor invisible. */
367 static void
368 tty_hide_cursor (struct tty_display_info *tty)
370 if (tty->cursor_hidden == 0)
372 tty->cursor_hidden = 1;
373 OUTPUT_IF (tty, tty->TS_cursor_invisible);
378 /* Ensure that cursor is visible. */
380 static void
381 tty_show_cursor (struct tty_display_info *tty)
383 if (tty->cursor_hidden)
385 tty->cursor_hidden = 0;
386 OUTPUT_IF (tty, tty->TS_cursor_normal);
387 if (visible_cursor)
388 OUTPUT_IF (tty, tty->TS_cursor_visible);
393 /* Set standout mode to the state it should be in for
394 empty space inside windows. What this is,
395 depends on the user option inverse-video. */
397 static void
398 tty_background_highlight (struct tty_display_info *tty)
400 if (inverse_video)
401 tty_turn_on_highlight (tty);
402 else
403 tty_turn_off_highlight (tty);
406 /* Set standout mode to the mode specified for the text to be output. */
408 static void
409 tty_highlight_if_desired (struct tty_display_info *tty)
411 if (inverse_video)
412 tty_turn_on_highlight (tty);
413 else
414 tty_turn_off_highlight (tty);
418 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
419 frame-relative coordinates. */
421 static void
422 tty_cursor_to (struct frame *f, int vpos, int hpos)
424 struct tty_display_info *tty = FRAME_TTY (f);
426 /* Detect the case where we are called from reset_sys_modes
427 and the costs have never been calculated. Do nothing. */
428 if (! tty->costs_set)
429 return;
431 if (curY (tty) == vpos
432 && curX (tty) == hpos)
433 return;
434 if (!tty->TF_standout_motion)
435 tty_background_highlight (tty);
436 if (!tty->TF_insmode_motion)
437 tty_turn_off_insert (tty);
438 cmgoto (tty, vpos, hpos);
441 /* Similar but don't take any account of the wasted characters. */
443 static void
444 tty_raw_cursor_to (struct frame *f, int row, int col)
446 struct tty_display_info *tty = FRAME_TTY (f);
448 if (curY (tty) == row
449 && curX (tty) == col)
450 return;
451 if (!tty->TF_standout_motion)
452 tty_background_highlight (tty);
453 if (!tty->TF_insmode_motion)
454 tty_turn_off_insert (tty);
455 cmgoto (tty, row, col);
458 /* Erase operations */
460 /* Clear from cursor to end of frame on a termcap device. */
462 static void
463 tty_clear_to_end (struct frame *f)
465 register int i;
466 struct tty_display_info *tty = FRAME_TTY (f);
468 if (tty->TS_clr_to_bottom)
470 tty_background_highlight (tty);
471 OUTPUT (tty, tty->TS_clr_to_bottom);
473 else
475 for (i = curY (tty); i < FRAME_LINES (f); i++)
477 cursor_to (f, i, 0);
478 clear_end_of_line (f, FRAME_COLS (f));
483 /* Clear an entire termcap frame. */
485 static void
486 tty_clear_frame (struct frame *f)
488 struct tty_display_info *tty = FRAME_TTY (f);
490 if (tty->TS_clr_frame)
492 tty_background_highlight (tty);
493 OUTPUT (tty, tty->TS_clr_frame);
494 cmat (tty, 0, 0);
496 else
498 cursor_to (f, 0, 0);
499 clear_to_end (f);
503 /* An implementation of clear_end_of_line for termcap frames.
505 Note that the cursor may be moved, on terminals lacking a `ce' string. */
507 static void
508 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
510 register int i;
511 struct tty_display_info *tty = FRAME_TTY (f);
513 /* Detect the case where we are called from reset_sys_modes
514 and the costs have never been calculated. Do nothing. */
515 if (! tty->costs_set)
516 return;
518 if (curX (tty) >= first_unused_hpos)
519 return;
520 tty_background_highlight (tty);
521 if (tty->TS_clr_line)
523 OUTPUT1 (tty, tty->TS_clr_line);
525 else
526 { /* have to do it the hard way */
527 tty_turn_off_insert (tty);
529 /* Do not write in last row last col with Auto-wrap on. */
530 if (AutoWrap (tty)
531 && curY (tty) == FrameRows (tty) - 1
532 && first_unused_hpos == FrameCols (tty))
533 first_unused_hpos--;
535 for (i = curX (tty); i < first_unused_hpos; i++)
537 if (tty->termscript)
538 fputc (' ', tty->termscript);
539 fputc (' ', tty->output);
541 cmplus (tty, first_unused_hpos - curX (tty));
545 /* Buffers to store the source and result of code conversion for terminal. */
546 static unsigned char *encode_terminal_src;
547 static unsigned char *encode_terminal_dst;
548 /* Allocated sizes of the above buffers. */
549 static int encode_terminal_src_size;
550 static int encode_terminal_dst_size;
552 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
553 Set CODING->produced to the byte-length of the resulting byte
554 sequence, and return a pointer to that byte sequence. */
556 unsigned char *
557 encode_terminal_code (struct glyph *src, int src_len, struct coding_system *coding)
559 struct glyph *src_end = src + src_len;
560 unsigned char *buf;
561 int nchars, nbytes, required;
562 register int tlen = GLYPH_TABLE_LENGTH;
563 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
564 Lisp_Object charset_list;
566 /* Allocate sufficient size of buffer to store all characters in
567 multibyte-form. But, it may be enlarged on demand if
568 Vglyph_table contains a string or a composite glyph is
569 encountered. */
570 required = MAX_MULTIBYTE_LENGTH * src_len;
571 if (encode_terminal_src_size < required)
573 if (encode_terminal_src)
574 encode_terminal_src = xrealloc (encode_terminal_src, required);
575 else
576 encode_terminal_src = xmalloc (required);
577 encode_terminal_src_size = required;
580 charset_list = coding_charset_list (coding);
582 buf = encode_terminal_src;
583 nchars = 0;
584 while (src < src_end)
586 if (src->type == COMPOSITE_GLYPH)
588 struct composition *cmp;
589 Lisp_Object gstring;
590 int i;
592 nbytes = buf - encode_terminal_src;
593 if (src->u.cmp.automatic)
595 gstring = composition_gstring_from_id (src->u.cmp.id);
596 required = src->slice.cmp.to + 1 - src->slice.cmp.from;
598 else
600 cmp = composition_table[src->u.cmp.id];
601 required = MAX_MULTIBYTE_LENGTH * cmp->glyph_len;
604 if (encode_terminal_src_size < nbytes + required)
606 encode_terminal_src_size = nbytes + required;
607 encode_terminal_src = xrealloc (encode_terminal_src,
608 encode_terminal_src_size);
609 buf = encode_terminal_src + nbytes;
612 if (src->u.cmp.automatic)
613 for (i = src->slice.cmp.from; i <= src->slice.cmp.to; i++)
615 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
616 int c = LGLYPH_CHAR (g);
618 if (! char_charset (c, charset_list, NULL))
619 c = '?';
620 buf += CHAR_STRING (c, buf);
621 nchars++;
623 else
624 for (i = 0; i < cmp->glyph_len; i++)
626 int c = COMPOSITION_GLYPH (cmp, i);
628 if (c == '\t')
629 continue;
630 if (char_charset (c, charset_list, NULL))
632 if (CHAR_WIDTH (c) == 0
633 && i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
634 /* Should be left-padded */
636 buf += CHAR_STRING (' ', buf);
637 nchars++;
640 else
641 c = '?';
642 buf += CHAR_STRING (c, buf);
643 nchars++;
646 /* We must skip glyphs to be padded for a wide character. */
647 else if (! CHAR_GLYPH_PADDING_P (*src))
649 GLYPH g;
650 int c;
651 Lisp_Object string;
653 string = Qnil;
654 SET_GLYPH_FROM_CHAR_GLYPH (g, src[0]);
656 if (GLYPH_INVALID_P (g) || GLYPH_SIMPLE_P (tbase, tlen, g))
658 /* This glyph doesn't have an entry in Vglyph_table. */
659 c = src->u.ch;
661 else
663 /* This glyph has an entry in Vglyph_table,
664 so process any alias before testing for simpleness. */
665 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
667 if (GLYPH_SIMPLE_P (tbase, tlen, g))
668 /* We set the multi-byte form of a character in G
669 (that should be an ASCII character) at WORKBUF. */
670 c = GLYPH_CHAR (g);
671 else
672 /* We have a string in Vglyph_table. */
673 string = tbase[GLYPH_CHAR (g)];
676 if (NILP (string))
678 nbytes = buf - encode_terminal_src;
679 if (encode_terminal_src_size < nbytes + MAX_MULTIBYTE_LENGTH)
681 encode_terminal_src_size = nbytes + MAX_MULTIBYTE_LENGTH;
682 encode_terminal_src = xrealloc (encode_terminal_src,
683 encode_terminal_src_size);
684 buf = encode_terminal_src + nbytes;
686 if (CHAR_BYTE8_P (c)
687 || char_charset (c, charset_list, NULL))
689 /* Store the multibyte form of C at BUF. */
690 buf += CHAR_STRING (c, buf);
691 nchars++;
693 else
695 /* C is not encodable. */
696 *buf++ = '?';
697 nchars++;
698 while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
700 *buf++ = '?';
701 nchars++;
702 src++;
706 else
708 unsigned char *p = SDATA (string);
710 if (! STRING_MULTIBYTE (string))
711 string = string_to_multibyte (string);
712 nbytes = buf - encode_terminal_src;
713 if (encode_terminal_src_size < nbytes + SBYTES (string))
715 encode_terminal_src_size = nbytes + SBYTES (string);
716 encode_terminal_src = xrealloc (encode_terminal_src,
717 encode_terminal_src_size);
718 buf = encode_terminal_src + nbytes;
720 memcpy (buf, SDATA (string), SBYTES (string));
721 buf += SBYTES (string);
722 nchars += SCHARS (string);
725 src++;
728 if (nchars == 0)
730 coding->produced = 0;
731 return NULL;
734 nbytes = buf - encode_terminal_src;
735 coding->source = encode_terminal_src;
736 if (encode_terminal_dst_size == 0)
738 encode_terminal_dst_size = encode_terminal_src_size;
739 if (encode_terminal_dst)
740 encode_terminal_dst = xrealloc (encode_terminal_dst,
741 encode_terminal_dst_size);
742 else
743 encode_terminal_dst = xmalloc (encode_terminal_dst_size);
745 coding->destination = encode_terminal_dst;
746 coding->dst_bytes = encode_terminal_dst_size;
747 encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
748 /* coding->destination may have been reallocated. */
749 encode_terminal_dst = coding->destination;
750 encode_terminal_dst_size = coding->dst_bytes;
752 return (encode_terminal_dst);
757 /* An implementation of write_glyphs for termcap frames. */
759 static void
760 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
762 unsigned char *conversion_buffer;
763 struct coding_system *coding;
765 struct tty_display_info *tty = FRAME_TTY (f);
767 tty_turn_off_insert (tty);
768 tty_hide_cursor (tty);
770 /* Don't dare write in last column of bottom line, if Auto-Wrap,
771 since that would scroll the whole frame on some terminals. */
773 if (AutoWrap (tty)
774 && curY (tty) + 1 == FRAME_LINES (f)
775 && (curX (tty) + len) == FRAME_COLS (f))
776 len --;
777 if (len <= 0)
778 return;
780 cmplus (tty, len);
782 /* If terminal_coding does any conversion, use it, otherwise use
783 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
784 because it always return 1 if the member src_multibyte is 1. */
785 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
786 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
787 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
788 the tail. */
789 coding->mode &= ~CODING_MODE_LAST_BLOCK;
791 while (len > 0)
793 /* Identify a run of glyphs with the same face. */
794 int face_id = string->face_id;
795 int n;
797 for (n = 1; n < len; ++n)
798 if (string[n].face_id != face_id)
799 break;
801 /* Turn appearance modes of the face of the run on. */
802 tty_highlight_if_desired (tty);
803 turn_on_face (f, face_id);
805 if (n == len)
806 /* This is the last run. */
807 coding->mode |= CODING_MODE_LAST_BLOCK;
808 conversion_buffer = encode_terminal_code (string, n, coding);
809 if (coding->produced > 0)
811 BLOCK_INPUT;
812 fwrite (conversion_buffer, 1, coding->produced, tty->output);
813 if (ferror (tty->output))
814 clearerr (tty->output);
815 if (tty->termscript)
816 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
817 UNBLOCK_INPUT;
819 len -= n;
820 string += n;
822 /* Turn appearance modes off. */
823 turn_off_face (f, face_id);
824 tty_turn_off_highlight (tty);
827 cmcheckmagic (tty);
830 #ifdef HAVE_GPM /* Only used by GPM code. */
832 static void
833 tty_write_glyphs_with_face (register struct frame *f, register struct glyph *string,
834 register int len, register int face_id)
836 unsigned char *conversion_buffer;
837 struct coding_system *coding;
839 struct tty_display_info *tty = FRAME_TTY (f);
841 tty_turn_off_insert (tty);
842 tty_hide_cursor (tty);
844 /* Don't dare write in last column of bottom line, if Auto-Wrap,
845 since that would scroll the whole frame on some terminals. */
847 if (AutoWrap (tty)
848 && curY (tty) + 1 == FRAME_LINES (f)
849 && (curX (tty) + len) == FRAME_COLS (f))
850 len --;
851 if (len <= 0)
852 return;
854 cmplus (tty, len);
856 /* If terminal_coding does any conversion, use it, otherwise use
857 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
858 because it always return 1 if the member src_multibyte is 1. */
859 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
860 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
861 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
862 the tail. */
863 coding->mode &= ~CODING_MODE_LAST_BLOCK;
865 /* Turn appearance modes of the face. */
866 tty_highlight_if_desired (tty);
867 turn_on_face (f, face_id);
869 coding->mode |= CODING_MODE_LAST_BLOCK;
870 conversion_buffer = encode_terminal_code (string, len, coding);
871 if (coding->produced > 0)
873 BLOCK_INPUT;
874 fwrite (conversion_buffer, 1, coding->produced, tty->output);
875 if (ferror (tty->output))
876 clearerr (tty->output);
877 if (tty->termscript)
878 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
879 UNBLOCK_INPUT;
882 /* Turn appearance modes off. */
883 turn_off_face (f, face_id);
884 tty_turn_off_highlight (tty);
886 cmcheckmagic (tty);
888 #endif
890 /* An implementation of insert_glyphs for termcap frames. */
892 static void
893 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
895 char *buf;
896 struct glyph *glyph = NULL;
897 unsigned char *conversion_buffer;
898 unsigned char space[1];
899 struct coding_system *coding;
901 struct tty_display_info *tty = FRAME_TTY (f);
903 if (tty->TS_ins_multi_chars)
905 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len);
906 OUTPUT1 (tty, buf);
907 xfree (buf);
908 if (start)
909 write_glyphs (f, start, len);
910 return;
913 tty_turn_on_insert (tty);
914 cmplus (tty, len);
916 if (! start)
917 space[0] = SPACEGLYPH;
919 /* If terminal_coding does any conversion, use it, otherwise use
920 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
921 because it always return 1 if the member src_multibyte is 1. */
922 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
923 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
924 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
925 the tail. */
926 coding->mode &= ~CODING_MODE_LAST_BLOCK;
928 while (len-- > 0)
930 OUTPUT1_IF (tty, tty->TS_ins_char);
931 if (!start)
933 conversion_buffer = space;
934 coding->produced = 1;
936 else
938 tty_highlight_if_desired (tty);
939 turn_on_face (f, start->face_id);
940 glyph = start;
941 ++start;
942 /* We must open sufficient space for a character which
943 occupies more than one column. */
944 while (len && CHAR_GLYPH_PADDING_P (*start))
946 OUTPUT1_IF (tty, tty->TS_ins_char);
947 start++, len--;
950 if (len <= 0)
951 /* This is the last glyph. */
952 coding->mode |= CODING_MODE_LAST_BLOCK;
954 conversion_buffer = encode_terminal_code (glyph, 1, coding);
957 if (coding->produced > 0)
959 BLOCK_INPUT;
960 fwrite (conversion_buffer, 1, coding->produced, tty->output);
961 if (ferror (tty->output))
962 clearerr (tty->output);
963 if (tty->termscript)
964 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
965 UNBLOCK_INPUT;
968 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
969 if (start)
971 turn_off_face (f, glyph->face_id);
972 tty_turn_off_highlight (tty);
976 cmcheckmagic (tty);
979 /* An implementation of delete_glyphs for termcap frames. */
981 static void
982 tty_delete_glyphs (struct frame *f, int n)
984 char *buf;
985 register int i;
987 struct tty_display_info *tty = FRAME_TTY (f);
989 if (tty->delete_in_insert_mode)
991 tty_turn_on_insert (tty);
993 else
995 tty_turn_off_insert (tty);
996 OUTPUT_IF (tty, tty->TS_delete_mode);
999 if (tty->TS_del_multi_chars)
1001 buf = tparam (tty->TS_del_multi_chars, 0, 0, n);
1002 OUTPUT1 (tty, buf);
1003 xfree (buf);
1005 else
1006 for (i = 0; i < n; i++)
1007 OUTPUT1 (tty, tty->TS_del_char);
1008 if (!tty->delete_in_insert_mode)
1009 OUTPUT_IF (tty, tty->TS_end_delete_mode);
1012 /* An implementation of ins_del_lines for termcap frames. */
1014 static void
1015 tty_ins_del_lines (struct frame *f, int vpos, int n)
1017 struct tty_display_info *tty = FRAME_TTY (f);
1018 char *multi = n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
1019 char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
1020 char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
1022 register int i = n > 0 ? n : -n;
1023 register char *buf;
1025 /* If the lines below the insertion are being pushed
1026 into the end of the window, this is the same as clearing;
1027 and we know the lines are already clear, since the matching
1028 deletion has already been done. So can ignore this. */
1029 /* If the lines below the deletion are blank lines coming
1030 out of the end of the window, don't bother,
1031 as there will be a matching inslines later that will flush them. */
1032 if (FRAME_SCROLL_REGION_OK (f)
1033 && vpos + i >= tty->specified_window)
1034 return;
1035 if (!FRAME_MEMORY_BELOW_FRAME (f)
1036 && vpos + i >= FRAME_LINES (f))
1037 return;
1039 if (multi)
1041 raw_cursor_to (f, vpos, 0);
1042 tty_background_highlight (tty);
1043 buf = tparam (multi, 0, 0, i);
1044 OUTPUT (tty, buf);
1045 xfree (buf);
1047 else if (single)
1049 raw_cursor_to (f, vpos, 0);
1050 tty_background_highlight (tty);
1051 while (--i >= 0)
1052 OUTPUT (tty, single);
1053 if (tty->TF_teleray)
1054 curX (tty) = 0;
1056 else
1058 tty_set_scroll_region (f, vpos, tty->specified_window);
1059 if (n < 0)
1060 raw_cursor_to (f, tty->specified_window - 1, 0);
1061 else
1062 raw_cursor_to (f, vpos, 0);
1063 tty_background_highlight (tty);
1064 while (--i >= 0)
1065 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1066 tty_set_scroll_region (f, 0, tty->specified_window);
1069 if (!FRAME_SCROLL_REGION_OK (f)
1070 && FRAME_MEMORY_BELOW_FRAME (f)
1071 && n < 0)
1073 cursor_to (f, FRAME_LINES (f) + n, 0);
1074 clear_to_end (f);
1078 /* Compute cost of sending "str", in characters,
1079 not counting any line-dependent padding. */
1082 string_cost (const char *str)
1084 cost = 0;
1085 if (str)
1086 tputs (str, 0, evalcost);
1087 return cost;
1090 /* Compute cost of sending "str", in characters,
1091 counting any line-dependent padding at one line. */
1093 static int
1094 string_cost_one_line (const char *str)
1096 cost = 0;
1097 if (str)
1098 tputs (str, 1, evalcost);
1099 return cost;
1102 /* Compute per line amount of line-dependent padding,
1103 in tenths of characters. */
1106 per_line_cost (const char *str)
1108 cost = 0;
1109 if (str)
1110 tputs (str, 0, evalcost);
1111 cost = - cost;
1112 if (str)
1113 tputs (str, 10, evalcost);
1114 return cost;
1117 /* char_ins_del_cost[n] is cost of inserting N characters.
1118 char_ins_del_cost[-n] is cost of deleting N characters.
1119 The length of this vector is based on max_frame_cols. */
1121 int *char_ins_del_vector;
1123 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1125 /* ARGSUSED */
1126 static void
1127 calculate_ins_del_char_costs (struct frame *f)
1129 struct tty_display_info *tty = FRAME_TTY (f);
1130 int ins_startup_cost, del_startup_cost;
1131 int ins_cost_per_char, del_cost_per_char;
1132 register int i;
1133 register int *p;
1135 if (tty->TS_ins_multi_chars)
1137 ins_cost_per_char = 0;
1138 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1140 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1141 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1143 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1144 + string_cost (tty->TS_end_insert_mode))) / 100;
1145 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1146 + string_cost_one_line (tty->TS_pad_inserted_char));
1148 else
1150 ins_startup_cost = 9999;
1151 ins_cost_per_char = 0;
1154 if (tty->TS_del_multi_chars)
1156 del_cost_per_char = 0;
1157 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1159 else if (tty->TS_del_char)
1161 del_startup_cost = (string_cost (tty->TS_delete_mode)
1162 + string_cost (tty->TS_end_delete_mode));
1163 if (tty->delete_in_insert_mode)
1164 del_startup_cost /= 2;
1165 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1167 else
1169 del_startup_cost = 9999;
1170 del_cost_per_char = 0;
1173 /* Delete costs are at negative offsets */
1174 p = &char_ins_del_cost (f)[0];
1175 for (i = FRAME_COLS (f); --i >= 0;)
1176 *--p = (del_startup_cost += del_cost_per_char);
1178 /* Doing nothing is free */
1179 p = &char_ins_del_cost (f)[0];
1180 *p++ = 0;
1182 /* Insert costs are at positive offsets */
1183 for (i = FRAME_COLS (f); --i >= 0;)
1184 *p++ = (ins_startup_cost += ins_cost_per_char);
1187 void
1188 calculate_costs (struct frame *frame)
1190 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1192 if (FRAME_TERMCAP_P (frame))
1194 struct tty_display_info *tty = FRAME_TTY (frame);
1195 register char *f = (tty->TS_set_scroll_region
1196 ? tty->TS_set_scroll_region
1197 : tty->TS_set_scroll_region_1);
1199 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1201 tty->costs_set = 1;
1203 /* These variables are only used for terminal stuff. They are
1204 allocated once for the terminal frame of X-windows emacs, but not
1205 used afterwards.
1207 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1208 X turns off char_ins_del_ok. */
1210 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1211 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1213 if (char_ins_del_vector != 0)
1214 char_ins_del_vector
1215 = (int *) xrealloc (char_ins_del_vector,
1216 (sizeof (int)
1217 + 2 * max_frame_cols * sizeof (int)));
1218 else
1219 char_ins_del_vector
1220 = (int *) xmalloc (sizeof (int)
1221 + 2 * max_frame_cols * sizeof (int));
1223 memset (char_ins_del_vector, 0,
1224 (sizeof (int) + 2 * max_frame_cols * sizeof (int)));
1227 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1228 do_line_insertion_deletion_costs (frame,
1229 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1230 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1231 f, f, 1);
1232 else
1233 do_line_insertion_deletion_costs (frame,
1234 tty->TS_ins_line, tty->TS_ins_multi_lines,
1235 tty->TS_del_line, tty->TS_del_multi_lines,
1236 0, 0, 1);
1238 calculate_ins_del_char_costs (frame);
1240 /* Don't use TS_repeat if its padding is worse than sending the chars */
1241 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1242 tty->RPov = string_cost (tty->TS_repeat);
1243 else
1244 tty->RPov = FRAME_COLS (frame) * 2;
1246 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1250 struct fkey_table {
1251 char *cap, *name;
1254 /* Termcap capability names that correspond directly to X keysyms.
1255 Some of these (marked "terminfo") aren't supplied by old-style
1256 (Berkeley) termcap entries. They're listed in X keysym order;
1257 except we put the keypad keys first, so that if they clash with
1258 other keys (as on the IBM PC keyboard) they get overridden.
1261 static const struct fkey_table keys[] =
1263 {"kh", "home"}, /* termcap */
1264 {"kl", "left"}, /* termcap */
1265 {"ku", "up"}, /* termcap */
1266 {"kr", "right"}, /* termcap */
1267 {"kd", "down"}, /* termcap */
1268 {"%8", "prior"}, /* terminfo */
1269 {"%5", "next"}, /* terminfo */
1270 {"@7", "end"}, /* terminfo */
1271 {"@1", "begin"}, /* terminfo */
1272 {"*6", "select"}, /* terminfo */
1273 {"%9", "print"}, /* terminfo */
1274 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1276 * "insert" --- see below
1278 {"&8", "undo"}, /* terminfo */
1279 {"%0", "redo"}, /* terminfo */
1280 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1281 {"@0", "find"}, /* terminfo */
1282 {"@2", "cancel"}, /* terminfo */
1283 {"%1", "help"}, /* terminfo */
1285 * "break" goes here, but can't be reliably intercepted with termcap
1287 {"&4", "reset"}, /* terminfo --- actually `restart' */
1289 * "system" and "user" --- no termcaps
1291 {"kE", "clearline"}, /* terminfo */
1292 {"kA", "insertline"}, /* terminfo */
1293 {"kL", "deleteline"}, /* terminfo */
1294 {"kI", "insertchar"}, /* terminfo */
1295 {"kD", "deletechar"}, /* terminfo */
1296 {"kB", "backtab"}, /* terminfo */
1298 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1300 {"@8", "kp-enter"}, /* terminfo */
1302 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1303 * "kp-multiply", "kp-add", "kp-separator",
1304 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1305 * --- no termcaps for any of these.
1307 {"K4", "kp-1"}, /* terminfo */
1309 * "kp-2" --- no termcap
1311 {"K5", "kp-3"}, /* terminfo */
1313 * "kp-4" --- no termcap
1315 {"K2", "kp-5"}, /* terminfo */
1317 * "kp-6" --- no termcap
1319 {"K1", "kp-7"}, /* terminfo */
1321 * "kp-8" --- no termcap
1323 {"K3", "kp-9"}, /* terminfo */
1325 * "kp-equal" --- no termcap
1327 {"k1", "f1"},
1328 {"k2", "f2"},
1329 {"k3", "f3"},
1330 {"k4", "f4"},
1331 {"k5", "f5"},
1332 {"k6", "f6"},
1333 {"k7", "f7"},
1334 {"k8", "f8"},
1335 {"k9", "f9"},
1337 {"&0", "S-cancel"}, /*shifted cancel key*/
1338 {"&9", "S-begin"}, /*shifted begin key*/
1339 {"*0", "S-find"}, /*shifted find key*/
1340 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1341 {"*4", "S-delete"}, /*shifted delete-character key*/
1342 {"*7", "S-end"}, /*shifted end key*/
1343 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1344 {"#1", "S-help"}, /*shifted help key*/
1345 {"#2", "S-home"}, /*shifted home key*/
1346 {"#3", "S-insert"}, /*shifted insert-character key*/
1347 {"#4", "S-left"}, /*shifted left-arrow key*/
1348 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1349 {"%c", "S-next"}, /*shifted next key*/
1350 {"%e", "S-prior"}, /*shifted previous key*/
1351 {"%f", "S-print"}, /*shifted print key*/
1352 {"%g", "S-redo"}, /*shifted redo key*/
1353 {"%i", "S-right"}, /*shifted right-arrow key*/
1354 {"!3", "S-undo"} /*shifted undo key*/
1357 #ifndef DOS_NT
1358 static char **term_get_fkeys_address;
1359 static KBOARD *term_get_fkeys_kboard;
1360 static Lisp_Object term_get_fkeys_1 (void);
1362 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1363 This function scans the termcap function key sequence entries, and
1364 adds entries to Vinput_decode_map for each function key it finds. */
1366 static void
1367 term_get_fkeys (char **address, KBOARD *kboard)
1369 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1370 errors during the call. The only errors should be from Fdefine_key
1371 when given a key sequence containing an invalid prefix key. If the
1372 termcap defines function keys which use a prefix that is already bound
1373 to a command by the default bindings, we should silently ignore that
1374 function key specification, rather than giving the user an error and
1375 refusing to run at all on such a terminal. */
1377 term_get_fkeys_address = address;
1378 term_get_fkeys_kboard = kboard;
1379 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1382 static Lisp_Object
1383 term_get_fkeys_1 (void)
1385 int i;
1387 char **address = term_get_fkeys_address;
1388 KBOARD *kboard = term_get_fkeys_kboard;
1390 /* This can happen if CANNOT_DUMP or with strange options. */
1391 if (!KEYMAPP (kboard->Vinput_decode_map))
1392 kboard->Vinput_decode_map = Fmake_sparse_keymap (Qnil);
1394 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1396 char *sequence = tgetstr (keys[i].cap, address);
1397 if (sequence)
1398 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
1399 Fmake_vector (make_number (1),
1400 intern (keys[i].name)));
1403 /* The uses of the "k0" capability are inconsistent; sometimes it
1404 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1405 We will attempt to politely accommodate both systems by testing for
1406 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1409 char *k_semi = tgetstr ("k;", address);
1410 char *k0 = tgetstr ("k0", address);
1411 char *k0_name = "f10";
1413 if (k_semi)
1415 if (k0)
1416 /* Define f0 first, so that f10 takes precedence in case the
1417 key sequences happens to be the same. */
1418 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
1419 Fmake_vector (make_number (1), intern ("f0")));
1420 Fdefine_key (kboard->Vinput_decode_map, build_string (k_semi),
1421 Fmake_vector (make_number (1), intern ("f10")));
1423 else if (k0)
1424 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
1425 Fmake_vector (make_number (1), intern (k0_name)));
1428 /* Set up cookies for numbered function keys above f10. */
1430 char fcap[3], fkey[4];
1432 fcap[0] = 'F'; fcap[2] = '\0';
1433 for (i = 11; i < 64; i++)
1435 if (i <= 19)
1436 fcap[1] = '1' + i - 11;
1437 else if (i <= 45)
1438 fcap[1] = 'A' + i - 20;
1439 else
1440 fcap[1] = 'a' + i - 46;
1443 char *sequence = tgetstr (fcap, address);
1444 if (sequence)
1446 sprintf (fkey, "f%d", i);
1447 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
1448 Fmake_vector (make_number (1),
1449 intern (fkey)));
1456 * Various mappings to try and get a better fit.
1459 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1460 if (!tgetstr (cap1, address)) \
1462 char *sequence = tgetstr (cap2, address); \
1463 if (sequence) \
1464 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence), \
1465 Fmake_vector (make_number (1), \
1466 intern (sym))); \
1469 /* if there's no key_next keycap, map key_npage to `next' keysym */
1470 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1471 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1472 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1473 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1474 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1475 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1476 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1478 /* IBM has their own non-standard dialect of terminfo.
1479 If the standard name isn't found, try the IBM name. */
1480 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1481 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1482 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1483 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1484 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1485 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1486 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1487 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1488 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1489 #undef CONDITIONAL_REASSIGN
1492 return Qnil;
1494 #endif /* not DOS_NT */
1497 /***********************************************************************
1498 Character Display Information
1499 ***********************************************************************/
1500 static void append_glyph (struct it *);
1501 static void produce_stretch_glyph (struct it *);
1502 static void append_composite_glyph (struct it *);
1503 static void produce_composite_glyph (struct it *);
1505 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1506 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1507 the character for which to produce glyphs; IT->face_id contains the
1508 character's face. Padding glyphs are appended if IT->c has a
1509 IT->pixel_width > 1. */
1511 static void
1512 append_glyph (struct it *it)
1514 struct glyph *glyph, *end;
1515 int i;
1517 xassert (it->glyph_row);
1518 glyph = (it->glyph_row->glyphs[it->area]
1519 + it->glyph_row->used[it->area]);
1520 end = it->glyph_row->glyphs[1 + it->area];
1522 /* If the glyph row is reversed, we need to prepend the glyph rather
1523 than append it. */
1524 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1526 struct glyph *g;
1527 int move_by = it->pixel_width;
1529 /* Make room for the new glyphs. */
1530 if (move_by > end - glyph) /* don't overstep end of this area */
1531 move_by = end - glyph;
1532 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1533 g[move_by] = *g;
1534 glyph = it->glyph_row->glyphs[it->area];
1535 end = glyph + move_by;
1538 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1539 right, even in the REVERSED_P case, since (a) all of its u.ch are
1540 identical, and (b) the PADDING_P flag needs to be set for the
1541 leftmost one, because we write to the terminal left-to-right. */
1542 for (i = 0;
1543 i < it->pixel_width && glyph < end;
1544 ++i)
1546 glyph->type = CHAR_GLYPH;
1547 glyph->pixel_width = 1;
1548 glyph->u.ch = it->char_to_display;
1549 glyph->face_id = it->face_id;
1550 glyph->padding_p = i > 0;
1551 glyph->charpos = CHARPOS (it->position);
1552 glyph->object = it->object;
1553 if (it->bidi_p)
1555 glyph->resolved_level = it->bidi_it.resolved_level;
1556 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1557 abort ();
1558 glyph->bidi_type = it->bidi_it.type;
1560 else
1562 glyph->resolved_level = 0;
1563 glyph->bidi_type = UNKNOWN_BT;
1566 ++it->glyph_row->used[it->area];
1567 ++glyph;
1571 /* Produce glyphs for the display element described by IT. *IT
1572 specifies what we want to produce a glyph for (character, image, ...),
1573 and where in the glyph matrix we currently are (glyph row and hpos).
1574 produce_glyphs fills in output fields of *IT with information such as the
1575 pixel width and height of a character, and maybe output actual glyphs at
1576 the same time if IT->glyph_row is non-null. For an overview, see
1577 the explanation in dispextern.h, before the definition of the
1578 display_element_type enumeration.
1580 produce_glyphs also stores the result of glyph width, ascent
1581 etc. computations in *IT.
1583 IT->glyph_row may be null, in which case produce_glyphs does not
1584 actually fill in the glyphs. This is used in the move_* functions
1585 in xdisp.c for text width and height computations.
1587 Callers usually don't call produce_glyphs directly;
1588 instead they use the macro PRODUCE_GLYPHS. */
1590 void
1591 produce_glyphs (struct it *it)
1593 /* If a hook is installed, let it do the work. */
1595 /* Nothing but characters are supported on terminal frames. */
1596 xassert (it->what == IT_CHARACTER
1597 || it->what == IT_COMPOSITION
1598 || it->what == IT_STRETCH);
1600 if (it->what == IT_STRETCH)
1602 produce_stretch_glyph (it);
1603 goto done;
1606 if (it->what == IT_COMPOSITION)
1608 produce_composite_glyph (it);
1609 goto done;
1612 if (it->char_to_display >= 040 && it->char_to_display < 0177)
1614 it->pixel_width = it->nglyphs = 1;
1615 if (it->glyph_row)
1616 append_glyph (it);
1618 else if (it->char_to_display == '\n')
1619 it->pixel_width = it->nglyphs = 0;
1620 else if (it->char_to_display == '\t')
1622 int absolute_x = (it->current_x
1623 + it->continuation_lines_width);
1624 int next_tab_x
1625 = (((1 + absolute_x + it->tab_width - 1)
1626 / it->tab_width)
1627 * it->tab_width);
1628 int nspaces;
1630 /* If part of the TAB has been displayed on the previous line
1631 which is continued now, continuation_lines_width will have
1632 been incremented already by the part that fitted on the
1633 continued line. So, we will get the right number of spaces
1634 here. */
1635 nspaces = next_tab_x - absolute_x;
1637 if (it->glyph_row)
1639 int n = nspaces;
1641 it->char_to_display = ' ';
1642 it->pixel_width = it->len = 1;
1644 while (n--)
1645 append_glyph (it);
1648 it->pixel_width = nspaces;
1649 it->nglyphs = nspaces;
1651 else if (CHAR_BYTE8_P (it->char_to_display))
1653 /* Coming here means that we must send the raw 8-bit byte as is
1654 to the terminal. Although there's no way to know how many
1655 columns it occupies on a screen, it is a good assumption that
1656 a single byte code has 1-column width. */
1657 it->pixel_width = it->nglyphs = 1;
1658 if (it->glyph_row)
1659 append_glyph (it);
1661 else
1663 it->pixel_width = CHAR_WIDTH (it->char_to_display);
1664 it->nglyphs = it->pixel_width;
1666 if (it->glyph_row)
1667 append_glyph (it);
1670 done:
1671 /* Advance current_x by the pixel width as a convenience for
1672 the caller. */
1673 if (it->area == TEXT_AREA)
1674 it->current_x += it->pixel_width;
1675 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1676 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1680 /* Produce a stretch glyph for iterator IT. IT->object is the value
1681 of the glyph property displayed. The value must be a list
1682 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1683 being recognized:
1685 1. `:width WIDTH' specifies that the space should be WIDTH *
1686 canonical char width wide. WIDTH may be an integer or floating
1687 point number.
1689 2. `:align-to HPOS' specifies that the space should be wide enough
1690 to reach HPOS, a value in canonical character units. */
1692 static void
1693 produce_stretch_glyph (struct it *it)
1695 /* (space :width WIDTH ...) */
1696 Lisp_Object prop, plist;
1697 int width = 0, align_to = -1;
1698 int zero_width_ok_p = 0;
1699 double tem;
1701 /* List should start with `space'. */
1702 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1703 plist = XCDR (it->object);
1705 /* Compute the width of the stretch. */
1706 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1707 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1709 /* Absolute width `:width WIDTH' specified and valid. */
1710 zero_width_ok_p = 1;
1711 width = (int)(tem + 0.5);
1713 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1714 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1716 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1717 align_to = (align_to < 0
1719 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1720 else if (align_to < 0)
1721 align_to = window_box_left_offset (it->w, TEXT_AREA);
1722 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1723 zero_width_ok_p = 1;
1725 else
1726 /* Nothing specified -> width defaults to canonical char width. */
1727 width = FRAME_COLUMN_WIDTH (it->f);
1729 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1730 width = 1;
1732 if (width > 0 && it->line_wrap != TRUNCATE
1733 && it->current_x + width > it->last_visible_x)
1734 width = it->last_visible_x - it->current_x - 1;
1736 if (width > 0 && it->glyph_row)
1738 Lisp_Object o_object = it->object;
1739 Lisp_Object object = it->stack[it->sp - 1].string;
1740 int n = width;
1742 if (!STRINGP (object))
1743 object = it->w->buffer;
1744 it->object = object;
1745 it->char_to_display = ' ';
1746 it->pixel_width = it->len = 1;
1747 while (n--)
1748 append_glyph (it);
1749 it->object = o_object;
1751 it->pixel_width = width;
1752 it->nglyphs = width;
1756 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1757 Called from produce_composite_glyph for terminal frames if
1758 IT->glyph_row != NULL. IT->face_id contains the character's
1759 face. */
1761 static void
1762 append_composite_glyph (struct it *it)
1764 struct glyph *glyph;
1766 xassert (it->glyph_row);
1767 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1768 if (glyph < it->glyph_row->glyphs[1 + it->area])
1770 /* If the glyph row is reversed, we need to prepend the glyph
1771 rather than append it. */
1772 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1774 struct glyph *g;
1776 /* Make room for the new glyph. */
1777 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1778 g[1] = *g;
1779 glyph = it->glyph_row->glyphs[it->area];
1781 glyph->type = COMPOSITE_GLYPH;
1782 glyph->pixel_width = it->pixel_width;
1783 glyph->u.cmp.id = it->cmp_it.id;
1784 if (it->cmp_it.ch < 0)
1786 glyph->u.cmp.automatic = 0;
1787 glyph->u.cmp.id = it->cmp_it.id;
1789 else
1791 glyph->u.cmp.automatic = 1;
1792 glyph->u.cmp.id = it->cmp_it.id;
1793 glyph->slice.cmp.from = it->cmp_it.from;
1794 glyph->slice.cmp.to = it->cmp_it.to - 1;
1797 glyph->face_id = it->face_id;
1798 glyph->padding_p = 0;
1799 glyph->charpos = CHARPOS (it->position);
1800 glyph->object = it->object;
1801 if (it->bidi_p)
1803 glyph->resolved_level = it->bidi_it.resolved_level;
1804 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1805 abort ();
1806 glyph->bidi_type = it->bidi_it.type;
1808 else
1810 glyph->resolved_level = 0;
1811 glyph->bidi_type = UNKNOWN_BT;
1814 ++it->glyph_row->used[it->area];
1815 ++glyph;
1820 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1821 the composition. We simply produces components of the composition
1822 assuming that the terminal has a capability to layout/render it
1823 correctly. */
1825 static void
1826 produce_composite_glyph (struct it *it)
1828 if (it->cmp_it.ch < 0)
1830 struct composition *cmp = composition_table[it->cmp_it.id];
1832 it->pixel_width = cmp->width;
1834 else
1836 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
1838 it->pixel_width = composition_gstring_width (gstring, it->cmp_it.from,
1839 it->cmp_it.to, NULL);
1841 it->nglyphs = 1;
1842 if (it->glyph_row)
1843 append_composite_glyph (it);
1847 /* Get information about special display element WHAT in an
1848 environment described by IT. WHAT is one of IT_TRUNCATION or
1849 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1850 non-null glyph_row member. This function ensures that fields like
1851 face_id, c, len of IT are left untouched. */
1853 void
1854 produce_special_glyphs (struct it *it, enum display_element_type what)
1856 struct it temp_it;
1857 Lisp_Object gc;
1858 GLYPH glyph;
1860 temp_it = *it;
1861 temp_it.dp = NULL;
1862 temp_it.what = IT_CHARACTER;
1863 temp_it.len = 1;
1864 temp_it.object = make_number (0);
1865 memset (&temp_it.current, 0, sizeof temp_it.current);
1867 if (what == IT_CONTINUATION)
1869 /* Continuation glyph. For R2L lines, we mirror it by hand. */
1870 if (it->bidi_it.paragraph_dir == R2L)
1871 SET_GLYPH_FROM_CHAR (glyph, '/');
1872 else
1873 SET_GLYPH_FROM_CHAR (glyph, '\\');
1874 if (it->dp
1875 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc))
1876 && GLYPH_CODE_CHAR_VALID_P (gc))
1878 /* FIXME: Should we mirror GC for R2L lines? */
1879 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1880 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1883 else if (what == IT_TRUNCATION)
1885 /* Truncation glyph. */
1886 SET_GLYPH_FROM_CHAR (glyph, '$');
1887 if (it->dp
1888 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc))
1889 && GLYPH_CODE_CHAR_VALID_P (gc))
1891 /* FIXME: Should we mirror GC for R2L lines? */
1892 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
1893 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
1896 else
1897 abort ();
1899 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
1900 temp_it.face_id = GLYPH_FACE (glyph);
1901 temp_it.len = CHAR_BYTES (temp_it.c);
1903 produce_glyphs (&temp_it);
1904 it->pixel_width = temp_it.pixel_width;
1905 it->nglyphs = temp_it.pixel_width;
1910 /***********************************************************************
1911 Faces
1912 ***********************************************************************/
1914 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1915 one of the enumerators from enum no_color_bit, or a bit set built
1916 from them. Some display attributes may not be used together with
1917 color; the termcap capability `NC' specifies which ones. */
1919 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1920 (tty->TN_max_colors > 0 \
1921 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1922 : 1)
1924 /* Turn appearances of face FACE_ID on tty frame F on.
1925 FACE_ID is a realized face ID number, in the face cache. */
1927 static void
1928 turn_on_face (struct frame *f, int face_id)
1930 struct face *face = FACE_FROM_ID (f, face_id);
1931 long fg = face->foreground;
1932 long bg = face->background;
1933 struct tty_display_info *tty = FRAME_TTY (f);
1935 /* Do this first because TS_end_standout_mode may be the same
1936 as TS_exit_attribute_mode, which turns all appearances off. */
1937 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1939 if (tty->TN_max_colors > 0)
1941 if (fg >= 0 && bg >= 0)
1943 /* If the terminal supports colors, we can set them
1944 below without using reverse video. The face's fg
1945 and bg colors are set as they should appear on
1946 the screen, i.e. they take the inverse-video'ness
1947 of the face already into account. */
1949 else if (inverse_video)
1951 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1952 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1953 tty_toggle_highlight (tty);
1955 else
1957 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1958 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1959 tty_toggle_highlight (tty);
1962 else
1964 /* If we can't display colors, use reverse video
1965 if the face specifies that. */
1966 if (inverse_video)
1968 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1969 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1970 tty_toggle_highlight (tty);
1972 else
1974 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1975 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1976 tty_toggle_highlight (tty);
1981 if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1982 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
1984 if (face->tty_dim_p && MAY_USE_WITH_COLORS_P (tty, NC_DIM))
1985 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
1987 /* Alternate charset and blinking not yet used. */
1988 if (face->tty_alt_charset_p
1989 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
1990 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
1992 if (face->tty_blinking_p
1993 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
1994 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
1996 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
1997 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
1999 if (tty->TN_max_colors > 0)
2001 char *ts, *p;
2003 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
2004 if (fg >= 0 && ts)
2006 p = tparam (ts, NULL, 0, (int) fg);
2007 OUTPUT (tty, p);
2008 xfree (p);
2011 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
2012 if (bg >= 0 && ts)
2014 p = tparam (ts, NULL, 0, (int) bg);
2015 OUTPUT (tty, p);
2016 xfree (p);
2022 /* Turn off appearances of face FACE_ID on tty frame F. */
2024 static void
2025 turn_off_face (struct frame *f, int face_id)
2027 struct face *face = FACE_FROM_ID (f, face_id);
2028 struct tty_display_info *tty = FRAME_TTY (f);
2030 xassert (face != NULL);
2032 if (tty->TS_exit_attribute_mode)
2034 /* Capability "me" will turn off appearance modes double-bright,
2035 half-bright, reverse-video, standout, underline. It may or
2036 may not turn off alt-char-mode. */
2037 if (face->tty_bold_p
2038 || face->tty_dim_p
2039 || face->tty_reverse_p
2040 || face->tty_alt_charset_p
2041 || face->tty_blinking_p
2042 || face->tty_underline_p)
2044 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
2045 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
2046 tty->standout_mode = 0;
2049 if (face->tty_alt_charset_p)
2050 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2052 else
2054 /* If we don't have "me" we can only have those appearances
2055 that have exit sequences defined. */
2056 if (face->tty_alt_charset_p)
2057 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
2059 if (face->tty_underline_p)
2060 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
2063 /* Switch back to default colors. */
2064 if (tty->TN_max_colors > 0
2065 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2066 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2067 || (face->background != FACE_TTY_DEFAULT_COLOR
2068 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
2069 OUTPUT1_IF (tty, tty->TS_orig_pair);
2073 /* Return non-zero if the terminal on frame F supports all of the
2074 capabilities in CAPS simultaneously, with foreground and background
2075 colors FG and BG. */
2078 tty_capable_p (struct tty_display_info *tty, unsigned int caps,
2079 unsigned long fg, unsigned long bg)
2081 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2082 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2083 return 0;
2085 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2086 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2087 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2088 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
2089 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
2090 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
2092 /* We can do it! */
2093 return 1;
2096 /* Return non-zero if the terminal is capable to display colors. */
2098 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2099 0, 1, 0,
2100 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2102 TERMINAL can be a terminal object, a frame, or nil (meaning the
2103 selected frame's terminal). This function always returns nil if
2104 TERMINAL does not refer to a text-only terminal. */)
2105 (Lisp_Object terminal)
2107 struct terminal *t = get_tty_terminal (terminal, 0);
2108 if (!t)
2109 return Qnil;
2110 else
2111 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
2114 /* Return the number of supported colors. */
2115 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2116 Stty_display_color_cells, 0, 1, 0,
2117 doc: /* Return the number of colors supported by the tty device TERMINAL.
2119 TERMINAL can be a terminal object, a frame, or nil (meaning the
2120 selected frame's terminal). This function always returns 0 if
2121 TERMINAL does not refer to a text-only terminal. */)
2122 (Lisp_Object terminal)
2124 struct terminal *t = get_tty_terminal (terminal, 0);
2125 if (!t)
2126 return make_number (0);
2127 else
2128 return make_number (t->display_info.tty->TN_max_colors);
2131 #ifndef DOS_NT
2133 /* Declare here rather than in the function, as in the rest of Emacs,
2134 to work around an HPUX compiler bug (?). See
2135 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2136 static int default_max_colors;
2137 static int default_max_pairs;
2138 static int default_no_color_video;
2139 static char *default_orig_pair;
2140 static char *default_set_foreground;
2141 static char *default_set_background;
2143 /* Save or restore the default color-related capabilities of this
2144 terminal. */
2145 static void
2146 tty_default_color_capabilities (struct tty_display_info *tty, int save)
2149 if (save)
2151 xfree (default_orig_pair);
2152 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
2154 xfree (default_set_foreground);
2155 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
2156 : NULL;
2158 xfree (default_set_background);
2159 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
2160 : NULL;
2162 default_max_colors = tty->TN_max_colors;
2163 default_max_pairs = tty->TN_max_pairs;
2164 default_no_color_video = tty->TN_no_color_video;
2166 else
2168 tty->TS_orig_pair = default_orig_pair;
2169 tty->TS_set_foreground = default_set_foreground;
2170 tty->TS_set_background = default_set_background;
2171 tty->TN_max_colors = default_max_colors;
2172 tty->TN_max_pairs = default_max_pairs;
2173 tty->TN_no_color_video = default_no_color_video;
2177 /* Setup one of the standard tty color schemes according to MODE.
2178 MODE's value is generally the number of colors which we want to
2179 support; zero means set up for the default capabilities, the ones
2180 we saw at init_tty time; -1 means turn off color support. */
2181 static void
2182 tty_setup_colors (struct tty_display_info *tty, int mode)
2184 /* Canonicalize all negative values of MODE. */
2185 if (mode < -1)
2186 mode = -1;
2188 switch (mode)
2190 case -1: /* no colors at all */
2191 tty->TN_max_colors = 0;
2192 tty->TN_max_pairs = 0;
2193 tty->TN_no_color_video = 0;
2194 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2195 break;
2196 case 0: /* default colors, if any */
2197 default:
2198 tty_default_color_capabilities (tty, 0);
2199 break;
2200 case 8: /* 8 standard ANSI colors */
2201 tty->TS_orig_pair = "\033[0m";
2202 #ifdef TERMINFO
2203 tty->TS_set_foreground = "\033[3%p1%dm";
2204 tty->TS_set_background = "\033[4%p1%dm";
2205 #else
2206 tty->TS_set_foreground = "\033[3%dm";
2207 tty->TS_set_background = "\033[4%dm";
2208 #endif
2209 tty->TN_max_colors = 8;
2210 tty->TN_max_pairs = 64;
2211 tty->TN_no_color_video = 0;
2212 break;
2216 void
2217 set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
2219 Lisp_Object tem, val;
2220 Lisp_Object color_mode;
2221 int mode;
2222 Lisp_Object tty_color_mode_alist
2223 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
2225 tem = assq_no_quit (Qtty_color_mode, f->param_alist);
2226 val = CONSP (tem) ? XCDR (tem) : Qnil;
2228 if (INTEGERP (val))
2229 color_mode = val;
2230 else if (SYMBOLP (tty_color_mode_alist))
2232 tem = Fassq (val, Fsymbol_value (tty_color_mode_alist));
2233 color_mode = CONSP (tem) ? XCDR (tem) : Qnil;
2235 else
2236 color_mode = Qnil;
2238 mode = INTEGERP (color_mode) ? XINT (color_mode) : 0;
2240 if (mode != tty->previous_color_mode)
2242 Lisp_Object funsym = intern ("tty-set-up-initial-frame-faces");
2243 tty->previous_color_mode = mode;
2244 tty_setup_colors (tty , mode);
2245 /* This recomputes all the faces given the new color definitions. */
2246 safe_call (1, &funsym);
2250 #endif /* !DOS_NT */
2254 /* Return the tty display object specified by TERMINAL. */
2256 struct terminal *
2257 get_tty_terminal (Lisp_Object terminal, int throw)
2259 struct terminal *t = get_terminal (terminal, throw);
2261 if (t && t->type != output_termcap && t->type != output_msdos_raw)
2263 if (throw)
2264 error ("Device %d is not a termcap terminal device", t->id);
2265 else
2266 return NULL;
2269 return t;
2272 /* Return an active termcap device that uses the tty device with the
2273 given name.
2275 This function ignores suspended devices.
2277 Returns NULL if the named terminal device is not opened. */
2279 struct terminal *
2280 get_named_tty (const char *name)
2282 struct terminal *t;
2284 if (!name)
2285 abort ();
2287 for (t = terminal_list; t; t = t->next_terminal)
2289 if ((t->type == output_termcap || t->type == output_msdos_raw)
2290 && !strcmp (t->display_info.tty->name, name)
2291 && TERMINAL_ACTIVE_P (t))
2292 return t;
2295 return 0;
2299 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2300 doc: /* Return the type of the tty device that TERMINAL uses.
2301 Returns nil if TERMINAL is not on a tty device.
2303 TERMINAL can be a terminal object, a frame, or nil (meaning the
2304 selected frame's terminal). */)
2305 (Lisp_Object terminal)
2307 struct terminal *t = get_terminal (terminal, 1);
2309 if (t->type != output_termcap && t->type != output_msdos_raw)
2310 return Qnil;
2312 if (t->display_info.tty->type)
2313 return build_string (t->display_info.tty->type);
2314 else
2315 return Qnil;
2318 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2319 doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2321 TERMINAL can be a terminal object, a frame, or nil (meaning the
2322 selected frame's terminal). This function always returns nil if
2323 TERMINAL is not on a tty device. */)
2324 (Lisp_Object terminal)
2326 struct terminal *t = get_terminal (terminal, 1);
2328 if ((t->type != output_termcap && t->type != output_msdos_raw)
2329 || strcmp (t->display_info.tty->name, DEV_TTY) != 0)
2330 return Qnil;
2331 else
2332 return Qt;
2335 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2336 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2337 This is used to override the terminfo data, for certain terminals that
2338 do not really do underlining, but say that they do. This function has
2339 no effect if used on a non-tty terminal.
2341 TERMINAL can be a terminal object, a frame or nil (meaning the
2342 selected frame's terminal). This function always returns nil if
2343 TERMINAL does not refer to a text-only terminal. */)
2344 (Lisp_Object terminal)
2346 struct terminal *t = get_terminal (terminal, 1);
2348 if (t->type == output_termcap)
2349 t->display_info.tty->TS_enter_underline_mode = 0;
2350 return Qnil;
2355 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2356 doc: /* Suspend the terminal device TTY.
2358 The device is restored to its default state, and Emacs ceases all
2359 access to the tty device. Frames that use the device are not deleted,
2360 but input is not read from them and if they change, their display is
2361 not updated.
2363 TTY may be a terminal object, a frame, or nil for the terminal device
2364 of the currently selected frame.
2366 This function runs `suspend-tty-functions' after suspending the
2367 device. The functions are run with one arg, the id of the suspended
2368 terminal device.
2370 `suspend-tty' does nothing if it is called on a device that is already
2371 suspended.
2373 A suspended tty may be resumed by calling `resume-tty' on it. */)
2374 (Lisp_Object tty)
2376 struct terminal *t = get_tty_terminal (tty, 1);
2377 FILE *f;
2379 if (!t)
2380 error ("Unknown tty device");
2382 f = t->display_info.tty->input;
2384 if (f)
2386 /* First run `suspend-tty-functions' and then clean up the tty
2387 state because `suspend-tty-functions' might need to change
2388 the tty state. */
2389 if (!NILP (Vrun_hooks))
2391 Lisp_Object args[2];
2392 args[0] = intern ("suspend-tty-functions");
2393 XSETTERMINAL (args[1], t);
2394 Frun_hook_with_args (2, args);
2397 reset_sys_modes (t->display_info.tty);
2398 delete_keyboard_wait_descriptor (fileno (f));
2400 #ifndef MSDOS
2401 fclose (f);
2402 if (f != t->display_info.tty->output)
2403 fclose (t->display_info.tty->output);
2404 #endif
2406 t->display_info.tty->input = 0;
2407 t->display_info.tty->output = 0;
2409 if (FRAMEP (t->display_info.tty->top_frame))
2410 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2414 /* Clear display hooks to prevent further output. */
2415 clear_tty_hooks (t);
2417 return Qnil;
2420 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2421 doc: /* Resume the previously suspended terminal device TTY.
2422 The terminal is opened and reinitialized. Frames that are on the
2423 suspended terminal are revived.
2425 It is an error to resume a terminal while another terminal is active
2426 on the same device.
2428 This function runs `resume-tty-functions' after resuming the terminal.
2429 The functions are run with one arg, the id of the resumed terminal
2430 device.
2432 `resume-tty' does nothing if it is called on a device that is not
2433 suspended.
2435 TTY may be a terminal object, a frame, or nil (meaning the selected
2436 frame's terminal). */)
2437 (Lisp_Object tty)
2439 struct terminal *t = get_tty_terminal (tty, 1);
2440 int fd;
2442 if (!t)
2443 error ("Unknown tty device");
2445 if (!t->display_info.tty->input)
2447 if (get_named_tty (t->display_info.tty->name))
2448 error ("Cannot resume display while another display is active on the same device");
2450 #ifdef MSDOS
2451 t->display_info.tty->output = stdout;
2452 t->display_info.tty->input = stdin;
2453 #else /* !MSDOS */
2454 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2456 if (fd == -1)
2457 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2459 if (strcmp (t->display_info.tty->name, DEV_TTY))
2460 dissociate_if_controlling_tty (fd);
2462 t->display_info.tty->output = fdopen (fd, "w+");
2463 t->display_info.tty->input = t->display_info.tty->output;
2464 #endif
2466 add_keyboard_wait_descriptor (fd);
2468 if (FRAMEP (t->display_info.tty->top_frame))
2470 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2471 int width, height;
2472 int old_height = FRAME_COLS (f);
2473 int old_width = FRAME_LINES (f);
2475 /* Check if terminal/window size has changed while the frame
2476 was suspended. */
2477 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2478 if (width != old_width || height != old_height)
2479 change_frame_size (f, height, width, 0, 0, 0);
2480 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2483 init_sys_modes (t->display_info.tty);
2485 /* Run `resume-tty-functions'. */
2486 if (!NILP (Vrun_hooks))
2488 Lisp_Object args[2];
2489 args[0] = intern ("resume-tty-functions");
2490 XSETTERMINAL (args[1], t);
2491 Frun_hook_with_args (2, args);
2495 set_tty_hooks (t);
2497 return Qnil;
2501 /***********************************************************************
2502 Mouse
2503 ***********************************************************************/
2505 #ifdef HAVE_GPM
2506 void
2507 term_mouse_moveto (int x, int y)
2509 /* TODO: how to set mouse position?
2510 const char *name;
2511 int fd;
2512 name = (const char *) ttyname (0);
2513 fd = open (name, O_WRONLY);
2514 SOME_FUNCTION (x, y, fd);
2515 close (fd);
2516 last_mouse_x = x;
2517 last_mouse_y = y; */
2520 static void
2521 term_show_mouse_face (enum draw_glyphs_face draw)
2523 struct window *w = XWINDOW (mouse_face_window);
2524 int save_x, save_y;
2525 int i;
2527 struct frame *f = XFRAME (w->frame);
2528 struct tty_display_info *tty = FRAME_TTY (f);
2530 if (/* If window is in the process of being destroyed, don't bother
2531 to do anything. */
2532 w->current_matrix != NULL
2533 /* Recognize when we are called to operate on rows that don't exist
2534 anymore. This can happen when a window is split. */
2535 && mouse_face_end_row < w->current_matrix->nrows)
2537 /* write_glyphs writes at cursor position, so we need to
2538 temporarily move cursor coordinates to the beginning of
2539 the highlight region. */
2541 /* Save current cursor co-ordinates */
2542 save_y = curY (tty);
2543 save_x = curX (tty);
2545 /* Note that mouse_face_beg_row etc. are window relative. */
2546 for (i = mouse_face_beg_row; i <= mouse_face_end_row; i++)
2548 int start_hpos, end_hpos, nglyphs;
2549 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
2551 /* Don't do anything if row doesn't have valid contents. */
2552 if (!row->enabled_p)
2553 continue;
2555 /* For all but the first row, the highlight starts at column 0. */
2556 if (i == mouse_face_beg_row)
2557 start_hpos = mouse_face_beg_col;
2558 else
2559 start_hpos = 0;
2561 if (i == mouse_face_end_row)
2562 end_hpos = mouse_face_end_col;
2563 else
2565 end_hpos = row->used[TEXT_AREA];
2566 if (draw == DRAW_NORMAL_TEXT)
2567 row->fill_line_p = 1; /* Clear to end of line */
2570 if (end_hpos <= start_hpos)
2571 continue;
2572 /* Record that some glyphs of this row are displayed in
2573 mouse-face. */
2574 row->mouse_face_p = draw > 0;
2576 nglyphs = end_hpos - start_hpos;
2578 if (end_hpos >= row->used[TEXT_AREA])
2579 nglyphs = row->used[TEXT_AREA] - start_hpos;
2581 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2582 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos
2583 + WINDOW_LEFT_EDGE_X (w);
2585 cursor_to (f, pos_y, pos_x);
2587 if (draw == DRAW_MOUSE_FACE)
2589 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2590 nglyphs, mouse_face_face_id);
2592 else /* draw == DRAW_NORMAL_TEXT */
2593 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
2595 cursor_to (f, save_y, save_x);
2599 static void
2600 term_clear_mouse_face (void)
2602 if (!NILP (mouse_face_window))
2603 term_show_mouse_face (DRAW_NORMAL_TEXT);
2605 mouse_face_beg_row = mouse_face_beg_col = -1;
2606 mouse_face_end_row = mouse_face_end_col = -1;
2607 mouse_face_window = Qnil;
2610 /* Find the glyph matrix position of buffer position POS in window W.
2611 *HPOS and *VPOS are set to the positions found. W's current glyphs
2612 must be up to date. If POS is above window start return (0, 0).
2613 If POS is after end of W, return end of last line in W.
2614 - taken from msdos.c */
2615 static int
2616 fast_find_position (struct window *w, EMACS_INT pos, int *hpos, int *vpos)
2618 int i, lastcol, maybe_next_line_p = 0;
2619 EMACS_INT line_start_position;
2620 int yb = window_text_bottom_y (w);
2621 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0), *best_row = row;
2623 while (row->y < yb)
2625 if (row->used[TEXT_AREA])
2626 line_start_position = row->glyphs[TEXT_AREA]->charpos;
2627 else
2628 line_start_position = 0;
2630 if (line_start_position > pos)
2631 break;
2632 /* If the position sought is the end of the buffer,
2633 don't include the blank lines at the bottom of the window. */
2634 else if (line_start_position == pos
2635 && pos == BUF_ZV (XBUFFER (w->buffer)))
2637 maybe_next_line_p = 1;
2638 break;
2640 else if (line_start_position > 0)
2641 best_row = row;
2643 /* Don't overstep the last matrix row, lest we get into the
2644 never-never land... */
2645 if (row->y + 1 >= yb)
2646 break;
2648 ++row;
2651 /* Find the right column within BEST_ROW. */
2652 lastcol = 0;
2653 row = best_row;
2654 for (i = 0; i < row->used[TEXT_AREA]; i++)
2656 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
2657 EMACS_INT charpos;
2659 charpos = glyph->charpos;
2660 if (charpos == pos)
2662 *hpos = i;
2663 *vpos = row->y;
2664 return 1;
2666 else if (charpos > pos)
2667 break;
2668 else if (charpos > 0)
2669 lastcol = i;
2672 /* If we're looking for the end of the buffer,
2673 and we didn't find it in the line we scanned,
2674 use the start of the following line. */
2675 if (maybe_next_line_p)
2677 ++row;
2678 lastcol = 0;
2681 *vpos = row->y;
2682 *hpos = lastcol + 1;
2683 return 0;
2686 static void
2687 term_mouse_highlight (struct frame *f, int x, int y)
2689 enum window_part part;
2690 Lisp_Object window;
2691 struct window *w;
2692 struct buffer *b;
2694 if (NILP (Vmouse_highlight)
2695 || !f->glyphs_initialized_p)
2696 return;
2698 /* Which window is that in? */
2699 window = window_from_coordinates (f, x, y, &part, &x, &y, 0);
2701 /* Not on a window -> return. */
2702 if (!WINDOWP (window))
2703 return;
2705 if (!EQ (window, mouse_face_window))
2706 term_clear_mouse_face ();
2708 w = XWINDOW (window);
2710 /* Are we in a window whose display is up to date?
2711 And verify the buffer's text has not changed. */
2712 b = XBUFFER (w->buffer);
2713 if (part == ON_TEXT
2714 && EQ (w->window_end_valid, w->buffer)
2715 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
2716 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
2718 int i, nrows = w->current_matrix->nrows;
2719 EMACS_INT pos;
2720 struct glyph_row *row;
2721 struct glyph *glyph;
2723 /* Find the glyph under X/Y. */
2724 glyph = NULL;
2725 if (y >= 0 && y < nrows)
2727 row = MATRIX_ROW (w->current_matrix, y);
2728 /* Give up if some row before the one we are looking for is
2729 not enabled. */
2730 for (i = 0; i <= y; i++)
2731 if (!MATRIX_ROW (w->current_matrix, i)->enabled_p)
2732 break;
2733 if (i > y /* all rows upto and including the one at Y are enabled */
2734 && row->displays_text_p
2735 && x < window_box_width (w, TEXT_AREA))
2737 glyph = row->glyphs[TEXT_AREA];
2738 if (x >= row->used[TEXT_AREA])
2739 glyph = NULL;
2740 else
2742 glyph += x;
2743 if (!BUFFERP (glyph->object))
2744 glyph = NULL;
2749 /* Clear mouse face if X/Y not over text. */
2750 if (glyph == NULL)
2752 term_clear_mouse_face ();
2753 return;
2756 if (!BUFFERP (glyph->object))
2757 abort ();
2758 pos = glyph->charpos;
2760 /* Check for mouse-face. */
2762 Lisp_Object mouse_face, overlay, position, *overlay_vec;
2763 int noverlays;
2764 EMACS_INT obegv, ozv;
2765 struct buffer *obuf;
2767 /* If we get an out-of-range value, return now; avoid an error. */
2768 if (pos > BUF_Z (b))
2769 return;
2771 /* Make the window's buffer temporarily current for
2772 overlays_at and compute_char_face. */
2773 obuf = current_buffer;
2774 current_buffer = b;
2775 obegv = BEGV;
2776 ozv = ZV;
2777 BEGV = BEG;
2778 ZV = Z;
2780 /* Is this char mouse-active? */
2781 XSETINT (position, pos);
2783 /* Put all the overlays we want in a vector in overlay_vec. */
2784 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
2785 /* Sort overlays into increasing priority order. */
2786 noverlays = sort_overlays (overlay_vec, noverlays, w);
2788 /* Check mouse-face highlighting. */
2789 if (!(EQ (window, mouse_face_window)
2790 && y >= mouse_face_beg_row
2791 && y <= mouse_face_end_row
2792 && (y > mouse_face_beg_row
2793 || x >= mouse_face_beg_col)
2794 && (y < mouse_face_end_row
2795 || x < mouse_face_end_col
2796 || mouse_face_past_end)))
2798 /* Clear the display of the old active region, if any. */
2799 term_clear_mouse_face ();
2801 /* Find the highest priority overlay that has a mouse-face
2802 property. */
2803 overlay = Qnil;
2804 for (i = noverlays - 1; i >= 0; --i)
2806 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
2807 if (!NILP (mouse_face))
2809 overlay = overlay_vec[i];
2810 break;
2814 /* If no overlay applies, get a text property. */
2815 if (NILP (overlay))
2816 mouse_face = Fget_text_property (position, Qmouse_face,
2817 w->buffer);
2819 /* Handle the overlay case. */
2820 if (!NILP (overlay))
2822 /* Find the range of text around this char that
2823 should be active. */
2824 Lisp_Object before, after;
2825 EMACS_INT ignore;
2828 before = Foverlay_start (overlay);
2829 after = Foverlay_end (overlay);
2830 /* Record this as the current active region. */
2831 fast_find_position (w, XFASTINT (before),
2832 &mouse_face_beg_col,
2833 &mouse_face_beg_row);
2835 mouse_face_past_end
2836 = !fast_find_position (w, XFASTINT (after),
2837 &mouse_face_end_col,
2838 &mouse_face_end_row);
2839 mouse_face_window = window;
2841 mouse_face_face_id
2842 = face_at_buffer_position (w, pos, 0, 0,
2843 &ignore, pos + 1, 1, -1);
2845 /* Display it as active. */
2846 term_show_mouse_face (DRAW_MOUSE_FACE);
2848 /* Handle the text property case. */
2849 else if (!NILP (mouse_face))
2851 /* Find the range of text around this char that
2852 should be active. */
2853 Lisp_Object before, after, beginning, end;
2854 EMACS_INT ignore;
2856 beginning = Fmarker_position (w->start);
2857 XSETINT (end, (BUF_Z (b) - XFASTINT (w->window_end_pos)));
2858 before
2859 = Fprevious_single_property_change (make_number (pos + 1),
2860 Qmouse_face,
2861 w->buffer, beginning);
2862 after
2863 = Fnext_single_property_change (position, Qmouse_face,
2864 w->buffer, end);
2866 /* Record this as the current active region. */
2867 fast_find_position (w, XFASTINT (before),
2868 &mouse_face_beg_col,
2869 &mouse_face_beg_row);
2870 mouse_face_past_end
2871 = !fast_find_position (w, XFASTINT (after),
2872 &mouse_face_end_col,
2873 &mouse_face_end_row);
2874 mouse_face_window = window;
2876 mouse_face_face_id
2877 = face_at_buffer_position (w, pos, 0, 0,
2878 &ignore, pos + 1, 1, -1);
2880 /* Display it as active. */
2881 term_show_mouse_face (DRAW_MOUSE_FACE);
2885 /* Look for a `help-echo' property. */
2887 Lisp_Object help;
2889 /* Check overlays first. */
2890 help = Qnil;
2891 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
2893 overlay = overlay_vec[i];
2894 help = Foverlay_get (overlay, Qhelp_echo);
2897 if (!NILP (help))
2899 help_echo_string = help;
2900 help_echo_window = window;
2901 help_echo_object = overlay;
2902 help_echo_pos = pos;
2904 /* Try text properties. */
2905 else if (NILP (help)
2906 && ((STRINGP (glyph->object)
2907 && glyph->charpos >= 0
2908 && glyph->charpos < SCHARS (glyph->object))
2909 || (BUFFERP (glyph->object)
2910 && glyph->charpos >= BEGV
2911 && glyph->charpos < ZV)))
2913 help = Fget_text_property (make_number (glyph->charpos),
2914 Qhelp_echo, glyph->object);
2915 if (!NILP (help))
2917 help_echo_string = help;
2918 help_echo_window = window;
2919 help_echo_object = glyph->object;
2920 help_echo_pos = glyph->charpos;
2925 BEGV = obegv;
2926 ZV = ozv;
2927 current_buffer = obuf;
2932 static int
2933 term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2935 /* Has the mouse moved off the glyph it was on at the last sighting? */
2936 if (event->x != last_mouse_x || event->y != last_mouse_y)
2938 frame->mouse_moved = 1;
2939 term_mouse_highlight (frame, event->x, event->y);
2940 /* Remember which glyph we're now on. */
2941 last_mouse_x = event->x;
2942 last_mouse_y = event->y;
2943 return 1;
2945 return 0;
2948 /* Return the current position of the mouse.
2950 Set *f to the frame the mouse is in, or zero if the mouse is in no
2951 Emacs frame. If it is set to zero, all the other arguments are
2952 garbage.
2954 Set *bar_window to Qnil, and *x and *y to the column and
2955 row of the character cell the mouse is over.
2957 Set *time to the time the mouse was at the returned position.
2959 This clears mouse_moved until the next motion
2960 event arrives. */
2961 static void
2962 term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2963 enum scroll_bar_part *part, Lisp_Object *x,
2964 Lisp_Object *y, unsigned long *time)
2966 struct timeval now;
2968 *fp = SELECTED_FRAME ();
2969 (*fp)->mouse_moved = 0;
2971 *bar_window = Qnil;
2972 *part = 0;
2974 XSETINT (*x, last_mouse_x);
2975 XSETINT (*y, last_mouse_y);
2976 gettimeofday(&now, 0);
2977 *time = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2980 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2982 If the event is a button press, then note that we have grabbed
2983 the mouse. */
2985 static Lisp_Object
2986 term_mouse_click (struct input_event *result, Gpm_Event *event,
2987 struct frame *f)
2989 struct timeval now;
2990 int i, j;
2992 result->kind = GPM_CLICK_EVENT;
2993 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2995 if (event->buttons & j) {
2996 result->code = i; /* button number */
2997 break;
3000 gettimeofday(&now, 0);
3001 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
3003 if (event->type & GPM_UP)
3004 result->modifiers = up_modifier;
3005 else if (event->type & GPM_DOWN)
3006 result->modifiers = down_modifier;
3007 else
3008 result->modifiers = 0;
3010 if (event->type & GPM_SINGLE)
3011 result->modifiers |= click_modifier;
3013 if (event->type & GPM_DOUBLE)
3014 result->modifiers |= double_modifier;
3016 if (event->type & GPM_TRIPLE)
3017 result->modifiers |= triple_modifier;
3019 if (event->type & GPM_DRAG)
3020 result->modifiers |= drag_modifier;
3022 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
3024 /* 1 << KG_SHIFT */
3025 if (event->modifiers & (1 << 0))
3026 result->modifiers |= shift_modifier;
3028 /* 1 << KG_CTRL */
3029 if (event->modifiers & (1 << 2))
3030 result->modifiers |= ctrl_modifier;
3032 /* 1 << KG_ALT || KG_ALTGR */
3033 if (event->modifiers & (1 << 3)
3034 || event->modifiers & (1 << 1))
3035 result->modifiers |= meta_modifier;
3038 XSETINT (result->x, event->x);
3039 XSETINT (result->y, event->y);
3040 XSETFRAME (result->frame_or_window, f);
3041 result->arg = Qnil;
3042 return Qnil;
3046 handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
3048 struct frame *f = XFRAME (tty->top_frame);
3049 struct input_event ie;
3050 int do_help = 0;
3051 int count = 0;
3053 EVENT_INIT (ie);
3054 ie.kind = NO_EVENT;
3055 ie.arg = Qnil;
3057 if (event->type & (GPM_MOVE | GPM_DRAG)) {
3058 previous_help_echo_string = help_echo_string;
3059 help_echo_string = Qnil;
3061 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
3063 if (!term_mouse_movement (f, event))
3064 help_echo_string = previous_help_echo_string;
3066 /* If the contents of the global variable help_echo_string
3067 has changed, generate a HELP_EVENT. */
3068 if (!NILP (help_echo_string)
3069 || !NILP (previous_help_echo_string))
3070 do_help = 1;
3072 goto done;
3074 else {
3075 f->mouse_moved = 0;
3076 term_mouse_click (&ie, event, f);
3079 done:
3080 if (ie.kind != NO_EVENT)
3082 kbd_buffer_store_event_hold (&ie, hold_quit);
3083 count++;
3086 if (do_help
3087 && !(hold_quit && hold_quit->kind != NO_EVENT))
3089 Lisp_Object frame;
3091 if (f)
3092 XSETFRAME (frame, f);
3093 else
3094 frame = Qnil;
3096 gen_help_event (help_echo_string, frame, help_echo_window,
3097 help_echo_object, help_echo_pos);
3098 count++;
3101 return count;
3104 DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
3105 0, 0, 0,
3106 doc: /* Open a connection to Gpm.
3107 Gpm-mouse can only be activated for one tty at a time. */)
3108 (void)
3110 struct frame *f = SELECTED_FRAME ();
3111 struct tty_display_info *tty
3112 = ((f)->output_method == output_termcap
3113 ? (f)->terminal->display_info.tty : NULL);
3114 Gpm_Connect connection;
3116 if (!tty)
3117 error ("Gpm-mouse only works in the GNU/Linux console");
3118 if (gpm_tty == tty)
3119 return Qnil; /* Already activated, nothing to do. */
3120 if (gpm_tty)
3121 error ("Gpm-mouse can only be activated for one tty at a time");
3123 connection.eventMask = ~0;
3124 connection.defaultMask = ~GPM_HARD;
3125 connection.maxMod = ~0;
3126 connection.minMod = 0;
3127 gpm_zerobased = 1;
3129 if (Gpm_Open (&connection, 0) < 0)
3130 error ("Gpm-mouse failed to connect to the gpm daemon");
3131 else
3133 gpm_tty = tty;
3134 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
3135 to generate SIGIOs. Apparently we need to call reset_sys_modes
3136 before calling init_sys_modes. */
3137 reset_sys_modes (tty);
3138 init_sys_modes (tty);
3139 add_gpm_wait_descriptor (gpm_fd);
3140 return Qnil;
3144 void
3145 close_gpm (int fd)
3147 if (fd >= 0)
3148 delete_gpm_wait_descriptor (fd);
3149 while (Gpm_Close()); /* close all the stack */
3150 gpm_tty = NULL;
3153 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
3154 0, 0, 0,
3155 doc: /* Close a connection to Gpm. */)
3156 (void)
3158 struct frame *f = SELECTED_FRAME ();
3159 struct tty_display_info *tty
3160 = ((f)->output_method == output_termcap
3161 ? (f)->terminal->display_info.tty : NULL);
3163 if (!tty || gpm_tty != tty)
3164 return Qnil; /* Not activated on this terminal, nothing to do. */
3166 close_gpm (gpm_fd);
3167 return Qnil;
3169 #endif /* HAVE_GPM */
3172 #ifndef MSDOS
3173 /***********************************************************************
3174 Initialization
3175 ***********************************************************************/
3177 /* Initialize the tty-dependent part of frame F. The frame must
3178 already have its device initialized. */
3180 void
3181 create_tty_output (struct frame *f)
3183 struct tty_output *t;
3185 if (! FRAME_TERMCAP_P (f))
3186 abort ();
3188 t = xmalloc (sizeof (struct tty_output));
3189 memset (t, 0, sizeof (struct tty_output));
3191 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
3193 f->output_data.tty = t;
3196 /* Delete frame F's face cache, and its tty-dependent part. */
3198 static void
3199 tty_free_frame_resources (struct frame *f)
3201 if (! FRAME_TERMCAP_P (f))
3202 abort ();
3204 if (FRAME_FACE_CACHE (f))
3205 free_frame_faces (f);
3207 xfree (f->output_data.tty);
3210 #else /* MSDOS */
3212 /* Delete frame F's face cache. */
3214 static void
3215 tty_free_frame_resources (struct frame *f)
3217 if (! FRAME_TERMCAP_P (f) && ! FRAME_MSDOS_P (f))
3218 abort ();
3220 if (FRAME_FACE_CACHE (f))
3221 free_frame_faces (f);
3223 #endif /* MSDOS */
3225 /* Reset the hooks in TERMINAL. */
3227 static void
3228 clear_tty_hooks (struct terminal *terminal)
3230 terminal->rif = 0;
3231 terminal->cursor_to_hook = 0;
3232 terminal->raw_cursor_to_hook = 0;
3233 terminal->clear_to_end_hook = 0;
3234 terminal->clear_frame_hook = 0;
3235 terminal->clear_end_of_line_hook = 0;
3236 terminal->ins_del_lines_hook = 0;
3237 terminal->insert_glyphs_hook = 0;
3238 terminal->write_glyphs_hook = 0;
3239 terminal->delete_glyphs_hook = 0;
3240 terminal->ring_bell_hook = 0;
3241 terminal->reset_terminal_modes_hook = 0;
3242 terminal->set_terminal_modes_hook = 0;
3243 terminal->update_begin_hook = 0;
3244 terminal->update_end_hook = 0;
3245 terminal->set_terminal_window_hook = 0;
3246 terminal->mouse_position_hook = 0;
3247 terminal->frame_rehighlight_hook = 0;
3248 terminal->frame_raise_lower_hook = 0;
3249 terminal->fullscreen_hook = 0;
3250 terminal->set_vertical_scroll_bar_hook = 0;
3251 terminal->condemn_scroll_bars_hook = 0;
3252 terminal->redeem_scroll_bar_hook = 0;
3253 terminal->judge_scroll_bars_hook = 0;
3254 terminal->read_socket_hook = 0;
3255 terminal->frame_up_to_date_hook = 0;
3257 /* Leave these two set, or suspended frames are not deleted
3258 correctly. */
3259 terminal->delete_frame_hook = &tty_free_frame_resources;
3260 terminal->delete_terminal_hook = &delete_tty;
3263 /* Initialize hooks in TERMINAL with the values needed for a tty. */
3265 static void
3266 set_tty_hooks (struct terminal *terminal)
3268 terminal->rif = 0; /* ttys don't support window-based redisplay. */
3270 terminal->cursor_to_hook = &tty_cursor_to;
3271 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
3273 terminal->clear_to_end_hook = &tty_clear_to_end;
3274 terminal->clear_frame_hook = &tty_clear_frame;
3275 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
3277 terminal->ins_del_lines_hook = &tty_ins_del_lines;
3279 terminal->insert_glyphs_hook = &tty_insert_glyphs;
3280 terminal->write_glyphs_hook = &tty_write_glyphs;
3281 terminal->delete_glyphs_hook = &tty_delete_glyphs;
3283 terminal->ring_bell_hook = &tty_ring_bell;
3285 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
3286 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
3287 terminal->update_begin_hook = 0; /* Not needed. */
3288 terminal->update_end_hook = &tty_update_end;
3289 terminal->set_terminal_window_hook = &tty_set_terminal_window;
3291 terminal->mouse_position_hook = 0; /* Not needed. */
3292 terminal->frame_rehighlight_hook = 0; /* Not needed. */
3293 terminal->frame_raise_lower_hook = 0; /* Not needed. */
3295 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
3296 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
3297 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
3298 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
3300 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
3301 terminal->frame_up_to_date_hook = 0; /* Not needed. */
3303 terminal->delete_frame_hook = &tty_free_frame_resources;
3304 terminal->delete_terminal_hook = &delete_tty;
3307 /* Drop the controlling terminal if fd is the same device. */
3308 static void
3309 dissociate_if_controlling_tty (int fd)
3311 #ifndef DOS_NT
3312 int pgid;
3313 EMACS_GET_TTY_PGRP (fd, &pgid); /* If tcgetpgrp succeeds, fd is the ctty. */
3314 if (pgid != -1)
3316 #if defined (USG5)
3317 setpgrp ();
3318 no_controlling_tty = 1;
3319 #elif defined (CYGWIN)
3320 setsid ();
3321 no_controlling_tty = 1;
3322 #else
3323 #ifdef TIOCNOTTY /* Try BSD ioctls. */
3324 sigblock (sigmask (SIGTTOU));
3325 fd = emacs_open (DEV_TTY, O_RDWR, 0);
3326 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
3328 no_controlling_tty = 1;
3330 if (fd != -1)
3331 emacs_close (fd);
3332 sigunblock (sigmask (SIGTTOU));
3333 #else
3334 /* Unknown system. */
3335 croak ();
3336 #endif /* ! TIOCNOTTY */
3337 #endif /* ! USG */
3339 #endif /* !DOS_NT */
3342 /* Create a termcap display on the tty device with the given name and
3343 type.
3345 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3346 Otherwise NAME should be a path to the tty device file,
3347 e.g. "/dev/pts/7".
3349 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3351 If MUST_SUCCEED is true, then all errors are fatal. */
3353 struct terminal *
3354 init_tty (const char *name, const char *terminal_type, int must_succeed)
3356 char *area = NULL;
3357 char **address = &area;
3358 int buffer_size = 4096;
3359 register char *p = NULL;
3360 int status;
3361 struct tty_display_info *tty = NULL;
3362 struct terminal *terminal = NULL;
3363 int ctty = 0; /* 1 if asked to open controlling tty. */
3365 if (!terminal_type)
3366 maybe_fatal (must_succeed, 0,
3367 "Unknown terminal type",
3368 "Unknown terminal type");
3370 if (name == NULL)
3371 name = DEV_TTY;
3372 if (!strcmp (name, DEV_TTY))
3373 ctty = 1;
3375 /* If we already have a terminal on the given device, use that. If
3376 all such terminals are suspended, create a new one instead. */
3377 /* XXX Perhaps this should be made explicit by having init_tty
3378 always create a new terminal and separating terminal and frame
3379 creation on Lisp level. */
3380 terminal = get_named_tty (name);
3381 if (terminal)
3382 return terminal;
3384 terminal = create_terminal ();
3385 #ifdef MSDOS
3386 if (been_here > 0)
3387 maybe_fatal (1, 0, "Attempt to create another terminal %s", "",
3388 name, "");
3389 been_here = 1;
3390 tty = &the_only_display_info;
3391 #else
3392 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
3393 #endif
3394 memset (tty, 0, sizeof (struct tty_display_info));
3395 tty->next = tty_list;
3396 tty_list = tty;
3398 terminal->type = output_termcap;
3399 terminal->display_info.tty = tty;
3400 tty->terminal = terminal;
3402 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3403 Wcm_clear (tty);
3405 encode_terminal_src_size = 0;
3406 encode_terminal_dst_size = 0;
3408 #ifdef HAVE_GPM
3409 terminal->mouse_position_hook = term_mouse_position;
3410 mouse_face_window = Qnil;
3411 #endif
3414 #ifndef DOS_NT
3415 set_tty_hooks (terminal);
3418 int fd;
3419 FILE *file;
3421 #ifdef O_IGNORE_CTTY
3422 if (!ctty)
3423 /* Open the terminal device. Don't recognize it as our
3424 controlling terminal, and don't make it the controlling tty
3425 if we don't have one at the moment. */
3426 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
3427 else
3428 #else
3429 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3430 defined on Hurd. On other systems, we need to explicitly
3431 dissociate ourselves from the controlling tty when we want to
3432 open a frame on the same terminal. */
3433 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
3434 #endif /* O_IGNORE_CTTY */
3436 tty->name = xstrdup (name);
3437 terminal->name = xstrdup (name);
3439 if (fd < 0)
3440 maybe_fatal (must_succeed, terminal,
3441 "Could not open file: %s",
3442 "Could not open file: %s",
3443 name);
3444 if (!isatty (fd))
3446 close (fd);
3447 maybe_fatal (must_succeed, terminal,
3448 "Not a tty device: %s",
3449 "Not a tty device: %s",
3450 name);
3453 #ifndef O_IGNORE_CTTY
3454 if (!ctty)
3455 dissociate_if_controlling_tty (fd);
3456 #endif
3458 file = fdopen (fd, "w+");
3459 tty->input = file;
3460 tty->output = file;
3463 tty->type = xstrdup (terminal_type);
3465 add_keyboard_wait_descriptor (fileno (tty->input));
3467 Wcm_clear (tty);
3469 tty->termcap_term_buffer = (char *) xmalloc (buffer_size);
3471 /* On some systems, tgetent tries to access the controlling
3472 terminal. */
3473 sigblock (sigmask (SIGTTOU));
3474 status = tgetent (tty->termcap_term_buffer, terminal_type);
3475 sigunblock (sigmask (SIGTTOU));
3477 if (status < 0)
3479 #ifdef TERMINFO
3480 maybe_fatal (must_succeed, terminal,
3481 "Cannot open terminfo database file",
3482 "Cannot open terminfo database file");
3483 #else
3484 maybe_fatal (must_succeed, terminal,
3485 "Cannot open termcap database file",
3486 "Cannot open termcap database file");
3487 #endif
3489 if (status == 0)
3491 maybe_fatal (must_succeed, terminal,
3492 "Terminal type %s is not defined",
3493 "Terminal type %s is not defined.\n\
3494 If that is not the actual type of terminal you have,\n\
3495 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3496 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3497 #ifdef TERMINFO
3498 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3499 #else
3500 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3501 #endif
3502 terminal_type);
3505 #ifndef TERMINFO
3506 if (strlen (tty->termcap_term_buffer) >= buffer_size)
3507 abort ();
3508 buffer_size = strlen (tty->termcap_term_buffer);
3509 #endif
3510 tty->termcap_strings_buffer = area = (char *) xmalloc (buffer_size);
3511 tty->TS_ins_line = tgetstr ("al", address);
3512 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3513 tty->TS_bell = tgetstr ("bl", address);
3514 BackTab (tty) = tgetstr ("bt", address);
3515 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3516 tty->TS_clr_line = tgetstr ("ce", address);
3517 tty->TS_clr_frame = tgetstr ("cl", address);
3518 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3519 AbsPosition (tty) = tgetstr ("cm", address);
3520 CR (tty) = tgetstr ("cr", address);
3521 tty->TS_set_scroll_region = tgetstr ("cs", address);
3522 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
3523 RowPosition (tty) = tgetstr ("cv", address);
3524 tty->TS_del_char = tgetstr ("dc", address);
3525 tty->TS_del_multi_chars = tgetstr ("DC", address);
3526 tty->TS_del_line = tgetstr ("dl", address);
3527 tty->TS_del_multi_lines = tgetstr ("DL", address);
3528 tty->TS_delete_mode = tgetstr ("dm", address);
3529 tty->TS_end_delete_mode = tgetstr ("ed", address);
3530 tty->TS_end_insert_mode = tgetstr ("ei", address);
3531 Home (tty) = tgetstr ("ho", address);
3532 tty->TS_ins_char = tgetstr ("ic", address);
3533 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3534 tty->TS_insert_mode = tgetstr ("im", address);
3535 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3536 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3537 tty->TS_keypad_mode = tgetstr ("ks", address);
3538 LastLine (tty) = tgetstr ("ll", address);
3539 Right (tty) = tgetstr ("nd", address);
3540 Down (tty) = tgetstr ("do", address);
3541 if (!Down (tty))
3542 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
3543 if (tgetflag ("bs"))
3544 Left (tty) = "\b"; /* can't possibly be longer! */
3545 else /* (Actually, "bs" is obsolete...) */
3546 Left (tty) = tgetstr ("le", address);
3547 if (!Left (tty))
3548 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
3549 tty->TS_pad_char = tgetstr ("pc", address);
3550 tty->TS_repeat = tgetstr ("rp", address);
3551 tty->TS_end_standout_mode = tgetstr ("se", address);
3552 tty->TS_fwd_scroll = tgetstr ("sf", address);
3553 tty->TS_standout_mode = tgetstr ("so", address);
3554 tty->TS_rev_scroll = tgetstr ("sr", address);
3555 tty->Wcm->cm_tab = tgetstr ("ta", address);
3556 tty->TS_end_termcap_modes = tgetstr ("te", address);
3557 tty->TS_termcap_modes = tgetstr ("ti", address);
3558 Up (tty) = tgetstr ("up", address);
3559 tty->TS_visible_bell = tgetstr ("vb", address);
3560 tty->TS_cursor_normal = tgetstr ("ve", address);
3561 tty->TS_cursor_visible = tgetstr ("vs", address);
3562 tty->TS_cursor_invisible = tgetstr ("vi", address);
3563 tty->TS_set_window = tgetstr ("wi", address);
3565 tty->TS_enter_underline_mode = tgetstr ("us", address);
3566 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3567 tty->TS_enter_bold_mode = tgetstr ("md", address);
3568 tty->TS_enter_dim_mode = tgetstr ("mh", address);
3569 tty->TS_enter_blink_mode = tgetstr ("mb", address);
3570 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3571 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3572 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3573 tty->TS_exit_attribute_mode = tgetstr ("me", address);
3575 MultiUp (tty) = tgetstr ("UP", address);
3576 MultiDown (tty) = tgetstr ("DO", address);
3577 MultiLeft (tty) = tgetstr ("LE", address);
3578 MultiRight (tty) = tgetstr ("RI", address);
3580 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
3581 color because we can't switch back to the default foreground and
3582 background. */
3583 tty->TS_orig_pair = tgetstr ("op", address);
3584 if (tty->TS_orig_pair)
3586 tty->TS_set_foreground = tgetstr ("AF", address);
3587 tty->TS_set_background = tgetstr ("AB", address);
3588 if (!tty->TS_set_foreground)
3590 /* SVr4. */
3591 tty->TS_set_foreground = tgetstr ("Sf", address);
3592 tty->TS_set_background = tgetstr ("Sb", address);
3595 tty->TN_max_colors = tgetnum ("Co");
3596 tty->TN_max_pairs = tgetnum ("pa");
3598 tty->TN_no_color_video = tgetnum ("NC");
3599 if (tty->TN_no_color_video == -1)
3600 tty->TN_no_color_video = 0;
3603 tty_default_color_capabilities (tty, 1);
3605 MagicWrap (tty) = tgetflag ("xn");
3606 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3607 the former flag imply the latter. */
3608 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
3609 terminal->memory_below_frame = tgetflag ("db");
3610 tty->TF_hazeltine = tgetflag ("hz");
3611 terminal->must_write_spaces = tgetflag ("in");
3612 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3613 tty->TF_insmode_motion = tgetflag ("mi");
3614 tty->TF_standout_motion = tgetflag ("ms");
3615 tty->TF_underscore = tgetflag ("ul");
3616 tty->TF_teleray = tgetflag ("xt");
3618 #else /* DOS_NT */
3619 #ifdef WINDOWSNT
3621 struct frame *f = XFRAME (selected_frame);
3623 initialize_w32_display (terminal);
3625 FrameRows (tty) = FRAME_LINES (f);
3626 FrameCols (tty) = FRAME_COLS (f);
3627 tty->specified_window = FRAME_LINES (f);
3629 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3630 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
3631 terminal->char_ins_del_ok = 1;
3632 baud_rate = 19200;
3634 #else /* MSDOS */
3636 int height, width;
3637 if (strcmp (terminal_type, "internal") == 0)
3638 terminal->type = output_msdos_raw;
3639 initialize_msdos_display (terminal);
3641 get_tty_size (fileno (tty->input), &width, &height);
3642 FrameCols (tty) = width;
3643 FrameRows (tty) = height;
3644 terminal->char_ins_del_ok = 0;
3645 init_baud_rate (fileno (tty->input));
3647 #endif /* MSDOS */
3648 tty->output = stdout;
3649 tty->input = stdin;
3650 /* The following two are inaccessible from w32console.c. */
3651 terminal->delete_frame_hook = &tty_free_frame_resources;
3652 terminal->delete_terminal_hook = &delete_tty;
3654 tty->name = xstrdup (name);
3655 terminal->name = xstrdup (name);
3656 tty->type = xstrdup (terminal_type);
3658 add_keyboard_wait_descriptor (0);
3660 /* FIXME: this should be removed, done earlier. */
3661 Wcm_clear (tty);
3663 tty->delete_in_insert_mode = 1;
3665 UseTabs (tty) = 0;
3666 terminal->scroll_region_ok = 0;
3668 /* Seems to insert lines when it's not supposed to, messing up the
3669 display. In doing a trace, it didn't seem to be called much, so I
3670 don't think we're losing anything by turning it off. */
3671 terminal->line_ins_del_ok = 0;
3673 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
3674 #endif /* DOS_NT */
3676 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3677 init_kboard (terminal->kboard);
3678 terminal->kboard->Vwindow_system = Qnil;
3679 terminal->kboard->next_kboard = all_kboards;
3680 all_kboards = terminal->kboard;
3681 terminal->kboard->reference_count++;
3682 /* Don't let the initial kboard remain current longer than necessary.
3683 That would cause problems if a file loaded on startup tries to
3684 prompt in the mini-buffer. */
3685 if (current_kboard == initial_kboard)
3686 current_kboard = terminal->kboard;
3687 #ifndef DOS_NT
3688 term_get_fkeys (address, terminal->kboard);
3690 /* Get frame size from system, or else from termcap. */
3692 int height, width;
3693 get_tty_size (fileno (tty->input), &width, &height);
3694 FrameCols (tty) = width;
3695 FrameRows (tty) = height;
3698 if (FrameCols (tty) <= 0)
3699 FrameCols (tty) = tgetnum ("co");
3700 if (FrameRows (tty) <= 0)
3701 FrameRows (tty) = tgetnum ("li");
3703 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
3704 maybe_fatal (must_succeed, terminal,
3705 "Screen size %dx%d is too small",
3706 "Screen size %dx%d is too small",
3707 FrameCols (tty), FrameRows (tty));
3709 TabWidth (tty) = tgetnum ("tw");
3711 if (!tty->TS_bell)
3712 tty->TS_bell = "\07";
3714 if (!tty->TS_fwd_scroll)
3715 tty->TS_fwd_scroll = Down (tty);
3717 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
3719 if (TabWidth (tty) < 0)
3720 TabWidth (tty) = 8;
3722 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3723 and newer termcap doc does not seem to say there is a default.
3724 if (!tty->Wcm->cm_tab)
3725 tty->Wcm->cm_tab = "\t";
3728 /* We don't support standout modes that use `magic cookies', so
3729 turn off any that do. */
3730 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
3732 tty->TS_standout_mode = 0;
3733 tty->TS_end_standout_mode = 0;
3735 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
3737 tty->TS_enter_underline_mode = 0;
3738 tty->TS_exit_underline_mode = 0;
3741 /* If there's no standout mode, try to use underlining instead. */
3742 if (tty->TS_standout_mode == 0)
3744 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3745 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
3748 /* If no `se' string, try using a `me' string instead.
3749 If that fails, we can't use standout mode at all. */
3750 if (tty->TS_end_standout_mode == 0)
3752 char *s = tgetstr ("me", address);
3753 if (s != 0)
3754 tty->TS_end_standout_mode = s;
3755 else
3756 tty->TS_standout_mode = 0;
3759 if (tty->TF_teleray)
3761 tty->Wcm->cm_tab = 0;
3762 /* We can't support standout mode, because it uses magic cookies. */
3763 tty->TS_standout_mode = 0;
3764 /* But that means we cannot rely on ^M to go to column zero! */
3765 CR (tty) = 0;
3766 /* LF can't be trusted either -- can alter hpos */
3767 /* if move at column 0 thru a line with TS_standout_mode */
3768 Down (tty) = 0;
3771 /* Special handling for certain terminal types known to need it */
3773 if (!strcmp (terminal_type, "supdup"))
3775 terminal->memory_below_frame = 1;
3776 tty->Wcm->cm_losewrap = 1;
3778 if (!strncmp (terminal_type, "c10", 3)
3779 || !strcmp (terminal_type, "perq"))
3781 /* Supply a makeshift :wi string.
3782 This string is not valid in general since it works only
3783 for windows starting at the upper left corner;
3784 but that is all Emacs uses.
3786 This string works only if the frame is using
3787 the top of the video memory, because addressing is memory-relative.
3788 So first check the :ti string to see if that is true.
3790 It would be simpler if the :wi string could go in the termcap
3791 entry, but it can't because it is not fully valid.
3792 If it were in the termcap entry, it would confuse other programs. */
3793 if (!tty->TS_set_window)
3795 p = tty->TS_termcap_modes;
3796 while (*p && strcmp (p, "\033v "))
3797 p++;
3798 if (*p)
3799 tty->TS_set_window = "\033v%C %C %C %C ";
3801 /* Termcap entry often fails to have :in: flag */
3802 terminal->must_write_spaces = 1;
3803 /* :ti string typically fails to have \E^G! in it */
3804 /* This limits scope of insert-char to one line. */
3805 strcpy (area, tty->TS_termcap_modes);
3806 strcat (area, "\033\007!");
3807 tty->TS_termcap_modes = area;
3808 area += strlen (area) + 1;
3809 p = AbsPosition (tty);
3810 /* Change all %+ parameters to %C, to handle
3811 values above 96 correctly for the C100. */
3812 while (*p)
3814 if (p[0] == '%' && p[1] == '+')
3815 p[1] = 'C';
3816 p++;
3820 tty->specified_window = FrameRows (tty);
3822 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3824 maybe_fatal (must_succeed, terminal,
3825 "Terminal type \"%s\" is not powerful enough to run Emacs",
3826 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3827 It lacks the ability to position the cursor.\n\
3828 If that is not the actual type of terminal you have,\n\
3829 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3830 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3831 # ifdef TERMINFO
3832 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3833 # else /* TERMCAP */
3834 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3835 # endif /* TERMINFO */
3836 terminal_type);
3839 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
3840 maybe_fatal (must_succeed, terminal,
3841 "Could not determine the frame size",
3842 "Could not determine the frame size");
3844 tty->delete_in_insert_mode
3845 = tty->TS_delete_mode && tty->TS_insert_mode
3846 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
3848 tty->se_is_so = (tty->TS_standout_mode
3849 && tty->TS_end_standout_mode
3850 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
3852 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
3854 terminal->scroll_region_ok
3855 = (tty->Wcm->cm_abs
3856 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
3858 terminal->line_ins_del_ok
3859 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3860 && (tty->TS_del_line || tty->TS_del_multi_lines))
3861 || (terminal->scroll_region_ok
3862 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
3864 terminal->char_ins_del_ok
3865 = ((tty->TS_ins_char || tty->TS_insert_mode
3866 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3867 && (tty->TS_del_char || tty->TS_del_multi_chars));
3869 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
3871 init_baud_rate (fileno (tty->input));
3873 #endif /* not DOS_NT */
3875 /* Init system terminal modes (RAW or CBREAK, etc.). */
3876 init_sys_modes (tty);
3878 return terminal;
3882 static void
3883 vfatal (const char *str, va_list ap)
3885 fprintf (stderr, "emacs: ");
3886 vfprintf (stderr, str, ap);
3887 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
3888 fprintf (stderr, "\n");
3889 va_end (ap);
3890 fflush (stderr);
3891 exit (1);
3895 /* Auxiliary error-handling function for init_tty.
3896 Delete TERMINAL, then call error or fatal with str1 or str2,
3897 respectively, according to MUST_SUCCEED. */
3899 static void
3900 maybe_fatal (int must_succeed, struct terminal *terminal,
3901 const char *str1, const char *str2, ...)
3903 va_list ap;
3904 va_start (ap, str2);
3905 if (terminal)
3906 delete_tty (terminal);
3908 if (must_succeed)
3909 vfatal (str2, ap);
3910 else
3911 verror (str1, ap);
3913 va_end (ap);
3914 abort ();
3917 void
3918 fatal (const char *str, ...)
3920 va_list ap;
3921 va_start (ap, str);
3922 vfatal (str, ap);
3923 va_end (ap);
3928 /* Delete the given tty terminal, closing all frames on it. */
3930 static void
3931 delete_tty (struct terminal *terminal)
3933 struct tty_display_info *tty;
3935 /* Protect against recursive calls. delete_frame in
3936 delete_terminal calls us back when it deletes our last frame. */
3937 if (!terminal->name)
3938 return;
3940 if (terminal->type != output_termcap)
3941 abort ();
3943 tty = terminal->display_info.tty;
3945 if (tty == tty_list)
3946 tty_list = tty->next;
3947 else
3949 struct tty_display_info *p;
3950 for (p = tty_list; p && p->next != tty; p = p->next)
3953 if (! p)
3954 /* This should not happen. */
3955 abort ();
3957 p->next = tty->next;
3958 tty->next = 0;
3961 /* reset_sys_modes needs a valid device, so this call needs to be
3962 before delete_terminal. */
3963 reset_sys_modes (tty);
3965 delete_terminal (terminal);
3967 xfree (tty->name);
3968 xfree (tty->type);
3970 if (tty->input)
3972 delete_keyboard_wait_descriptor (fileno (tty->input));
3973 if (tty->input != stdin)
3974 fclose (tty->input);
3976 if (tty->output && tty->output != stdout && tty->output != tty->input)
3977 fclose (tty->output);
3978 if (tty->termscript)
3979 fclose (tty->termscript);
3981 xfree (tty->old_tty);
3982 xfree (tty->Wcm);
3983 xfree (tty->termcap_strings_buffer);
3984 xfree (tty->termcap_term_buffer);
3986 memset (tty, 0, sizeof (struct tty_display_info));
3987 xfree (tty);
3992 /* Mark the pointers in the tty_display_info objects.
3993 Called by the Fgarbage_collector. */
3995 void
3996 mark_ttys (void)
3998 struct tty_display_info *tty;
4000 for (tty = tty_list; tty; tty = tty->next)
4001 mark_object (tty->top_frame);
4006 void
4007 syms_of_term (void)
4009 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
4010 doc: /* Non-nil means the system uses terminfo rather than termcap.
4011 This variable can be used by terminal emulator packages. */);
4012 #ifdef TERMINFO
4013 system_uses_terminfo = 1;
4014 #else
4015 system_uses_terminfo = 0;
4016 #endif
4018 DEFVAR_LISP ("suspend-tty-functions", &Vsuspend_tty_functions,
4019 doc: /* Functions to be run after suspending a tty.
4020 The functions are run with one argument, the terminal object to be suspended.
4021 See `suspend-tty'. */);
4022 Vsuspend_tty_functions = Qnil;
4025 DEFVAR_LISP ("resume-tty-functions", &Vresume_tty_functions,
4026 doc: /* Functions to be run after resuming a tty.
4027 The functions are run with one argument, the terminal object that was revived.
4028 See `resume-tty'. */);
4029 Vresume_tty_functions = Qnil;
4031 DEFVAR_BOOL ("visible-cursor", &visible_cursor,
4032 doc: /* Non-nil means to make the cursor very visible.
4033 This only has an effect when running in a text terminal.
4034 What means \"very visible\" is up to your terminal. It may make the cursor
4035 bigger, or it may make it blink, or it may do nothing at all. */);
4036 visible_cursor = 1;
4038 defsubr (&Stty_display_color_p);
4039 defsubr (&Stty_display_color_cells);
4040 defsubr (&Stty_no_underline);
4041 defsubr (&Stty_type);
4042 defsubr (&Scontrolling_tty_p);
4043 defsubr (&Ssuspend_tty);
4044 defsubr (&Sresume_tty);
4045 #ifdef HAVE_GPM
4046 defsubr (&Sgpm_mouse_start);
4047 defsubr (&Sgpm_mouse_stop);
4049 staticpro (&mouse_face_window);
4050 #endif /* HAVE_GPM */
4052 #ifndef DOS_NT
4053 default_orig_pair = NULL;
4054 default_set_foreground = NULL;
4055 default_set_background = NULL;
4056 #endif /* !DOS_NT */
4058 encode_terminal_src = NULL;
4059 encode_terminal_dst = NULL;
4064 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
4065 (do not change this comment) */