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