home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / editors / mntemacs.zoo / src / term.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-30  |  32.8 KB  |  1,258 lines

  1. /* terminal control module for terminals described by TERMCAP
  2.    Copyright (C) 1985, 1986, 1987 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include "config.h"
  24. #include "termhooks.h"
  25. #include "termchar.h"
  26. #include "termopts.h"
  27. #include "cm.h"
  28.  
  29. /**
  30.  **  (sjk)++ stuff for the xconsole i/o routines.
  31.  **/
  32. #if defined(atarist)
  33. #include <keycodes.h>
  34. #ifndef __MINT__
  35. extern void initialize_st_bindings();
  36. #endif
  37. #endif 
  38.  
  39. #define max(a, b) ((a) > (b) ? (a) : (b))
  40. #define min(a, b) ((a) < (b) ? (a) : (b))
  41.  
  42. #define OUTPUT(a) tputs (a, screen_height - curY, cmputc)
  43. #define OUTPUT1(a) tputs (a, 1, cmputc)
  44. #define OUTPUTL(a, lines) tputs (a, lines, cmputc)
  45. #define OUTPUT_IF(a) { if (a) tputs (a, screen_height - curY, cmputc); }
  46. #define OUTPUT1_IF(a) { if (a) tputs (a, 1, cmputc); }
  47.  
  48. /* Terminal charateristics that higher levels want to look at.
  49.    These are all extern'd in termchar.h */
  50.  
  51. int screen_width;        /* Number of usable columns */
  52. int screen_height;        /* Number of lines */
  53. int must_write_spaces;        /* Nonzero means spaces in the text
  54.                    must actually be output; can't just skip
  55.                    over some columns to leave them blank.  */
  56. int min_padding_speed;        /* Speed below which no padding necessary */
  57.  
  58. int line_ins_del_ok;        /* Terminal can insert and delete lines */
  59. int char_ins_del_ok;        /* Terminal can insert and delete chars */
  60. int scroll_region_ok;        /* Terminal supports setting the scroll window */
  61. int memory_below_screen;    /* Terminal remembers lines scrolled off bottom */
  62. int fast_clear_end_of_line;    /* Terminal has a `ce' string */
  63.  
  64. int dont_calculate_costs;    /* Nonzero means don't bother computing */
  65.                 /* various cost tables; we won't use them.  */
  66.  
  67. /* Nonzero means no need to redraw the entire screen on resuming
  68.    a suspended Emacs.  This is useful on terminals with multiple pages,
  69.    where one page is used for Emacs and another for all else. */
  70. int no_redraw_on_reenter;
  71.  
  72. /* DCICcost[n] is cost of inserting N characters.
  73.    DCICcost[-n] is cost of deleting N characters. */
  74.  
  75. #define DCICcost (&DC_ICcost[screen_width])
  76. int *DC_ICcost;
  77.  
  78. /* Hook functions that you can set to snap out the functions in this file.
  79.    These are all extern'd in termhooks.h  */
  80.  
  81. int (*move_cursor_hook) ();
  82. int (*raw_move_cursor_hook) ();
  83.  
  84. int (*clear_to_end_hook) ();
  85. int (*clear_screen_hook) ();
  86. int (*clear_end_of_line_hook) ();
  87.  
  88. int (*ins_del_lines_hook) ();
  89.  
  90. int (*change_line_highlight_hook) ();
  91. int (*reassert_line_highlight_hook) ();
  92.  
  93. int (*insert_chars_hook) ();
  94. int (*output_chars_hook) ();
  95. int (*delete_chars_hook) ();
  96.  
  97. int (*ring_bell_hook) ();
  98.  
  99. int (*reset_terminal_modes_hook) ();
  100. int (*set_terminal_modes_hook) ();
  101. int (*update_begin_hook) ();
  102. int (*update_end_hook) ();
  103. int (*set_terminal_window_hook) ();
  104.  
  105. int (*read_socket_hook) ();
  106. int (*fix_screen_hook) ();
  107. int (*calculate_costs_hook) ();
  108.  
  109. /* Strings, numbers and flags taken from the termcap entry.  */
  110.  
  111. char *TS_ins_line;        /* termcap "al" */
  112. char *TS_ins_multi_lines;    /* "AL" (one parameter, # lines to insert) */
  113. char *TS_bell;            /* "bl" */
  114. char *TS_clr_to_bottom;        /* "cd" */
  115. char *TS_clr_line;        /* "ce", clear to end of line */
  116. char *TS_clr_screen;        /* "cl" */
  117. char *TS_set_scroll_region;    /* "cs" (2 params, first line and last line) */
  118. char *TS_set_scroll_region_1;   /* "cS" (4 params: total lines,
  119.                    lines above scroll region, lines below it,
  120.                    total lines again) */
  121. char *TS_del_char;        /* "dc" */
  122. char *TS_del_multi_chars;    /* "DC" (one parameter, # chars to delete) */
  123. char *TS_del_line;        /* "dl" */
  124. char *TS_del_multi_lines;    /* "DL" (one parameter, # lines to delete) */
  125. char *TS_delete_mode;        /* "dm", enter character-delete mode */
  126. char *TS_end_delete_mode;    /* "ed", leave character-delete mode */
  127. char *TS_end_insert_mode;    /* "ei", leave character-insert mode */
  128. char *TS_ins_char;        /* "ic" */
  129. char *TS_ins_multi_chars;    /* "IC" (one parameter, # chars to insert) */
  130. char *TS_insert_mode;        /* "im", enter character-insert mode */
  131. char *TS_pad_inserted_char;    /* "ip".  Just padding, no commands.  */
  132. char *TS_end_keypad_mode;    /* "ke" */
  133. char *TS_keypad_mode;        /* "ks" */
  134. char *TS_pad_char;        /* "pc", char to use as padding */
  135. char *TS_repeat;        /* "rp" (2 params, # times to repeat
  136.                    and character to be repeated) */
  137. char *TS_end_standout_mode;    /* "se" */
  138. char *TS_fwd_scroll;        /* "sf" */
  139. char *TS_standout_mode;        /* "so" */
  140. char *TS_rev_scroll;        /* "sr" */
  141. char *TS_end_termcap_modes;    /* "te" */
  142. char *TS_termcap_modes;        /* "ti" */
  143. char *TS_visible_bell;        /* "vb" */
  144. char *TS_end_visual_mode;    /* "ve" */
  145. char *TS_visual_mode;        /* "vi" */
  146. char *TS_set_window;        /* "wi" (4 params, start and end of window,
  147.                    each as vpos and hpos) */
  148.  
  149. int TF_hazeltine;    /* termcap hz flag. */
  150. int TF_insmode_motion;    /* termcap mi flag: can move while in insert mode. */
  151. int TF_standout_motion;    /* termcap mi flag: can move while in standout mode. */
  152. int TF_underscore;    /* termcap ul flag: _ underlines if overstruck on
  153.                nonblank position.  Must clear before writing _.  */
  154. int TF_teleray;        /* termcap xt flag: many weird consequences.  For t1061. */
  155.  
  156. int TF_xs;        /* Nonzero for "xs".  If set together with
  157.                TN_standout_width == 0, it means don't bother
  158.                to write any end-standout cookies.  */
  159.  
  160. int TN_standout_width;    /* termcap sg number: width occupied by standout markers */
  161.  
  162. static int RPov;    /* # chars to start a TS_repeat */
  163.  
  164. static int delete_in_insert_mode;    /* delete mode == insert mode */
  165.  
  166. static int se_is_so;    /* 1 if same string both enters and leaves standout mode */
  167.  
  168. /* internal state */
  169.  
  170. /* Number of chars of space used for standout marker at beginning of line,
  171.    or'd with 0100.  Zero if no standout marker at all.  */
  172. /* used iff TN_standout_width >= 0. */
  173. char *chars_wasted;
  174. static char *copybuf;
  175.  
  176. /* nonzero means supposed to write text in standout mode.  */
  177. int standout_requested;
  178.  
  179. int insert_mode;            /* Nonzero when in insert mode.  */
  180. int standout_mode;            /* Nonzero when in standout mode.  */
  181.  
  182. /* Size of window specified by higher levels.
  183.    This is the number of lines, starting from top of screen,
  184.    to participate in ins/del line operations.
  185.    Effectively it excludes the bottom
  186.       screen_height - specified_window_size
  187.    lines from those operations.  */
  188.  
  189. int specified_window;
  190.  
  191. char *tparam ();
  192.  
  193. ring_bell ()
  194. {
  195.   if (ring_bell_hook)
  196.     {
  197.       (*ring_bell_hook) ();
  198.       return;
  199.     }
  200.   OUTPUT (TS_visible_bell && visible_bell ? TS_visible_bell : TS_bell);
  201. }
  202.  
  203. set_terminal_modes ()
  204. {
  205.   if (set_terminal_modes_hook)
  206.     {
  207.       (*set_terminal_modes_hook) ();
  208.       return;
  209.     }
  210.   OUTPUT_IF (TS_termcap_modes);
  211.   OUTPUT_IF (TS_visual_mode);
  212.   OUTPUT_IF (TS_keypad_mode);
  213.   losecursor ();
  214. }
  215.  
  216. reset_terminal_modes ()
  217. {
  218.   if (reset_terminal_modes_hook)
  219.     {
  220.       (*reset_terminal_modes_hook) ();
  221.       return;
  222.     }
  223.   if (TN_standout_width < 0)
  224.     turn_off_highlight ();
  225.   turn_off_insert ();
  226.   OUTPUT_IF (TS_end_keypad_mode);
  227.   OUTPUT_IF (TS_end_visual_mode);
  228.   OUTPUT_IF (TS_end_termcap_modes);
  229. }
  230.  
  231. update_begin ()
  232. {
  233.   if (update_begin_hook)
  234.     (*update_begin_hook) ();
  235. }
  236.  
  237. update_end ()
  238. {
  239.   if (update_end_hook)
  240.     {
  241.       (*update_end_hook) ();
  242.       return;
  243.     }
  244.   turn_off_insert ();
  245.   background_highlight ();
  246.   standout_requested = 0;
  247. }
  248.  
  249. set_terminal_window (size)
  250.      int size;
  251. {
  252.   if (set_terminal_window_hook)
  253.     {
  254.       (*set_terminal_window_hook) (size);
  255.       return;
  256.     }
  257.   specified_window = size ? size : screen_height;
  258.   if (!scroll_region_ok)
  259.     return;
  260.   set_scroll_region (0, specified_window);
  261. }
  262.  
  263. set_scroll_region (start, stop)
  264.      int start, stop;
  265. {
  266.   char *buf;
  267.   if (TS_set_scroll_region)
  268.     {
  269.       buf = tparam (TS_set_scroll_region, 0, 0, start, stop - 1);
  270.     }
  271.   else if (TS_set_scroll_region_1)
  272.     {
  273.       buf = tparam (TS_set_scroll_region_1, 0, 0,
  274.             screen_height, start, screen_height - stop, screen_height);
  275.     }
  276.   else
  277.     {
  278.       buf = tparam (TS_set_window, 0, 0, start, 0, stop, screen_width);
  279.     }
  280.   OUTPUT (buf);
  281.   free (buf);
  282.   losecursor ();
  283. }
  284.  
  285. turn_on_insert ()
  286. {
  287.   if (!insert_mode)
  288.     OUTPUT (TS_insert_mode);
  289.   insert_mode = 1;
  290. }
  291.  
  292. turn_off_insert ()
  293. {
  294.   if (insert_mode)
  295.     OUTPUT (TS_end_insert_mode);
  296.   insert_mode = 0;
  297. }
  298.  
  299. /* Handle highlighting when TN_standout_width (termcap sg) is not specified.
  300.    In these terminals, output is affected by the value of standout
  301.    mode when the output is written.
  302.  
  303.    These functions are called on all terminals, but do nothing
  304.    on terminals whose standout mode does not work that way.  */
  305.  
  306. turn_off_highlight ()
  307. {
  308.   if (TN_standout_width < 0)
  309.     {
  310.       if (standout_mode)
  311.     OUTPUT_IF (TS_end_standout_mode);
  312.       standout_mode = 0;
  313.     }
  314. }
  315.  
  316. turn_on_highlight ()
  317. {
  318.   if (TN_standout_width < 0)
  319.     {
  320.       if (!standout_mode)
  321.     OUTPUT_IF (TS_standout_mode);
  322.       standout_mode = 1;
  323.     }
  324. }
  325.  
  326. /* Set standout mode to the state it should be in for
  327.    empty space inside windows.  What this is,
  328.    depends on the user option inverse-video.  */
  329.  
  330. background_highlight ()
  331. {
  332.   if (TN_standout_width >= 0)
  333.     return;
  334.   if (inverse_video)
  335.     turn_on_highlight ();
  336.   else
  337.     turn_off_highlight ();
  338. }
  339.  
  340. /* Set standout mode to the mode specified for the text to be output.  */
  341.  
  342. static
  343. highlight_if_desired ()
  344. {
  345.   if (TN_standout_width >= 0)
  346.     return;
  347.   if (!inverse_video == !standout_requested)
  348.     turn_off_highlight ();
  349.   else
  350.     turn_on_highlight ();
  351. }
  352.  
  353. /* Handle standout mode for terminals in which TN_standout_width >= 0.
  354.    On these terminals, standout is controlled by markers that
  355.    live inside the screen memory.  TN_standout_width is the width
  356.    that the marker occupies in memory.  Standout runs from the marker
  357.    to the end of the line on some terminals, or to the next
  358.    turn-off-standout marker (TS_end_standout_mode) string
  359.    on other terminals.  */
  360.  
  361. /* Write a standout marker or end-standout marker at the front of the line
  362.    at vertical position vpos.  */
  363.  
  364. write_standout_marker (flag, vpos)
  365.      int flag, vpos;
  366. {
  367.   if (flag || (TS_end_standout_mode && !TF_teleray && !se_is_so
  368.            && !(TF_xs && TN_standout_width == 0)))
  369.     {
  370.       cmgoto (vpos, 0);
  371.       cmplus (TN_standout_width);
  372.       OUTPUT (flag ? TS_standout_mode : TS_end_standout_mode);
  373.       chars_wasted[curY] = TN_standout_width | 0100;
  374.     }
  375. }
  376.  
  377. /* External interface to control of standout mode.
  378.    Call this when about to modify line at position VPOS
  379.    and not change whether it is highlighted.  */
  380.  
  381. reassert_line_highlight (highlight, vpos)
  382.      int highlight;
  383.      int vpos;
  384. {
  385.   if (reassert_line_highlight_hook)
  386.     {
  387.       (*reassert_line_highlight_hook) (highlight, vpos);
  388.       return;
  389.     }
  390.   if (TN_standout_width < 0)
  391.     /* Handle terminals where standout takes affect at output time */
  392.     standout_requested = highlight;
  393.   else if (chars_wasted[vpos] == 0)
  394.     /* For terminals with standout markers, write one on this line
  395.        if there isn't one already.  */
  396.     write_standout_marker (highlight, vpos);
  397. }
  398.  
  399. /* Call this when about to modify line at position VPOS
  400.    and change whether it is highlighted.  */
  401.  
  402. change_line_highlight (new_highlight, vpos, first_unused_hpos)
  403.      int new_highlight, vpos, first_unused_hpos;
  404. {
  405.   standout_requested = new_highlight;
  406.   if (change_line_highlight_hook)
  407.     {
  408.       (*change_line_highlight_hook) (new_highlight, vpos, first_unused_hpos);
  409.       return;
  410.     }
  411.  
  412.   move_cursor (vpos, 0);
  413.  
  414.   if (TN_standout_width < 0)
  415.     background_highlight ();
  416.   /* If line starts with a marker, delete the marker */
  417.   else if (TS_clr_line && chars_wasted[curY])
  418.     {
  419.       turn_off_insert ();
  420.       /* On Teleray, make sure to erase the SO marker.  */
  421.       if (TF_teleray)
  422.     {
  423.       cmgoto (curY - 1, screen_width - 4);
  424.       OUTPUT ("\033S");
  425.       curY++;        /* ESC S moves to next line where the TS_standout_mode was */
  426.       curX = 0;
  427.     }
  428.       else
  429.     cmgoto (curY, 0);    /* reposition to kill standout marker */
  430.     }
  431.   clear_end_of_line_raw (first_unused_hpos);
  432.   reassert_line_highlight (new_highlight, curY);
  433. }
  434.  
  435. /* Move to absolute position, specified origin 0 */
  436.  
  437. move_cursor (row, col)
  438. {
  439.   col += chars_wasted[row] & 077;
  440.   if (move_cursor_hook)
  441.     {
  442.       (*move_cursor_hook) (row, col);
  443.       return;
  444.     }
  445.   if (curY == row && curX == col)
  446.     return;
  447.   if (!TF_standout_motion)
  448.     background_highlight ();
  449.   if (!TF_insmode_motion)
  450.     turn_off_insert ();
  451.   cmgoto (row, col);
  452. }
  453.  
  454. /* Similar but don't take any account of the wasted characters.  */
  455.  
  456. raw_move_cursor (row, col)
  457. {
  458.   if (raw_move_cursor_hook)
  459.     {
  460.       (*raw_move_cursor_hook) (row, col);
  461.       return;
  462.     }
  463.   if (curY == row && curX == col)
  464.     return;
  465.   if (!TF_standout_motion)
  466.     background_highlight ();
  467.   if (!TF_insmode_motion)
  468.     turn_off_insert ();
  469.   cmgoto (row, col);
  470. }
  471.  
  472. /* Erase operations */
  473.  
  474. /* clear from cursor to end of screen */
  475. clear_to_end ()
  476. {
  477.   register int i;
  478.  
  479.   if (clear_to_end_hook)
  480.     {
  481.       (*clear_to_end_hook) ();
  482.       return;
  483.     }
  484.   if (TS_clr_to_bottom)
  485.     {
  486.       background_highlight ();
  487.       OUTPUT (TS_clr_to_bottom);
  488.       bzero (chars_wasted + curY, screen_height - curY);
  489.     }
  490.   else
  491.     {
  492.       for (i = curY; i < screen_height; i++)
  493.     {
  494.       move_cursor (i, 0);
  495.       clear_end_of_line_raw (screen_width);
  496.     }
  497.     }
  498. }
  499.  
  500. /* Clear entire screen */
  501.  
  502. clear_screen ()
  503. {
  504.   if (clear_screen_hook)
  505.     {
  506.       (*clear_screen_hook) ();
  507.       return;
  508.     }
  509.   if (TS_clr_screen)
  510.     {
  511.       background_highlight ();
  512.       OUTPUT (TS_clr_screen);
  513.       bzero (chars_wasted, screen_height);
  514.       cmat (0, 0);
  515.     }
  516.   else
  517.     {
  518.       move_cursor (0, 0);
  519.       clear_to_end ();
  520.     }
  521. }
  522.  
  523. /* Clear to end of line, but do not clear any standout marker.
  524.    Assumes that the cursor is positioned at a character of real text,
  525.    which implies it cannot be before a standout marker
  526.    unless the marker has zero width.
  527.  
  528.    Note that the cursor may be moved.  */
  529.  
  530. clear_end_of_line (first_unused_hpos)
  531.      int first_unused_hpos;
  532. {
  533.   if (TN_standout_width == 0 && curX == 0 && chars_wasted[curY] != 0)
  534.     output_chars (" ", 1);
  535.   clear_end_of_line_raw (first_unused_hpos);
  536. }
  537.  
  538. /* Clear from cursor to end of line.
  539.    Assume that the line is already clear starting at column first_unused_hpos.
  540.    If the cursor is at a standout marker, erase the marker.
  541.  
  542.    Note that the cursor may be moved, on terminals lacking a `ce' string.  */
  543.  
  544. clear_end_of_line_raw (first_unused_hpos)
  545.      int first_unused_hpos;
  546. {
  547.   register int i;
  548.   first_unused_hpos += chars_wasted[curY] & 077;
  549.   if (clear_end_of_line_hook)
  550.     {
  551.       (*clear_end_of_line_hook) (first_unused_hpos);
  552.       return;
  553.     }
  554.   if (curX >= first_unused_hpos)
  555.     return;
  556.   /* Notice if we are erasing a magic cookie */
  557.   if (curX == 0)
  558.     chars_wasted[curY] = 0;
  559.   background_highlight ();
  560.   if (TS_clr_line)
  561.     {
  562.       OUTPUT1 (TS_clr_line);
  563.     }
  564.   else
  565.     {            /* have to do it the hard way */
  566.       turn_off_insert ();
  567.       for (i = curX; i < first_unused_hpos; i++)
  568.     {
  569.       if (termscript)
  570.         fputc (' ', termscript);
  571.       putchar (' ');
  572.     }
  573.       cmplus (first_unused_hpos - curX);
  574.     }
  575. }
  576.  
  577. output_chars (string, len)
  578.      register char *string;
  579.      int len;
  580. {
  581.   register char *p;
  582.   register int n;
  583.   register char *buf;
  584.   register int c;
  585.   char *first_check;
  586.  
  587.   if (output_chars_hook)
  588.     {
  589.       (*output_chars_hook) (string, len);
  590.       return;
  591.     }
  592.   highlight_if_desired ();
  593.   turn_off_insert ();
  594.  
  595.   /* Don't dare write in last column of bottom line, if AutoWrap,
  596.      since that would scroll the whole screen on some terminals.  */
  597.  
  598.   if (AutoWrap && curY + 1 == screen_height
  599.       && curX + len - (chars_wasted[curY] & 077) == screen_width)
  600.     len --;
  601.  
  602.   cmplus (len);
  603.  
  604.   first_check = string;
  605.  
  606.   if (RPov > len && !TF_underscore && !TF_hazeltine)
  607.     {
  608.       fwrite (string, 1, len, stdout);
  609.       if (ferror (stdout))
  610.     clearerr (stdout);
  611.       if (termscript)
  612.     fwrite (string, 1, len, termscript);
  613.     }
  614.   else
  615.     while (--len >= 0)
  616.       {
  617.     c = *string;
  618.     if (RPov + 1 < len && string >= first_check)
  619.       {
  620.         int repeat_count;
  621.  
  622.         p = string + 1;
  623.  
  624.         /* Now, len is number of chars left starting at p */
  625.         while (*p++ == c);
  626.         p--;
  627.  
  628.         repeat_count = p - string;
  629.         if (repeat_count > RPov)
  630.           {
  631.         buf = tparam (TS_repeat, 0, 0, *string, repeat_count);
  632.         tputs (buf, repeat_count, cmputc);
  633.         free (buf);
  634.         string = p;
  635.         len -= repeat_count - 1;
  636.         continue;
  637.           }
  638.         else
  639.           /* If all N identical chars are too few,
  640.          don't even consider the last N-1, the last N-2,...  */
  641.           first_check = p;
  642.       }
  643.     if (c == '_' && TF_underscore)
  644.       {
  645.         if (termscript)
  646.           fputc (' ', termscript);
  647.         putchar (' ');
  648.         OUTPUT (Left);
  649.       }
  650.     if (TF_hazeltine && c == '~')
  651.       c = '`';
  652.     if (termscript)
  653.       fputc (c, termscript);
  654.     putchar (c);
  655.     string++;
  656.       }
  657. }
  658.  
  659. /* If start is zero, insert blanks instead of a string at start */
  660.  
  661. insert_chars (start, len)
  662.      register char *start;
  663.      int len;
  664. {
  665.   register char *buf;
  666.   register int c;
  667.  
  668.   if (insert_chars_hook)
  669.     {
  670.       (*insert_chars_hook) (start, len);
  671.       return;
  672.     }
  673.   highlight_if_desired ();
  674.  
  675.   if (TS_ins_multi_chars)
  676.     {
  677.       buf = tparam (TS_ins_multi_chars, 0, 0, len);
  678.       OUTPUT1 (buf);
  679.       free (buf);
  680.       if (start)
  681.     output_chars (start, len);
  682.       return;
  683.     }
  684.  
  685.   turn_on_insert ();
  686.   cmplus (len);
  687.  
  688.   if (!TF_underscore && !TF_hazeltine && start
  689.       && TS_pad_inserted_char == 0 && TS_ins_char == 0)
  690.     {
  691.       fwrite (start, 1, len, stdout);
  692.       if (termscript)
  693.     fwrite (start, 1, len, termscript);
  694.     }
  695.   else
  696.     while (--len >= 0)
  697.       {
  698.     OUTPUT1_IF (TS_ins_char);
  699.     if (!start)
  700.       c = ' ';
  701.     else
  702.       {
  703.         c = *start++;
  704.         if (TF_hazeltine && c == '~')
  705.           c = '`';
  706.       }
  707.     if (termscript)
  708.       fputc (c, termscript);
  709.     putchar (c);
  710.     OUTPUT1_IF (TS_pad_inserted_char);
  711.     }
  712. }
  713.  
  714. delete_chars (n)
  715.      register int n;
  716. {
  717.   char *buf;
  718.   register int i;
  719.  
  720.   if (delete_chars_hook)
  721.     {
  722.       (*delete_chars_hook) (n);
  723.       return;
  724.     }
  725.  
  726.   if (delete_in_insert_mode)
  727.     {
  728.       turn_on_insert ();
  729.     }
  730.   else
  731.     {
  732.       turn_off_insert ();
  733.       OUTPUT_IF (TS_delete_mode);
  734.     }
  735.  
  736.   if (TS_del_multi_chars)
  737.     {
  738.       buf = tparam (TS_del_multi_chars, 0, 0, n);
  739.       OUTPUT1 (buf);
  740.       free (buf);
  741.     }
  742.   else
  743.     for (i = 0; i < n; i++)
  744.       OUTPUT1 (TS_del_char);
  745.   if (!delete_in_insert_mode)
  746.     OUTPUT_IF (TS_end_delete_mode);
  747. }
  748.  
  749. /* Insert N lines at vpos VPOS.  If N is negative, delete -N lines.  */
  750.  
  751. ins_del_lines (vpos, n)
  752.      int vpos, n;
  753. {
  754.   char *multi = n > 0 ? TS_ins_multi_lines : TS_del_multi_lines;
  755.   char *single = n > 0 ? TS_ins_line : TS_del_line;
  756.   char *scroll = n > 0 ? TS_rev_scroll : TS_fwd_scroll;
  757.  
  758.   register int i = n > 0 ? n : -n;
  759.   register char *buf;
  760.  
  761.   if (ins_del_lines_hook)
  762.     {
  763.       (*ins_del_lines_hook) (vpos, n);
  764.       return;
  765.     }
  766.  
  767.   /* If the lines below the insertion are being pushed
  768.      into the end of the window, this is the same as clearing;
  769.      and we know the lines are already clear, since the matching
  770.      deletion has already been done.  So can ignore this.  */
  771.   /* If the lines below the deletion are blank lines coming
  772.      out of the end of the window, don't bother,
  773.      as there will be a matching inslines later that will flush them. */
  774.   if (scroll_region_ok && vpos + i >= specified_window)
  775.     return;
  776.   if (!memory_below_screen && vpos + i >= screen_height)
  777.     return;
  778.  
  779.   if (multi)
  780.     {
  781.       raw_move_cursor (vpos, 0);
  782.       background_highlight ();
  783.       buf = tparam (multi, 0, 0, i);
  784.       OUTPUT (buf);
  785.       free (buf);
  786.     }
  787.   else if (single)
  788.     {
  789.       raw_move_cursor (vpos, 0);
  790.       background_highlight ();
  791.       while (--i >= 0)
  792.     OUTPUT (single);
  793.       if (TF_teleray)
  794.     curX = 0;
  795.     }
  796.   else
  797.     {
  798.       set_scroll_region (vpos, specified_window);
  799.       if (n < 0)
  800.     raw_move_cursor (specified_window - 1, 0);
  801.       else
  802.     raw_move_cursor (vpos, 0);
  803.       background_highlight ();
  804.       while (--i >= 0)
  805.     OUTPUTL (scroll, specified_window - vpos);
  806.       set_scroll_region (0, specified_window);
  807.     }
  808.  
  809.   if (TN_standout_width >= 0)
  810.     {
  811.       register lower_limit
  812.     = scroll_region_ok ? specified_window : screen_height;
  813.       if (n < 0)
  814.     {
  815.       bcopy (&chars_wasted[vpos - n], &chars_wasted[vpos],
  816.          lower_limit - vpos + n);
  817.       bzero (&chars_wasted[lower_limit + n], - n);
  818.     }
  819.       else
  820.     {
  821.       bcopy (&chars_wasted[vpos], ©buf[vpos], lower_limit - vpos - n);
  822.       bcopy (©buf[vpos], &chars_wasted[vpos + n],
  823.          lower_limit - vpos - n);
  824.       bzero (&chars_wasted[vpos], n);
  825.     }
  826.     }
  827.   if (!scroll_region_ok && memory_below_screen && n < 0)
  828.     {
  829.       move_cursor (screen_height + n, 0);
  830.       clear_to_end ();
  831.     }
  832. }
  833.  
  834. extern int cost;        /* In cm.c */
  835. extern evalcost ();
  836.  
  837. /* Compute cost of sending "str", in characters,
  838.    not counting any line-dependent padding.  */
  839. string_cost (str)
  840.      char *str;
  841. {
  842.   cost = 0;
  843.   if (str)
  844.     tputs (str, 0, evalcost);
  845.   return cost;
  846. }
  847.  
  848. /* Compute cost of sending "str", in characters,
  849.    counting any line-dependent padding at one line.  */
  850. string_cost_one_line (str)
  851.      char *str;
  852. {
  853.   cost = 0;
  854.   if (str)
  855.     tputs (str, 1, evalcost);
  856.   return cost;
  857. }
  858.  
  859. /* Compute per line amount of line-dependent padding,
  860.    in tenths of characters.  */
  861. per_line_cost (str)
  862.      register char *str;
  863. {
  864.   cost = 0;
  865.   if (str)
  866.     tputs (str, 0, evalcost);
  867.   cost = - cost;
  868.   if (str)
  869.     tputs (str, 10, evalcost);
  870.   return cost;
  871. }
  872.  
  873. /* ARGSUSED */
  874. calculate_ins_del_char_costs ()
  875. {
  876.   int ins_startup_cost, del_startup_cost;
  877.   int ins_cost_per_char, del_cost_per_char;
  878.   register int i;
  879.   register int *p;
  880.  
  881.   if (TS_ins_multi_chars)
  882.     {
  883.       ins_cost_per_char = 0;
  884.       ins_startup_cost = string_cost_one_line (TS_ins_multi_chars);
  885.     }
  886.   else if (TS_ins_char || TS_pad_inserted_char
  887.        || (TS_insert_mode && TS_end_insert_mode))
  888.     {
  889.       ins_startup_cost = (30 * (string_cost (TS_insert_mode) + string_cost (TS_end_insert_mode))) / 100;
  890.       ins_cost_per_char = (string_cost_one_line (TS_ins_char)
  891.                + string_cost_one_line (TS_pad_inserted_char));
  892.     }
  893.   else
  894.     {
  895.       ins_startup_cost = 9999;
  896.       ins_cost_per_char = 0;
  897.     }
  898.  
  899.   if (TS_del_multi_chars)
  900.     {
  901.       del_cost_per_char = 0;
  902.       del_startup_cost = string_cost_one_line (TS_del_multi_chars);
  903.     }
  904.   else if (TS_del_char)
  905.     {
  906.       del_startup_cost = (string_cost (TS_delete_mode)
  907.               + string_cost (TS_end_delete_mode));
  908.       if (delete_in_insert_mode)
  909.     del_startup_cost /= 2;
  910.       del_cost_per_char = string_cost_one_line (TS_del_char);
  911.     }
  912.   else
  913.     {
  914.       del_startup_cost = 9999;
  915.       del_cost_per_char = 0;
  916.     }
  917.  
  918.   /* Delete costs are at negative offsets */
  919.   p = &DCICcost[0];
  920.   for (i = screen_width; --i >= 0;)
  921.     *--p = (del_startup_cost += del_cost_per_char);
  922.  
  923.   /* Doing nothing is free */
  924.   p = &DCICcost[0];
  925.   *p++ = 0;
  926.  
  927.   /* Insert costs are at positive offsets */
  928.   for (i = screen_width; --i >= 0;)
  929.     *p++ = (ins_startup_cost += ins_cost_per_char);
  930. }
  931.  
  932. calculate_costs ()
  933. {
  934.   register char *s
  935.     = TS_set_scroll_region ? TS_set_scroll_region : TS_set_scroll_region_1;
  936.  
  937.   if (chars_wasted != 0)
  938.     chars_wasted = (char *) xrealloc (chars_wasted, screen_height);
  939.   else
  940.     chars_wasted = (char *) xmalloc (screen_height);
  941.   bzero (chars_wasted, screen_height);
  942.  
  943.   if (copybuf != 0)
  944.     copybuf = (char *) xrealloc (copybuf, screen_height);
  945.   else
  946.     copybuf = (char *) xmalloc (screen_height);
  947.  
  948.   if (DC_ICcost != 0)
  949.     DC_ICcost = (int *) xrealloc (DC_ICcost,
  950.                   (2 * screen_width + 1) * sizeof (int));
  951.   else
  952.     DC_ICcost = (int *) xmalloc ((2 * screen_width + 1) * sizeof (int));
  953.  
  954.   /* Always call CalcIDCosts because it allocates some vectors.
  955.      That function handles dont_calculate_costs.  */
  956.   if (s && (!TS_ins_line && !TS_del_line))
  957.     CalcIDCosts (TS_rev_scroll, TS_ins_multi_lines,
  958.          TS_fwd_scroll, TS_del_multi_lines,
  959.          s, s);
  960.   else
  961.     CalcIDCosts (TS_ins_line, TS_ins_multi_lines,
  962.          TS_del_line, TS_del_multi_lines,
  963.          0, 0);
  964.  
  965.   if (dont_calculate_costs)
  966.     {
  967.       bzero (DC_ICcost, 2 * screen_width * sizeof (int));
  968.       return;
  969.     }
  970.  
  971.   calculate_ins_del_char_costs ();
  972.  
  973.   /* Don't use TS_repeat if its padding is worse than sending the chars */
  974.   if (TS_repeat && per_line_cost (TS_repeat) * baud_rate < 9000)
  975.     RPov = string_cost (TS_repeat);
  976.   else
  977.     RPov = screen_width * 2;
  978.  
  979.   cmcostinit ();        /* set up cursor motion costs */
  980. }
  981.  
  982. term_init (terminal_type)
  983.      char *terminal_type;
  984. {
  985.   char *area;
  986.   char **address = &area;
  987.   char buffer[2044];
  988.   register char *p;
  989.   int status;
  990.  
  991.   extern char *tgetstr ();
  992.  
  993. /**
  994.  **  (sjk)++ Setup the default key bindings (Thanx to frank ridderbusch)
  995.  **/
  996. #if defined(atarist) && !defined(__MINT__)
  997.   initialize_st_bindings();
  998. #endif 
  999.  
  1000.   Wcm_clear ();
  1001.   dont_calculate_costs = 0;
  1002.  
  1003.   status = tgetent (buffer, terminal_type);
  1004.   if (status < 0)
  1005.     fatal ("Cannot open termcap database file.\n");
  1006.   if (status == 0)
  1007.     fatal ("Terminal type %s is not defined.\n", terminal_type);
  1008.  
  1009. #ifdef TERMINFO
  1010.   area = (char *) malloc (2044);
  1011. #else
  1012.   area = (char *) malloc (strlen (buffer));
  1013. #endif /* not TERMINFO */
  1014.   if (area == 0)
  1015.     abort ();
  1016.  
  1017.   TS_ins_line = tgetstr ("al", address);
  1018.   TS_ins_multi_lines = tgetstr ("AL", address);
  1019.   TS_bell = tgetstr ("bl", address);
  1020.   TS_clr_to_bottom = tgetstr ("cd", address);
  1021.   TS_clr_line = tgetstr ("ce", address);
  1022.   TS_clr_screen = tgetstr ("cl", address);
  1023.   ColPosition = tgetstr ("ch", address);
  1024.   AbsPosition = tgetstr ("cm", address);
  1025.   CR = tgetstr ("cr", address);
  1026.   TS_set_scroll_region = tgetstr ("cs", address);
  1027.   TS_set_scroll_region_1 = tgetstr ("cS", address);
  1028.   RowPosition = tgetstr ("cv", address);
  1029.   TS_del_char = tgetstr ("dc", address);
  1030.   TS_del_multi_chars = tgetstr ("DC", address);
  1031.   TS_del_line = tgetstr ("dl", address);
  1032.   TS_del_multi_lines = tgetstr ("DL", address);
  1033.   TS_delete_mode = tgetstr ("dm", address);
  1034.   TS_end_delete_mode = tgetstr ("ed", address);
  1035.   TS_end_insert_mode = tgetstr ("ei", address);
  1036.   Home = tgetstr ("ho", address);
  1037.   TS_ins_char = tgetstr ("ic", address);
  1038.   TS_ins_multi_chars = tgetstr ("IC", address);
  1039.   TS_insert_mode = tgetstr ("im", address);
  1040.   TS_pad_inserted_char = tgetstr ("ip", address);
  1041.   TS_end_keypad_mode = tgetstr ("ke", address);
  1042.   TS_keypad_mode = tgetstr ("ks", address);
  1043.   LastLine = tgetstr ("ll", address);
  1044.   Right = tgetstr ("nd", address);
  1045.   Down = tgetstr ("do", address);
  1046.   if (!Down)
  1047.     Down = tgetstr ("nl", address); /* Obsolete name for "do" */
  1048. #ifdef VMS
  1049.   /* VMS puts a carriage return before each linefeed,
  1050.      so it is not safe to use linefeeds.  */
  1051.   if (Down && Down[0] == '\n' && Down[1] == '\0')
  1052.     Down = 0;
  1053. #endif /* VMS */
  1054.   if (tgetflag ("bs"))
  1055.     Left = "\b";          /* can't possibly be longer! */
  1056.   else                  /* (Actually, "bs" is obsolete...) */
  1057.     Left = tgetstr ("le", address);
  1058.   if (!Left)
  1059.     Left = tgetstr ("bc", address); /* Obsolete name for "le" */
  1060.   TS_pad_char = tgetstr ("pc", address);
  1061.   TS_repeat = tgetstr ("rp", address);
  1062.   TS_end_standout_mode = tgetstr ("se", address);
  1063.   TS_fwd_scroll = tgetstr ("sf", address);
  1064.   TS_standout_mode = tgetstr ("so", address);
  1065.   TS_rev_scroll = tgetstr ("sr", address);
  1066.   Wcm.cm_tab = tgetstr ("ta", address);
  1067.   TS_end_termcap_modes = tgetstr ("te", address);
  1068.   TS_termcap_modes = tgetstr ("ti", address);
  1069.   Up = tgetstr ("up", address);
  1070.   TS_visible_bell = tgetstr ("vb", address);
  1071.   TS_end_visual_mode = tgetstr ("ve", address);
  1072.   TS_visual_mode = tgetstr ("vs", address);
  1073.   TS_set_window = tgetstr ("wi", address);
  1074.  
  1075.   AutoWrap = tgetflag ("am");
  1076.   memory_below_screen = tgetflag ("db");
  1077.   TF_hazeltine = tgetflag ("hz");
  1078.   must_write_spaces = tgetflag ("in");
  1079.   meta_key = tgetflag ("km") || tgetflag ("MT");
  1080.   TF_insmode_motion = tgetflag ("mi");
  1081.   TF_standout_motion = tgetflag ("ms");
  1082.   TF_underscore = tgetflag ("ul");
  1083.   MagicWrap = tgetflag ("xn");
  1084.   TF_xs = tgetflag ("xs");
  1085.   TF_teleray = tgetflag ("xt");
  1086.  
  1087.   /* Get screen size from system, or else from termcap.  */
  1088.   get_screen_size (&screen_width, &screen_height);
  1089.   if (screen_width <= 0)
  1090.     screen_width = tgetnum ("co");
  1091.   if (screen_height <= 0)
  1092.     screen_height = tgetnum ("li");
  1093.  
  1094.   min_padding_speed = tgetnum ("pb");
  1095.   TN_standout_width = tgetnum ("sg");
  1096.   TabWidth = tgetnum ("tw");
  1097.  
  1098. #ifdef VMS
  1099.   /* These capabilities commonly use ^J.
  1100.      I don't know why, but sending them on VMS does not work;
  1101.      it causes following spaces to be lost, sometimes.
  1102.      For now, the simplest fix is to avoid using these capabilities ever.  */
  1103.   if (Down && Down[0] == '\n')
  1104.     Down = 0;
  1105. #endif /* VMS */
  1106.  
  1107.   if (!TS_bell)
  1108.     TS_bell = "\07";
  1109.  
  1110.   if (!TS_fwd_scroll)
  1111.     TS_fwd_scroll = Down;
  1112.  
  1113.   PC = TS_pad_char ? *TS_pad_char : 0;
  1114.  
  1115.   if (TabWidth < 0)
  1116.     TabWidth = 8;
  1117.   
  1118. /* Turned off since /etc/termcap seems to have :ta= for most terminals
  1119.    and newer termcap doc does not seem to say there is a default.
  1120.   if (!Wcm.cm_tab)
  1121.     Wcm.cm_tab = "\t";
  1122. */
  1123.  
  1124.   if (TS_standout_mode == 0)
  1125.     {
  1126.       TN_standout_width = tgetnum ("ug");
  1127.       TS_end_standout_mode = tgetstr ("ue", address);
  1128.       TS_standout_mode = tgetstr ("us", address);
  1129.     }
  1130.  
  1131.   if (TF_teleray)
  1132.     {
  1133.       Wcm.cm_tab = 0;
  1134.       /* Teleray: most programs want a space in front of TS_standout_mode,
  1135.        but Emacs can do without it (and give one extra column).  */
  1136.       TS_standout_mode = "\033RD";
  1137.       TN_standout_width = 1;
  1138.       /* But that means we cannot rely on ^M to go to column zero! */
  1139.       CR = 0;
  1140.       /* LF can't be trusted either -- can alter hpos */
  1141.       /* if move at column 0 thru a line with TS_standout_mode */
  1142.       Down = 0;
  1143.     }
  1144.  
  1145.   /* Special handling for certain terminal types known to need it */
  1146.  
  1147.   if (!strcmp (terminal_type, "supdup"))
  1148.     {
  1149.       memory_below_screen = 1;
  1150.       Wcm.cm_losewrap = 1;
  1151.     }
  1152.   if (!strncmp (terminal_type, "c10", 3)
  1153.       || !strcmp (terminal_type, "perq"))
  1154.     {
  1155.       /* Supply a makeshift :wi string.
  1156.      This string is not valid in general since it works only
  1157.      for windows starting at the upper left corner;
  1158.      but that is all Emacs uses.
  1159.  
  1160.      This string works only if the screen is using
  1161.      the top of the video memory, because addressing is memory-relative.
  1162.      So first check the :ti string to see if that is true.
  1163.  
  1164.      It would be simpler if the :wi string could go in the termcap
  1165.      entry, but it can't because it is not fully valid.
  1166.      If it were in the termcap entry, it would confuse other programs.  */
  1167.       if (!TS_set_window)
  1168.     {
  1169.       p = TS_termcap_modes;
  1170.       while (*p && strcmp (p, "\033v  "))
  1171.         p++;
  1172.       if (*p)
  1173.         TS_set_window = "\033v%C %C %C %C ";
  1174.     }
  1175.       /* Termcap entry often fails to have :in: flag */
  1176.       must_write_spaces = 1;
  1177.       /* :ti string typically fails to have \E^G! in it */
  1178.       /* This limits scope of insert-char to one line.  */
  1179.       strcpy (area, TS_termcap_modes);
  1180.       strcat (area, "\033\007!");
  1181.       TS_termcap_modes = area;
  1182.       area += strlen (area) + 1;
  1183.       p = AbsPosition;
  1184.       /* Change all %+ parameters to %C, to handle
  1185.      values above 96 correctly for the C100.  */
  1186.       while (*p)
  1187.     {
  1188.       if (p[0] == '%' && p[1] == '+')
  1189.         p[1] = 'C';
  1190.       p++;
  1191.     }
  1192.     }
  1193.  
  1194.   ScreenRows = screen_height;
  1195.   ScreenCols = screen_width;
  1196.   specified_window = screen_height;
  1197.  
  1198.   if (Wcm_init ())    /* can't do cursor motion */
  1199. #ifdef VMS
  1200.     fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
  1201. It lacks the ability to position the cursor.\n\
  1202. If that is not the actual type of terminal you have, use either the\n\
  1203. DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
  1204. or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.\n",
  1205.            terminal_type);
  1206. #else
  1207.     fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
  1208. It lacks the ability to position the cursor.\n\
  1209. If that is not the actual type of terminal you have,\n\
  1210. use the C-shell command `setenv TERM ...' to specify the correct type.\n\
  1211. It may be necessary to do `unsetenv TERMCAP' as well.\n",
  1212.        terminal_type);
  1213. #endif
  1214.  
  1215.   delete_in_insert_mode
  1216.     = TS_delete_mode && TS_insert_mode
  1217.       && !strcmp (TS_delete_mode, TS_insert_mode);
  1218.  
  1219.   se_is_so = TS_standout_mode && TS_end_standout_mode
  1220.     && !strcmp (TS_standout_mode, TS_end_standout_mode);
  1221.  
  1222.   /* Remove width of standout marker from usable width of line */
  1223.   if (TN_standout_width > 0)
  1224.     screen_width -= TN_standout_width;
  1225.  
  1226.   UseTabs = tabs_safe_p () && TabWidth == 8;
  1227.  
  1228.   scroll_region_ok = TS_set_window || TS_set_scroll_region
  1229.     || TS_set_scroll_region_1;
  1230.  
  1231.   line_ins_del_ok = (((TS_ins_line || TS_ins_multi_lines)
  1232.               && (TS_del_line || TS_del_multi_lines))
  1233.              || (scroll_region_ok
  1234.              && TS_fwd_scroll
  1235.              && TS_rev_scroll));
  1236.  
  1237.   char_ins_del_ok = ((TS_ins_char || TS_insert_mode ||
  1238.               TS_pad_inserted_char || TS_ins_multi_chars)
  1239.              && (TS_del_char || TS_del_multi_chars));
  1240.  
  1241.   fast_clear_end_of_line = TS_clr_line != 0;
  1242.  
  1243.   init_baud_rate ();
  1244.   if (read_socket_hook)        /* Baudrate is somewhat */
  1245.                 /* meaningless in this case */
  1246.     baud_rate = 9600;
  1247. }
  1248.  
  1249. /* VARARGS 1 */
  1250. fatal (str, arg1, arg2)
  1251.      char *str;
  1252. {
  1253.   fprintf (stderr, "emacs: ");
  1254.   fprintf (stderr, str, arg1, arg2);
  1255.   fflush (stderr);
  1256.   exit (1);
  1257. }
  1258.