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