gnus-win.el (gnus-configure-frame): Pass an arg to window-dedicated-p
[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
2135TERMINAL does not refer to a text-only 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
2152TERMINAL does not refer to a text-only 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
2374TERMINAL does not refer to a text-only 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
ed8dad6b
KL
2384\f
2385
2386DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2387 doc: /* Suspend the terminal device TTY.
2388
2389The device is restored to its default state, and Emacs ceases all
2390access to the tty device. Frames that use the device are not deleted,
2391but input is not read from them and if they change, their display is
2392not updated.
2393
401e9e57
CY
2394TTY may be a terminal object, a frame, or nil for the terminal device
2395of the currently selected frame.
ed8dad6b 2396
e4019195 2397This function runs `suspend-tty-functions' after suspending the
ed8dad6b
KL
2398device. The functions are run with one arg, the id of the suspended
2399terminal device.
2400
2401`suspend-tty' does nothing if it is called on a device that is already
2402suspended.
2403
2404A suspended tty may be resumed by calling `resume-tty' on it. */)
5842a27b 2405 (Lisp_Object tty)
ed8dad6b 2406{
717a00ef 2407 struct terminal *t = get_tty_terminal (tty, 1);
ed8dad6b 2408 FILE *f;
f4d953fc 2409
6ed8eeff 2410 if (!t)
ed8dad6b
KL
2411 error ("Unknown tty device");
2412
6ed8eeff 2413 f = t->display_info.tty->input;
f4d953fc 2414
ed8dad6b
KL
2415 if (f)
2416 {
23d4cba5
DN
2417 /* First run `suspend-tty-functions' and then clean up the tty
2418 state because `suspend-tty-functions' might need to change
2419 the tty state. */
dee091a3
JD
2420 Lisp_Object args[2];
2421 args[0] = intern ("suspend-tty-functions");
2422 XSETTERMINAL (args[1], t);
2423 Frun_hook_with_args (2, args);
23d4cba5 2424
6ed8eeff 2425 reset_sys_modes (t->display_info.tty);
ed8dad6b 2426 delete_keyboard_wait_descriptor (fileno (f));
f4d953fc 2427
84704c5c 2428#ifndef MSDOS
ed8dad6b 2429 fclose (f);
6ed8eeff
KL
2430 if (f != t->display_info.tty->output)
2431 fclose (t->display_info.tty->output);
84704c5c 2432#endif
f4d953fc 2433
6ed8eeff
KL
2434 t->display_info.tty->input = 0;
2435 t->display_info.tty->output = 0;
ed8dad6b 2436
6ed8eeff
KL
2437 if (FRAMEP (t->display_info.tty->top_frame))
2438 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
f4d953fc 2439
ed8dad6b
KL
2440 }
2441
4a665758
KL
2442 /* Clear display hooks to prevent further output. */
2443 clear_tty_hooks (t);
2444
ed8dad6b
KL
2445 return Qnil;
2446}
2447
2448DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2449 doc: /* Resume the previously suspended terminal device TTY.
2450The terminal is opened and reinitialized. Frames that are on the
6ed8eeff 2451suspended terminal are revived.
ed8dad6b 2452
6ed8eeff
KL
2453It is an error to resume a terminal while another terminal is active
2454on the same device.
ed8dad6b 2455
e4019195 2456This function runs `resume-tty-functions' after resuming the terminal.
6ed8eeff 2457The functions are run with one arg, the id of the resumed terminal
ed8dad6b
KL
2458device.
2459
2460`resume-tty' does nothing if it is called on a device that is not
2461suspended.
2462
401e9e57
CY
2463TTY may be a terminal object, a frame, or nil (meaning the selected
2464frame's terminal). */)
5842a27b 2465 (Lisp_Object tty)
ed8dad6b 2466{
717a00ef 2467 struct terminal *t = get_tty_terminal (tty, 1);
ed8dad6b
KL
2468 int fd;
2469
6ed8eeff 2470 if (!t)
ed8dad6b
KL
2471 error ("Unknown tty device");
2472
6ed8eeff 2473 if (!t->display_info.tty->input)
ed8dad6b 2474 {
6ed8eeff 2475 if (get_named_tty (t->display_info.tty->name))
ed8dad6b
KL
2476 error ("Cannot resume display while another display is active on the same device");
2477
84704c5c
EZ
2478#ifdef MSDOS
2479 t->display_info.tty->output = stdout;
2480 t->display_info.tty->input = stdin;
2481#else /* !MSDOS */
6ed8eeff 2482 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
ed8dad6b 2483
59b3194c
KL
2484 if (fd == -1)
2485 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
ed8dad6b 2486
78048085 2487 if (strcmp (t->display_info.tty->name, DEV_TTY))
59b3194c 2488 dissociate_if_controlling_tty (fd);
78048085 2489
6ed8eeff
KL
2490 t->display_info.tty->output = fdopen (fd, "w+");
2491 t->display_info.tty->input = t->display_info.tty->output;
84704c5c 2492#endif
78048085 2493
ed8dad6b
KL
2494 add_keyboard_wait_descriptor (fd);
2495
6ed8eeff 2496 if (FRAMEP (t->display_info.tty->top_frame))
db878925
DN
2497 {
2498 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2499 int width, height;
2500 int old_height = FRAME_COLS (f);
2501 int old_width = FRAME_LINES (f);
2502
2503 /* Check if terminal/window size has changed while the frame
2504 was suspended. */
2505 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2506 if (width != old_width || height != old_height)
2507 change_frame_size (f, height, width, 0, 0, 0);
2508 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2509 }
ed8dad6b 2510
04f2d78b 2511 set_tty_hooks (t);
6ed8eeff 2512 init_sys_modes (t->display_info.tty);
ed8dad6b 2513
dee091a3
JD
2514 {
2515 /* Run `resume-tty-functions'. */
2516 Lisp_Object args[2];
2517 args[0] = intern ("resume-tty-functions");
2518 XSETTERMINAL (args[1], t);
2519 Frun_hook_with_args (2, args);
2520 }
ed8dad6b
KL
2521 }
2522
4a665758
KL
2523 set_tty_hooks (t);
2524
ed8dad6b
KL
2525 return Qnil;
2526}
a3fbb897 2527
819b8f00 2528\f
e882229c
NR
2529/***********************************************************************
2530 Mouse
2531 ***********************************************************************/
2532
7e5a23bd 2533#ifdef HAVE_GPM
64520e5c
PE
2534
2535#ifndef HAVE_WINDOW_SYSTEM
5faa03ba
RS
2536void
2537term_mouse_moveto (int x, int y)
94da14a0 2538{
1ec5dc77 2539 /* TODO: how to set mouse position?
94da14a0
NR
2540 const char *name;
2541 int fd;
2542 name = (const char *) ttyname (0);
2543 fd = open (name, O_WRONLY);
80fb2ce0 2544 SOME_FUNCTION (x, y, fd);
94da14a0
NR
2545 close (fd);
2546 last_mouse_x = x;
80fb2ce0 2547 last_mouse_y = y; */
94da14a0 2548}
64520e5c 2549#endif /* HAVE_WINDOW_SYSTEM */
94da14a0 2550
cf482c50
EZ
2551/* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2552void
2553tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row,
2554 int start_hpos, int end_hpos,
2555 enum draw_glyphs_face draw)
e882229c 2556{
cf482c50
EZ
2557 int nglyphs = end_hpos - start_hpos;
2558 struct frame *f = XFRAME (WINDOW_FRAME (w));
7be1c21a 2559 struct tty_display_info *tty = FRAME_TTY (f);
cf482c50
EZ
2560 int face_id = tty->mouse_highlight.mouse_face_face_id;
2561 int save_x, save_y, pos_x, pos_y;
7be1c21a 2562
cf482c50
EZ
2563 if (end_hpos >= row->used[TEXT_AREA])
2564 nglyphs = row->used[TEXT_AREA] - start_hpos;
e882229c 2565
cf482c50
EZ
2566 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2567 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos + WINDOW_LEFT_EDGE_X (w);
e882229c 2568
cf482c50
EZ
2569 /* Save current cursor co-ordinates. */
2570 save_y = curY (tty);
2571 save_x = curX (tty);
2572 cursor_to (f, pos_y, pos_x);
e882229c 2573
cf482c50
EZ
2574 if (draw == DRAW_MOUSE_FACE)
2575 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2576 nglyphs, face_id);
2577 else if (draw == DRAW_NORMAL_TEXT)
2578 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
e882229c 2579
cf482c50 2580 cursor_to (f, save_y, save_x);
e882229c
NR
2581}
2582
2583static int
2584term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
2585{
2586 /* Has the mouse moved off the glyph it was on at the last sighting? */
2587 if (event->x != last_mouse_x || event->y != last_mouse_y)
2588 {
2589 frame->mouse_moved = 1;
cf482c50 2590 note_mouse_highlight (frame, event->x, event->y);
e882229c
NR
2591 /* Remember which glyph we're now on. */
2592 last_mouse_x = event->x;
2593 last_mouse_y = event->y;
2594 return 1;
2595 }
2596 return 0;
2597}
2598
2599/* Return the current position of the mouse.
2600
2601 Set *f to the frame the mouse is in, or zero if the mouse is in no
2602 Emacs frame. If it is set to zero, all the other arguments are
2603 garbage.
2604
2605 Set *bar_window to Qnil, and *x and *y to the column and
2606 row of the character cell the mouse is over.
2607
7f3f1250 2608 Set *timeptr to the time the mouse was at the returned position.
e882229c 2609
80fb2ce0 2610 This clears mouse_moved until the next motion
94da14a0 2611 event arrives. */
e882229c
NR
2612static void
2613term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
2614 enum scroll_bar_part *part, Lisp_Object *x,
08dc5ae6 2615 Lisp_Object *y, Time *timeptr)
e882229c 2616{
e882229c 2617 struct timeval now;
f6a24d19 2618 Time sec, usec;
e882229c
NR
2619
2620 *fp = SELECTED_FRAME ();
94da14a0 2621 (*fp)->mouse_moved = 0;
e882229c
NR
2622
2623 *bar_window = Qnil;
2624 *part = 0;
2625
80fb2ce0 2626 XSETINT (*x, last_mouse_x);
94da14a0 2627 XSETINT (*y, last_mouse_y);
e882229c 2628 gettimeofday(&now, 0);
f6a24d19
PE
2629 sec = now.tv_sec;
2630 usec = now.tv_usec;
2631 *timeptr = (sec * 1000) + (usec / 1000);
e882229c
NR
2632}
2633
2634/* Prepare a mouse-event in *RESULT for placement in the input queue.
2635
2636 If the event is a button press, then note that we have grabbed
2637 the mouse. */
2638
2639static Lisp_Object
2640term_mouse_click (struct input_event *result, Gpm_Event *event,
2641 struct frame *f)
2642{
2643 struct timeval now;
2644 int i, j;
2645
2646 result->kind = GPM_CLICK_EVENT;
2647 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2648 {
2649 if (event->buttons & j) {
2650 result->code = i; /* button number */
2651 break;
2652 }
2653 }
2654 gettimeofday(&now, 0);
2655 result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
2656
2657 if (event->type & GPM_UP)
2658 result->modifiers = up_modifier;
2659 else if (event->type & GPM_DOWN)
2660 result->modifiers = down_modifier;
2661 else
2662 result->modifiers = 0;
f4d953fc 2663
e882229c
NR
2664 if (event->type & GPM_SINGLE)
2665 result->modifiers |= click_modifier;
f4d953fc 2666
e882229c
NR
2667 if (event->type & GPM_DOUBLE)
2668 result->modifiers |= double_modifier;
2669
2670 if (event->type & GPM_TRIPLE)
2671 result->modifiers |= triple_modifier;
2672
2673 if (event->type & GPM_DRAG)
2674 result->modifiers |= drag_modifier;
2675
80fb2ce0 2676 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
e882229c
NR
2677
2678 /* 1 << KG_SHIFT */
2679 if (event->modifiers & (1 << 0))
2680 result->modifiers |= shift_modifier;
2681
2682 /* 1 << KG_CTRL */
2683 if (event->modifiers & (1 << 2))
2684 result->modifiers |= ctrl_modifier;
2685
2686 /* 1 << KG_ALT || KG_ALTGR */
2687 if (event->modifiers & (1 << 3)
2688 || event->modifiers & (1 << 1))
2689 result->modifiers |= meta_modifier;
2690 }
2691
80fb2ce0
NR
2692 XSETINT (result->x, event->x);
2693 XSETINT (result->y, event->y);
e882229c
NR
2694 XSETFRAME (result->frame_or_window, f);
2695 result->arg = Qnil;
2696 return Qnil;
2697}
2698
f4d953fc 2699int
7be1c21a 2700handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
e882229c 2701{
7be1c21a 2702 struct frame *f = XFRAME (tty->top_frame);
e882229c
NR
2703 struct input_event ie;
2704 int do_help = 0;
2705 int count = 0;
2706
2707 EVENT_INIT (ie);
2708 ie.kind = NO_EVENT;
2709 ie.arg = Qnil;
2710
80fb2ce0 2711 if (event->type & (GPM_MOVE | GPM_DRAG)) {
e882229c
NR
2712 previous_help_echo_string = help_echo_string;
2713 help_echo_string = Qnil;
2714
6178ce5e 2715 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
e882229c 2716
80fb2ce0
NR
2717 if (!term_mouse_movement (f, event))
2718 help_echo_string = previous_help_echo_string;
e882229c
NR
2719
2720 /* If the contents of the global variable help_echo_string
2721 has changed, generate a HELP_EVENT. */
2722 if (!NILP (help_echo_string)
2723 || !NILP (previous_help_echo_string))
2724 do_help = 1;
2725
2726 goto done;
2727 }
2728 else {
2729 f->mouse_moved = 0;
2730 term_mouse_click (&ie, event, f);
e882229c
NR
2731 }
2732
2733 done:
2734 if (ie.kind != NO_EVENT)
2735 {
2736 kbd_buffer_store_event_hold (&ie, hold_quit);
2737 count++;
2738 }
2739
2740 if (do_help
2741 && !(hold_quit && hold_quit->kind != NO_EVENT))
2742 {
2743 Lisp_Object frame;
2744
2745 if (f)
2746 XSETFRAME (frame, f);
2747 else
2748 frame = Qnil;
2749
2750 gen_help_event (help_echo_string, frame, help_echo_window,
2751 help_echo_object, help_echo_pos);
2752 count++;
2753 }
2754
2755 return count;
2756}
2757
6178ce5e 2758DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
e882229c 2759 0, 0, 0,
71f44e7a 2760 doc: /* Open a connection to Gpm.
6178ce5e 2761Gpm-mouse can only be activated for one tty at a time. */)
5842a27b 2762 (void)
e882229c 2763{
71f44e7a
SM
2764 struct frame *f = SELECTED_FRAME ();
2765 struct tty_display_info *tty
2766 = ((f)->output_method == output_termcap
2767 ? (f)->terminal->display_info.tty : NULL);
e882229c
NR
2768 Gpm_Connect connection;
2769
6178ce5e
SM
2770 if (!tty)
2771 error ("Gpm-mouse only works in the GNU/Linux console");
4ce5ab77
SM
2772 if (gpm_tty == tty)
2773 return Qnil; /* Already activated, nothing to do. */
2774 if (gpm_tty)
2775 error ("Gpm-mouse can only be activated for one tty at a time");
71f44e7a 2776
e882229c
NR
2777 connection.eventMask = ~0;
2778 connection.defaultMask = ~GPM_HARD;
2779 connection.maxMod = ~0;
2780 connection.minMod = 0;
80fb2ce0 2781 gpm_zerobased = 1;
e882229c
NR
2782
2783 if (Gpm_Open (&connection, 0) < 0)
6178ce5e 2784 error ("Gpm-mouse failed to connect to the gpm daemon");
e882229c
NR
2785 else
2786 {
71f44e7a 2787 gpm_tty = tty;
1023cbed
SM
2788 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2789 to generate SIGIOs. Apparently we need to call reset_sys_modes
2790 before calling init_sys_modes. */
7be1c21a
MB
2791 reset_sys_modes (tty);
2792 init_sys_modes (tty);
e882229c 2793 add_gpm_wait_descriptor (gpm_fd);
6178ce5e 2794 return Qnil;
e882229c
NR
2795 }
2796}
2797
ed5ff21d 2798void
d347e494 2799close_gpm (int fd)
ed5ff21d 2800{
d347e494
SM
2801 if (fd >= 0)
2802 delete_gpm_wait_descriptor (fd);
ed5ff21d
SM
2803 while (Gpm_Close()); /* close all the stack */
2804 gpm_tty = NULL;
2805}
2806
6178ce5e 2807DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
e882229c
NR
2808 0, 0, 0,
2809 doc: /* Close a connection to Gpm. */)
5842a27b 2810 (void)
e882229c 2811{
4ce5ab77
SM
2812 struct frame *f = SELECTED_FRAME ();
2813 struct tty_display_info *tty
2814 = ((f)->output_method == output_termcap
2815 ? (f)->terminal->display_info.tty : NULL);
2816
2817 if (!tty || gpm_tty != tty)
2818 return Qnil; /* Not activated on this terminal, nothing to do. */
f4d953fc 2819
d347e494 2820 close_gpm (gpm_fd);
6178ce5e 2821 return Qnil;
e882229c 2822}
7e5a23bd 2823#endif /* HAVE_GPM */
e882229c
NR
2824
2825\f
89ba96f4 2826#ifndef MSDOS
a168702a
GM
2827/***********************************************************************
2828 Initialization
2829 ***********************************************************************/
2830
ed8dad6b
KL
2831/* Initialize the tty-dependent part of frame F. The frame must
2832 already have its device initialized. */
3224dac1 2833
dfcf069d 2834void
ed8dad6b 2835create_tty_output (struct frame *f)
28d440ab 2836{
ed8dad6b
KL
2837 struct tty_output *t;
2838
2839 if (! FRAME_TERMCAP_P (f))
428a555e
KL
2840 abort ();
2841
ed8dad6b 2842 t = xmalloc (sizeof (struct tty_output));
72af86bd 2843 memset (t, 0, sizeof (struct tty_output));
b6660415 2844
6ed8eeff 2845 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
bedb9c0e 2846
ed8dad6b 2847 f->output_data.tty = t;
3224dac1
KL
2848}
2849
9f215d25 2850/* Delete frame F's face cache, and its tty-dependent part. */
da8e1115 2851
ed8dad6b 2852static void
9f215d25 2853tty_free_frame_resources (struct frame *f)
3224dac1 2854{
ed8dad6b 2855 if (! FRAME_TERMCAP_P (f))
3224dac1
KL
2856 abort ();
2857
9f215d25
CY
2858 if (FRAME_FACE_CACHE (f))
2859 free_frame_faces (f);
2860
ed8dad6b 2861 xfree (f->output_data.tty);
28d440ab
KL
2862}
2863
89ba96f4
EZ
2864#else /* MSDOS */
2865
2866/* Delete frame F's face cache. */
2867
2868static void
2869tty_free_frame_resources (struct frame *f)
2870{
2871 if (! FRAME_TERMCAP_P (f) && ! FRAME_MSDOS_P (f))
2872 abort ();
2873
2874 if (FRAME_FACE_CACHE (f))
2875 free_frame_faces (f);
2876}
2877#endif /* MSDOS */
ed8dad6b 2878\f
47cc8819 2879/* Reset the hooks in TERMINAL. */
ed8dad6b 2880
4a665758
KL
2881static void
2882clear_tty_hooks (struct terminal *terminal)
2883{
2884 terminal->rif = 0;
2885 terminal->cursor_to_hook = 0;
2886 terminal->raw_cursor_to_hook = 0;
2887 terminal->clear_to_end_hook = 0;
2888 terminal->clear_frame_hook = 0;
2889 terminal->clear_end_of_line_hook = 0;
2890 terminal->ins_del_lines_hook = 0;
2891 terminal->insert_glyphs_hook = 0;
2892 terminal->write_glyphs_hook = 0;
2893 terminal->delete_glyphs_hook = 0;
2894 terminal->ring_bell_hook = 0;
2895 terminal->reset_terminal_modes_hook = 0;
2896 terminal->set_terminal_modes_hook = 0;
2897 terminal->update_begin_hook = 0;
2898 terminal->update_end_hook = 0;
2899 terminal->set_terminal_window_hook = 0;
2900 terminal->mouse_position_hook = 0;
2901 terminal->frame_rehighlight_hook = 0;
2902 terminal->frame_raise_lower_hook = 0;
974b73e8 2903 terminal->fullscreen_hook = 0;
4a665758
KL
2904 terminal->set_vertical_scroll_bar_hook = 0;
2905 terminal->condemn_scroll_bars_hook = 0;
2906 terminal->redeem_scroll_bar_hook = 0;
2907 terminal->judge_scroll_bars_hook = 0;
2908 terminal->read_socket_hook = 0;
2909 terminal->frame_up_to_date_hook = 0;
2910
2911 /* Leave these two set, or suspended frames are not deleted
2912 correctly. */
9f215d25 2913 terminal->delete_frame_hook = &tty_free_frame_resources;
4a665758
KL
2914 terminal->delete_terminal_hook = &delete_tty;
2915}
2916
47cc8819
DN
2917/* Initialize hooks in TERMINAL with the values needed for a tty. */
2918
4a665758
KL
2919static void
2920set_tty_hooks (struct terminal *terminal)
2921{
2922 terminal->rif = 0; /* ttys don't support window-based redisplay. */
2923
2924 terminal->cursor_to_hook = &tty_cursor_to;
2925 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
2926
2927 terminal->clear_to_end_hook = &tty_clear_to_end;
2928 terminal->clear_frame_hook = &tty_clear_frame;
2929 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
2930
2931 terminal->ins_del_lines_hook = &tty_ins_del_lines;
2932
2933 terminal->insert_glyphs_hook = &tty_insert_glyphs;
2934 terminal->write_glyphs_hook = &tty_write_glyphs;
2935 terminal->delete_glyphs_hook = &tty_delete_glyphs;
2936
2937 terminal->ring_bell_hook = &tty_ring_bell;
f4d953fc 2938
4a665758
KL
2939 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
2940 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
2941 terminal->update_begin_hook = 0; /* Not needed. */
2942 terminal->update_end_hook = &tty_update_end;
2943 terminal->set_terminal_window_hook = &tty_set_terminal_window;
2944
2945 terminal->mouse_position_hook = 0; /* Not needed. */
2946 terminal->frame_rehighlight_hook = 0; /* Not needed. */
2947 terminal->frame_raise_lower_hook = 0; /* Not needed. */
2948
2949 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
2950 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
2951 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
2952 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
2953
2954 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
2955 terminal->frame_up_to_date_hook = 0; /* Not needed. */
f4d953fc 2956
9f215d25 2957 terminal->delete_frame_hook = &tty_free_frame_resources;
4a665758
KL
2958 terminal->delete_terminal_hook = &delete_tty;
2959}
2960
03a1d6bd 2961/* Drop the controlling terminal if fd is the same device. */
ed8dad6b 2962static void
03a1d6bd
KL
2963dissociate_if_controlling_tty (int fd)
2964{
84704c5c 2965#ifndef DOS_NT
12e610e8 2966 int pgid = tcgetpgrp (fd); /* If tcgetpgrp succeeds, fd is the ctty. */
03a1d6bd
KL
2967 if (pgid != -1)
2968 {
6254cdda 2969#if defined (USG5)
03a1d6bd
KL
2970 setpgrp ();
2971 no_controlling_tty = 1;
de52dcbb
DN
2972#elif defined (CYGWIN)
2973 setsid ();
2974 no_controlling_tty = 1;
03a1d6bd
KL
2975#else
2976#ifdef TIOCNOTTY /* Try BSD ioctls. */
8516815b 2977 sigblock (sigmask (SIGTTOU));
78048085 2978 fd = emacs_open (DEV_TTY, O_RDWR, 0);
8516815b
KL
2979 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
2980 {
2981 no_controlling_tty = 1;
2982 }
2983 if (fd != -1)
2984 emacs_close (fd);
2985 sigunblock (sigmask (SIGTTOU));
03a1d6bd 2986#else
8516815b
KL
2987 /* Unknown system. */
2988 croak ();
03a1d6bd
KL
2989#endif /* ! TIOCNOTTY */
2990#endif /* ! USG */
71a96040 2991 }
84704c5c 2992#endif /* !DOS_NT */
03a1d6bd
KL
2993}
2994
3224dac1
KL
2995/* Create a termcap display on the tty device with the given name and
2996 type.
2997
ab797f65 2998 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3224dac1
KL
2999 Otherwise NAME should be a path to the tty device file,
3000 e.g. "/dev/pts/7".
28d440ab 3001
3224dac1
KL
3002 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3003
3004 If MUST_SUCCEED is true, then all errors are fatal. */
da8e1115 3005
6ed8eeff 3006struct terminal *
8ea90aa3 3007init_tty (const char *name, const char *terminal_type, int must_succeed)
08a24c47 3008{
59b3194c 3009 char *area = NULL;
08a24c47 3010 char **address = &area;
6b61353c 3011 int buffer_size = 4096;
08a24c47 3012 int status;
59b3194c
KL
3013 struct tty_display_info *tty = NULL;
3014 struct terminal *terminal = NULL;
3015 int ctty = 0; /* 1 if asked to open controlling tty. */
28d440ab 3016
3224dac1 3017 if (!terminal_type)
d31eee5e 3018 maybe_fatal (must_succeed, 0,
3224dac1
KL
3019 "Unknown terminal type",
3020 "Unknown terminal type");
b6660415 3021
ab797f65 3022 if (name == NULL)
78048085
EZ
3023 name = DEV_TTY;
3024 if (!strcmp (name, DEV_TTY))
ab797f65
KL
3025 ctty = 1;
3026
6ed8eeff
KL
3027 /* If we already have a terminal on the given device, use that. If
3028 all such terminals are suspended, create a new one instead. */
a4c6993d 3029 /* XXX Perhaps this should be made explicit by having init_tty
6ed8eeff 3030 always create a new terminal and separating terminal and frame
b6660415 3031 creation on Lisp level. */
6ed8eeff
KL
3032 terminal = get_named_tty (name);
3033 if (terminal)
3034 return terminal;
78048085 3035
6ed8eeff 3036 terminal = create_terminal ();
84704c5c
EZ
3037#ifdef MSDOS
3038 if (been_here > 0)
762b15be 3039 maybe_fatal (0, 0, "Attempt to create another terminal %s", "",
84704c5c
EZ
3040 name, "");
3041 been_here = 1;
3042 tty = &the_only_display_info;
3043#else
3224dac1 3044 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
84704c5c 3045#endif
72af86bd 3046 memset (tty, 0, sizeof (struct tty_display_info));
3224dac1
KL
3047 tty->next = tty_list;
3048 tty_list = tty;
428a555e 3049
6ed8eeff
KL
3050 terminal->type = output_termcap;
3051 terminal->display_info.tty = tty;
3052 tty->terminal = terminal;
28d440ab 3053
7b00d185
KL
3054 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
3055 Wcm_clear (tty);
daf01701 3056
dce4c2ac
DN
3057 encode_terminal_src_size = 0;
3058 encode_terminal_dst_size = 0;
3059
51b59d79 3060
84704c5c 3061#ifndef DOS_NT
4a665758 3062 set_tty_hooks (terminal);
78048085 3063
59b3194c
KL
3064 {
3065 int fd;
3066 FILE *file;
0c72d684
KL
3067
3068#ifdef O_IGNORE_CTTY
59b3194c 3069 if (!ctty)
0c72d684
KL
3070 /* Open the terminal device. Don't recognize it as our
3071 controlling terminal, and don't make it the controlling tty
3072 if we don't have one at the moment. */
3073 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
59b3194c 3074 else
0080dc6b 3075#endif /* O_IGNORE_CTTY */
2666355c 3076 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
59b3194c
KL
3077 defined on Hurd. On other systems, we need to explicitly
3078 dissociate ourselves from the controlling tty when we want to
3079 open a frame on the same terminal. */
0c72d684 3080 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
0c72d684 3081
97c6058a
DN
3082 tty->name = xstrdup (name);
3083 terminal->name = xstrdup (name);
3084
59b3194c 3085 if (fd < 0)
d31eee5e 3086 maybe_fatal (must_succeed, terminal,
59b3194c
KL
3087 "Could not open file: %s",
3088 "Could not open file: %s",
3089 name);
3090 if (!isatty (fd))
3091 {
3092 close (fd);
d31eee5e 3093 maybe_fatal (must_succeed, terminal,
59b3194c
KL
3094 "Not a tty device: %s",
3095 "Not a tty device: %s",
3096 name);
3097 }
0c72d684 3098
59b3194c
KL
3099#ifndef O_IGNORE_CTTY
3100 if (!ctty)
03a1d6bd 3101 dissociate_if_controlling_tty (fd);
59b3194c
KL
3102#endif
3103
3104 file = fdopen (fd, "w+");
59b3194c
KL
3105 tty->input = file;
3106 tty->output = file;
3107 }
78048085 3108
28d7d09f 3109 tty->type = xstrdup (terminal_type);
28d440ab 3110
819b8f00 3111 add_keyboard_wait_descriptor (fileno (tty->input));
08a24c47 3112
6548cf00 3113 Wcm_clear (tty);
08a24c47 3114
d31eee5e 3115 tty->termcap_term_buffer = (char *) xmalloc (buffer_size);
78048085 3116
03a1d6bd
KL
3117 /* On some systems, tgetent tries to access the controlling
3118 terminal. */
3119 sigblock (sigmask (SIGTTOU));
d31eee5e 3120 status = tgetent (tty->termcap_term_buffer, terminal_type);
03a1d6bd 3121 sigunblock (sigmask (SIGTTOU));
78048085 3122
08a24c47 3123 if (status < 0)
b0347178
KH
3124 {
3125#ifdef TERMINFO
d31eee5e 3126 maybe_fatal (must_succeed, terminal,
3224dac1
KL
3127 "Cannot open terminfo database file",
3128 "Cannot open terminfo database file");
b0347178 3129#else
d31eee5e 3130 maybe_fatal (must_succeed, terminal,
3224dac1
KL
3131 "Cannot open termcap database file",
3132 "Cannot open termcap database file");
b0347178
KH
3133#endif
3134 }
08a24c47 3135 if (status == 0)
b0347178 3136 {
d31eee5e 3137 maybe_fatal (must_succeed, terminal,
3224dac1
KL
3138 "Terminal type %s is not defined",
3139 "Terminal type %s is not defined.\n\
b0347178
KH
3140If that is not the actual type of terminal you have,\n\
3141use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
84164a0d
SM
3142`setenv TERM ...') to specify the correct type. It may be necessary\n"
3143#ifdef TERMINFO
3144"to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
b0347178 3145#else
84164a0d 3146"to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
b0347178 3147#endif
84164a0d 3148 terminal_type);
b0347178 3149 }
6b61353c
KH
3150
3151#ifndef TERMINFO
d31eee5e 3152 if (strlen (tty->termcap_term_buffer) >= buffer_size)
08a24c47 3153 abort ();
d31eee5e 3154 buffer_size = strlen (tty->termcap_term_buffer);
6b61353c 3155#endif
d31eee5e 3156 tty->termcap_strings_buffer = area = (char *) xmalloc (buffer_size);
fca177d4
KL
3157 tty->TS_ins_line = tgetstr ("al", address);
3158 tty->TS_ins_multi_lines = tgetstr ("AL", address);
3159 tty->TS_bell = tgetstr ("bl", address);
6548cf00 3160 BackTab (tty) = tgetstr ("bt", address);
fca177d4
KL
3161 tty->TS_clr_to_bottom = tgetstr ("cd", address);
3162 tty->TS_clr_line = tgetstr ("ce", address);
3163 tty->TS_clr_frame = tgetstr ("cl", address);
6548cf00
KL
3164 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
3165 AbsPosition (tty) = tgetstr ("cm", address);
3166 CR (tty) = tgetstr ("cr", address);
fca177d4
KL
3167 tty->TS_set_scroll_region = tgetstr ("cs", address);
3168 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
6548cf00 3169 RowPosition (tty) = tgetstr ("cv", address);
fca177d4
KL
3170 tty->TS_del_char = tgetstr ("dc", address);
3171 tty->TS_del_multi_chars = tgetstr ("DC", address);
3172 tty->TS_del_line = tgetstr ("dl", address);
3173 tty->TS_del_multi_lines = tgetstr ("DL", address);
3174 tty->TS_delete_mode = tgetstr ("dm", address);
3175 tty->TS_end_delete_mode = tgetstr ("ed", address);
3176 tty->TS_end_insert_mode = tgetstr ("ei", address);
6548cf00 3177 Home (tty) = tgetstr ("ho", address);
fca177d4
KL
3178 tty->TS_ins_char = tgetstr ("ic", address);
3179 tty->TS_ins_multi_chars = tgetstr ("IC", address);
3180 tty->TS_insert_mode = tgetstr ("im", address);
3181 tty->TS_pad_inserted_char = tgetstr ("ip", address);
3182 tty->TS_end_keypad_mode = tgetstr ("ke", address);
3183 tty->TS_keypad_mode = tgetstr ("ks", address);
6548cf00
KL
3184 LastLine (tty) = tgetstr ("ll", address);
3185 Right (tty) = tgetstr ("nd", address);
3186 Down (tty) = tgetstr ("do", address);
3187 if (!Down (tty))
3188 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
08a24c47 3189 if (tgetflag ("bs"))
6548cf00 3190 Left (tty) = "\b"; /* can't possibly be longer! */
08a24c47 3191 else /* (Actually, "bs" is obsolete...) */
6548cf00
KL
3192 Left (tty) = tgetstr ("le", address);
3193 if (!Left (tty))
3194 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
fca177d4
KL
3195 tty->TS_pad_char = tgetstr ("pc", address);
3196 tty->TS_repeat = tgetstr ("rp", address);
3197 tty->TS_end_standout_mode = tgetstr ("se", address);
3198 tty->TS_fwd_scroll = tgetstr ("sf", address);
3199 tty->TS_standout_mode = tgetstr ("so", address);
3200 tty->TS_rev_scroll = tgetstr ("sr", address);
6548cf00 3201 tty->Wcm->cm_tab = tgetstr ("ta", address);
fca177d4
KL
3202 tty->TS_end_termcap_modes = tgetstr ("te", address);
3203 tty->TS_termcap_modes = tgetstr ("ti", address);
6548cf00 3204 Up (tty) = tgetstr ("up", address);
fca177d4
KL
3205 tty->TS_visible_bell = tgetstr ("vb", address);
3206 tty->TS_cursor_normal = tgetstr ("ve", address);
3207 tty->TS_cursor_visible = tgetstr ("vs", address);
3208 tty->TS_cursor_invisible = tgetstr ("vi", address);
3209 tty->TS_set_window = tgetstr ("wi", address);
3210
3211 tty->TS_enter_underline_mode = tgetstr ("us", address);
3212 tty->TS_exit_underline_mode = tgetstr ("ue", address);
3213 tty->TS_enter_bold_mode = tgetstr ("md", address);
cd4eb164 3214 tty->TS_enter_italic_mode = tgetstr ("ZH", address);
fca177d4 3215 tty->TS_enter_dim_mode = tgetstr ("mh", address);
fca177d4
KL
3216 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
3217 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
3218 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
3219 tty->TS_exit_attribute_mode = tgetstr ("me", address);
177c0ea7 3220
6548cf00
KL
3221 MultiUp (tty) = tgetstr ("UP", address);
3222 MultiDown (tty) = tgetstr ("DO", address);
3223 MultiLeft (tty) = tgetstr ("LE", address);
3224 MultiRight (tty) = tgetstr ("RI", address);
08a24c47 3225
5c32d3f2 3226 /* SVr4/ANSI color support. If "op" isn't available, don't support
e7f90eab
GM
3227 color because we can't switch back to the default foreground and
3228 background. */
fca177d4
KL
3229 tty->TS_orig_pair = tgetstr ("op", address);
3230 if (tty->TS_orig_pair)
a168702a 3231 {
fca177d4
KL
3232 tty->TS_set_foreground = tgetstr ("AF", address);
3233 tty->TS_set_background = tgetstr ("AB", address);
3234 if (!tty->TS_set_foreground)
e7f90eab
GM
3235 {
3236 /* SVr4. */
fca177d4
KL
3237 tty->TS_set_foreground = tgetstr ("Sf", address);
3238 tty->TS_set_background = tgetstr ("Sb", address);
e7f90eab 3239 }
177c0ea7 3240
fca177d4
KL
3241 tty->TN_max_colors = tgetnum ("Co");
3242 tty->TN_max_pairs = tgetnum ("pa");
177c0ea7 3243
fca177d4
KL
3244 tty->TN_no_color_video = tgetnum ("NC");
3245 if (tty->TN_no_color_video == -1)
3246 tty->TN_no_color_video = 0;
a168702a 3247 }
a168702a 3248
fca177d4 3249 tty_default_color_capabilities (tty, 1);
ace28297 3250
6548cf00 3251 MagicWrap (tty) = tgetflag ("xn");
e4058338
KH
3252 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3253 the former flag imply the latter. */
6548cf00 3254 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
6ed8eeff 3255 terminal->memory_below_frame = tgetflag ("db");
fca177d4 3256 tty->TF_hazeltine = tgetflag ("hz");
6ed8eeff 3257 terminal->must_write_spaces = tgetflag ("in");
fca177d4
KL
3258 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
3259 tty->TF_insmode_motion = tgetflag ("mi");
3260 tty->TF_standout_motion = tgetflag ("ms");
3261 tty->TF_underscore = tgetflag ("ul");
3262 tty->TF_teleray = tgetflag ("xt");
08a24c47 3263
dce4c2ac
DN
3264#else /* DOS_NT */
3265#ifdef WINDOWSNT
3266 {
3267 struct frame *f = XFRAME (selected_frame);
3268
3269 initialize_w32_display (terminal);
3270
3271 FrameRows (tty) = FRAME_LINES (f);
3272 FrameCols (tty) = FRAME_COLS (f);
3273 tty->specified_window = FRAME_LINES (f);
3274
3275 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
3276 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
3277 terminal->char_ins_del_ok = 1;
3278 baud_rate = 19200;
3279 }
3280#else /* MSDOS */
3281 {
3282 int height, width;
3283 if (strcmp (terminal_type, "internal") == 0)
3284 terminal->type = output_msdos_raw;
3285 initialize_msdos_display (terminal);
3286
3287 get_tty_size (fileno (tty->input), &width, &height);
3288 FrameCols (tty) = width;
3289 FrameRows (tty) = height;
3290 terminal->char_ins_del_ok = 0;
3291 init_baud_rate (fileno (tty->input));
3292 }
3293#endif /* MSDOS */
3294 tty->output = stdout;
3295 tty->input = stdin;
3296 /* The following two are inaccessible from w32console.c. */
3297 terminal->delete_frame_hook = &tty_free_frame_resources;
3298 terminal->delete_terminal_hook = &delete_tty;
3299
3300 tty->name = xstrdup (name);
3301 terminal->name = xstrdup (name);
3302 tty->type = xstrdup (terminal_type);
3303
3304 add_keyboard_wait_descriptor (0);
3305
dce4c2ac
DN
3306 tty->delete_in_insert_mode = 1;
3307
3308 UseTabs (tty) = 0;
3309 terminal->scroll_region_ok = 0;
3310
3311 /* Seems to insert lines when it's not supposed to, messing up the
3312 display. In doing a trace, it didn't seem to be called much, so I
3313 don't think we're losing anything by turning it off. */
3314 terminal->line_ins_del_ok = 0;
3315
3316 tty->TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
3317#endif /* DOS_NT */
3318
6bc8cd65
JB
3319#ifdef HAVE_GPM
3320 terminal->mouse_position_hook = term_mouse_position;
3321 tty->mouse_highlight.mouse_face_window = Qnil;
3322#endif
3323
6ed8eeff
KL
3324 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
3325 init_kboard (terminal->kboard);
1344aad4 3326 KVAR (terminal->kboard, Vwindow_system) = Qnil;
6ed8eeff
KL
3327 terminal->kboard->next_kboard = all_kboards;
3328 all_kboards = terminal->kboard;
3329 terminal->kboard->reference_count++;
e7cf0fa0
KL
3330 /* Don't let the initial kboard remain current longer than necessary.
3331 That would cause problems if a file loaded on startup tries to
3332 prompt in the mini-buffer. */
3333 if (current_kboard == initial_kboard)
6ed8eeff 3334 current_kboard = terminal->kboard;
84704c5c 3335#ifndef DOS_NT
6ed8eeff 3336 term_get_fkeys (address, terminal->kboard);
5c2c7893 3337
ff11dfa1 3338 /* Get frame size from system, or else from termcap. */
3b12ce12
RS
3339 {
3340 int height, width;
0b0d3e0b 3341 get_tty_size (fileno (tty->input), &width, &height);
0a125897
KL
3342 FrameCols (tty) = width;
3343 FrameRows (tty) = height;
3b12ce12
RS
3344 }
3345
0a125897
KL
3346 if (FrameCols (tty) <= 0)
3347 FrameCols (tty) = tgetnum ("co");
3348 if (FrameRows (tty) <= 0)
3349 FrameRows (tty) = tgetnum ("li");
177c0ea7 3350
0a125897 3351 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
d31eee5e 3352 maybe_fatal (must_succeed, terminal,
b3ffc17c 3353 "Screen size %dx%d is too small",
3224dac1 3354 "Screen size %dx%d is too small",
0a125897 3355 FrameCols (tty), FrameRows (tty));
ee7a2de4 3356
6548cf00 3357 TabWidth (tty) = tgetnum ("tw");
08a24c47 3358
fca177d4
KL
3359 if (!tty->TS_bell)
3360 tty->TS_bell = "\07";
08a24c47 3361
fca177d4
KL
3362 if (!tty->TS_fwd_scroll)
3363 tty->TS_fwd_scroll = Down (tty);
08a24c47 3364
fca177d4 3365 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
08a24c47 3366
6548cf00
KL
3367 if (TabWidth (tty) < 0)
3368 TabWidth (tty) = 8;
177c0ea7 3369
08a24c47
JB
3370/* Turned off since /etc/termcap seems to have :ta= for most terminals
3371 and newer termcap doc does not seem to say there is a default.
6548cf00
KL
3372 if (!tty->Wcm->cm_tab)
3373 tty->Wcm->cm_tab = "\t";
08a24c47
JB
3374*/
3375
54800acb
MB
3376 /* We don't support standout modes that use `magic cookies', so
3377 turn off any that do. */
fca177d4 3378 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
54800acb 3379 {
fca177d4
KL
3380 tty->TS_standout_mode = 0;
3381 tty->TS_end_standout_mode = 0;
54800acb 3382 }
fca177d4 3383 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
54800acb 3384 {
fca177d4
KL
3385 tty->TS_enter_underline_mode = 0;
3386 tty->TS_exit_underline_mode = 0;
54800acb
MB
3387 }
3388
3389 /* If there's no standout mode, try to use underlining instead. */
fca177d4 3390 if (tty->TS_standout_mode == 0)
08a24c47 3391 {
fca177d4
KL
3392 tty->TS_standout_mode = tty->TS_enter_underline_mode;
3393 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
08a24c47
JB
3394 }
3395
afd359c4
RS
3396 /* If no `se' string, try using a `me' string instead.
3397 If that fails, we can't use standout mode at all. */
fca177d4 3398 if (tty->TS_end_standout_mode == 0)
afd359c4 3399 {
e4bfb3b6 3400 char *s = tgetstr ("me", address);
afd359c4 3401 if (s != 0)
fca177d4 3402 tty->TS_end_standout_mode = s;
afd359c4 3403 else
fca177d4 3404 tty->TS_standout_mode = 0;
afd359c4
RS
3405 }
3406
fca177d4 3407 if (tty->TF_teleray)
08a24c47 3408 {
6548cf00 3409 tty->Wcm->cm_tab = 0;
54800acb 3410 /* We can't support standout mode, because it uses magic cookies. */
fca177d4 3411 tty->TS_standout_mode = 0;
08a24c47 3412 /* But that means we cannot rely on ^M to go to column zero! */
6548cf00 3413 CR (tty) = 0;
08a24c47
JB
3414 /* LF can't be trusted either -- can alter hpos */
3415 /* if move at column 0 thru a line with TS_standout_mode */
6548cf00 3416 Down (tty) = 0;
08a24c47
JB
3417 }
3418
0a125897 3419 tty->specified_window = FrameRows (tty);
08a24c47 3420
6548cf00 3421 if (Wcm_init (tty) == -1) /* can't do cursor motion */
3224dac1 3422 {
d31eee5e 3423 maybe_fatal (must_succeed, terminal,
3224dac1 3424 "Terminal type \"%s\" is not powerful enough to run Emacs",
3224dac1 3425 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
37dad45a
RS
3426It lacks the ability to position the cursor.\n\
3427If that is not the actual type of terminal you have,\n\
3428use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
84164a0d
SM
3429`setenv TERM ...') to specify the correct type. It may be necessary\n"
3430# ifdef TERMINFO
3431"to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
37dad45a 3432# else /* TERMCAP */
84164a0d 3433"to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
37dad45a 3434# endif /* TERMINFO */
3224dac1 3435 terminal_type);
fca177d4 3436 }
daf01701 3437
0a125897 3438 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
d31eee5e 3439 maybe_fatal (must_succeed, terminal,
3224dac1
KL
3440 "Could not determine the frame size",
3441 "Could not determine the frame size");
08a24c47 3442
fca177d4
KL
3443 tty->delete_in_insert_mode
3444 = tty->TS_delete_mode && tty->TS_insert_mode
3445 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
08a24c47 3446
fca177d4
KL
3447 tty->se_is_so = (tty->TS_standout_mode
3448 && tty->TS_end_standout_mode
3449 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
08a24c47 3450
0b0d3e0b 3451 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
08a24c47 3452
6ed8eeff 3453 terminal->scroll_region_ok
6548cf00 3454 = (tty->Wcm->cm_abs
fca177d4 3455 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
08a24c47 3456
6ed8eeff 3457 terminal->line_ins_del_ok
fca177d4
KL
3458 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
3459 && (tty->TS_del_line || tty->TS_del_multi_lines))
6ed8eeff 3460 || (terminal->scroll_region_ok
fca177d4 3461 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
08a24c47 3462
6ed8eeff 3463 terminal->char_ins_del_ok
fca177d4
KL
3464 = ((tty->TS_ins_char || tty->TS_insert_mode
3465 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
3466 && (tty->TS_del_char || tty->TS_del_multi_chars));
08a24c47 3467
6ed8eeff 3468 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
08a24c47 3469
0b0d3e0b 3470 init_baud_rate (fileno (tty->input));
20a558dc 3471
84704c5c 3472#endif /* not DOS_NT */
635e3b29 3473
0a125897
KL
3474 /* Init system terminal modes (RAW or CBREAK, etc.). */
3475 init_sys_modes (tty);
daf01701 3476
6ed8eeff 3477 return terminal;
08a24c47
JB
3478}
3479
b3ffc17c
DN
3480
3481static void
3482vfatal (const char *str, va_list ap)
3483{
3484 fprintf (stderr, "emacs: ");
3485 vfprintf (stderr, str, ap);
3486 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
3487 fprintf (stderr, "\n");
b3ffc17c
DN
3488 fflush (stderr);
3489 exit (1);
3490}
3491
3492
a4c6993d 3493/* Auxiliary error-handling function for init_tty.
d31eee5e 3494 Delete TERMINAL, then call error or fatal with str1 or str2,
762b15be 3495 respectively, according to whether MUST_SUCCEED is zero or not. */
6b61353c 3496
3224dac1 3497static void
b3ffc17c
DN
3498maybe_fatal (int must_succeed, struct terminal *terminal,
3499 const char *str1, const char *str2, ...)
3224dac1 3500{
b3ffc17c
DN
3501 va_list ap;
3502 va_start (ap, str2);
6ed8eeff
KL
3503 if (terminal)
3504 delete_tty (terminal);
f4d953fc 3505
3224dac1 3506 if (must_succeed)
b3ffc17c 3507 vfatal (str2, ap);
3224dac1 3508 else
b3ffc17c 3509 verror (str1, ap);
3224dac1 3510
b3ffc17c 3511 va_end (ap);
3224dac1 3512 abort ();
08a24c47
JB
3513}
3514
dfcf069d 3515void
7c401d15 3516fatal (const char *str, ...)
08a24c47 3517{
7c401d15
DN
3518 va_list ap;
3519 va_start (ap, str);
b3ffc17c 3520 vfatal (str, ap);
7c401d15 3521 va_end (ap);
08a24c47 3522}
07c57952 3523
819b8f00
KL
3524\f
3525
6ed8eeff 3526/* Delete the given tty terminal, closing all frames on it. */
da8e1115 3527
ed8dad6b 3528static void
6ed8eeff 3529delete_tty (struct terminal *terminal)
072d84a6 3530{
428a555e 3531 struct tty_display_info *tty;
f4d953fc 3532
e2749141 3533 /* Protect against recursive calls. delete_frame in
ab797f65 3534 delete_terminal calls us back when it deletes our last frame. */
59a7bc63 3535 if (!terminal->name)
0a125897 3536 return;
fca177d4 3537
6ed8eeff 3538 if (terminal->type != output_termcap)
428a555e
KL
3539 abort ();
3540
6ed8eeff 3541 tty = terminal->display_info.tty;
f4d953fc 3542
fca177d4
KL
3543 if (tty == tty_list)
3544 tty_list = tty->next;
3545 else
3546 {
28d7d09f 3547 struct tty_display_info *p;
fca177d4
KL
3548 for (p = tty_list; p && p->next != tty; p = p->next)
3549 ;
3550
3551 if (! p)
3552 /* This should not happen. */
3553 abort ();
3554
0a125897
KL
3555 p->next = tty->next;
3556 tty->next = 0;
fca177d4
KL
3557 }
3558
7e59217d 3559 /* reset_sys_modes needs a valid device, so this call needs to be
6ed8eeff 3560 before delete_terminal. */
04c3243c 3561 reset_sys_modes (tty);
fca177d4 3562
6ed8eeff 3563 delete_terminal (terminal);
3224dac1 3564
70fdbb46
JM
3565 xfree (tty->name);
3566 xfree (tty->type);
daf01701 3567
fca177d4 3568 if (tty->input)
819b8f00
KL
3569 {
3570 delete_keyboard_wait_descriptor (fileno (tty->input));
3571 if (tty->input != stdin)
3572 fclose (tty->input);
3573 }
3574 if (tty->output && tty->output != stdout && tty->output != tty->input)
fca177d4
KL
3575 fclose (tty->output);
3576 if (tty->termscript)
3577 fclose (tty->termscript);
daf01701 3578
70fdbb46
JM
3579 xfree (tty->old_tty);
3580 xfree (tty->Wcm);
5f445726
JM
3581 xfree (tty->termcap_strings_buffer);
3582 xfree (tty->termcap_term_buffer);
daf01701 3583
72af86bd 3584 memset (tty, 0, sizeof (struct tty_display_info));
fca177d4 3585 xfree (tty);
fca177d4
KL
3586}
3587
428a555e
KL
3588\f
3589
28d7d09f 3590/* Mark the pointers in the tty_display_info objects.
0ba2624f 3591 Called by Fgarbage_collect. */
da8e1115 3592
fca177d4 3593void
ed8dad6b 3594mark_ttys (void)
fca177d4 3595{
28d7d09f 3596 struct tty_display_info *tty;
0c72d684 3597
819b8f00 3598 for (tty = tty_list; tty; tty = tty->next)
3b6fc48f 3599 mark_object (tty->top_frame);
072d84a6
RS
3600}
3601
428a555e
KL
3602\f
3603
dfcf069d 3604void
d3da34e0 3605syms_of_term (void)
07c57952 3606{
29208e82 3607 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo,
7ee72033 3608 doc: /* Non-nil means the system uses terminfo rather than termcap.
228299fa 3609This variable can be used by terminal emulator packages. */);
07c57952
KH
3610#ifdef TERMINFO
3611 system_uses_terminfo = 1;
3612#else
3613 system_uses_terminfo = 0;
3614#endif
c291d9ef 3615
29208e82 3616 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions,
e4019195 3617 doc: /* Functions to be run after suspending a tty.
fdc496e7 3618The functions are run with one argument, the terminal object to be suspended.
0b0d3e0b 3619See `suspend-tty'. */);
e4019195 3620 Vsuspend_tty_functions = Qnil;
0b0d3e0b
KL
3621
3622
29208e82 3623 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions,
e4019195 3624 doc: /* Functions to be run after resuming a tty.
fdc496e7 3625The functions are run with one argument, the terminal object that was revived.
0b0d3e0b 3626See `resume-tty'. */);
e4019195 3627 Vresume_tty_functions = Qnil;
a168702a 3628
29208e82 3629 DEFVAR_BOOL ("visible-cursor", visible_cursor,
0db017c0
SM
3630 doc: /* Non-nil means to make the cursor very visible.
3631This only has an effect when running in a text terminal.
3632What means \"very visible\" is up to your terminal. It may make the cursor
3633bigger, or it may make it blink, or it may do nothing at all. */);
3634 visible_cursor = 1;
3635
a168702a 3636 defsubr (&Stty_display_color_p);
bfa62f96 3637 defsubr (&Stty_display_color_cells);
072d84a6 3638 defsubr (&Stty_no_underline);
6ed8eeff
KL
3639 defsubr (&Stty_type);
3640 defsubr (&Scontrolling_tty_p);
0b0d3e0b
KL
3641 defsubr (&Ssuspend_tty);
3642 defsubr (&Sresume_tty);
7e5a23bd 3643#ifdef HAVE_GPM
6178ce5e
SM
3644 defsubr (&Sgpm_mouse_start);
3645 defsubr (&Sgpm_mouse_stop);
7e5a23bd 3646#endif /* HAVE_GPM */
3a7c5d40 3647
84704c5c 3648#ifndef DOS_NT
3a7c5d40
CY
3649 default_orig_pair = NULL;
3650 default_set_foreground = NULL;
3651 default_set_background = NULL;
84704c5c 3652#endif /* !DOS_NT */
d31eee5e
CY
3653
3654 encode_terminal_src = NULL;
3655 encode_terminal_dst = NULL;
07c57952 3656}