home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / term.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  60KB  |  1,982 lines

  1. /* terminal control module for terminals described by TERMCAP
  2.    Copyright (C) 1985, 86, 87, 93, 94, 95 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21. /* Modified for emx by Jeremy Bowen, May 1999 based on patches
  22.    to v19.33 by Eberhard Mattes */
  23.  
  24. #include <config.h>
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include "termchar.h"
  28. #include "termopts.h"
  29. #undef NULL
  30. #include "lisp.h"
  31. #include "charset.h"
  32. #include "coding.h"
  33. #include "frame.h"
  34. #include "disptab.h"
  35. #include "termhooks.h"
  36. #include "keyboard.h"
  37. #include "dispextern.h"
  38. #include "cm.h"
  39.  
  40. #ifdef HAVE_X_WINDOWS
  41. #ifndef HAVE_PM
  42. #include "xterm.h"
  43. #else /* HAVE_PM */
  44. #include "pmterm.h"
  45. #endif /* HAVE_PM */
  46. #endif    /* HAVE_X_WINDOWS */
  47.  
  48. #define max(a, b) ((a) > (b) ? (a) : (b))
  49. #define min(a, b) ((a) < (b) ? (a) : (b))
  50.  
  51. #define OUTPUT(a) tputs (a, (int) (FRAME_HEIGHT (selected_frame) - curY), cmputc)
  52. #define OUTPUT1(a) tputs (a, 1, cmputc)
  53. #define OUTPUTL(a, lines) tputs (a, lines, cmputc)
  54. #define OUTPUT_IF(a) { if (a) tputs (a, (int) (FRAME_HEIGHT (selected_frame) - curY), cmputc); }
  55. #define OUTPUT1_IF(a) { if (a) tputs (a, 1, cmputc); }
  56.  
  57. /* Function to use to ring the bell.  */
  58. Lisp_Object Vring_bell_function;
  59.  
  60. /* Terminal characteristics that higher levels want to look at.
  61.    These are all extern'd in termchar.h */
  62.  
  63. int must_write_spaces;        /* Nonzero means spaces in the text
  64.                    must actually be output; can't just skip
  65.                    over some columns to leave them blank.  */
  66. int min_padding_speed;        /* Speed below which no padding necessary */
  67.  
  68. int line_ins_del_ok;        /* Terminal can insert and delete lines */
  69. int char_ins_del_ok;        /* Terminal can insert and delete chars */
  70. int scroll_region_ok;        /* Terminal supports setting the
  71.                    scroll window */
  72. int scroll_region_cost;        /* Cost of setting a scroll window,
  73.                    measured in characters */
  74. int memory_below_frame;        /* Terminal remembers lines
  75.                    scrolled off bottom */
  76. int fast_clear_end_of_line;    /* Terminal has a `ce' string */
  77.  
  78. /* Nonzero means no need to redraw the entire frame on resuming
  79.    a suspended Emacs.  This is useful on terminals with multiple pages,
  80.    where one page is used for Emacs and another for all else. */
  81. int no_redraw_on_reenter;
  82.  
  83. /* Hook functions that you can set to snap out the functions in this file.
  84.    These are all extern'd in termhooks.h  */
  85.  
  86. void (*cursor_to_hook) P_ ((int, int));
  87. void (*raw_cursor_to_hook) P_ ((int, int));
  88.  
  89. void (*clear_to_end_hook) P_ ((void));
  90. void (*clear_frame_hook) P_ ((void));
  91. void (*clear_end_of_line_hook) P_ ((int));
  92.  
  93. void (*ins_del_lines_hook) P_ ((int, int));
  94.  
  95. void (*change_line_highlight_hook) P_ ((int, int, int));
  96. void (*reassert_line_highlight_hook) P_ ((int, int));
  97.  
  98. void (*insert_glyphs_hook) P_ ((GLYPH *, int));
  99. void (*write_glyphs_hook) P_ ((GLYPH *, int));
  100. void (*delete_glyphs_hook) P_ ((int));
  101.  
  102. void (*ring_bell_hook) P_ ((void));
  103.  
  104. void (*reset_terminal_modes_hook) P_ ((void));
  105. void (*set_terminal_modes_hook) P_ ((void));
  106. void (*update_begin_hook) P_ ((struct frame *));
  107. void (*update_end_hook) P_ ((struct frame *));
  108. void (*set_terminal_window_hook) P_ ((int));
  109.  
  110. int (*read_socket_hook) P_ ((int, struct input_event *, int, int));
  111.  
  112. void (*frame_up_to_date_hook) P_ ((struct frame *));
  113.  
  114. /* Return the current position of the mouse.
  115.  
  116.    Set *f to the frame the mouse is in, or zero if the mouse is in no
  117.    Emacs frame.  If it is set to zero, all the other arguments are
  118.    garbage.
  119.  
  120.    If the motion started in a scroll bar, set *bar_window to the
  121.    scroll bar's window, *part to the part the mouse is currently over,
  122.    *x to the position of the mouse along the scroll bar, and *y to the
  123.    overall length of the scroll bar.
  124.  
  125.    Otherwise, set *bar_window to Qnil, and *x and *y to the column and
  126.    row of the character cell the mouse is over.
  127.  
  128.    Set *time to the time the mouse was at the returned position.
  129.  
  130.    This should clear mouse_moved until the next motion
  131.    event arrives.  */
  132. void (*mouse_position_hook) P_ ((FRAME_PTR *f, int insist,
  133.                  Lisp_Object *bar_window,
  134.                  enum scroll_bar_part *part,
  135.                  Lisp_Object *x,
  136.                  Lisp_Object *y,
  137.                  unsigned long *time));
  138.  
  139. /* When reading from a minibuffer in a different frame, Emacs wants
  140.    to shift the highlight from the selected frame to the minibuffer's
  141.    frame; under X, this means it lies about where the focus is.
  142.    This hook tells the window system code to re-decide where to put
  143.    the highlight.  */
  144. void (*frame_rehighlight_hook) P_ ((FRAME_PTR f));
  145.  
  146. /* If we're displaying frames using a window system that can stack
  147.    frames on top of each other, this hook allows you to bring a frame
  148.    to the front, or bury it behind all the other windows.  If this
  149.    hook is zero, that means the device we're displaying on doesn't
  150.    support overlapping frames, so there's no need to raise or lower
  151.    anything.
  152.  
  153.    If RAISE is non-zero, F is brought to the front, before all other
  154.    windows.  If RAISE is zero, F is sent to the back, behind all other
  155.    windows.  */
  156. void (*frame_raise_lower_hook) P_ ((FRAME_PTR f, int raise));
  157.  
  158. /* Set the vertical scroll bar for WINDOW to have its upper left corner
  159.    at (TOP, LEFT), and be LENGTH rows high.  Set its handle to
  160.    indicate that we are displaying PORTION characters out of a total
  161.    of WHOLE characters, starting at POSITION.  If WINDOW doesn't yet
  162.    have a scroll bar, create one for it.  */
  163. void (*set_vertical_scroll_bar_hook)
  164.      P_ ((struct window *window,
  165.       int portion, int whole, int position));
  166.  
  167.  
  168. /* The following three hooks are used when we're doing a thorough
  169.    redisplay of the frame.  We don't explicitly know which scroll bars
  170.    are going to be deleted, because keeping track of when windows go
  171.    away is a real pain - can you say set-window-configuration?
  172.    Instead, we just assert at the beginning of redisplay that *all*
  173.    scroll bars are to be removed, and then save scroll bars from the
  174.    fiery pit when we actually redisplay their window.  */
  175.  
  176. /* Arrange for all scroll bars on FRAME to be removed at the next call
  177.    to `*judge_scroll_bars_hook'.  A scroll bar may be spared if
  178.    `*redeem_scroll_bar_hook' is applied to its window before the judgement. 
  179.  
  180.    This should be applied to each frame each time its window tree is
  181.    redisplayed, even if it is not displaying scroll bars at the moment;
  182.    if the HAS_SCROLL_BARS flag has just been turned off, only calling
  183.    this and the judge_scroll_bars_hook will get rid of them.
  184.  
  185.    If non-zero, this hook should be safe to apply to any frame,
  186.    whether or not it can support scroll bars, and whether or not it is
  187.    currently displaying them.  */
  188. void (*condemn_scroll_bars_hook) P_ ((FRAME_PTR frame));
  189.  
  190. /* Unmark WINDOW's scroll bar for deletion in this judgement cycle.
  191.    Note that it's okay to redeem a scroll bar that is not condemned.  */
  192. void (*redeem_scroll_bar_hook) P_ ((struct window *window));
  193.  
  194. /* Remove all scroll bars on FRAME that haven't been saved since the
  195.    last call to `*condemn_scroll_bars_hook'.  
  196.  
  197.    This should be applied to each frame after each time its window
  198.    tree is redisplayed, even if it is not displaying scroll bars at the
  199.    moment; if the HAS_SCROLL_BARS flag has just been turned off, only
  200.    calling this and condemn_scroll_bars_hook will get rid of them.
  201.  
  202.    If non-zero, this hook should be safe to apply to any frame,
  203.    whether or not it can support scroll bars, and whether or not it is
  204.    currently displaying them.  */
  205. void (*judge_scroll_bars_hook) P_ ((FRAME_PTR FRAME));
  206.  
  207.  
  208. /* Strings, numbers and flags taken from the termcap entry.  */
  209.  
  210. char *TS_end_italic_mode;    /* termcap "ae" */
  211. char *TS_ins_line;        /* "al" */
  212. char *TS_italic_mode;        /* "as" */
  213. char *TS_ins_multi_lines;    /* "AL" (one parameter, # lines to insert) */
  214. char *TS_bell;            /* "bl" */
  215. char *TS_clr_to_bottom;        /* "cd" */
  216. char *TS_clr_line;        /* "ce", clear to end of line */
  217. char *TS_clr_frame;        /* "cl" */
  218. char *TS_set_scroll_region;    /* "cs" (2 params, first line and last line) */
  219. char *TS_set_scroll_region_1;   /* "cS" (4 params: total lines,
  220.                    lines above scroll region, lines below it,
  221.                    total lines again) */
  222. char *TS_del_char;        /* "dc" */
  223. char *TS_del_multi_chars;    /* "DC" (one parameter, # chars to delete) */
  224. char *TS_del_line;        /* "dl" */
  225. char *TS_del_multi_lines;    /* "DL" (one parameter, # lines to delete) */
  226. char *TS_delete_mode;        /* "dm", enter character-delete mode */
  227. char *TS_end_delete_mode;    /* "ed", leave character-delete mode */
  228. char *TS_end_insert_mode;    /* "ei", leave character-insert mode */
  229. char *TS_ins_char;        /* "ic" */
  230. char *TS_ins_multi_chars;    /* "IC" (one parameter, # chars to insert) */
  231. char *TS_insert_mode;        /* "im", enter character-insert mode */
  232. char *TS_pad_inserted_char;    /* "ip".  Just padding, no commands.  */
  233. char *TS_end_keypad_mode;    /* "ke" */
  234. char *TS_keypad_mode;        /* "ks" */
  235. char *TS_bold_mode;        /* "md" */
  236. char *TS_end_bold_mode;        /* "me" */
  237. char *TS_pad_char;        /* "pc", char to use as padding */
  238. char *TS_repeat;        /* "rp" (2 params, # times to repeat
  239.                    and character to be repeated) */
  240. char *TS_end_standout_mode;    /* "se" */
  241. char *TS_fwd_scroll;        /* "sf" */
  242. char *TS_standout_mode;        /* "so" */
  243. char *TS_rev_scroll;        /* "sr" */
  244. char *TS_end_termcap_modes;    /* "te" */
  245. char *TS_termcap_modes;        /* "ti" */
  246. char *TS_end_underscore_mode;    /* "ue" */
  247. char *TS_underscore_mode;    /* "us" */
  248. char *TS_visible_bell;        /* "vb" */
  249. char *TS_end_visual_mode;    /* "ve" */
  250. char *TS_visual_mode;        /* "vi" */
  251. char *TS_set_window;        /* "wi" (4 params, start and end of window,
  252.                    each as vpos and hpos) */
  253.  
  254. int TF_hazeltine;    /* termcap hz flag. */
  255. int TF_insmode_motion;    /* termcap mi flag: can move while in insert mode. */
  256. int TF_standout_motion;    /* termcap mi flag: can move while in standout mode. */
  257. int TF_underscore;    /* termcap ul flag: _ underlines if overstruck on
  258.                nonblank position.  Must clear before writing _.  */
  259. int TF_teleray;        /* termcap xt flag: many weird consequences.
  260.                For t1061. */
  261.  
  262. int TF_xs;        /* Nonzero for "xs".  If set together with
  263.                TN_standout_width == 0, it means don't bother
  264.                to write any end-standout cookies.  */
  265.  
  266. int TN_standout_width;    /* termcap sg number: width occupied by standout
  267.                markers */
  268.  
  269. static int RPov;    /* # chars to start a TS_repeat */
  270.  
  271. static int delete_in_insert_mode;    /* delete mode == insert mode */
  272.  
  273. static int se_is_so;    /* 1 if same string both enters and leaves
  274.                standout mode */
  275.  
  276. /* internal state */
  277.  
  278. /* The largest frame width in any call to calculate_costs.  */
  279. int max_frame_width;
  280. /* The largest frame height in any call to calculate_costs.  */
  281. int max_frame_height;
  282.  
  283. /* Number of chars of space used for standout marker at beginning of line,
  284.    or'd with 0100.  Zero if no standout marker at all.
  285.    The length of these vectors is max_frame_height.
  286.  
  287.    Used IFF TN_standout_width >= 0. */
  288.  
  289. static char *chars_wasted;
  290. static char *copybuf;
  291.  
  292. /* nonzero means supposed to write text in standout mode.  */
  293. int standout_requested;
  294.  
  295. int insert_mode;            /* Nonzero when in insert mode.  */
  296. int standout_mode;            /* Nonzero when in standout mode.  */
  297.  
  298. /* Size of window specified by higher levels.
  299.    This is the number of lines, from the top of frame downwards,
  300.    which can participate in insert-line/delete-line operations.
  301.  
  302.    Effectively it excludes the bottom frame_height - specified_window_size
  303.    lines from those operations.  */
  304.  
  305. int specified_window;
  306.  
  307. /* Frame currently being redisplayed; 0 if not currently redisplaying.
  308.    (Direct output does not count).  */
  309.  
  310. FRAME_PTR updating_frame;
  311.  
  312. /* Provided for lisp packages.  */
  313. static int system_uses_terminfo;
  314.  
  315. char *tparam ();
  316.  
  317. extern char *tgetstr ();
  318.  
  319.  
  320. #ifdef WINDOWSNT
  321. /* We aren't X windows, but we aren't termcap either.  This makes me
  322.    uncertain as to what value to use for frame.output_method.  For
  323.    this file, we'll define FRAME_TERMCAP_P to be zero so that our
  324.    output hooks get called instead of the termcap functions.  Probably
  325.    the best long-term solution is to define an output_windows_nt...  */
  326.  
  327. #undef FRAME_TERMCAP_P
  328. #define FRAME_TERMCAP_P(_f_) 0
  329. #endif /* WINDOWSNT */
  330.  
  331. void
  332. ring_bell ()
  333. {
  334.   if (! NILP (Vring_bell_function))
  335.     {
  336.       Lisp_Object function;
  337.  
  338.       /* Temporarily set the global variable to nil
  339.      so that if we get an error, it stays nil
  340.      and we don't call it over and over.
  341.  
  342.      We don't specbind it, because that would carefully
  343.      restore the bad value if there's an error
  344.      and make the loop of errors happen anyway.  */
  345.       function = Vring_bell_function;
  346.       Vring_bell_function = Qnil;
  347.  
  348.       call0 (function);
  349.  
  350.       Vring_bell_function = function;
  351.       return;
  352.     }
  353.  
  354.   if (! FRAME_TERMCAP_P (selected_frame))
  355.     {
  356.       (*ring_bell_hook) ();
  357.       return;
  358.     }
  359.   OUTPUT (TS_visible_bell && visible_bell ? TS_visible_bell : TS_bell);
  360. }
  361.  
  362. void
  363. set_terminal_modes ()
  364. {
  365.   if (! FRAME_TERMCAP_P (selected_frame))
  366.     {
  367.       (*set_terminal_modes_hook) ();
  368.       return;
  369.     }
  370.   OUTPUT_IF (TS_termcap_modes);
  371.   OUTPUT_IF (TS_visual_mode);
  372.   OUTPUT_IF (TS_keypad_mode);
  373.   losecursor ();
  374. }
  375.  
  376. void
  377. reset_terminal_modes ()
  378. {
  379.   if (! FRAME_TERMCAP_P (selected_frame))
  380.     {
  381.       if (reset_terminal_modes_hook)
  382.     (*reset_terminal_modes_hook) ();
  383.       return;
  384.     }
  385.   if (TN_standout_width < 0)
  386.     turn_off_highlight ();
  387.   turn_off_insert ();
  388.   OUTPUT_IF (TS_end_keypad_mode);
  389.   OUTPUT_IF (TS_end_visual_mode);
  390.   OUTPUT_IF (TS_end_termcap_modes);
  391. #ifdef EMX
  392.   _fsetmode (stdout, "t");
  393. #endif /* EMX */
  394.   /* Output raw CR so kernel can track the cursor hpos.  */
  395.   /* But on magic-cookie terminals this can erase an end-standout marker and
  396.      cause the rest of the frame to be in standout, so move down first.  */
  397.   if (TN_standout_width >= 0)
  398.     cmputc ('\n');
  399.   cmputc ('\r');
  400. }
  401.  
  402. void
  403. update_begin (f)
  404.      FRAME_PTR f;
  405. {
  406.   updating_frame = f;
  407.   if (! FRAME_TERMCAP_P (updating_frame))
  408.     (*update_begin_hook) (f);
  409. }
  410.  
  411. void
  412. update_end (f)
  413.      FRAME_PTR f;
  414. {
  415.   if (! FRAME_TERMCAP_P (updating_frame))
  416.     {
  417.       (*update_end_hook) (f);
  418.       updating_frame = 0;
  419.       return;
  420.     }
  421.   turn_off_insert ();
  422.   background_highlight ();
  423.   standout_requested = 0;
  424.   updating_frame = 0;
  425. }
  426.  
  427. void
  428. set_terminal_window (size)
  429.      int size;
  430. {
  431.   if (! FRAME_TERMCAP_P (updating_frame))
  432.     {
  433.       (*set_terminal_window_hook) (size);
  434.       return;
  435.     }
  436.   specified_window = size ? size : FRAME_HEIGHT (selected_frame);
  437.   if (!scroll_region_ok)
  438.     return;
  439.   set_scroll_region (0, specified_window);
  440. }
  441.  
  442. void
  443. set_scroll_region (start, stop)
  444.      int start, stop;
  445. {
  446.   char *buf;
  447.   if (TS_set_scroll_region)
  448.     {
  449.       buf = tparam (TS_set_scroll_region, 0, 0, start, stop - 1);
  450.     }
  451.   else if (TS_set_scroll_region_1)
  452.     {
  453.       buf = tparam (TS_set_scroll_region_1, 0, 0,
  454.             FRAME_HEIGHT (selected_frame), start,
  455.             FRAME_HEIGHT (selected_frame) - stop,
  456.             FRAME_HEIGHT (selected_frame));
  457.     }
  458.   else
  459.     {
  460.       buf = tparam (TS_set_window, 0, 0, start, 0, stop, FRAME_WIDTH (selected_frame));
  461.     }
  462.   OUTPUT (buf);
  463.   xfree (buf);
  464.   losecursor ();
  465. }
  466.  
  467. void
  468. turn_on_insert ()
  469. {
  470.   if (!insert_mode)
  471.     OUTPUT (TS_insert_mode);
  472.   insert_mode = 1;
  473. }
  474.  
  475. void
  476. turn_off_insert ()
  477. {
  478.   if (insert_mode)
  479.     OUTPUT (TS_end_insert_mode);
  480.   insert_mode = 0;
  481. }
  482.  
  483. /* Handle highlighting when TN_standout_width (termcap sg) is not specified.
  484.    In these terminals, output is affected by the value of standout
  485.    mode when the output is written.
  486.  
  487.    These functions are called on all terminals, but do nothing
  488.    on terminals whose standout mode does not work that way.  */
  489.  
  490. void
  491. turn_off_highlight ()
  492. {
  493.   if (TN_standout_width < 0)
  494.     {
  495.       if (standout_mode)
  496.     OUTPUT_IF (TS_end_standout_mode);
  497.       standout_mode = 0;
  498.     }
  499. }
  500.  
  501. void
  502. turn_on_highlight ()
  503. {
  504.   if (TN_standout_width < 0)
  505.     {
  506.       if (!standout_mode)
  507.     OUTPUT_IF (TS_standout_mode);
  508.       standout_mode = 1;
  509.     }
  510. }
  511.  
  512. /* Set standout mode to the state it should be in for
  513.    empty space inside windows.  What this is,
  514.    depends on the user option inverse-video.  */
  515.  
  516. void
  517. background_highlight ()
  518. {
  519.   if (TN_standout_width >= 0)
  520.     return;
  521.   if (inverse_video)
  522.     turn_on_highlight ();
  523.   else
  524.     turn_off_highlight ();
  525. }
  526.  
  527. /* Set standout mode to the mode specified for the text to be output.  */
  528.  
  529. static void
  530. highlight_if_desired ()
  531. {
  532.   if (TN_standout_width >= 0)
  533.     return;
  534.   if (!inverse_video == !standout_requested)
  535.     turn_off_highlight ();
  536.   else
  537.     turn_on_highlight ();
  538. }
  539.  
  540. /* Handle standout mode for terminals in which TN_standout_width >= 0.
  541.    On these terminals, standout is controlled by markers that
  542.    live inside the terminal's memory.  TN_standout_width is the width
  543.    that the marker occupies in memory.  Standout runs from the marker
  544.    to the end of the line on some terminals, or to the next
  545.    turn-off-standout marker (TS_end_standout_mode) string
  546.    on other terminals.  */
  547.  
  548. /* Write a standout marker or end-standout marker at the front of the line
  549.    at vertical position vpos.  */
  550.  
  551. void
  552. write_standout_marker (flag, vpos)
  553.      int flag, vpos;
  554. {
  555.   if (flag || (TS_end_standout_mode && !TF_teleray && !se_is_so
  556.            && !(TF_xs && TN_standout_width == 0)))
  557.     {
  558.       cmgoto (vpos, 0);
  559.       cmplus (TN_standout_width);
  560.       OUTPUT (flag ? TS_standout_mode : TS_end_standout_mode);
  561.       chars_wasted[curY] = TN_standout_width | 0100;
  562.     }
  563. }
  564.  
  565. /* External interface to control of standout mode.
  566.    Call this when about to modify line at position VPOS
  567.    and not change whether it is highlighted.  */
  568.  
  569. void
  570. reassert_line_highlight (highlight, vpos)
  571.      int highlight;
  572.      int vpos;
  573. {
  574.   if (! FRAME_TERMCAP_P ((updating_frame ? updating_frame : selected_frame)))
  575.     {
  576.       (*reassert_line_highlight_hook) (highlight, vpos);
  577.       return;
  578.     }
  579.   if (TN_standout_width < 0)
  580.     /* Handle terminals where standout takes affect at output time */
  581.     standout_requested = highlight;
  582.   else if (chars_wasted[vpos] == 0)
  583.     /* For terminals with standout markers, write one on this line
  584.        if there isn't one already.  */
  585.     write_standout_marker (highlight, vpos);
  586. }
  587.  
  588. /* Call this when about to modify line at position VPOS
  589.    and change whether it is highlighted.  */
  590.  
  591. void
  592. change_line_highlight (new_highlight, vpos, first_unused_hpos)
  593.      int new_highlight, vpos, first_unused_hpos;
  594. {
  595.   standout_requested = new_highlight;
  596.   if (! FRAME_TERMCAP_P (updating_frame))
  597.     {
  598.       (*change_line_highlight_hook) (new_highlight, vpos, first_unused_hpos);
  599.       return;
  600.     }
  601.  
  602.   cursor_to (vpos, 0);
  603.  
  604.   if (TN_standout_width < 0)
  605.     background_highlight ();
  606.   /* If line starts with a marker, delete the marker */
  607.   else if (TS_clr_line && chars_wasted[curY])
  608.     {
  609.       turn_off_insert ();
  610.       /* On Teleray, make sure to erase the SO marker.  */
  611.       if (TF_teleray)
  612.     {
  613.       cmgoto (curY - 1, FRAME_WIDTH (selected_frame) - 4);
  614.       OUTPUT ("\033S");
  615.       curY++;        /* ESC S moves to next line where the TS_standout_mode was */
  616.       curX = 0;
  617.     }
  618.       else
  619.     cmgoto (curY, 0);    /* reposition to kill standout marker */
  620.     }
  621.   clear_end_of_line_raw (first_unused_hpos);
  622.   reassert_line_highlight (new_highlight, curY);
  623. }
  624.  
  625.  
  626. /* Move to absolute position, specified origin 0 */
  627.  
  628. void
  629. cursor_to (row, col)
  630.      int row, col;
  631. {
  632.   if (! FRAME_TERMCAP_P ((updating_frame
  633.                 ? updating_frame
  634.                 : selected_frame))
  635.       && cursor_to_hook)
  636.     {
  637.       (*cursor_to_hook) (row, col);
  638.       return;
  639.     }
  640.  
  641.   /* Detect the case where we are called from reset_sys_modes
  642.      and the costs have never been calculated.  Do nothing.  */
  643.   if (chars_wasted == 0)
  644.     return;
  645.  
  646.   col += chars_wasted[row] & 077;
  647.   if (curY == row && curX == col)
  648.     return;
  649.   if (!TF_standout_motion)
  650.     background_highlight ();
  651.   if (!TF_insmode_motion)
  652.     turn_off_insert ();
  653.   cmgoto (row, col);
  654. }
  655.  
  656. /* Similar but don't take any account of the wasted characters.  */
  657.  
  658. void
  659. raw_cursor_to (row, col)
  660.      int row, col;
  661. {
  662.   if (! FRAME_TERMCAP_P ((updating_frame ? updating_frame : selected_frame)))
  663.     {
  664.       (*raw_cursor_to_hook) (row, col);
  665.       return;
  666.     }
  667.   if (curY == row && curX == col)
  668.     return;
  669.   if (!TF_standout_motion)
  670.     background_highlight ();
  671.   if (!TF_insmode_motion)
  672.     turn_off_insert ();
  673.   cmgoto (row, col);
  674. }
  675.  
  676. /* Erase operations */
  677.  
  678. /* clear from cursor to end of frame */
  679. void
  680. clear_to_end ()
  681. {
  682.   register int i;
  683.  
  684.   if (clear_to_end_hook && ! FRAME_TERMCAP_P (updating_frame))
  685.     {
  686.       (*clear_to_end_hook) ();
  687.       return;
  688.     }
  689.   if (TS_clr_to_bottom)
  690.     {
  691.       background_highlight ();
  692.       OUTPUT (TS_clr_to_bottom);
  693.       bzero (chars_wasted + curY, FRAME_HEIGHT (selected_frame) - curY);
  694.     }
  695.   else
  696.     {
  697.       for (i = curY; i < FRAME_HEIGHT (selected_frame); i++)
  698.     {
  699.       cursor_to (i, 0);
  700.       clear_end_of_line_raw (FRAME_WIDTH (selected_frame));
  701.     }
  702.     }
  703. }
  704.  
  705. /* Clear entire frame */
  706.  
  707. void
  708. clear_frame ()
  709. {
  710.   if (clear_frame_hook
  711.       && ! FRAME_TERMCAP_P ((updating_frame ? updating_frame : selected_frame)))
  712.     {
  713.       (*clear_frame_hook) ();
  714.       return;
  715.     }
  716.   if (TS_clr_frame)
  717.     {
  718.       background_highlight ();
  719.       OUTPUT (TS_clr_frame);
  720.       bzero (chars_wasted, FRAME_HEIGHT (selected_frame));
  721.       cmat (0, 0);
  722.     }
  723.   else
  724.     {
  725.       cursor_to (0, 0);
  726.       clear_to_end ();
  727.     }
  728. }
  729.  
  730. /* Clear to end of line, but do not clear any standout marker.
  731.    Assumes that the cursor is positioned at a character of real text,
  732.    which implies it cannot be before a standout marker
  733.    unless the marker has zero width.
  734.  
  735.    Note that the cursor may be moved.  */
  736.  
  737. void
  738. clear_end_of_line (first_unused_hpos)
  739.      int first_unused_hpos;
  740. {
  741.   static GLYPH buf = SPACEGLYPH;
  742.   if (FRAME_TERMCAP_P (selected_frame)
  743.       && chars_wasted != 0
  744.       && TN_standout_width == 0 && curX == 0 && chars_wasted[curY] != 0)
  745.     write_glyphs (&buf, 1);
  746.   clear_end_of_line_raw (first_unused_hpos);
  747. }
  748.  
  749. /* Clear from cursor to end of line.
  750.    Assume that the line is already clear starting at column first_unused_hpos.
  751.    If the cursor is at a standout marker, erase the marker.
  752.  
  753.    Note that the cursor may be moved, on terminals lacking a `ce' string.  */
  754.  
  755. void
  756. clear_end_of_line_raw (first_unused_hpos)
  757.      int first_unused_hpos;
  758. {
  759.   register int i;
  760.  
  761.   if (clear_end_of_line_hook
  762.       && ! FRAME_TERMCAP_P ((updating_frame
  763.                    ? updating_frame
  764.                    : selected_frame)))
  765.     {
  766.       (*clear_end_of_line_hook) (first_unused_hpos);
  767.       return;
  768.     }
  769.  
  770.   /* Detect the case where we are called from reset_sys_modes
  771.      and the costs have never been calculated.  Do nothing.  */
  772.   if (chars_wasted == 0)
  773.     return;
  774.  
  775.   first_unused_hpos += chars_wasted[curY] & 077;
  776.   if (curX >= first_unused_hpos)
  777.     return;
  778.   /* Notice if we are erasing a magic cookie */
  779.   if (curX == 0)
  780.     chars_wasted[curY] = 0;
  781.   background_highlight ();
  782.   if (TS_clr_line)
  783.     {
  784.       OUTPUT1 (TS_clr_line);
  785.     }
  786.   else
  787.     {            /* have to do it the hard way */
  788.       turn_off_insert ();
  789.  
  790.       /* Do not write in last row last col with Autowrap on. */
  791.       if (AutoWrap && curY == FRAME_HEIGHT (selected_frame) - 1
  792.       && first_unused_hpos == FRAME_WIDTH (selected_frame))
  793.     first_unused_hpos--;
  794.  
  795.       for (i = curX; i < first_unused_hpos; i++)
  796.     {
  797.       if (termscript)
  798.         fputc (' ', termscript);
  799.       putchar (' ');
  800.     }
  801.       cmplus (first_unused_hpos - curX);
  802.     }
  803. }
  804.  
  805. /* Encode SRC_LEN glyphs starting at SRC to terminal output codes and
  806.    store them at DST.  Do not write more than DST_LEN bytes.  That may
  807.    require stopping before all SRC_LEN input glyphs have been
  808.    converted.
  809.  
  810.    We store the number of glyphs actually converted in *CONSUMED.  The
  811.    return value is the number of bytes store in DST.  */
  812.  
  813. int
  814. encode_terminal_code (src, dst, src_len, dst_len, consumed)
  815.      GLYPH *src;
  816.      int src_len;
  817.      unsigned char *dst;
  818.      int dst_len, *consumed;
  819. {
  820.     GLYPH *src_start = src, *src_end = src + src_len;
  821.     unsigned char *dst_start = dst, *dst_end = dst + dst_len;
  822.     register GLYPH g;
  823.     unsigned int c;
  824.     unsigned char workbuf[4], *buf;
  825.     int len;
  826.     register int tlen = GLYPH_TABLE_LENGTH;
  827.     register Lisp_Object *tbase = GLYPH_TABLE_BASE;
  828.     struct coding_system *coding;
  829.  
  830.     coding = (CODING_REQUIRE_ENCODING (&terminal_coding)
  831.               ? &terminal_coding
  832.               : &safe_terminal_coding);
  833.  
  834.     while (src < src_end)
  835.     {
  836.         g = *src;
  837.         /* We must skip glyphs to be padded for a wide character.  */
  838.         if (! (g & GLYPH_MASK_PADDING))
  839.         {
  840.             if ((c = GLYPH_CHAR (selected_frame, g)) > MAX_CHAR)
  841.             {
  842.                 c = ' ';
  843.                 g = MAKE_GLYPH (selected_frame, c,
  844.                                 GLYPH_FACE (selected_frame, g));
  845.             }
  846.             if (COMPOSITE_CHAR_P (c))
  847.             {
  848.                 /* If C is a composite character, we can display
  849.                    only the first component.  */
  850.                 g = cmpchar_table[COMPOSITE_CHAR_ID (c)]->glyph[0],
  851.                     c = GLYPH_CHAR (selected_frame, g);
  852.             }
  853.             if (c < tlen)
  854.             {
  855.                 /* G has an entry in Vglyph_table,
  856.                    so process any alias before testing for simpleness.  */
  857.                 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
  858.                 c = GLYPH_CHAR (selected_frame, g);
  859.             }
  860.             if (GLYPH_SIMPLE_P (tbase, tlen, g))
  861.                 /* We set the multi-byte form of C at BUF.  */
  862.                 len = CHAR_STRING (c, workbuf, buf);
  863.             else
  864.             {
  865.                 /* We have a string in Vglyph_table.  */
  866.                 len = GLYPH_LENGTH (tbase, g);
  867.                 buf = GLYPH_STRING (tbase, g);
  868.             }
  869.       
  870.             encode_coding (coding, buf, dst, len, dst_end - dst);
  871.             /* Changes suggested by Ilya Zakharevich */
  872.             len -= coding->consumed;
  873.             dst += coding->produced;
  874.             if (len > 0)
  875.             {
  876.                 if (len > dst_end - dst)
  877.                     /* The remaining output buffer is too short.  We must
  878.                        break the loop here without increasing SRC so that
  879.                        the next call of this function start from the same
  880.                        glyph.  */
  881.                     break;
  882.                 buf += len;
  883.                 while (len--) *dst++ = *buf++;
  884.           
  885.             }      
  886.         }
  887.         src++;
  888.     }
  889.     *consumed = src - src_start;
  890.     return (dst - dst_start);
  891. }
  892.  
  893.  
  894. void
  895. write_glyphs (string, len)
  896.      register GLYPH *string;
  897.      register int len;
  898. {
  899.   register GLYPH g;
  900.   register int tlen = GLYPH_TABLE_LENGTH;
  901.   register Lisp_Object *tbase = GLYPH_TABLE_BASE;
  902.   int produced, consumed;
  903.  
  904.   if (write_glyphs_hook
  905.       && ! FRAME_TERMCAP_P ((updating_frame ? updating_frame : selected_frame)))
  906.     {
  907.       (*write_glyphs_hook) (string, len);
  908.       return;
  909.     }
  910.  
  911.   highlight_if_desired ();
  912.   turn_off_insert ();
  913.  
  914.   /* Don't dare write in last column of bottom line, if AutoWrap,
  915.      since that would scroll the whole frame on some terminals.  */
  916.  
  917.   if (AutoWrap
  918.       && curY + 1 == FRAME_HEIGHT (selected_frame)
  919.       && (curX + len - (chars_wasted[curY] & 077)
  920.       == FRAME_WIDTH (selected_frame)))
  921.     len --;
  922.   if (len <= 0)
  923.     return;
  924.  
  925.   cmplus (len);
  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;
  929.   while (len > 0)
  930.     {
  931.       /* We use shared conversion buffer of the current size (1024
  932.      bytes at least).  Usually it is sufficient, but if not, we
  933.      just repeat the loop.  */
  934.       produced = encode_terminal_code (string, conversion_buffer,
  935.                        len, conversion_buffer_size, &consumed);
  936.       if (produced > 0)
  937.     {
  938.       fwrite (conversion_buffer, 1, produced, stdout);
  939.       if (ferror (stdout))
  940.         clearerr (stdout);
  941.       if (termscript)
  942.         fwrite (conversion_buffer, 1, produced, termscript);
  943.     }
  944.       len -= consumed;
  945.       string += consumed;
  946.     }
  947.   /* We may have to output some codes to terminate the writing.  */
  948.   if (CODING_REQUIRE_FLUSHING (&terminal_coding))
  949.     {
  950.       terminal_coding.mode |= CODING_MODE_LAST_BLOCK;
  951.       encode_coding (&terminal_coding, "", conversion_buffer,
  952.              0, conversion_buffer_size);
  953.       if (terminal_coding.produced > 0)
  954.     {
  955.       fwrite (conversion_buffer, 1, terminal_coding.produced, stdout);
  956.       if (ferror (stdout))
  957.         clearerr (stdout);
  958.       if (termscript)
  959.         fwrite (conversion_buffer, 1, terminal_coding.produced,
  960.             termscript);
  961.     }
  962.     }
  963.   cmcheckmagic ();
  964. }
  965.  
  966. /* If start is zero, insert blanks instead of a string at start */
  967.  
  968. void
  969. insert_glyphs (start, len)
  970.      register GLYPH *start;
  971.      register int len;
  972. {
  973.   char *buf;
  974.   GLYPH g;
  975.   register int tlen = GLYPH_TABLE_LENGTH;
  976.   register Lisp_Object *tbase = GLYPH_TABLE_BASE;
  977.  
  978.   if (len <= 0)
  979.     return;
  980.  
  981.   if (insert_glyphs_hook && ! FRAME_TERMCAP_P (updating_frame))
  982.     {
  983.       (*insert_glyphs_hook) (start, len);
  984.       return;
  985.     }
  986.   highlight_if_desired ();
  987.  
  988.   if (TS_ins_multi_chars)
  989.     {
  990.       buf = tparam (TS_ins_multi_chars, 0, 0, len);
  991.       OUTPUT1 (buf);
  992.       xfree (buf);
  993.       if (start)
  994.     write_glyphs (start, len);
  995.       return;
  996.     }
  997.  
  998.   turn_on_insert ();
  999.   cmplus (len);
  1000.   /* The bit CODING_MODE_LAST_BLOCK should be set to 1 only at the tail.  */
  1001.   terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
  1002.   while (len-- > 0)
  1003.     {
  1004.       int produced, consumed;
  1005.  
  1006.       OUTPUT1_IF (TS_ins_char);
  1007.       if (!start)
  1008.     g = SPACEGLYPH;
  1009.       else
  1010.     {
  1011.       g = *start++;
  1012.       /* We must open sufficient space for a character which
  1013.          occupies more than one column.  */
  1014.       while (*start & GLYPH_MASK_PADDING)
  1015.         {
  1016.           OUTPUT1_IF (TS_ins_char);
  1017.           start++, len--;
  1018.         }
  1019.     }
  1020.  
  1021.       if (len <= 0)
  1022.     /* This is the last glyph.  */
  1023.     terminal_coding.mode |= CODING_MODE_LAST_BLOCK;
  1024.  
  1025.       /* We use shared conversion buffer of the current size (1024
  1026.      bytes at least).  It is surely sufficient for just one glyph.  */
  1027.       produced = encode_terminal_code (&g, conversion_buffer,
  1028.                        1, conversion_buffer_size, &consumed);
  1029.       if (produced > 0)
  1030.     {
  1031.       fwrite (conversion_buffer, 1, produced, stdout);
  1032.       if (ferror (stdout))
  1033.         clearerr (stdout);
  1034.       if (termscript)
  1035.         fwrite (conversion_buffer, 1, produced, termscript);
  1036.     }
  1037.  
  1038.       OUTPUT1_IF (TS_pad_inserted_char);
  1039.     }
  1040.   cmcheckmagic ();
  1041. }
  1042.  
  1043. void
  1044. delete_glyphs (n)
  1045.      register int n;
  1046. {
  1047.   char *buf;
  1048.   register int i;
  1049.  
  1050.   if (delete_glyphs_hook && ! FRAME_TERMCAP_P (updating_frame))
  1051.     {
  1052.       (*delete_glyphs_hook) (n);
  1053.       return;
  1054.     }
  1055.  
  1056.   if (delete_in_insert_mode)
  1057.     {
  1058.       turn_on_insert ();
  1059.     }
  1060.   else
  1061.     {
  1062.       turn_off_insert ();
  1063.       OUTPUT_IF (TS_delete_mode);
  1064.     }
  1065.  
  1066.   if (TS_del_multi_chars)
  1067.     {
  1068.       buf = tparam (TS_del_multi_chars, 0, 0, n);
  1069.       OUTPUT1 (buf);
  1070.       xfree (buf);
  1071.     }
  1072.   else
  1073.     for (i = 0; i < n; i++)
  1074.       OUTPUT1 (TS_del_char);
  1075.   if (!delete_in_insert_mode)
  1076.     OUTPUT_IF (TS_end_delete_mode);
  1077. }
  1078.  
  1079. /* Insert N lines at vpos VPOS.  If N is negative, delete -N lines.  */
  1080.  
  1081. void
  1082. ins_del_lines (vpos, n)
  1083.      int vpos, n;
  1084. {
  1085.   char *multi = n > 0 ? TS_ins_multi_lines : TS_del_multi_lines;
  1086.   char *single = n > 0 ? TS_ins_line : TS_del_line;
  1087.   char *scroll = n > 0 ? TS_rev_scroll : TS_fwd_scroll;
  1088.  
  1089.   register int i = n > 0 ? n : -n;
  1090.   register char *buf;
  1091.  
  1092.   if (ins_del_lines_hook && ! FRAME_TERMCAP_P (updating_frame))
  1093.     {
  1094.       (*ins_del_lines_hook) (vpos, n);
  1095.       return;
  1096.     }
  1097.  
  1098.   /* If the lines below the insertion are being pushed
  1099.      into the end of the window, this is the same as clearing;
  1100.      and we know the lines are already clear, since the matching
  1101.      deletion has already been done.  So can ignore this.  */
  1102.   /* If the lines below the deletion are blank lines coming
  1103.      out of the end of the window, don't bother,
  1104.      as there will be a matching inslines later that will flush them. */
  1105.   if (scroll_region_ok && vpos + i >= specified_window)
  1106.     return;
  1107.   if (!memory_below_frame && vpos + i >= FRAME_HEIGHT (selected_frame))
  1108.     return;
  1109.  
  1110.   if (multi)
  1111.     {
  1112.       raw_cursor_to (vpos, 0);
  1113.       background_highlight ();
  1114.       buf = tparam (multi, 0, 0, i);
  1115.       OUTPUT (buf);
  1116.       xfree (buf);
  1117.     }
  1118.   else if (single)
  1119.     {
  1120.       raw_cursor_to (vpos, 0);
  1121.       background_highlight ();
  1122.       while (--i >= 0)
  1123.     OUTPUT (single);
  1124.       if (TF_teleray)
  1125.     curX = 0;
  1126.     }
  1127.   else
  1128.     {
  1129.       set_scroll_region (vpos, specified_window);
  1130.       if (n < 0)
  1131.     raw_cursor_to (specified_window - 1, 0);
  1132.       else
  1133.     raw_cursor_to (vpos, 0);
  1134.       background_highlight ();
  1135.       while (--i >= 0)
  1136.     OUTPUTL (scroll, specified_window - vpos);
  1137.       set_scroll_region (0, specified_window);
  1138.     }
  1139.  
  1140.   if (TN_standout_width >= 0)
  1141.     {
  1142.       register int lower_limit
  1143.     = (scroll_region_ok
  1144.        ? specified_window
  1145.        : FRAME_HEIGHT (selected_frame));
  1146.  
  1147.       if (n < 0)
  1148.     {
  1149.       bcopy (&chars_wasted[vpos - n], &chars_wasted[vpos],
  1150.          lower_limit - vpos + n);
  1151.       bzero (&chars_wasted[lower_limit + n], - n);
  1152.     }
  1153.       else
  1154.     {
  1155.       bcopy (&chars_wasted[vpos], ©buf[vpos], lower_limit - vpos - n);
  1156.       bcopy (©buf[vpos], &chars_wasted[vpos + n],
  1157.          lower_limit - vpos - n);
  1158.       bzero (&chars_wasted[vpos], n);
  1159.     }
  1160.     }
  1161.   if (!scroll_region_ok && memory_below_frame && n < 0)
  1162.     {
  1163.       cursor_to (FRAME_HEIGHT (selected_frame) + n, 0);
  1164.       clear_to_end ();
  1165.     }
  1166. }
  1167.  
  1168. /* Compute cost of sending "str", in characters,
  1169.    not counting any line-dependent padding.  */
  1170.  
  1171. int
  1172. string_cost (str)
  1173.      char *str;
  1174. {
  1175.   cost = 0;
  1176.   if (str)
  1177.     tputs (str, 0, evalcost);
  1178.   return cost;
  1179. }
  1180.  
  1181. /* Compute cost of sending "str", in characters,
  1182.    counting any line-dependent padding at one line.  */
  1183.  
  1184. static int
  1185. string_cost_one_line (str)
  1186.      char *str;
  1187. {
  1188.   cost = 0;
  1189.   if (str)
  1190.     tputs (str, 1, evalcost);
  1191.   return cost;
  1192. }
  1193.  
  1194. /* Compute per line amount of line-dependent padding,
  1195.    in tenths of characters.  */
  1196.  
  1197. int
  1198. per_line_cost (str)
  1199.      register char *str;
  1200. {
  1201.   cost = 0;
  1202.   if (str)
  1203.     tputs (str, 0, evalcost);
  1204.   cost = - cost;
  1205.   if (str)
  1206.     tputs (str, 10, evalcost);
  1207.   return cost;
  1208. }
  1209.  
  1210. #ifndef old
  1211. /* char_ins_del_cost[n] is cost of inserting N characters.
  1212.    char_ins_del_cost[-n] is cost of deleting N characters.
  1213.    The length of this vector is based on max_frame_width.  */
  1214.  
  1215. int *char_ins_del_vector;
  1216.  
  1217. #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_WIDTH ((f))])
  1218. #endif
  1219.  
  1220. /* ARGSUSED */
  1221. static void
  1222. calculate_ins_del_char_costs (frame)
  1223.      FRAME_PTR frame;
  1224. {
  1225.   int ins_startup_cost, del_startup_cost;
  1226.   int ins_cost_per_char, del_cost_per_char;
  1227.   register int i;
  1228.   register int *p;
  1229.  
  1230.   if (TS_ins_multi_chars)
  1231.     {
  1232.       ins_cost_per_char = 0;
  1233.       ins_startup_cost = string_cost_one_line (TS_ins_multi_chars);
  1234.     }
  1235.   else if (TS_ins_char || TS_pad_inserted_char
  1236.        || (TS_insert_mode && TS_end_insert_mode))
  1237.     {
  1238.       ins_startup_cost = (30 * (string_cost (TS_insert_mode)
  1239.                 + string_cost (TS_end_insert_mode))) / 100;
  1240.       ins_cost_per_char = (string_cost_one_line (TS_ins_char)
  1241.                + string_cost_one_line (TS_pad_inserted_char));
  1242.     }
  1243.   else
  1244.     {
  1245.       ins_startup_cost = 9999;
  1246.       ins_cost_per_char = 0;
  1247.     }
  1248.  
  1249.   if (TS_del_multi_chars)
  1250.     {
  1251.       del_cost_per_char = 0;
  1252.       del_startup_cost = string_cost_one_line (TS_del_multi_chars);
  1253.     }
  1254.   else if (TS_del_char)
  1255.     {
  1256.       del_startup_cost = (string_cost (TS_delete_mode)
  1257.               + string_cost (TS_end_delete_mode));
  1258.       if (delete_in_insert_mode)
  1259.     del_startup_cost /= 2;
  1260.       del_cost_per_char = string_cost_one_line (TS_del_char);
  1261.     }
  1262.   else
  1263.     {
  1264.       del_startup_cost = 9999;
  1265.       del_cost_per_char = 0;
  1266.     }
  1267.  
  1268.   /* Delete costs are at negative offsets */
  1269.   p = &char_ins_del_cost (frame)[0];
  1270.   for (i = FRAME_WIDTH (frame); --i >= 0;)
  1271.     *--p = (del_startup_cost += del_cost_per_char);
  1272.  
  1273.   /* Doing nothing is free */
  1274.   p = &char_ins_del_cost (frame)[0];
  1275.   *p++ = 0;
  1276.  
  1277.   /* Insert costs are at positive offsets */
  1278.   for (i = FRAME_WIDTH (frame); --i >= 0;)
  1279.     *p++ = (ins_startup_cost += ins_cost_per_char);
  1280. }
  1281.  
  1282. void
  1283. calculate_costs (frame)
  1284.      FRAME_PTR frame;
  1285. {
  1286.   register char *f = (TS_set_scroll_region
  1287.               ? TS_set_scroll_region
  1288.               : TS_set_scroll_region_1);
  1289.  
  1290.   FRAME_COST_BAUD_RATE (frame) = baud_rate;
  1291.  
  1292.   scroll_region_cost = string_cost (f);
  1293. #ifdef HAVE_X_WINDOWS
  1294.   if (FRAME_X_P (frame))
  1295.     {
  1296.       do_line_insertion_deletion_costs (frame, 0, ".5*", 0, ".5*",
  1297.                     0, 0,
  1298.                     x_screen_planes (frame));
  1299.       scroll_region_cost = 0;
  1300.       return;
  1301.     }
  1302. #endif
  1303.  
  1304.   /* These variables are only used for terminal stuff.  They are allocated
  1305.      once for the terminal frame of X-windows emacs, but not used afterwards.
  1306.  
  1307.      char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
  1308.      X turns off char_ins_del_ok.
  1309.  
  1310.      chars_wasted and copybuf are only used here in term.c in cases where
  1311.      the term hook isn't called. */
  1312.  
  1313.   max_frame_height = max (max_frame_height, FRAME_HEIGHT (frame));
  1314.   max_frame_width = max (max_frame_width, FRAME_WIDTH (frame));
  1315.  
  1316.   if (chars_wasted != 0)
  1317.     chars_wasted = (char *) xrealloc (chars_wasted, max_frame_height);
  1318.   else
  1319.     chars_wasted = (char *) xmalloc (max_frame_height);
  1320.  
  1321.   if (copybuf != 0)
  1322.     copybuf = (char *) xrealloc (copybuf, max_frame_height);
  1323.   else
  1324.     copybuf = (char *) xmalloc (max_frame_height);
  1325.  
  1326.   if (char_ins_del_vector != 0)
  1327.     char_ins_del_vector
  1328.       = (int *) xrealloc (char_ins_del_vector,
  1329.               (sizeof (int)
  1330.                + 2 * max_frame_width * sizeof (int)));
  1331.   else
  1332.     char_ins_del_vector
  1333.       = (int *) xmalloc (sizeof (int)
  1334.              + 2 * max_frame_width * sizeof (int));
  1335.  
  1336.   bzero (chars_wasted, max_frame_height);
  1337.   bzero (copybuf, max_frame_height);
  1338.   bzero (char_ins_del_vector, (sizeof (int)
  1339.                    + 2 * max_frame_width * sizeof (int)));
  1340.  
  1341.   if (f && (!TS_ins_line && !TS_del_line))
  1342.     do_line_insertion_deletion_costs (frame,
  1343.                       TS_rev_scroll, TS_ins_multi_lines,
  1344.                       TS_fwd_scroll, TS_del_multi_lines,
  1345.                       f, f, 1);
  1346.   else
  1347.     do_line_insertion_deletion_costs (frame,
  1348.                       TS_ins_line, TS_ins_multi_lines,
  1349.                       TS_del_line, TS_del_multi_lines,
  1350.                       0, 0, 1);
  1351.  
  1352.   calculate_ins_del_char_costs (frame);
  1353.  
  1354.   /* Don't use TS_repeat if its padding is worse than sending the chars */
  1355.   if (TS_repeat && per_line_cost (TS_repeat) * baud_rate < 9000)
  1356.     RPov = string_cost (TS_repeat);
  1357.   else
  1358.     RPov = FRAME_WIDTH (frame) * 2;
  1359.  
  1360.   cmcostinit ();        /* set up cursor motion costs */
  1361. }
  1362.  
  1363. struct fkey_table {
  1364.   char *cap, *name;
  1365. };
  1366.  
  1367.   /* Termcap capability names that correspond directly to X keysyms.
  1368.      Some of these (marked "terminfo") aren't supplied by old-style
  1369.      (Berkeley) termcap entries.  They're listed in X keysym order;
  1370.      except we put the keypad keys first, so that if they clash with
  1371.      other keys (as on the IBM PC keyboard) they get overridden.
  1372.   */
  1373.  
  1374. static struct fkey_table keys[] = {
  1375.   "kh", "home",        /* termcap */
  1376.   "kl", "left",        /* termcap */
  1377.   "ku", "up",        /* termcap */
  1378.   "kr", "right",    /* termcap */
  1379.   "kd", "down",        /* termcap */
  1380.   "%8", "prior",    /* terminfo */
  1381.   "%5", "next",        /* terminfo */
  1382.   "@7",    "end",        /* terminfo */
  1383.   "@1", "begin",    /* terminfo */
  1384.   "*6", "select",    /* terminfo */
  1385.   "%9", "print",    /* terminfo */
  1386.   "@4", "execute",    /* terminfo --- actually the `command' key */
  1387.   /*
  1388.    * "insert" --- see below
  1389.    */
  1390.   "&8",    "undo",        /* terminfo */
  1391.   "%0",    "redo",        /* terminfo */
  1392.   "%7",    "menu",        /* terminfo --- actually the `options' key */
  1393.   "@0",    "find",        /* terminfo */
  1394.   "@2",    "cancel",    /* terminfo */
  1395.   "%1", "help",        /* terminfo */
  1396.   /*
  1397.    * "break" goes here, but can't be reliably intercepted with termcap
  1398.    */
  1399.   "&4", "reset",    /* terminfo --- actually `restart' */
  1400.   /*
  1401.    * "system" and "user" --- no termcaps
  1402.    */
  1403.   "kE", "clearline",    /* terminfo */
  1404.   "kA", "insertline",    /* terminfo */
  1405.   "kL", "deleteline",    /* terminfo */
  1406.   "kI", "insertchar",    /* terminfo */
  1407.   "kD", "deletechar",    /* terminfo */
  1408.   "kB", "backtab",    /* terminfo */
  1409.   /*
  1410.    * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
  1411.    */
  1412.   "@8", "kp-enter",    /* terminfo */
  1413.   /*
  1414.    * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
  1415.    * "kp-multiply", "kp-add", "kp-separator",
  1416.    * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
  1417.    * --- no termcaps for any of these.
  1418.    */
  1419.   "K4", "kp-1",        /* terminfo */
  1420.   /*
  1421.    * "kp-2" --- no termcap
  1422.    */
  1423.   "K5", "kp-3",        /* terminfo */
  1424.   /*
  1425.    * "kp-4" --- no termcap
  1426.    */
  1427.   "K2", "kp-5",        /* terminfo */
  1428.   /*
  1429.    * "kp-6" --- no termcap
  1430.    */
  1431.   "K1", "kp-7",        /* terminfo */
  1432.   /*
  1433.    * "kp-8" --- no termcap
  1434.    */
  1435.   "K3", "kp-9",        /* terminfo */
  1436.   /*
  1437.    * "kp-equal" --- no termcap
  1438.    */
  1439.   "k1",    "f1",
  1440.   "k2",    "f2",
  1441.   "k3",    "f3",
  1442.   "k4",    "f4",
  1443.   "k5",    "f5",
  1444.   "k6",    "f6",
  1445.   "k7",    "f7",
  1446.   "k8",    "f8",
  1447.   "k9",    "f9",
  1448.   };
  1449.  
  1450. static char **term_get_fkeys_arg;
  1451. static Lisp_Object term_get_fkeys_1 ();
  1452.  
  1453. /* Find the escape codes sent by the function keys for Vfunction_key_map.
  1454.    This function scans the termcap function key sequence entries, and 
  1455.    adds entries to Vfunction_key_map for each function key it finds.  */
  1456.  
  1457. void
  1458. term_get_fkeys (address)
  1459.      char **address;
  1460. {
  1461.   /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
  1462.      errors during the call.  The only errors should be from Fdefine_key
  1463.      when given a key sequence containing an invalid prefix key.  If the
  1464.      termcap defines function keys which use a prefix that is already bound
  1465.      to a command by the default bindings, we should silently ignore that
  1466.      function key specification, rather than giving the user an error and
  1467.      refusing to run at all on such a terminal.  */
  1468.  
  1469.   extern Lisp_Object Fidentity ();
  1470.   term_get_fkeys_arg = address;
  1471.   internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
  1472. }
  1473.  
  1474. static Lisp_Object
  1475. term_get_fkeys_1 ()
  1476. {
  1477.   int i;
  1478.  
  1479.   char **address = term_get_fkeys_arg;
  1480.  
  1481.   /* This can happen if CANNOT_DUMP or with strange options.  */
  1482.   if (!initialized)
  1483.     Vfunction_key_map = Fmake_sparse_keymap (Qnil);
  1484.  
  1485.   for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
  1486.     {
  1487.       char *sequence = tgetstr (keys[i].cap, address);
  1488.       if (sequence)
  1489.     Fdefine_key (Vfunction_key_map, build_string (sequence),
  1490.              Fmake_vector (make_number (1),
  1491.                    intern (keys[i].name)));
  1492.     }
  1493.  
  1494.   /* The uses of the "k0" capability are inconsistent; sometimes it
  1495.      describes F10, whereas othertimes it describes F0 and "k;" describes F10.
  1496.      We will attempt to politely accommodate both systems by testing for
  1497.      "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
  1498.      */
  1499.   {
  1500.     char *k_semi  = tgetstr ("k;", address);
  1501.     char *k0      = tgetstr ("k0", address);
  1502.     char *k0_name = "f10";
  1503.  
  1504.     if (k_semi)
  1505.       {
  1506.     Fdefine_key (Vfunction_key_map, build_string (k_semi),
  1507.              Fmake_vector (make_number (1), intern ("f10")));
  1508.     k0_name = "f0";
  1509.       }
  1510.  
  1511.     if (k0)
  1512.       Fdefine_key (Vfunction_key_map, build_string (k0),
  1513.            Fmake_vector (make_number (1), intern (k0_name)));
  1514.   }
  1515.  
  1516.   /* Set up cookies for numbered function keys above f10. */
  1517.   {
  1518.     char fcap[3], fkey[4];
  1519.  
  1520.     fcap[0] = 'F'; fcap[2] = '\0';
  1521.     for (i = 11; i < 64; i++)
  1522.       {
  1523.     if (i <= 19)
  1524.       fcap[1] = '1' + i - 11;
  1525.     else if (i <= 45)
  1526.       fcap[1] = 'A' + i - 20;
  1527.     else
  1528.       fcap[1] = 'a' + i - 46;
  1529.  
  1530.     {
  1531.       char *sequence = tgetstr (fcap, address);
  1532.       if (sequence)
  1533.         {
  1534.           sprintf (fkey, "f%d", i);
  1535.           Fdefine_key (Vfunction_key_map, build_string (sequence),
  1536.                Fmake_vector (make_number (1),
  1537.                      intern (fkey)));
  1538.         }
  1539.     }
  1540.       }
  1541.    }
  1542.  
  1543.   /*
  1544.    * Various mappings to try and get a better fit.
  1545.    */
  1546.   {
  1547. #define CONDITIONAL_REASSIGN(cap1, cap2, sym)                \
  1548.       if (!tgetstr (cap1, address))                    \
  1549.     {                                \
  1550.       char *sequence = tgetstr (cap2, address);            \
  1551.       if (sequence)                            \
  1552.         Fdefine_key (Vfunction_key_map, build_string (sequence),    \
  1553.              Fmake_vector (make_number (1),    \
  1554.                        intern (sym)));    \
  1555.     }
  1556.       
  1557.       /* if there's no key_next keycap, map key_npage to `next' keysym */
  1558.       CONDITIONAL_REASSIGN ("%5", "kN", "next");
  1559.       /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
  1560.       CONDITIONAL_REASSIGN ("%8", "kP", "prior");
  1561.       /* if there's no key_dc keycap, map key_ic to `insert' keysym */
  1562.       CONDITIONAL_REASSIGN ("kD", "kI", "insert");
  1563.       /* if there's no key_end keycap, map key_ll to 'end' keysym */
  1564.       CONDITIONAL_REASSIGN ("@7", "kH", "end");
  1565.  
  1566.       /* IBM has their own non-standard dialect of terminfo.
  1567.      If the standard name isn't found, try the IBM name.  */
  1568.       CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
  1569.       CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
  1570.       CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
  1571.       CONDITIONAL_REASSIGN ("%7", "ki", "menu");
  1572.       CONDITIONAL_REASSIGN ("@7", "kw", "end");
  1573.       CONDITIONAL_REASSIGN ("F1", "k<", "f11");
  1574.       CONDITIONAL_REASSIGN ("F2", "k>", "f12");
  1575.       CONDITIONAL_REASSIGN ("%1", "kq", "help");
  1576.       CONDITIONAL_REASSIGN ("*6", "kU", "select");
  1577. #undef CONDITIONAL_REASSIGN
  1578.   }
  1579. }
  1580.  
  1581.  
  1582. void
  1583. term_init (terminal_type)
  1584.      char *terminal_type;
  1585. {
  1586.   char *area;
  1587.   char **address = &area;
  1588.   char buffer[2044];
  1589.   register char *p;
  1590.   int status;
  1591.  
  1592. #ifdef WINDOWSNT
  1593.   initialize_w32_display ();
  1594.  
  1595.   Wcm_clear ();
  1596.  
  1597.   area = (char *) malloc (2044);
  1598.  
  1599.   if (area == 0)
  1600.     abort ();
  1601.  
  1602.   FrameRows = FRAME_HEIGHT (selected_frame);
  1603.   FrameCols = FRAME_WIDTH (selected_frame);
  1604.   specified_window = FRAME_HEIGHT (selected_frame);
  1605.  
  1606.   delete_in_insert_mode = 1;
  1607.  
  1608.   UseTabs = 0;
  1609.   scroll_region_ok = 0;
  1610.  
  1611.   /* Seems to insert lines when it's not supposed to, messing
  1612.      up the display.  In doing a trace, it didn't seem to be
  1613.      called much, so I don't think we're losing anything by
  1614.      turning it off.  */
  1615.  
  1616.   line_ins_del_ok = 0;
  1617.   char_ins_del_ok = 1;
  1618.  
  1619.   baud_rate = 19200;
  1620.  
  1621.   FRAME_CAN_HAVE_SCROLL_BARS (selected_frame) = 0;
  1622.   FRAME_VERTICAL_SCROLL_BAR_TYPE (selected_frame) = vertical_scroll_bar_none;
  1623.  
  1624.   return;
  1625. #else  /* not WINDOWSNT */
  1626.  
  1627.   Wcm_clear ();
  1628.  
  1629.   status = tgetent (buffer, terminal_type);
  1630.   if (status < 0)
  1631.     {
  1632. #ifdef TERMINFO
  1633.       fatal ("Cannot open terminfo database file");
  1634. #else
  1635.       fatal ("Cannot open termcap database file");
  1636. #endif
  1637.     }
  1638.   if (status == 0)
  1639.     {
  1640. #ifdef TERMINFO
  1641.       fatal ("Terminal type %s is not defined.\n\
  1642. If that is not the actual type of terminal you have,\n\
  1643. use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
  1644. `setenv TERM ...') to specify the correct type.  It may be necessary\n\
  1645. to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
  1646.          terminal_type);
  1647. #else
  1648.       fatal ("Terminal type %s is not defined.\n\
  1649. If that is not the actual type of terminal you have,\n\
  1650. use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
  1651. `setenv TERM ...') to specify the correct type.  It may be necessary\n\
  1652. to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
  1653.          terminal_type);
  1654. #endif
  1655.     }
  1656. #ifdef TERMINFO
  1657.   area = (char *) malloc (2044);
  1658. #else
  1659.   area = (char *) malloc (strlen (buffer));
  1660. #endif /* not TERMINFO */
  1661.   if (area == 0)
  1662.     abort ();
  1663.  
  1664.   TS_ins_line = tgetstr ("al", address);
  1665.   TS_ins_multi_lines = tgetstr ("AL", address);
  1666.   TS_bell = tgetstr ("bl", address);
  1667.   BackTab = tgetstr ("bt", address);
  1668.   TS_clr_to_bottom = tgetstr ("cd", address);
  1669.   TS_clr_line = tgetstr ("ce", address);
  1670.   TS_clr_frame = tgetstr ("cl", address);
  1671.   ColPosition = tgetstr ("ch", address);
  1672.   AbsPosition = tgetstr ("cm", address);
  1673.   CR = tgetstr ("cr", address);
  1674.   TS_set_scroll_region = tgetstr ("cs", address);
  1675.   TS_set_scroll_region_1 = tgetstr ("cS", address);
  1676.   RowPosition = tgetstr ("cv", address);
  1677.   TS_del_char = tgetstr ("dc", address);
  1678.   TS_del_multi_chars = tgetstr ("DC", address);
  1679.   TS_del_line = tgetstr ("dl", address);
  1680.   TS_del_multi_lines = tgetstr ("DL", address);
  1681.   TS_delete_mode = tgetstr ("dm", address);
  1682.   TS_end_delete_mode = tgetstr ("ed", address);
  1683.   TS_end_insert_mode = tgetstr ("ei", address);
  1684.   Home = tgetstr ("ho", address);
  1685.   TS_ins_char = tgetstr ("ic", address);
  1686.   TS_ins_multi_chars = tgetstr ("IC", address);
  1687.   TS_insert_mode = tgetstr ("im", address);
  1688.   TS_pad_inserted_char = tgetstr ("ip", address);
  1689.   TS_end_keypad_mode = tgetstr ("ke", address);
  1690.   TS_keypad_mode = tgetstr ("ks", address);
  1691.   LastLine = tgetstr ("ll", address);
  1692.   Right = tgetstr ("nd", address);
  1693.   Down = tgetstr ("do", address);
  1694.   if (!Down)
  1695.     Down = tgetstr ("nl", address); /* Obsolete name for "do" */
  1696. #ifdef VMS
  1697.   /* VMS puts a carriage return before each linefeed,
  1698.      so it is not safe to use linefeeds.  */
  1699.   if (Down && Down[0] == '\n' && Down[1] == '\0')
  1700.     Down = 0;
  1701. #endif /* VMS */
  1702.   if (tgetflag ("bs"))
  1703.     Left = "\b";          /* can't possibly be longer! */
  1704.   else                  /* (Actually, "bs" is obsolete...) */
  1705.     Left = tgetstr ("le", address);
  1706.   if (!Left)
  1707.     Left = tgetstr ("bc", address); /* Obsolete name for "le" */
  1708.   TS_pad_char = tgetstr ("pc", address);
  1709.   TS_repeat = tgetstr ("rp", address);
  1710.   TS_end_standout_mode = tgetstr ("se", address);
  1711.   TS_fwd_scroll = tgetstr ("sf", address);
  1712.   TS_standout_mode = tgetstr ("so", address);
  1713.   TS_rev_scroll = tgetstr ("sr", address);
  1714.   Wcm.cm_tab = tgetstr ("ta", address);
  1715.   TS_end_termcap_modes = tgetstr ("te", address);
  1716.   TS_termcap_modes = tgetstr ("ti", address);
  1717.   TS_bold_mode = tgetstr ("md", address);
  1718.   TS_end_bold_mode = tgetstr ("me", address);
  1719.   TS_underscore_mode = tgetstr ("us", address);
  1720.   TS_end_underscore_mode = tgetstr ("ue", address);
  1721.   Up = tgetstr ("up", address);
  1722.   TS_visible_bell = tgetstr ("vb", address);
  1723.   TS_end_visual_mode = tgetstr ("ve", address);
  1724.   TS_visual_mode = tgetstr ("vs", address);
  1725.   TS_set_window = tgetstr ("wi", address);
  1726.   MultiUp = tgetstr ("UP", address);
  1727.   MultiDown = tgetstr ("DO", address);
  1728.   MultiLeft = tgetstr ("LE", address);
  1729.   MultiRight = tgetstr ("RI", address);
  1730.  
  1731.   MagicWrap = tgetflag ("xn");
  1732.   /* Since we make MagicWrap terminals look like AutoWrap, we need to have
  1733.      the former flag imply the latter.  */
  1734.   AutoWrap = MagicWrap || tgetflag ("am");
  1735.   memory_below_frame = tgetflag ("db");
  1736.   TF_hazeltine = tgetflag ("hz");
  1737.   must_write_spaces = tgetflag ("in");
  1738.   meta_key = tgetflag ("km") || tgetflag ("MT");
  1739.   TF_insmode_motion = tgetflag ("mi");
  1740.   TF_standout_motion = tgetflag ("ms");
  1741.   TF_underscore = tgetflag ("ul");
  1742.   TF_xs = tgetflag ("xs");
  1743.   TF_teleray = tgetflag ("xt");
  1744.  
  1745.   term_get_fkeys (address);
  1746.  
  1747.   /* Get frame size from system, or else from termcap.  */
  1748.   {
  1749.     int height, width;
  1750.     get_frame_size (&width, &height);
  1751.     FRAME_WIDTH (selected_frame) = width;
  1752.     FRAME_HEIGHT (selected_frame) = height;
  1753.   }
  1754.  
  1755.   if (FRAME_WIDTH (selected_frame) <= 0)
  1756.     SET_FRAME_WIDTH (selected_frame, tgetnum ("co"));
  1757.   else
  1758.     /* Keep width and external_width consistent */
  1759.     SET_FRAME_WIDTH (selected_frame, FRAME_WIDTH (selected_frame));
  1760.   if (FRAME_HEIGHT (selected_frame) <= 0)
  1761.     FRAME_HEIGHT (selected_frame) = tgetnum ("li");
  1762.   
  1763.   if (FRAME_HEIGHT (selected_frame) < 3
  1764.       || FRAME_WIDTH (selected_frame) < 3)
  1765.     fatal ("Screen size %dx%d is too small",
  1766.        FRAME_HEIGHT (selected_frame), FRAME_WIDTH (selected_frame));
  1767.  
  1768.   min_padding_speed = tgetnum ("pb");
  1769.   TN_standout_width = tgetnum ("sg");
  1770.   TabWidth = tgetnum ("tw");
  1771.  
  1772. #ifdef VMS
  1773.   /* These capabilities commonly use ^J.
  1774.      I don't know why, but sending them on VMS does not work;
  1775.      it causes following spaces to be lost, sometimes.
  1776.      For now, the simplest fix is to avoid using these capabilities ever.  */
  1777.   if (Down && Down[0] == '\n')
  1778.     Down = 0;
  1779. #endif /* VMS */
  1780.  
  1781.   if (!TS_bell)
  1782.     TS_bell = "\07";
  1783.  
  1784.   if (!TS_fwd_scroll)
  1785.     TS_fwd_scroll = Down;
  1786.  
  1787.   PC = TS_pad_char ? *TS_pad_char : 0;
  1788.  
  1789.   if (TabWidth < 0)
  1790.     TabWidth = 8;
  1791.   
  1792. /* Turned off since /etc/termcap seems to have :ta= for most terminals
  1793.    and newer termcap doc does not seem to say there is a default.
  1794.   if (!Wcm.cm_tab)
  1795.     Wcm.cm_tab = "\t";
  1796. */
  1797.  
  1798.   if (TS_standout_mode == 0)
  1799.     {
  1800.       TN_standout_width = tgetnum ("ug");
  1801.       TS_end_standout_mode = tgetstr ("ue", address);
  1802.       TS_standout_mode = tgetstr ("us", address);
  1803.     }
  1804.  
  1805.   /* If no `se' string, try using a `me' string instead.
  1806.      If that fails, we can't use standout mode at all.  */
  1807.   if (TS_end_standout_mode == 0)
  1808.     {
  1809.       char *s = tgetstr ("me", address);
  1810.       if (s != 0)
  1811.     TS_end_standout_mode = s;
  1812.       else
  1813.     TS_standout_mode = 0;
  1814.     }
  1815.  
  1816.   if (TF_teleray)
  1817.     {
  1818.       Wcm.cm_tab = 0;
  1819.       /* Teleray: most programs want a space in front of TS_standout_mode,
  1820.        but Emacs can do without it (and give one extra column).  */
  1821.       TS_standout_mode = "\033RD";
  1822.       TN_standout_width = 1;
  1823.       /* But that means we cannot rely on ^M to go to column zero! */
  1824.       CR = 0;
  1825.       /* LF can't be trusted either -- can alter hpos */
  1826.       /* if move at column 0 thru a line with TS_standout_mode */
  1827.       Down = 0;
  1828.     }
  1829.  
  1830.   /* Special handling for certain terminal types known to need it */
  1831.  
  1832.   if (!strcmp (terminal_type, "supdup"))
  1833.     {
  1834.       memory_below_frame = 1;
  1835.       Wcm.cm_losewrap = 1;
  1836.     }
  1837.   if (!strncmp (terminal_type, "c10", 3)
  1838.       || !strcmp (terminal_type, "perq"))
  1839.     {
  1840.       /* Supply a makeshift :wi string.
  1841.      This string is not valid in general since it works only
  1842.      for windows starting at the upper left corner;
  1843.      but that is all Emacs uses.
  1844.  
  1845.      This string works only if the frame is using
  1846.      the top of the video memory, because addressing is memory-relative.
  1847.      So first check the :ti string to see if that is true.
  1848.  
  1849.      It would be simpler if the :wi string could go in the termcap
  1850.      entry, but it can't because it is not fully valid.
  1851.      If it were in the termcap entry, it would confuse other programs.  */
  1852.       if (!TS_set_window)
  1853.     {
  1854.       p = TS_termcap_modes;
  1855.       while (*p && strcmp (p, "\033v  "))
  1856.         p++;
  1857.       if (*p)
  1858.         TS_set_window = "\033v%C %C %C %C ";
  1859.     }
  1860.       /* Termcap entry often fails to have :in: flag */
  1861.       must_write_spaces = 1;
  1862.       /* :ti string typically fails to have \E^G! in it */
  1863.       /* This limits scope of insert-char to one line.  */
  1864.       strcpy (area, TS_termcap_modes);
  1865.       strcat (area, "\033\007!");
  1866.       TS_termcap_modes = area;
  1867.       area += strlen (area) + 1;
  1868.       p = AbsPosition;
  1869.       /* Change all %+ parameters to %C, to handle
  1870.      values above 96 correctly for the C100.  */
  1871.       while (*p)
  1872.     {
  1873.       if (p[0] == '%' && p[1] == '+')
  1874.         p[1] = 'C';
  1875.       p++;
  1876.     }
  1877.     }
  1878.  
  1879.   FrameRows = FRAME_HEIGHT (selected_frame);
  1880.   FrameCols = FRAME_WIDTH (selected_frame);
  1881.   specified_window = FRAME_HEIGHT (selected_frame);
  1882.  
  1883.   if (Wcm_init () == -1)    /* can't do cursor motion */
  1884. #ifdef VMS
  1885.     fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
  1886. It lacks the ability to position the cursor.\n\
  1887. If that is not the actual type of terminal you have, use either the\n\
  1888. DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
  1889. or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
  1890.            terminal_type);
  1891. #else /* not VMS */
  1892. # ifdef TERMINFO
  1893.     fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
  1894. It lacks the ability to position the cursor.\n\
  1895. If that is not the actual type of terminal you have,\n\
  1896. use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
  1897. `setenv TERM ...') to specify the correct type.  It may be necessary\n\
  1898. to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
  1899.        terminal_type);
  1900. # else /* TERMCAP */
  1901.     fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
  1902. It lacks the ability to position the cursor.\n\
  1903. If that is not the actual type of terminal you have,\n\
  1904. use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
  1905. `setenv TERM ...') to specify the correct type.  It may be necessary\n\
  1906. to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
  1907.        terminal_type);
  1908. # endif /* TERMINFO */
  1909. #endif /*VMS */
  1910.   if (FRAME_HEIGHT (selected_frame) <= 0
  1911.       || FRAME_WIDTH (selected_frame) <= 0)
  1912.     fatal ("The frame size has not been specified");
  1913.  
  1914.   delete_in_insert_mode
  1915.     = TS_delete_mode && TS_insert_mode
  1916.       && !strcmp (TS_delete_mode, TS_insert_mode);
  1917.  
  1918.   se_is_so = (TS_standout_mode
  1919.           && TS_end_standout_mode
  1920.           && !strcmp (TS_standout_mode, TS_end_standout_mode));
  1921.  
  1922.   /* Remove width of standout marker from usable width of line */
  1923.   if (TN_standout_width > 0)
  1924.     SET_FRAME_WIDTH (selected_frame,
  1925.              FRAME_WIDTH (selected_frame) - TN_standout_width);
  1926.  
  1927.   UseTabs = tabs_safe_p () && TabWidth == 8;
  1928.  
  1929.   scroll_region_ok
  1930.     = (Wcm.cm_abs
  1931.        && (TS_set_window || TS_set_scroll_region || TS_set_scroll_region_1));
  1932.  
  1933.   line_ins_del_ok = (((TS_ins_line || TS_ins_multi_lines)
  1934.               && (TS_del_line || TS_del_multi_lines))
  1935.              || (scroll_region_ok && TS_fwd_scroll && TS_rev_scroll));
  1936.  
  1937.   char_ins_del_ok = ((TS_ins_char || TS_insert_mode
  1938.               || TS_pad_inserted_char || TS_ins_multi_chars)
  1939.              && (TS_del_char || TS_del_multi_chars));
  1940.  
  1941.   fast_clear_end_of_line = TS_clr_line != 0;
  1942.  
  1943.   init_baud_rate ();
  1944.   if (read_socket_hook)        /* Baudrate is somewhat */
  1945.                 /* meaningless in this case */
  1946.     baud_rate = 9600;
  1947.  
  1948.   FRAME_CAN_HAVE_SCROLL_BARS (selected_frame) = 0;
  1949.   FRAME_VERTICAL_SCROLL_BAR_TYPE (selected_frame) = vertical_scroll_bar_none;
  1950. #endif /* WINDOWSNT */
  1951. }
  1952.  
  1953. /* VARARGS 1 */
  1954. void
  1955. fatal (str, arg1, arg2)
  1956.      char *str, *arg1, *arg2;
  1957. {
  1958.   fprintf (stderr, "emacs: ");
  1959.   fprintf (stderr, str, arg1, arg2);
  1960.   fprintf (stderr, "\n");
  1961.   fflush (stderr);
  1962.   exit (1);
  1963. }
  1964.  
  1965. void
  1966. syms_of_term ()
  1967. {
  1968.   DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
  1969.     "Non-nil means the system uses terminfo rather than termcap.\n\
  1970. This variable can be used by terminal emulator packages.");
  1971. #ifdef TERMINFO
  1972.   system_uses_terminfo = 1;
  1973. #else
  1974.   system_uses_terminfo = 0;
  1975. #endif
  1976.  
  1977.   DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
  1978.     "Non-nil means call this function to ring the bell.\n\
  1979. The function should accept no arguments.");
  1980.   Vring_bell_function = Qnil;
  1981. }
  1982.