(IT_write_glyphs): Adapt to new glyph type.
[bpt/emacs.git] / src / term.c
CommitLineData
d284f58f 1/* Terminal control module for terminals described by TERMCAP
0b5538bd 2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1998, 2000, 2001,
8cabe764
GM
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
08a24c47
JB
5
6This file is part of GNU Emacs.
7
8GNU Emacs is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
684d6f5b 10the Free Software Foundation; either version 3, or (at your option)
08a24c47
JB
11any later version.
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
20the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
08a24c47 22
d284f58f 23/* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
08a24c47 24
565620a5 25#include <config.h>
08a24c47
JB
26#include <stdio.h>
27#include <ctype.h>
a168702a 28#include <string.h>
59b3194c 29#include <errno.h>
28d440ab 30#include <sys/file.h>
7eb1e453 31
d8b18043 32#ifdef HAVE_UNISTD_H
1ec5dc77 33#include <unistd.h>
d8b18043 34#endif
7ee72033 35
67ad74df
KL
36#if HAVE_TERMIOS_H
37#include <termios.h> /* For TIOCNOTTY. */
38#endif
03a1d6bd
KL
39
40#include <signal.h>
7c401d15 41#include <stdarg.h>
0c72d684 42
9628b887 43#include "lisp.h"
08a24c47
JB
44#include "termchar.h"
45#include "termopts.h"
9332ea03
KH
46#include "buffer.h"
47#include "character.h"
a4decb7f
KH
48#include "charset.h"
49#include "coding.h"
4c12d738 50#include "composite.h"
2538fae4 51#include "keyboard.h"
ff11dfa1 52#include "frame.h"
08a24c47
JB
53#include "disptab.h"
54#include "termhooks.h"
dfcf069d 55#include "dispextern.h"
a168702a 56#include "window.h"
8feddab4 57#include "keymap.h"
1a935bfd 58#include "blockinput.h"
03a1d6bd
KL
59#include "syssignal.h"
60#include "systty.h"
c9612b8e 61#include "intervals.h"
a168702a 62
ff23e1dd
GM
63/* For now, don't try to include termcap.h. On some systems,
64 configure finds a non-standard termcap.h that the main build
65 won't find. */
66
67#if defined HAVE_TERMCAP_H && 0
b0f61f15 68#include <termcap.h>
46d1b2bc
DL
69#else
70extern void tputs P_ ((const char *, int, int (*)(int)));
71extern int tgetent P_ ((char *, const char *));
72extern int tgetflag P_ ((char *id));
73extern int tgetnum P_ ((char *id));
b0f61f15
GM
74#endif
75
eccec691 76#include "cm.h"
dfcf069d
AS
77#ifdef HAVE_X_WINDOWS
78#include "xterm.h"
79#endif
e0f712ba 80#ifdef MAC_OS
1a578e9b
AC
81#include "macterm.h"
82#endif
c8951b18 83
28d440ab
KL
84#ifndef O_RDWR
85#define O_RDWR 2
86#endif
6b61353c 87
0c72d684
KL
88#ifndef O_NOCTTY
89#define O_NOCTTY 0
90#endif
6b61353c 91
78048085
EZ
92/* The name of the default console device. */
93#ifdef WINDOWSNT
94#define DEV_TTY "CONOUT$"
95#else
96#define DEV_TTY "/dev/tty"
97#endif
a168702a 98
ed8dad6b 99static void tty_set_scroll_region P_ ((struct frame *f, int start, int stop));
a168702a
GM
100static void turn_on_face P_ ((struct frame *, int face_id));
101static void turn_off_face P_ ((struct frame *, int face_id));
28d7d09f
KL
102static void tty_show_cursor P_ ((struct tty_display_info *));
103static void tty_hide_cursor P_ ((struct tty_display_info *));
ed8dad6b 104static void tty_background_highlight P_ ((struct tty_display_info *tty));
4a665758
KL
105static void clear_tty_hooks P_ ((struct terminal *terminal));
106static void set_tty_hooks P_ ((struct terminal *terminal));
ed8dad6b 107static void dissociate_if_controlling_tty P_ ((int fd));
6ed8eeff 108static void delete_tty P_ ((struct terminal *));
a168702a 109
6548cf00
KL
110#define OUTPUT(tty, a) \
111 emacs_tputs ((tty), a, \
112 (int) (FRAME_LINES (XFRAME (selected_frame)) \
113 - curY (tty)), \
114 cmputc)
115
28d440ab
KL
116#define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
117#define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
a168702a 118
28d440ab 119#define OUTPUT_IF(tty, a) \
6548cf00
KL
120 do { \
121 if (a) \
122 emacs_tputs ((tty), a, \
123 (int) (FRAME_LINES (XFRAME (selected_frame)) \
124 - curY (tty) ), \
125 cmputc); \
126 } while (0)
177c0ea7 127
28d440ab 128#define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
c291d9ef 129
0db017c0
SM
130/* If true, use "vs", otherwise use "ve" to make the cursor visible. */
131
132static int visible_cursor;
133
f46c2aff 134/* Display space properties */
08a24c47 135
f46c2aff 136extern Lisp_Object Qspace, QCalign_to, QCwidth;
08a24c47 137
0b0d3e0b 138/* Functions to call after suspending a tty. */
e4019195 139Lisp_Object Vsuspend_tty_functions;
08a24c47 140
0b0d3e0b 141/* Functions to call after resuming a tty. */
e4019195 142Lisp_Object Vresume_tty_functions;
08a24c47 143
428a555e 144/* Chain of all tty device parameters. */
28d7d09f 145struct tty_display_info *tty_list;
08a24c47 146
fca177d4
KL
147/* Nonzero means no need to redraw the entire frame on resuming a
148 suspended Emacs. This is useful on terminals with multiple
149 pages, where one page is used for Emacs and another for all
150 else. */
08a24c47 151int no_redraw_on_reenter;
4e6ba4a4
GM
152
153/* Meaning of bits in no_color_video. Each bit set means that the
154 corresponding attribute cannot be combined with colors. */
155
156enum no_color_bit
157{
158 NC_STANDOUT = 1 << 0,
159 NC_UNDERLINE = 1 << 1,
160 NC_REVERSE = 1 << 2,
161 NC_BLINK = 1 << 3,
162 NC_DIM = 1 << 4,
163 NC_BOLD = 1 << 5,
164 NC_INVIS = 1 << 6,
165 NC_PROTECT = 1 << 7,
166 NC_ALT_CHARSET = 1 << 8
167};
168
08a24c47
JB
169/* internal state */
170
8dd0c7cb 171/* The largest frame width in any call to calculate_costs. */
a168702a 172
9882535b 173int max_frame_cols;
a168702a 174
8dd0c7cb 175/* The largest frame height in any call to calculate_costs. */
a168702a 176
9882535b 177int max_frame_lines;
8dd0c7cb 178
0c72d684
KL
179/* Non-zero if we have dropped our controlling tty and therefore
180 should not open a frame on stdout. */
181static int no_controlling_tty;
08a24c47 182
07c57952 183/* Provided for lisp packages. */
a168702a 184
07c57952
KH
185static int system_uses_terminfo;
186
08a24c47 187char *tparam ();
e4bfb3b6
RS
188
189extern char *tgetstr ();
08a24c47 190\f
cb28b9c2 191
7e5a23bd 192#ifdef HAVE_GPM
e882229c 193#include <sys/fcntl.h>
e882229c 194
b870aa61
SM
195static void term_clear_mouse_face ();
196static void term_mouse_highlight (struct frame *f, int x, int y);
197
71f44e7a
SM
198/* The device for which we have enabled gpm support (or NULL). */
199struct tty_display_info *gpm_tty = NULL;
e882229c
NR
200
201/* These variables describe the range of text currently shown in its
202 mouse-face, together with the window they apply to. As long as
203 the mouse stays within this range, we need not redraw anything on
204 its account. Rows and columns are glyph matrix positions in
205 MOUSE_FACE_WINDOW. */
206static int mouse_face_beg_row, mouse_face_beg_col;
207static int mouse_face_end_row, mouse_face_end_col;
208static int mouse_face_past_end;
6178ce5e 209static Lisp_Object mouse_face_window;
e882229c
NR
210static int mouse_face_face_id;
211
e882229c
NR
212static int pos_x, pos_y;
213static int last_mouse_x, last_mouse_y;
7e5a23bd 214#endif /* HAVE_GPM */
e882229c 215
da8e1115 216/* Ring the bell on a tty. */
c291d9ef 217
ed8dad6b 218static void
385ed61f 219tty_ring_bell (struct frame *f)
3224dac1 220{
3224dac1 221 struct tty_display_info *tty = FRAME_TTY (f);
c291d9ef 222
b6660415
KL
223 if (tty->output)
224 {
225 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
226 ? tty->TS_visible_bell
227 : tty->TS_bell));
228 fflush (tty->output);
08a24c47 229 }
08a24c47
JB
230}
231
da8e1115
KL
232/* Set up termcap modes for Emacs. */
233
dfcf069d 234void
6ed8eeff 235tty_set_terminal_modes (struct terminal *terminal)
08a24c47 236{
6ed8eeff 237 struct tty_display_info *tty = terminal->display_info.tty;
3224dac1 238
0b0d3e0b 239 if (tty->output)
08a24c47 240 {
fbf34973
KL
241 if (tty->TS_termcap_modes)
242 OUTPUT (tty, tty->TS_termcap_modes);
3ae9c96a 243 else
fbf34973
KL
244 {
245 /* Output enough newlines to scroll all the old screen contents
246 off the screen, so it won't be overwritten and lost. */
247 int i;
a3c07f68 248 current_tty = tty;
fbf34973 249 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
a3c07f68 250 cmputc ('\n');
fbf34973
KL
251 }
252
0b0d3e0b 253 OUTPUT_IF (tty, tty->TS_termcap_modes);
b58cb614 254 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
0b0d3e0b
KL
255 OUTPUT_IF (tty, tty->TS_keypad_mode);
256 losecursor (tty);
2f98e6e3 257 fflush (tty->output);
08a24c47 258 }
08a24c47
JB
259}
260
da8e1115
KL
261/* Reset termcap modes before exiting Emacs. */
262
dfcf069d 263void
6ed8eeff 264tty_reset_terminal_modes (struct terminal *terminal)
08a24c47 265{
6ed8eeff 266 struct tty_display_info *tty = terminal->display_info.tty;
0b0d3e0b
KL
267
268 if (tty->output)
08a24c47 269 {
ed8dad6b
KL
270 tty_turn_off_highlight (tty);
271 tty_turn_off_insert (tty);
0b0d3e0b
KL
272 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
273 OUTPUT_IF (tty, tty->TS_cursor_normal);
274 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
275 OUTPUT_IF (tty, tty->TS_orig_pair);
d284f58f 276 /* Output raw CR so kernel can track the cursor hpos. */
0b0d3e0b 277 current_tty = tty;
d284f58f 278 cmputc ('\r');
2f98e6e3 279 fflush (tty->output);
08a24c47 280 }
08a24c47
JB
281}
282
6ed8eeff 283/* Flag the end of a display update on a termcap terminal. */
08a24c47 284
ed8dad6b 285static void
3224dac1 286tty_update_end (struct frame *f)
08a24c47 287{
3224dac1 288 struct tty_display_info *tty = FRAME_TTY (f);
177c0ea7 289
3224dac1
KL
290 if (!XWINDOW (selected_window)->cursor_off_p)
291 tty_show_cursor (tty);
ed8dad6b
KL
292 tty_turn_off_insert (tty);
293 tty_background_highlight (tty);
08a24c47
JB
294}
295
da8e1115
KL
296/* The implementation of set_terminal_window for termcap frames. */
297
ed8dad6b 298static void
385ed61f 299tty_set_terminal_window (struct frame *f, int size)
08a24c47 300{
3224dac1
KL
301 struct tty_display_info *tty = FRAME_TTY (f);
302
303 tty->specified_window = size ? size : FRAME_LINES (f);
304 if (FRAME_SCROLL_REGION_OK (f))
ed8dad6b 305 tty_set_scroll_region (f, 0, tty->specified_window);
08a24c47
JB
306}
307
ed8dad6b
KL
308static void
309tty_set_scroll_region (struct frame *f, int start, int stop)
08a24c47
JB
310{
311 char *buf;
28d7d09f 312 struct tty_display_info *tty = FRAME_TTY (f);
177c0ea7 313
fca177d4
KL
314 if (tty->TS_set_scroll_region)
315 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1);
316 else if (tty->TS_set_scroll_region_1)
317 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
ed02974b
KL
318 FRAME_LINES (f), start,
319 FRAME_LINES (f) - stop,
320 FRAME_LINES (f));
08a24c47 321 else
fca177d4 322 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
177c0ea7 323
6548cf00 324 OUTPUT (tty, buf);
9ac0d9e0 325 xfree (buf);
6548cf00 326 losecursor (tty);
08a24c47 327}
d284f58f 328
08a24c47 329\f
d284f58f 330static void
ed8dad6b 331tty_turn_on_insert (struct tty_display_info *tty)
08a24c47 332{
fca177d4
KL
333 if (!tty->insert_mode)
334 OUTPUT (tty, tty->TS_insert_mode);
335 tty->insert_mode = 1;
08a24c47
JB
336}
337
dfcf069d 338void
ed8dad6b 339tty_turn_off_insert (struct tty_display_info *tty)
08a24c47 340{
fca177d4
KL
341 if (tty->insert_mode)
342 OUTPUT (tty, tty->TS_end_insert_mode);
343 tty->insert_mode = 0;
08a24c47
JB
344}
345\f
54800acb 346/* Handle highlighting. */
08a24c47 347
dfcf069d 348void
ed8dad6b 349tty_turn_off_highlight (struct tty_display_info *tty)
08a24c47 350{
fca177d4
KL
351 if (tty->standout_mode)
352 OUTPUT_IF (tty, tty->TS_end_standout_mode);
353 tty->standout_mode = 0;
08a24c47
JB
354}
355
d284f58f 356static void
ed8dad6b 357tty_turn_on_highlight (struct tty_display_info *tty)
08a24c47 358{
fca177d4
KL
359 if (!tty->standout_mode)
360 OUTPUT_IF (tty, tty->TS_standout_mode);
361 tty->standout_mode = 1;
08a24c47
JB
362}
363
86a7d192 364static void
ed8dad6b 365tty_toggle_highlight (struct tty_display_info *tty)
86a7d192 366{
fca177d4 367 if (tty->standout_mode)
ed8dad6b 368 tty_turn_off_highlight (tty);
86a7d192 369 else
ed8dad6b 370 tty_turn_on_highlight (tty);
86a7d192
GM
371}
372
a168702a
GM
373
374/* Make cursor invisible. */
375
376static void
28d7d09f 377tty_hide_cursor (struct tty_display_info *tty)
a168702a 378{
fca177d4 379 if (tty->cursor_hidden == 0)
d284f58f 380 {
fca177d4
KL
381 tty->cursor_hidden = 1;
382 OUTPUT_IF (tty, tty->TS_cursor_invisible);
d284f58f 383 }
a168702a
GM
384}
385
386
387/* Ensure that cursor is visible. */
388
389static void
28d7d09f 390tty_show_cursor (struct tty_display_info *tty)
a168702a 391{
fca177d4 392 if (tty->cursor_hidden)
d284f58f 393 {
fca177d4
KL
394 tty->cursor_hidden = 0;
395 OUTPUT_IF (tty, tty->TS_cursor_normal);
0db017c0 396 if (visible_cursor)
b58cb614 397 OUTPUT_IF (tty, tty->TS_cursor_visible);
d284f58f 398 }
a168702a
GM
399}
400
401
08a24c47
JB
402/* Set standout mode to the state it should be in for
403 empty space inside windows. What this is,
404 depends on the user option inverse-video. */
405
ed8dad6b
KL
406static void
407tty_background_highlight (struct tty_display_info *tty)
08a24c47 408{
08a24c47 409 if (inverse_video)
ed8dad6b 410 tty_turn_on_highlight (tty);
08a24c47 411 else
ed8dad6b 412 tty_turn_off_highlight (tty);
08a24c47
JB
413}
414
415/* Set standout mode to the mode specified for the text to be output. */
416
dfcf069d 417static void
ed8dad6b 418tty_highlight_if_desired (struct tty_display_info *tty)
08a24c47 419{
8ede64a5 420 if (inverse_video)
ed8dad6b 421 tty_turn_on_highlight (tty);
8ede64a5 422 else
ed8dad6b 423 tty_turn_off_highlight (tty);
08a24c47
JB
424}
425\f
426
a168702a
GM
427/* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
428 frame-relative coordinates. */
08a24c47 429
ed8dad6b 430static void
385ed61f 431tty_cursor_to (struct frame *f, int vpos, int hpos)
08a24c47 432{
3224dac1 433 struct tty_display_info *tty = FRAME_TTY (f);
08a24c47 434
36cae867
KH
435 /* Detect the case where we are called from reset_sys_modes
436 and the costs have never been calculated. Do nothing. */
fca177d4 437 if (! tty->costs_set)
36cae867
KH
438 return;
439
6548cf00
KL
440 if (curY (tty) == vpos
441 && curX (tty) == hpos)
08a24c47 442 return;
fca177d4 443 if (!tty->TF_standout_motion)
ed8dad6b 444 tty_background_highlight (tty);
fca177d4 445 if (!tty->TF_insmode_motion)
ed8dad6b 446 tty_turn_off_insert (tty);
6548cf00 447 cmgoto (tty, vpos, hpos);
08a24c47
JB
448}
449
450/* Similar but don't take any account of the wasted characters. */
451
ed8dad6b 452static void
385ed61f 453tty_raw_cursor_to (struct frame *f, int row, int col)
08a24c47 454{
3224dac1
KL
455 struct tty_display_info *tty = FRAME_TTY (f);
456
6548cf00
KL
457 if (curY (tty) == row
458 && curX (tty) == col)
08a24c47 459 return;
fca177d4 460 if (!tty->TF_standout_motion)
ed8dad6b 461 tty_background_highlight (tty);
fca177d4 462 if (!tty->TF_insmode_motion)
ed8dad6b 463 tty_turn_off_insert (tty);
6548cf00 464 cmgoto (tty, row, col);
08a24c47
JB
465}
466\f
467/* Erase operations */
468
da8e1115
KL
469/* Clear from cursor to end of frame on a termcap device. */
470
ed8dad6b 471static void
385ed61f 472tty_clear_to_end (struct frame *f)
08a24c47
JB
473{
474 register int i;
3224dac1 475 struct tty_display_info *tty = FRAME_TTY (f);
08a24c47 476
fca177d4 477 if (tty->TS_clr_to_bottom)
08a24c47 478 {
ed8dad6b 479 tty_background_highlight (tty);
fca177d4 480 OUTPUT (tty, tty->TS_clr_to_bottom);
08a24c47
JB
481 }
482 else
483 {
6548cf00 484 for (i = curY (tty); i < FRAME_LINES (f); i++)
08a24c47 485 {
385ed61f
KL
486 cursor_to (f, i, 0);
487 clear_end_of_line (f, FRAME_COLS (f));
08a24c47
JB
488 }
489 }
490}
491
da8e1115 492/* Clear an entire termcap frame. */
08a24c47 493
ed8dad6b 494static void
385ed61f 495tty_clear_frame (struct frame *f)
08a24c47 496{
3224dac1 497 struct tty_display_info *tty = FRAME_TTY (f);
177c0ea7 498
fca177d4 499 if (tty->TS_clr_frame)
08a24c47 500 {
ed8dad6b 501 tty_background_highlight (tty);
fca177d4 502 OUTPUT (tty, tty->TS_clr_frame);
6548cf00 503 cmat (tty, 0, 0);
08a24c47
JB
504 }
505 else
506 {
385ed61f
KL
507 cursor_to (f, 0, 0);
508 clear_to_end (f);
08a24c47
JB
509 }
510}
511
da8e1115 512/* An implementation of clear_end_of_line for termcap frames.
08a24c47
JB
513
514 Note that the cursor may be moved, on terminals lacking a `ce' string. */
515
ed8dad6b 516static void
385ed61f 517tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
08a24c47
JB
518{
519 register int i;
3224dac1 520 struct tty_display_info *tty = FRAME_TTY (f);
08a24c47 521
36cae867
KH
522 /* Detect the case where we are called from reset_sys_modes
523 and the costs have never been calculated. Do nothing. */
fca177d4 524 if (! tty->costs_set)
36cae867
KH
525 return;
526
6548cf00 527 if (curX (tty) >= first_unused_hpos)
08a24c47 528 return;
ed8dad6b 529 tty_background_highlight (tty);
fca177d4 530 if (tty->TS_clr_line)
08a24c47 531 {
fca177d4 532 OUTPUT1 (tty, tty->TS_clr_line);
08a24c47
JB
533 }
534 else
535 { /* have to do it the hard way */
ed8dad6b 536 tty_turn_off_insert (tty);
08a24c47 537
a168702a 538 /* Do not write in last row last col with Auto-wrap on. */
6548cf00 539 if (AutoWrap (tty)
0a125897
KL
540 && curY (tty) == FrameRows (tty) - 1
541 && first_unused_hpos == FrameCols (tty))
08a24c47
JB
542 first_unused_hpos--;
543
6548cf00 544 for (i = curX (tty); i < first_unused_hpos; i++)
08a24c47 545 {
0b0d3e0b
KL
546 if (tty->termscript)
547 fputc (' ', tty->termscript);
548 fputc (' ', tty->output);
08a24c47 549 }
6548cf00 550 cmplus (tty, first_unused_hpos - curX (tty));
08a24c47
JB
551 }
552}
553\f
288d5132
KH
554/* Buffers to store the source and result of code conversion for terminal. */
555static unsigned char *encode_terminal_src;
556static unsigned char *encode_terminal_dst;
557/* Allocated sizes of the above buffers. */
558static int encode_terminal_src_size;
559static int encode_terminal_dst_size;
560
561/* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
562 Set CODING->produced to the byte-length of the resulting byte
563 sequence, and return a pointer to that byte sequence. */
564
565unsigned char *
566encode_terminal_code (src, src_len, coding)
a168702a 567 struct glyph *src;
a4decb7f 568 int src_len;
288d5132 569 struct coding_system *coding;
a4decb7f 570{
feef4f84 571 struct glyph *src_end = src + src_len;
085d5908 572 register GLYPH g;
288d5132
KH
573 unsigned char *buf;
574 int nchars, nbytes, required;
a4decb7f
KH
575 register int tlen = GLYPH_TABLE_LENGTH;
576 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
288d5132 577 Lisp_Object charset_list;
c5a518df 578
288d5132
KH
579 /* Allocate sufficient size of buffer to store all characters in
580 multibyte-form. But, it may be enlarged on demand if
4c12d738
KH
581 Vglyph_table contains a string or a composite glyph is
582 encountered. */
288d5132
KH
583 required = MAX_MULTIBYTE_LENGTH * src_len;
584 if (encode_terminal_src_size < required)
585 {
586 if (encode_terminal_src_size == 0)
587 encode_terminal_src = xmalloc (required);
588 else
589 encode_terminal_src = xrealloc (encode_terminal_src, required);
590 encode_terminal_src_size = required;
591 }
6589fd67 592
288d5132 593 charset_list = coding_charset_list (coding);
a4decb7f 594
288d5132
KH
595 buf = encode_terminal_src;
596 nchars = 0;
597 while (src < src_end)
a4decb7f 598 {
4c12d738
KH
599 if (src->type == COMPOSITE_GLYPH)
600 {
601 struct composition *cmp = composition_table[src->u.cmp_id];
602 int i;
603
604 nbytes = buf - encode_terminal_src;
605 required = MAX_MULTIBYTE_LENGTH * cmp->glyph_len;
606
607 if (encode_terminal_src_size < nbytes + required)
608 {
609 encode_terminal_src_size = nbytes + required;
610 encode_terminal_src = xrealloc (encode_terminal_src,
611 encode_terminal_src_size);
612 buf = encode_terminal_src + nbytes;
613 }
614
615 for (i = 0; i < cmp->glyph_len; i++)
616 {
617 int c = COMPOSITION_GLYPH (cmp, i);
618
619 if (! char_charset (c, charset_list, NULL))
620 break;
621 buf += CHAR_STRING (c, buf);
622 nchars++;
623 }
624 if (i == 0)
625 {
626 /* The first character of the composition is not encodable. */
627 *buf++ = '?';
628 nchars++;
629 }
630 }
a4decb7f 631 /* We must skip glyphs to be padded for a wide character. */
4c12d738 632 else if (! CHAR_GLYPH_PADDING_P (*src))
a4decb7f 633 {
288d5132
KH
634 int c;
635 Lisp_Object string;
636
4ae0a2c0 637 string = Qnil;
288d5132 638 g = GLYPH_FROM_CHAR_GLYPH (src[0]);
32de38e4
KH
639
640 if (g < 0 || g >= tlen)
288d5132
KH
641 {
642 /* This glyph doesn't has an entry in Vglyph_table. */
643 c = src->u.ch;
644 }
32de38e4 645 else
a4decb7f 646 {
32de38e4 647 /* This glyph has an entry in Vglyph_table,
a4decb7f
KH
648 so process any alias before testing for simpleness. */
649 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
32de38e4
KH
650
651 if (GLYPH_SIMPLE_P (tbase, tlen, g))
4ae0a2c0
KH
652 /* We set the multi-byte form of a character in G
653 (that should be an ASCII character) at WORKBUF. */
654 c = FAST_GLYPH_CHAR (g);
32de38e4 655 else
4ae0a2c0
KH
656 /* We have a string in Vglyph_table. */
657 string = tbase[g];
171d7f24 658 }
177c0ea7 659
4ae0a2c0 660 if (NILP (string))
c42869c4 661 {
4c12d738
KH
662 nbytes = buf - encode_terminal_src;
663 if (encode_terminal_src_size < nbytes + MAX_MULTIBYTE_LENGTH)
664 {
665 encode_terminal_src_size = nbytes + MAX_MULTIBYTE_LENGTH;
666 encode_terminal_src = xrealloc (encode_terminal_src,
667 encode_terminal_src_size);
668 buf = encode_terminal_src + nbytes;
669 }
288d5132 670 if (char_charset (c, charset_list, NULL))
c5a518df 671 {
288d5132
KH
672 /* Store the multibyte form of C at BUF. */
673 buf += CHAR_STRING (c, buf);
674 nchars++;
c5a518df
KH
675 }
676 else
677 {
288d5132
KH
678 /* C is not encodable. */
679 *buf++ = '?';
c5a518df 680 nchars++;
288d5132
KH
681 while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
682 {
683 *buf++ = '?';
684 nchars++;
685 src++;
686 }
c5a518df 687 }
4ae0a2c0
KH
688 }
689 else
690 {
288d5132
KH
691 unsigned char *p = SDATA (string), *pend = p + SBYTES (string);
692
693 if (! STRING_MULTIBYTE (string))
694 string = string_to_multibyte (string);
695 nbytes = buf - encode_terminal_src;
54c4c546 696 if (encode_terminal_src_size < nbytes + SBYTES (string))
4ae0a2c0 697 {
288d5132
KH
698 encode_terminal_src_size = nbytes + SBYTES (string);
699 encode_terminal_src = xrealloc (encode_terminal_src,
700 encode_terminal_src_size);
701 buf = encode_terminal_src + nbytes;
4ae0a2c0 702 }
288d5132
KH
703 bcopy (SDATA (string), buf, SBYTES (string));
704 buf += SBYTES (string);
705 nchars += SCHARS (string);
c42869c4 706 }
a4decb7f 707 }
288d5132
KH
708 src++;
709 }
710
711 if (nchars == 0)
712 {
713 coding->produced = 0;
714 return NULL;
a4decb7f 715 }
177c0ea7 716
288d5132
KH
717 nbytes = buf - encode_terminal_src;
718 coding->source = encode_terminal_src;
719 if (encode_terminal_dst_size == 0)
4ae0a2c0 720 {
288d5132
KH
721 encode_terminal_dst_size = encode_terminal_src_size;
722 encode_terminal_dst = xmalloc (encode_terminal_dst_size);
4ae0a2c0 723 }
288d5132
KH
724 coding->destination = encode_terminal_dst;
725 coding->dst_bytes = encode_terminal_dst_size;
726 encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
c5a518df 727 /* coding->destination may have been reallocated. */
288d5132
KH
728 encode_terminal_dst = coding->destination;
729 encode_terminal_dst_size = coding->dst_bytes;
4ae0a2c0 730
288d5132 731 return (encode_terminal_dst);
a4decb7f
KH
732}
733
08a24c47 734
c73bd236 735
da8e1115
KL
736/* An implementation of write_glyphs for termcap frames. */
737
ed8dad6b 738static void
385ed61f 739tty_write_glyphs (struct frame *f, struct glyph *string, int len)
08a24c47 740{
288d5132
KH
741 unsigned char *conversion_buffer;
742 struct coding_system *coding;
08a24c47 743
3224dac1 744 struct tty_display_info *tty = FRAME_TTY (f);
08a24c47 745
ed8dad6b 746 tty_turn_off_insert (tty);
fca177d4 747 tty_hide_cursor (tty);
08a24c47 748
a168702a 749 /* Don't dare write in last column of bottom line, if Auto-Wrap,
ff11dfa1 750 since that would scroll the whole frame on some terminals. */
08a24c47 751
6548cf00
KL
752 if (AutoWrap (tty)
753 && curY (tty) + 1 == FRAME_LINES (f)
754 && (curX (tty) + len) == FRAME_COLS (f))
08a24c47 755 len --;
a4decb7f
KH
756 if (len <= 0)
757 return;
08a24c47 758
6548cf00 759 cmplus (tty, len);
177c0ea7 760
af645abf
KH
761 /* If terminal_coding does any conversion, use it, otherwise use
762 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
763 because it always return 1 if the member src_multibyte is 1. */
fad2f685
KL
764 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
765 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
6589fd67
KH
766 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
767 the tail. */
af645abf 768 coding->mode &= ~CODING_MODE_LAST_BLOCK;
177c0ea7 769
a4decb7f 770 while (len > 0)
08a24c47 771 {
a168702a 772 /* Identify a run of glyphs with the same face. */
32de38e4 773 int face_id = string->face_id;
a168702a 774 int n;
177c0ea7 775
a168702a 776 for (n = 1; n < len; ++n)
32de38e4 777 if (string[n].face_id != face_id)
a168702a
GM
778 break;
779
780 /* Turn appearance modes of the face of the run on. */
ed8dad6b 781 tty_highlight_if_desired (tty);
a168702a
GM
782 turn_on_face (f, face_id);
783
288d5132
KH
784 if (n == len)
785 /* This is the last run. */
786 coding->mode |= CODING_MODE_LAST_BLOCK;
787 conversion_buffer = encode_terminal_code (string, n, coding);
788 if (coding->produced > 0)
08a24c47 789 {
1a935bfd 790 BLOCK_INPUT;
fad2f685
KL
791 fwrite (conversion_buffer, 1, coding->produced, tty->output);
792 if (ferror (tty->output))
793 clearerr (tty->output);
794 if (tty->termscript)
795 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
1a935bfd 796 UNBLOCK_INPUT;
08a24c47 797 }
288d5132
KH
798 len -= n;
799 string += n;
a168702a
GM
800
801 /* Turn appearance modes off. */
802 turn_off_face (f, face_id);
ed8dad6b 803 tty_turn_off_highlight (tty);
a4decb7f 804 }
177c0ea7 805
6548cf00 806 cmcheckmagic (tty);
08a24c47
JB
807}
808
b870aa61
SM
809#ifdef HAVE_GPM /* Only used by GPM code. */
810
7be1c21a
MB
811static void
812tty_write_glyphs_with_face (f, string, len, face_id)
813 register struct frame *f;
e882229c
NR
814 register struct glyph *string;
815 register int len, face_id;
816{
e882229c
NR
817 unsigned char *conversion_buffer;
818 struct coding_system *coding;
819
7be1c21a
MB
820 struct tty_display_info *tty = FRAME_TTY (f);
821
822 tty_turn_off_insert (tty);
823 tty_hide_cursor (tty);
e882229c
NR
824
825 /* Don't dare write in last column of bottom line, if Auto-Wrap,
826 since that would scroll the whole frame on some terminals. */
827
7be1c21a
MB
828 if (AutoWrap (tty)
829 && curY (tty) + 1 == FRAME_LINES (f)
830 && (curX (tty) + len) == FRAME_COLS (f))
e882229c
NR
831 len --;
832 if (len <= 0)
833 return;
834
7be1c21a 835 cmplus (tty, len);
e882229c
NR
836
837 /* If terminal_coding does any conversion, use it, otherwise use
838 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
839 because it always return 1 if the member src_multibyte is 1. */
7be1c21a
MB
840 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
841 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
e882229c
NR
842 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
843 the tail. */
844 coding->mode &= ~CODING_MODE_LAST_BLOCK;
845
e882229c 846 /* Turn appearance modes of the face. */
7be1c21a 847 tty_highlight_if_desired (tty);
e882229c
NR
848 turn_on_face (f, face_id);
849
850 coding->mode |= CODING_MODE_LAST_BLOCK;
851 conversion_buffer = encode_terminal_code (string, len, coding);
852 if (coding->produced > 0)
853 {
854 BLOCK_INPUT;
7be1c21a
MB
855 fwrite (conversion_buffer, 1, coding->produced, tty->output);
856 if (ferror (tty->output))
857 clearerr (tty->output);
858 if (tty->termscript)
859 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
e882229c
NR
860 UNBLOCK_INPUT;
861 }
862
863 /* Turn appearance modes off. */
864 turn_off_face (f, face_id);
7be1c21a 865 tty_turn_off_highlight (tty);
e882229c 866
7be1c21a 867 cmcheckmagic (tty);
e882229c 868}
b870aa61 869#endif
e882229c 870
da8e1115 871/* An implementation of insert_glyphs for termcap frames. */
177c0ea7 872
ed8dad6b 873static void
385ed61f 874tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
08a24c47
JB
875{
876 char *buf;
6bbd7a29 877 struct glyph *glyph = NULL;
af645abf
KH
878 unsigned char *conversion_buffer;
879 unsigned char space[1];
880 struct coding_system *coding;
08a24c47 881
3224dac1 882 struct tty_display_info *tty = FRAME_TTY (f);
a168702a 883
fca177d4 884 if (tty->TS_ins_multi_chars)
08a24c47 885 {
fca177d4 886 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len);
6548cf00 887 OUTPUT1 (tty, buf);
9ac0d9e0 888 xfree (buf);
08a24c47 889 if (start)
385ed61f 890 write_glyphs (f, start, len);
08a24c47
JB
891 return;
892 }
893
ed8dad6b 894 tty_turn_on_insert (tty);
6548cf00 895 cmplus (tty, len);
288d5132
KH
896
897 if (! start)
898 space[0] = SPACEGLYPH;
899
900 /* If terminal_coding does any conversion, use it, otherwise use
901 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
902 because it always return 1 if the member src_multibyte is 1. */
fad2f685
KL
903 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
904 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
288d5132
KH
905 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
906 the tail. */
907 coding->mode &= ~CODING_MODE_LAST_BLOCK;
908
07109bf9 909 while (len-- > 0)
08a24c47 910 {
fca177d4 911 OUTPUT1_IF (tty, tty->TS_ins_char);
08a24c47 912 if (!start)
32de38e4 913 {
288d5132
KH
914 conversion_buffer = space;
915 coding->produced = 1;
32de38e4 916 }
08a24c47 917 else
08a24c47 918 {
ed8dad6b 919 tty_highlight_if_desired (tty);
32de38e4 920 turn_on_face (f, start->face_id);
816be8b8 921 glyph = start;
a168702a 922 ++start;
a4decb7f
KH
923 /* We must open sufficient space for a character which
924 occupies more than one column. */
a168702a 925 while (len && CHAR_GLYPH_PADDING_P (*start))
a4decb7f 926 {
fca177d4 927 OUTPUT1_IF (tty, tty->TS_ins_char);
a4decb7f
KH
928 start++, len--;
929 }
288d5132
KH
930
931 if (len <= 0)
932 /* This is the last glyph. */
933 coding->mode |= CODING_MODE_LAST_BLOCK;
934
fad2f685 935 conversion_buffer = encode_terminal_code (glyph, 1, coding);
32de38e4 936 }
a4decb7f 937
288d5132 938 if (coding->produced > 0)
08a24c47 939 {
1a935bfd 940 BLOCK_INPUT;
fad2f685 941 fwrite (conversion_buffer, 1, coding->produced, tty->output);
0b0d3e0b
KL
942 if (ferror (tty->output))
943 clearerr (tty->output);
944 if (tty->termscript)
fad2f685 945 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
1a935bfd 946 UNBLOCK_INPUT;
08a24c47
JB
947 }
948
fca177d4 949 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
32de38e4 950 if (start)
65aa5e85
GM
951 {
952 turn_off_face (f, glyph->face_id);
ed8dad6b 953 tty_turn_off_highlight (tty);
65aa5e85 954 }
9a6b6f92 955 }
177c0ea7 956
6548cf00 957 cmcheckmagic (tty);
08a24c47
JB
958}
959
da8e1115
KL
960/* An implementation of delete_glyphs for termcap frames. */
961
ed8dad6b 962static void
385ed61f 963tty_delete_glyphs (struct frame *f, int n)
08a24c47
JB
964{
965 char *buf;
966 register int i;
967
3224dac1 968 struct tty_display_info *tty = FRAME_TTY (f);
08a24c47 969
fca177d4 970 if (tty->delete_in_insert_mode)
08a24c47 971 {
ed8dad6b 972 tty_turn_on_insert (tty);
08a24c47
JB
973 }
974 else
975 {
ed8dad6b 976 tty_turn_off_insert (tty);
fca177d4 977 OUTPUT_IF (tty, tty->TS_delete_mode);
08a24c47
JB
978 }
979
fca177d4 980 if (tty->TS_del_multi_chars)
08a24c47 981 {
fca177d4 982 buf = tparam (tty->TS_del_multi_chars, 0, 0, n);
6548cf00 983 OUTPUT1 (tty, buf);
9ac0d9e0 984 xfree (buf);
08a24c47
JB
985 }
986 else
987 for (i = 0; i < n; i++)
fca177d4
KL
988 OUTPUT1 (tty, tty->TS_del_char);
989 if (!tty->delete_in_insert_mode)
990 OUTPUT_IF (tty, tty->TS_end_delete_mode);
08a24c47
JB
991}
992\f
da8e1115 993/* An implementation of ins_del_lines for termcap frames. */
08a24c47 994
ed8dad6b 995static void
385ed61f 996tty_ins_del_lines (struct frame *f, int vpos, int n)
08a24c47 997{
3224dac1
KL
998 struct tty_display_info *tty = FRAME_TTY (f);
999 char *multi = n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
1000 char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
1001 char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
08a24c47
JB
1002
1003 register int i = n > 0 ? n : -n;
1004 register char *buf;
1005
08a24c47
JB
1006 /* If the lines below the insertion are being pushed
1007 into the end of the window, this is the same as clearing;
1008 and we know the lines are already clear, since the matching
1009 deletion has already been done. So can ignore this. */
1010 /* If the lines below the deletion are blank lines coming
1011 out of the end of the window, don't bother,
1012 as there will be a matching inslines later that will flush them. */
3224dac1
KL
1013 if (FRAME_SCROLL_REGION_OK (f)
1014 && vpos + i >= tty->specified_window)
08a24c47 1015 return;
3224dac1
KL
1016 if (!FRAME_MEMORY_BELOW_FRAME (f)
1017 && vpos + i >= FRAME_LINES (f))
08a24c47 1018 return;
3224dac1 1019
08a24c47
JB
1020 if (multi)
1021 {
6839f1e2 1022 raw_cursor_to (f, vpos, 0);
ed8dad6b 1023 tty_background_highlight (tty);
08a24c47 1024 buf = tparam (multi, 0, 0, i);
3224dac1 1025 OUTPUT (tty, buf);
9ac0d9e0 1026 xfree (buf);
08a24c47
JB
1027 }
1028 else if (single)
1029 {
6839f1e2 1030 raw_cursor_to (f, vpos, 0);
ed8dad6b 1031 tty_background_highlight (tty);
08a24c47 1032 while (--i >= 0)
3224dac1
KL
1033 OUTPUT (tty, single);
1034 if (tty->TF_teleray)
1035 curX (tty) = 0;
08a24c47
JB
1036 }
1037 else
1038 {
ed8dad6b 1039 tty_set_scroll_region (f, vpos, tty->specified_window);
08a24c47 1040 if (n < 0)
6839f1e2 1041 raw_cursor_to (f, tty->specified_window - 1, 0);
08a24c47 1042 else
6839f1e2 1043 raw_cursor_to (f, vpos, 0);
ed8dad6b 1044 tty_background_highlight (tty);
08a24c47 1045 while (--i >= 0)
3224dac1 1046 OUTPUTL (tty, scroll, tty->specified_window - vpos);
ed8dad6b 1047 tty_set_scroll_region (f, 0, tty->specified_window);
08a24c47 1048 }
3224dac1
KL
1049
1050 if (!FRAME_SCROLL_REGION_OK (f)
1051 && FRAME_MEMORY_BELOW_FRAME (f)
1052 && n < 0)
08a24c47 1053 {
385ed61f
KL
1054 cursor_to (f, FRAME_LINES (f) + n, 0);
1055 clear_to_end (f);
08a24c47
JB
1056 }
1057}
1058\f
1059/* Compute cost of sending "str", in characters,
1060 not counting any line-dependent padding. */
1061
1062int
6839f1e2 1063string_cost (char *str)
08a24c47
JB
1064{
1065 cost = 0;
1066 if (str)
1067 tputs (str, 0, evalcost);
1068 return cost;
1069}
1070
1071/* Compute cost of sending "str", in characters,
1072 counting any line-dependent padding at one line. */
1073
1074static int
6839f1e2 1075string_cost_one_line (char *str)
08a24c47
JB
1076{
1077 cost = 0;
1078 if (str)
1079 tputs (str, 1, evalcost);
1080 return cost;
1081}
1082
1083/* Compute per line amount of line-dependent padding,
1084 in tenths of characters. */
1085
1086int
6839f1e2 1087per_line_cost (char *str)
08a24c47
JB
1088{
1089 cost = 0;
1090 if (str)
1091 tputs (str, 0, evalcost);
1092 cost = - cost;
1093 if (str)
1094 tputs (str, 10, evalcost);
1095 return cost;
1096}
1097
1098#ifndef old
1099/* char_ins_del_cost[n] is cost of inserting N characters.
8dd0c7cb 1100 char_ins_del_cost[-n] is cost of deleting N characters.
9882535b 1101 The length of this vector is based on max_frame_cols. */
08a24c47
JB
1102
1103int *char_ins_del_vector;
1104
9882535b 1105#define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
08a24c47
JB
1106#endif
1107
1108/* ARGSUSED */
1109static void
6839f1e2 1110calculate_ins_del_char_costs (struct frame *f)
08a24c47 1111{
28d7d09f 1112 struct tty_display_info *tty = FRAME_TTY (f);
08a24c47
JB
1113 int ins_startup_cost, del_startup_cost;
1114 int ins_cost_per_char, del_cost_per_char;
1115 register int i;
1116 register int *p;
1117
fca177d4 1118 if (tty->TS_ins_multi_chars)
08a24c47
JB
1119 {
1120 ins_cost_per_char = 0;
fca177d4 1121 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
08a24c47 1122 }
fca177d4
KL
1123 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1124 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
08a24c47 1125 {
fca177d4
KL
1126 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1127 + string_cost (tty->TS_end_insert_mode))) / 100;
1128 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1129 + string_cost_one_line (tty->TS_pad_inserted_char));
08a24c47
JB
1130 }
1131 else
1132 {
1133 ins_startup_cost = 9999;
1134 ins_cost_per_char = 0;
1135 }
1136
fca177d4 1137 if (tty->TS_del_multi_chars)
08a24c47
JB
1138 {
1139 del_cost_per_char = 0;
fca177d4 1140 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
08a24c47 1141 }
fca177d4 1142 else if (tty->TS_del_char)
08a24c47 1143 {
fca177d4
KL
1144 del_startup_cost = (string_cost (tty->TS_delete_mode)
1145 + string_cost (tty->TS_end_delete_mode));
1146 if (tty->delete_in_insert_mode)
08a24c47 1147 del_startup_cost /= 2;
fca177d4 1148 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
08a24c47
JB
1149 }
1150 else
1151 {
1152 del_startup_cost = 9999;
1153 del_cost_per_char = 0;
1154 }
1155
1156 /* Delete costs are at negative offsets */
fca177d4
KL
1157 p = &char_ins_del_cost (f)[0];
1158 for (i = FRAME_COLS (f); --i >= 0;)
08a24c47
JB
1159 *--p = (del_startup_cost += del_cost_per_char);
1160
1161 /* Doing nothing is free */
fca177d4 1162 p = &char_ins_del_cost (f)[0];
08a24c47
JB
1163 *p++ = 0;
1164
1165 /* Insert costs are at positive offsets */
fca177d4 1166 for (i = FRAME_COLS (f); --i >= 0;)
08a24c47
JB
1167 *p++ = (ins_startup_cost += ins_cost_per_char);
1168}
1169
dfcf069d 1170void
6839f1e2 1171calculate_costs (struct frame *frame)
08a24c47 1172{
9f732a77 1173 FRAME_COST_BAUD_RATE (frame) = baud_rate;
08a24c47 1174
28d440ab 1175 if (FRAME_TERMCAP_P (frame))
daf01701
KL
1176 {
1177 struct tty_display_info *tty = FRAME_TTY (frame);
1178 register char *f = (tty->TS_set_scroll_region
1179 ? tty->TS_set_scroll_region
1180 : tty->TS_set_scroll_region_1);
08a24c47 1181
daf01701 1182 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
08a24c47 1183
daf01701 1184 tty->costs_set = 1;
08a24c47 1185
daf01701
KL
1186 /* These variables are only used for terminal stuff. They are
1187 allocated once for the terminal frame of X-windows emacs, but not
1188 used afterwards.
8dd0c7cb 1189
daf01701
KL
1190 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1191 X turns off char_ins_del_ok. */
08a24c47 1192
daf01701
KL
1193 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1194 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
08a24c47 1195
daf01701
KL
1196 if (char_ins_del_vector != 0)
1197 char_ins_del_vector
1198 = (int *) xrealloc (char_ins_del_vector,
1199 (sizeof (int)
1200 + 2 * max_frame_cols * sizeof (int)));
1201 else
1202 char_ins_del_vector
1203 = (int *) xmalloc (sizeof (int)
1204 + 2 * max_frame_cols * sizeof (int));
08a24c47 1205
daf01701
KL
1206 bzero (char_ins_del_vector, (sizeof (int)
1207 + 2 * max_frame_cols * sizeof (int)));
08a24c47 1208
daf01701
KL
1209
1210 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1211 do_line_insertion_deletion_costs (frame,
1212 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1213 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1214 f, f, 1);
1215 else
1216 do_line_insertion_deletion_costs (frame,
1217 tty->TS_ins_line, tty->TS_ins_multi_lines,
1218 tty->TS_del_line, tty->TS_del_multi_lines,
1219 0, 0, 1);
1220
1221 calculate_ins_del_char_costs (frame);
1222
1223 /* Don't use TS_repeat if its padding is worse than sending the chars */
1224 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1225 tty->RPov = string_cost (tty->TS_repeat);
1226 else
1227 tty->RPov = FRAME_COLS (frame) * 2;
08a24c47 1228
daf01701
KL
1229 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1230 }
08a24c47
JB
1231}
1232\f
a796ac82
JB
1233struct fkey_table {
1234 char *cap, *name;
1235};
1236
01d8deb0
ER
1237 /* Termcap capability names that correspond directly to X keysyms.
1238 Some of these (marked "terminfo") aren't supplied by old-style
1239 (Berkeley) termcap entries. They're listed in X keysym order;
1240 except we put the keypad keys first, so that if they clash with
1241 other keys (as on the IBM PC keyboard) they get overridden.
1242 */
1243
a168702a
GM
1244static struct fkey_table keys[] =
1245{
8103ad1a
PJ
1246 {"kh", "home"}, /* termcap */
1247 {"kl", "left"}, /* termcap */
1248 {"ku", "up"}, /* termcap */
1249 {"kr", "right"}, /* termcap */
1250 {"kd", "down"}, /* termcap */
1251 {"%8", "prior"}, /* terminfo */
1252 {"%5", "next"}, /* terminfo */
1253 {"@7", "end"}, /* terminfo */
1254 {"@1", "begin"}, /* terminfo */
1255 {"*6", "select"}, /* terminfo */
1256 {"%9", "print"}, /* terminfo */
1257 {"@4", "execute"}, /* terminfo --- actually the `command' key */
01d8deb0
ER
1258 /*
1259 * "insert" --- see below
1260 */
8103ad1a
PJ
1261 {"&8", "undo"}, /* terminfo */
1262 {"%0", "redo"}, /* terminfo */
1263 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1264 {"@0", "find"}, /* terminfo */
1265 {"@2", "cancel"}, /* terminfo */
1266 {"%1", "help"}, /* terminfo */
01d8deb0
ER
1267 /*
1268 * "break" goes here, but can't be reliably intercepted with termcap
1269 */
8103ad1a 1270 {"&4", "reset"}, /* terminfo --- actually `restart' */
01d8deb0
ER
1271 /*
1272 * "system" and "user" --- no termcaps
1273 */
8103ad1a
PJ
1274 {"kE", "clearline"}, /* terminfo */
1275 {"kA", "insertline"}, /* terminfo */
1276 {"kL", "deleteline"}, /* terminfo */
1277 {"kI", "insertchar"}, /* terminfo */
1278 {"kD", "deletechar"}, /* terminfo */
1279 {"kB", "backtab"}, /* terminfo */
01d8deb0
ER
1280 /*
1281 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1282 */
8103ad1a 1283 {"@8", "kp-enter"}, /* terminfo */
01d8deb0
ER
1284 /*
1285 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1286 * "kp-multiply", "kp-add", "kp-separator",
1287 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1288 * --- no termcaps for any of these.
1289 */
8103ad1a 1290 {"K4", "kp-1"}, /* terminfo */
01d8deb0
ER
1291 /*
1292 * "kp-2" --- no termcap
1293 */
8103ad1a 1294 {"K5", "kp-3"}, /* terminfo */
01d8deb0
ER
1295 /*
1296 * "kp-4" --- no termcap
1297 */
8103ad1a 1298 {"K2", "kp-5"}, /* terminfo */
01d8deb0
ER
1299 /*
1300 * "kp-6" --- no termcap
1301 */
8103ad1a 1302 {"K1", "kp-7"}, /* terminfo */
01d8deb0
ER
1303 /*
1304 * "kp-8" --- no termcap
1305 */
8103ad1a 1306 {"K3", "kp-9"}, /* terminfo */
01d8deb0
ER
1307 /*
1308 * "kp-equal" --- no termcap
1309 */
8103ad1a
PJ
1310 {"k1", "f1"},
1311 {"k2", "f2"},
1312 {"k3", "f3"},
1313 {"k4", "f4"},
1314 {"k5", "f5"},
1315 {"k6", "f6"},
1316 {"k7", "f7"},
1317 {"k8", "f8"},
60ec7b7e
DN
1318 {"k9", "f9"},
1319
1320 {"&0", "S-cancel"}, /*shifted cancel key*/
1321 {"&9", "S-begin"}, /*shifted begin key*/
1322 {"*0", "S-find"}, /*shifted find key*/
1323 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1324 {"*4", "S-delete"}, /*shifted delete-character key*/
1325 {"*7", "S-end"}, /*shifted end key*/
1326 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1327 {"#1", "S-help"}, /*shifted help key*/
1328 {"#2", "S-home"}, /*shifted home key*/
1329 {"#3", "S-insert"}, /*shifted insert-character key*/
1330 {"#4", "S-left"}, /*shifted left-arrow key*/
1331 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1332 {"%c", "S-next"}, /*shifted next key*/
1333 {"%e", "S-prior"}, /*shifted previous key*/
1334 {"%f", "S-print"}, /*shifted print key*/
1335 {"%g", "S-redo"}, /*shifted redo key*/
1336 {"%i", "S-right"}, /*shifted right-arrow key*/
1337 {"!3", "S-undo"} /*shifted undo key*/
a796ac82
JB
1338 };
1339
e7cf0fa0
KL
1340static char **term_get_fkeys_address;
1341static KBOARD *term_get_fkeys_kboard;
f2a00342 1342static Lisp_Object term_get_fkeys_1 ();
465db27b 1343
4f4a84ec 1344/* Find the escape codes sent by the function keys for Vinput_decode_map.
177c0ea7 1345 This function scans the termcap function key sequence entries, and
4f4a84ec 1346 adds entries to Vinput_decode_map for each function key it finds. */
01d8deb0 1347
ed8dad6b 1348static void
e7cf0fa0 1349term_get_fkeys (address, kboard)
5c2c7893 1350 char **address;
e7cf0fa0 1351 KBOARD *kboard;
f2a00342
RM
1352{
1353 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1354 errors during the call. The only errors should be from Fdefine_key
1355 when given a key sequence containing an invalid prefix key. If the
1356 termcap defines function keys which use a prefix that is already bound
1357 to a command by the default bindings, we should silently ignore that
1358 function key specification, rather than giving the user an error and
1359 refusing to run at all on such a terminal. */
1360
1361 extern Lisp_Object Fidentity ();
e7cf0fa0
KL
1362 term_get_fkeys_address = address;
1363 term_get_fkeys_kboard = kboard;
f2a00342
RM
1364 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1365}
1366
1367static Lisp_Object
1368term_get_fkeys_1 ()
5c2c7893 1369{
5c2c7893
JB
1370 int i;
1371
e7cf0fa0
KL
1372 char **address = term_get_fkeys_address;
1373 KBOARD *kboard = term_get_fkeys_kboard;
1374
3e65092f 1375 /* This can happen if CANNOT_DUMP or with strange options. */
4f4a84ec
SM
1376 if (!KEYMAPP (kboard->Vinput_decode_map))
1377 kboard->Vinput_decode_map = Fmake_sparse_keymap (Qnil);
3e65092f 1378
5c2c7893
JB
1379 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1380 {
1381 char *sequence = tgetstr (keys[i].cap, address);
1382 if (sequence)
4f4a84ec 1383 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
f2a00342
RM
1384 Fmake_vector (make_number (1),
1385 intern (keys[i].name)));
5c2c7893 1386 }
a796ac82
JB
1387
1388 /* The uses of the "k0" capability are inconsistent; sometimes it
1389 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
eb8c3be9 1390 We will attempt to politely accommodate both systems by testing for
a796ac82
JB
1391 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1392 */
1393 {
1394 char *k_semi = tgetstr ("k;", address);
1395 char *k0 = tgetstr ("k0", address);
1396 char *k0_name = "f10";
1397
1398 if (k_semi)
1399 {
95c11956
SM
1400 if (k0)
1401 /* Define f0 first, so that f10 takes precedence in case the
1402 key sequences happens to be the same. */
4f4a84ec 1403 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
95c11956 1404 Fmake_vector (make_number (1), intern ("f0")));
4f4a84ec 1405 Fdefine_key (kboard->Vinput_decode_map, build_string (k_semi),
f2a00342 1406 Fmake_vector (make_number (1), intern ("f10")));
a796ac82 1407 }
95c11956 1408 else if (k0)
4f4a84ec 1409 Fdefine_key (kboard->Vinput_decode_map, build_string (k0),
f2a00342 1410 Fmake_vector (make_number (1), intern (k0_name)));
a796ac82 1411 }
01d8deb0
ER
1412
1413 /* Set up cookies for numbered function keys above f10. */
1414 {
1415 char fcap[3], fkey[4];
1416
fc4f24da 1417 fcap[0] = 'F'; fcap[2] = '\0';
01d8deb0
ER
1418 for (i = 11; i < 64; i++)
1419 {
1420 if (i <= 19)
1421 fcap[1] = '1' + i - 11;
1422 else if (i <= 45)
b59ab95c 1423 fcap[1] = 'A' + i - 20;
01d8deb0 1424 else
b59ab95c 1425 fcap[1] = 'a' + i - 46;
01d8deb0 1426
fc4f24da
RS
1427 {
1428 char *sequence = tgetstr (fcap, address);
1429 if (sequence)
1430 {
465db27b 1431 sprintf (fkey, "f%d", i);
4f4a84ec 1432 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence),
f2a00342
RM
1433 Fmake_vector (make_number (1),
1434 intern (fkey)));
fc4f24da
RS
1435 }
1436 }
01d8deb0
ER
1437 }
1438 }
1439
1440 /*
1441 * Various mappings to try and get a better fit.
1442 */
1443 {
fc4f24da
RS
1444#define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1445 if (!tgetstr (cap1, address)) \
1446 { \
1447 char *sequence = tgetstr (cap2, address); \
e7cf0fa0 1448 if (sequence) \
4f4a84ec 1449 Fdefine_key (kboard->Vinput_decode_map, build_string (sequence), \
e7cf0fa0
KL
1450 Fmake_vector (make_number (1), \
1451 intern (sym))); \
fc4f24da 1452 }
177c0ea7 1453
01d8deb0 1454 /* if there's no key_next keycap, map key_npage to `next' keysym */
27b61785 1455 CONDITIONAL_REASSIGN ("%5", "kN", "next");
01d8deb0 1456 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
381d11a1 1457 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
01d8deb0 1458 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
27b61785 1459 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
403c995b
RS
1460 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1461 CONDITIONAL_REASSIGN ("@7", "kH", "end");
0a7f697a
KH
1462
1463 /* IBM has their own non-standard dialect of terminfo.
1464 If the standard name isn't found, try the IBM name. */
1465 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1466 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1467 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1468 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1469 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1470 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1471 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1472 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1473 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1dd40212 1474#undef CONDITIONAL_REASSIGN
01d8deb0 1475 }
a168702a
GM
1476
1477 return Qnil;
5c2c7893
JB
1478}
1479
1480\f
a168702a
GM
1481/***********************************************************************
1482 Character Display Information
1483 ***********************************************************************/
1484
47021b11
KS
1485/* Avoid name clash with functions defined in xterm.c */
1486#ifdef static
1487#define append_glyph append_glyph_term
1488#define produce_stretch_glyph produce_stretch_glyph_term
4c12d738
KH
1489#define append_composite_glyph append_composite_glyph_term
1490#define produce_composite_glyph produce_composite_glyph_term
47021b11
KS
1491#endif
1492
a168702a 1493static void append_glyph P_ ((struct it *));
6b61353c 1494static void produce_stretch_glyph P_ ((struct it *));
4c12d738
KH
1495static void append_composite_glyph P_ ((struct it *));
1496static void produce_composite_glyph P_ ((struct it *));
a168702a
GM
1497
1498/* Append glyphs to IT's glyph_row. Called from produce_glyphs for
d2b4c17d
KH
1499 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1500 the character for which to produce glyphs; IT->face_id contains the
1501 character's face. Padding glyphs are appended if IT->c has a
1502 IT->pixel_width > 1. */
177c0ea7 1503
a168702a
GM
1504static void
1505append_glyph (it)
1506 struct it *it;
1507{
1508 struct glyph *glyph, *end;
1509 int i;
1510
1511 xassert (it->glyph_row);
1512 glyph = (it->glyph_row->glyphs[it->area]
1513 + it->glyph_row->used[it->area]);
1514 end = it->glyph_row->glyphs[1 + it->area];
1515
177c0ea7
JB
1516 for (i = 0;
1517 i < it->pixel_width && glyph < end;
a168702a
GM
1518 ++i)
1519 {
1520 glyph->type = CHAR_GLYPH;
d13f3e2e 1521 glyph->pixel_width = 1;
d2b4c17d 1522 glyph->u.ch = it->char_to_display;
32de38e4
KH
1523 glyph->face_id = it->face_id;
1524 glyph->padding_p = i > 0;
a168702a
GM
1525 glyph->charpos = CHARPOS (it->position);
1526 glyph->object = it->object;
177c0ea7 1527
a168702a
GM
1528 ++it->glyph_row->used[it->area];
1529 ++glyph;
1530 }
1531}
1532
1533
b50fe468
RS
1534/* Produce glyphs for the display element described by IT. *IT
1535 specifies what we want to produce a glyph for (character, image, ...),
1536 and where in the glyph matrix we currently are (glyph row and hpos).
1537 produce_glyphs fills in output fields of *IT with information such as the
1538 pixel width and height of a character, and maybe output actual glyphs at
a168702a 1539 the same time if IT->glyph_row is non-null. See the explanation of
b50fe468
RS
1540 struct display_iterator in dispextern.h for an overview.
1541
1542 produce_glyphs also stores the result of glyph width, ascent
1543 etc. computations in *IT.
1544
1545 IT->glyph_row may be null, in which case produce_glyphs does not
1546 actually fill in the glyphs. This is used in the move_* functions
1547 in xdisp.c for text width and height computations.
1548
1549 Callers usually don't call produce_glyphs directly;
1550 instead they use the macro PRODUCE_GLYPHS. */
a168702a 1551
177c0ea7 1552void
a168702a
GM
1553produce_glyphs (it)
1554 struct it *it;
1555{
1556 /* If a hook is installed, let it do the work. */
4c12d738
KH
1557
1558 /* Nothing but characters are supported on terminal frames. */
a168702a 1559 xassert (it->what == IT_CHARACTER
c7cba11d 1560 || it->what == IT_COMPOSITION
a168702a 1561 || it->what == IT_STRETCH);
177c0ea7 1562
6b61353c
KH
1563 if (it->what == IT_STRETCH)
1564 {
1565 produce_stretch_glyph (it);
1566 goto done;
1567 }
1568
4c12d738
KH
1569 if (it->what == IT_COMPOSITION)
1570 {
1571 produce_composite_glyph (it);
1572 goto done;
1573 }
a168702a 1574
d2b4c17d
KH
1575 /* Maybe translate single-byte characters to multibyte. */
1576 it->char_to_display = it->c;
1577
a168702a
GM
1578 if (it->c >= 040 && it->c < 0177)
1579 {
1580 it->pixel_width = it->nglyphs = 1;
1581 if (it->glyph_row)
1582 append_glyph (it);
1583 }
1584 else if (it->c == '\n')
1585 it->pixel_width = it->nglyphs = 0;
1586 else if (it->c == '\t')
1587 {
2efdbcdd 1588 int absolute_x = (it->current_x
a168702a 1589 + it->continuation_lines_width);
177c0ea7
JB
1590 int next_tab_x
1591 = (((1 + absolute_x + it->tab_width - 1)
a168702a
GM
1592 / it->tab_width)
1593 * it->tab_width);
1594 int nspaces;
1595
1596 /* If part of the TAB has been displayed on the previous line
1597 which is continued now, continuation_lines_width will have
1598 been incremented already by the part that fitted on the
1599 continued line. So, we will get the right number of spaces
1600 here. */
1601 nspaces = next_tab_x - absolute_x;
177c0ea7 1602
a168702a
GM
1603 if (it->glyph_row)
1604 {
1605 int n = nspaces;
177c0ea7 1606
d2b4c17d 1607 it->char_to_display = ' ';
a168702a 1608 it->pixel_width = it->len = 1;
177c0ea7 1609
a168702a
GM
1610 while (n--)
1611 append_glyph (it);
a168702a
GM
1612 }
1613
1614 it->pixel_width = nspaces;
1615 it->nglyphs = nspaces;
1616 }
c5a518df 1617 else if (CHAR_BYTE8_P (it->c))
add44890 1618 {
5b203baa
KH
1619 if (unibyte_display_via_language_environment
1620 && (it->c >= 0240))
1621 {
1622 it->char_to_display = unibyte_char_to_multibyte (it->c);
1623 it->pixel_width = CHAR_WIDTH (it->char_to_display);
1624 it->nglyphs = it->pixel_width;
1625 if (it->glyph_row)
1626 append_glyph (it);
1627 }
1628 else
1629 {
1630 /* Coming here means that it->c is from display table, thus
1631 we must send the raw 8-bit byte as is to the terminal.
1632 Although there's no way to know how many columns it
1633 occupies on a screen, it is a good assumption that a
1634 single byte code has 1-column width. */
1635 it->pixel_width = it->nglyphs = 1;
1636 if (it->glyph_row)
1637 append_glyph (it);
1638 }
add44890 1639 }
a168702a
GM
1640 else
1641 {
9332ea03 1642 it->pixel_width = CHAR_WIDTH (it->c);
a168702a 1643 it->nglyphs = it->pixel_width;
177c0ea7 1644
a168702a
GM
1645 if (it->glyph_row)
1646 append_glyph (it);
1647 }
1648
6b61353c 1649 done:
177c0ea7 1650 /* Advance current_x by the pixel width as a convenience for
a168702a
GM
1651 the caller. */
1652 if (it->area == TEXT_AREA)
1653 it->current_x += it->pixel_width;
cfe8a05e
GM
1654 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1655 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
a168702a
GM
1656}
1657
1658
6b61353c
KH
1659/* Produce a stretch glyph for iterator IT. IT->object is the value
1660 of the glyph property displayed. The value must be a list
1661 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1662 being recognized:
1663
1664 1. `:width WIDTH' specifies that the space should be WIDTH *
1665 canonical char width wide. WIDTH may be an integer or floating
1666 point number.
1667
1668 2. `:align-to HPOS' specifies that the space should be wide enough
1669 to reach HPOS, a value in canonical character units. */
1670
1671static void
1672produce_stretch_glyph (it)
1673 struct it *it;
1674{
1675 /* (space :width WIDTH ...) */
1676 Lisp_Object prop, plist;
1677 int width = 0, align_to = -1;
1678 int zero_width_ok_p = 0;
1679 double tem;
1680
1681 /* List should start with `space'. */
1682 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1683 plist = XCDR (it->object);
1684
1685 /* Compute the width of the stretch. */
1686 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1687 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1688 {
1689 /* Absolute width `:width WIDTH' specified and valid. */
1690 zero_width_ok_p = 1;
1691 width = (int)(tem + 0.5);
1692 }
1693 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1694 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1695 {
1696 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
feef4f84 1697 align_to = (align_to < 0
6b61353c
KH
1698 ? 0
1699 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1700 else if (align_to < 0)
1701 align_to = window_box_left_offset (it->w, TEXT_AREA);
1702 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1703 zero_width_ok_p = 1;
1704 }
1705 else
1706 /* Nothing specified -> width defaults to canonical char width. */
1707 width = FRAME_COLUMN_WIDTH (it->f);
1708
1709 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1710 width = 1;
1711
1712 if (width > 0 && it->glyph_row)
1713 {
1714 Lisp_Object o_object = it->object;
1715 Lisp_Object object = it->stack[it->sp - 1].string;
1716 int n = width;
6b61353c
KH
1717
1718 if (!STRINGP (object))
1719 object = it->w->buffer;
1720 it->object = object;
d2b4c17d 1721 it->char_to_display = ' ';
6b61353c
KH
1722 it->pixel_width = it->len = 1;
1723 while (n--)
1724 append_glyph (it);
1725 it->object = o_object;
6b61353c
KH
1726 }
1727 it->pixel_width = width;
1728 it->nglyphs = width;
1729}
1730
1731
4c12d738
KH
1732/* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1733 Called from produce_composite_glyph for terminal frames if
1734 IT->glyph_row != NULL. IT->face_id contains the character's
1735 face. */
1736
1737static void
1738append_composite_glyph (it)
1739 struct it *it;
1740{
1741 struct glyph *glyph;
1742
1743 xassert (it->glyph_row);
1744 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1745 if (glyph < it->glyph_row->glyphs[1 + it->area])
1746 {
1747 glyph->type = COMPOSITE_GLYPH;
1748 glyph->pixel_width = it->pixel_width;
1749 glyph->u.cmp_id = it->cmp_id;
1750 glyph->face_id = it->face_id;
1751 glyph->padding_p = 0;
1752 glyph->charpos = CHARPOS (it->position);
1753 glyph->object = it->object;
1754
1755 ++it->glyph_row->used[it->area];
1756 ++glyph;
1757 }
1758}
1759
1760
1761/* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1762 the composition. We simply produces components of the composition
1763 assuming that that the terminal has a capability to layout/render
1764 it correctly. */
1765
1766static void
1767produce_composite_glyph (it)
1768 struct it *it;
1769{
1770 struct composition *cmp = composition_table[it->cmp_id];
1771 int c;
1772
1773 xassert (cmp->glyph_len > 0);
1774 c = COMPOSITION_GLYPH (cmp, 0);
1775 it->pixel_width = CHAR_WIDTH (it->c);
1776 it->nglyphs = 1;
1777
1778 if (it->glyph_row)
1779 append_composite_glyph (it);
1780}
1781
1782
a168702a
GM
1783/* Get information about special display element WHAT in an
1784 environment described by IT. WHAT is one of IT_TRUNCATION or
1785 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1786 non-null glyph_row member. This function ensures that fields like
1787 face_id, c, len of IT are left untouched. */
1788
1789void
1790produce_special_glyphs (it, what)
1791 struct it *it;
1792 enum display_element_type what;
1793{
1794 struct it temp_it;
072d84a6 1795 GLYPH glyph;
177c0ea7 1796
a168702a
GM
1797 temp_it = *it;
1798 temp_it.dp = NULL;
1799 temp_it.what = IT_CHARACTER;
1800 temp_it.len = 1;
7c752c80 1801 temp_it.object = make_number (0);
a168702a
GM
1802 bzero (&temp_it.current, sizeof temp_it.current);
1803
1804 if (what == IT_CONTINUATION)
1805 {
1806 /* Continuation glyph. */
1807 if (it->dp
1808 && INTEGERP (DISP_CONTINUE_GLYPH (it->dp))
1809 && GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (it->dp))))
1810 {
072d84a6
RS
1811 glyph = XINT (DISP_CONTINUE_GLYPH (it->dp));
1812 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
a168702a
GM
1813 }
1814 else
072d84a6 1815 glyph = '\\';
a168702a
GM
1816 }
1817 else if (what == IT_TRUNCATION)
1818 {
1819 /* Truncation glyph. */
1820 if (it->dp
1821 && INTEGERP (DISP_TRUNC_GLYPH (it->dp))
1822 && GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (it->dp))))
1823 {
072d84a6
RS
1824 glyph = XINT (DISP_TRUNC_GLYPH (it->dp));
1825 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
a168702a
GM
1826 }
1827 else
072d84a6 1828 glyph = '$';
a168702a
GM
1829 }
1830 else
1831 abort ();
072d84a6
RS
1832
1833 temp_it.c = FAST_GLYPH_CHAR (glyph);
1834 temp_it.face_id = FAST_GLYPH_FACE (glyph);
1835 temp_it.len = CHAR_BYTES (temp_it.c);
1836
1837 produce_glyphs (&temp_it);
1838 it->pixel_width = temp_it.pixel_width;
1839 it->nglyphs = temp_it.pixel_width;
a168702a
GM
1840}
1841
1842
a168702a
GM
1843\f
1844/***********************************************************************
1845 Faces
1846 ***********************************************************************/
1847
4e6ba4a4
GM
1848/* Value is non-zero if attribute ATTR may be used. ATTR should be
1849 one of the enumerators from enum no_color_bit, or a bit set built
1850 from them. Some display attributes may not be used together with
1851 color; the termcap capability `NC' specifies which ones. */
1852
fca177d4
KL
1853#define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1854 (tty->TN_max_colors > 0 \
1855 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1856 : 1)
a168702a 1857
072d84a6
RS
1858/* Turn appearances of face FACE_ID on tty frame F on.
1859 FACE_ID is a realized face ID number, in the face cache. */
a168702a
GM
1860
1861static void
1862turn_on_face (f, face_id)
1863 struct frame *f;
1864 int face_id;
1865{
1866 struct face *face = FACE_FROM_ID (f, face_id);
86a7d192
GM
1867 long fg = face->foreground;
1868 long bg = face->background;
28d7d09f 1869 struct tty_display_info *tty = FRAME_TTY (f);
a168702a 1870
86a7d192
GM
1871 /* Do this first because TS_end_standout_mode may be the same
1872 as TS_exit_attribute_mode, which turns all appearances off. */
fca177d4 1873 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
86a7d192 1874 {
fca177d4 1875 if (tty->TN_max_colors > 0)
86a7d192
GM
1876 {
1877 if (fg >= 0 && bg >= 0)
1878 {
1879 /* If the terminal supports colors, we can set them
1880 below without using reverse video. The face's fg
1881 and bg colors are set as they should appear on
1882 the screen, i.e. they take the inverse-video'ness
1883 of the face already into account. */
1884 }
1885 else if (inverse_video)
1886 {
1887 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1888 || bg == FACE_TTY_DEFAULT_BG_COLOR)
ed8dad6b 1889 tty_toggle_highlight (tty);
86a7d192
GM
1890 }
1891 else
1892 {
1893 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1894 || bg == FACE_TTY_DEFAULT_FG_COLOR)
ed8dad6b 1895 tty_toggle_highlight (tty);
86a7d192
GM
1896 }
1897 }
1898 else
1899 {
1900 /* If we can't display colors, use reverse video
1901 if the face specifies that. */
37526b42
GM
1902 if (inverse_video)
1903 {
1904 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1905 || bg == FACE_TTY_DEFAULT_BG_COLOR)
ed8dad6b 1906 tty_toggle_highlight (tty);
37526b42
GM
1907 }
1908 else
1909 {
1910 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1911 || bg == FACE_TTY_DEFAULT_FG_COLOR)
ed8dad6b 1912 tty_toggle_highlight (tty);
37526b42 1913 }
86a7d192
GM
1914 }
1915 }
a168702a
GM
1916
1917 if (face->tty_bold_p)
4e6ba4a4 1918 {
fca177d4
KL
1919 if (MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1920 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
4e6ba4a4 1921 }
a168702a 1922 else if (face->tty_dim_p)
fca177d4
KL
1923 if (MAY_USE_WITH_COLORS_P (tty, NC_DIM))
1924 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
a168702a
GM
1925
1926 /* Alternate charset and blinking not yet used. */
4e6ba4a4 1927 if (face->tty_alt_charset_p
fca177d4
KL
1928 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
1929 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
a168702a 1930
4e6ba4a4 1931 if (face->tty_blinking_p
fca177d4
KL
1932 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
1933 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
a168702a 1934
fca177d4
KL
1935 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
1936 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
a168702a 1937
fca177d4 1938 if (tty->TN_max_colors > 0)
a168702a 1939 {
753d161b 1940 char *ts, *p;
177c0ea7 1941
fbf34973 1942 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
753d161b 1943 if (fg >= 0 && ts)
a168702a 1944 {
fbf34973 1945 p = tparam (ts, NULL, 0, (int) fg);
6548cf00 1946 OUTPUT (tty, p);
a168702a
GM
1947 xfree (p);
1948 }
1949
fbf34973 1950 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
753d161b 1951 if (bg >= 0 && ts)
a168702a 1952 {
fbf34973 1953 p = tparam (ts, NULL, 0, (int) bg);
6548cf00 1954 OUTPUT (tty, p);
a168702a
GM
1955 xfree (p);
1956 }
1957 }
1958}
177c0ea7 1959
a168702a
GM
1960
1961/* Turn off appearances of face FACE_ID on tty frame F. */
1962
1963static void
1964turn_off_face (f, face_id)
1965 struct frame *f;
1966 int face_id;
1967{
1968 struct face *face = FACE_FROM_ID (f, face_id);
28d7d09f 1969 struct tty_display_info *tty = FRAME_TTY (f);
a168702a
GM
1970
1971 xassert (face != NULL);
1972
fca177d4 1973 if (tty->TS_exit_attribute_mode)
a168702a
GM
1974 {
1975 /* Capability "me" will turn off appearance modes double-bright,
1976 half-bright, reverse-video, standout, underline. It may or
1977 may not turn off alt-char-mode. */
1978 if (face->tty_bold_p
1979 || face->tty_dim_p
1980 || face->tty_reverse_p
1981 || face->tty_alt_charset_p
1982 || face->tty_blinking_p
1983 || face->tty_underline_p)
65aa5e85 1984 {
fca177d4
KL
1985 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
1986 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
1987 tty->standout_mode = 0;
65aa5e85 1988 }
a168702a
GM
1989
1990 if (face->tty_alt_charset_p)
fca177d4 1991 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
a168702a
GM
1992 }
1993 else
1994 {
1995 /* If we don't have "me" we can only have those appearances
1996 that have exit sequences defined. */
1997 if (face->tty_alt_charset_p)
fca177d4 1998 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
a168702a 1999
54800acb 2000 if (face->tty_underline_p)
fca177d4 2001 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
a168702a
GM
2002 }
2003
2004 /* Switch back to default colors. */
fca177d4 2005 if (tty->TN_max_colors > 0
f9d2fdc4
EZ
2006 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2007 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2008 || (face->background != FACE_TTY_DEFAULT_COLOR
2009 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
fca177d4 2010 OUTPUT1_IF (tty, tty->TS_orig_pair);
a168702a 2011}
177c0ea7
JB
2012
2013
b63a55ac
MB
2014/* Return non-zero if the terminal on frame F supports all of the
2015 capabilities in CAPS simultaneously, with foreground and background
2016 colors FG and BG. */
2017
4a8130f2 2018int
fca177d4 2019tty_capable_p (tty, caps, fg, bg)
28d7d09f 2020 struct tty_display_info *tty;
b63a55ac
MB
2021 unsigned caps;
2022 unsigned long fg, bg;
2023{
fca177d4
KL
2024#define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2025 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
b63a55ac
MB
2026 return 0;
2027
fca177d4
KL
2028 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2029 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2030 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2031 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
2032 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
2033 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
b63a55ac
MB
2034
2035 /* We can do it! */
2036 return 1;
2037}
2038
a168702a
GM
2039/* Return non-zero if the terminal is capable to display colors. */
2040
2041DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
981e4297 2042 0, 1, 0,
6ed8eeff
KL
2043 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2044
2045TERMINAL can be a terminal id, a frame or nil (meaning the selected
2046frame's terminal). This function always returns nil if TERMINAL
2047is not on a tty device. */)
2048 (terminal)
2049 Lisp_Object terminal;
a168702a 2050{
717a00ef 2051 struct terminal *t = get_tty_terminal (terminal, 0);
6ed8eeff 2052 if (!t)
428a555e 2053 return Qnil;
3224dac1 2054 else
6ed8eeff 2055 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
a168702a
GM
2056}
2057
bfa62f96
EZ
2058/* Return the number of supported colors. */
2059DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2060 Stty_display_color_cells, 0, 1, 0,
6ed8eeff
KL
2061 doc: /* Return the number of colors supported by the tty device TERMINAL.
2062
2063TERMINAL can be a terminal id, a frame or nil (meaning the selected
717a00ef 2064frame's terminal). This function always returns 0 if TERMINAL
6ed8eeff
KL
2065is not on a tty device. */)
2066 (terminal)
2067 Lisp_Object terminal;
bfa62f96 2068{
717a00ef 2069 struct terminal *t = get_tty_terminal (terminal, 0);
6ed8eeff 2070 if (!t)
8c8d5f35 2071 return make_number (0);
3224dac1 2072 else
6ed8eeff 2073 return make_number (t->display_info.tty->TN_max_colors);
bfa62f96
EZ
2074}
2075
ace28297 2076#ifndef WINDOWSNT
a168702a 2077
5205ee62
GM
2078/* Declare here rather than in the function, as in the rest of Emacs,
2079 to work around an HPUX compiler bug (?). See
57407fb4 2080 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
5205ee62
GM
2081static int default_max_colors;
2082static int default_max_pairs;
2083static int default_no_color_video;
2084static char *default_orig_pair;
2085static char *default_set_foreground;
2086static char *default_set_background;
fcf8ff2e 2087
ace28297
EZ
2088/* Save or restore the default color-related capabilities of this
2089 terminal. */
2090static void
28d7d09f 2091tty_default_color_capabilities (struct tty_display_info *tty, int save)
ace28297 2092{
ace28297
EZ
2093
2094 if (save)
2095 {
2096 if (default_orig_pair)
2097 xfree (default_orig_pair);
fca177d4 2098 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
ace28297
EZ
2099
2100 if (default_set_foreground)
2101 xfree (default_set_foreground);
fca177d4 2102 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
ace28297
EZ
2103 : NULL;
2104
2105 if (default_set_background)
2106 xfree (default_set_background);
fca177d4 2107 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
ace28297
EZ
2108 : NULL;
2109
fca177d4
KL
2110 default_max_colors = tty->TN_max_colors;
2111 default_max_pairs = tty->TN_max_pairs;
2112 default_no_color_video = tty->TN_no_color_video;
ace28297
EZ
2113 }
2114 else
2115 {
fca177d4
KL
2116 tty->TS_orig_pair = default_orig_pair;
2117 tty->TS_set_foreground = default_set_foreground;
2118 tty->TS_set_background = default_set_background;
2119 tty->TN_max_colors = default_max_colors;
2120 tty->TN_max_pairs = default_max_pairs;
2121 tty->TN_no_color_video = default_no_color_video;
ace28297
EZ
2122 }
2123}
2124
2125/* Setup one of the standard tty color schemes according to MODE.
2126 MODE's value is generally the number of colors which we want to
2127 support; zero means set up for the default capabilities, the ones
a4c6993d 2128 we saw at init_tty time; -1 means turn off color support. */
ed8dad6b 2129static void
28d7d09f 2130tty_setup_colors (struct tty_display_info *tty, int mode)
ace28297 2131{
6b61353c
KH
2132 /* Canonicalize all negative values of MODE. */
2133 if (mode < -1)
2134 mode = -1;
2135
ace28297
EZ
2136 switch (mode)
2137 {
2138 case -1: /* no colors at all */
fca177d4
KL
2139 tty->TN_max_colors = 0;
2140 tty->TN_max_pairs = 0;
2141 tty->TN_no_color_video = 0;
2142 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
ace28297
EZ
2143 break;
2144 case 0: /* default colors, if any */
2145 default:
fca177d4 2146 tty_default_color_capabilities (tty, 0);
ace28297
EZ
2147 break;
2148 case 8: /* 8 standard ANSI colors */
fca177d4 2149 tty->TS_orig_pair = "\033[0m";
ace28297 2150#ifdef TERMINFO
fca177d4
KL
2151 tty->TS_set_foreground = "\033[3%p1%dm";
2152 tty->TS_set_background = "\033[4%p1%dm";
ace28297 2153#else
fca177d4
KL
2154 tty->TS_set_foreground = "\033[3%dm";
2155 tty->TS_set_background = "\033[4%dm";
ace28297 2156#endif
fca177d4
KL
2157 tty->TN_max_colors = 8;
2158 tty->TN_max_pairs = 64;
2159 tty->TN_no_color_video = 0;
ace28297
EZ
2160 break;
2161 }
2162}
2163
2164void
2165set_tty_color_mode (f, val)
2166 struct frame *f;
2167 Lisp_Object val;
2168{
dfc7a077 2169 Lisp_Object color_mode_spec, current_mode_spec;
ace28297
EZ
2170 Lisp_Object color_mode, current_mode;
2171 int mode, old_mode;
2172 extern Lisp_Object Qtty_color_mode;
2173 Lisp_Object tty_color_mode_alist;
2174
2175 tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
2176 Qnil);
2177
6b61353c 2178 if (INTEGERP (val))
ace28297
EZ
2179 color_mode = val;
2180 else
2181 {
2182 if (NILP (tty_color_mode_alist))
2183 color_mode_spec = Qnil;
2184 else
2185 color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
ace28297
EZ
2186
2187 if (CONSP (color_mode_spec))
2188 color_mode = XCDR (color_mode_spec);
2189 else
2190 color_mode = Qnil;
2191 }
6b61353c
KH
2192
2193 current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
2194
ace28297
EZ
2195 if (CONSP (current_mode_spec))
2196 current_mode = XCDR (current_mode_spec);
2197 else
2198 current_mode = Qnil;
6b61353c 2199 if (INTEGERP (color_mode))
ace28297
EZ
2200 mode = XINT (color_mode);
2201 else
2202 mode = 0; /* meaning default */
6b61353c 2203 if (INTEGERP (current_mode))
ace28297
EZ
2204 old_mode = XINT (current_mode);
2205 else
2206 old_mode = 0;
2207
2208 if (mode != old_mode)
2209 {
fca177d4 2210 tty_setup_colors (FRAME_TTY (f), mode);
ace28297
EZ
2211 /* This recomputes all the faces given the new color
2212 definitions. */
2213 call0 (intern ("tty-set-up-initial-frame-faces"));
2214 redraw_frame (f);
2215 }
2216}
2217
2218#endif /* !WINDOWSNT */
a168702a
GM
2219
2220\f
28d440ab 2221
6ed8eeff 2222/* Return the tty display object specified by TERMINAL. */
b6660415 2223
6ed8eeff 2224struct terminal *
717a00ef 2225get_tty_terminal (Lisp_Object terminal, int throw)
b6660415 2226{
717a00ef 2227 struct terminal *t = get_terminal (terminal, throw);
62af879c 2228
6ed8eeff 2229 if (t && t->type != output_termcap)
717a00ef
KL
2230 {
2231 if (throw)
2232 error ("Device %d is not a termcap terminal device", t->id);
2233 else
2234 return NULL;
2235 }
b6660415 2236
6ed8eeff 2237 return t;
b6660415
KL
2238}
2239
ab797f65
KL
2240/* Return an active termcap device that uses the tty device with the
2241 given name.
b6660415 2242
7e59217d 2243 This function ignores suspended devices.
da8e1115
KL
2244
2245 Returns NULL if the named terminal device is not opened. */
2246
6ed8eeff 2247struct terminal *
7e59217d 2248get_named_tty (name)
28d440ab
KL
2249 char *name;
2250{
6ed8eeff
KL
2251 struct terminal *t;
2252
ab797f65
KL
2253 if (!name)
2254 abort ();
2255
2256 for (t = terminal_list; t; t = t->next_terminal)
2257 {
2258 if (t->type == output_termcap
2259 && !strcmp (t->display_info.tty->name, name)
2260 && TERMINAL_ACTIVE_P (t))
2261 return t;
2262 }
28d440ab
KL
2263
2264 return 0;
2265}
2266
2267\f
6ed8eeff
KL
2268DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2269 doc: /* Return the type of the tty device that TERMINAL uses.
ab797f65 2270Returns nil if TERMINAL is not on a tty device.
a3fbb897 2271
6ed8eeff
KL
2272TERMINAL can be a terminal id, a frame or nil (meaning the selected
2273frame's terminal). */)
2274 (terminal)
2275 Lisp_Object terminal;
b6660415 2276{
6ed8eeff 2277 struct terminal *t = get_terminal (terminal, 1);
819b8f00 2278
6ed8eeff 2279 if (t->type != output_termcap)
ab797f65
KL
2280 return Qnil;
2281
6ed8eeff
KL
2282 if (t->display_info.tty->type)
2283 return build_string (t->display_info.tty->type);
819b8f00
KL
2284 else
2285 return Qnil;
2286}
2287
6ed8eeff
KL
2288DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2289 doc: /* Return non-nil if TERMINAL is on the controlling tty of the Emacs process.
a3fbb897 2290
6ed8eeff
KL
2291TERMINAL can be a terminal id, a frame or nil (meaning the selected
2292frame's terminal). This function always returns nil if TERMINAL
2293is not on a tty device. */)
2294 (terminal)
2295 Lisp_Object terminal;
4a933ef8 2296{
6ed8eeff 2297 struct terminal *t = get_terminal (terminal, 1);
4a933ef8 2298
78048085 2299 if (t->type != output_termcap || strcmp (t->display_info.tty->name, DEV_TTY))
4a933ef8
KL
2300 return Qnil;
2301 else
2302 return Qt;
2303}
2304
a3fbb897 2305DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
6ed8eeff 2306 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
a3fbb897
KL
2307This is used to override the terminfo data, for certain terminals that
2308do not really do underlining, but say that they do. This function has
6ed8eeff 2309no effect if used on a non-tty terminal.
a3fbb897 2310
6ed8eeff
KL
2311TERMINAL can be a terminal id, a frame or nil (meaning the selected
2312frame's terminal). This function always returns nil if TERMINAL
2313is not on a tty device. */)
2314 (terminal)
2315 Lisp_Object terminal;
a3fbb897 2316{
6ed8eeff 2317 struct terminal *t = get_terminal (terminal, 1);
a3fbb897 2318
6ed8eeff
KL
2319 if (t->type == output_termcap)
2320 t->display_info.tty->TS_enter_underline_mode = 0;
a3fbb897
KL
2321 return Qnil;
2322}
2323
ed8dad6b
KL
2324\f
2325
2326DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2327 doc: /* Suspend the terminal device TTY.
2328
2329The device is restored to its default state, and Emacs ceases all
2330access to the tty device. Frames that use the device are not deleted,
2331but input is not read from them and if they change, their display is
2332not updated.
2333
2334TTY may be a terminal id, a frame, or nil for the terminal device of
2335the currently selected frame.
2336
e4019195 2337This function runs `suspend-tty-functions' after suspending the
ed8dad6b
KL
2338device. The functions are run with one arg, the id of the suspended
2339terminal device.
2340
2341`suspend-tty' does nothing if it is called on a device that is already
2342suspended.
2343
2344A suspended tty may be resumed by calling `resume-tty' on it. */)
2345 (tty)
2346 Lisp_Object tty;
2347{
717a00ef 2348 struct terminal *t = get_tty_terminal (tty, 1);
ed8dad6b
KL
2349 FILE *f;
2350
6ed8eeff 2351 if (!t)
ed8dad6b
KL
2352 error ("Unknown tty device");
2353
6ed8eeff 2354 f = t->display_info.tty->input;
ed8dad6b
KL
2355
2356 if (f)
2357 {
23d4cba5
DN
2358 /* First run `suspend-tty-functions' and then clean up the tty
2359 state because `suspend-tty-functions' might need to change
2360 the tty state. */
2361 if (!NILP (Vrun_hooks))
2362 {
2363 Lisp_Object args[2];
2364 args[0] = intern ("suspend-tty-functions");
2365 XSETTERMINAL (args[1], t);
2366 Frun_hook_with_args (2, args);
2367 }
2368
6ed8eeff 2369 reset_sys_modes (t->display_info.tty);
ed8dad6b
KL
2370
2371 delete_keyboard_wait_descriptor (fileno (f));
2372
2373 fclose (f);
6ed8eeff
KL
2374 if (f != t->display_info.tty->output)
2375 fclose (t->display_info.tty->output);
ed8dad6b 2376
6ed8eeff
KL
2377 t->display_info.tty->input = 0;
2378 t->display_info.tty->output = 0;
ed8dad6b 2379
6ed8eeff
KL
2380 if (FRAMEP (t->display_info.tty->top_frame))
2381 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
ed8dad6b 2382
ed8dad6b
KL
2383 }
2384
4a665758
KL
2385 /* Clear display hooks to prevent further output. */
2386 clear_tty_hooks (t);
2387
ed8dad6b
KL
2388 return Qnil;
2389}
2390
2391DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2392 doc: /* Resume the previously suspended terminal device TTY.
2393The terminal is opened and reinitialized. Frames that are on the
6ed8eeff 2394suspended terminal are revived.
ed8dad6b 2395
6ed8eeff
KL
2396It is an error to resume a terminal while another terminal is active
2397on the same device.
ed8dad6b 2398
e4019195 2399This function runs `resume-tty-functions' after resuming the terminal.
6ed8eeff 2400The functions are run with one arg, the id of the resumed terminal
ed8dad6b
KL
2401device.
2402
2403`resume-tty' does nothing if it is called on a device that is not
2404suspended.
2405
6ed8eeff
KL
2406TTY may be a terminal id, a frame, or nil for the terminal device of
2407the currently selected frame. */)
ed8dad6b
KL
2408 (tty)
2409 Lisp_Object tty;
2410{
717a00ef 2411 struct terminal *t = get_tty_terminal (tty, 1);
ed8dad6b
KL
2412 int fd;
2413
6ed8eeff 2414 if (!t)
ed8dad6b
KL
2415 error ("Unknown tty device");
2416
6ed8eeff 2417 if (!t->display_info.tty->input)
ed8dad6b 2418 {
6ed8eeff 2419 if (get_named_tty (t->display_info.tty->name))
ed8dad6b
KL
2420 error ("Cannot resume display while another display is active on the same device");
2421
6ed8eeff 2422 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
ed8dad6b 2423
59b3194c
KL
2424 if (fd == -1)
2425 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
ed8dad6b 2426
78048085 2427 if (strcmp (t->display_info.tty->name, DEV_TTY))
59b3194c 2428 dissociate_if_controlling_tty (fd);
78048085 2429
6ed8eeff
KL
2430 t->display_info.tty->output = fdopen (fd, "w+");
2431 t->display_info.tty->input = t->display_info.tty->output;
78048085 2432
ed8dad6b
KL
2433 add_keyboard_wait_descriptor (fd);
2434
6ed8eeff
KL
2435 if (FRAMEP (t->display_info.tty->top_frame))
2436 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
ed8dad6b 2437
6ed8eeff 2438 init_sys_modes (t->display_info.tty);
ed8dad6b 2439
e4019195 2440 /* Run `resume-tty-functions'. */
ed8dad6b
KL
2441 if (!NILP (Vrun_hooks))
2442 {
2443 Lisp_Object args[2];
e4019195 2444 args[0] = intern ("resume-tty-functions");
59a7bc63 2445 XSETTERMINAL (args[1], t);
ed8dad6b
KL
2446 Frun_hook_with_args (2, args);
2447 }
2448 }
2449
4a665758
KL
2450 set_tty_hooks (t);
2451
ed8dad6b
KL
2452 return Qnil;
2453}
a3fbb897 2454
819b8f00 2455\f
e882229c
NR
2456/***********************************************************************
2457 Mouse
2458 ***********************************************************************/
2459
7e5a23bd 2460#ifdef HAVE_GPM
5faa03ba
RS
2461void
2462term_mouse_moveto (int x, int y)
94da14a0 2463{
1ec5dc77 2464 /* TODO: how to set mouse position?
94da14a0
NR
2465 const char *name;
2466 int fd;
2467 name = (const char *) ttyname (0);
2468 fd = open (name, O_WRONLY);
80fb2ce0 2469 SOME_FUNCTION (x, y, fd);
94da14a0
NR
2470 close (fd);
2471 last_mouse_x = x;
80fb2ce0 2472 last_mouse_y = y; */
94da14a0
NR
2473}
2474
e882229c
NR
2475static void
2476term_show_mouse_face (enum draw_glyphs_face draw)
2477{
6178ce5e 2478 struct window *w = XWINDOW (mouse_face_window);
e882229c 2479 int save_x, save_y;
1ec5dc77 2480 int i;
e882229c 2481
7be1c21a
MB
2482 struct frame *f = XFRAME (w->frame);
2483 struct tty_display_info *tty = FRAME_TTY (f);
2484
e882229c
NR
2485 if (/* If window is in the process of being destroyed, don't bother
2486 to do anything. */
2487 w->current_matrix != NULL
2488 /* Recognize when we are called to operate on rows that don't exist
2489 anymore. This can happen when a window is split. */
2490 && mouse_face_end_row < w->current_matrix->nrows)
2491 {
2492 /* write_glyphs writes at cursor position, so we need to
2493 temporarily move cursor coordinates to the beginning of
2494 the highlight region. */
2495
2496 /* Save current cursor co-ordinates */
7be1c21a
MB
2497 save_y = curY (tty);
2498 save_x = curX (tty);
e882229c
NR
2499
2500 /* Note that mouse_face_beg_row etc. are window relative. */
2501 for (i = mouse_face_beg_row; i <= mouse_face_end_row; i++)
2502 {
2503 int start_hpos, end_hpos, nglyphs;
2504 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
2505
2506 /* Don't do anything if row doesn't have valid contents. */
2507 if (!row->enabled_p)
2508 continue;
2509
2510 /* For all but the first row, the highlight starts at column 0. */
2511 if (i == mouse_face_beg_row)
2512 start_hpos = mouse_face_beg_col;
2513 else
2514 start_hpos = 0;
2515
2516 if (i == mouse_face_end_row)
2517 end_hpos = mouse_face_end_col;
2518 else
2519 {
2520 end_hpos = row->used[TEXT_AREA];
2521 if (draw == DRAW_NORMAL_TEXT)
2522 row->fill_line_p = 1; /* Clear to end of line */
2523 }
2524
2525 if (end_hpos <= start_hpos)
2526 continue;
2527 /* Record that some glyphs of this row are displayed in
2528 mouse-face. */
2529 row->mouse_face_p = draw > 0;
2530
2531 nglyphs = end_hpos - start_hpos;
2532
2533 if (end_hpos >= row->used[TEXT_AREA])
2534 nglyphs = row->used[TEXT_AREA] - start_hpos;
2535
2536 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2537 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos
2538 + WINDOW_LEFT_EDGE_X (w);
2539
7be1c21a 2540 cursor_to (f, pos_y, pos_x);
e882229c
NR
2541
2542 if (draw == DRAW_MOUSE_FACE)
2543 {
7be1c21a 2544 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
e882229c
NR
2545 nglyphs, mouse_face_face_id);
2546 }
2547 else /* draw == DRAW_NORMAL_TEXT */
7be1c21a 2548 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
e882229c 2549 }
7be1c21a 2550 cursor_to (f, save_y, save_x);
e882229c
NR
2551 }
2552}
2553
2554static void
2555term_clear_mouse_face ()
2556{
6178ce5e 2557 if (!NILP (mouse_face_window))
e882229c
NR
2558 term_show_mouse_face (DRAW_NORMAL_TEXT);
2559
2560 mouse_face_beg_row = mouse_face_beg_col = -1;
2561 mouse_face_end_row = mouse_face_end_col = -1;
6178ce5e 2562 mouse_face_window = Qnil;
e882229c
NR
2563}
2564
2565/* Find the glyph matrix position of buffer position POS in window W.
2566 *HPOS and *VPOS are set to the positions found. W's current glyphs
2567 must be up to date. If POS is above window start return (0, 0).
2568 If POS is after end of W, return end of last line in W.
2569 - taken from msdos.c */
2570static int
2571fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
2572{
2573 int i, lastcol, line_start_position, maybe_next_line_p = 0;
2574 int yb = window_text_bottom_y (w);
2575 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0), *best_row = row;
2576
2577 while (row->y < yb)
2578 {
2579 if (row->used[TEXT_AREA])
2580 line_start_position = row->glyphs[TEXT_AREA]->charpos;
2581 else
2582 line_start_position = 0;
2583
2584 if (line_start_position > pos)
2585 break;
2586 /* If the position sought is the end of the buffer,
2587 don't include the blank lines at the bottom of the window. */
2588 else if (line_start_position == pos
2589 && pos == BUF_ZV (XBUFFER (w->buffer)))
2590 {
2591 maybe_next_line_p = 1;
2592 break;
2593 }
2594 else if (line_start_position > 0)
2595 best_row = row;
2596
2597 /* Don't overstep the last matrix row, lest we get into the
2598 never-never land... */
2599 if (row->y + 1 >= yb)
2600 break;
2601
2602 ++row;
2603 }
2604
2605 /* Find the right column within BEST_ROW. */
2606 lastcol = 0;
2607 row = best_row;
2608 for (i = 0; i < row->used[TEXT_AREA]; i++)
2609 {
2610 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
2611 int charpos;
2612
2613 charpos = glyph->charpos;
2614 if (charpos == pos)
2615 {
2616 *hpos = i;
2617 *vpos = row->y;
2618 return 1;
2619 }
2620 else if (charpos > pos)
2621 break;
2622 else if (charpos > 0)
2623 lastcol = i;
2624 }
2625
2626 /* If we're looking for the end of the buffer,
2627 and we didn't find it in the line we scanned,
2628 use the start of the following line. */
2629 if (maybe_next_line_p)
2630 {
2631 ++row;
2632 lastcol = 0;
2633 }
2634
2635 *vpos = row->y;
2636 *hpos = lastcol + 1;
2637 return 0;
2638}
2639
2640static void
2641term_mouse_highlight (struct frame *f, int x, int y)
2642{
2643 enum window_part part;
2644 Lisp_Object window;
2645 struct window *w;
2646 struct buffer *b;
2647
2648 if (NILP (Vmouse_highlight)
2649 || !f->glyphs_initialized_p)
2650 return;
2651
e882229c
NR
2652 /* Which window is that in? */
2653 window = window_from_coordinates (f, x, y, &part, &x, &y, 0);
2654
2655 /* Not on a window -> return. */
2656 if (!WINDOWP (window))
2657 return;
2658
6178ce5e 2659 if (!EQ (window, mouse_face_window))
e882229c
NR
2660 term_clear_mouse_face ();
2661
2662 w = XWINDOW (window);
2663
2664 /* Are we in a window whose display is up to date?
2665 And verify the buffer's text has not changed. */
2666 b = XBUFFER (w->buffer);
2667 if (part == ON_TEXT
2668 && EQ (w->window_end_valid, w->buffer)
2669 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
2670 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
2671 {
2672 int pos, i, nrows = w->current_matrix->nrows;
2673 struct glyph_row *row;
2674 struct glyph *glyph;
2675
2676 /* Find the glyph under X/Y. */
2677 glyph = NULL;
2678 if (y >= 0 && y < nrows)
2679 {
2680 row = MATRIX_ROW (w->current_matrix, y);
2681 /* Give up if some row before the one we are looking for is
2682 not enabled. */
2683 for (i = 0; i <= y; i++)
2684 if (!MATRIX_ROW (w->current_matrix, i)->enabled_p)
2685 break;
2686 if (i > y /* all rows upto and including the one at Y are enabled */
2687 && row->displays_text_p
2688 && x < window_box_width (w, TEXT_AREA))
2689 {
2690 glyph = row->glyphs[TEXT_AREA];
2691 if (x >= row->used[TEXT_AREA])
2692 glyph = NULL;
2693 else
2694 {
2695 glyph += x;
2696 if (!BUFFERP (glyph->object))
2697 glyph = NULL;
2698 }
2699 }
2700 }
2701
2702 /* Clear mouse face if X/Y not over text. */
2703 if (glyph == NULL)
2704 {
2705 term_clear_mouse_face ();
2706 return;
2707 }
2708
2709 if (!BUFFERP (glyph->object))
2710 abort ();
2711 pos = glyph->charpos;
2712
2713 /* Check for mouse-face. */
2714 {
2715 extern Lisp_Object Qmouse_face;
2716 Lisp_Object mouse_face, overlay, position, *overlay_vec;
3b8c0c70 2717 int noverlays, obegv, ozv;
e882229c
NR
2718 struct buffer *obuf;
2719
2720 /* If we get an out-of-range value, return now; avoid an error. */
2721 if (pos > BUF_Z (b))
2722 return;
2723
2724 /* Make the window's buffer temporarily current for
2725 overlays_at and compute_char_face. */
2726 obuf = current_buffer;
2727 current_buffer = b;
2728 obegv = BEGV;
2729 ozv = ZV;
2730 BEGV = BEG;
2731 ZV = Z;
2732
2733 /* Is this char mouse-active? */
2734 XSETINT (position, pos);
2735
2736 /* Put all the overlays we want in a vector in overlay_vec. */
2737 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
2738 /* Sort overlays into increasing priority order. */
2739 noverlays = sort_overlays (overlay_vec, noverlays, w);
2740
2741 /* Check mouse-face highlighting. */
6178ce5e 2742 if (!(EQ (window, mouse_face_window)
e882229c
NR
2743 && y >= mouse_face_beg_row
2744 && y <= mouse_face_end_row
2745 && (y > mouse_face_beg_row
2746 || x >= mouse_face_beg_col)
2747 && (y < mouse_face_end_row
2748 || x < mouse_face_end_col
2749 || mouse_face_past_end)))
2750 {
2751 /* Clear the display of the old active region, if any. */
2752 term_clear_mouse_face ();
2753
2754 /* Find the highest priority overlay that has a mouse-face
2755 property. */
2756 overlay = Qnil;
2757 for (i = noverlays - 1; i >= 0; --i)
2758 {
2759 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
2760 if (!NILP (mouse_face))
2761 {
2762 overlay = overlay_vec[i];
2763 break;
2764 }
2765 }
2766
2767 /* If no overlay applies, get a text property. */
2768 if (NILP (overlay))
2769 mouse_face = Fget_text_property (position, Qmouse_face,
2770 w->buffer);
2771
2772 /* Handle the overlay case. */
2773 if (!NILP (overlay))
2774 {
2775 /* Find the range of text around this char that
2776 should be active. */
2777 Lisp_Object before, after;
0f8b27ea 2778 EMACS_INT ignore;
e882229c
NR
2779
2780
2781 before = Foverlay_start (overlay);
2782 after = Foverlay_end (overlay);
2783 /* Record this as the current active region. */
2784 fast_find_position (w, XFASTINT (before),
2785 &mouse_face_beg_col,
2786 &mouse_face_beg_row);
2787
2788 mouse_face_past_end
2789 = !fast_find_position (w, XFASTINT (after),
2790 &mouse_face_end_col,
2791 &mouse_face_end_row);
6178ce5e 2792 mouse_face_window = window;
e882229c
NR
2793
2794 mouse_face_face_id
2795 = face_at_buffer_position (w, pos, 0, 0,
2796 &ignore, pos + 1, 1);
2797
2798 /* Display it as active. */
2799 term_show_mouse_face (DRAW_MOUSE_FACE);
2800 }
2801 /* Handle the text property case. */
2802 else if (!NILP (mouse_face))
2803 {
2804 /* Find the range of text around this char that
2805 should be active. */
2806 Lisp_Object before, after, beginning, end;
0f8b27ea 2807 EMACS_INT ignore;
e882229c
NR
2808
2809 beginning = Fmarker_position (w->start);
2810 XSETINT (end, (BUF_Z (b) - XFASTINT (w->window_end_pos)));
2811 before
2812 = Fprevious_single_property_change (make_number (pos + 1),
2813 Qmouse_face,
2814 w->buffer, beginning);
2815 after
2816 = Fnext_single_property_change (position, Qmouse_face,
2817 w->buffer, end);
2818
2819 /* Record this as the current active region. */
2820 fast_find_position (w, XFASTINT (before),
2821 &mouse_face_beg_col,
2822 &mouse_face_beg_row);
2823 mouse_face_past_end
2824 = !fast_find_position (w, XFASTINT (after),
2825 &mouse_face_end_col,
2826 &mouse_face_end_row);
6178ce5e 2827 mouse_face_window = window;
e882229c
NR
2828
2829 mouse_face_face_id
2830 = face_at_buffer_position (w, pos, 0, 0,
2831 &ignore, pos + 1, 1);
2832
2833 /* Display it as active. */
2834 term_show_mouse_face (DRAW_MOUSE_FACE);
2835 }
2836 }
2837
2838 /* Look for a `help-echo' property. */
2839 {
2840 Lisp_Object help;
2841 extern Lisp_Object Qhelp_echo;
2842
2843 /* Check overlays first. */
2844 help = Qnil;
2845 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
2846 {
2847 overlay = overlay_vec[i];
2848 help = Foverlay_get (overlay, Qhelp_echo);
2849 }
2850
2851 if (!NILP (help))
2852 {
2853 help_echo_string = help;
2854 help_echo_window = window;
2855 help_echo_object = overlay;
2856 help_echo_pos = pos;
2857 }
2858 /* Try text properties. */
2859 else if (NILP (help)
2860 && ((STRINGP (glyph->object)
2861 && glyph->charpos >= 0
2862 && glyph->charpos < SCHARS (glyph->object))
2863 || (BUFFERP (glyph->object)
2864 && glyph->charpos >= BEGV
2865 && glyph->charpos < ZV)))
2866 {
2867 help = Fget_text_property (make_number (glyph->charpos),
2868 Qhelp_echo, glyph->object);
2869 if (!NILP (help))
2870 {
2871 help_echo_string = help;
2872 help_echo_window = window;
2873 help_echo_object = glyph->object;
2874 help_echo_pos = glyph->charpos;
2875 }
2876 }
2877 }
2878
2879 BEGV = obegv;
2880 ZV = ozv;
2881 current_buffer = obuf;
2882 }
2883 }
2884}
2885
2886static int
2887term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2888{
2889 /* Has the mouse moved off the glyph it was on at the last sighting? */
2890 if (event->x != last_mouse_x || event->y != last_mouse_y)
2891 {
2892 frame->mouse_moved = 1;
80fb2ce0 2893 term_mouse_highlight (frame, event->x, event->y);
e882229c
NR
2894 /* Remember which glyph we're now on. */
2895 last_mouse_x = event->x;
2896 last_mouse_y = event->y;
2897 return 1;
2898 }
2899 return 0;
2900}
2901
2902/* Return the current position of the mouse.
2903
2904 Set *f to the frame the mouse is in, or zero if the mouse is in no
2905 Emacs frame. If it is set to zero, all the other arguments are
2906 garbage.
2907
2908 Set *bar_window to Qnil, and *x and *y to the column and
2909 row of the character cell the mouse is over.
2910
2911 Set *time to the time the mouse was at the returned position.
2912
80fb2ce0 2913 This clears mouse_moved until the next motion
94da14a0 2914 event arrives. */
e882229c
NR
2915static void
2916term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2917 enum scroll_bar_part *part, Lisp_Object *x,
2918 Lisp_Object *y, unsigned long *time)
2919{
e882229c 2920 struct timeval now;
e882229c
NR
2921
2922 *fp = SELECTED_FRAME ();
94da14a0 2923 (*fp)->mouse_moved = 0;
e882229c
NR
2924
2925 *bar_window = Qnil;
2926 *part = 0;
2927
80fb2ce0 2928 XSETINT (*x, last_mouse_x);
94da14a0 2929 XSETINT (*y, last_mouse_y);
e882229c
NR
2930 gettimeofday(&now, 0);
2931 *time = (now.tv_sec * 1000) + (now.tv_usec / 1000);
e882229c
NR
2932}
2933
2934/* Prepare a mouse-event in *RESULT for placement in the input queue.
2935
2936 If the event is a button press, then note that we have grabbed
2937 the mouse. */
2938
2939static Lisp_Object
2940term_mouse_click (struct input_event *result, Gpm_Event *event,
2941 struct frame *f)
2942{
2943 struct timeval now;
2944 int i, j;
2945
2946 result->kind = GPM_CLICK_EVENT;
2947 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2948 {
2949 if (event->buttons & j) {
2950 result->code = i; /* button number */
2951 break;
2952 }
2953 }
2954 gettimeofday(&now, 0);
2955 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2956
2957 if (event->type & GPM_UP)
2958 result->modifiers = up_modifier;
2959 else if (event->type & GPM_DOWN)
2960 result->modifiers = down_modifier;
2961 else
2962 result->modifiers = 0;
2963
2964 if (event->type & GPM_SINGLE)
2965 result->modifiers |= click_modifier;
2966
2967 if (event->type & GPM_DOUBLE)
2968 result->modifiers |= double_modifier;
2969
2970 if (event->type & GPM_TRIPLE)
2971 result->modifiers |= triple_modifier;
2972
2973 if (event->type & GPM_DRAG)
2974 result->modifiers |= drag_modifier;
2975
80fb2ce0 2976 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
e882229c
NR
2977
2978 /* 1 << KG_SHIFT */
2979 if (event->modifiers & (1 << 0))
2980 result->modifiers |= shift_modifier;
2981
2982 /* 1 << KG_CTRL */
2983 if (event->modifiers & (1 << 2))
2984 result->modifiers |= ctrl_modifier;
2985
2986 /* 1 << KG_ALT || KG_ALTGR */
2987 if (event->modifiers & (1 << 3)
2988 || event->modifiers & (1 << 1))
2989 result->modifiers |= meta_modifier;
2990 }
2991
80fb2ce0
NR
2992 XSETINT (result->x, event->x);
2993 XSETINT (result->y, event->y);
e882229c
NR
2994 XSETFRAME (result->frame_or_window, f);
2995 result->arg = Qnil;
2996 return Qnil;
2997}
2998
2999int
7be1c21a 3000handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
e882229c 3001{
7be1c21a 3002 struct frame *f = XFRAME (tty->top_frame);
e882229c
NR
3003 struct input_event ie;
3004 int do_help = 0;
3005 int count = 0;
3006
3007 EVENT_INIT (ie);
3008 ie.kind = NO_EVENT;
3009 ie.arg = Qnil;
3010
80fb2ce0 3011 if (event->type & (GPM_MOVE | GPM_DRAG)) {
e882229c
NR
3012 previous_help_echo_string = help_echo_string;
3013 help_echo_string = Qnil;
3014
6178ce5e 3015 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
e882229c 3016
80fb2ce0
NR
3017 if (!term_mouse_movement (f, event))
3018 help_echo_string = previous_help_echo_string;
e882229c
NR
3019
3020 /* If the contents of the global variable help_echo_string
3021 has changed, generate a HELP_EVENT. */
3022 if (!NILP (help_echo_string)
3023 || !NILP (previous_help_echo_string))
3024 do_help = 1;
3025
3026 goto done;
3027 }
3028 else {
3029 f->mouse_moved = 0;
3030 term_mouse_click (&ie, event, f);
e882229c
NR
3031 }
3032
3033 done:
3034 if (ie.kind != NO_EVENT)
3035 {
3036 kbd_buffer_store_event_hold (&ie, hold_quit);
3037 count++;
3038 }
3039
3040 if (do_help
3041 && !(hold_quit && hold_quit->kind != NO_EVENT))
3042 {
3043 Lisp_Object frame;
3044
3045 if (f)
3046 XSETFRAME (frame, f);
3047 else
3048 frame = Qnil;
3049
3050 gen_help_event (help_echo_string, frame, help_echo_window,
3051 help_echo_object, help_echo_pos);
3052 count++;
3053 }
3054
3055 return count;
3056}
3057
6178ce5e 3058DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
e882229c 3059 0, 0, 0,
71f44e7a 3060 doc: /* Open a connection to Gpm.
6178ce5e 3061Gpm-mouse can only be activated for one tty at a time. */)
e882229c
NR
3062 ()
3063{
71f44e7a
SM
3064 struct frame *f = SELECTED_FRAME ();
3065 struct tty_display_info *tty
3066 = ((f)->output_method == output_termcap
3067 ? (f)->terminal->display_info.tty : NULL);
e882229c
NR
3068 Gpm_Connect connection;
3069
6178ce5e
SM
3070 if (!tty)
3071 error ("Gpm-mouse only works in the GNU/Linux console");
4ce5ab77
SM
3072 if (gpm_tty == tty)
3073 return Qnil; /* Already activated, nothing to do. */
3074 if (gpm_tty)
3075 error ("Gpm-mouse can only be activated for one tty at a time");
71f44e7a 3076
e882229c
NR
3077 connection.eventMask = ~0;
3078 connection.defaultMask = ~GPM_HARD;
3079 connection.maxMod = ~0;
3080 connection.minMod = 0;
80fb2ce0 3081 gpm_zerobased = 1;
e882229c
NR
3082
3083 if (Gpm_Open (&connection, 0) < 0)
6178ce5e 3084 error ("Gpm-mouse failed to connect to the gpm daemon");
e882229c
NR
3085 else
3086 {
71f44e7a 3087 gpm_tty = tty;
1023cbed
SM
3088 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
3089 to generate SIGIOs. Apparently we need to call reset_sys_modes
3090 before calling init_sys_modes. */
7be1c21a
MB
3091 reset_sys_modes (tty);
3092 init_sys_modes (tty);
e882229c 3093 add_gpm_wait_descriptor (gpm_fd);
6178ce5e 3094 return Qnil;
e882229c
NR
3095 }
3096}
3097
6178ce5e 3098DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
e882229c
NR
3099 0, 0, 0,
3100 doc: /* Close a connection to Gpm. */)
3101 ()
3102{
4ce5ab77
SM
3103 struct frame *f = SELECTED_FRAME ();
3104 struct tty_display_info *tty
3105 = ((f)->output_method == output_termcap
3106 ? (f)->terminal->display_info.tty : NULL);
3107
3108 if (!tty || gpm_tty != tty)
3109 return Qnil; /* Not activated on this terminal, nothing to do. */
3110
6178ce5e
SM
3111 if (gpm_fd >= 0)
3112 delete_gpm_wait_descriptor (gpm_fd);
3113 while (Gpm_Close()); /* close all the stack */
3114 gpm_tty = NULL;
3115 return Qnil;
e882229c 3116}
7e5a23bd 3117#endif /* HAVE_GPM */
e882229c
NR
3118
3119\f
a168702a
GM
3120/***********************************************************************
3121 Initialization
3122 ***********************************************************************/
3123
ed8dad6b
KL
3124/* Initialize the tty-dependent part of frame F. The frame must
3125 already have its device initialized. */
3224dac1 3126
dfcf069d 3127void
ed8dad6b 3128create_tty_output (struct frame *f)
28d440ab 3129{
ed8dad6b
KL
3130 struct tty_output *t;
3131
3132 if (! FRAME_TERMCAP_P (f))
428a555e
KL
3133 abort ();
3134
ed8dad6b
KL
3135 t = xmalloc (sizeof (struct tty_output));
3136 bzero (t, sizeof (struct tty_output));
b6660415 3137
6ed8eeff 3138 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
bedb9c0e 3139
ed8dad6b 3140 f->output_data.tty = t;
3224dac1
KL
3141}
3142
ed8dad6b 3143/* Delete the tty-dependent part of frame F. */
da8e1115 3144
ed8dad6b
KL
3145static void
3146delete_tty_output (struct frame *f)
3224dac1 3147{
ed8dad6b 3148 if (! FRAME_TERMCAP_P (f))
3224dac1
KL
3149 abort ();
3150
ed8dad6b 3151 xfree (f->output_data.tty);
28d440ab
KL
3152}
3153
ed8dad6b 3154\f
47cc8819 3155/* Reset the hooks in TERMINAL. */
ed8dad6b 3156
4a665758
KL
3157static void
3158clear_tty_hooks (struct terminal *terminal)
3159{
3160 terminal->rif = 0;
3161 terminal->cursor_to_hook = 0;
3162 terminal->raw_cursor_to_hook = 0;
3163 terminal->clear_to_end_hook = 0;
3164 terminal->clear_frame_hook = 0;
3165 terminal->clear_end_of_line_hook = 0;
3166 terminal->ins_del_lines_hook = 0;
3167 terminal->insert_glyphs_hook = 0;
3168 terminal->write_glyphs_hook = 0;
3169 terminal->delete_glyphs_hook = 0;
3170 terminal->ring_bell_hook = 0;
3171 terminal->reset_terminal_modes_hook = 0;
3172 terminal->set_terminal_modes_hook = 0;
3173 terminal->update_begin_hook = 0;
3174 terminal->update_end_hook = 0;
3175 terminal->set_terminal_window_hook = 0;
3176 terminal->mouse_position_hook = 0;
3177 terminal->frame_rehighlight_hook = 0;
3178 terminal->frame_raise_lower_hook = 0;
974b73e8 3179 terminal->fullscreen_hook = 0;
4a665758
KL
3180 terminal->set_vertical_scroll_bar_hook = 0;
3181 terminal->condemn_scroll_bars_hook = 0;
3182 terminal->redeem_scroll_bar_hook = 0;
3183 terminal->judge_scroll_bars_hook = 0;
3184 terminal->read_socket_hook = 0;
3185 terminal->frame_up_to_date_hook = 0;
3186
3187 /* Leave these two set, or suspended frames are not deleted
3188 correctly. */
3189 terminal->delete_frame_hook = &delete_tty_output;
3190 terminal->delete_terminal_hook = &delete_tty;
3191}
3192
47cc8819
DN
3193/* Initialize hooks in TERMINAL with the values needed for a tty. */
3194
4a665758
KL
3195static void
3196set_tty_hooks (struct terminal *terminal)
3197{
3198 terminal->rif = 0; /* ttys don't support window-based redisplay. */
3199
3200 terminal->cursor_to_hook = &tty_cursor_to;
3201 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
3202
3203 terminal->clear_to_end_hook = &tty_clear_to_end;
3204 terminal->clear_frame_hook = &tty_clear_frame;
3205 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
3206
3207 terminal->ins_del_lines_hook = &tty_ins_del_lines;
3208
3209 terminal->insert_glyphs_hook = &tty_insert_glyphs;
3210 terminal->write_glyphs_hook = &tty_write_glyphs;
3211 terminal->delete_glyphs_hook = &tty_delete_glyphs;
3212
3213 terminal->ring_bell_hook = &tty_ring_bell;
3214
3215 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
3216 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
3217 terminal->update_begin_hook = 0; /* Not needed. */
3218 terminal->update_end_hook = &tty_update_end;
3219 terminal->set_terminal_window_hook = &tty_set_terminal_window;
3220
3221 terminal->mouse_position_hook = 0; /* Not needed. */
3222 terminal->frame_rehighlight_hook = 0; /* Not needed. */
3223 terminal->frame_raise_lower_hook = 0; /* Not needed. */
3224
3225 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
3226 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
3227 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
3228 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
3229
3230 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
3231 terminal->frame_up_to_date_hook = 0; /* Not needed. */
3232
3233 terminal->delete_frame_hook = &delete_tty_output;
3234 terminal->delete_terminal_hook = &delete_tty;
3235}
3236
03a1d6bd 3237/* Drop the controlling terminal if fd is the same device. */
ed8dad6b 3238static void
03a1d6bd
KL
3239dissociate_if_controlling_tty (int fd)
3240{
5d333979 3241#ifndef WINDOWSNT
03a1d6bd 3242 int pgid;
8516815b 3243 EMACS_GET_TTY_PGRP (fd, &pgid); /* If tcgetpgrp succeeds, fd is the ctty. */
03a1d6bd
KL
3244 if (pgid != -1)
3245 {
8516815b 3246#if defined (USG) && !defined (BSD_PGRPS)
03a1d6bd
KL
3247 setpgrp ();
3248 no_controlling_tty = 1;
de52dcbb
DN
3249#elif defined (CYGWIN)
3250 setsid ();
3251 no_controlling_tty = 1;
03a1d6bd
KL
3252#else
3253#ifdef TIOCNOTTY /* Try BSD ioctls. */
8516815b 3254 sigblock (sigmask (SIGTTOU));
78048085 3255 fd = emacs_open (DEV_TTY, O_RDWR, 0);
8516815b
KL
3256 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
3257 {
3258 no_controlling_tty = 1;
3259 }
3260 if (fd != -1)
3261 emacs_close (fd);
3262 sigunblock (sigmask (SIGTTOU));
03a1d6bd 3263#else
8516815b
KL
3264 /* Unknown system. */
3265 croak ();
03a1d6bd
KL
3266#endif /* ! TIOCNOTTY */
3267#endif /* ! USG */
71a96040 3268 }
78048085 3269#endif /* !WINDOWSNT */
03a1d6bd
KL
3270}
3271
520c4dfd
KL
3272static void maybe_fatal();
3273
3224dac1
KL
3274/* Create a termcap display on the tty device with the given name and
3275 type.
3276
ab797f65 3277 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3224dac1
KL
3278 Otherwise NAME should be a path to the tty device file,
3279 e.g. "/dev/pts/7".
28d440ab 3280
3224dac1
KL
3281 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3282
3283 If MUST_SUCCEED is true, then all errors are fatal. */
da8e1115 3284
6ed8eeff 3285struct terminal *
a4c6993d 3286init_tty (char *name, char *terminal_type, int must_succeed)
08a24c47 3287{
59b3194c 3288 char *area = NULL;
08a24c47 3289 char **address = &area;
6b61353c
KH
3290 char *buffer = NULL;
3291 int buffer_size = 4096;
59b3194c 3292 register char *p = NULL;
08a24c47 3293 int status;
59b3194c
KL
3294 struct tty_display_info *tty = NULL;
3295 struct terminal *terminal = NULL;
3296 int ctty = 0; /* 1 if asked to open controlling tty. */
28d440ab 3297
3224dac1
KL
3298 if (!terminal_type)
3299 maybe_fatal (must_succeed, 0, 0,
3300 "Unknown terminal type",
3301 "Unknown terminal type");
b6660415 3302
ab797f65 3303 if (name == NULL)
78048085
EZ
3304 name = DEV_TTY;
3305 if (!strcmp (name, DEV_TTY))
ab797f65
KL
3306 ctty = 1;
3307
6ed8eeff
KL
3308 /* If we already have a terminal on the given device, use that. If
3309 all such terminals are suspended, create a new one instead. */
a4c6993d 3310 /* XXX Perhaps this should be made explicit by having init_tty
6ed8eeff 3311 always create a new terminal and separating terminal and frame
b6660415 3312 creation on Lisp level. */
6ed8eeff
KL
3313 terminal = get_named_tty (name);
3314 if (terminal)
3315 return terminal;
78048085 3316
6ed8eeff 3317 terminal = create_terminal ();
3224dac1
KL
3318 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
3319 bzero (tty, sizeof (struct tty_display_info));
3320 tty->next = tty_list;
3321 tty_list = tty;
428a555e 3322
6ed8eeff
KL
3323 terminal->type = output_termcap;
3324 terminal->display_info.tty = tty;
3325 tty->terminal = terminal;
28d440ab 3326
7b00d185
KL
3327 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3328 Wcm_clear (tty);
daf01701 3329
55d5acfa 3330#ifndef WINDOWSNT
4a665758 3331 set_tty_hooks (terminal);
78048085 3332
59b3194c
KL
3333 {
3334 int fd;
3335 FILE *file;
0c72d684
KL
3336
3337#ifdef O_IGNORE_CTTY
59b3194c 3338 if (!ctty)
0c72d684
KL
3339 /* Open the terminal device. Don't recognize it as our
3340 controlling terminal, and don't make it the controlling tty
3341 if we don't have one at the moment. */
3342 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
59b3194c 3343 else
0c72d684 3344#else
2666355c 3345 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
59b3194c
KL
3346 defined on Hurd. On other systems, we need to explicitly
3347 dissociate ourselves from the controlling tty when we want to
3348 open a frame on the same terminal. */
0c72d684 3349 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
0c72d684
KL
3350#endif /* O_IGNORE_CTTY */
3351
59b3194c
KL
3352 if (fd < 0)
3353 maybe_fatal (must_succeed, buffer, terminal,
3354 "Could not open file: %s",
3355 "Could not open file: %s",
3356 name);
3357 if (!isatty (fd))
3358 {
3359 close (fd);
3360 maybe_fatal (must_succeed, buffer, terminal,
3361 "Not a tty device: %s",
3362 "Not a tty device: %s",
3363 name);
3364 }
0c72d684 3365
59b3194c
KL
3366#ifndef O_IGNORE_CTTY
3367 if (!ctty)
03a1d6bd 3368 dissociate_if_controlling_tty (fd);
59b3194c
KL
3369#endif
3370
3371 file = fdopen (fd, "w+");
3372 tty->name = xstrdup (name);
3373 terminal->name = xstrdup (name);
3374 tty->input = file;
3375 tty->output = file;
3376 }
78048085 3377
28d7d09f 3378 tty->type = xstrdup (terminal_type);
28d440ab 3379
819b8f00 3380 add_keyboard_wait_descriptor (fileno (tty->input));
daf01701 3381
55d5acfa 3382#endif
08a24c47 3383
288d5132
KH
3384 encode_terminal_src_size = 0;
3385 encode_terminal_dst_size = 0;
af645abf 3386
7e5a23bd 3387#ifdef HAVE_GPM
88acaaf2 3388 terminal->mouse_position_hook = term_mouse_position;
6178ce5e 3389 mouse_face_window = Qnil;
e882229c
NR
3390#endif
3391
cb28b9c2 3392#ifdef WINDOWSNT
953bf0dc 3393 initialize_w32_display (terminal);
a0236551
JR
3394 /* The following two are inaccessible from w32console.c. */
3395 terminal->delete_frame_hook = &delete_tty_output;
3396 terminal->delete_terminal_hook = &delete_tty;
cb28b9c2 3397
78048085
EZ
3398 tty->name = xstrdup (name);
3399 terminal->name = xstrdup (name);
3400 tty->type = xstrdup (terminal_type);
cb28b9c2 3401
55d5acfa 3402 tty->output = stdout;
a0236551
JR
3403 tty->input = stdin;
3404 add_keyboard_wait_descriptor (0);
cb28b9c2 3405
a0236551 3406 Wcm_clear (tty);
cb28b9c2 3407
5d333979
JR
3408 {
3409 struct frame *f = XFRAME (selected_frame);
cb28b9c2 3410
a0236551
JR
3411 FrameRows (tty) = FRAME_LINES (f);
3412 FrameCols (tty) = FRAME_COLS (f);
3413 tty->specified_window = FRAME_LINES (f);
cb28b9c2 3414
a0236551
JR
3415 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3416 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
5d333979
JR
3417 }
3418 tty->delete_in_insert_mode = 1;
cb28b9c2 3419
daf01701 3420 UseTabs (tty) = 0;
6ed8eeff 3421 terminal->scroll_region_ok = 0;
cb28b9c2 3422
7e59217d 3423 /* Seems to insert lines when it's not supposed to, messing up the
6ed8eeff 3424 display. In doing a trace, it didn't seem to be called much, so I
7e59217d 3425 don't think we're losing anything by turning it off. */
6ed8eeff
KL
3426 terminal->line_ins_del_ok = 0;
3427 terminal->char_ins_del_ok = 1;
cb28b9c2
RS
3428
3429 baud_rate = 19200;
3430
5d333979 3431 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
cb28b9c2 3432
eccec691 3433#else /* not WINDOWSNT */
cb28b9c2 3434
6548cf00 3435 Wcm_clear (tty);
08a24c47 3436
6b61353c 3437 buffer = (char *) xmalloc (buffer_size);
78048085 3438
03a1d6bd
KL
3439 /* On some systems, tgetent tries to access the controlling
3440 terminal. */
3441 sigblock (sigmask (SIGTTOU));
08a24c47 3442 status = tgetent (buffer, terminal_type);
03a1d6bd 3443 sigunblock (sigmask (SIGTTOU));
78048085 3444
08a24c47 3445 if (status < 0)
b0347178
KH
3446 {
3447#ifdef TERMINFO
6ed8eeff 3448 maybe_fatal (must_succeed, buffer, terminal,
3224dac1
KL
3449 "Cannot open terminfo database file",
3450 "Cannot open terminfo database file");
b0347178 3451#else
6ed8eeff 3452 maybe_fatal (must_succeed, buffer, terminal,
3224dac1
KL
3453 "Cannot open termcap database file",
3454 "Cannot open termcap database file");
b0347178
KH
3455#endif
3456 }
08a24c47 3457 if (status == 0)
b0347178
KH
3458 {
3459#ifdef TERMINFO
6ed8eeff 3460 maybe_fatal (must_succeed, buffer, terminal,
3224dac1
KL
3461 "Terminal type %s is not defined",
3462 "Terminal type %s is not defined.\n\
b0347178
KH
3463If that is not the actual type of terminal you have,\n\
3464use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3465`setenv TERM ...') to specify the correct type. It may be necessary\n\
e12c1054 3466to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3224dac1 3467 terminal_type);
b0347178 3468#else
6ed8eeff 3469 maybe_fatal (must_succeed, buffer, terminal,
3224dac1
KL
3470 "Terminal type %s is not defined",
3471 "Terminal type %s is not defined.\n\
c5a9c3e6
RS
3472If that is not the actual type of terminal you have,\n\
3473use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3474`setenv TERM ...') to specify the correct type. It may be necessary\n\
e12c1054 3475to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3224dac1 3476 terminal_type);
b0347178
KH
3477#endif
3478 }
6b61353c
KH
3479
3480#ifndef TERMINFO
3481 if (strlen (buffer) >= buffer_size)
08a24c47 3482 abort ();
6b61353c
KH
3483 buffer_size = strlen (buffer);
3484#endif
3485 area = (char *) xmalloc (buffer_size);
08a24c47 3486
fca177d4
KL
3487 tty->TS_ins_line = tgetstr ("al", address);
3488 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3489 tty->TS_bell = tgetstr ("bl", address);
6548cf00 3490 BackTab (tty) = tgetstr ("bt", address);
fca177d4
KL
3491 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3492 tty->TS_clr_line = tgetstr ("ce", address);
3493 tty->TS_clr_frame = tgetstr ("cl", address);
6548cf00
KL
3494 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3495 AbsPosition (tty) = tgetstr ("cm", address);
3496 CR (tty) = tgetstr ("cr", address);
fca177d4
KL
3497 tty->TS_set_scroll_region = tgetstr ("cs", address);
3498 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
6548cf00 3499 RowPosition (tty) = tgetstr ("cv", address);
fca177d4
KL
3500 tty->TS_del_char = tgetstr ("dc", address);
3501 tty->TS_del_multi_chars = tgetstr ("DC", address);
3502 tty->TS_del_line = tgetstr ("dl", address);
3503 tty->TS_del_multi_lines = tgetstr ("DL", address);
3504 tty->TS_delete_mode = tgetstr ("dm", address);
3505 tty->TS_end_delete_mode = tgetstr ("ed", address);
3506 tty->TS_end_insert_mode = tgetstr ("ei", address);
6548cf00 3507 Home (tty) = tgetstr ("ho", address);
fca177d4
KL
3508 tty->TS_ins_char = tgetstr ("ic", address);
3509 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3510 tty->TS_insert_mode = tgetstr ("im", address);
3511 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3512 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3513 tty->TS_keypad_mode = tgetstr ("ks", address);
6548cf00
KL
3514 LastLine (tty) = tgetstr ("ll", address);
3515 Right (tty) = tgetstr ("nd", address);
3516 Down (tty) = tgetstr ("do", address);
3517 if (!Down (tty))
3518 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
08a24c47
JB
3519#ifdef VMS
3520 /* VMS puts a carriage return before each linefeed,
3521 so it is not safe to use linefeeds. */
6548cf00
KL
3522 if (Down (tty) && Down (tty)[0] == '\n' && Down (tty)[1] == '\0')
3523 Down (tty) = 0;
08a24c47
JB
3524#endif /* VMS */
3525 if (tgetflag ("bs"))
6548cf00 3526 Left (tty) = "\b"; /* can't possibly be longer! */
08a24c47 3527 else /* (Actually, "bs" is obsolete...) */
6548cf00
KL
3528 Left (tty) = tgetstr ("le", address);
3529 if (!Left (tty))
3530 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
fca177d4
KL
3531 tty->TS_pad_char = tgetstr ("pc", address);
3532 tty->TS_repeat = tgetstr ("rp", address);
3533 tty->TS_end_standout_mode = tgetstr ("se", address);
3534 tty->TS_fwd_scroll = tgetstr ("sf", address);
3535 tty->TS_standout_mode = tgetstr ("so", address);
3536 tty->TS_rev_scroll = tgetstr ("sr", address);
6548cf00 3537 tty->Wcm->cm_tab = tgetstr ("ta", address);
fca177d4
KL
3538 tty->TS_end_termcap_modes = tgetstr ("te", address);
3539 tty->TS_termcap_modes = tgetstr ("ti", address);
6548cf00 3540 Up (tty) = tgetstr ("up", address);
fca177d4
KL
3541 tty->TS_visible_bell = tgetstr ("vb", address);
3542 tty->TS_cursor_normal = tgetstr ("ve", address);
3543 tty->TS_cursor_visible = tgetstr ("vs", address);
3544 tty->TS_cursor_invisible = tgetstr ("vi", address);
3545 tty->TS_set_window = tgetstr ("wi", address);
3546
3547 tty->TS_enter_underline_mode = tgetstr ("us", address);
3548 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3549 tty->TS_enter_bold_mode = tgetstr ("md", address);
3550 tty->TS_enter_dim_mode = tgetstr ("mh", address);
3551 tty->TS_enter_blink_mode = tgetstr ("mb", address);
3552 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3553 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3554 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3555 tty->TS_exit_attribute_mode = tgetstr ("me", address);
177c0ea7 3556
6548cf00
KL
3557 MultiUp (tty) = tgetstr ("UP", address);
3558 MultiDown (tty) = tgetstr ("DO", address);
3559 MultiLeft (tty) = tgetstr ("LE", address);
3560 MultiRight (tty) = tgetstr ("RI", address);
08a24c47 3561
e7f90eab
GM
3562 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
3563 color because we can't switch back to the default foreground and
3564 background. */
fca177d4
KL
3565 tty->TS_orig_pair = tgetstr ("op", address);
3566 if (tty->TS_orig_pair)
a168702a 3567 {
fca177d4
KL
3568 tty->TS_set_foreground = tgetstr ("AF", address);
3569 tty->TS_set_background = tgetstr ("AB", address);
3570 if (!tty->TS_set_foreground)
e7f90eab
GM
3571 {
3572 /* SVr4. */
fca177d4
KL
3573 tty->TS_set_foreground = tgetstr ("Sf", address);
3574 tty->TS_set_background = tgetstr ("Sb", address);
e7f90eab 3575 }
177c0ea7 3576
fca177d4
KL
3577 tty->TN_max_colors = tgetnum ("Co");
3578 tty->TN_max_pairs = tgetnum ("pa");
177c0ea7 3579
fca177d4
KL
3580 tty->TN_no_color_video = tgetnum ("NC");
3581 if (tty->TN_no_color_video == -1)
3582 tty->TN_no_color_video = 0;
a168702a 3583 }
a168702a 3584
fca177d4 3585 tty_default_color_capabilities (tty, 1);
ace28297 3586
6548cf00 3587 MagicWrap (tty) = tgetflag ("xn");
e4058338
KH
3588 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3589 the former flag imply the latter. */
6548cf00 3590 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
6ed8eeff 3591 terminal->memory_below_frame = tgetflag ("db");
fca177d4 3592 tty->TF_hazeltine = tgetflag ("hz");
6ed8eeff 3593 terminal->must_write_spaces = tgetflag ("in");
fca177d4
KL
3594 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3595 tty->TF_insmode_motion = tgetflag ("mi");
3596 tty->TF_standout_motion = tgetflag ("ms");
3597 tty->TF_underscore = tgetflag ("ul");
3598 tty->TF_teleray = tgetflag ("xt");
08a24c47 3599
953bf0dc 3600#endif /* !WINDOWSNT */
e7cf0fa0 3601#ifdef MULTI_KBOARD
6ed8eeff
KL
3602 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3603 init_kboard (terminal->kboard);
70b8d0a4 3604 terminal->kboard->Vwindow_system = Qnil;
6ed8eeff
KL
3605 terminal->kboard->next_kboard = all_kboards;
3606 all_kboards = terminal->kboard;
3607 terminal->kboard->reference_count++;
e7cf0fa0
KL
3608 /* Don't let the initial kboard remain current longer than necessary.
3609 That would cause problems if a file loaded on startup tries to
3610 prompt in the mini-buffer. */
3611 if (current_kboard == initial_kboard)
6ed8eeff 3612 current_kboard = terminal->kboard;
953bf0dc 3613#ifndef WINDOWSNT
6ed8eeff 3614 term_get_fkeys (address, terminal->kboard);
80ca7302 3615#endif
953bf0dc 3616#endif
5c2c7893 3617
953bf0dc 3618#ifndef WINDOWSNT
ff11dfa1 3619 /* Get frame size from system, or else from termcap. */
3b12ce12
RS
3620 {
3621 int height, width;
0b0d3e0b 3622 get_tty_size (fileno (tty->input), &width, &height);
0a125897
KL
3623 FrameCols (tty) = width;
3624 FrameRows (tty) = height;
3b12ce12
RS
3625 }
3626
0a125897
KL
3627 if (FrameCols (tty) <= 0)
3628 FrameCols (tty) = tgetnum ("co");
3629 if (FrameRows (tty) <= 0)
3630 FrameRows (tty) = tgetnum ("li");
177c0ea7 3631
0a125897 3632 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
6ed8eeff 3633 maybe_fatal (must_succeed, NULL, terminal,
3224dac1
KL
3634 "Screen size %dx%d is too small"
3635 "Screen size %dx%d is too small",
0a125897 3636 FrameCols (tty), FrameRows (tty));
ee7a2de4 3637
8a56675d 3638#if 0 /* This is not used anywhere. */
6ed8eeff 3639 tty->terminal->min_padding_speed = tgetnum ("pb");
8a56675d 3640#endif
28d440ab 3641
6548cf00 3642 TabWidth (tty) = tgetnum ("tw");
08a24c47
JB
3643
3644#ifdef VMS
3645 /* These capabilities commonly use ^J.
3646 I don't know why, but sending them on VMS does not work;
3647 it causes following spaces to be lost, sometimes.
3648 For now, the simplest fix is to avoid using these capabilities ever. */
6548cf00
KL
3649 if (Down (tty) && Down (tty)[0] == '\n')
3650 Down (tty) = 0;
08a24c47
JB
3651#endif /* VMS */
3652
fca177d4
KL
3653 if (!tty->TS_bell)
3654 tty->TS_bell = "\07";
08a24c47 3655
fca177d4
KL
3656 if (!tty->TS_fwd_scroll)
3657 tty->TS_fwd_scroll = Down (tty);
08a24c47 3658
fca177d4 3659 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
08a24c47 3660
6548cf00
KL
3661 if (TabWidth (tty) < 0)
3662 TabWidth (tty) = 8;
177c0ea7 3663
08a24c47
JB
3664/* Turned off since /etc/termcap seems to have :ta= for most terminals
3665 and newer termcap doc does not seem to say there is a default.
6548cf00
KL
3666 if (!tty->Wcm->cm_tab)
3667 tty->Wcm->cm_tab = "\t";
08a24c47
JB
3668*/
3669
54800acb
MB
3670 /* We don't support standout modes that use `magic cookies', so
3671 turn off any that do. */
fca177d4 3672 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
54800acb 3673 {
fca177d4
KL
3674 tty->TS_standout_mode = 0;
3675 tty->TS_end_standout_mode = 0;
54800acb 3676 }
fca177d4 3677 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
54800acb 3678 {
fca177d4
KL
3679 tty->TS_enter_underline_mode = 0;
3680 tty->TS_exit_underline_mode = 0;
54800acb
MB
3681 }
3682
3683 /* If there's no standout mode, try to use underlining instead. */
fca177d4 3684 if (tty->TS_standout_mode == 0)
08a24c47 3685 {
fca177d4
KL
3686 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3687 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
08a24c47
JB
3688 }
3689
afd359c4
RS
3690 /* If no `se' string, try using a `me' string instead.
3691 If that fails, we can't use standout mode at all. */
fca177d4 3692 if (tty->TS_end_standout_mode == 0)
afd359c4 3693 {
e4bfb3b6 3694 char *s = tgetstr ("me", address);
afd359c4 3695 if (s != 0)
fca177d4 3696 tty->TS_end_standout_mode = s;
afd359c4 3697 else
fca177d4 3698 tty->TS_standout_mode = 0;
afd359c4
RS
3699 }
3700
fca177d4 3701 if (tty->TF_teleray)
08a24c47 3702 {
6548cf00 3703 tty->Wcm->cm_tab = 0;
54800acb 3704 /* We can't support standout mode, because it uses magic cookies. */
fca177d4 3705 tty->TS_standout_mode = 0;
08a24c47 3706 /* But that means we cannot rely on ^M to go to column zero! */
6548cf00 3707 CR (tty) = 0;
08a24c47
JB
3708 /* LF can't be trusted either -- can alter hpos */
3709 /* if move at column 0 thru a line with TS_standout_mode */
6548cf00 3710 Down (tty) = 0;
08a24c47
JB
3711 }
3712
3713 /* Special handling for certain terminal types known to need it */
3714
3715 if (!strcmp (terminal_type, "supdup"))
3716 {
6ed8eeff 3717 terminal->memory_below_frame = 1;
6548cf00 3718 tty->Wcm->cm_losewrap = 1;
08a24c47
JB
3719 }
3720 if (!strncmp (terminal_type, "c10", 3)
3721 || !strcmp (terminal_type, "perq"))
3722 {
3723 /* Supply a makeshift :wi string.
3724 This string is not valid in general since it works only
3725 for windows starting at the upper left corner;
3726 but that is all Emacs uses.
3727
ff11dfa1 3728 This string works only if the frame is using
08a24c47
JB
3729 the top of the video memory, because addressing is memory-relative.
3730 So first check the :ti string to see if that is true.
3731
3732 It would be simpler if the :wi string could go in the termcap
3733 entry, but it can't because it is not fully valid.
3734 If it were in the termcap entry, it would confuse other programs. */
fca177d4 3735 if (!tty->TS_set_window)
08a24c47 3736 {
fca177d4 3737 p = tty->TS_termcap_modes;
08a24c47
JB
3738 while (*p && strcmp (p, "\033v "))
3739 p++;
3740 if (*p)
fca177d4 3741 tty->TS_set_window = "\033v%C %C %C %C ";
08a24c47
JB
3742 }
3743 /* Termcap entry often fails to have :in: flag */
6ed8eeff 3744 terminal->must_write_spaces = 1;
08a24c47
JB
3745 /* :ti string typically fails to have \E^G! in it */
3746 /* This limits scope of insert-char to one line. */
fca177d4 3747 strcpy (area, tty->TS_termcap_modes);
08a24c47 3748 strcat (area, "\033\007!");
fca177d4 3749 tty->TS_termcap_modes = area;
08a24c47 3750 area += strlen (area) + 1;
6548cf00 3751 p = AbsPosition (tty);
08a24c47 3752 /* Change all %+ parameters to %C, to handle
28d440ab 3753 values above 96 correctly for the C100. */
08a24c47 3754 while (*p)
28d440ab
KL
3755 {
3756 if (p[0] == '%' && p[1] == '+')
3757 p[1] = 'C';
3758 p++;
3759 }
08a24c47
JB
3760 }
3761
0a125897 3762 tty->specified_window = FrameRows (tty);
08a24c47 3763
6548cf00 3764 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3224dac1 3765 {
6ed8eeff 3766 maybe_fatal (must_succeed, NULL, terminal,
3224dac1 3767 "Terminal type \"%s\" is not powerful enough to run Emacs",
08a24c47 3768#ifdef VMS
3224dac1 3769 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
08a24c47
JB
3770It lacks the ability to position the cursor.\n\
3771If that is not the actual type of terminal you have, use either the\n\
3772DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
e12c1054 3773or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
37dad45a
RS
3774#else /* not VMS */
3775# ifdef TERMINFO
3224dac1 3776 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
37dad45a
RS
3777It lacks the ability to position the cursor.\n\
3778If that is not the actual type of terminal you have,\n\
3779use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3780`setenv TERM ...') to specify the correct type. It may be necessary\n\
e12c1054 3781to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
37dad45a 3782# else /* TERMCAP */
3224dac1 3783 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
08a24c47
JB
3784It lacks the ability to position the cursor.\n\
3785If that is not the actual type of terminal you have,\n\
c5a9c3e6
RS
3786use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3787`setenv TERM ...') to specify the correct type. It may be necessary\n\
e12c1054 3788to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
37dad45a
RS
3789# endif /* TERMINFO */
3790#endif /*VMS */
3224dac1 3791 terminal_type);
fca177d4 3792 }
daf01701 3793
0a125897 3794 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
6ed8eeff 3795 maybe_fatal (must_succeed, NULL, terminal,
3224dac1
KL
3796 "Could not determine the frame size",
3797 "Could not determine the frame size");
08a24c47 3798
fca177d4
KL
3799 tty->delete_in_insert_mode
3800 = tty->TS_delete_mode && tty->TS_insert_mode
3801 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
08a24c47 3802
fca177d4
KL
3803 tty->se_is_so = (tty->TS_standout_mode
3804 && tty->TS_end_standout_mode
3805 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
08a24c47 3806
0b0d3e0b 3807 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
08a24c47 3808
6ed8eeff 3809 terminal->scroll_region_ok
6548cf00 3810 = (tty->Wcm->cm_abs
fca177d4 3811 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
08a24c47 3812
6ed8eeff 3813 terminal->line_ins_del_ok
fca177d4
KL
3814 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3815 && (tty->TS_del_line || tty->TS_del_multi_lines))
6ed8eeff 3816 || (terminal->scroll_region_ok
fca177d4 3817 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
08a24c47 3818
6ed8eeff 3819 terminal->char_ins_del_ok
fca177d4
KL
3820 = ((tty->TS_ins_char || tty->TS_insert_mode
3821 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3822 && (tty->TS_del_char || tty->TS_del_multi_chars));
08a24c47 3823
6ed8eeff 3824 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
08a24c47 3825
0b0d3e0b 3826 init_baud_rate (fileno (tty->input));
20a558dc 3827
daf01701
KL
3828#ifdef AIXHFT
3829 /* The HFT system on AIX doesn't optimize for scrolling, so it's
3830 really ugly at times. */
6ed8eeff
KL
3831 terminal->line_ins_del_ok = 0;
3832 terminal->char_ins_del_ok = 0;
daf01701
KL
3833#endif
3834
0a125897
KL
3835 /* Don't do this. I think termcap may still need the buffer. */
3836 /* xfree (buffer); */
28d440ab 3837
635e3b29
JR
3838#endif /* not WINDOWSNT */
3839
0a125897
KL
3840 /* Init system terminal modes (RAW or CBREAK, etc.). */
3841 init_sys_modes (tty);
daf01701 3842
6ed8eeff 3843 return terminal;
08a24c47
JB
3844}
3845
a4c6993d 3846/* Auxiliary error-handling function for init_tty.
6ed8eeff 3847 Free BUFFER and delete TERMINAL, then call error or fatal
da8e1115 3848 with str1 or str2, respectively, according to MUST_SUCCEED. */
6b61353c 3849
3224dac1 3850static void
6ed8eeff 3851maybe_fatal (must_succeed, buffer, terminal, str1, str2, arg1, arg2)
3224dac1
KL
3852 int must_succeed;
3853 char *buffer;
6ed8eeff 3854 struct terminal *terminal;
3224dac1
KL
3855 char *str1, *str2, *arg1, *arg2;
3856{
3857 if (buffer)
3858 xfree (buffer);
3859
6ed8eeff
KL
3860 if (terminal)
3861 delete_tty (terminal);
3224dac1
KL
3862
3863 if (must_succeed)
3864 fatal (str2, arg1, arg2);
3865 else
3866 error (str1, arg1, arg2);
3867
3868 abort ();
08a24c47
JB
3869}
3870
dfcf069d 3871void
7c401d15 3872fatal (const char *str, ...)
08a24c47 3873{
7c401d15
DN
3874 va_list ap;
3875 va_start (ap, str);
08a24c47 3876 fprintf (stderr, "emacs: ");
7c401d15
DN
3877 vfprintf (stderr, str, ap);
3878 va_end (ap);
08a24c47
JB
3879 fflush (stderr);
3880 exit (1);
3881}
07c57952 3882
819b8f00
KL
3883\f
3884
6ed8eeff 3885/* Delete the given tty terminal, closing all frames on it. */
da8e1115 3886
ed8dad6b 3887static void
6ed8eeff 3888delete_tty (struct terminal *terminal)
072d84a6 3889{
428a555e 3890 struct tty_display_info *tty;
fca177d4 3891 Lisp_Object tail, frame;
6ed8eeff 3892 int last_terminal;
4d553a13 3893
ab797f65
KL
3894 /* Protect against recursive calls. Fdelete_frame in
3895 delete_terminal calls us back when it deletes our last frame. */
59a7bc63 3896 if (!terminal->name)
0a125897 3897 return;
fca177d4 3898
6ed8eeff 3899 if (terminal->type != output_termcap)
428a555e
KL
3900 abort ();
3901
6ed8eeff 3902 tty = terminal->display_info.tty;
428a555e 3903
6ed8eeff 3904 last_terminal = 1;
81be1a93
KL
3905 FOR_EACH_FRAME (tail, frame)
3906 {
3907 struct frame *f = XFRAME (frame);
3908 if (FRAME_LIVE_P (f) && (!FRAME_TERMCAP_P (f) || FRAME_TTY (f) != tty))
3909 {
6ed8eeff 3910 last_terminal = 0;
81be1a93
KL
3911 break;
3912 }
3913 }
6ed8eeff
KL
3914 if (last_terminal)
3915 error ("Attempt to delete the sole terminal device with live frames");
81be1a93 3916
fca177d4
KL
3917 if (tty == tty_list)
3918 tty_list = tty->next;
3919 else
3920 {
28d7d09f 3921 struct tty_display_info *p;
fca177d4
KL
3922 for (p = tty_list; p && p->next != tty; p = p->next)
3923 ;
3924
3925 if (! p)
3926 /* This should not happen. */
3927 abort ();
3928
0a125897
KL
3929 p->next = tty->next;
3930 tty->next = 0;
fca177d4
KL
3931 }
3932
7e59217d 3933 /* reset_sys_modes needs a valid device, so this call needs to be
6ed8eeff 3934 before delete_terminal. */
04c3243c 3935 reset_sys_modes (tty);
fca177d4 3936
6ed8eeff 3937 delete_terminal (terminal);
3224dac1 3938
ab797f65
KL
3939 if (tty->name)
3940 xfree (tty->name);
3941
fca177d4
KL
3942 if (tty->type)
3943 xfree (tty->type);
daf01701 3944
fca177d4 3945 if (tty->input)
819b8f00
KL
3946 {
3947 delete_keyboard_wait_descriptor (fileno (tty->input));
3948 if (tty->input != stdin)
3949 fclose (tty->input);
3950 }
3951 if (tty->output && tty->output != stdout && tty->output != tty->input)
fca177d4
KL
3952 fclose (tty->output);
3953 if (tty->termscript)
3954 fclose (tty->termscript);
daf01701 3955
fca177d4 3956 if (tty->old_tty)
0a125897 3957 xfree (tty->old_tty);
fca177d4 3958
0a125897 3959 if (tty->Wcm)
daf01701
KL
3960 xfree (tty->Wcm);
3961
28d7d09f 3962 bzero (tty, sizeof (struct tty_display_info));
fca177d4 3963 xfree (tty);
fca177d4
KL
3964}
3965
428a555e
KL
3966\f
3967
28d7d09f 3968/* Mark the pointers in the tty_display_info objects.
819b8f00 3969 Called by the Fgarbage_collector. */
da8e1115 3970
fca177d4 3971void
ed8dad6b 3972mark_ttys (void)
fca177d4 3973{
28d7d09f 3974 struct tty_display_info *tty;
0c72d684 3975
819b8f00 3976 for (tty = tty_list; tty; tty = tty->next)
3b6fc48f 3977 mark_object (tty->top_frame);
072d84a6
RS
3978}
3979
428a555e
KL
3980\f
3981
dfcf069d 3982void
07c57952
KH
3983syms_of_term ()
3984{
7ee72033
MB
3985 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
3986 doc: /* Non-nil means the system uses terminfo rather than termcap.
228299fa 3987This variable can be used by terminal emulator packages. */);
07c57952
KH
3988#ifdef TERMINFO
3989 system_uses_terminfo = 1;
3990#else
3991 system_uses_terminfo = 0;
3992#endif
c291d9ef 3993
e4019195
DN
3994 DEFVAR_LISP ("suspend-tty-functions", &Vsuspend_tty_functions,
3995 doc: /* Functions to be run after suspending a tty.
ab797f65 3996The functions are run with one argument, the terminal id to be suspended.
0b0d3e0b 3997See `suspend-tty'. */);
e4019195 3998 Vsuspend_tty_functions = Qnil;
0b0d3e0b
KL
3999
4000
e4019195
DN
4001 DEFVAR_LISP ("resume-tty-functions", &Vresume_tty_functions,
4002 doc: /* Functions to be run after resuming a tty.
ab797f65 4003The functions are run with one argument, the terminal id that was revived.
0b0d3e0b 4004See `resume-tty'. */);
e4019195 4005 Vresume_tty_functions = Qnil;
a168702a 4006
0db017c0
SM
4007 DEFVAR_BOOL ("visible-cursor", &visible_cursor,
4008 doc: /* Non-nil means to make the cursor very visible.
4009This only has an effect when running in a text terminal.
4010What means \"very visible\" is up to your terminal. It may make the cursor
4011bigger, or it may make it blink, or it may do nothing at all. */);
4012 visible_cursor = 1;
4013
a168702a 4014 defsubr (&Stty_display_color_p);
bfa62f96 4015 defsubr (&Stty_display_color_cells);
072d84a6 4016 defsubr (&Stty_no_underline);
6ed8eeff
KL
4017 defsubr (&Stty_type);
4018 defsubr (&Scontrolling_tty_p);
0b0d3e0b
KL
4019 defsubr (&Ssuspend_tty);
4020 defsubr (&Sresume_tty);
7e5a23bd 4021#ifdef HAVE_GPM
6178ce5e
SM
4022 defsubr (&Sgpm_mouse_start);
4023 defsubr (&Sgpm_mouse_stop);
94da14a0 4024
6178ce5e 4025 staticpro (&mouse_face_window);
7e5a23bd 4026#endif /* HAVE_GPM */
07c57952 4027}
a168702a 4028
819b8f00
KL
4029
4030
6b61353c
KH
4031/* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
4032 (do not change this comment) */