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