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