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