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