Fix doc-string of pop-to-buffer-same-window. (Bug#15492)
[bpt/emacs.git] / src / term.c
CommitLineData
d284f58f 1/* Terminal control module for terminals described by TERMCAP
ab422c4d
PE
2 Copyright (C) 1985-1987, 1993-1995, 1998, 2000-2013 Free Software
3 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>
59b3194c 23#include <errno.h>
49cdacda
PE
24#include <fcntl.h>
25#include <stdio.h>
28d440ab 26#include <sys/file.h>
d35af63c 27#include <sys/time.h>
1ec5dc77 28#include <unistd.h>
0c72d684 29
9628b887 30#include "lisp.h"
08a24c47 31#include "termchar.h"
50938595 32#include "tparam.h"
9332ea03 33#include "character.h"
e5560ff7 34#include "buffer.h"
a4decb7f
KH
35#include "charset.h"
36#include "coding.h"
4c12d738 37#include "composite.h"
2538fae4 38#include "keyboard.h"
ff11dfa1 39#include "frame.h"
08a24c47
JB
40#include "disptab.h"
41#include "termhooks.h"
dfcf069d 42#include "dispextern.h"
a168702a 43#include "window.h"
8feddab4 44#include "keymap.h"
1a935bfd 45#include "blockinput.h"
03a1d6bd
KL
46#include "syssignal.h"
47#include "systty.h"
c9612b8e 48#include "intervals.h"
84704c5c
EZ
49#ifdef MSDOS
50#include "msdos.h"
51static int been_here = -1;
52#endif
a168702a 53
c7a7f318
EZ
54#ifdef USE_X_TOOLKIT
55#include "../lwlib/lwlib.h"
56#endif
57
eccec691 58#include "cm.h"
dfcf069d
AS
59#ifdef HAVE_X_WINDOWS
60#include "xterm.h"
61#endif
c8951b18 62
b5e9cbb6 63#include "menu.h"
6b61353c 64
78048085
EZ
65/* The name of the default console device. */
66#ifdef WINDOWSNT
67#define DEV_TTY "CONOUT$"
a68089e4 68#include "w32term.h"
78048085
EZ
69#else
70#define DEV_TTY "/dev/tty"
71#endif
a168702a 72
f57e2426
J
73static void tty_set_scroll_region (struct frame *f, int start, int stop);
74static void turn_on_face (struct frame *, int face_id);
75static void turn_off_face (struct frame *, int face_id);
64520e5c 76static void tty_turn_off_highlight (struct tty_display_info *);
f57e2426
J
77static void tty_show_cursor (struct tty_display_info *);
78static void tty_hide_cursor (struct tty_display_info *);
79static void tty_background_highlight (struct tty_display_info *tty);
653d4f43 80static struct terminal *get_tty_terminal (Lisp_Object, bool);
f57e2426
J
81static void clear_tty_hooks (struct terminal *terminal);
82static void set_tty_hooks (struct terminal *terminal);
83static void dissociate_if_controlling_tty (int fd);
84static void delete_tty (struct terminal *);
653d4f43
PE
85static _Noreturn void maybe_fatal (bool, struct terminal *,
86 const char *, const char *, ...)
845ca893
PE
87 ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
88static _Noreturn void vfatal (const char *str, va_list ap)
89 ATTRIBUTE_FORMAT_PRINTF (1, 0);
b3ffc17c 90
a168702a 91
6548cf00
KL
92#define OUTPUT(tty, a) \
93 emacs_tputs ((tty), a, \
653d4f43 94 FRAME_LINES (XFRAME (selected_frame)) - curY (tty), \
6548cf00
KL
95 cmputc)
96
28d440ab
KL
97#define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
98#define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
a168702a 99
28d440ab 100#define OUTPUT_IF(tty, a) \
6548cf00
KL
101 do { \
102 if (a) \
b286858c 103 OUTPUT (tty, a); \
6548cf00 104 } while (0)
177c0ea7 105
28d440ab 106#define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
c291d9ef 107
f46c2aff 108/* Display space properties */
08a24c47 109
428a555e 110/* Chain of all tty device parameters. */
28d7d09f 111struct tty_display_info *tty_list;
08a24c47 112
4e6ba4a4
GM
113/* Meaning of bits in no_color_video. Each bit set means that the
114 corresponding attribute cannot be combined with colors. */
115
116enum no_color_bit
117{
118 NC_STANDOUT = 1 << 0,
119 NC_UNDERLINE = 1 << 1,
120 NC_REVERSE = 1 << 2,
cd4eb164 121 NC_ITALIC = 1 << 3,
4e6ba4a4
GM
122 NC_DIM = 1 << 4,
123 NC_BOLD = 1 << 5,
124 NC_INVIS = 1 << 6,
cd4eb164 125 NC_PROTECT = 1 << 7
4e6ba4a4
GM
126};
127
08a24c47
JB
128/* internal state */
129
8dd0c7cb 130/* The largest frame width in any call to calculate_costs. */
a168702a 131
64520e5c 132static int max_frame_cols;
a168702a 133
08a24c47 134\f
cb28b9c2 135
7e5a23bd 136#ifdef HAVE_GPM
e882229c 137#include <sys/fcntl.h>
e882229c 138
71f44e7a
SM
139/* The device for which we have enabled gpm support (or NULL). */
140struct tty_display_info *gpm_tty = NULL;
e882229c 141
cf482c50 142/* Last recorded mouse coordinates. */
e882229c 143static int last_mouse_x, last_mouse_y;
7e5a23bd 144#endif /* HAVE_GPM */
e882229c 145
da8e1115 146/* Ring the bell on a tty. */
c291d9ef 147
ed8dad6b 148static void
385ed61f 149tty_ring_bell (struct frame *f)
3224dac1 150{
3224dac1 151 struct tty_display_info *tty = FRAME_TTY (f);
c291d9ef 152
b6660415
KL
153 if (tty->output)
154 {
155 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
156 ? tty->TS_visible_bell
157 : tty->TS_bell));
158 fflush (tty->output);
08a24c47 159 }
08a24c47
JB
160}
161
da8e1115
KL
162/* Set up termcap modes for Emacs. */
163
64520e5c 164static void
6ed8eeff 165tty_set_terminal_modes (struct terminal *terminal)
08a24c47 166{
6ed8eeff 167 struct tty_display_info *tty = terminal->display_info.tty;
f4d953fc 168
0b0d3e0b 169 if (tty->output)
08a24c47 170 {
fbf34973
KL
171 if (tty->TS_termcap_modes)
172 OUTPUT (tty, tty->TS_termcap_modes);
3ae9c96a 173 else
fbf34973
KL
174 {
175 /* Output enough newlines to scroll all the old screen contents
176 off the screen, so it won't be overwritten and lost. */
177 int i;
a3c07f68 178 current_tty = tty;
fbf34973 179 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
a3c07f68 180 cmputc ('\n');
fbf34973
KL
181 }
182
b58cb614 183 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
0b0d3e0b
KL
184 OUTPUT_IF (tty, tty->TS_keypad_mode);
185 losecursor (tty);
2f98e6e3 186 fflush (tty->output);
08a24c47 187 }
08a24c47
JB
188}
189
da8e1115
KL
190/* Reset termcap modes before exiting Emacs. */
191
64520e5c 192static void
6ed8eeff 193tty_reset_terminal_modes (struct terminal *terminal)
08a24c47 194{
6ed8eeff 195 struct tty_display_info *tty = terminal->display_info.tty;
0b0d3e0b
KL
196
197 if (tty->output)
08a24c47 198 {
ed8dad6b
KL
199 tty_turn_off_highlight (tty);
200 tty_turn_off_insert (tty);
0b0d3e0b
KL
201 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
202 OUTPUT_IF (tty, tty->TS_cursor_normal);
203 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
204 OUTPUT_IF (tty, tty->TS_orig_pair);
d284f58f 205 /* Output raw CR so kernel can track the cursor hpos. */
0b0d3e0b 206 current_tty = tty;
d284f58f 207 cmputc ('\r');
2f98e6e3 208 fflush (tty->output);
08a24c47 209 }
08a24c47
JB
210}
211
6ed8eeff 212/* Flag the end of a display update on a termcap terminal. */
08a24c47 213
ed8dad6b 214static void
3224dac1 215tty_update_end (struct frame *f)
08a24c47 216{
3224dac1 217 struct tty_display_info *tty = FRAME_TTY (f);
177c0ea7 218
3224dac1
KL
219 if (!XWINDOW (selected_window)->cursor_off_p)
220 tty_show_cursor (tty);
ed8dad6b
KL
221 tty_turn_off_insert (tty);
222 tty_background_highlight (tty);
08a24c47
JB
223}
224
da8e1115
KL
225/* The implementation of set_terminal_window for termcap frames. */
226
ed8dad6b 227static void
385ed61f 228tty_set_terminal_window (struct frame *f, int size)
08a24c47 229{
3224dac1
KL
230 struct tty_display_info *tty = FRAME_TTY (f);
231
232 tty->specified_window = size ? size : FRAME_LINES (f);
233 if (FRAME_SCROLL_REGION_OK (f))
ed8dad6b 234 tty_set_scroll_region (f, 0, tty->specified_window);
08a24c47
JB
235}
236
ed8dad6b
KL
237static void
238tty_set_scroll_region (struct frame *f, int start, int stop)
08a24c47
JB
239{
240 char *buf;
28d7d09f 241 struct tty_display_info *tty = FRAME_TTY (f);
177c0ea7 242
fca177d4 243 if (tty->TS_set_scroll_region)
50938595 244 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1, 0, 0);
fca177d4
KL
245 else if (tty->TS_set_scroll_region_1)
246 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
ed02974b
KL
247 FRAME_LINES (f), start,
248 FRAME_LINES (f) - stop,
249 FRAME_LINES (f));
08a24c47 250 else
fca177d4 251 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
177c0ea7 252
6548cf00 253 OUTPUT (tty, buf);
9ac0d9e0 254 xfree (buf);
6548cf00 255 losecursor (tty);
08a24c47 256}
d284f58f 257
08a24c47 258\f
d284f58f 259static void
ed8dad6b 260tty_turn_on_insert (struct tty_display_info *tty)
08a24c47 261{
fca177d4
KL
262 if (!tty->insert_mode)
263 OUTPUT (tty, tty->TS_insert_mode);
264 tty->insert_mode = 1;
08a24c47
JB
265}
266
dfcf069d 267void
ed8dad6b 268tty_turn_off_insert (struct tty_display_info *tty)
08a24c47 269{
fca177d4
KL
270 if (tty->insert_mode)
271 OUTPUT (tty, tty->TS_end_insert_mode);
272 tty->insert_mode = 0;
08a24c47
JB
273}
274\f
54800acb 275/* Handle highlighting. */
08a24c47 276
64520e5c 277static void
ed8dad6b 278tty_turn_off_highlight (struct tty_display_info *tty)
08a24c47 279{
fca177d4
KL
280 if (tty->standout_mode)
281 OUTPUT_IF (tty, tty->TS_end_standout_mode);
282 tty->standout_mode = 0;
08a24c47
JB
283}
284
d284f58f 285static void
ed8dad6b 286tty_turn_on_highlight (struct tty_display_info *tty)
08a24c47 287{
fca177d4
KL
288 if (!tty->standout_mode)
289 OUTPUT_IF (tty, tty->TS_standout_mode);
290 tty->standout_mode = 1;
08a24c47
JB
291}
292
86a7d192 293static void
ed8dad6b 294tty_toggle_highlight (struct tty_display_info *tty)
86a7d192 295{
fca177d4 296 if (tty->standout_mode)
ed8dad6b 297 tty_turn_off_highlight (tty);
86a7d192 298 else
ed8dad6b 299 tty_turn_on_highlight (tty);
86a7d192
GM
300}
301
a168702a
GM
302
303/* Make cursor invisible. */
304
305static void
28d7d09f 306tty_hide_cursor (struct tty_display_info *tty)
a168702a 307{
fca177d4 308 if (tty->cursor_hidden == 0)
d284f58f 309 {
fca177d4 310 tty->cursor_hidden = 1;
28a16449
EZ
311#ifdef WINDOWSNT
312 w32con_hide_cursor ();
313#else
fca177d4 314 OUTPUT_IF (tty, tty->TS_cursor_invisible);
28a16449 315#endif
d284f58f 316 }
a168702a
GM
317}
318
319
320/* Ensure that cursor is visible. */
321
322static void
28d7d09f 323tty_show_cursor (struct tty_display_info *tty)
a168702a 324{
fca177d4 325 if (tty->cursor_hidden)
d284f58f 326 {
fca177d4 327 tty->cursor_hidden = 0;
28a16449
EZ
328#ifdef WINDOWSNT
329 w32con_show_cursor ();
330#else
fca177d4 331 OUTPUT_IF (tty, tty->TS_cursor_normal);
0db017c0 332 if (visible_cursor)
b58cb614 333 OUTPUT_IF (tty, tty->TS_cursor_visible);
28a16449 334#endif
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;
653d4f43 711 int 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 {
4d7e6e51 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);
4d7e6e51 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 {
4d7e6e51 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);
4d7e6e51 825 unblock_input ();
e882229c
NR
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 {
4d7e6e51 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);
4d7e6e51 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 968
71376d4b
PE
969 int i = eabs (n);
970 char *buf;
08a24c47 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 1334 if (!KEYMAPP (KVAR (kboard, Vinput_decode_map)))
15dbb4d6 1335 kset_input_decode_map (kboard, Fmake_sparse_keymap (Qnil));
3e65092f 1336
cbae07d5 1337 for (i = 0; i < (sizeof (keys) / sizeof (keys[0])); i++)
5c2c7893
JB
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 *);
653d4f43 1447static void produce_glyphless_glyph (struct it *, 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
a54e2c05 1461 eassert (it->glyph_row);
a168702a
GM
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 1500 if ((it->bidi_it.type & 7) != it->bidi_it.type)
1088b922 1501 emacs_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. */
a54e2c05 1548 eassert (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 {
653d4f43 1567 produce_glyphless_glyph (it, Qnil);
b18fad6d
KH
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
a54e2c05 1635 eassert (it->what == IT_GLYPHLESS);
653d4f43 1636 produce_glyphless_glyph (it, acronym);
b18fad6d 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
a54e2c05 1659 eassert (it->glyph_row);
4c12d738
KH
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)
1088b922 1698 emacs_abort ();
93d68d0c
EZ
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
a54e2c05 1751 eassert (it->glyph_row);
b18fad6d
KH
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)
1088b922 1783 emacs_abort ();
b18fad6d
KH
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 1809
653d4f43 1810 ACRONYM, if non-nil, is an acronym string for the character.
b18fad6d
KH
1811
1812 The glyphs actually produced are of type CHAR_GLYPH. */
1813
1814static void
653d4f43 1815produce_glyphless_glyph (struct it *it, Lisp_Object acronym)
b18fad6d 1816{
77394d40 1817 int len, face_id = merge_glyphless_glyph_face (it);
80f2e268 1818 char buf[sizeof "\\x" + max (6, (sizeof it->c * CHAR_BIT + 3) / 4)];
fbceeba2 1819 char const *str = " ";
b18fad6d 1820
b18fad6d
KH
1821 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
1822 {
0eb025fb
EZ
1823 /* As there's no way to produce a thin space, we produce a space
1824 of canonical width. */
b18fad6d
KH
1825 len = 1;
1826 }
1827 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
1828 {
1829 len = CHAR_WIDTH (it->c);
1830 if (len == 0)
1831 len = 1;
0eb025fb 1832 else if (len > 4)
b18fad6d 1833 len = 4;
99027bdd 1834 len = sprintf (buf, "[%.*s]", len, str);
0eb025fb 1835 str = buf;
b18fad6d
KH
1836 }
1837 else
1838 {
1839 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
1840 {
b18fad6d
KH
1841 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
1842 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
16a43933
CY
1843 if (CONSP (acronym))
1844 acronym = XCDR (acronym);
b18fad6d 1845 buf[0] = '[';
51b59d79 1846 str = STRINGP (acronym) ? SSDATA (acronym) : "";
b18fad6d
KH
1847 for (len = 0; len < 6 && str[len] && ASCII_BYTE_P (str[len]); len++)
1848 buf[1 + len] = str[len];
1849 buf[1 + len] = ']';
1850 len += 2;
1851 }
1852 else
1853 {
a54e2c05 1854 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
7c2d713b
EZ
1855 len = (it->c < 0x10000 ? sprintf (buf, "\\u%04X", it->c)
1856 : it->c <= MAX_UNICODE_CHAR ? sprintf (buf, "\\U%06X", it->c)
1857 : sprintf (buf, "\\x%06X", it->c));
b18fad6d
KH
1858 }
1859 str = buf;
1860 }
1861
1862 it->pixel_width = len;
1863 it->nglyphs = len;
d284567f 1864 if (it->glyph_row)
b18fad6d
KH
1865 append_glyphless_glyph (it, face_id, str);
1866}
1867
a168702a
GM
1868\f
1869/***********************************************************************
1870 Faces
1871 ***********************************************************************/
1872
4e6ba4a4
GM
1873/* Value is non-zero if attribute ATTR may be used. ATTR should be
1874 one of the enumerators from enum no_color_bit, or a bit set built
1875 from them. Some display attributes may not be used together with
1876 color; the termcap capability `NC' specifies which ones. */
1877
fca177d4
KL
1878#define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1879 (tty->TN_max_colors > 0 \
1880 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1881 : 1)
a168702a 1882
072d84a6
RS
1883/* Turn appearances of face FACE_ID on tty frame F on.
1884 FACE_ID is a realized face ID number, in the face cache. */
a168702a
GM
1885
1886static void
d3da34e0 1887turn_on_face (struct frame *f, int face_id)
a168702a
GM
1888{
1889 struct face *face = FACE_FROM_ID (f, face_id);
86a7d192
GM
1890 long fg = face->foreground;
1891 long bg = face->background;
28d7d09f 1892 struct tty_display_info *tty = FRAME_TTY (f);
a168702a 1893
86a7d192
GM
1894 /* Do this first because TS_end_standout_mode may be the same
1895 as TS_exit_attribute_mode, which turns all appearances off. */
fca177d4 1896 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
86a7d192 1897 {
fca177d4 1898 if (tty->TN_max_colors > 0)
86a7d192
GM
1899 {
1900 if (fg >= 0 && bg >= 0)
1901 {
1902 /* If the terminal supports colors, we can set them
1903 below without using reverse video. The face's fg
1904 and bg colors are set as they should appear on
1905 the screen, i.e. they take the inverse-video'ness
1906 of the face already into account. */
1907 }
1908 else if (inverse_video)
1909 {
1910 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1911 || bg == FACE_TTY_DEFAULT_BG_COLOR)
ed8dad6b 1912 tty_toggle_highlight (tty);
86a7d192
GM
1913 }
1914 else
1915 {
1916 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1917 || bg == FACE_TTY_DEFAULT_FG_COLOR)
ed8dad6b 1918 tty_toggle_highlight (tty);
86a7d192
GM
1919 }
1920 }
1921 else
1922 {
1923 /* If we can't display colors, use reverse video
1924 if the face specifies that. */
37526b42
GM
1925 if (inverse_video)
1926 {
1927 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1928 || bg == FACE_TTY_DEFAULT_BG_COLOR)
ed8dad6b 1929 tty_toggle_highlight (tty);
37526b42
GM
1930 }
1931 else
1932 {
1933 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1934 || bg == FACE_TTY_DEFAULT_FG_COLOR)
ed8dad6b 1935 tty_toggle_highlight (tty);
37526b42 1936 }
86a7d192
GM
1937 }
1938 }
a168702a 1939
4189ed40
CY
1940 if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1941 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
1942
cd4eb164
CY
1943 if (face->tty_italic_p && MAY_USE_WITH_COLORS_P (tty, NC_ITALIC))
1944 {
1945 if (tty->TS_enter_italic_mode)
1946 OUTPUT1 (tty, tty->TS_enter_italic_mode);
1947 else
1948 /* Italics mode is unavailable on many terminals. In that
1949 case, map slant to dimmed text; we want italic text to
1950 appear different and dimming is not otherwise used. */
1951 OUTPUT1 (tty, tty->TS_enter_dim_mode);
1952 }
a168702a 1953
fca177d4
KL
1954 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
1955 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
a168702a 1956
fca177d4 1957 if (tty->TN_max_colors > 0)
a168702a 1958 {
fbceeba2
PE
1959 const char *ts;
1960 char *p;
177c0ea7 1961
fbf34973 1962 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
753d161b 1963 if (fg >= 0 && ts)
a168702a 1964 {
653d4f43 1965 p = tparam (ts, NULL, 0, fg, 0, 0, 0);
6548cf00 1966 OUTPUT (tty, p);
a168702a
GM
1967 xfree (p);
1968 }
1969
fbf34973 1970 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
753d161b 1971 if (bg >= 0 && ts)
a168702a 1972 {
653d4f43 1973 p = tparam (ts, NULL, 0, bg, 0, 0, 0);
6548cf00 1974 OUTPUT (tty, p);
a168702a
GM
1975 xfree (p);
1976 }
1977 }
1978}
177c0ea7 1979
a168702a
GM
1980
1981/* Turn off appearances of face FACE_ID on tty frame F. */
1982
1983static void
d3da34e0 1984turn_off_face (struct frame *f, int face_id)
a168702a
GM
1985{
1986 struct face *face = FACE_FROM_ID (f, face_id);
28d7d09f 1987 struct tty_display_info *tty = FRAME_TTY (f);
a168702a 1988
a54e2c05 1989 eassert (face != NULL);
a168702a 1990
fca177d4 1991 if (tty->TS_exit_attribute_mode)
a168702a
GM
1992 {
1993 /* Capability "me" will turn off appearance modes double-bright,
1994 half-bright, reverse-video, standout, underline. It may or
1995 may not turn off alt-char-mode. */
1996 if (face->tty_bold_p
cd4eb164 1997 || face->tty_italic_p
a168702a 1998 || face->tty_reverse_p
a168702a 1999 || face->tty_underline_p)
65aa5e85 2000 {
fca177d4
KL
2001 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
2002 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
2003 tty->standout_mode = 0;
65aa5e85 2004 }
a168702a
GM
2005 }
2006 else
2007 {
2008 /* If we don't have "me" we can only have those appearances
2009 that have exit sequences defined. */
54800acb 2010 if (face->tty_underline_p)
fca177d4 2011 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
a168702a
GM
2012 }
2013
2014 /* Switch back to default colors. */
fca177d4 2015 if (tty->TN_max_colors > 0
f9d2fdc4
EZ
2016 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2017 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2018 || (face->background != FACE_TTY_DEFAULT_COLOR
2019 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
fca177d4 2020 OUTPUT1_IF (tty, tty->TS_orig_pair);
a168702a 2021}
177c0ea7
JB
2022
2023
653d4f43 2024/* Return true if the terminal on frame F supports all of the
b63a55ac
MB
2025 capabilities in CAPS simultaneously, with foreground and background
2026 colors FG and BG. */
2027
653d4f43 2028bool
d3da34e0
JB
2029tty_capable_p (struct tty_display_info *tty, unsigned int caps,
2030 unsigned long fg, unsigned long bg)
b63a55ac 2031{
fca177d4
KL
2032#define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2033 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
b63a55ac
MB
2034 return 0;
2035
fca177d4
KL
2036 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2037 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2038 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2039 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
cd4eb164 2040 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ITALIC, tty->TS_enter_italic_mode, NC_ITALIC);
b63a55ac
MB
2041
2042 /* We can do it! */
2043 return 1;
2044}
2045
a168702a
GM
2046/* Return non-zero if the terminal is capable to display colors. */
2047
2048DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
981e4297 2049 0, 1, 0,
6ed8eeff
KL
2050 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2051
401e9e57
CY
2052TERMINAL can be a terminal object, a frame, or nil (meaning the
2053selected frame's terminal). This function always returns nil if
c6bf3022 2054TERMINAL does not refer to a text terminal. */)
5842a27b 2055 (Lisp_Object terminal)
a168702a 2056{
717a00ef 2057 struct terminal *t = get_tty_terminal (terminal, 0);
6ed8eeff 2058 if (!t)
428a555e 2059 return Qnil;
3224dac1 2060 else
6ed8eeff 2061 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
a168702a
GM
2062}
2063
bfa62f96
EZ
2064/* Return the number of supported colors. */
2065DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2066 Stty_display_color_cells, 0, 1, 0,
6ed8eeff
KL
2067 doc: /* Return the number of colors supported by the tty device TERMINAL.
2068
401e9e57
CY
2069TERMINAL can be a terminal object, a frame, or nil (meaning the
2070selected frame's terminal). This function always returns 0 if
c6bf3022 2071TERMINAL does not refer to a text terminal. */)
5842a27b 2072 (Lisp_Object terminal)
bfa62f96 2073{
717a00ef 2074 struct terminal *t = get_tty_terminal (terminal, 0);
6ed8eeff 2075 if (!t)
8c8d5f35 2076 return make_number (0);
3224dac1 2077 else
6ed8eeff 2078 return make_number (t->display_info.tty->TN_max_colors);
bfa62f96
EZ
2079}
2080
84704c5c 2081#ifndef DOS_NT
a168702a 2082
5205ee62
GM
2083/* Declare here rather than in the function, as in the rest of Emacs,
2084 to work around an HPUX compiler bug (?). See
57407fb4 2085 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
5205ee62
GM
2086static int default_max_colors;
2087static int default_max_pairs;
2088static int default_no_color_video;
2089static char *default_orig_pair;
2090static char *default_set_foreground;
2091static char *default_set_background;
fcf8ff2e 2092
ace28297
EZ
2093/* Save or restore the default color-related capabilities of this
2094 terminal. */
2095static void
653d4f43 2096tty_default_color_capabilities (struct tty_display_info *tty, bool save)
ace28297 2097{
ace28297
EZ
2098
2099 if (save)
2100 {
70fdbb46 2101 xfree (default_orig_pair);
fca177d4 2102 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
ace28297 2103
70fdbb46 2104 xfree (default_set_foreground);
fca177d4 2105 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
ace28297
EZ
2106 : NULL;
2107
70fdbb46 2108 xfree (default_set_background);
fca177d4 2109 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
ace28297
EZ
2110 : NULL;
2111
fca177d4
KL
2112 default_max_colors = tty->TN_max_colors;
2113 default_max_pairs = tty->TN_max_pairs;
2114 default_no_color_video = tty->TN_no_color_video;
ace28297
EZ
2115 }
2116 else
2117 {
fca177d4
KL
2118 tty->TS_orig_pair = default_orig_pair;
2119 tty->TS_set_foreground = default_set_foreground;
2120 tty->TS_set_background = default_set_background;
2121 tty->TN_max_colors = default_max_colors;
2122 tty->TN_max_pairs = default_max_pairs;
2123 tty->TN_no_color_video = default_no_color_video;
ace28297
EZ
2124 }
2125}
2126
2127/* Setup one of the standard tty color schemes according to MODE.
2128 MODE's value is generally the number of colors which we want to
2129 support; zero means set up for the default capabilities, the ones
a4c6993d 2130 we saw at init_tty time; -1 means turn off color support. */
ed8dad6b 2131static void
28d7d09f 2132tty_setup_colors (struct tty_display_info *tty, int mode)
ace28297 2133{
6b61353c
KH
2134 /* Canonicalize all negative values of MODE. */
2135 if (mode < -1)
2136 mode = -1;
2137
ace28297
EZ
2138 switch (mode)
2139 {
2140 case -1: /* no colors at all */
fca177d4
KL
2141 tty->TN_max_colors = 0;
2142 tty->TN_max_pairs = 0;
2143 tty->TN_no_color_video = 0;
2144 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
ace28297
EZ
2145 break;
2146 case 0: /* default colors, if any */
2147 default:
fca177d4 2148 tty_default_color_capabilities (tty, 0);
ace28297
EZ
2149 break;
2150 case 8: /* 8 standard ANSI colors */
fca177d4 2151 tty->TS_orig_pair = "\033[0m";
ace28297 2152#ifdef TERMINFO
fca177d4
KL
2153 tty->TS_set_foreground = "\033[3%p1%dm";
2154 tty->TS_set_background = "\033[4%p1%dm";
ace28297 2155#else
fca177d4
KL
2156 tty->TS_set_foreground = "\033[3%dm";
2157 tty->TS_set_background = "\033[4%dm";
ace28297 2158#endif
fca177d4
KL
2159 tty->TN_max_colors = 8;
2160 tty->TN_max_pairs = 64;
2161 tty->TN_no_color_video = 0;
ace28297
EZ
2162 break;
2163 }
2164}
2165
2166void
d3da34e0 2167set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
ace28297 2168{
ce5b453a 2169 Lisp_Object tem, val;
9b2cd403
SM
2170 Lisp_Object color_mode;
2171 int mode;
9b2cd403
SM
2172 Lisp_Object tty_color_mode_alist
2173 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
ace28297 2174
e69b0960 2175 tem = assq_no_quit (Qtty_color_mode, f->param_alist);
9b2cd403 2176 val = CONSP (tem) ? XCDR (tem) : Qnil;
ace28297 2177
6b61353c 2178 if (INTEGERP (val))
ace28297 2179 color_mode = val;
ce5b453a 2180 else if (SYMBOLP (tty_color_mode_alist))
ace28297 2181 {
ce5b453a 2182 tem = Fassq (val, Fsymbol_value (tty_color_mode_alist));
9b2cd403 2183 color_mode = CONSP (tem) ? XCDR (tem) : Qnil;
ace28297 2184 }
ce5b453a
SM
2185 else
2186 color_mode = Qnil;
6b61353c 2187
d311d28c 2188 mode = TYPE_RANGED_INTEGERP (int, color_mode) ? XINT (color_mode) : 0;
ace28297 2189
9b2cd403 2190 if (mode != tty->previous_color_mode)
ace28297 2191 {
9b2cd403
SM
2192 tty->previous_color_mode = mode;
2193 tty_setup_colors (tty , mode);
2194 /* This recomputes all the faces given the new color definitions. */
6cd7a139 2195 safe_call (1, intern ("tty-set-up-initial-frame-faces"));
ace28297
EZ
2196 }
2197}
2198
84704c5c 2199#endif /* !DOS_NT */
a168702a
GM
2200
2201\f
28d440ab 2202
6ed8eeff 2203/* Return the tty display object specified by TERMINAL. */
b6660415 2204
64520e5c 2205static struct terminal *
653d4f43 2206get_tty_terminal (Lisp_Object terminal, bool throw)
b6660415 2207{
717a00ef 2208 struct terminal *t = get_terminal (terminal, throw);
62af879c 2209
84704c5c 2210 if (t && t->type != output_termcap && t->type != output_msdos_raw)
717a00ef
KL
2211 {
2212 if (throw)
2213 error ("Device %d is not a termcap terminal device", t->id);
2214 else
2215 return NULL;
2216 }
b6660415 2217
6ed8eeff 2218 return t;
b6660415
KL
2219}
2220
ab797f65
KL
2221/* Return an active termcap device that uses the tty device with the
2222 given name.
b6660415 2223
7e59217d 2224 This function ignores suspended devices.
da8e1115
KL
2225
2226 Returns NULL if the named terminal device is not opened. */
f4d953fc 2227
6ed8eeff 2228struct terminal *
8ea90aa3 2229get_named_tty (const char *name)
28d440ab 2230{
6ed8eeff
KL
2231 struct terminal *t;
2232
9c253307 2233 eassert (name);
ab797f65
KL
2234
2235 for (t = terminal_list; t; t = t->next_terminal)
2236 {
5cc67f65 2237 if ((t->type == output_termcap || t->type == output_msdos_raw)
ab797f65
KL
2238 && !strcmp (t->display_info.tty->name, name)
2239 && TERMINAL_ACTIVE_P (t))
2240 return t;
2241 }
28d440ab
KL
2242
2243 return 0;
2244}
2245
2246\f
a7ca3326 2247DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
6ed8eeff 2248 doc: /* Return the type of the tty device that TERMINAL uses.
ab797f65 2249Returns nil if TERMINAL is not on a tty device.
a3fbb897 2250
401e9e57
CY
2251TERMINAL can be a terminal object, a frame, or nil (meaning the
2252selected frame's terminal). */)
5842a27b 2253 (Lisp_Object terminal)
b6660415 2254{
6ed8eeff 2255 struct terminal *t = get_terminal (terminal, 1);
819b8f00 2256
84704c5c 2257 if (t->type != output_termcap && t->type != output_msdos_raw)
ab797f65
KL
2258 return Qnil;
2259
6ed8eeff
KL
2260 if (t->display_info.tty->type)
2261 return build_string (t->display_info.tty->type);
819b8f00
KL
2262 else
2263 return Qnil;
2264}
2265
6ed8eeff 2266DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
84704c5c 2267 doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
a3fbb897 2268
401e9e57
CY
2269TERMINAL can be a terminal object, a frame, or nil (meaning the
2270selected frame's terminal). This function always returns nil if
2271TERMINAL is not on a tty device. */)
5842a27b 2272 (Lisp_Object terminal)
4a933ef8 2273{
6ed8eeff 2274 struct terminal *t = get_terminal (terminal, 1);
4a933ef8 2275
84704c5c
EZ
2276 if ((t->type != output_termcap && t->type != output_msdos_raw)
2277 || strcmp (t->display_info.tty->name, DEV_TTY) != 0)
4a933ef8
KL
2278 return Qnil;
2279 else
2280 return Qt;
2281}
2282
a3fbb897 2283DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
6ed8eeff 2284 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
a3fbb897
KL
2285This is used to override the terminfo data, for certain terminals that
2286do not really do underlining, but say that they do. This function has
6ed8eeff 2287no effect if used on a non-tty terminal.
a3fbb897 2288
401e9e57
CY
2289TERMINAL can be a terminal object, a frame or nil (meaning the
2290selected frame's terminal). This function always returns nil if
c6bf3022 2291TERMINAL does not refer to a text terminal. */)
5842a27b 2292 (Lisp_Object terminal)
a3fbb897 2293{
6ed8eeff 2294 struct terminal *t = get_terminal (terminal, 1);
a3fbb897 2295
6ed8eeff
KL
2296 if (t->type == output_termcap)
2297 t->display_info.tty->TS_enter_underline_mode = 0;
a3fbb897
KL
2298 return Qnil;
2299}
2300
c6bf3022
CY
2301DEFUN ("tty-top-frame", Ftty_top_frame, Stty_top_frame, 0, 1, 0,
2302 doc: /* Return the topmost terminal frame on TERMINAL.
2303TERMINAL can be a terminal object, a frame or nil (meaning the
2304selected frame's terminal). This function returns nil if TERMINAL
2305does not refer to a text terminal. Otherwise, it returns the
2306top-most frame on the text terminal. */)
2307 (Lisp_Object terminal)
2308{
2309 struct terminal *t = get_terminal (terminal, 1);
2310
2311 if (t->type == output_termcap)
2312 return t->display_info.tty->top_frame;
2313 return Qnil;
2314}
2315
ed8dad6b
KL
2316\f
2317
2318DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2319 doc: /* Suspend the terminal device TTY.
2320
2321The device is restored to its default state, and Emacs ceases all
2322access to the tty device. Frames that use the device are not deleted,
2323but input is not read from them and if they change, their display is
2324not updated.
2325
401e9e57
CY
2326TTY may be a terminal object, a frame, or nil for the terminal device
2327of the currently selected frame.
ed8dad6b 2328
e4019195 2329This function runs `suspend-tty-functions' after suspending the
ed8dad6b
KL
2330device. The functions are run with one arg, the id of the suspended
2331terminal device.
2332
2333`suspend-tty' does nothing if it is called on a device that is already
2334suspended.
2335
2336A suspended tty may be resumed by calling `resume-tty' on it. */)
5842a27b 2337 (Lisp_Object tty)
ed8dad6b 2338{
717a00ef 2339 struct terminal *t = get_tty_terminal (tty, 1);
ed8dad6b 2340 FILE *f;
f4d953fc 2341
6ed8eeff 2342 if (!t)
ed8dad6b
KL
2343 error ("Unknown tty device");
2344
6ed8eeff 2345 f = t->display_info.tty->input;
f4d953fc 2346
ed8dad6b
KL
2347 if (f)
2348 {
23d4cba5
DN
2349 /* First run `suspend-tty-functions' and then clean up the tty
2350 state because `suspend-tty-functions' might need to change
2351 the tty state. */
dee091a3
JD
2352 Lisp_Object args[2];
2353 args[0] = intern ("suspend-tty-functions");
2354 XSETTERMINAL (args[1], t);
2355 Frun_hook_with_args (2, args);
23d4cba5 2356
6ed8eeff 2357 reset_sys_modes (t->display_info.tty);
ed8dad6b 2358 delete_keyboard_wait_descriptor (fileno (f));
f4d953fc 2359
84704c5c 2360#ifndef MSDOS
ed8dad6b 2361 fclose (f);
6ed8eeff
KL
2362 if (f != t->display_info.tty->output)
2363 fclose (t->display_info.tty->output);
84704c5c 2364#endif
f4d953fc 2365
6ed8eeff
KL
2366 t->display_info.tty->input = 0;
2367 t->display_info.tty->output = 0;
ed8dad6b 2368
6ed8eeff 2369 if (FRAMEP (t->display_info.tty->top_frame))
edfa7fa0 2370 SET_FRAME_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
f4d953fc 2371
ed8dad6b
KL
2372 }
2373
4a665758
KL
2374 /* Clear display hooks to prevent further output. */
2375 clear_tty_hooks (t);
2376
ed8dad6b
KL
2377 return Qnil;
2378}
2379
2380DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2381 doc: /* Resume the previously suspended terminal device TTY.
2382The terminal is opened and reinitialized. Frames that are on the
6ed8eeff 2383suspended terminal are revived.
ed8dad6b 2384
6ed8eeff
KL
2385It is an error to resume a terminal while another terminal is active
2386on the same device.
ed8dad6b 2387
e4019195 2388This function runs `resume-tty-functions' after resuming the terminal.
6ed8eeff 2389The functions are run with one arg, the id of the resumed terminal
ed8dad6b
KL
2390device.
2391
2392`resume-tty' does nothing if it is called on a device that is not
2393suspended.
2394
401e9e57
CY
2395TTY may be a terminal object, a frame, or nil (meaning the selected
2396frame's terminal). */)
5842a27b 2397 (Lisp_Object tty)
ed8dad6b 2398{
717a00ef 2399 struct terminal *t = get_tty_terminal (tty, 1);
ed8dad6b
KL
2400 int fd;
2401
6ed8eeff 2402 if (!t)
ed8dad6b
KL
2403 error ("Unknown tty device");
2404
6ed8eeff 2405 if (!t->display_info.tty->input)
ed8dad6b 2406 {
6ed8eeff 2407 if (get_named_tty (t->display_info.tty->name))
ed8dad6b
KL
2408 error ("Cannot resume display while another display is active on the same device");
2409
84704c5c
EZ
2410#ifdef MSDOS
2411 t->display_info.tty->output = stdout;
2412 t->display_info.tty->input = stdin;
2413#else /* !MSDOS */
6ed8eeff 2414 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
ef30e638
PE
2415 t->display_info.tty->input = t->display_info.tty->output
2416 = fd < 0 ? 0 : fdopen (fd, "w+");
ed8dad6b 2417
ef30e638
PE
2418 if (! t->display_info.tty->input)
2419 {
2420 int open_errno = errno;
2421 emacs_close (fd);
2422 report_file_errno ("Cannot reopen tty device",
2423 build_string (t->display_info.tty->name),
2424 open_errno);
2425 }
ed8dad6b 2426
b8956427 2427 if (!O_IGNORE_CTTY && strcmp (t->display_info.tty->name, DEV_TTY) != 0)
59b3194c 2428 dissociate_if_controlling_tty (fd);
84704c5c 2429#endif
78048085 2430
ed8dad6b
KL
2431 add_keyboard_wait_descriptor (fd);
2432
6ed8eeff 2433 if (FRAMEP (t->display_info.tty->top_frame))
db878925
DN
2434 {
2435 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2436 int width, height;
2437 int old_height = FRAME_COLS (f);
2438 int old_width = FRAME_LINES (f);
2439
2440 /* Check if terminal/window size has changed while the frame
2441 was suspended. */
2442 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2443 if (width != old_width || height != old_height)
2444 change_frame_size (f, height, width, 0, 0, 0);
edfa7fa0 2445 SET_FRAME_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
db878925 2446 }
ed8dad6b 2447
04f2d78b 2448 set_tty_hooks (t);
6ed8eeff 2449 init_sys_modes (t->display_info.tty);
ed8dad6b 2450
dee091a3
JD
2451 {
2452 /* Run `resume-tty-functions'. */
2453 Lisp_Object args[2];
2454 args[0] = intern ("resume-tty-functions");
2455 XSETTERMINAL (args[1], t);
2456 Frun_hook_with_args (2, args);
2457 }
ed8dad6b
KL
2458 }
2459
4a665758
KL
2460 set_tty_hooks (t);
2461
ed8dad6b
KL
2462 return Qnil;
2463}
a3fbb897 2464
819b8f00 2465\f
e882229c
NR
2466/***********************************************************************
2467 Mouse
2468 ***********************************************************************/
2469
7e5a23bd 2470#ifdef HAVE_GPM
64520e5c
PE
2471
2472#ifndef HAVE_WINDOW_SYSTEM
5faa03ba
RS
2473void
2474term_mouse_moveto (int x, int y)
94da14a0 2475{
1ec5dc77 2476 /* TODO: how to set mouse position?
94da14a0
NR
2477 const char *name;
2478 int fd;
2479 name = (const char *) ttyname (0);
406af475 2480 fd = emacs_open (name, O_WRONLY, 0);
80fb2ce0 2481 SOME_FUNCTION (x, y, fd);
bacba3c2 2482 emacs_close (fd);
94da14a0 2483 last_mouse_x = x;
80fb2ce0 2484 last_mouse_y = y; */
94da14a0 2485}
64520e5c 2486#endif /* HAVE_WINDOW_SYSTEM */
94da14a0 2487
cf482c50
EZ
2488/* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2489void
2490tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row,
2491 int start_hpos, int end_hpos,
2492 enum draw_glyphs_face draw)
e882229c 2493{
cf482c50
EZ
2494 int nglyphs = end_hpos - start_hpos;
2495 struct frame *f = XFRAME (WINDOW_FRAME (w));
7be1c21a 2496 struct tty_display_info *tty = FRAME_TTY (f);
cf482c50
EZ
2497 int face_id = tty->mouse_highlight.mouse_face_face_id;
2498 int save_x, save_y, pos_x, pos_y;
7be1c21a 2499
cf482c50
EZ
2500 if (end_hpos >= row->used[TEXT_AREA])
2501 nglyphs = row->used[TEXT_AREA] - start_hpos;
e882229c 2502
cf482c50
EZ
2503 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2504 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos + WINDOW_LEFT_EDGE_X (w);
e882229c 2505
cf482c50
EZ
2506 /* Save current cursor co-ordinates. */
2507 save_y = curY (tty);
2508 save_x = curX (tty);
2509 cursor_to (f, pos_y, pos_x);
e882229c 2510
cf482c50
EZ
2511 if (draw == DRAW_MOUSE_FACE)
2512 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2513 nglyphs, face_id);
2514 else if (draw == DRAW_NORMAL_TEXT)
2515 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
e882229c 2516
cf482c50 2517 cursor_to (f, save_y, save_x);
e882229c
NR
2518}
2519
653d4f43 2520static bool
a10c8269 2521term_mouse_movement (struct frame *frame, Gpm_Event *event)
e882229c
NR
2522{
2523 /* Has the mouse moved off the glyph it was on at the last sighting? */
2524 if (event->x != last_mouse_x || event->y != last_mouse_y)
2525 {
2526 frame->mouse_moved = 1;
cf482c50 2527 note_mouse_highlight (frame, event->x, event->y);
e882229c
NR
2528 /* Remember which glyph we're now on. */
2529 last_mouse_x = event->x;
2530 last_mouse_y = event->y;
2531 return 1;
2532 }
2533 return 0;
2534}
2535
d35af63c
PE
2536/* Return the Time that corresponds to T. Wrap around on overflow. */
2537static Time
2538timeval_to_Time (struct timeval const *t)
2539{
2540 Time s_1000, ms;
2541
2542 s_1000 = t->tv_sec;
2543 s_1000 *= 1000;
2544 ms = t->tv_usec / 1000;
2545 return s_1000 + ms;
2546}
2547
e882229c
NR
2548/* Return the current position of the mouse.
2549
2550 Set *f to the frame the mouse is in, or zero if the mouse is in no
2551 Emacs frame. If it is set to zero, all the other arguments are
2552 garbage.
2553
2554 Set *bar_window to Qnil, and *x and *y to the column and
2555 row of the character cell the mouse is over.
2556
7f3f1250 2557 Set *timeptr to the time the mouse was at the returned position.
e882229c 2558
80fb2ce0 2559 This clears mouse_moved until the next motion
94da14a0 2560 event arrives. */
e882229c 2561static void
a10c8269 2562term_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
e882229c 2563 enum scroll_bar_part *part, Lisp_Object *x,
08dc5ae6 2564 Lisp_Object *y, Time *timeptr)
e882229c 2565{
e882229c 2566 struct timeval now;
e882229c
NR
2567
2568 *fp = SELECTED_FRAME ();
94da14a0 2569 (*fp)->mouse_moved = 0;
e882229c
NR
2570
2571 *bar_window = Qnil;
2572 *part = 0;
2573
80fb2ce0 2574 XSETINT (*x, last_mouse_x);
94da14a0 2575 XSETINT (*y, last_mouse_y);
e882229c 2576 gettimeofday(&now, 0);
d35af63c 2577 *timeptr = timeval_to_Time (&now);
e882229c
NR
2578}
2579
2580/* Prepare a mouse-event in *RESULT for placement in the input queue.
2581
2582 If the event is a button press, then note that we have grabbed
2583 the mouse. */
2584
2585static Lisp_Object
2586term_mouse_click (struct input_event *result, Gpm_Event *event,
2587 struct frame *f)
2588{
2589 struct timeval now;
2590 int i, j;
2591
2592 result->kind = GPM_CLICK_EVENT;
2593 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2594 {
2595 if (event->buttons & j) {
2596 result->code = i; /* button number */
2597 break;
2598 }
2599 }
2600 gettimeofday(&now, 0);
d35af63c 2601 result->timestamp = timeval_to_Time (&now);
e882229c
NR
2602
2603 if (event->type & GPM_UP)
2604 result->modifiers = up_modifier;
2605 else if (event->type & GPM_DOWN)
2606 result->modifiers = down_modifier;
2607 else
2608 result->modifiers = 0;
f4d953fc 2609
e882229c
NR
2610 if (event->type & GPM_SINGLE)
2611 result->modifiers |= click_modifier;
f4d953fc 2612
e882229c
NR
2613 if (event->type & GPM_DOUBLE)
2614 result->modifiers |= double_modifier;
2615
2616 if (event->type & GPM_TRIPLE)
2617 result->modifiers |= triple_modifier;
2618
2619 if (event->type & GPM_DRAG)
2620 result->modifiers |= drag_modifier;
2621
80fb2ce0 2622 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
e882229c
NR
2623
2624 /* 1 << KG_SHIFT */
2625 if (event->modifiers & (1 << 0))
2626 result->modifiers |= shift_modifier;
2627
2628 /* 1 << KG_CTRL */
2629 if (event->modifiers & (1 << 2))
2630 result->modifiers |= ctrl_modifier;
2631
2632 /* 1 << KG_ALT || KG_ALTGR */
2633 if (event->modifiers & (1 << 3)
2634 || event->modifiers & (1 << 1))
2635 result->modifiers |= meta_modifier;
2636 }
2637
80fb2ce0
NR
2638 XSETINT (result->x, event->x);
2639 XSETINT (result->y, event->y);
e882229c
NR
2640 XSETFRAME (result->frame_or_window, f);
2641 result->arg = Qnil;
2642 return Qnil;
2643}
2644
f4d953fc 2645int
7be1c21a 2646handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
e882229c 2647{
7be1c21a 2648 struct frame *f = XFRAME (tty->top_frame);
e882229c 2649 struct input_event ie;
653d4f43 2650 bool do_help = 0;
e882229c
NR
2651 int count = 0;
2652
2653 EVENT_INIT (ie);
2654 ie.kind = NO_EVENT;
2655 ie.arg = Qnil;
2656
80fb2ce0 2657 if (event->type & (GPM_MOVE | GPM_DRAG)) {
e882229c
NR
2658 previous_help_echo_string = help_echo_string;
2659 help_echo_string = Qnil;
2660
6178ce5e 2661 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
e882229c 2662
80fb2ce0
NR
2663 if (!term_mouse_movement (f, event))
2664 help_echo_string = previous_help_echo_string;
e882229c
NR
2665
2666 /* If the contents of the global variable help_echo_string
2667 has changed, generate a HELP_EVENT. */
2668 if (!NILP (help_echo_string)
2669 || !NILP (previous_help_echo_string))
2670 do_help = 1;
2671
2672 goto done;
2673 }
2674 else {
2675 f->mouse_moved = 0;
2676 term_mouse_click (&ie, event, f);
e882229c
NR
2677 }
2678
2679 done:
2680 if (ie.kind != NO_EVENT)
2681 {
2682 kbd_buffer_store_event_hold (&ie, hold_quit);
2683 count++;
2684 }
2685
2686 if (do_help
2687 && !(hold_quit && hold_quit->kind != NO_EVENT))
2688 {
2689 Lisp_Object frame;
2690
2691 if (f)
2692 XSETFRAME (frame, f);
2693 else
2694 frame = Qnil;
2695
2696 gen_help_event (help_echo_string, frame, help_echo_window,
2697 help_echo_object, help_echo_pos);
2698 count++;
2699 }
2700
2701 return count;
2702}
2703
6178ce5e 2704DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
e882229c 2705 0, 0, 0,
71f44e7a 2706 doc: /* Open a connection to Gpm.
6178ce5e 2707Gpm-mouse can only be activated for one tty at a time. */)
5842a27b 2708 (void)
e882229c 2709{
71f44e7a
SM
2710 struct frame *f = SELECTED_FRAME ();
2711 struct tty_display_info *tty
2712 = ((f)->output_method == output_termcap
2713 ? (f)->terminal->display_info.tty : NULL);
e882229c
NR
2714 Gpm_Connect connection;
2715
6178ce5e
SM
2716 if (!tty)
2717 error ("Gpm-mouse only works in the GNU/Linux console");
4ce5ab77
SM
2718 if (gpm_tty == tty)
2719 return Qnil; /* Already activated, nothing to do. */
2720 if (gpm_tty)
2721 error ("Gpm-mouse can only be activated for one tty at a time");
71f44e7a 2722
e882229c
NR
2723 connection.eventMask = ~0;
2724 connection.defaultMask = ~GPM_HARD;
2725 connection.maxMod = ~0;
2726 connection.minMod = 0;
80fb2ce0 2727 gpm_zerobased = 1;
e882229c
NR
2728
2729 if (Gpm_Open (&connection, 0) < 0)
6178ce5e 2730 error ("Gpm-mouse failed to connect to the gpm daemon");
e882229c
NR
2731 else
2732 {
71f44e7a 2733 gpm_tty = tty;
1023cbed
SM
2734 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2735 to generate SIGIOs. Apparently we need to call reset_sys_modes
2736 before calling init_sys_modes. */
7be1c21a
MB
2737 reset_sys_modes (tty);
2738 init_sys_modes (tty);
e882229c 2739 add_gpm_wait_descriptor (gpm_fd);
6178ce5e 2740 return Qnil;
e882229c
NR
2741 }
2742}
2743
ed5ff21d 2744void
d347e494 2745close_gpm (int fd)
ed5ff21d 2746{
d347e494
SM
2747 if (fd >= 0)
2748 delete_gpm_wait_descriptor (fd);
ed5ff21d
SM
2749 while (Gpm_Close()); /* close all the stack */
2750 gpm_tty = NULL;
2751}
2752
6178ce5e 2753DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
e882229c
NR
2754 0, 0, 0,
2755 doc: /* Close a connection to Gpm. */)
5842a27b 2756 (void)
e882229c 2757{
4ce5ab77
SM
2758 struct frame *f = SELECTED_FRAME ();
2759 struct tty_display_info *tty
2760 = ((f)->output_method == output_termcap
2761 ? (f)->terminal->display_info.tty : NULL);
2762
2763 if (!tty || gpm_tty != tty)
2764 return Qnil; /* Not activated on this terminal, nothing to do. */
f4d953fc 2765
d347e494 2766 close_gpm (gpm_fd);
6178ce5e 2767 return Qnil;
e882229c 2768}
7e5a23bd 2769#endif /* HAVE_GPM */
e882229c
NR
2770
2771\f
b5e9cbb6
EZ
2772/***********************************************************************
2773 Menus
2774 ***********************************************************************/
2775
2776#if defined (HAVE_MENUS) && !defined (MSDOS)
2777
2778/* TTY menu implementation and main ideas are borrowed from msdos.c.
2779
2780 However, unlike on MSDOS, where the menu text is drawn directly to
fa93733d
EZ
2781 the display video memory, on a TTY we use display_string (see
2782 display_tty_menu_item in xdisp.c) to put the glyphs produced from
2783 the menu items directly into the frame's 'desired_matrix' glyph
2784 matrix, and then call update_frame_with_menu to deliver the results
2785 to the glass. The previous contents of the screen, in the form of
2786 the current_matrix, is stashed away, and used to restore screen
b5e9cbb6
EZ
2787 contents when the menu selection changes or when the final
2788 selection is made and the menu should be popped down.
2789
141f1ff7 2790 The idea of this implementation was suggested by Gerd Moellmann. */
b5e9cbb6
EZ
2791
2792#define TTYM_FAILURE -1
2793#define TTYM_SUCCESS 1
2794#define TTYM_NO_SELECT 2
2795#define TTYM_IA_SELECT 3
4a48e94d
EZ
2796#define TTYM_NEXT 4
2797#define TTYM_PREV 5
b5e9cbb6
EZ
2798
2799/* These hold text of the current and the previous menu help messages. */
2800static const char *menu_help_message, *prev_menu_help_message;
2801/* Pane number and item number of the menu item which generated the
2802 last menu help message. */
2803static int menu_help_paneno, menu_help_itemno;
2804
df782309
EZ
2805static Lisp_Object Qtty_menu_navigation_map, Qtty_menu_exit;
2806static Lisp_Object Qtty_menu_prev_item, Qtty_menu_next_item;
2807static Lisp_Object Qtty_menu_next_menu, Qtty_menu_prev_menu;
2808static Lisp_Object Qtty_menu_select, Qtty_menu_ignore;
e648f699 2809static Lisp_Object Qtty_menu_mouse_movement;
f0177f86 2810
b5e9cbb6
EZ
2811typedef struct tty_menu_struct
2812{
2813 int count;
2814 char **text;
2815 struct tty_menu_struct **submenu;
5cbcc455 2816 int *panenumber; /* Also used as enabled flag. */
3b158d11 2817 ptrdiff_t allocated;
b5e9cbb6
EZ
2818 int panecount;
2819 int width;
2820 const char **help_text;
2821} tty_menu;
2822
2823/* Create a brand new menu structure. */
2824
2825static tty_menu *
2826tty_menu_create (void)
2827{
3b158d11 2828 return xzalloc (sizeof *tty_menu_create ());
b5e9cbb6
EZ
2829}
2830
2831/* Allocate some (more) memory for MENU ensuring that there is room for one
3b158d11 2832 more item. */
b5e9cbb6
EZ
2833
2834static void
2835tty_menu_make_room (tty_menu *menu)
2836{
3b158d11 2837 if (menu->allocated == menu->count)
b5e9cbb6 2838 {
3b158d11
PE
2839 ptrdiff_t allocated = menu->allocated;
2840 menu->text = xpalloc (menu->text, &allocated, 1, -1, sizeof *menu->text);
2841 menu->text = xrealloc (menu->text, allocated * sizeof *menu->text);
2842 menu->submenu = xrealloc (menu->submenu,
2843 allocated * sizeof *menu->submenu);
2844 menu->panenumber = xrealloc (menu->panenumber,
2845 allocated * sizeof *menu->panenumber);
2846 menu->help_text = xrealloc (menu->help_text,
2847 allocated * sizeof *menu->help_text);
2848 menu->allocated = allocated;
b5e9cbb6
EZ
2849 }
2850}
2851
2852/* Search the given menu structure for a given pane number. */
2853
2854static tty_menu *
2855tty_menu_search_pane (tty_menu *menu, int pane)
2856{
2857 int i;
2858 tty_menu *try;
2859
2860 for (i = 0; i < menu->count; i++)
2861 if (menu->submenu[i])
2862 {
2863 if (pane == menu->panenumber[i])
2864 return menu->submenu[i];
2865 if ((try = tty_menu_search_pane (menu->submenu[i], pane)))
2866 return try;
2867 }
2868 return (tty_menu *) 0;
2869}
2870
2871/* Determine how much screen space a given menu needs. */
2872
2873static void
2874tty_menu_calc_size (tty_menu *menu, int *width, int *height)
2875{
2876 int i, h2, w2, maxsubwidth, maxheight;
2877
9e30f0e1 2878 maxsubwidth = menu->width;
b5e9cbb6
EZ
2879 maxheight = menu->count;
2880 for (i = 0; i < menu->count; i++)
2881 {
2882 if (menu->submenu[i])
2883 {
2884 tty_menu_calc_size (menu->submenu[i], &w2, &h2);
2885 if (w2 > maxsubwidth) maxsubwidth = w2;
2886 if (i + h2 > maxheight) maxheight = i + h2;
2887 }
2888 }
9e30f0e1 2889 *width = maxsubwidth;
b5e9cbb6
EZ
2890 *height = maxheight;
2891}
2892
e648f699
EZ
2893static void
2894mouse_get_xy (int *x, int *y)
2895{
2896 struct frame *sf = SELECTED_FRAME ();
5cbcc455 2897 Lisp_Object lmx = Qnil, lmy = Qnil, lisp_dummy;
e648f699
EZ
2898 enum scroll_bar_part part_dummy;
2899 Time time_dummy;
2900
2901 if (FRAME_TERMINAL (sf)->mouse_position_hook)
2902 (*FRAME_TERMINAL (sf)->mouse_position_hook) (&sf, -1,
2903 &lisp_dummy, &part_dummy,
2904 &lmx, &lmy,
2905 &time_dummy);
2906 if (!NILP (lmx))
2907 {
2908 *x = XINT (lmx);
2909 *y = XINT (lmy);
2910 }
2911}
2912
b5e9cbb6
EZ
2913/* Display MENU at (X,Y) using FACES. */
2914
b5e9cbb6 2915static void
ffc3882f 2916tty_menu_display (tty_menu *menu, int x, int y, int pn, int *faces,
bbc10837 2917 int mx, int my, int disp_help)
b5e9cbb6 2918{
bbc10837 2919 int i, face, width, enabled, mousehere, row, col;
b5e9cbb6
EZ
2920 struct frame *sf = SELECTED_FRAME ();
2921 struct tty_display_info *tty = FRAME_TTY (sf);
b5e9cbb6
EZ
2922
2923 menu_help_message = NULL;
2924
2925 width = menu->width;
e7873136
EZ
2926 col = cursorX (tty);
2927 row = cursorY (tty);
b5e9cbb6
EZ
2928 for (i = 0; i < menu->count; i++)
2929 {
342cf494 2930 int max_width = width + 2; /* +2 for padding blanks on each side */
b5e9cbb6
EZ
2931
2932 cursor_to (sf, y + i, x);
342cf494
EZ
2933 if (menu->submenu[i])
2934 max_width += 2; /* for displaying " >" after the item */
b5e9cbb6
EZ
2935 enabled
2936 = (!menu->submenu[i] && menu->panenumber[i]) || (menu->submenu[i]);
2937 mousehere = (y + i == my && x <= mx && mx < x + max_width);
2938 face = faces[enabled + mousehere * 2];
2939 /* Display the menu help string for the i-th menu item even if
2940 the menu item is currently disabled. That's what the GUI
2941 code does. */
2942 if (disp_help && enabled + mousehere * 2 >= 2)
2943 {
2944 menu_help_message = menu->help_text[i];
2945 menu_help_paneno = pn - 1;
2946 menu_help_itemno = i;
2947 }
342cf494 2948 display_tty_menu_item (menu->text[i], max_width, face, x, y + i,
b5e9cbb6
EZ
2949 menu->submenu[i] != NULL);
2950 }
2951 update_frame_with_menu (sf);
2952 cursor_to (sf, row, col);
2953}
2954
2955/* --------------------------- X Menu emulation ---------------------- */
2956
b5e9cbb6
EZ
2957/* Create a new pane and place it on the outer-most level. */
2958
e7873136
EZ
2959static int
2960tty_menu_add_pane (tty_menu *menu, const char *txt)
b5e9cbb6
EZ
2961{
2962 int len;
3b158d11 2963 const unsigned char *p;
b5e9cbb6
EZ
2964
2965 tty_menu_make_room (menu);
2966 menu->submenu[menu->count] = tty_menu_create ();
2967 menu->text[menu->count] = (char *)txt;
2968 menu->panenumber[menu->count] = ++menu->panecount;
2969 menu->help_text[menu->count] = NULL;
2970 menu->count++;
2971
2972 /* Update the menu width, if necessary. */
3b158d11 2973 for (len = 0, p = (unsigned char *) txt; *p; )
b5e9cbb6
EZ
2974 {
2975 int ch_len;
2976 int ch = STRING_CHAR_AND_LENGTH (p, ch_len);
2977
2978 len += CHAR_WIDTH (ch);
2979 p += ch_len;
2980 }
2981
2982 if (len > menu->width)
2983 menu->width = len;
2984
2985 return menu->panecount;
2986}
2987
2988/* Create a new item in a menu pane. */
2989
3b158d11 2990static int
b5e9cbb6
EZ
2991tty_menu_add_selection (tty_menu *menu, int pane,
2992 char *txt, int enable, char const *help_text)
2993{
2994 int len;
3b158d11 2995 unsigned char *p;
b5e9cbb6
EZ
2996
2997 if (pane)
2998 if (!(menu = tty_menu_search_pane (menu, pane)))
2999 return TTYM_FAILURE;
3000 tty_menu_make_room (menu);
3001 menu->submenu[menu->count] = (tty_menu *) 0;
3002 menu->text[menu->count] = txt;
3003 menu->panenumber[menu->count] = enable;
3004 menu->help_text[menu->count] = help_text;
3005 menu->count++;
3006
3007 /* Update the menu width, if necessary. */
3b158d11 3008 for (len = 0, p = (unsigned char *) txt; *p; )
b5e9cbb6
EZ
3009 {
3010 int ch_len;
3011 int ch = STRING_CHAR_AND_LENGTH (p, ch_len);
3012
3013 len += CHAR_WIDTH (ch);
3014 p += ch_len;
3015 }
3016
3017 if (len > menu->width)
3018 menu->width = len;
3019
3020 return TTYM_SUCCESS;
3021}
3022
3023/* Decide where the menu would be placed if requested at (X,Y). */
3024
3b158d11 3025static void
b5e9cbb6
EZ
3026tty_menu_locate (tty_menu *menu, int x, int y,
3027 int *ulx, int *uly, int *width, int *height)
3028{
3029 tty_menu_calc_size (menu, width, height);
3030 *ulx = x + 1;
3031 *uly = y;
3032 *width += 2;
3033}
3034
3035struct tty_menu_state
3036{
141f1ff7 3037 struct glyph_matrix *screen_behind;
b5e9cbb6
EZ
3038 tty_menu *menu;
3039 int pane;
3040 int x, y;
3041};
3042
f34729ea
EZ
3043/* Save away the contents of frame F's current frame matrix, and
3044 enable all its rows. Value is a glyph matrix holding the contents
3045 of F's current frame matrix with all its glyph rows enabled. */
3046
4ed77415 3047static struct glyph_matrix *
f34729ea
EZ
3048save_and_enable_current_matrix (struct frame *f)
3049{
3050 int i;
3051 struct glyph_matrix *saved = xzalloc (sizeof *saved);
3052 saved->nrows = f->current_matrix->nrows;
3053 saved->rows = xzalloc (saved->nrows * sizeof *saved->rows);
3054
3055 for (i = 0; i < saved->nrows; ++i)
3056 {
3057 struct glyph_row *from = f->current_matrix->rows + i;
3058 struct glyph_row *to = saved->rows + i;
3059 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
3060
3061 to->glyphs[TEXT_AREA] = xmalloc (nbytes);
3062 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
3063 to->used[TEXT_AREA] = from->used[TEXT_AREA];
3064 /* Make sure every row is enabled, or else update_frame will not
3065 redraw them. (Rows that are identical to what is already on
3066 screen will not be redrawn anyway.) */
3067 to->enabled_p = 1;
3068 to->hash = from->hash;
3069 if (from->used[LEFT_MARGIN_AREA])
3070 {
3071 nbytes = from->used[LEFT_MARGIN_AREA] * sizeof (struct glyph);
3b158d11 3072 to->glyphs[LEFT_MARGIN_AREA] = xmalloc (nbytes);
f34729ea
EZ
3073 memcpy (to->glyphs[LEFT_MARGIN_AREA],
3074 from->glyphs[LEFT_MARGIN_AREA], nbytes);
3075 to->used[LEFT_MARGIN_AREA] = from->used[LEFT_MARGIN_AREA];
3076 }
3077 if (from->used[RIGHT_MARGIN_AREA])
3078 {
3079 nbytes = from->used[RIGHT_MARGIN_AREA] * sizeof (struct glyph);
3b158d11 3080 to->glyphs[RIGHT_MARGIN_AREA] = xmalloc (nbytes);
f34729ea
EZ
3081 memcpy (to->glyphs[RIGHT_MARGIN_AREA],
3082 from->glyphs[RIGHT_MARGIN_AREA], nbytes);
3083 to->used[RIGHT_MARGIN_AREA] = from->used[RIGHT_MARGIN_AREA];
3084 }
3085 }
3086
3087 return saved;
3088}
3089
141f1ff7
EZ
3090/* Restore the contents of frame F's desired frame matrix from SAVED,
3091 and free memory associated with SAVED. */
b5e9cbb6 3092
141f1ff7
EZ
3093static void
3094restore_desired_matrix (struct frame *f, struct glyph_matrix *saved)
3095{
3096 int i;
3097
3098 for (i = 0; i < saved->nrows; ++i)
3099 {
3100 struct glyph_row *from = saved->rows + i;
3101 struct glyph_row *to = f->desired_matrix->rows + i;
3102 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
3103
96114a30 3104 eassert (to->glyphs[TEXT_AREA] != from->glyphs[TEXT_AREA]);
141f1ff7
EZ
3105 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
3106 to->used[TEXT_AREA] = from->used[TEXT_AREA];
fa93733d
EZ
3107 to->enabled_p = from->enabled_p;
3108 to->hash = from->hash;
96114a30 3109 nbytes = from->used[LEFT_MARGIN_AREA] * sizeof (struct glyph);
141f1ff7
EZ
3110 if (nbytes)
3111 {
96114a30 3112 eassert (to->glyphs[LEFT_MARGIN_AREA] != from->glyphs[LEFT_MARGIN_AREA]);
141f1ff7
EZ
3113 memcpy (to->glyphs[LEFT_MARGIN_AREA],
3114 from->glyphs[LEFT_MARGIN_AREA], nbytes);
3115 to->used[LEFT_MARGIN_AREA] = from->used[LEFT_MARGIN_AREA];
141f1ff7
EZ
3116 }
3117 else
3118 to->used[LEFT_MARGIN_AREA] = 0;
96114a30 3119 nbytes = from->used[RIGHT_MARGIN_AREA] * sizeof (struct glyph);
141f1ff7
EZ
3120 if (nbytes)
3121 {
96114a30 3122 eassert (to->glyphs[RIGHT_MARGIN_AREA] != from->glyphs[RIGHT_MARGIN_AREA]);
141f1ff7
EZ
3123 memcpy (to->glyphs[RIGHT_MARGIN_AREA],
3124 from->glyphs[RIGHT_MARGIN_AREA], nbytes);
3125 to->used[RIGHT_MARGIN_AREA] = from->used[RIGHT_MARGIN_AREA];
141f1ff7
EZ
3126 }
3127 else
3128 to->used[RIGHT_MARGIN_AREA] = 0;
3129 }
141f1ff7
EZ
3130}
3131
3132static void
3133free_saved_screen (struct glyph_matrix *saved)
3134{
3135 int i;
3136
3137 if (!saved)
3138 return; /* already freed */
3139
3140 for (i = 0; i < saved->nrows; ++i)
3141 {
3142 struct glyph_row *from = saved->rows + i;
3143
3144 xfree (from->glyphs[TEXT_AREA]);
96114a30 3145 if (from->used[LEFT_MARGIN_AREA])
141f1ff7 3146 xfree (from->glyphs[LEFT_MARGIN_AREA]);
96114a30 3147 if (from->used[RIGHT_MARGIN_AREA])
141f1ff7
EZ
3148 xfree (from->glyphs[RIGHT_MARGIN_AREA]);
3149 }
3150
3151 xfree (saved->rows);
3152 xfree (saved);
3153}
3154
3155/* Update the display of frame F from its saved contents. */
3156static void
3157screen_update (struct frame *f, struct glyph_matrix *mtx)
3158{
3159 restore_desired_matrix (f, mtx);
3160 update_frame_with_menu (f);
3161}
3162
3163/* Read user input and return X and Y coordinates where that input
3164 puts us. We only consider mouse movement and click events and
0de83597
EZ
3165 keyboard movement commands; the rest are ignored.
3166
4a48e94d
EZ
3167 Value is -1 if C-g was pressed, 1 if an item was selected, 2 or 3
3168 if we need to move to the next or previous menu-bar menu, zero
e648f699 3169 otherwise. */
0de83597 3170static int
df782309
EZ
3171read_menu_input (struct frame *sf, int *x, int *y, int min_y, int max_y,
3172 bool *first_time)
141f1ff7 3173{
ffc3882f
EZ
3174 if (*first_time)
3175 {
3176 *first_time = false;
3177 sf->mouse_moved = 1;
ffc3882f 3178 }
afd8eb2c 3179 else
141f1ff7 3180 {
7ace9265 3181 Lisp_Object cmd;
f0177f86
EZ
3182 int usable_input = 1;
3183 int st = 0;
f34729ea 3184 struct tty_display_info *tty = FRAME_TTY (sf);
e648f699 3185 Lisp_Object saved_mouse_tracking = do_mouse_tracking;
f0177f86 3186
f34729ea
EZ
3187 /* Signal the keyboard reading routines we are displaying a menu
3188 on this terminal. */
3189 tty->showing_menu = 1;
e648f699
EZ
3190 /* We want mouse movements be reported by read_menu_command. */
3191 do_mouse_tracking = Qt;
7ace9265
EZ
3192 do {
3193 cmd = read_menu_command ();
e648f699 3194 } while (NILP (cmd));
f34729ea 3195 tty->showing_menu = 0;
e648f699 3196 do_mouse_tracking = saved_mouse_tracking;
7ace9265 3197
df782309 3198 if (EQ (cmd, Qt) || EQ (cmd, Qtty_menu_exit))
f0177f86 3199 return -1;
e648f699 3200 if (EQ (cmd, Qtty_menu_mouse_movement))
3b158d11 3201 mouse_get_xy (x, y);
e648f699 3202 else if (EQ (cmd, Qtty_menu_next_menu))
4a48e94d
EZ
3203 {
3204 usable_input = 0;
3205 st = 2;
3206 }
df782309 3207 else if (EQ (cmd, Qtty_menu_prev_menu))
4a48e94d
EZ
3208 {
3209 usable_input = 0;
3210 st = 3;
3211 }
df782309
EZ
3212 else if (EQ (cmd, Qtty_menu_next_item))
3213 {
3214 if (*y < max_y)
3215 *y += 1;
3216 }
3217 else if (EQ (cmd, Qtty_menu_prev_item))
3218 {
3219 if (*y > min_y)
3220 *y -= 1;
3221 }
3222 else if (EQ (cmd, Qtty_menu_select))
f0177f86 3223 st = 1;
df782309 3224 else if (!EQ (cmd, Qtty_menu_ignore))
e648f699 3225 usable_input = 0;
f0177f86
EZ
3226 if (usable_input)
3227 sf->mouse_moved = 1;
0de83597 3228 return st;
141f1ff7 3229 }
0de83597 3230 return 0;
141f1ff7
EZ
3231}
3232
3233/* Display menu, wait for user's response, and return that response. */
4a48e94d 3234static int
b5e9cbb6
EZ
3235tty_menu_activate (tty_menu *menu, int *pane, int *selidx,
3236 int x0, int y0, char **txt,
4a48e94d
EZ
3237 void (*help_callback)(char const *, int, int),
3238 int kbd_navigation)
b5e9cbb6
EZ
3239{
3240 struct tty_menu_state *state;
3b158d11
PE
3241 int statecount, x, y, i, leave, onepane;
3242 int result IF_LINT (= 0);
b5e9cbb6
EZ
3243 int title_faces[4]; /* face to display the menu title */
3244 int faces[4], buffers_num_deleted = 0;
3245 struct frame *sf = SELECTED_FRAME ();
28a16449 3246 struct tty_display_info *tty = FRAME_TTY (sf);
ffc3882f 3247 bool first_time;
3b158d11 3248 Lisp_Object selectface;
b5e9cbb6
EZ
3249
3250 /* Don't allow non-positive x0 and y0, lest the menu will wrap
3251 around the display. */
3252 if (x0 <= 0)
3253 x0 = 1;
3254 if (y0 <= 0)
3255 y0 = 1;
3256
b5e9cbb6 3257 state = alloca (menu->panecount * sizeof (struct tty_menu_state));
141f1ff7 3258 memset (state, 0, sizeof (*state));
b5e9cbb6
EZ
3259 faces[0]
3260 = lookup_derived_face (sf, intern ("tty-menu-disabled-face"),
3261 DEFAULT_FACE_ID, 1);
3262 faces[1]
3263 = lookup_derived_face (sf, intern ("tty-menu-enabled-face"),
3264 DEFAULT_FACE_ID, 1);
3265 selectface = intern ("tty-menu-selected-face");
3266 faces[2] = lookup_derived_face (sf, selectface,
3267 faces[0], 1);
3268 faces[3] = lookup_derived_face (sf, selectface,
3269 faces[1], 1);
3270
3271 /* Make sure the menu title is always displayed with
50a5f95e 3272 `tty-menu-selected-face', no matter where the mouse pointer is. */
b5e9cbb6
EZ
3273 for (i = 0; i < 4; i++)
3274 title_faces[i] = faces[3];
3275
3276 statecount = 1;
3277
3278 /* Don't let the title for the "Buffers" popup menu include a
3279 digit (which is ugly).
3280
3281 This is a terrible kludge, but I think the "Buffers" case is
3282 the only one where the title includes a number, so it doesn't
3283 seem to be necessary to make this more general. */
3284 if (strncmp (menu->text[0], "Buffers 1", 9) == 0)
3285 {
3286 menu->text[0][7] = '\0';
3287 buffers_num_deleted = 1;
3288 }
3289
141f1ff7
EZ
3290 /* Force update of the current frame, so that the desired and the
3291 current matrices are identical. */
3292 update_frame_with_menu (sf);
b5e9cbb6 3293 state[0].menu = menu;
f34729ea 3294 state[0].screen_behind = save_and_enable_current_matrix (sf);
b5e9cbb6 3295
ffc3882f
EZ
3296 /* Display the menu title. We subtract 1 from x0 and y0 because we
3297 want to interpret them as zero-based column and row coordinates,
3298 and also because we want the first item of the menu, not its
3299 title, to appear at x0,y0. */
bbc10837 3300 tty_menu_display (menu, x0 - 1, y0 - 1, 1, title_faces, x0 - 1, y0 - 1, 0);
6270e29e
EZ
3301
3302 /* Turn off the cursor. Otherwise it shows through the menu
3303 panes, which is ugly. */
3304 tty_hide_cursor (tty);
b5e9cbb6
EZ
3305 if (buffers_num_deleted)
3306 menu->text[0][7] = ' ';
3307 if ((onepane = menu->count == 1 && menu->submenu[0]))
3308 {
3309 menu->width = menu->submenu[0]->width;
3310 state[0].menu = menu->submenu[0];
3311 }
3312 else
3313 {
3314 state[0].menu = menu;
3315 }
3316 state[0].x = x0 - 1;
3317 state[0].y = y0;
3318 state[0].pane = onepane;
3319
141f1ff7
EZ
3320 x = state[0].x;
3321 y = state[0].y;
ffc3882f 3322 first_time = true;
141f1ff7 3323
b5e9cbb6
EZ
3324 leave = 0;
3325 while (!leave)
3326 {
f0177f86 3327 int input_status;
df782309 3328 int min_y = state[0].y, max_y = min_y + state[0].menu->count - 1;
e7873136 3329
df782309 3330 input_status = read_menu_input (sf, &x, &y, min_y, max_y, &first_time);
f0177f86
EZ
3331 if (input_status)
3332 {
4a48e94d 3333 leave = 1;
f0177f86 3334 if (input_status == -1)
824682ea
EZ
3335 {
3336 /* Remove the last help-echo, so that it doesn't
3337 re-appear after "Quit". */
3338 show_help_echo (Qnil, Qnil, Qnil, Qnil);
3339 result = TTYM_NO_SELECT;
3340 }
4a48e94d
EZ
3341 else if (input_status == 2)
3342 {
3343 if (kbd_navigation)
3344 result = TTYM_NEXT;
3345 else
3346 leave = 0;
3347 }
3348 else if (input_status == 3)
3349 {
3350 if (kbd_navigation)
3351 result = TTYM_PREV;
3352 else
3353 leave = 0;
3354 }
f0177f86 3355 }
f34729ea 3356 if (sf->mouse_moved && input_status != -1)
b5e9cbb6
EZ
3357 {
3358 sf->mouse_moved = 0;
3359 result = TTYM_IA_SELECT;
b5e9cbb6
EZ
3360 for (i = 0; i < statecount; i++)
3361 if (state[i].x <= x && x < state[i].x + state[i].menu->width + 2)
3362 {
3363 int dy = y - state[i].y;
3364 if (0 <= dy && dy < state[i].menu->count)
3365 {
3366 if (!state[i].menu->submenu[dy])
3367 {
3368 if (state[i].menu->panenumber[dy])
3369 result = TTYM_SUCCESS;
3370 else
3371 result = TTYM_IA_SELECT;
3372 }
3373 *pane = state[i].pane - 1;
3374 *selidx = dy;
3375 /* We hit some part of a menu, so drop extra menus that
3376 have been opened. That does not include an open and
3377 active submenu. */
3378 if (i != statecount - 2
3379 || state[i].menu->submenu[dy] != state[i+1].menu)
3380 while (i != statecount - 1)
3381 {
3382 statecount--;
e7873136 3383 screen_update (sf, state[statecount].screen_behind);
141f1ff7 3384 state[statecount].screen_behind = NULL;
b5e9cbb6
EZ
3385 }
3386 if (i == statecount - 1 && state[i].menu->submenu[dy])
3387 {
3388 tty_menu_display (state[i].menu,
b5e9cbb6 3389 state[i].x,
ffc3882f 3390 state[i].y,
b5e9cbb6 3391 state[i].pane,
bbc10837 3392 faces, x, y, 1);
b5e9cbb6
EZ
3393 state[statecount].menu = state[i].menu->submenu[dy];
3394 state[statecount].pane = state[i].menu->panenumber[dy];
141f1ff7 3395 state[statecount].screen_behind
f34729ea 3396 = save_and_enable_current_matrix (sf);
b5e9cbb6
EZ
3397 state[statecount].x
3398 = state[i].x + state[i].menu->width + 2;
3399 state[statecount].y = y;
3400 statecount++;
3401 }
3402 }
3403 }
3404 tty_menu_display (state[statecount - 1].menu,
b5e9cbb6 3405 state[statecount - 1].x,
ffc3882f 3406 state[statecount - 1].y,
b5e9cbb6 3407 state[statecount - 1].pane,
bbc10837 3408 faces, x, y, 1);
6270e29e
EZ
3409 tty_hide_cursor (tty);
3410 fflush (tty->output);
b5e9cbb6 3411 }
824682ea
EZ
3412
3413 /* Display the help-echo message for the currently-selected menu
3414 item. */
3415 if ((menu_help_message || prev_menu_help_message)
3416 && menu_help_message != prev_menu_help_message)
b5e9cbb6 3417 {
824682ea
EZ
3418 help_callback (menu_help_message,
3419 menu_help_paneno, menu_help_itemno);
3420 tty_hide_cursor (tty);
6270e29e 3421 fflush (tty->output);
824682ea 3422 prev_menu_help_message = menu_help_message;
b5e9cbb6 3423 }
b5e9cbb6
EZ
3424 }
3425
ffc3882f 3426 sf->mouse_moved = 0;
141f1ff7 3427 screen_update (sf, state[0].screen_behind);
b5e9cbb6 3428 while (statecount--)
141f1ff7 3429 free_saved_screen (state[statecount].screen_behind);
28a16449 3430 tty_show_cursor (tty); /* turn cursor back on */
9f6a18d2 3431 fflush (tty->output);
28a16449
EZ
3432
3433/* Clean up any mouse events that are waiting inside Emacs event queue.
b5e9cbb6
EZ
3434 These events are likely to be generated before the menu was even
3435 displayed, probably because the user pressed and released the button
3436 (which invoked the menu) too quickly. If we don't remove these events,
3437 Emacs will process them after we return and surprise the user. */
3438 discard_mouse_events ();
e7873136 3439 if (!kbd_buffer_events_waiting ())
b5e9cbb6 3440 clear_input_pending ();
b5e9cbb6
EZ
3441 return result;
3442}
3443
3444/* Dispose of a menu. */
3445
3b158d11 3446static void
b5e9cbb6
EZ
3447tty_menu_destroy (tty_menu *menu)
3448{
3449 int i;
3450 if (menu->allocated)
3451 {
3452 for (i = 0; i < menu->count; i++)
3453 if (menu->submenu[i])
3454 tty_menu_destroy (menu->submenu[i]);
3455 xfree (menu->text);
3456 xfree (menu->submenu);
3457 xfree (menu->panenumber);
3458 xfree (menu->help_text);
3459 }
3460 xfree (menu);
3461 menu_help_message = prev_menu_help_message = NULL;
3462}
3463
e7873136
EZ
3464/* Show help HELP_STRING, or clear help if HELP_STRING is null.
3465
3466 PANE is the pane number, and ITEM is the menu item number in
5cbcc455 3467 the menu (currently not used). */
e7873136
EZ
3468
3469static void
3470tty_menu_help_callback (char const *help_string, int pane, int item)
3471{
3472 Lisp_Object *first_item;
3473 Lisp_Object pane_name;
3474 Lisp_Object menu_object;
3475
f573ac33 3476 first_item = XVECTOR (menu_items)->u.contents;
e7873136
EZ
3477 if (EQ (first_item[0], Qt))
3478 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3479 else if (EQ (first_item[0], Qquote))
3480 /* This shouldn't happen, see xmenu_show. */
3481 pane_name = empty_unibyte_string;
3482 else
3483 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
3484
3485 /* (menu-item MENU-NAME PANE-NUMBER) */
3486 menu_object = list3 (Qmenu_item, pane_name, make_number (pane));
3487 show_help_echo (help_string ? build_string (help_string) : Qnil,
3488 Qnil, menu_object, make_number (item));
3489}
3490
3491static void
3492tty_pop_down_menu (Lisp_Object arg)
3493{
3494 tty_menu *menu = XSAVE_POINTER (arg, 0);
3495
3496 block_input ();
3497 tty_menu_destroy (menu);
3498 unblock_input ();
3499}
3500
4a48e94d
EZ
3501/* Return the zero-based index of the last menu-bar item on frame F. */
3502static int
3503tty_menu_last_menubar_item (struct frame *f)
3504{
3505 int i = 0;
3506
3507 eassert (FRAME_TERMCAP_P (f) && FRAME_LIVE_P (f));
3508 if (FRAME_TERMCAP_P (f) && FRAME_LIVE_P (f))
3509 {
3510 Lisp_Object items = FRAME_MENU_BAR_ITEMS (f);
3511
3512 while (i < ASIZE (items))
3513 {
3514 Lisp_Object str;
3515
3516 str = AREF (items, i + 1);
3517 if (NILP (str))
3518 break;
3519 i += 4;
3520 }
3521 i -= 4; /* went one too far */
3522 }
3523 return i;
3524}
3525
3526/* Find in frame F's menu bar the menu item that is next or previous
3527 to the item at X/Y, and return that item's position in X/Y. WHICH
3528 says which one--next or previous--item to look for. X and Y are
3529 measured in character cells. This should only be called on TTY
3530 frames. */
3531static void
3532tty_menu_new_item_coords (struct frame *f, int which, int *x, int *y)
3533{
3534 eassert (FRAME_TERMCAP_P (f) && FRAME_LIVE_P (f));
3535 if (FRAME_TERMCAP_P (f) && FRAME_LIVE_P (f))
3536 {
3537 Lisp_Object items = FRAME_MENU_BAR_ITEMS (f);
3538 int last_i = tty_menu_last_menubar_item (f);
3539 int i, prev_x;
3540
3541 /* This loop assumes a single menu-bar line, and will fail to
3542 find an item if it is not in the first line. Note that
3543 make_lispy_event in keyboard.c makes the same assumption. */
3544 for (i = 0, prev_x = -1; i < ASIZE (items); i += 4)
3545 {
3546 Lisp_Object pos, str;
3547 int ix;
3548
3549 str = AREF (items, i + 1);
3550 pos = AREF (items, i + 3);
3551 if (NILP (str))
3552 return;
3553 ix = XINT (pos);
3554 if (ix <= *x
3555 /* We use <= so the blank between 2 items on a TTY is
3556 considered part of the previous item. */
3b158d11 3557 && *x <= ix + menu_item_width (SDATA (str)))
4a48e94d
EZ
3558 {
3559 /* Found current item. Now compute the X coordinate of
3560 the previous or next item. */
3561 if (which == TTYM_NEXT)
3562 {
3563 if (i < last_i)
3564 *x = XINT (AREF (items, i + 4 + 3));
3565 else
3566 *x = 0; /* wrap around to the first item */
3567 }
3568 else if (prev_x < 0)
3569 {
3570 /* Wrap around to the last item. */
3571 *x = XINT (AREF (items, last_i + 3));
3572 }
3573 else
3574 *x = prev_x;
3575 return;
3576 }
3577 prev_x = ix;
3578 }
3579 }
3580}
3581
b5e9cbb6 3582Lisp_Object
e7873136 3583tty_menu_show (struct frame *f, int x, int y, int for_click, int keymaps,
4a48e94d 3584 Lisp_Object title, int kbd_navigation, const char **error_name)
b5e9cbb6
EZ
3585{
3586 tty_menu *menu;
3587 int pane, selidx, lpane, status;
3588 Lisp_Object entry, pane_prefix;
3589 char *datap;
3590 int ulx, uly, width, height;
4a48e94d 3591 int item_x, item_y;
b5e9cbb6
EZ
3592 int dispwidth, dispheight;
3593 int i, j, lines, maxlines;
3594 int maxwidth;
b5e9cbb6
EZ
3595 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
3596
3597 if (! FRAME_TERMCAP_P (f))
e7873136 3598 emacs_abort ();
b5e9cbb6
EZ
3599
3600 *error_name = 0;
3601 if (menu_items_n_panes == 0)
3602 return Qnil;
3603
3604 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3605 {
3606 *error_name = "Empty menu";
3607 return Qnil;
3608 }
3609
3610 /* Make the menu on that window. */
3611 menu = tty_menu_create ();
3612 if (menu == NULL)
3613 {
3614 *error_name = "Can't create menu";
3615 return Qnil;
3616 }
3617
3618 /* Don't GC while we prepare and show the menu, because we give the
3619 menu functions pointers to the contents of strings. */
3620 inhibit_garbage_collection ();
3621
3622 /* Adjust coordinates to be root-window-relative. */
4a48e94d
EZ
3623 item_x = x += f->left_pos;
3624 item_y = y += f->top_pos;
b5e9cbb6
EZ
3625
3626 /* Create all the necessary panes and their items. */
3627 maxwidth = maxlines = lines = i = 0;
3628 lpane = TTYM_FAILURE;
3629 while (i < menu_items_used)
3630 {
dfe3ac02 3631 if (EQ (AREF (menu_items, i), Qt))
b5e9cbb6
EZ
3632 {
3633 /* Create a new pane. */
3634 Lisp_Object pane_name, prefix;
3635 const char *pane_string;
3636
3637 maxlines = max (maxlines, lines);
3638 lines = 0;
dfe3ac02
EZ
3639 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
3640 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
b5e9cbb6
EZ
3641 pane_string = (NILP (pane_name)
3642 ? "" : SSDATA (pane_name));
3643 if (keymaps && !NILP (prefix))
3644 pane_string++;
3645
3646 lpane = tty_menu_add_pane (menu, pane_string);
3647 if (lpane == TTYM_FAILURE)
3648 {
3649 tty_menu_destroy (menu);
3650 *error_name = "Can't create pane";
3651 return Qnil;
3652 }
3653 i += MENU_ITEMS_PANE_LENGTH;
3654
3655 /* Find the width of the widest item in this pane. */
3656 j = i;
3657 while (j < menu_items_used)
3658 {
3659 Lisp_Object item;
dfe3ac02 3660 item = AREF (menu_items, j);
b5e9cbb6
EZ
3661 if (EQ (item, Qt))
3662 break;
3663 if (NILP (item))
3664 {
3665 j++;
3666 continue;
3667 }
3668 width = SBYTES (item);
3669 if (width > maxwidth)
3670 maxwidth = width;
3671
3672 j += MENU_ITEMS_ITEM_LENGTH;
3673 }
3674 }
3675 /* Ignore a nil in the item list.
3676 It's meaningful only for dialog boxes. */
dfe3ac02 3677 else if (EQ (AREF (menu_items, i), Qquote))
b5e9cbb6
EZ
3678 i += 1;
3679 else
3680 {
3681 /* Create a new item within current pane. */
3682 Lisp_Object item_name, enable, descrip, help;
3683 char *item_data;
3684 char const *help_string;
3685
dfe3ac02
EZ
3686 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
3687 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
3688 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
3689 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
b5e9cbb6
EZ
3690 help_string = STRINGP (help) ? SSDATA (help) : NULL;
3691
3692 if (!NILP (descrip))
3693 {
3694 /* if alloca is fast, use that to make the space,
3695 to reduce gc needs. */
3696 item_data = (char *) alloca (maxwidth + SBYTES (descrip) + 1);
3697 memcpy (item_data, SSDATA (item_name), SBYTES (item_name));
3698 for (j = SCHARS (item_name); j < maxwidth; j++)
3699 item_data[j] = ' ';
3700 memcpy (item_data + j, SSDATA (descrip), SBYTES (descrip));
3701 item_data[j + SBYTES (descrip)] = 0;
3702 }
3703 else
3704 item_data = SSDATA (item_name);
3705
3706 if (lpane == TTYM_FAILURE
3707 || (tty_menu_add_selection (menu, lpane, item_data,
3708 !NILP (enable), help_string)
3709 == TTYM_FAILURE))
3710 {
3711 tty_menu_destroy (menu);
3712 *error_name = "Can't add selection to menu";
3713 return Qnil;
3714 }
3715 i += MENU_ITEMS_ITEM_LENGTH;
3716 lines++;
3717 }
3718 }
3719
3720 maxlines = max (maxlines, lines);
3721
3722 /* All set and ready to fly. */
3723 dispwidth = f->text_cols;
3724 dispheight = f->text_lines;
3725 x = min (x, dispwidth);
3726 y = min (y, dispheight);
3727 x = max (x, 1);
3728 y = max (y, 1);
3729 tty_menu_locate (menu, x, y, &ulx, &uly, &width, &height);
3730 if (ulx+width > dispwidth)
3731 {
3732 x -= (ulx + width) - dispwidth;
3733 ulx = dispwidth - width;
3734 }
3735 if (uly+height > dispheight)
3736 {
3737 y -= (uly + height) - dispheight;
3738 uly = dispheight - height;
3739 }
3740
df782309 3741 if (FRAME_HAS_MINIBUF_P (f) && uly+height > dispheight - 2)
b5e9cbb6
EZ
3742 {
3743 /* Move the menu away of the echo area, to avoid overwriting the
3744 menu with help echo messages or vice versa. */
3745 if (BUFFERP (echo_area_buffer[0]) && WINDOWP (echo_area_window))
3746 {
df782309
EZ
3747 y -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window)) + 1;
3748 uly -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window)) + 1;
b5e9cbb6
EZ
3749 }
3750 else
3751 {
df782309
EZ
3752 y -= 2;
3753 uly -= 2;
b5e9cbb6
EZ
3754 }
3755 }
3756
3757 if (ulx < 0) x -= ulx;
3758 if (uly < 0) y -= uly;
3759
a01f89af
EZ
3760#if 0
3761 /* This code doesn't make sense on a TTY, since it can easily annul
3762 the adjustments above that carefully avoid truncation of the menu
5cbcc455
EZ
3763 items. I think it was written to fix some problem that only
3764 happens on X11. */
b5e9cbb6
EZ
3765 if (! for_click)
3766 {
3767 /* If position was not given by a mouse click, adjust so upper left
3768 corner of the menu as a whole ends up at given coordinates. This
3769 is what x-popup-menu says in its documentation. */
3770 x += width/2;
3771 y += 1.5*height/(maxlines+2);
3772 }
a01f89af 3773#endif
b5e9cbb6
EZ
3774
3775 pane = selidx = 0;
3776
e7873136 3777 record_unwind_protect (tty_pop_down_menu, make_save_ptr (menu));
b5e9cbb6 3778
df782309
EZ
3779 specbind (Qoverriding_terminal_local_map,
3780 Fsymbol_value (Qtty_menu_navigation_map));
b5e9cbb6 3781 status = tty_menu_activate (menu, &pane, &selidx, x, y, &datap,
4a48e94d 3782 tty_menu_help_callback, kbd_navigation);
b5e9cbb6
EZ
3783 entry = pane_prefix = Qnil;
3784
3785 switch (status)
3786 {
3787 case TTYM_SUCCESS:
3788 /* Find the item number SELIDX in pane number PANE. */
3789 i = 0;
3790 while (i < menu_items_used)
3791 {
dfe3ac02 3792 if (EQ (AREF (menu_items, i), Qt))
b5e9cbb6
EZ
3793 {
3794 if (pane == 0)
3795 pane_prefix
dfe3ac02 3796 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
b5e9cbb6
EZ
3797 pane--;
3798 i += MENU_ITEMS_PANE_LENGTH;
3799 }
3800 else
3801 {
3802 if (pane == -1)
3803 {
3804 if (selidx == 0)
3805 {
3806 entry
dfe3ac02 3807 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
b5e9cbb6
EZ
3808 if (keymaps != 0)
3809 {
3810 entry = Fcons (entry, Qnil);
3811 if (!NILP (pane_prefix))
3812 entry = Fcons (pane_prefix, entry);
3813 }
3814 break;
3815 }
3816 selidx--;
3817 }
3818 i += MENU_ITEMS_ITEM_LENGTH;
3819 }
3820 }
3821 break;
3822
4a48e94d
EZ
3823 case TTYM_NEXT:
3824 case TTYM_PREV:
3825 tty_menu_new_item_coords (f, status, &item_x, &item_y);
3826 entry = Fcons (make_number (item_x), make_number (item_y));
3827 break;
3828
b5e9cbb6
EZ
3829 case TTYM_FAILURE:
3830 *error_name = "Can't activate menu";
3831 case TTYM_IA_SELECT:
3832 break;
3833 case TTYM_NO_SELECT:
3834 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3835 the menu was invoked with a mouse event as POSITION). */
3836 if (! for_click)
3837 Fsignal (Qquit, Qnil);
3838 break;
3839 }
3840
3841 unbind_to (specpdl_count, Qnil);
3842
3843 return entry;
3844}
3845
3846#endif /* HAVE_MENUS && !MSDOS */
3847
3848\f
89ba96f4 3849#ifndef MSDOS
a168702a
GM
3850/***********************************************************************
3851 Initialization
3852 ***********************************************************************/
3853
ed8dad6b
KL
3854/* Initialize the tty-dependent part of frame F. The frame must
3855 already have its device initialized. */
3224dac1 3856
dfcf069d 3857void
ed8dad6b 3858create_tty_output (struct frame *f)
28d440ab 3859{
38182d90 3860 struct tty_output *t = xzalloc (sizeof *t);
ed8dad6b 3861
9c253307 3862 eassert (FRAME_TERMCAP_P (f));
428a555e 3863
6ed8eeff 3864 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
bedb9c0e 3865
ed8dad6b 3866 f->output_data.tty = t;
3224dac1
KL
3867}
3868
9f215d25 3869/* Delete frame F's face cache, and its tty-dependent part. */
da8e1115 3870
ed8dad6b 3871static void
9f215d25 3872tty_free_frame_resources (struct frame *f)
3224dac1 3873{
9c253307 3874 eassert (FRAME_TERMCAP_P (f));
3224dac1 3875
9f215d25
CY
3876 if (FRAME_FACE_CACHE (f))
3877 free_frame_faces (f);
3878
ed8dad6b 3879 xfree (f->output_data.tty);
28d440ab
KL
3880}
3881
89ba96f4
EZ
3882#else /* MSDOS */
3883
3884/* Delete frame F's face cache. */
3885
3886static void
3887tty_free_frame_resources (struct frame *f)
3888{
9c253307 3889 eassert (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
89ba96f4
EZ
3890
3891 if (FRAME_FACE_CACHE (f))
3892 free_frame_faces (f);
3893}
3894#endif /* MSDOS */
ed8dad6b 3895\f
47cc8819 3896/* Reset the hooks in TERMINAL. */
ed8dad6b 3897
4a665758
KL
3898static void
3899clear_tty_hooks (struct terminal *terminal)
3900{
3901 terminal->rif = 0;
3902 terminal->cursor_to_hook = 0;
3903 terminal->raw_cursor_to_hook = 0;
3904 terminal->clear_to_end_hook = 0;
3905 terminal->clear_frame_hook = 0;
3906 terminal->clear_end_of_line_hook = 0;
3907 terminal->ins_del_lines_hook = 0;
3908 terminal->insert_glyphs_hook = 0;
3909 terminal->write_glyphs_hook = 0;
3910 terminal->delete_glyphs_hook = 0;
3911 terminal->ring_bell_hook = 0;
3912 terminal->reset_terminal_modes_hook = 0;
3913 terminal->set_terminal_modes_hook = 0;
3914 terminal->update_begin_hook = 0;
3915 terminal->update_end_hook = 0;
3916 terminal->set_terminal_window_hook = 0;
3917 terminal->mouse_position_hook = 0;
3918 terminal->frame_rehighlight_hook = 0;
3919 terminal->frame_raise_lower_hook = 0;
974b73e8 3920 terminal->fullscreen_hook = 0;
4a665758
KL
3921 terminal->set_vertical_scroll_bar_hook = 0;
3922 terminal->condemn_scroll_bars_hook = 0;
3923 terminal->redeem_scroll_bar_hook = 0;
3924 terminal->judge_scroll_bars_hook = 0;
3925 terminal->read_socket_hook = 0;
3926 terminal->frame_up_to_date_hook = 0;
3927
3928 /* Leave these two set, or suspended frames are not deleted
3929 correctly. */
9f215d25 3930 terminal->delete_frame_hook = &tty_free_frame_resources;
4a665758
KL
3931 terminal->delete_terminal_hook = &delete_tty;
3932}
3933
47cc8819
DN
3934/* Initialize hooks in TERMINAL with the values needed for a tty. */
3935
4a665758
KL
3936static void
3937set_tty_hooks (struct terminal *terminal)
3938{
3939 terminal->rif = 0; /* ttys don't support window-based redisplay. */
3940
3941 terminal->cursor_to_hook = &tty_cursor_to;
3942 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
3943
3944 terminal->clear_to_end_hook = &tty_clear_to_end;
3945 terminal->clear_frame_hook = &tty_clear_frame;
3946 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
3947
3948 terminal->ins_del_lines_hook = &tty_ins_del_lines;
3949
3950 terminal->insert_glyphs_hook = &tty_insert_glyphs;
3951 terminal->write_glyphs_hook = &tty_write_glyphs;
3952 terminal->delete_glyphs_hook = &tty_delete_glyphs;
3953
3954 terminal->ring_bell_hook = &tty_ring_bell;
f4d953fc 3955
4a665758
KL
3956 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
3957 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
3958 terminal->update_begin_hook = 0; /* Not needed. */
3959 terminal->update_end_hook = &tty_update_end;
3960 terminal->set_terminal_window_hook = &tty_set_terminal_window;
3961
3962 terminal->mouse_position_hook = 0; /* Not needed. */
3963 terminal->frame_rehighlight_hook = 0; /* Not needed. */
3964 terminal->frame_raise_lower_hook = 0; /* Not needed. */
3965
3966 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
3967 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
3968 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
3969 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
3970
3971 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
3972 terminal->frame_up_to_date_hook = 0; /* Not needed. */
f4d953fc 3973
9f215d25 3974 terminal->delete_frame_hook = &tty_free_frame_resources;
4a665758
KL
3975 terminal->delete_terminal_hook = &delete_tty;
3976}
3977
b8956427 3978/* If FD is the controlling terminal, drop it. */
ed8dad6b 3979static void
03a1d6bd
KL
3980dissociate_if_controlling_tty (int fd)
3981{
b8956427
PE
3982 /* If tcgetpgrp succeeds, fd is the controlling terminal,
3983 so dissociate it by invoking setsid. */
908589fd 3984 if (tcgetpgrp (fd) >= 0 && setsid () < 0)
b8956427
PE
3985 {
3986#ifdef TIOCNOTTY
3987 /* setsid failed, presumably because Emacs is already a process
3988 group leader. Fall back on the obsolescent way to dissociate
3989 a controlling tty. */
3990 block_tty_out_signal ();
3991 ioctl (fd, TIOCNOTTY, 0);
3992 unblock_tty_out_signal ();
3993#endif
3994 }
03a1d6bd
KL
3995}
3996
3224dac1
KL
3997/* Create a termcap display on the tty device with the given name and
3998 type.
3999
ab797f65 4000 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3224dac1
KL
4001 Otherwise NAME should be a path to the tty device file,
4002 e.g. "/dev/pts/7".
28d440ab 4003
3224dac1
KL
4004 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
4005
a104f656 4006 If MUST_SUCCEED is true, then all errors are fatal. */
da8e1115 4007
6ed8eeff 4008struct terminal *
653d4f43 4009init_tty (const char *name, const char *terminal_type, bool must_succeed)
08a24c47 4010{
1fc8eb33 4011 char *area;
08a24c47 4012 char **address = &area;
08a24c47 4013 int status;
59b3194c
KL
4014 struct tty_display_info *tty = NULL;
4015 struct terminal *terminal = NULL;
a104f656 4016 bool ctty = false; /* True if asked to open controlling tty. */
28d440ab 4017
3224dac1 4018 if (!terminal_type)
d31eee5e 4019 maybe_fatal (must_succeed, 0,
3224dac1
KL
4020 "Unknown terminal type",
4021 "Unknown terminal type");
b6660415 4022
ab797f65 4023 if (name == NULL)
78048085
EZ
4024 name = DEV_TTY;
4025 if (!strcmp (name, DEV_TTY))
ab797f65
KL
4026 ctty = 1;
4027
6ed8eeff
KL
4028 /* If we already have a terminal on the given device, use that. If
4029 all such terminals are suspended, create a new one instead. */
a4c6993d 4030 /* XXX Perhaps this should be made explicit by having init_tty
6ed8eeff 4031 always create a new terminal and separating terminal and frame
b6660415 4032 creation on Lisp level. */
6ed8eeff
KL
4033 terminal = get_named_tty (name);
4034 if (terminal)
4035 return terminal;
78048085 4036
6ed8eeff 4037 terminal = create_terminal ();
84704c5c
EZ
4038#ifdef MSDOS
4039 if (been_here > 0)
762b15be 4040 maybe_fatal (0, 0, "Attempt to create another terminal %s", "",
84704c5c
EZ
4041 name, "");
4042 been_here = 1;
4043 tty = &the_only_display_info;
4044#else
38182d90 4045 tty = xzalloc (sizeof *tty);
84704c5c 4046#endif
c650a5de 4047 tty->top_frame = Qnil;
3224dac1
KL
4048 tty->next = tty_list;
4049 tty_list = tty;
428a555e 4050
6ed8eeff
KL
4051 terminal->type = output_termcap;
4052 terminal->display_info.tty = tty;
4053 tty->terminal = terminal;
28d440ab 4054
38182d90 4055 tty->Wcm = xmalloc (sizeof *tty->Wcm);
7b00d185 4056 Wcm_clear (tty);
daf01701 4057
dce4c2ac
DN
4058 encode_terminal_src_size = 0;
4059 encode_terminal_dst_size = 0;
4060
51b59d79 4061
84704c5c 4062#ifndef DOS_NT
4a665758 4063 set_tty_hooks (terminal);
78048085 4064
59b3194c 4065 {
49cdacda 4066 /* Open the terminal device. */
0c72d684 4067
49cdacda
PE
4068 /* If !ctty, don't recognize it as our controlling terminal, and
4069 don't make it the controlling tty if we don't have one now.
4070
4071 Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
4072 defined on Hurd. On other systems, we need to explicitly
4073 dissociate ourselves from the controlling tty when we want to
4074 open a frame on the same terminal. */
4075 int flags = O_RDWR | O_NOCTTY | (ctty ? 0 : O_IGNORE_CTTY);
4076 int fd = emacs_open (name, flags, 0);
ef30e638 4077 tty->input = tty->output = fd < 0 || ! isatty (fd) ? 0 : fdopen (fd, "w+");
0c72d684 4078
ef30e638 4079 if (! tty->input)
59b3194c 4080 {
ef30e638
PE
4081 char const *diagnostic
4082 = tty->input ? "Not a tty device: %s" : "Could not open file: %s";
4083 emacs_close (fd);
4084 maybe_fatal (must_succeed, terminal, diagnostic, diagnostic, name);
59b3194c 4085 }
0c72d684 4086
ef30e638
PE
4087 tty->name = xstrdup (name);
4088 terminal->name = xstrdup (name);
4089
49cdacda 4090 if (!O_IGNORE_CTTY && !ctty)
03a1d6bd 4091 dissociate_if_controlling_tty (fd);
59b3194c 4092 }
78048085 4093
28d7d09f 4094 tty->type = xstrdup (terminal_type);
28d440ab 4095
819b8f00 4096 add_keyboard_wait_descriptor (fileno (tty->input));
08a24c47 4097
6548cf00 4098 Wcm_clear (tty);
08a24c47 4099
03a1d6bd 4100 /* On some systems, tgetent tries to access the controlling
a104f656 4101 terminal. */
b8956427
PE
4102 block_tty_out_signal ();
4103 status = tgetent (tty->termcap_term_buffer, terminal_type);
1fc8eb33
PE
4104 if (tty->termcap_term_buffer[TERMCAP_BUFFER_SIZE - 1])
4105 emacs_abort ();
b8956427 4106 unblock_tty_out_signal ();
78048085 4107
08a24c47 4108 if (status < 0)
b0347178
KH
4109 {
4110#ifdef TERMINFO
d31eee5e 4111 maybe_fatal (must_succeed, terminal,
3224dac1
KL
4112 "Cannot open terminfo database file",
4113 "Cannot open terminfo database file");
b0347178 4114#else
d31eee5e 4115 maybe_fatal (must_succeed, terminal,
3224dac1
KL
4116 "Cannot open termcap database file",
4117 "Cannot open termcap database file");
b0347178
KH
4118#endif
4119 }
08a24c47 4120 if (status == 0)
b0347178 4121 {
d31eee5e 4122 maybe_fatal (must_succeed, terminal,
3224dac1
KL
4123 "Terminal type %s is not defined",
4124 "Terminal type %s is not defined.\n\
b0347178
KH
4125If that is not the actual type of terminal you have,\n\
4126use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
84164a0d
SM
4127`setenv TERM ...') to specify the correct type. It may be necessary\n"
4128#ifdef TERMINFO
4129"to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
b0347178 4130#else
84164a0d 4131"to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
b0347178 4132#endif
84164a0d 4133 terminal_type);
b0347178 4134 }
6b61353c 4135
1fc8eb33 4136 area = tty->termcap_strings_buffer;
fca177d4
KL
4137 tty->TS_ins_line = tgetstr ("al", address);
4138 tty->TS_ins_multi_lines = tgetstr ("AL", address);
4139 tty->TS_bell = tgetstr ("bl", address);
6548cf00 4140 BackTab (tty) = tgetstr ("bt", address);
fca177d4
KL
4141 tty->TS_clr_to_bottom = tgetstr ("cd", address);
4142 tty->TS_clr_line = tgetstr ("ce", address);
4143 tty->TS_clr_frame = tgetstr ("cl", address);
6548cf00
KL
4144 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
4145 AbsPosition (tty) = tgetstr ("cm", address);
4146 CR (tty) = tgetstr ("cr", address);
fca177d4
KL
4147 tty->TS_set_scroll_region = tgetstr ("cs", address);
4148 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
6548cf00 4149 RowPosition (tty) = tgetstr ("cv", address);
fca177d4
KL
4150 tty->TS_del_char = tgetstr ("dc", address);
4151 tty->TS_del_multi_chars = tgetstr ("DC", address);
4152 tty->TS_del_line = tgetstr ("dl", address);
4153 tty->TS_del_multi_lines = tgetstr ("DL", address);
4154 tty->TS_delete_mode = tgetstr ("dm", address);
4155 tty->TS_end_delete_mode = tgetstr ("ed", address);
4156 tty->TS_end_insert_mode = tgetstr ("ei", address);
6548cf00 4157 Home (tty) = tgetstr ("ho", address);
fca177d4
KL
4158 tty->TS_ins_char = tgetstr ("ic", address);
4159 tty->TS_ins_multi_chars = tgetstr ("IC", address);
4160 tty->TS_insert_mode = tgetstr ("im", address);
4161 tty->TS_pad_inserted_char = tgetstr ("ip", address);
4162 tty->TS_end_keypad_mode = tgetstr ("ke", address);
4163 tty->TS_keypad_mode = tgetstr ("ks", address);
6548cf00
KL
4164 LastLine (tty) = tgetstr ("ll", address);
4165 Right (tty) = tgetstr ("nd", address);
4166 Down (tty) = tgetstr ("do", address);
4167 if (!Down (tty))
a104f656 4168 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do". */
08a24c47 4169 if (tgetflag ("bs"))
a104f656
SM
4170 Left (tty) = "\b"; /* Can't possibly be longer! */
4171 else /* (Actually, "bs" is obsolete...) */
6548cf00
KL
4172 Left (tty) = tgetstr ("le", address);
4173 if (!Left (tty))
a104f656 4174 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le". */
fca177d4
KL
4175 tty->TS_pad_char = tgetstr ("pc", address);
4176 tty->TS_repeat = tgetstr ("rp", address);
4177 tty->TS_end_standout_mode = tgetstr ("se", address);
4178 tty->TS_fwd_scroll = tgetstr ("sf", address);
4179 tty->TS_standout_mode = tgetstr ("so", address);
4180 tty->TS_rev_scroll = tgetstr ("sr", address);
6548cf00 4181 tty->Wcm->cm_tab = tgetstr ("ta", address);
fca177d4
KL
4182 tty->TS_end_termcap_modes = tgetstr ("te", address);
4183 tty->TS_termcap_modes = tgetstr ("ti", address);
6548cf00 4184 Up (tty) = tgetstr ("up", address);
fca177d4
KL
4185 tty->TS_visible_bell = tgetstr ("vb", address);
4186 tty->TS_cursor_normal = tgetstr ("ve", address);
4187 tty->TS_cursor_visible = tgetstr ("vs", address);
4188 tty->TS_cursor_invisible = tgetstr ("vi", address);
4189 tty->TS_set_window = tgetstr ("wi", address);
4190
4191 tty->TS_enter_underline_mode = tgetstr ("us", address);
4192 tty->TS_exit_underline_mode = tgetstr ("ue", address);
4193 tty->TS_enter_bold_mode = tgetstr ("md", address);
cd4eb164 4194 tty->TS_enter_italic_mode = tgetstr ("ZH", address);
fca177d4 4195 tty->TS_enter_dim_mode = tgetstr ("mh", address);
fca177d4
KL
4196 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
4197 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
4198 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
4199 tty->TS_exit_attribute_mode = tgetstr ("me", address);
177c0ea7 4200
6548cf00
KL
4201 MultiUp (tty) = tgetstr ("UP", address);
4202 MultiDown (tty) = tgetstr ("DO", address);
4203 MultiLeft (tty) = tgetstr ("LE", address);
4204 MultiRight (tty) = tgetstr ("RI", address);
08a24c47 4205
5c32d3f2 4206 /* SVr4/ANSI color support. If "op" isn't available, don't support
e7f90eab
GM
4207 color because we can't switch back to the default foreground and
4208 background. */
fca177d4
KL
4209 tty->TS_orig_pair = tgetstr ("op", address);
4210 if (tty->TS_orig_pair)
a168702a 4211 {
fca177d4
KL
4212 tty->TS_set_foreground = tgetstr ("AF", address);
4213 tty->TS_set_background = tgetstr ("AB", address);
4214 if (!tty->TS_set_foreground)
e7f90eab
GM
4215 {
4216 /* SVr4. */
fca177d4
KL
4217 tty->TS_set_foreground = tgetstr ("Sf", address);
4218 tty->TS_set_background = tgetstr ("Sb", address);
e7f90eab 4219 }
177c0ea7 4220
fca177d4
KL
4221 tty->TN_max_colors = tgetnum ("Co");
4222 tty->TN_max_pairs = tgetnum ("pa");
177c0ea7 4223
fca177d4
KL
4224 tty->TN_no_color_video = tgetnum ("NC");
4225 if (tty->TN_no_color_video == -1)
4226 tty->TN_no_color_video = 0;
a168702a 4227 }
a168702a 4228
fca177d4 4229 tty_default_color_capabilities (tty, 1);
ace28297 4230
6548cf00 4231 MagicWrap (tty) = tgetflag ("xn");
e4058338
KH
4232 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
4233 the former flag imply the latter. */
6548cf00 4234 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
6ed8eeff 4235 terminal->memory_below_frame = tgetflag ("db");
fca177d4 4236 tty->TF_hazeltine = tgetflag ("hz");
6ed8eeff 4237 terminal->must_write_spaces = tgetflag ("in");
fca177d4
KL
4238 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
4239 tty->TF_insmode_motion = tgetflag ("mi");
4240 tty->TF_standout_motion = tgetflag ("ms");
4241 tty->TF_underscore = tgetflag ("ul");
4242 tty->TF_teleray = tgetflag ("xt");
08a24c47 4243
dce4c2ac
DN
4244#else /* DOS_NT */
4245#ifdef WINDOWSNT
4246 {
4247 struct frame *f = XFRAME (selected_frame);
9337e206 4248 int height, width;
dce4c2ac 4249
9337e206 4250 initialize_w32_display (terminal, &width, &height);
dce4c2ac 4251
9337e206
EZ
4252 FrameRows (tty) = height;
4253 FrameCols (tty) = width;
4254 tty->specified_window = height;
dce4c2ac 4255
dce4c2ac
DN
4256 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
4257 terminal->char_ins_del_ok = 1;
4258 baud_rate = 19200;
4259 }
4260#else /* MSDOS */
4261 {
4262 int height, width;
4263 if (strcmp (terminal_type, "internal") == 0)
4264 terminal->type = output_msdos_raw;
4265 initialize_msdos_display (terminal);
4266
4267 get_tty_size (fileno (tty->input), &width, &height);
4268 FrameCols (tty) = width;
4269 FrameRows (tty) = height;
4270 terminal->char_ins_del_ok = 0;
4271 init_baud_rate (fileno (tty->input));
4272 }
4273#endif /* MSDOS */
4274 tty->output = stdout;
4275 tty->input = stdin;
4276 /* The following two are inaccessible from w32console.c. */
4277 terminal->delete_frame_hook = &tty_free_frame_resources;
4278 terminal->delete_terminal_hook = &delete_tty;
4279
4280 tty->name = xstrdup (name);
4281 terminal->name = xstrdup (name);
4282 tty->type = xstrdup (terminal_type);
4283
4284 add_keyboard_wait_descriptor (0);
4285
dce4c2ac
DN
4286 tty->delete_in_insert_mode = 1;
4287
4288 UseTabs (tty) = 0;
4289 terminal->scroll_region_ok = 0;
4290
4291 /* Seems to insert lines when it's not supposed to, messing up the
4292 display. In doing a trace, it didn't seem to be called much, so I
4293 don't think we're losing anything by turning it off. */
4294 terminal->line_ins_del_ok = 0;
4295
a104f656 4296 tty->TN_max_colors = 16; /* Must be non-zero for tty-display-color-p. */
dce4c2ac
DN
4297#endif /* DOS_NT */
4298
6bc8cd65
JB
4299#ifdef HAVE_GPM
4300 terminal->mouse_position_hook = term_mouse_position;
4301 tty->mouse_highlight.mouse_face_window = Qnil;
4302#endif
4303
1afcba63 4304 terminal->kboard = allocate_kboard (Qnil);
6ed8eeff 4305 terminal->kboard->reference_count++;
e7cf0fa0
KL
4306 /* Don't let the initial kboard remain current longer than necessary.
4307 That would cause problems if a file loaded on startup tries to
4308 prompt in the mini-buffer. */
4309 if (current_kboard == initial_kboard)
6ed8eeff 4310 current_kboard = terminal->kboard;
84704c5c 4311#ifndef DOS_NT
6ed8eeff 4312 term_get_fkeys (address, terminal->kboard);
5c2c7893 4313
ff11dfa1 4314 /* Get frame size from system, or else from termcap. */
3b12ce12
RS
4315 {
4316 int height, width;
0b0d3e0b 4317 get_tty_size (fileno (tty->input), &width, &height);
0a125897
KL
4318 FrameCols (tty) = width;
4319 FrameRows (tty) = height;
3b12ce12
RS
4320 }
4321
0a125897
KL
4322 if (FrameCols (tty) <= 0)
4323 FrameCols (tty) = tgetnum ("co");
4324 if (FrameRows (tty) <= 0)
4325 FrameRows (tty) = tgetnum ("li");
177c0ea7 4326
0a125897 4327 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
d31eee5e 4328 maybe_fatal (must_succeed, terminal,
b3ffc17c 4329 "Screen size %dx%d is too small",
3224dac1 4330 "Screen size %dx%d is too small",
0a125897 4331 FrameCols (tty), FrameRows (tty));
ee7a2de4 4332
6548cf00 4333 TabWidth (tty) = tgetnum ("tw");
08a24c47 4334
fca177d4
KL
4335 if (!tty->TS_bell)
4336 tty->TS_bell = "\07";
08a24c47 4337
fca177d4
KL
4338 if (!tty->TS_fwd_scroll)
4339 tty->TS_fwd_scroll = Down (tty);
08a24c47 4340
fca177d4 4341 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
08a24c47 4342
6548cf00
KL
4343 if (TabWidth (tty) < 0)
4344 TabWidth (tty) = 8;
177c0ea7 4345
08a24c47
JB
4346/* Turned off since /etc/termcap seems to have :ta= for most terminals
4347 and newer termcap doc does not seem to say there is a default.
6548cf00
KL
4348 if (!tty->Wcm->cm_tab)
4349 tty->Wcm->cm_tab = "\t";
08a24c47
JB
4350*/
4351
54800acb
MB
4352 /* We don't support standout modes that use `magic cookies', so
4353 turn off any that do. */
fca177d4 4354 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
54800acb 4355 {
fca177d4
KL
4356 tty->TS_standout_mode = 0;
4357 tty->TS_end_standout_mode = 0;
54800acb 4358 }
fca177d4 4359 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
54800acb 4360 {
fca177d4
KL
4361 tty->TS_enter_underline_mode = 0;
4362 tty->TS_exit_underline_mode = 0;
54800acb
MB
4363 }
4364
4365 /* If there's no standout mode, try to use underlining instead. */
fca177d4 4366 if (tty->TS_standout_mode == 0)
08a24c47 4367 {
fca177d4
KL
4368 tty->TS_standout_mode = tty->TS_enter_underline_mode;
4369 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
08a24c47
JB
4370 }
4371
afd359c4
RS
4372 /* If no `se' string, try using a `me' string instead.
4373 If that fails, we can't use standout mode at all. */
fca177d4 4374 if (tty->TS_end_standout_mode == 0)
afd359c4 4375 {
e4bfb3b6 4376 char *s = tgetstr ("me", address);
afd359c4 4377 if (s != 0)
fca177d4 4378 tty->TS_end_standout_mode = s;
afd359c4 4379 else
fca177d4 4380 tty->TS_standout_mode = 0;
afd359c4
RS
4381 }
4382
fca177d4 4383 if (tty->TF_teleray)
08a24c47 4384 {
6548cf00 4385 tty->Wcm->cm_tab = 0;
54800acb 4386 /* We can't support standout mode, because it uses magic cookies. */
fca177d4 4387 tty->TS_standout_mode = 0;
a104f656 4388 /* But that means we cannot rely on ^M to go to column zero! */
6548cf00 4389 CR (tty) = 0;
a104f656
SM
4390 /* LF can't be trusted either -- can alter hpos. */
4391 /* If move at column 0 thru a line with TS_standout_mode. */
6548cf00 4392 Down (tty) = 0;
08a24c47
JB
4393 }
4394
0a125897 4395 tty->specified_window = FrameRows (tty);
08a24c47 4396
a104f656 4397 if (Wcm_init (tty) == -1) /* Can't do cursor motion. */
3224dac1 4398 {
d31eee5e 4399 maybe_fatal (must_succeed, terminal,
3224dac1 4400 "Terminal type \"%s\" is not powerful enough to run Emacs",
3224dac1 4401 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
37dad45a
RS
4402It lacks the ability to position the cursor.\n\
4403If that is not the actual type of terminal you have,\n\
4404use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
84164a0d
SM
4405`setenv TERM ...') to specify the correct type. It may be necessary\n"
4406# ifdef TERMINFO
4407"to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
37dad45a 4408# else /* TERMCAP */
84164a0d 4409"to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
37dad45a 4410# endif /* TERMINFO */
3224dac1 4411 terminal_type);
fca177d4 4412 }
daf01701 4413
0a125897 4414 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
d31eee5e 4415 maybe_fatal (must_succeed, terminal,
3224dac1
KL
4416 "Could not determine the frame size",
4417 "Could not determine the frame size");
08a24c47 4418
fca177d4
KL
4419 tty->delete_in_insert_mode
4420 = tty->TS_delete_mode && tty->TS_insert_mode
4421 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
08a24c47 4422
0b0d3e0b 4423 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
08a24c47 4424
6ed8eeff 4425 terminal->scroll_region_ok
6548cf00 4426 = (tty->Wcm->cm_abs
fca177d4 4427 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
08a24c47 4428
6ed8eeff 4429 terminal->line_ins_del_ok
fca177d4
KL
4430 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
4431 && (tty->TS_del_line || tty->TS_del_multi_lines))
6ed8eeff 4432 || (terminal->scroll_region_ok
fca177d4 4433 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
08a24c47 4434
6ed8eeff 4435 terminal->char_ins_del_ok
fca177d4
KL
4436 = ((tty->TS_ins_char || tty->TS_insert_mode
4437 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
4438 && (tty->TS_del_char || tty->TS_del_multi_chars));
08a24c47 4439
6ed8eeff 4440 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
08a24c47 4441
0b0d3e0b 4442 init_baud_rate (fileno (tty->input));
20a558dc 4443
84704c5c 4444#endif /* not DOS_NT */
635e3b29 4445
0a125897
KL
4446 /* Init system terminal modes (RAW or CBREAK, etc.). */
4447 init_sys_modes (tty);
daf01701 4448
6ed8eeff 4449 return terminal;
08a24c47
JB
4450}
4451
b3ffc17c
DN
4452
4453static void
4454vfatal (const char *str, va_list ap)
4455{
4456 fprintf (stderr, "emacs: ");
4457 vfprintf (stderr, str, ap);
4458 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
4459 fprintf (stderr, "\n");
b3ffc17c
DN
4460 fflush (stderr);
4461 exit (1);
4462}
4463
4464
a4c6993d 4465/* Auxiliary error-handling function for init_tty.
d31eee5e 4466 Delete TERMINAL, then call error or fatal with str1 or str2,
653d4f43 4467 respectively, according to whether MUST_SUCCEED is true. */
6b61353c 4468
3224dac1 4469static void
653d4f43 4470maybe_fatal (bool must_succeed, struct terminal *terminal,
b3ffc17c 4471 const char *str1, const char *str2, ...)
3224dac1 4472{
b3ffc17c
DN
4473 va_list ap;
4474 va_start (ap, str2);
6ed8eeff
KL
4475 if (terminal)
4476 delete_tty (terminal);
f4d953fc 4477
3224dac1 4478 if (must_succeed)
b3ffc17c 4479 vfatal (str2, ap);
3224dac1 4480 else
b3ffc17c 4481 verror (str1, ap);
08a24c47
JB
4482}
4483
dfcf069d 4484void
7c401d15 4485fatal (const char *str, ...)
08a24c47 4486{
7c401d15
DN
4487 va_list ap;
4488 va_start (ap, str);
b3ffc17c 4489 vfatal (str, ap);
08a24c47 4490}
07c57952 4491
819b8f00
KL
4492\f
4493
6ed8eeff 4494/* Delete the given tty terminal, closing all frames on it. */
da8e1115 4495
ed8dad6b 4496static void
6ed8eeff 4497delete_tty (struct terminal *terminal)
072d84a6 4498{
428a555e 4499 struct tty_display_info *tty;
f4d953fc 4500
e2749141 4501 /* Protect against recursive calls. delete_frame in
ab797f65 4502 delete_terminal calls us back when it deletes our last frame. */
59a7bc63 4503 if (!terminal->name)
0a125897 4504 return;
fca177d4 4505
9c253307 4506 eassert (terminal->type == output_termcap);
428a555e 4507
6ed8eeff 4508 tty = terminal->display_info.tty;
f4d953fc 4509
fca177d4
KL
4510 if (tty == tty_list)
4511 tty_list = tty->next;
4512 else
4513 {
28d7d09f 4514 struct tty_display_info *p;
fca177d4
KL
4515 for (p = tty_list; p && p->next != tty; p = p->next)
4516 ;
4517
4518 if (! p)
4519 /* This should not happen. */
1088b922 4520 emacs_abort ();
fca177d4 4521
0a125897
KL
4522 p->next = tty->next;
4523 tty->next = 0;
fca177d4
KL
4524 }
4525
7e59217d 4526 /* reset_sys_modes needs a valid device, so this call needs to be
6ed8eeff 4527 before delete_terminal. */
04c3243c 4528 reset_sys_modes (tty);
fca177d4 4529
6ed8eeff 4530 delete_terminal (terminal);
3224dac1 4531
70fdbb46
JM
4532 xfree (tty->name);
4533 xfree (tty->type);
daf01701 4534
fca177d4 4535 if (tty->input)
819b8f00
KL
4536 {
4537 delete_keyboard_wait_descriptor (fileno (tty->input));
4538 if (tty->input != stdin)
4539 fclose (tty->input);
4540 }
4541 if (tty->output && tty->output != stdout && tty->output != tty->input)
fca177d4
KL
4542 fclose (tty->output);
4543 if (tty->termscript)
4544 fclose (tty->termscript);
daf01701 4545
70fdbb46
JM
4546 xfree (tty->old_tty);
4547 xfree (tty->Wcm);
fca177d4 4548 xfree (tty);
fca177d4
KL
4549}
4550
dfcf069d 4551void
d3da34e0 4552syms_of_term (void)
07c57952 4553{
29208e82 4554 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo,
7ee72033 4555 doc: /* Non-nil means the system uses terminfo rather than termcap.
228299fa 4556This variable can be used by terminal emulator packages. */);
07c57952
KH
4557#ifdef TERMINFO
4558 system_uses_terminfo = 1;
4559#else
4560 system_uses_terminfo = 0;
4561#endif
c291d9ef 4562
29208e82 4563 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions,
e5d9c0d1 4564 doc: /* Functions run after suspending a tty.
fdc496e7 4565The functions are run with one argument, the terminal object to be suspended.
0b0d3e0b 4566See `suspend-tty'. */);
e4019195 4567 Vsuspend_tty_functions = Qnil;
0b0d3e0b
KL
4568
4569
29208e82 4570 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions,
e5d9c0d1 4571 doc: /* Functions run after resuming a tty.
fdc496e7 4572The functions are run with one argument, the terminal object that was revived.
0b0d3e0b 4573See `resume-tty'. */);
e4019195 4574 Vresume_tty_functions = Qnil;
a168702a 4575
29208e82 4576 DEFVAR_BOOL ("visible-cursor", visible_cursor,
0db017c0
SM
4577 doc: /* Non-nil means to make the cursor very visible.
4578This only has an effect when running in a text terminal.
4579What means \"very visible\" is up to your terminal. It may make the cursor
4580bigger, or it may make it blink, or it may do nothing at all. */);
4581 visible_cursor = 1;
4582
a168702a 4583 defsubr (&Stty_display_color_p);
bfa62f96 4584 defsubr (&Stty_display_color_cells);
072d84a6 4585 defsubr (&Stty_no_underline);
6ed8eeff
KL
4586 defsubr (&Stty_type);
4587 defsubr (&Scontrolling_tty_p);
c6bf3022 4588 defsubr (&Stty_top_frame);
0b0d3e0b
KL
4589 defsubr (&Ssuspend_tty);
4590 defsubr (&Sresume_tty);
7e5a23bd 4591#ifdef HAVE_GPM
6178ce5e
SM
4592 defsubr (&Sgpm_mouse_start);
4593 defsubr (&Sgpm_mouse_stop);
7e5a23bd 4594#endif /* HAVE_GPM */
3a7c5d40 4595
84704c5c 4596#ifndef DOS_NT
3a7c5d40
CY
4597 default_orig_pair = NULL;
4598 default_set_foreground = NULL;
4599 default_set_background = NULL;
84704c5c 4600#endif /* !DOS_NT */
d31eee5e
CY
4601
4602 encode_terminal_src = NULL;
4603 encode_terminal_dst = NULL;
f0177f86 4604
df782309
EZ
4605 DEFSYM (Qtty_menu_next_item, "tty-menu-next-item");
4606 DEFSYM (Qtty_menu_prev_item, "tty-menu-prev-item");
4607 DEFSYM (Qtty_menu_next_menu, "tty-menu-next-menu");
4608 DEFSYM (Qtty_menu_prev_menu, "tty-menu-prev-menu");
4609 DEFSYM (Qtty_menu_select, "tty-menu-select");
4610 DEFSYM (Qtty_menu_ignore, "tty-menu-ignore");
4611 DEFSYM (Qtty_menu_exit, "tty-menu-exit");
e648f699 4612 DEFSYM (Qtty_menu_mouse_movement, "tty-menu-mouse-movement");
df782309 4613 DEFSYM (Qtty_menu_navigation_map, "tty-menu-navigation-map");
07c57952 4614}