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