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