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