Implemented multiple tty support.
[bpt/emacs.git] / src / term.c
CommitLineData
d284f58f 1/* Terminal control module for terminals described by TERMCAP
4a8130f2 2 Copyright (C) 1985, 86, 87, 93, 94, 95, 98, 2000, 2001, 2002
d284f58f 3 Free Software Foundation, Inc.
08a24c47
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
08a24c47
JB
10any later version.
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
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
08a24c47 21
d284f58f 22/* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
08a24c47 23
565620a5 24#include <config.h>
08a24c47
JB
25#include <stdio.h>
26#include <ctype.h>
a168702a 27#include <string.h>
7ee72033 28
28d440ab
KL
29#include <sys/file.h>
30
31#include "systty.h" /* For emacs_tty in termchar.h */
08a24c47
JB
32#include "termchar.h"
33#include "termopts.h"
08a24c47 34#include "lisp.h"
a4decb7f
KH
35#include "charset.h"
36#include "coding.h"
2538fae4 37#include "keyboard.h"
ff11dfa1 38#include "frame.h"
08a24c47
JB
39#include "disptab.h"
40#include "termhooks.h"
dfcf069d 41#include "dispextern.h"
a168702a 42#include "window.h"
8feddab4 43#include "keymap.h"
a168702a 44
ff23e1dd
GM
45/* For now, don't try to include termcap.h. On some systems,
46 configure finds a non-standard termcap.h that the main build
47 won't find. */
48
49#if defined HAVE_TERMCAP_H && 0
b0f61f15 50#include <termcap.h>
46d1b2bc
DL
51#else
52extern void tputs P_ ((const char *, int, int (*)(int)));
53extern int tgetent P_ ((char *, const char *));
54extern int tgetflag P_ ((char *id));
55extern int tgetnum P_ ((char *id));
b0f61f15
GM
56#endif
57
eccec691 58#include "cm.h"
dfcf069d
AS
59#ifdef HAVE_X_WINDOWS
60#include "xterm.h"
61#endif
e0f712ba 62#ifdef MAC_OS
1a578e9b
AC
63#include "macterm.h"
64#endif
c8951b18 65
28d440ab
KL
66#ifndef O_RDWR
67#define O_RDWR 2
68#endif
69
a168702a
GM
70static void turn_on_face P_ ((struct frame *, int face_id));
71static void turn_off_face P_ ((struct frame *, int face_id));
72static void tty_show_cursor P_ ((void));
73static void tty_hide_cursor P_ ((void));
74
28d440ab
KL
75#define OUTPUT(tty, a) \
76 emacs_tputs ((tty), a, (int) (FRAME_LINES (XFRAME (selected_frame)) - curY), cmputc)
77#define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
78#define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
a168702a 79
28d440ab 80#define OUTPUT_IF(tty, a) \
b0f61f15 81 do { \
28d440ab
KL
82 if (a) \
83 emacs_tputs ((tty), a, (int) (FRAME_LINES (XFRAME (selected_frame)) \
b0f61f15
GM
84 - curY), cmputc); \
85 } while (0)
177c0ea7 86
28d440ab 87#define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
08a24c47 88
c291d9ef 89/* Function to use to ring the bell. */
a168702a 90
c291d9ef
RS
91Lisp_Object Vring_bell_function;
92
8a56675d 93/* Terminal characteristics that higher levels want to look at. */
28d440ab
KL
94
95struct tty_output *tty_list;
08a24c47 96
ff11dfa1 97/* Nonzero means no need to redraw the entire frame on resuming
08a24c47
JB
98 a suspended Emacs. This is useful on terminals with multiple pages,
99 where one page is used for Emacs and another for all else. */
100int no_redraw_on_reenter;
101
102/* Hook functions that you can set to snap out the functions in this file.
103 These are all extern'd in termhooks.h */
104
dfcf069d
AS
105void (*cursor_to_hook) P_ ((int, int));
106void (*raw_cursor_to_hook) P_ ((int, int));
dfcf069d
AS
107void (*clear_to_end_hook) P_ ((void));
108void (*clear_frame_hook) P_ ((void));
109void (*clear_end_of_line_hook) P_ ((int));
08a24c47 110
dfcf069d 111void (*ins_del_lines_hook) P_ ((int, int));
08a24c47 112
dfcf069d 113void (*delete_glyphs_hook) P_ ((int));
08a24c47 114
dfcf069d 115void (*ring_bell_hook) P_ ((void));
08a24c47 116
dfcf069d
AS
117void (*reset_terminal_modes_hook) P_ ((void));
118void (*set_terminal_modes_hook) P_ ((void));
119void (*update_begin_hook) P_ ((struct frame *));
120void (*update_end_hook) P_ ((struct frame *));
121void (*set_terminal_window_hook) P_ ((int));
a168702a
GM
122void (*insert_glyphs_hook) P_ ((struct glyph *, int));
123void (*write_glyphs_hook) P_ ((struct glyph *, int));
124void (*delete_glyphs_hook) P_ ((int));
08a24c47 125
dfcf069d 126int (*read_socket_hook) P_ ((int, struct input_event *, int, int));
08a24c47 127
dfcf069d 128void (*frame_up_to_date_hook) P_ ((struct frame *));
55cc089c 129
20a558dc 130/* Return the current position of the mouse.
371fbaeb
JB
131
132 Set *f to the frame the mouse is in, or zero if the mouse is in no
133 Emacs frame. If it is set to zero, all the other arguments are
134 garbage.
135
a3c87d4e
JB
136 If the motion started in a scroll bar, set *bar_window to the
137 scroll bar's window, *part to the part the mouse is currently over,
138 *x to the position of the mouse along the scroll bar, and *y to the
139 overall length of the scroll bar.
371fbaeb
JB
140
141 Otherwise, set *bar_window to Qnil, and *x and *y to the column and
142 row of the character cell the mouse is over.
143
144 Set *time to the time the mouse was at the returned position.
145
146 This should clear mouse_moved until the next motion
147 event arrives. */
d284f58f 148
dfcf069d 149void (*mouse_position_hook) P_ ((FRAME_PTR *f, int insist,
371fbaeb 150 Lisp_Object *bar_window,
a3c87d4e 151 enum scroll_bar_part *part,
20a558dc
JB
152 Lisp_Object *x,
153 Lisp_Object *y,
dfcf069d 154 unsigned long *time));
08a24c47 155
ff11dfa1 156/* When reading from a minibuffer in a different frame, Emacs wants
a168702a 157 to shift the highlight from the selected frame to the mini-buffer's
ff11dfa1 158 frame; under X, this means it lies about where the focus is.
2e146aa4
JB
159 This hook tells the window system code to re-decide where to put
160 the highlight. */
d284f58f 161
dfcf069d 162void (*frame_rehighlight_hook) P_ ((FRAME_PTR f));
2e146aa4 163
dbc4e1c1
JB
164/* If we're displaying frames using a window system that can stack
165 frames on top of each other, this hook allows you to bring a frame
166 to the front, or bury it behind all the other windows. If this
167 hook is zero, that means the device we're displaying on doesn't
168 support overlapping frames, so there's no need to raise or lower
169 anything.
170
171 If RAISE is non-zero, F is brought to the front, before all other
172 windows. If RAISE is zero, F is sent to the back, behind all other
173 windows. */
d284f58f 174
dfcf069d 175void (*frame_raise_lower_hook) P_ ((FRAME_PTR f, int raise));
dbc4e1c1 176
a3c87d4e 177/* Set the vertical scroll bar for WINDOW to have its upper left corner
371fbaeb
JB
178 at (TOP, LEFT), and be LENGTH rows high. Set its handle to
179 indicate that we are displaying PORTION characters out of a total
180 of WHOLE characters, starting at POSITION. If WINDOW doesn't yet
a3c87d4e 181 have a scroll bar, create one for it. */
a168702a 182
a3c87d4e 183void (*set_vertical_scroll_bar_hook)
dfcf069d
AS
184 P_ ((struct window *window,
185 int portion, int whole, int position));
20a558dc 186
371fbaeb 187
20a558dc 188/* The following three hooks are used when we're doing a thorough
a3c87d4e 189 redisplay of the frame. We don't explicitly know which scroll bars
20a558dc
JB
190 are going to be deleted, because keeping track of when windows go
191 away is a real pain - can you say set-window-configuration?
192 Instead, we just assert at the beginning of redisplay that *all*
a3c87d4e 193 scroll bars are to be removed, and then save scroll bars from the
8e6208c5 194 fiery pit when we actually redisplay their window. */
20a558dc 195
a3c87d4e
JB
196/* Arrange for all scroll bars on FRAME to be removed at the next call
197 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
177c0ea7 198 `*redeem_scroll_bar_hook' is applied to its window before the judgment.
20a558dc 199
371fbaeb 200 This should be applied to each frame each time its window tree is
a3c87d4e
JB
201 redisplayed, even if it is not displaying scroll bars at the moment;
202 if the HAS_SCROLL_BARS flag has just been turned off, only calling
203 this and the judge_scroll_bars_hook will get rid of them.
371fbaeb
JB
204
205 If non-zero, this hook should be safe to apply to any frame,
a3c87d4e 206 whether or not it can support scroll bars, and whether or not it is
371fbaeb 207 currently displaying them. */
d284f58f 208
dfcf069d 209void (*condemn_scroll_bars_hook) P_ ((FRAME_PTR frame));
371fbaeb 210
a3c87d4e
JB
211/* Unmark WINDOW's scroll bar for deletion in this judgement cycle.
212 Note that it's okay to redeem a scroll bar that is not condemned. */
d284f58f 213
dfcf069d 214void (*redeem_scroll_bar_hook) P_ ((struct window *window));
20a558dc 215
a3c87d4e 216/* Remove all scroll bars on FRAME that haven't been saved since the
177c0ea7 217 last call to `*condemn_scroll_bars_hook'.
20a558dc 218
371fbaeb 219 This should be applied to each frame after each time its window
a3c87d4e
JB
220 tree is redisplayed, even if it is not displaying scroll bars at the
221 moment; if the HAS_SCROLL_BARS flag has just been turned off, only
222 calling this and condemn_scroll_bars_hook will get rid of them.
371fbaeb
JB
223
224 If non-zero, this hook should be safe to apply to any frame,
a3c87d4e 225 whether or not it can support scroll bars, and whether or not it is
371fbaeb 226 currently displaying them. */
d284f58f 227
dfcf069d 228void (*judge_scroll_bars_hook) P_ ((FRAME_PTR FRAME));
20a558dc 229
08a24c47
JB
230/* Strings, numbers and flags taken from the termcap entry. */
231
a4decb7f 232char *TS_ins_line; /* "al" */
08a24c47
JB
233char *TS_ins_multi_lines; /* "AL" (one parameter, # lines to insert) */
234char *TS_bell; /* "bl" */
235char *TS_clr_to_bottom; /* "cd" */
236char *TS_clr_line; /* "ce", clear to end of line */
ff11dfa1 237char *TS_clr_frame; /* "cl" */
08a24c47
JB
238char *TS_set_scroll_region; /* "cs" (2 params, first line and last line) */
239char *TS_set_scroll_region_1; /* "cS" (4 params: total lines,
240 lines above scroll region, lines below it,
241 total lines again) */
242char *TS_del_char; /* "dc" */
243char *TS_del_multi_chars; /* "DC" (one parameter, # chars to delete) */
244char *TS_del_line; /* "dl" */
245char *TS_del_multi_lines; /* "DL" (one parameter, # lines to delete) */
246char *TS_delete_mode; /* "dm", enter character-delete mode */
247char *TS_end_delete_mode; /* "ed", leave character-delete mode */
248char *TS_end_insert_mode; /* "ei", leave character-insert mode */
249char *TS_ins_char; /* "ic" */
250char *TS_ins_multi_chars; /* "IC" (one parameter, # chars to insert) */
251char *TS_insert_mode; /* "im", enter character-insert mode */
252char *TS_pad_inserted_char; /* "ip". Just padding, no commands. */
253char *TS_end_keypad_mode; /* "ke" */
254char *TS_keypad_mode; /* "ks" */
255char *TS_pad_char; /* "pc", char to use as padding */
256char *TS_repeat; /* "rp" (2 params, # times to repeat
257 and character to be repeated) */
258char *TS_end_standout_mode; /* "se" */
259char *TS_fwd_scroll; /* "sf" */
260char *TS_standout_mode; /* "so" */
261char *TS_rev_scroll; /* "sr" */
262char *TS_end_termcap_modes; /* "te" */
263char *TS_termcap_modes; /* "ti" */
264char *TS_visible_bell; /* "vb" */
a168702a
GM
265char *TS_cursor_normal; /* "ve" */
266char *TS_cursor_visible; /* "vs" */
267char *TS_cursor_invisible; /* "vi" */
08a24c47
JB
268char *TS_set_window; /* "wi" (4 params, start and end of window,
269 each as vpos and hpos) */
270
4e6ba4a4
GM
271/* Value of the "NC" (no_color_video) capability, or 0 if not
272 present. */
273
274static int TN_no_color_video;
275
276/* Meaning of bits in no_color_video. Each bit set means that the
277 corresponding attribute cannot be combined with colors. */
278
279enum no_color_bit
280{
281 NC_STANDOUT = 1 << 0,
282 NC_UNDERLINE = 1 << 1,
283 NC_REVERSE = 1 << 2,
284 NC_BLINK = 1 << 3,
285 NC_DIM = 1 << 4,
286 NC_BOLD = 1 << 5,
287 NC_INVIS = 1 << 6,
288 NC_PROTECT = 1 << 7,
289 NC_ALT_CHARSET = 1 << 8
290};
291
a168702a
GM
292/* "md" -- turn on bold (extra bright mode). */
293
294char *TS_enter_bold_mode;
295
296/* "mh" -- turn on half-bright mode. */
297
298char *TS_enter_dim_mode;
299
300/* "mb" -- enter blinking mode. */
301
302char *TS_enter_blink_mode;
303
304/* "mr" -- enter reverse video mode. */
305
306char *TS_enter_reverse_mode;
307
308/* "us"/"ue" -- start/end underlining. */
309
310char *TS_exit_underline_mode, *TS_enter_underline_mode;
311
a168702a
GM
312/* "as"/"ae" -- start/end alternate character set. Not really
313 supported, yet. */
314
315char *TS_enter_alt_charset_mode, *TS_exit_alt_charset_mode;
316
317/* "me" -- switch appearances off. */
318
319char *TS_exit_attribute_mode;
320
321/* "Co" -- number of colors. */
322
323int TN_max_colors;
324
325/* "pa" -- max. number of color pairs on screen. Not handled yet.
326 Could be a problem if not equal to TN_max_colors * TN_max_colors. */
327
328int TN_max_pairs;
329
330/* "op" -- SVr4 set default pair to its original value. */
331
332char *TS_orig_pair;
333
334/* "AF"/"AB" or "Sf"/"Sb"-- set ANSI or SVr4 foreground/background color.
335 1 param, the color index. */
336
337char *TS_set_foreground, *TS_set_background;
338
08a24c47
JB
339int TF_hazeltine; /* termcap hz flag. */
340int TF_insmode_motion; /* termcap mi flag: can move while in insert mode. */
341int TF_standout_motion; /* termcap mi flag: can move while in standout mode. */
a168702a
GM
342int TF_underscore; /* termcap ul flag: _ underlines if over-struck on
343 non-blank position. Must clear before writing _. */
08a24c47
JB
344int TF_teleray; /* termcap xt flag: many weird consequences.
345 For t1061. */
346
08a24c47
JB
347static int RPov; /* # chars to start a TS_repeat */
348
349static int delete_in_insert_mode; /* delete mode == insert mode */
350
351static int se_is_so; /* 1 if same string both enters and leaves
352 standout mode */
353
354/* internal state */
355
8dd0c7cb 356/* The largest frame width in any call to calculate_costs. */
a168702a 357
9882535b 358int max_frame_cols;
a168702a 359
8dd0c7cb 360/* The largest frame height in any call to calculate_costs. */
a168702a 361
9882535b 362int max_frame_lines;
8dd0c7cb 363
646e4dff 364static int costs_set; /* Nonzero if costs have been calculated. */
08a24c47
JB
365
366int insert_mode; /* Nonzero when in insert mode. */
367int standout_mode; /* Nonzero when in standout mode. */
368
369/* Size of window specified by higher levels.
ff11dfa1 370 This is the number of lines, from the top of frame downwards,
08a24c47
JB
371 which can participate in insert-line/delete-line operations.
372
9882535b 373 Effectively it excludes the bottom frame_lines - specified_window_size
08a24c47
JB
374 lines from those operations. */
375
376int specified_window;
377
ff11dfa1 378/* Frame currently being redisplayed; 0 if not currently redisplaying.
08a24c47
JB
379 (Direct output does not count). */
380
ff11dfa1 381FRAME_PTR updating_frame;
08a24c47 382
07c57952 383/* Provided for lisp packages. */
a168702a 384
07c57952
KH
385static int system_uses_terminfo;
386
d284f58f
GM
387/* Flag used in tty_show/hide_cursor. */
388
389static int tty_cursor_hidden;
390
08a24c47 391char *tparam ();
e4bfb3b6
RS
392
393extern char *tgetstr ();
08a24c47 394\f
cb28b9c2 395
1a89b7c6
AI
396#ifdef WINDOWSNT
397/* We aren't X windows, but we aren't termcap either. This makes me
398 uncertain as to what value to use for frame.output_method. For
399 this file, we'll define FRAME_TERMCAP_P to be zero so that our
400 output hooks get called instead of the termcap functions. Probably
401 the best long-term solution is to define an output_windows_nt... */
402
403#undef FRAME_TERMCAP_P
404#define FRAME_TERMCAP_P(_f_) 0
405#endif /* WINDOWSNT */
406
dfcf069d 407void
08a24c47
JB
408ring_bell ()
409{
d284f58f 410 if (!NILP (Vring_bell_function))
c291d9ef
RS
411 {
412 Lisp_Object function;
413
414 /* Temporarily set the global variable to nil
415 so that if we get an error, it stays nil
416 and we don't call it over and over.
417
418 We don't specbind it, because that would carefully
419 restore the bad value if there's an error
420 and make the loop of errors happen anyway. */
177c0ea7 421
c291d9ef
RS
422 function = Vring_bell_function;
423 Vring_bell_function = Qnil;
424
425 call0 (function);
426
427 Vring_bell_function = function;
08a24c47 428 }
d284f58f
GM
429 else if (!FRAME_TERMCAP_P (XFRAME (selected_frame)))
430 (*ring_bell_hook) ();
28d440ab
KL
431 else {
432 OUTPUT (CURTTY (), TS_visible_bell && visible_bell ? TS_visible_bell : TS_bell);
433 }
08a24c47
JB
434}
435
dfcf069d 436void
28d440ab 437set_terminal_modes (struct tty_output *tty)
08a24c47 438{
d284f58f 439 if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
08a24c47 440 {
28d440ab
KL
441 OUTPUT_IF (tty, TS_termcap_modes);
442 OUTPUT_IF (tty, TS_cursor_visible);
443 OUTPUT_IF (tty, TS_keypad_mode);
d284f58f 444 losecursor ();
08a24c47 445 }
d284f58f
GM
446 else
447 (*set_terminal_modes_hook) ();
08a24c47
JB
448}
449
dfcf069d 450void
28d440ab 451reset_terminal_modes (struct tty_output *tty)
08a24c47 452{
d284f58f 453 if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
08a24c47 454 {
54800acb 455 turn_off_highlight ();
d284f58f 456 turn_off_insert ();
28d440ab
KL
457 OUTPUT_IF (tty, TS_end_keypad_mode);
458 OUTPUT_IF (tty, TS_cursor_normal);
459 OUTPUT_IF (tty, TS_end_termcap_modes);
460 OUTPUT_IF (tty, TS_orig_pair);
d284f58f 461 /* Output raw CR so kernel can track the cursor hpos. */
28d440ab 462 current_tty = tty;
d284f58f 463 cmputc ('\r');
08a24c47 464 }
d284f58f
GM
465 else if (reset_terminal_modes_hook)
466 (*reset_terminal_modes_hook) ();
08a24c47
JB
467}
468
dfcf069d 469void
ff11dfa1 470update_begin (f)
d284f58f 471 struct frame *f;
08a24c47 472{
ff11dfa1 473 updating_frame = f;
d284f58f
GM
474 if (!FRAME_TERMCAP_P (f))
475 update_begin_hook (f);
08a24c47
JB
476}
477
dfcf069d 478void
ff11dfa1 479update_end (f)
d284f58f 480 struct frame *f;
08a24c47 481{
d284f58f 482 if (FRAME_TERMCAP_P (f))
08a24c47 483 {
d284f58f
GM
484 if (!XWINDOW (selected_window)->cursor_off_p)
485 tty_show_cursor ();
486 turn_off_insert ();
487 background_highlight ();
08a24c47 488 }
d284f58f
GM
489 else
490 update_end_hook (f);
177c0ea7 491
d284f58f 492 updating_frame = NULL;
08a24c47
JB
493}
494
dfcf069d 495void
08a24c47
JB
496set_terminal_window (size)
497 int size;
498{
d284f58f 499 if (FRAME_TERMCAP_P (updating_frame))
08a24c47 500 {
9882535b 501 specified_window = size ? size : FRAME_LINES (updating_frame);
28d440ab 502 if (TTY_SCROLL_REGION_OK (FRAME_TTY (updating_frame)))
d284f58f 503 set_scroll_region (0, specified_window);
08a24c47 504 }
d284f58f
GM
505 else
506 set_terminal_window_hook (size);
08a24c47
JB
507}
508
dfcf069d 509void
08a24c47
JB
510set_scroll_region (start, stop)
511 int start, stop;
512{
513 char *buf;
e52f4e08 514 struct frame *sf = XFRAME (selected_frame);
177c0ea7 515
08a24c47 516 if (TS_set_scroll_region)
d284f58f 517 buf = tparam (TS_set_scroll_region, 0, 0, start, stop - 1);
08a24c47 518 else if (TS_set_scroll_region_1)
d284f58f 519 buf = tparam (TS_set_scroll_region_1, 0, 0,
9882535b
KS
520 FRAME_LINES (sf), start,
521 FRAME_LINES (sf) - stop,
522 FRAME_LINES (sf));
08a24c47 523 else
9882535b 524 buf = tparam (TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (sf));
177c0ea7 525
28d440ab 526 OUTPUT (CURTTY (), buf);
9ac0d9e0 527 xfree (buf);
08a24c47
JB
528 losecursor ();
529}
d284f58f 530
08a24c47 531\f
d284f58f 532static void
08a24c47
JB
533turn_on_insert ()
534{
535 if (!insert_mode)
28d440ab 536 OUTPUT (CURTTY (), TS_insert_mode);
08a24c47
JB
537 insert_mode = 1;
538}
539
dfcf069d 540void
08a24c47
JB
541turn_off_insert ()
542{
543 if (insert_mode)
28d440ab 544 OUTPUT (CURTTY (), TS_end_insert_mode);
08a24c47
JB
545 insert_mode = 0;
546}
547\f
54800acb 548/* Handle highlighting. */
08a24c47 549
dfcf069d 550void
08a24c47
JB
551turn_off_highlight ()
552{
54800acb 553 if (standout_mode)
28d440ab 554 OUTPUT_IF (CURTTY (), TS_end_standout_mode);
54800acb 555 standout_mode = 0;
08a24c47
JB
556}
557
d284f58f 558static void
08a24c47
JB
559turn_on_highlight ()
560{
54800acb 561 if (!standout_mode)
28d440ab 562 OUTPUT_IF (CURTTY(), TS_standout_mode);
54800acb 563 standout_mode = 1;
08a24c47
JB
564}
565
86a7d192
GM
566static void
567toggle_highlight ()
568{
569 if (standout_mode)
570 turn_off_highlight ();
571 else
572 turn_on_highlight ();
573}
574
a168702a
GM
575
576/* Make cursor invisible. */
577
578static void
579tty_hide_cursor ()
580{
d284f58f
GM
581 if (tty_cursor_hidden == 0)
582 {
583 tty_cursor_hidden = 1;
28d440ab 584 OUTPUT_IF (CURTTY (), TS_cursor_invisible);
d284f58f 585 }
a168702a
GM
586}
587
588
589/* Ensure that cursor is visible. */
590
591static void
592tty_show_cursor ()
593{
d284f58f
GM
594 if (tty_cursor_hidden)
595 {
596 tty_cursor_hidden = 0;
28d440ab
KL
597 OUTPUT_IF (CURTTY (), TS_cursor_normal);
598 OUTPUT_IF (CURTTY (), TS_cursor_visible);
d284f58f 599 }
a168702a
GM
600}
601
602
08a24c47
JB
603/* Set standout mode to the state it should be in for
604 empty space inside windows. What this is,
605 depends on the user option inverse-video. */
606
dfcf069d 607void
08a24c47
JB
608background_highlight ()
609{
08a24c47
JB
610 if (inverse_video)
611 turn_on_highlight ();
612 else
613 turn_off_highlight ();
614}
615
616/* Set standout mode to the mode specified for the text to be output. */
617
dfcf069d 618static void
08a24c47
JB
619highlight_if_desired ()
620{
8ede64a5 621 if (inverse_video)
08a24c47 622 turn_on_highlight ();
8ede64a5
MB
623 else
624 turn_off_highlight ();
08a24c47
JB
625}
626\f
627
a168702a
GM
628/* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
629 frame-relative coordinates. */
08a24c47 630
dfcf069d 631void
a168702a
GM
632cursor_to (vpos, hpos)
633 int vpos, hpos;
08a24c47 634{
e52f4e08 635 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
177c0ea7 636
e52f4e08 637 if (! FRAME_TERMCAP_P (f) && cursor_to_hook)
08a24c47 638 {
a168702a 639 (*cursor_to_hook) (vpos, hpos);
08a24c47
JB
640 return;
641 }
642
36cae867
KH
643 /* Detect the case where we are called from reset_sys_modes
644 and the costs have never been calculated. Do nothing. */
8ede64a5 645 if (! costs_set)
36cae867
KH
646 return;
647
a168702a 648 if (curY == vpos && curX == hpos)
08a24c47
JB
649 return;
650 if (!TF_standout_motion)
651 background_highlight ();
652 if (!TF_insmode_motion)
653 turn_off_insert ();
28d440ab 654 cmgoto (FRAME_TTY (f), vpos, hpos);
08a24c47
JB
655}
656
657/* Similar but don't take any account of the wasted characters. */
658
dfcf069d 659void
08a24c47 660raw_cursor_to (row, col)
4746118a 661 int row, col;
08a24c47 662{
e52f4e08
GM
663 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
664 if (! FRAME_TERMCAP_P (f))
08a24c47
JB
665 {
666 (*raw_cursor_to_hook) (row, col);
667 return;
668 }
669 if (curY == row && curX == col)
670 return;
671 if (!TF_standout_motion)
672 background_highlight ();
673 if (!TF_insmode_motion)
674 turn_off_insert ();
28d440ab 675 cmgoto (FRAME_TTY (f), row, col);
08a24c47
JB
676}
677\f
678/* Erase operations */
679
ff11dfa1 680/* clear from cursor to end of frame */
dfcf069d 681void
08a24c47
JB
682clear_to_end ()
683{
684 register int i;
685
759d9005 686 if (clear_to_end_hook && ! FRAME_TERMCAP_P (updating_frame))
08a24c47
JB
687 {
688 (*clear_to_end_hook) ();
689 return;
690 }
691 if (TS_clr_to_bottom)
692 {
693 background_highlight ();
28d440ab 694 OUTPUT (CURTTY (), TS_clr_to_bottom);
08a24c47
JB
695 }
696 else
697 {
9882535b 698 for (i = curY; i < FRAME_LINES (XFRAME (selected_frame)); i++)
08a24c47
JB
699 {
700 cursor_to (i, 0);
9882535b 701 clear_end_of_line (FRAME_COLS (XFRAME (selected_frame)));
08a24c47
JB
702 }
703 }
704}
705
ff11dfa1 706/* Clear entire frame */
08a24c47 707
dfcf069d 708void
ff11dfa1 709clear_frame ()
08a24c47 710{
e52f4e08 711 struct frame *sf = XFRAME (selected_frame);
177c0ea7 712
ff11dfa1 713 if (clear_frame_hook
e52f4e08 714 && ! FRAME_TERMCAP_P ((updating_frame ? updating_frame : sf)))
08a24c47 715 {
ff11dfa1 716 (*clear_frame_hook) ();
08a24c47
JB
717 return;
718 }
ff11dfa1 719 if (TS_clr_frame)
08a24c47
JB
720 {
721 background_highlight ();
28d440ab 722 OUTPUT (FRAME_TTY (updating_frame ? updating_frame : sf), TS_clr_frame);
08a24c47
JB
723 cmat (0, 0);
724 }
725 else
726 {
727 cursor_to (0, 0);
728 clear_to_end ();
729 }
730}
731
08a24c47
JB
732/* Clear from cursor to end of line.
733 Assume that the line is already clear starting at column first_unused_hpos.
08a24c47
JB
734
735 Note that the cursor may be moved, on terminals lacking a `ce' string. */
736
dfcf069d 737void
8ede64a5 738clear_end_of_line (first_unused_hpos)
08a24c47
JB
739 int first_unused_hpos;
740{
741 register int i;
742
743 if (clear_end_of_line_hook
1820044b 744 && ! FRAME_TERMCAP_P ((updating_frame
ff11dfa1 745 ? updating_frame
e52f4e08 746 : XFRAME (selected_frame))))
08a24c47
JB
747 {
748 (*clear_end_of_line_hook) (first_unused_hpos);
749 return;
750 }
751
36cae867
KH
752 /* Detect the case where we are called from reset_sys_modes
753 and the costs have never been calculated. Do nothing. */
8ede64a5 754 if (! costs_set)
36cae867
KH
755 return;
756
08a24c47
JB
757 if (curX >= first_unused_hpos)
758 return;
08a24c47
JB
759 background_highlight ();
760 if (TS_clr_line)
761 {
28d440ab 762 OUTPUT1 (CURTTY (), TS_clr_line);
08a24c47
JB
763 }
764 else
765 { /* have to do it the hard way */
e52f4e08 766 struct frame *sf = XFRAME (selected_frame);
08a24c47
JB
767 turn_off_insert ();
768
a168702a 769 /* Do not write in last row last col with Auto-wrap on. */
9882535b
KS
770 if (AutoWrap && curY == FRAME_LINES (sf) - 1
771 && first_unused_hpos == FRAME_COLS (sf))
08a24c47
JB
772 first_unused_hpos--;
773
774 for (i = curX; i < first_unused_hpos; i++)
775 {
28d440ab
KL
776 if (TTY_TERMSCRIPT (CURTTY ()))
777 fputc (' ', TTY_TERMSCRIPT (CURTTY ()));
778 fputc (' ', TTY_OUTPUT (CURTTY ()));
08a24c47
JB
779 }
780 cmplus (first_unused_hpos - curX);
781 }
782}
783\f
a4decb7f
KH
784/* Encode SRC_LEN glyphs starting at SRC to terminal output codes and
785 store them at DST. Do not write more than DST_LEN bytes. That may
786 require stopping before all SRC_LEN input glyphs have been
787 converted.
788
789 We store the number of glyphs actually converted in *CONSUMED. The
790 return value is the number of bytes store in DST. */
791
792int
793encode_terminal_code (src, dst, src_len, dst_len, consumed)
a168702a 794 struct glyph *src;
a4decb7f
KH
795 int src_len;
796 unsigned char *dst;
797 int dst_len, *consumed;
798{
a168702a 799 struct glyph *src_start = src, *src_end = src + src_len;
a4decb7f 800 unsigned char *dst_start = dst, *dst_end = dst + dst_len;
085d5908 801 register GLYPH g;
405ea71c
KR
802 unsigned char workbuf[MAX_MULTIBYTE_LENGTH];
803 const unsigned char *buf;
6589fd67 804 int len;
a4decb7f
KH
805 register int tlen = GLYPH_TABLE_LENGTH;
806 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
f3ac545f 807 int result;
6589fd67
KH
808 struct coding_system *coding;
809
1ac0700a
KH
810 /* If terminal_coding does any conversion, use it, otherwise use
811 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
812 because it always return 1 if the member src_multibyte is 1. */
813 coding = (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
6589fd67
KH
814 ? &terminal_coding
815 : &safe_terminal_coding);
a4decb7f
KH
816
817 while (src < src_end)
818 {
a4decb7f 819 /* We must skip glyphs to be padded for a wide character. */
a168702a 820 if (! CHAR_GLYPH_PADDING_P (*src))
a4decb7f 821 {
32de38e4
KH
822 g = GLYPH_FROM_CHAR_GLYPH (src[0]);
823
824 if (g < 0 || g >= tlen)
085d5908 825 {
32de38e4
KH
826 /* This glyph doesn't has an entry in Vglyph_table. */
827 if (! CHAR_VALID_P (src->u.ch, 0))
828 {
829 len = 1;
830 buf = " ";
f2ba40be 831 coding->src_multibyte = 0;
32de38e4
KH
832 }
833 else
834 {
835 len = CHAR_STRING (src->u.ch, workbuf);
836 buf = workbuf;
f2ba40be 837 coding->src_multibyte = 1;
32de38e4 838 }
085d5908 839 }
32de38e4 840 else
a4decb7f 841 {
32de38e4 842 /* This glyph has an entry in Vglyph_table,
a4decb7f
KH
843 so process any alias before testing for simpleness. */
844 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
32de38e4
KH
845
846 if (GLYPH_SIMPLE_P (tbase, tlen, g))
847 {
c989b7be
KH
848 /* We set the multi-byte form of a character in G
849 (that should be an ASCII character) at
850 WORKBUF. */
851 workbuf[0] = FAST_GLYPH_CHAR (g);
852 len = 1;
32de38e4 853 buf = workbuf;
f2ba40be 854 coding->src_multibyte = 0;
32de38e4
KH
855 }
856 else
857 {
858 /* We have a string in Vglyph_table. */
859 len = GLYPH_LENGTH (tbase, g);
860 buf = GLYPH_STRING (tbase, g);
f2ba40be 861 coding->src_multibyte = STRING_MULTIBYTE (tbase[g]);
32de38e4 862 }
171d7f24 863 }
177c0ea7 864
f3ac545f 865 result = encode_coding (coding, buf, dst, len, dst_end - dst);
c42869c4 866 len -= coding->consumed;
6589fd67 867 dst += coding->produced;
f3ac545f
KH
868 if (result == CODING_FINISH_INSUFFICIENT_DST
869 || (result == CODING_FINISH_INSUFFICIENT_SRC
870 && len > dst_end - dst))
871 /* The remaining output buffer is too short. We must
872 break the loop here without increasing SRC so that the
873 next call of this function starts from the same glyph. */
874 break;
875
c42869c4
KH
876 if (len > 0)
877 {
f3ac545f
KH
878 /* This is the case that a code of the range 0200..0237
879 exists in buf. We must just write out such a code. */
880 buf += coding->consumed;
881 while (len--)
882 *dst++ = *buf++;
c42869c4 883 }
a4decb7f
KH
884 }
885 src++;
886 }
177c0ea7 887
a4decb7f
KH
888 *consumed = src - src_start;
889 return (dst - dst_start);
890}
891
08a24c47 892
dfcf069d 893void
08a24c47 894write_glyphs (string, len)
a168702a 895 register struct glyph *string;
08a24c47
JB
896 register int len;
897{
a4decb7f 898 int produced, consumed;
e52f4e08
GM
899 struct frame *sf = XFRAME (selected_frame);
900 struct frame *f = updating_frame ? updating_frame : sf;
d2fdb076
KH
901 unsigned char conversion_buffer[1024];
902 int conversion_buffer_size = sizeof conversion_buffer;
08a24c47
JB
903
904 if (write_glyphs_hook
a168702a 905 && ! FRAME_TERMCAP_P (f))
08a24c47
JB
906 {
907 (*write_glyphs_hook) (string, len);
908 return;
909 }
910
08a24c47 911 turn_off_insert ();
d284f58f 912 tty_hide_cursor ();
08a24c47 913
a168702a 914 /* Don't dare write in last column of bottom line, if Auto-Wrap,
ff11dfa1 915 since that would scroll the whole frame on some terminals. */
08a24c47
JB
916
917 if (AutoWrap
9882535b
KS
918 && curY + 1 == FRAME_LINES (sf)
919 && (curX + len) == FRAME_COLS (sf))
08a24c47 920 len --;
a4decb7f
KH
921 if (len <= 0)
922 return;
08a24c47
JB
923
924 cmplus (len);
177c0ea7 925
6589fd67
KH
926 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
927 the tail. */
928 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
177c0ea7 929
a4decb7f 930 while (len > 0)
08a24c47 931 {
a168702a 932 /* Identify a run of glyphs with the same face. */
32de38e4 933 int face_id = string->face_id;
a168702a 934 int n;
177c0ea7 935
a168702a 936 for (n = 1; n < len; ++n)
32de38e4 937 if (string[n].face_id != face_id)
a168702a
GM
938 break;
939
940 /* Turn appearance modes of the face of the run on. */
2535aa8c 941 highlight_if_desired ();
a168702a
GM
942 turn_on_face (f, face_id);
943
944 while (n > 0)
08a24c47 945 {
d2fdb076
KH
946 /* We use a fixed size (1024 bytes) of conversion buffer.
947 Usually it is sufficient, but if not, we just repeat the
948 loop. */
a168702a
GM
949 produced = encode_terminal_code (string, conversion_buffer,
950 n, conversion_buffer_size,
951 &consumed);
952 if (produced > 0)
953 {
28d440ab
KL
954 fwrite (conversion_buffer, 1, produced,
955 TTY_OUTPUT (FRAME_TTY (f)));
956 if (ferror (TTY_OUTPUT (FRAME_TTY (f))))
957 clearerr (TTY_OUTPUT (FRAME_TTY (f)));
958 if (TTY_TERMSCRIPT (FRAME_TTY (f)))
959 fwrite (conversion_buffer, 1, produced,
960 TTY_TERMSCRIPT (FRAME_TTY (f)));
a168702a
GM
961 }
962 len -= consumed;
963 n -= consumed;
964 string += consumed;
08a24c47 965 }
a168702a
GM
966
967 /* Turn appearance modes off. */
968 turn_off_face (f, face_id);
65aa5e85 969 turn_off_highlight ();
a4decb7f 970 }
177c0ea7 971
a4decb7f 972 /* We may have to output some codes to terminate the writing. */
ca1e6a13 973 if (CODING_REQUIRE_FLUSHING (&terminal_coding))
a4decb7f 974 {
6589fd67 975 terminal_coding.mode |= CODING_MODE_LAST_BLOCK;
1814bd62 976 encode_coding (&terminal_coding, "", conversion_buffer,
6589fd67
KH
977 0, conversion_buffer_size);
978 if (terminal_coding.produced > 0)
df8bf431 979 {
28d440ab
KL
980 fwrite (conversion_buffer, 1, terminal_coding.produced,
981 TTY_OUTPUT (FRAME_TTY (f)));
982 if (ferror (TTY_OUTPUT (FRAME_TTY (f))))
983 clearerr (TTY_OUTPUT (FRAME_TTY (f)));
984 if (TTY_TERMSCRIPT (FRAME_TTY (f)))
6589fd67 985 fwrite (conversion_buffer, 1, terminal_coding.produced,
28d440ab 986 TTY_TERMSCRIPT (FRAME_TTY (f)));
df8bf431 987 }
08a24c47 988 }
177c0ea7 989
28d440ab 990 cmcheckmagic (FRAME_TTY (f));
08a24c47
JB
991}
992
993/* If start is zero, insert blanks instead of a string at start */
177c0ea7 994
dfcf069d 995void
08a24c47 996insert_glyphs (start, len)
a168702a 997 register struct glyph *start;
08a24c47
JB
998 register int len;
999{
1000 char *buf;
6bbd7a29 1001 struct glyph *glyph = NULL;
e52f4e08 1002 struct frame *f, *sf;
08a24c47 1003
a4decb7f
KH
1004 if (len <= 0)
1005 return;
1006
a168702a 1007 if (insert_glyphs_hook)
08a24c47
JB
1008 {
1009 (*insert_glyphs_hook) (start, len);
1010 return;
1011 }
a168702a 1012
e52f4e08
GM
1013 sf = XFRAME (selected_frame);
1014 f = updating_frame ? updating_frame : sf;
08a24c47
JB
1015
1016 if (TS_ins_multi_chars)
1017 {
1018 buf = tparam (TS_ins_multi_chars, 0, 0, len);
28d440ab 1019 OUTPUT1 (FRAME_TTY (f), buf);
9ac0d9e0 1020 xfree (buf);
08a24c47
JB
1021 if (start)
1022 write_glyphs (start, len);
1023 return;
1024 }
1025
1026 turn_on_insert ();
1027 cmplus (len);
6589fd67
KH
1028 /* The bit CODING_MODE_LAST_BLOCK should be set to 1 only at the tail. */
1029 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
07109bf9 1030 while (len-- > 0)
08a24c47 1031 {
a4decb7f 1032 int produced, consumed;
d2fdb076
KH
1033 unsigned char conversion_buffer[1024];
1034 int conversion_buffer_size = sizeof conversion_buffer;
a4decb7f 1035
28d440ab 1036 OUTPUT1_IF (FRAME_TTY (f), TS_ins_char);
08a24c47 1037 if (!start)
32de38e4
KH
1038 {
1039 conversion_buffer[0] = SPACEGLYPH;
1040 produced = 1;
1041 }
08a24c47 1042 else
08a24c47 1043 {
65aa5e85 1044 highlight_if_desired ();
32de38e4 1045 turn_on_face (f, start->face_id);
816be8b8 1046 glyph = start;
a168702a 1047 ++start;
a4decb7f
KH
1048 /* We must open sufficient space for a character which
1049 occupies more than one column. */
a168702a 1050 while (len && CHAR_GLYPH_PADDING_P (*start))
a4decb7f 1051 {
28d440ab 1052 OUTPUT1_IF (FRAME_TTY (f), TS_ins_char);
a4decb7f
KH
1053 start++, len--;
1054 }
a4decb7f 1055
32de38e4
KH
1056 if (len <= 0)
1057 /* This is the last glyph. */
1058 terminal_coding.mode |= CODING_MODE_LAST_BLOCK;
1059
d2fdb076
KH
1060 /* The size of conversion buffer (1024 bytes) is surely
1061 sufficient for just one glyph. */
816be8b8 1062 produced = encode_terminal_code (glyph, conversion_buffer, 1,
32de38e4
KH
1063 conversion_buffer_size, &consumed);
1064 }
a4decb7f 1065
a4decb7f 1066 if (produced > 0)
08a24c47 1067 {
28d440ab
KL
1068 fwrite (conversion_buffer, 1, produced,
1069 TTY_OUTPUT (FRAME_TTY (f)));
1070 if (ferror (TTY_OUTPUT (FRAME_TTY (f))))
1071 clearerr (TTY_OUTPUT (FRAME_TTY (f)));
1072 if (TTY_TERMSCRIPT (FRAME_TTY (f)))
1073 fwrite (conversion_buffer, 1, produced,
1074 TTY_TERMSCRIPT (FRAME_TTY (f)));
08a24c47
JB
1075 }
1076
28d440ab 1077 OUTPUT1_IF (FRAME_TTY (f), TS_pad_inserted_char);
32de38e4 1078 if (start)
65aa5e85
GM
1079 {
1080 turn_off_face (f, glyph->face_id);
1081 turn_off_highlight ();
1082 }
9a6b6f92 1083 }
177c0ea7 1084
28d440ab 1085 cmcheckmagic (FRAME_TTY (f));
08a24c47
JB
1086}
1087
dfcf069d 1088void
08a24c47
JB
1089delete_glyphs (n)
1090 register int n;
1091{
1092 char *buf;
1093 register int i;
1094
1820044b 1095 if (delete_glyphs_hook && ! FRAME_TERMCAP_P (updating_frame))
08a24c47
JB
1096 {
1097 (*delete_glyphs_hook) (n);
1098 return;
1099 }
1100
1101 if (delete_in_insert_mode)
1102 {
1103 turn_on_insert ();
1104 }
1105 else
1106 {
1107 turn_off_insert ();
28d440ab 1108 OUTPUT_IF (FRAME_TTY (updating_frame), TS_delete_mode);
08a24c47
JB
1109 }
1110
1111 if (TS_del_multi_chars)
1112 {
1113 buf = tparam (TS_del_multi_chars, 0, 0, n);
28d440ab 1114 OUTPUT1 (FRAME_TTY (updating_frame), buf);
9ac0d9e0 1115 xfree (buf);
08a24c47
JB
1116 }
1117 else
1118 for (i = 0; i < n; i++)
28d440ab 1119 OUTPUT1 (FRAME_TTY (updating_frame), TS_del_char);
08a24c47 1120 if (!delete_in_insert_mode)
28d440ab 1121 OUTPUT_IF (FRAME_TTY (updating_frame), TS_end_delete_mode);
08a24c47
JB
1122}
1123\f
1124/* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
1125
dfcf069d 1126void
08a24c47
JB
1127ins_del_lines (vpos, n)
1128 int vpos, n;
1129{
1130 char *multi = n > 0 ? TS_ins_multi_lines : TS_del_multi_lines;
1131 char *single = n > 0 ? TS_ins_line : TS_del_line;
1132 char *scroll = n > 0 ? TS_rev_scroll : TS_fwd_scroll;
e52f4e08 1133 struct frame *sf;
08a24c47
JB
1134
1135 register int i = n > 0 ? n : -n;
1136 register char *buf;
1137
1820044b 1138 if (ins_del_lines_hook && ! FRAME_TERMCAP_P (updating_frame))
08a24c47
JB
1139 {
1140 (*ins_del_lines_hook) (vpos, n);
1141 return;
1142 }
1143
e52f4e08 1144 sf = XFRAME (selected_frame);
177c0ea7 1145
08a24c47
JB
1146 /* If the lines below the insertion are being pushed
1147 into the end of the window, this is the same as clearing;
1148 and we know the lines are already clear, since the matching
1149 deletion has already been done. So can ignore this. */
1150 /* If the lines below the deletion are blank lines coming
1151 out of the end of the window, don't bother,
1152 as there will be a matching inslines later that will flush them. */
28d440ab 1153 if (TTY_SCROLL_REGION_OK (FRAME_TTY (sf))
8a56675d 1154 && vpos + i >= specified_window)
08a24c47 1155 return;
28d440ab 1156 if (!TTY_MEMORY_BELOW_FRAME (FRAME_TTY (sf))
8a56675d 1157 && vpos + i >= FRAME_LINES (sf))
08a24c47
JB
1158 return;
1159
1160 if (multi)
1161 {
1162 raw_cursor_to (vpos, 0);
1163 background_highlight ();
1164 buf = tparam (multi, 0, 0, i);
28d440ab 1165 OUTPUT (FRAME_TTY (sf), buf);
9ac0d9e0 1166 xfree (buf);
08a24c47
JB
1167 }
1168 else if (single)
1169 {
1170 raw_cursor_to (vpos, 0);
1171 background_highlight ();
1172 while (--i >= 0)
28d440ab 1173 OUTPUT (FRAME_TTY (sf), single);
08a24c47
JB
1174 if (TF_teleray)
1175 curX = 0;
1176 }
1177 else
1178 {
1179 set_scroll_region (vpos, specified_window);
1180 if (n < 0)
1181 raw_cursor_to (specified_window - 1, 0);
1182 else
1183 raw_cursor_to (vpos, 0);
1184 background_highlight ();
1185 while (--i >= 0)
28d440ab 1186 OUTPUTL (FRAME_TTY (sf), scroll, specified_window - vpos);
08a24c47
JB
1187 set_scroll_region (0, specified_window);
1188 }
1189
28d440ab
KL
1190 if (!TTY_SCROLL_REGION_OK (FRAME_TTY (sf))
1191 && TTY_MEMORY_BELOW_FRAME (FRAME_TTY (sf))
8a56675d 1192 && n < 0)
08a24c47 1193 {
9882535b 1194 cursor_to (FRAME_LINES (sf) + n, 0);
08a24c47
JB
1195 clear_to_end ();
1196 }
1197}
1198\f
1199/* Compute cost of sending "str", in characters,
1200 not counting any line-dependent padding. */
1201
1202int
1203string_cost (str)
1204 char *str;
1205{
1206 cost = 0;
1207 if (str)
1208 tputs (str, 0, evalcost);
1209 return cost;
1210}
1211
1212/* Compute cost of sending "str", in characters,
1213 counting any line-dependent padding at one line. */
1214
1215static int
1216string_cost_one_line (str)
1217 char *str;
1218{
1219 cost = 0;
1220 if (str)
1221 tputs (str, 1, evalcost);
1222 return cost;
1223}
1224
1225/* Compute per line amount of line-dependent padding,
1226 in tenths of characters. */
1227
1228int
1229per_line_cost (str)
1230 register char *str;
1231{
1232 cost = 0;
1233 if (str)
1234 tputs (str, 0, evalcost);
1235 cost = - cost;
1236 if (str)
1237 tputs (str, 10, evalcost);
1238 return cost;
1239}
1240
1241#ifndef old
1242/* char_ins_del_cost[n] is cost of inserting N characters.
8dd0c7cb 1243 char_ins_del_cost[-n] is cost of deleting N characters.
9882535b 1244 The length of this vector is based on max_frame_cols. */
08a24c47
JB
1245
1246int *char_ins_del_vector;
1247
9882535b 1248#define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
08a24c47
JB
1249#endif
1250
1251/* ARGSUSED */
1252static void
ff11dfa1
JB
1253calculate_ins_del_char_costs (frame)
1254 FRAME_PTR frame;
08a24c47
JB
1255{
1256 int ins_startup_cost, del_startup_cost;
1257 int ins_cost_per_char, del_cost_per_char;
1258 register int i;
1259 register int *p;
1260
1261 if (TS_ins_multi_chars)
1262 {
1263 ins_cost_per_char = 0;
1264 ins_startup_cost = string_cost_one_line (TS_ins_multi_chars);
1265 }
1266 else if (TS_ins_char || TS_pad_inserted_char
1267 || (TS_insert_mode && TS_end_insert_mode))
1268 {
1269 ins_startup_cost = (30 * (string_cost (TS_insert_mode)
1270 + string_cost (TS_end_insert_mode))) / 100;
1271 ins_cost_per_char = (string_cost_one_line (TS_ins_char)
1272 + string_cost_one_line (TS_pad_inserted_char));
1273 }
1274 else
1275 {
1276 ins_startup_cost = 9999;
1277 ins_cost_per_char = 0;
1278 }
1279
1280 if (TS_del_multi_chars)
1281 {
1282 del_cost_per_char = 0;
1283 del_startup_cost = string_cost_one_line (TS_del_multi_chars);
1284 }
1285 else if (TS_del_char)
1286 {
1287 del_startup_cost = (string_cost (TS_delete_mode)
1288 + string_cost (TS_end_delete_mode));
1289 if (delete_in_insert_mode)
1290 del_startup_cost /= 2;
1291 del_cost_per_char = string_cost_one_line (TS_del_char);
1292 }
1293 else
1294 {
1295 del_startup_cost = 9999;
1296 del_cost_per_char = 0;
1297 }
1298
1299 /* Delete costs are at negative offsets */
ff11dfa1 1300 p = &char_ins_del_cost (frame)[0];
9882535b 1301 for (i = FRAME_COLS (frame); --i >= 0;)
08a24c47
JB
1302 *--p = (del_startup_cost += del_cost_per_char);
1303
1304 /* Doing nothing is free */
ff11dfa1 1305 p = &char_ins_del_cost (frame)[0];
08a24c47
JB
1306 *p++ = 0;
1307
1308 /* Insert costs are at positive offsets */
9882535b 1309 for (i = FRAME_COLS (frame); --i >= 0;)
08a24c47
JB
1310 *p++ = (ins_startup_cost += ins_cost_per_char);
1311}
1312
dfcf069d 1313void
ff11dfa1
JB
1314calculate_costs (frame)
1315 FRAME_PTR frame;
08a24c47 1316{
9f732a77
RS
1317 register char *f = (TS_set_scroll_region
1318 ? TS_set_scroll_region
1319 : TS_set_scroll_region_1);
08a24c47 1320
9f732a77 1321 FRAME_COST_BAUD_RATE (frame) = baud_rate;
08a24c47 1322
28d440ab
KL
1323 if (FRAME_TERMCAP_P (frame))
1324 TTY_SCROLL_REGION_COST (frame->output_data.tty) = string_cost (f);
08a24c47
JB
1325
1326 /* These variables are only used for terminal stuff. They are allocated
ff11dfa1 1327 once for the terminal frame of X-windows emacs, but not used afterwards.
08a24c47
JB
1328
1329 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
8ede64a5 1330 X turns off char_ins_del_ok. */
08a24c47 1331
9882535b
KS
1332 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1333 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
8dd0c7cb 1334
8ede64a5 1335 costs_set = 1;
08a24c47
JB
1336
1337 if (char_ins_del_vector != 0)
1338 char_ins_del_vector
1339 = (int *) xrealloc (char_ins_del_vector,
1340 (sizeof (int)
9882535b 1341 + 2 * max_frame_cols * sizeof (int)));
08a24c47
JB
1342 else
1343 char_ins_del_vector
1344 = (int *) xmalloc (sizeof (int)
9882535b 1345 + 2 * max_frame_cols * sizeof (int));
08a24c47 1346
08a24c47 1347 bzero (char_ins_del_vector, (sizeof (int)
9882535b 1348 + 2 * max_frame_cols * sizeof (int)));
08a24c47 1349
ff11dfa1
JB
1350 if (f && (!TS_ins_line && !TS_del_line))
1351 do_line_insertion_deletion_costs (frame,
08a24c47
JB
1352 TS_rev_scroll, TS_ins_multi_lines,
1353 TS_fwd_scroll, TS_del_multi_lines,
ff11dfa1 1354 f, f, 1);
08a24c47 1355 else
ff11dfa1 1356 do_line_insertion_deletion_costs (frame,
08a24c47
JB
1357 TS_ins_line, TS_ins_multi_lines,
1358 TS_del_line, TS_del_multi_lines,
1359 0, 0, 1);
1360
ff11dfa1 1361 calculate_ins_del_char_costs (frame);
08a24c47
JB
1362
1363 /* Don't use TS_repeat if its padding is worse than sending the chars */
1364 if (TS_repeat && per_line_cost (TS_repeat) * baud_rate < 9000)
1365 RPov = string_cost (TS_repeat);
1366 else
9882535b 1367 RPov = FRAME_COLS (frame) * 2;
08a24c47
JB
1368
1369 cmcostinit (); /* set up cursor motion costs */
1370}
1371\f
a796ac82
JB
1372struct fkey_table {
1373 char *cap, *name;
1374};
1375
01d8deb0
ER
1376 /* Termcap capability names that correspond directly to X keysyms.
1377 Some of these (marked "terminfo") aren't supplied by old-style
1378 (Berkeley) termcap entries. They're listed in X keysym order;
1379 except we put the keypad keys first, so that if they clash with
1380 other keys (as on the IBM PC keyboard) they get overridden.
1381 */
1382
a168702a
GM
1383static struct fkey_table keys[] =
1384{
8103ad1a
PJ
1385 {"kh", "home"}, /* termcap */
1386 {"kl", "left"}, /* termcap */
1387 {"ku", "up"}, /* termcap */
1388 {"kr", "right"}, /* termcap */
1389 {"kd", "down"}, /* termcap */
1390 {"%8", "prior"}, /* terminfo */
1391 {"%5", "next"}, /* terminfo */
1392 {"@7", "end"}, /* terminfo */
1393 {"@1", "begin"}, /* terminfo */
1394 {"*6", "select"}, /* terminfo */
1395 {"%9", "print"}, /* terminfo */
1396 {"@4", "execute"}, /* terminfo --- actually the `command' key */
01d8deb0
ER
1397 /*
1398 * "insert" --- see below
1399 */
8103ad1a
PJ
1400 {"&8", "undo"}, /* terminfo */
1401 {"%0", "redo"}, /* terminfo */
1402 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1403 {"@0", "find"}, /* terminfo */
1404 {"@2", "cancel"}, /* terminfo */
1405 {"%1", "help"}, /* terminfo */
01d8deb0
ER
1406 /*
1407 * "break" goes here, but can't be reliably intercepted with termcap
1408 */
8103ad1a 1409 {"&4", "reset"}, /* terminfo --- actually `restart' */
01d8deb0
ER
1410 /*
1411 * "system" and "user" --- no termcaps
1412 */
8103ad1a
PJ
1413 {"kE", "clearline"}, /* terminfo */
1414 {"kA", "insertline"}, /* terminfo */
1415 {"kL", "deleteline"}, /* terminfo */
1416 {"kI", "insertchar"}, /* terminfo */
1417 {"kD", "deletechar"}, /* terminfo */
1418 {"kB", "backtab"}, /* terminfo */
01d8deb0
ER
1419 /*
1420 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1421 */
8103ad1a 1422 {"@8", "kp-enter"}, /* terminfo */
01d8deb0
ER
1423 /*
1424 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1425 * "kp-multiply", "kp-add", "kp-separator",
1426 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1427 * --- no termcaps for any of these.
1428 */
8103ad1a 1429 {"K4", "kp-1"}, /* terminfo */
01d8deb0
ER
1430 /*
1431 * "kp-2" --- no termcap
1432 */
8103ad1a 1433 {"K5", "kp-3"}, /* terminfo */
01d8deb0
ER
1434 /*
1435 * "kp-4" --- no termcap
1436 */
8103ad1a 1437 {"K2", "kp-5"}, /* terminfo */
01d8deb0
ER
1438 /*
1439 * "kp-6" --- no termcap
1440 */
8103ad1a 1441 {"K1", "kp-7"}, /* terminfo */
01d8deb0
ER
1442 /*
1443 * "kp-8" --- no termcap
1444 */
8103ad1a 1445 {"K3", "kp-9"}, /* terminfo */
01d8deb0
ER
1446 /*
1447 * "kp-equal" --- no termcap
1448 */
8103ad1a
PJ
1449 {"k1", "f1"},
1450 {"k2", "f2"},
1451 {"k3", "f3"},
1452 {"k4", "f4"},
1453 {"k5", "f5"},
1454 {"k6", "f6"},
1455 {"k7", "f7"},
1456 {"k8", "f8"},
1457 {"k9", "f9"}
a796ac82
JB
1458 };
1459
f2a00342
RM
1460static char **term_get_fkeys_arg;
1461static Lisp_Object term_get_fkeys_1 ();
465db27b 1462
01d8deb0 1463/* Find the escape codes sent by the function keys for Vfunction_key_map.
177c0ea7 1464 This function scans the termcap function key sequence entries, and
01d8deb0
ER
1465 adds entries to Vfunction_key_map for each function key it finds. */
1466
5c2c7893
JB
1467void
1468term_get_fkeys (address)
1469 char **address;
f2a00342
RM
1470{
1471 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1472 errors during the call. The only errors should be from Fdefine_key
1473 when given a key sequence containing an invalid prefix key. If the
1474 termcap defines function keys which use a prefix that is already bound
1475 to a command by the default bindings, we should silently ignore that
1476 function key specification, rather than giving the user an error and
1477 refusing to run at all on such a terminal. */
1478
1479 extern Lisp_Object Fidentity ();
f2a00342
RM
1480 term_get_fkeys_arg = address;
1481 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1482}
1483
1484static Lisp_Object
1485term_get_fkeys_1 ()
5c2c7893 1486{
5c2c7893
JB
1487 int i;
1488
37085233 1489 char **address = term_get_fkeys_arg;
f778aff2 1490
3e65092f
RS
1491 /* This can happen if CANNOT_DUMP or with strange options. */
1492 if (!initialized)
1493 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
1494
5c2c7893
JB
1495 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1496 {
1497 char *sequence = tgetstr (keys[i].cap, address);
1498 if (sequence)
f2a00342
RM
1499 Fdefine_key (Vfunction_key_map, build_string (sequence),
1500 Fmake_vector (make_number (1),
1501 intern (keys[i].name)));
5c2c7893 1502 }
a796ac82
JB
1503
1504 /* The uses of the "k0" capability are inconsistent; sometimes it
1505 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
eb8c3be9 1506 We will attempt to politely accommodate both systems by testing for
a796ac82
JB
1507 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1508 */
1509 {
1510 char *k_semi = tgetstr ("k;", address);
1511 char *k0 = tgetstr ("k0", address);
1512 char *k0_name = "f10";
1513
1514 if (k_semi)
1515 {
95c11956
SM
1516 if (k0)
1517 /* Define f0 first, so that f10 takes precedence in case the
1518 key sequences happens to be the same. */
1519 Fdefine_key (Vfunction_key_map, build_string (k0),
1520 Fmake_vector (make_number (1), intern ("f0")));
f2a00342
RM
1521 Fdefine_key (Vfunction_key_map, build_string (k_semi),
1522 Fmake_vector (make_number (1), intern ("f10")));
a796ac82 1523 }
95c11956 1524 else if (k0)
f2a00342
RM
1525 Fdefine_key (Vfunction_key_map, build_string (k0),
1526 Fmake_vector (make_number (1), intern (k0_name)));
a796ac82 1527 }
01d8deb0
ER
1528
1529 /* Set up cookies for numbered function keys above f10. */
1530 {
1531 char fcap[3], fkey[4];
1532
fc4f24da 1533 fcap[0] = 'F'; fcap[2] = '\0';
01d8deb0
ER
1534 for (i = 11; i < 64; i++)
1535 {
1536 if (i <= 19)
1537 fcap[1] = '1' + i - 11;
1538 else if (i <= 45)
b59ab95c 1539 fcap[1] = 'A' + i - 20;
01d8deb0 1540 else
b59ab95c 1541 fcap[1] = 'a' + i - 46;
01d8deb0 1542
fc4f24da
RS
1543 {
1544 char *sequence = tgetstr (fcap, address);
1545 if (sequence)
1546 {
465db27b 1547 sprintf (fkey, "f%d", i);
f2a00342
RM
1548 Fdefine_key (Vfunction_key_map, build_string (sequence),
1549 Fmake_vector (make_number (1),
1550 intern (fkey)));
fc4f24da
RS
1551 }
1552 }
01d8deb0
ER
1553 }
1554 }
1555
1556 /*
1557 * Various mappings to try and get a better fit.
1558 */
1559 {
fc4f24da
RS
1560#define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1561 if (!tgetstr (cap1, address)) \
1562 { \
1563 char *sequence = tgetstr (cap2, address); \
1564 if (sequence) \
f2a00342
RM
1565 Fdefine_key (Vfunction_key_map, build_string (sequence), \
1566 Fmake_vector (make_number (1), \
1567 intern (sym))); \
fc4f24da 1568 }
177c0ea7 1569
01d8deb0 1570 /* if there's no key_next keycap, map key_npage to `next' keysym */
27b61785 1571 CONDITIONAL_REASSIGN ("%5", "kN", "next");
01d8deb0 1572 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
381d11a1 1573 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
01d8deb0 1574 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
27b61785 1575 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
403c995b
RS
1576 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1577 CONDITIONAL_REASSIGN ("@7", "kH", "end");
0a7f697a
KH
1578
1579 /* IBM has their own non-standard dialect of terminfo.
1580 If the standard name isn't found, try the IBM name. */
1581 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1582 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1583 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1584 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1585 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1586 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1587 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1588 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1589 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1dd40212 1590#undef CONDITIONAL_REASSIGN
01d8deb0 1591 }
a168702a
GM
1592
1593 return Qnil;
5c2c7893
JB
1594}
1595
1596\f
a168702a
GM
1597/***********************************************************************
1598 Character Display Information
1599 ***********************************************************************/
1600
1601static void append_glyph P_ ((struct it *));
1602
1603
1604/* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1605 terminal frames if IT->glyph_row != NULL. IT->c is the character
1606 for which to produce glyphs; IT->face_id contains the character's
1607 face. Padding glyphs are appended if IT->c has a IT->pixel_width >
1608 1. */
177c0ea7 1609
a168702a
GM
1610static void
1611append_glyph (it)
1612 struct it *it;
1613{
1614 struct glyph *glyph, *end;
1615 int i;
1616
1617 xassert (it->glyph_row);
1618 glyph = (it->glyph_row->glyphs[it->area]
1619 + it->glyph_row->used[it->area]);
1620 end = it->glyph_row->glyphs[1 + it->area];
1621
177c0ea7
JB
1622 for (i = 0;
1623 i < it->pixel_width && glyph < end;
a168702a
GM
1624 ++i)
1625 {
1626 glyph->type = CHAR_GLYPH;
d13f3e2e 1627 glyph->pixel_width = 1;
32de38e4
KH
1628 glyph->u.ch = it->c;
1629 glyph->face_id = it->face_id;
1630 glyph->padding_p = i > 0;
a168702a
GM
1631 glyph->charpos = CHARPOS (it->position);
1632 glyph->object = it->object;
177c0ea7 1633
a168702a
GM
1634 ++it->glyph_row->used[it->area];
1635 ++glyph;
1636 }
1637}
1638
1639
b50fe468
RS
1640/* Produce glyphs for the display element described by IT. *IT
1641 specifies what we want to produce a glyph for (character, image, ...),
1642 and where in the glyph matrix we currently are (glyph row and hpos).
1643 produce_glyphs fills in output fields of *IT with information such as the
1644 pixel width and height of a character, and maybe output actual glyphs at
a168702a 1645 the same time if IT->glyph_row is non-null. See the explanation of
b50fe468
RS
1646 struct display_iterator in dispextern.h for an overview.
1647
1648 produce_glyphs also stores the result of glyph width, ascent
1649 etc. computations in *IT.
1650
1651 IT->glyph_row may be null, in which case produce_glyphs does not
1652 actually fill in the glyphs. This is used in the move_* functions
1653 in xdisp.c for text width and height computations.
1654
1655 Callers usually don't call produce_glyphs directly;
1656 instead they use the macro PRODUCE_GLYPHS. */
a168702a 1657
177c0ea7 1658void
a168702a
GM
1659produce_glyphs (it)
1660 struct it *it;
1661{
1662 /* If a hook is installed, let it do the work. */
1663 xassert (it->what == IT_CHARACTER
c7cba11d 1664 || it->what == IT_COMPOSITION
a168702a
GM
1665 || it->what == IT_IMAGE
1666 || it->what == IT_STRETCH);
177c0ea7 1667
c7cba11d
KH
1668 /* Nothing but characters are supported on terminal frames. For a
1669 composition sequence, it->c is the first character of the
1670 sequence. */
1671 xassert (it->what == IT_CHARACTER
1672 || it->what == IT_COMPOSITION);
a168702a
GM
1673
1674 if (it->c >= 040 && it->c < 0177)
1675 {
1676 it->pixel_width = it->nglyphs = 1;
1677 if (it->glyph_row)
1678 append_glyph (it);
1679 }
1680 else if (it->c == '\n')
1681 it->pixel_width = it->nglyphs = 0;
1682 else if (it->c == '\t')
1683 {
2efdbcdd 1684 int absolute_x = (it->current_x
a168702a 1685 + it->continuation_lines_width);
177c0ea7
JB
1686 int next_tab_x
1687 = (((1 + absolute_x + it->tab_width - 1)
a168702a
GM
1688 / it->tab_width)
1689 * it->tab_width);
1690 int nspaces;
1691
1692 /* If part of the TAB has been displayed on the previous line
1693 which is continued now, continuation_lines_width will have
1694 been incremented already by the part that fitted on the
1695 continued line. So, we will get the right number of spaces
1696 here. */
1697 nspaces = next_tab_x - absolute_x;
177c0ea7 1698
a168702a
GM
1699 if (it->glyph_row)
1700 {
1701 int n = nspaces;
177c0ea7 1702
a168702a
GM
1703 it->c = ' ';
1704 it->pixel_width = it->len = 1;
177c0ea7 1705
a168702a
GM
1706 while (n--)
1707 append_glyph (it);
177c0ea7 1708
a168702a
GM
1709 it->c = '\t';
1710 }
1711
1712 it->pixel_width = nspaces;
1713 it->nglyphs = nspaces;
1714 }
add44890
EZ
1715 else if (SINGLE_BYTE_CHAR_P (it->c))
1716 {
cf872af5 1717 /* Coming here means that it->c is from display table, thus we
add44890
EZ
1718 must send the code as is to the terminal. Although there's
1719 no way to know how many columns it occupies on a screen, it
1720 is a good assumption that a single byte code has 1-column
1721 width. */
1722 it->pixel_width = it->nglyphs = 1;
1723 if (it->glyph_row)
1724 append_glyph (it);
1725 }
a168702a
GM
1726 else
1727 {
c7cba11d
KH
1728 /* A multi-byte character. The display width is fixed for all
1729 characters of the set. Some of the glyphs may have to be
1730 ignored because they are already displayed in a continued
1731 line. */
a168702a
GM
1732 int charset = CHAR_CHARSET (it->c);
1733
c7cba11d 1734 it->pixel_width = CHARSET_WIDTH (charset);
a168702a 1735 it->nglyphs = it->pixel_width;
177c0ea7 1736
a168702a
GM
1737 if (it->glyph_row)
1738 append_glyph (it);
1739 }
1740
177c0ea7 1741 /* Advance current_x by the pixel width as a convenience for
a168702a
GM
1742 the caller. */
1743 if (it->area == TEXT_AREA)
1744 it->current_x += it->pixel_width;
cfe8a05e
GM
1745 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1746 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
a168702a
GM
1747}
1748
1749
1750/* Get information about special display element WHAT in an
1751 environment described by IT. WHAT is one of IT_TRUNCATION or
1752 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1753 non-null glyph_row member. This function ensures that fields like
1754 face_id, c, len of IT are left untouched. */
1755
1756void
1757produce_special_glyphs (it, what)
1758 struct it *it;
1759 enum display_element_type what;
1760{
1761 struct it temp_it;
177c0ea7 1762
a168702a
GM
1763 temp_it = *it;
1764 temp_it.dp = NULL;
1765 temp_it.what = IT_CHARACTER;
1766 temp_it.len = 1;
7c752c80 1767 temp_it.object = make_number (0);
a168702a
GM
1768 bzero (&temp_it.current, sizeof temp_it.current);
1769
1770 if (what == IT_CONTINUATION)
1771 {
1772 /* Continuation glyph. */
1773 if (it->dp
1774 && INTEGERP (DISP_CONTINUE_GLYPH (it->dp))
1775 && GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (it->dp))))
1776 {
1777 temp_it.c = FAST_GLYPH_CHAR (XINT (DISP_CONTINUE_GLYPH (it->dp)));
68526af5 1778 temp_it.len = CHAR_BYTES (temp_it.c);
a168702a
GM
1779 }
1780 else
1781 temp_it.c = '\\';
177c0ea7 1782
a168702a
GM
1783 produce_glyphs (&temp_it);
1784 it->pixel_width = temp_it.pixel_width;
1785 it->nglyphs = temp_it.pixel_width;
1786 }
1787 else if (what == IT_TRUNCATION)
1788 {
1789 /* Truncation glyph. */
1790 if (it->dp
1791 && INTEGERP (DISP_TRUNC_GLYPH (it->dp))
1792 && GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (it->dp))))
1793 {
1794 temp_it.c = FAST_GLYPH_CHAR (XINT (DISP_TRUNC_GLYPH (it->dp)));
68526af5 1795 temp_it.len = CHAR_BYTES (temp_it.c);
a168702a
GM
1796 }
1797 else
1798 temp_it.c = '$';
177c0ea7 1799
a168702a
GM
1800 produce_glyphs (&temp_it);
1801 it->pixel_width = temp_it.pixel_width;
1802 it->nglyphs = temp_it.pixel_width;
1803 }
1804 else
1805 abort ();
1806}
1807
1808
a168702a
GM
1809\f
1810/***********************************************************************
1811 Faces
1812 ***********************************************************************/
1813
4e6ba4a4
GM
1814/* Value is non-zero if attribute ATTR may be used. ATTR should be
1815 one of the enumerators from enum no_color_bit, or a bit set built
1816 from them. Some display attributes may not be used together with
1817 color; the termcap capability `NC' specifies which ones. */
1818
1819#define MAY_USE_WITH_COLORS_P(ATTR) \
1820 (TN_max_colors > 0 \
1821 ? (TN_no_color_video & (ATTR)) == 0 \
1822 : 1)
a168702a
GM
1823
1824/* Turn appearances of face FACE_ID on tty frame F on. */
1825
1826static void
1827turn_on_face (f, face_id)
1828 struct frame *f;
1829 int face_id;
1830{
1831 struct face *face = FACE_FROM_ID (f, face_id);
86a7d192
GM
1832 long fg = face->foreground;
1833 long bg = face->background;
a168702a 1834
86a7d192
GM
1835 /* Do this first because TS_end_standout_mode may be the same
1836 as TS_exit_attribute_mode, which turns all appearances off. */
1837 if (MAY_USE_WITH_COLORS_P (NC_REVERSE))
1838 {
1839 if (TN_max_colors > 0)
1840 {
1841 if (fg >= 0 && bg >= 0)
1842 {
1843 /* If the terminal supports colors, we can set them
1844 below without using reverse video. The face's fg
1845 and bg colors are set as they should appear on
1846 the screen, i.e. they take the inverse-video'ness
1847 of the face already into account. */
1848 }
1849 else if (inverse_video)
1850 {
1851 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1852 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1853 toggle_highlight ();
1854 }
1855 else
1856 {
1857 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1858 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1859 toggle_highlight ();
1860 }
1861 }
1862 else
1863 {
1864 /* If we can't display colors, use reverse video
1865 if the face specifies that. */
37526b42
GM
1866 if (inverse_video)
1867 {
1868 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1869 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1870 toggle_highlight ();
1871 }
1872 else
1873 {
1874 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1875 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1876 toggle_highlight ();
1877 }
86a7d192
GM
1878 }
1879 }
a168702a
GM
1880
1881 if (face->tty_bold_p)
4e6ba4a4
GM
1882 {
1883 if (MAY_USE_WITH_COLORS_P (NC_BOLD))
28d440ab 1884 OUTPUT1_IF (FRAME_TTY (f), TS_enter_bold_mode);
4e6ba4a4 1885 }
a168702a 1886 else if (face->tty_dim_p)
4e6ba4a4 1887 if (MAY_USE_WITH_COLORS_P (NC_DIM))
28d440ab 1888 OUTPUT1_IF (FRAME_TTY (f), TS_enter_dim_mode);
a168702a
GM
1889
1890 /* Alternate charset and blinking not yet used. */
4e6ba4a4
GM
1891 if (face->tty_alt_charset_p
1892 && MAY_USE_WITH_COLORS_P (NC_ALT_CHARSET))
28d440ab 1893 OUTPUT1_IF (FRAME_TTY (f), TS_enter_alt_charset_mode);
a168702a 1894
4e6ba4a4
GM
1895 if (face->tty_blinking_p
1896 && MAY_USE_WITH_COLORS_P (NC_BLINK))
28d440ab 1897 OUTPUT1_IF (FRAME_TTY (f), TS_enter_blink_mode);
a168702a 1898
54800acb 1899 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (NC_UNDERLINE))
28d440ab 1900 OUTPUT1_IF (FRAME_TTY (f), TS_enter_underline_mode);
a168702a 1901
a168702a
GM
1902 if (TN_max_colors > 0)
1903 {
1904 char *p;
177c0ea7 1905
86a7d192 1906 if (fg >= 0 && TS_set_foreground)
a168702a 1907 {
86a7d192 1908 p = tparam (TS_set_foreground, NULL, 0, (int) fg);
28d440ab 1909 OUTPUT (FRAME_TTY (f), p);
a168702a
GM
1910 xfree (p);
1911 }
1912
86a7d192 1913 if (bg >= 0 && TS_set_background)
a168702a 1914 {
86a7d192 1915 p = tparam (TS_set_background, NULL, 0, (int) bg);
28d440ab 1916 OUTPUT (FRAME_TTY (f), p);
a168702a
GM
1917 xfree (p);
1918 }
1919 }
1920}
177c0ea7 1921
a168702a
GM
1922
1923/* Turn off appearances of face FACE_ID on tty frame F. */
1924
1925static void
1926turn_off_face (f, face_id)
1927 struct frame *f;
1928 int face_id;
1929{
1930 struct face *face = FACE_FROM_ID (f, face_id);
a168702a
GM
1931
1932 xassert (face != NULL);
1933
1934 if (TS_exit_attribute_mode)
1935 {
1936 /* Capability "me" will turn off appearance modes double-bright,
1937 half-bright, reverse-video, standout, underline. It may or
1938 may not turn off alt-char-mode. */
1939 if (face->tty_bold_p
1940 || face->tty_dim_p
1941 || face->tty_reverse_p
1942 || face->tty_alt_charset_p
1943 || face->tty_blinking_p
1944 || face->tty_underline_p)
65aa5e85 1945 {
28d440ab 1946 OUTPUT1_IF (FRAME_TTY (f), TS_exit_attribute_mode);
65aa5e85
GM
1947 if (strcmp (TS_exit_attribute_mode, TS_end_standout_mode) == 0)
1948 standout_mode = 0;
1949 }
a168702a
GM
1950
1951 if (face->tty_alt_charset_p)
28d440ab 1952 OUTPUT_IF (FRAME_TTY (f), TS_exit_alt_charset_mode);
a168702a
GM
1953 }
1954 else
1955 {
1956 /* If we don't have "me" we can only have those appearances
1957 that have exit sequences defined. */
1958 if (face->tty_alt_charset_p)
28d440ab 1959 OUTPUT_IF (FRAME_TTY (f), TS_exit_alt_charset_mode);
a168702a 1960
54800acb 1961 if (face->tty_underline_p)
28d440ab 1962 OUTPUT_IF (FRAME_TTY (f), TS_exit_underline_mode);
a168702a
GM
1963 }
1964
1965 /* Switch back to default colors. */
1966 if (TN_max_colors > 0
f9d2fdc4
EZ
1967 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
1968 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
1969 || (face->background != FACE_TTY_DEFAULT_COLOR
1970 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
28d440ab 1971 OUTPUT1_IF (FRAME_TTY (f), TS_orig_pair);
a168702a 1972}
177c0ea7
JB
1973
1974
b63a55ac
MB
1975/* Return non-zero if the terminal on frame F supports all of the
1976 capabilities in CAPS simultaneously, with foreground and background
1977 colors FG and BG. */
1978
4a8130f2
MB
1979int
1980tty_capable_p (f, caps, fg, bg)
b63a55ac
MB
1981 struct frame *f;
1982 unsigned caps;
1983 unsigned long fg, bg;
1984{
1985#define TTY_CAPABLE_P_TRY(cap, TS, NC_bit) \
1986 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(NC_bit))) \
1987 return 0;
1988
1989 TTY_CAPABLE_P_TRY (TTY_CAP_INVERSE, TS_standout_mode, NC_REVERSE);
1990 TTY_CAPABLE_P_TRY (TTY_CAP_UNDERLINE, TS_enter_underline_mode, NC_UNDERLINE);
1991 TTY_CAPABLE_P_TRY (TTY_CAP_BOLD, TS_enter_bold_mode, NC_BOLD);
1992 TTY_CAPABLE_P_TRY (TTY_CAP_DIM, TS_enter_dim_mode, NC_DIM);
1993 TTY_CAPABLE_P_TRY (TTY_CAP_BLINK, TS_enter_blink_mode, NC_BLINK);
1994 TTY_CAPABLE_P_TRY (TTY_CAP_ALT_CHARSET, TS_enter_alt_charset_mode, NC_ALT_CHARSET);
1995
1996 /* We can do it! */
1997 return 1;
1998}
1999
2000
a168702a
GM
2001/* Return non-zero if the terminal is capable to display colors. */
2002
2003DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
981e4297 2004 0, 1, 0,
99fdd6bc
EZ
2005 doc: /* Return non-nil if TTY can display colors on DISPLAY. */)
2006 (display)
2007 Lisp_Object display;
a168702a
GM
2008{
2009 return TN_max_colors > 0 ? Qt : Qnil;
2010}
2011
bfa62f96
EZ
2012/* Return the number of supported colors. */
2013DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2014 Stty_display_color_cells, 0, 1, 0,
99fdd6bc
EZ
2015 doc: /* Return the number of colors supported by TTY on DISPLAY. */)
2016 (display)
2017 Lisp_Object display;
bfa62f96
EZ
2018{
2019 return make_number (TN_max_colors);
2020}
2021
ace28297 2022#ifndef WINDOWSNT
a168702a 2023
ace28297
EZ
2024/* Save or restore the default color-related capabilities of this
2025 terminal. */
2026static void
2027tty_default_color_capabilities (save)
2028 int save;
2029{
2030 static char
2031 *default_orig_pair, *default_set_foreground, *default_set_background;
2032 static int default_max_colors, default_max_pairs, default_no_color_video;
2033
2034 if (save)
2035 {
2036 if (default_orig_pair)
2037 xfree (default_orig_pair);
2038 default_orig_pair = TS_orig_pair ? xstrdup (TS_orig_pair) : NULL;
2039
2040 if (default_set_foreground)
2041 xfree (default_set_foreground);
2042 default_set_foreground = TS_set_foreground ? xstrdup (TS_set_foreground)
2043 : NULL;
2044
2045 if (default_set_background)
2046 xfree (default_set_background);
2047 default_set_background = TS_set_background ? xstrdup (TS_set_background)
2048 : NULL;
2049
2050 default_max_colors = TN_max_colors;
2051 default_max_pairs = TN_max_pairs;
2052 default_no_color_video = TN_no_color_video;
2053 }
2054 else
2055 {
2056 TS_orig_pair = default_orig_pair;
2057 TS_set_foreground = default_set_foreground;
2058 TS_set_background = default_set_background;
2059 TN_max_colors = default_max_colors;
2060 TN_max_pairs = default_max_pairs;
2061 TN_no_color_video = default_no_color_video;
2062 }
2063}
2064
2065/* Setup one of the standard tty color schemes according to MODE.
2066 MODE's value is generally the number of colors which we want to
2067 support; zero means set up for the default capabilities, the ones
2068 we saw at term_init time; -1 means turn off color support. */
2069void
2070tty_setup_colors (mode)
2071 int mode;
2072{
40409f05
EZ
2073 /* Canonicalize all negative values of MODE. */
2074 if (mode < -1)
2075 mode = -1;
2076
ace28297
EZ
2077 switch (mode)
2078 {
2079 case -1: /* no colors at all */
2080 TN_max_colors = 0;
2081 TN_max_pairs = 0;
2082 TN_no_color_video = 0;
2083 TS_set_foreground = TS_set_background = TS_orig_pair = NULL;
2084 break;
2085 case 0: /* default colors, if any */
2086 default:
2087 tty_default_color_capabilities (0);
2088 break;
2089 case 8: /* 8 standard ANSI colors */
2090 TS_orig_pair = "\033[0m";
2091#ifdef TERMINFO
2092 TS_set_foreground = "\033[3%p1%dm";
2093 TS_set_background = "\033[4%p1%dm";
2094#else
2095 TS_set_foreground = "\033[3%dm";
2096 TS_set_background = "\033[4%dm";
2097#endif
2098 TN_max_colors = 8;
2099 TN_max_pairs = 64;
2100 TN_no_color_video = 0;
2101 break;
2102 }
2103}
2104
2105void
2106set_tty_color_mode (f, val)
2107 struct frame *f;
2108 Lisp_Object val;
2109{
dfc7a077 2110 Lisp_Object color_mode_spec, current_mode_spec;
ace28297
EZ
2111 Lisp_Object color_mode, current_mode;
2112 int mode, old_mode;
2113 extern Lisp_Object Qtty_color_mode;
2114 Lisp_Object tty_color_mode_alist;
2115
2116 tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
2117 Qnil);
2118
9ac61a89 2119 if (INTEGERP (val))
ace28297
EZ
2120 color_mode = val;
2121 else
2122 {
2123 if (NILP (tty_color_mode_alist))
2124 color_mode_spec = Qnil;
2125 else
2126 color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
ace28297
EZ
2127
2128 if (CONSP (color_mode_spec))
2129 color_mode = XCDR (color_mode_spec);
2130 else
2131 color_mode = Qnil;
2132 }
545d91d5
RS
2133
2134 current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
2135
ace28297
EZ
2136 if (CONSP (current_mode_spec))
2137 current_mode = XCDR (current_mode_spec);
2138 else
2139 current_mode = Qnil;
9ac61a89 2140 if (INTEGERP (color_mode))
ace28297
EZ
2141 mode = XINT (color_mode);
2142 else
2143 mode = 0; /* meaning default */
9ac61a89 2144 if (INTEGERP (current_mode))
ace28297
EZ
2145 old_mode = XINT (current_mode);
2146 else
2147 old_mode = 0;
2148
2149 if (mode != old_mode)
2150 {
2151 tty_setup_colors (mode);
2152 /* This recomputes all the faces given the new color
2153 definitions. */
2154 call0 (intern ("tty-set-up-initial-frame-faces"));
2155 redraw_frame (f);
2156 }
2157}
2158
2159#endif /* !WINDOWSNT */
a168702a
GM
2160
2161\f
28d440ab
KL
2162
2163struct tty_output *
2164get_named_tty (name)
2165 char *name;
2166{
2167 struct tty_output *tty = tty_list;
2168
2169 while (tty) {
2170 if ((tty->name == 0 && name == 0)
2171 || (name && tty->name && !strcmp (tty->name, name)))
2172 return tty;
2173 tty = tty->next;
2174 };
2175
2176 return 0;
2177}
2178
2179\f
a168702a
GM
2180/***********************************************************************
2181 Initialization
2182 ***********************************************************************/
2183
28d440ab
KL
2184struct tty_output *
2185term_dummy_init (void)
2186{
2187 if (initialized || tty_list)
2188 error ("tty already initialized");
2189
2190 tty_list = xmalloc (sizeof (struct tty_output));
2191 bzero (tty_list, sizeof (struct tty_output));
2192 TTY_NAME (tty_list) = 0;
2193 TTY_INPUT (tty_list) = stdin;
2194 TTY_OUTPUT (tty_list) = stdout;
2195 return tty_list;
2196}
2197
2198
2199struct tty_output *
2200term_init (name, terminal_type)
2201 char *name;
08a24c47
JB
2202 char *terminal_type;
2203{
2204 char *area;
2205 char **address = &area;
3a06a6d9 2206 char *buffer = NULL;
5846981b 2207 int buffer_size = 4096;
08a24c47
JB
2208 register char *p;
2209 int status;
e52f4e08 2210 struct frame *sf = XFRAME (selected_frame);
08a24c47 2211
28d440ab
KL
2212 struct tty_output *tty;
2213
2214 tty = get_named_tty (name);
2215 if (tty)
2216 {
2217 /* Return the previously initialized terminal, except if it is the dummy
2218 terminal created for the initial frame. */
2219 if (tty->type)
2220 return tty;
2221 }
2222 else
2223 {
2224 if (!terminal_type)
2225 error ("Unknown terminal type");
2226
2227 tty = (struct tty_output *) xmalloc (sizeof (struct tty_output));
2228 bzero (tty, sizeof (struct tty_output));
2229 tty->next = tty_list;
2230 tty_list = tty;
2231 }
2232
2233 if (name)
2234 {
2235 int fd;
2236 FILE *f;
2237 fd = emacs_open (name, O_RDWR, 0);
2238 if (fd < 0)
2239 {
2240 tty_list = tty->next;
2241 xfree (tty);
2242 error ("could not open file: %s", name);
2243 }
2244 f = fdopen (fd, "w+");
2245 TTY_NAME (tty) = xstrdup (name);
2246 TTY_INPUT (tty) = f;
2247 TTY_OUTPUT (tty) = f;
2248 }
2249 else
2250 {
2251 TTY_NAME (tty) = 0;
2252 TTY_INPUT (tty) = stdin;
2253 TTY_OUTPUT (tty) = stdout;
2254 }
2255
2256 init_sys_modes (tty);
2257
cb28b9c2 2258#ifdef WINDOWSNT
29f27c39 2259 initialize_w32_display ();
cb28b9c2
RS
2260
2261 Wcm_clear ();
cb28b9c2 2262
a678d9ff 2263 area = (char *) xmalloc (2044);
cb28b9c2 2264
9882535b
KS
2265 FrameRows = FRAME_LINES (sf);
2266 FrameCols = FRAME_COLS (sf);
2267 specified_window = FRAME_LINES (sf);
cb28b9c2
RS
2268
2269 delete_in_insert_mode = 1;
2270
2271 UseTabs = 0;
28d440ab 2272 TTY_SCROLL_REGION_OK (tty) = 0;
cb28b9c2
RS
2273
2274 /* Seems to insert lines when it's not supposed to, messing
2275 up the display. In doing a trace, it didn't seem to be
2276 called much, so I don't think we're losing anything by
2277 turning it off. */
28d440ab 2278 TTY_LINE_INS_DEL_OK (tty) = 0;
cb28b9c2 2279
28d440ab 2280 TTY_CHAR_INS_DEL_OK (tty) = 1;
cb28b9c2
RS
2281
2282 baud_rate = 19200;
2283
e52f4e08
GM
2284 FRAME_CAN_HAVE_SCROLL_BARS (sf) = 0;
2285 FRAME_VERTICAL_SCROLL_BAR_TYPE (sf) = vertical_scroll_bar_none;
acfcd5cd 2286 TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
cb28b9c2 2287
28d440ab 2288 return tty;
eccec691 2289#else /* not WINDOWSNT */
cb28b9c2 2290
08a24c47 2291 Wcm_clear ();
08a24c47 2292
28d440ab
KL
2293 TTY_TYPE (tty) = xstrdup (terminal_type);
2294
3a06a6d9 2295 buffer = (char *) xmalloc (buffer_size);
08a24c47
JB
2296 status = tgetent (buffer, terminal_type);
2297 if (status < 0)
b0347178
KH
2298 {
2299#ifdef TERMINFO
e12c1054 2300 fatal ("Cannot open terminfo database file");
b0347178 2301#else
e12c1054 2302 fatal ("Cannot open termcap database file");
b0347178
KH
2303#endif
2304 }
08a24c47 2305 if (status == 0)
b0347178
KH
2306 {
2307#ifdef TERMINFO
2308 fatal ("Terminal type %s is not defined.\n\
2309If that is not the actual type of terminal you have,\n\
2310use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2311`setenv TERM ...') to specify the correct type. It may be necessary\n\
e12c1054 2312to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
28d440ab 2313 terminal_type);
b0347178
KH
2314#else
2315 fatal ("Terminal type %s is not defined.\n\
c5a9c3e6
RS
2316If that is not the actual type of terminal you have,\n\
2317use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2318`setenv TERM ...') to specify the correct type. It may be necessary\n\
e12c1054 2319to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
28d440ab 2320 terminal_type);
b0347178
KH
2321#endif
2322 }
3a06a6d9 2323
f730033e 2324#ifndef TERMINFO
3a06a6d9 2325 if (strlen (buffer) >= buffer_size)
08a24c47 2326 abort ();
f730033e 2327 buffer_size = strlen (buffer);
3dd3a502 2328#endif
f730033e 2329 area = (char *) xmalloc (buffer_size);
08a24c47
JB
2330
2331 TS_ins_line = tgetstr ("al", address);
2332 TS_ins_multi_lines = tgetstr ("AL", address);
2333 TS_bell = tgetstr ("bl", address);
2334 BackTab = tgetstr ("bt", address);
2335 TS_clr_to_bottom = tgetstr ("cd", address);
2336 TS_clr_line = tgetstr ("ce", address);
ff11dfa1 2337 TS_clr_frame = tgetstr ("cl", address);
2efdbcdd 2338 ColPosition = NULL; /* tgetstr ("ch", address); */
08a24c47
JB
2339 AbsPosition = tgetstr ("cm", address);
2340 CR = tgetstr ("cr", address);
2341 TS_set_scroll_region = tgetstr ("cs", address);
2342 TS_set_scroll_region_1 = tgetstr ("cS", address);
2343 RowPosition = tgetstr ("cv", address);
2344 TS_del_char = tgetstr ("dc", address);
2345 TS_del_multi_chars = tgetstr ("DC", address);
2346 TS_del_line = tgetstr ("dl", address);
2347 TS_del_multi_lines = tgetstr ("DL", address);
2348 TS_delete_mode = tgetstr ("dm", address);
2349 TS_end_delete_mode = tgetstr ("ed", address);
2350 TS_end_insert_mode = tgetstr ("ei", address);
2351 Home = tgetstr ("ho", address);
2352 TS_ins_char = tgetstr ("ic", address);
2353 TS_ins_multi_chars = tgetstr ("IC", address);
2354 TS_insert_mode = tgetstr ("im", address);
2355 TS_pad_inserted_char = tgetstr ("ip", address);
2356 TS_end_keypad_mode = tgetstr ("ke", address);
2357 TS_keypad_mode = tgetstr ("ks", address);
2358 LastLine = tgetstr ("ll", address);
2359 Right = tgetstr ("nd", address);
2360 Down = tgetstr ("do", address);
2361 if (!Down)
2362 Down = tgetstr ("nl", address); /* Obsolete name for "do" */
2363#ifdef VMS
2364 /* VMS puts a carriage return before each linefeed,
2365 so it is not safe to use linefeeds. */
2366 if (Down && Down[0] == '\n' && Down[1] == '\0')
2367 Down = 0;
2368#endif /* VMS */
2369 if (tgetflag ("bs"))
2370 Left = "\b"; /* can't possibly be longer! */
2371 else /* (Actually, "bs" is obsolete...) */
2372 Left = tgetstr ("le", address);
2373 if (!Left)
2374 Left = tgetstr ("bc", address); /* Obsolete name for "le" */
2375 TS_pad_char = tgetstr ("pc", address);
2376 TS_repeat = tgetstr ("rp", address);
2377 TS_end_standout_mode = tgetstr ("se", address);
2378 TS_fwd_scroll = tgetstr ("sf", address);
2379 TS_standout_mode = tgetstr ("so", address);
2380 TS_rev_scroll = tgetstr ("sr", address);
2381 Wcm.cm_tab = tgetstr ("ta", address);
2382 TS_end_termcap_modes = tgetstr ("te", address);
2383 TS_termcap_modes = tgetstr ("ti", address);
2384 Up = tgetstr ("up", address);
2385 TS_visible_bell = tgetstr ("vb", address);
a168702a
GM
2386 TS_cursor_normal = tgetstr ("ve", address);
2387 TS_cursor_visible = tgetstr ("vs", address);
2388 TS_cursor_invisible = tgetstr ("vi", address);
08a24c47 2389 TS_set_window = tgetstr ("wi", address);
177c0ea7 2390
a168702a
GM
2391 TS_enter_underline_mode = tgetstr ("us", address);
2392 TS_exit_underline_mode = tgetstr ("ue", address);
a168702a
GM
2393 TS_enter_bold_mode = tgetstr ("md", address);
2394 TS_enter_dim_mode = tgetstr ("mh", address);
2395 TS_enter_blink_mode = tgetstr ("mb", address);
2396 TS_enter_reverse_mode = tgetstr ("mr", address);
2397 TS_enter_alt_charset_mode = tgetstr ("as", address);
2398 TS_exit_alt_charset_mode = tgetstr ("ae", address);
2399 TS_exit_attribute_mode = tgetstr ("me", address);
177c0ea7 2400
08a24c47
JB
2401 MultiUp = tgetstr ("UP", address);
2402 MultiDown = tgetstr ("DO", address);
2403 MultiLeft = tgetstr ("LE", address);
2404 MultiRight = tgetstr ("RI", address);
2405
e7f90eab
GM
2406 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
2407 color because we can't switch back to the default foreground and
2408 background. */
a168702a 2409 TS_orig_pair = tgetstr ("op", address);
e7f90eab 2410 if (TS_orig_pair)
a168702a 2411 {
e7f90eab
GM
2412 TS_set_foreground = tgetstr ("AF", address);
2413 TS_set_background = tgetstr ("AB", address);
2414 if (!TS_set_foreground)
2415 {
2416 /* SVr4. */
2417 TS_set_foreground = tgetstr ("Sf", address);
2418 TS_set_background = tgetstr ("Sb", address);
2419 }
177c0ea7 2420
e7f90eab
GM
2421 TN_max_colors = tgetnum ("Co");
2422 TN_max_pairs = tgetnum ("pa");
177c0ea7 2423
4e6ba4a4
GM
2424 TN_no_color_video = tgetnum ("NC");
2425 if (TN_no_color_video == -1)
28d440ab 2426 TN_no_color_video = 0;
a168702a 2427 }
a168702a 2428
ace28297
EZ
2429 tty_default_color_capabilities (1);
2430
e4058338
KH
2431 MagicWrap = tgetflag ("xn");
2432 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
2433 the former flag imply the latter. */
2434 AutoWrap = MagicWrap || tgetflag ("am");
28d440ab 2435 TTY_MEMORY_BELOW_FRAME (tty) = tgetflag ("db");
08a24c47 2436 TF_hazeltine = tgetflag ("hz");
28d440ab 2437 TTY_MUST_WRITE_SPACES (tty) = tgetflag ("in");
08a24c47
JB
2438 meta_key = tgetflag ("km") || tgetflag ("MT");
2439 TF_insmode_motion = tgetflag ("mi");
2440 TF_standout_motion = tgetflag ("ms");
2441 TF_underscore = tgetflag ("ul");
08a24c47
JB
2442 TF_teleray = tgetflag ("xt");
2443
5c2c7893
JB
2444 term_get_fkeys (address);
2445
ff11dfa1 2446 /* Get frame size from system, or else from termcap. */
3b12ce12
RS
2447 {
2448 int height, width;
2449 get_frame_size (&width, &height);
9882535b
KS
2450 FRAME_COLS (sf) = width;
2451 FRAME_LINES (sf) = height;
3b12ce12
RS
2452 }
2453
9882535b
KS
2454 if (FRAME_COLS (sf) <= 0)
2455 SET_FRAME_COLS (sf, tgetnum ("co"));
1efd8636
RS
2456 else
2457 /* Keep width and external_width consistent */
9882535b
KS
2458 SET_FRAME_COLS (sf, FRAME_COLS (sf));
2459 if (FRAME_LINES (sf) <= 0)
2460 FRAME_LINES (sf) = tgetnum ("li");
177c0ea7 2461
9882535b 2462 if (FRAME_LINES (sf) < 3 || FRAME_COLS (sf) < 3)
e12c1054 2463 fatal ("Screen size %dx%d is too small",
28d440ab 2464 FRAME_LINES (sf), FRAME_COLS (sf));
ee7a2de4 2465
8a56675d 2466#if 0 /* This is not used anywhere. */
28d440ab 2467 TTY_MIN_PADDING_SPEED (tty) = tgetnum ("pb");
8a56675d 2468#endif
28d440ab 2469
08a24c47
JB
2470 TabWidth = tgetnum ("tw");
2471
2472#ifdef VMS
2473 /* These capabilities commonly use ^J.
2474 I don't know why, but sending them on VMS does not work;
2475 it causes following spaces to be lost, sometimes.
2476 For now, the simplest fix is to avoid using these capabilities ever. */
2477 if (Down && Down[0] == '\n')
2478 Down = 0;
2479#endif /* VMS */
2480
2481 if (!TS_bell)
2482 TS_bell = "\07";
2483
2484 if (!TS_fwd_scroll)
2485 TS_fwd_scroll = Down;
2486
2487 PC = TS_pad_char ? *TS_pad_char : 0;
2488
2489 if (TabWidth < 0)
2490 TabWidth = 8;
177c0ea7 2491
08a24c47
JB
2492/* Turned off since /etc/termcap seems to have :ta= for most terminals
2493 and newer termcap doc does not seem to say there is a default.
2494 if (!Wcm.cm_tab)
2495 Wcm.cm_tab = "\t";
2496*/
2497
54800acb
MB
2498 /* We don't support standout modes that use `magic cookies', so
2499 turn off any that do. */
2500 if (TS_standout_mode && tgetnum ("sg") >= 0)
2501 {
2502 TS_standout_mode = 0;
2503 TS_end_standout_mode = 0;
2504 }
2505 if (TS_enter_underline_mode && tgetnum ("ug") >= 0)
2506 {
2507 TS_enter_underline_mode = 0;
2508 TS_exit_underline_mode = 0;
2509 }
2510
2511 /* If there's no standout mode, try to use underlining instead. */
08a24c47
JB
2512 if (TS_standout_mode == 0)
2513 {
54800acb
MB
2514 TS_standout_mode = TS_enter_underline_mode;
2515 TS_end_standout_mode = TS_exit_underline_mode;
08a24c47
JB
2516 }
2517
afd359c4
RS
2518 /* If no `se' string, try using a `me' string instead.
2519 If that fails, we can't use standout mode at all. */
2520 if (TS_end_standout_mode == 0)
2521 {
e4bfb3b6 2522 char *s = tgetstr ("me", address);
afd359c4 2523 if (s != 0)
28d440ab 2524 TS_end_standout_mode = s;
afd359c4 2525 else
28d440ab 2526 TS_standout_mode = 0;
afd359c4
RS
2527 }
2528
08a24c47
JB
2529 if (TF_teleray)
2530 {
2531 Wcm.cm_tab = 0;
54800acb
MB
2532 /* We can't support standout mode, because it uses magic cookies. */
2533 TS_standout_mode = 0;
08a24c47
JB
2534 /* But that means we cannot rely on ^M to go to column zero! */
2535 CR = 0;
2536 /* LF can't be trusted either -- can alter hpos */
2537 /* if move at column 0 thru a line with TS_standout_mode */
2538 Down = 0;
2539 }
2540
2541 /* Special handling for certain terminal types known to need it */
2542
2543 if (!strcmp (terminal_type, "supdup"))
2544 {
28d440ab 2545 TTY_MEMORY_BELOW_FRAME (tty) = 1;
08a24c47
JB
2546 Wcm.cm_losewrap = 1;
2547 }
2548 if (!strncmp (terminal_type, "c10", 3)
2549 || !strcmp (terminal_type, "perq"))
2550 {
2551 /* Supply a makeshift :wi string.
2552 This string is not valid in general since it works only
2553 for windows starting at the upper left corner;
2554 but that is all Emacs uses.
28d440ab 2555
ff11dfa1 2556 This string works only if the frame is using
08a24c47
JB
2557 the top of the video memory, because addressing is memory-relative.
2558 So first check the :ti string to see if that is true.
2559
2560 It would be simpler if the :wi string could go in the termcap
2561 entry, but it can't because it is not fully valid.
2562 If it were in the termcap entry, it would confuse other programs. */
2563 if (!TS_set_window)
2564 {
2565 p = TS_termcap_modes;
2566 while (*p && strcmp (p, "\033v "))
2567 p++;
2568 if (*p)
2569 TS_set_window = "\033v%C %C %C %C ";
2570 }
2571 /* Termcap entry often fails to have :in: flag */
28d440ab 2572 TTY_MUST_WRITE_SPACES (tty) = 1;
08a24c47
JB
2573 /* :ti string typically fails to have \E^G! in it */
2574 /* This limits scope of insert-char to one line. */
2575 strcpy (area, TS_termcap_modes);
2576 strcat (area, "\033\007!");
2577 TS_termcap_modes = area;
2578 area += strlen (area) + 1;
2579 p = AbsPosition;
2580 /* Change all %+ parameters to %C, to handle
28d440ab 2581 values above 96 correctly for the C100. */
08a24c47 2582 while (*p)
28d440ab
KL
2583 {
2584 if (p[0] == '%' && p[1] == '+')
2585 p[1] = 'C';
2586 p++;
2587 }
08a24c47
JB
2588 }
2589
9882535b
KS
2590 FrameRows = FRAME_LINES (sf);
2591 FrameCols = FRAME_COLS (sf);
2592 specified_window = FRAME_LINES (sf);
08a24c47
JB
2593
2594 if (Wcm_init () == -1) /* can't do cursor motion */
2595#ifdef VMS
2596 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2597It lacks the ability to position the cursor.\n\
2598If that is not the actual type of terminal you have, use either the\n\
2599DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
e12c1054 2600or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
08a24c47 2601 terminal_type);
37dad45a
RS
2602#else /* not VMS */
2603# ifdef TERMINFO
28d440ab 2604 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
37dad45a
RS
2605It lacks the ability to position the cursor.\n\
2606If that is not the actual type of terminal you have,\n\
2607use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2608`setenv TERM ...') to specify the correct type. It may be necessary\n\
e12c1054 2609to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
28d440ab 2610 terminal_type);
37dad45a 2611# else /* TERMCAP */
28d440ab 2612 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
08a24c47
JB
2613It lacks the ability to position the cursor.\n\
2614If that is not the actual type of terminal you have,\n\
c5a9c3e6
RS
2615use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2616`setenv TERM ...') to specify the correct type. It may be necessary\n\
e12c1054 2617to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
28d440ab 2618 terminal_type);
37dad45a
RS
2619# endif /* TERMINFO */
2620#endif /*VMS */
9882535b
KS
2621 if (FRAME_LINES (sf) <= 0
2622 || FRAME_COLS (sf) <= 0)
e12c1054 2623 fatal ("The frame size has not been specified");
08a24c47
JB
2624
2625 delete_in_insert_mode
2626 = TS_delete_mode && TS_insert_mode
28d440ab 2627 && !strcmp (TS_delete_mode, TS_insert_mode);
08a24c47
JB
2628
2629 se_is_so = (TS_standout_mode
28d440ab
KL
2630 && TS_end_standout_mode
2631 && !strcmp (TS_standout_mode, TS_end_standout_mode));
08a24c47 2632
08a24c47
JB
2633 UseTabs = tabs_safe_p () && TabWidth == 8;
2634
28d440ab 2635 TTY_SCROLL_REGION_OK (tty)
08a24c47
JB
2636 = (Wcm.cm_abs
2637 && (TS_set_window || TS_set_scroll_region || TS_set_scroll_region_1));
2638
28d440ab 2639 TTY_LINE_INS_DEL_OK (tty)
8a56675d
KL
2640 = (((TS_ins_line || TS_ins_multi_lines)
2641 && (TS_del_line || TS_del_multi_lines))
28d440ab 2642 || (TTY_SCROLL_REGION_OK (tty)
8a56675d 2643 && TS_fwd_scroll && TS_rev_scroll));
28d440ab
KL
2644
2645 TTY_CHAR_INS_DEL_OK (tty)
8a56675d
KL
2646 = ((TS_ins_char || TS_insert_mode
2647 || TS_pad_inserted_char || TS_ins_multi_chars)
2648 && (TS_del_char || TS_del_multi_chars));
2649
28d440ab 2650 TTY_FAST_CLEAR_END_OF_LINE (tty) = TS_clr_line != 0;
08a24c47
JB
2651
2652 init_baud_rate ();
28d440ab
KL
2653 if (read_socket_hook) /* Baudrate is somewhat
2654 meaningless in this case */
08a24c47 2655 baud_rate = 9600;
20a558dc 2656
e52f4e08
GM
2657 FRAME_CAN_HAVE_SCROLL_BARS (sf) = 0;
2658 FRAME_VERTICAL_SCROLL_BAR_TYPE (sf) = vertical_scroll_bar_none;
eccec691 2659#endif /* WINDOWSNT */
3a06a6d9
RS
2660
2661 xfree (buffer);
28d440ab
KL
2662
2663 return tty;
08a24c47
JB
2664}
2665
2666/* VARARGS 1 */
dfcf069d 2667void
08a24c47 2668fatal (str, arg1, arg2)
4746118a 2669 char *str, *arg1, *arg2;
08a24c47
JB
2670{
2671 fprintf (stderr, "emacs: ");
2672 fprintf (stderr, str, arg1, arg2);
e12c1054 2673 fprintf (stderr, "\n");
08a24c47
JB
2674 fflush (stderr);
2675 exit (1);
2676}
07c57952 2677
dfcf069d 2678void
07c57952
KH
2679syms_of_term ()
2680{
7ee72033
MB
2681 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
2682 doc: /* Non-nil means the system uses terminfo rather than termcap.
228299fa 2683This variable can be used by terminal emulator packages. */);
07c57952
KH
2684#ifdef TERMINFO
2685 system_uses_terminfo = 1;
2686#else
2687 system_uses_terminfo = 0;
2688#endif
c291d9ef 2689
7ee72033
MB
2690 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
2691 doc: /* Non-nil means call this function to ring the bell.
228299fa 2692The function should accept no arguments. */);
c291d9ef 2693 Vring_bell_function = Qnil;
a168702a
GM
2694
2695 defsubr (&Stty_display_color_p);
bfa62f96 2696 defsubr (&Stty_display_color_cells);
07c57952 2697}
a168702a 2698
28d440ab
KL
2699struct tty_output *
2700get_current_tty ()
2701{
2702 return CURTTY();
2703}
2704
2705
ab5796a9
MB
2706/* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
2707 (do not change this comment) */