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