home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / Editors / mjovesrc.zoo / screen.c < prev    next >
C/C++ Source or Header  |  1992-04-04  |  23KB  |  1,083 lines

  1. /***************************************************************************
  2.  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
  3.  * is provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is    *
  5.  * included in all the files.                                              *
  6.  ***************************************************************************/
  7.  
  8. #include "jove.h"
  9. #include "fp.h"
  10. #include "ctype.h"
  11. #include "termcap.h"
  12. #include "disp.h"
  13. #include <signal.h>
  14.  
  15. int    AbortCnt,
  16.     tabstop = 8;
  17. bool
  18.     CanScroll = NO;
  19.  
  20. #ifdef    TERMCAP
  21. private void
  22.     (*TTins_line) proto((int, int, int)),
  23.     (*TTdel_line) proto((int, int, int));
  24. #endif    /* TERMCAP */
  25.  
  26. struct scrimage
  27.     *DesiredScreen = NULL,
  28.     *PhysScreen = NULL;
  29.  
  30. struct screenline    *Screen = NULL,    /* the screen (a bunch of screenline) */
  31.             *Curline = NULL;    /* current line */
  32.  
  33. private struct screenline   *Savelines = NULL;    /* another bunch (LI of them) */
  34.  
  35.  
  36. private char    *cursor;            /* offset into current Line */
  37.  
  38. char    *cursend;
  39.  
  40. int    CapCol,
  41.     CapLine,
  42.  
  43.     i_line,
  44.     i_col;
  45.  
  46. #ifdef    IBMPC
  47. extern unsigned char    CHPL;
  48. extern void        near normfun(),
  49.             near scr_win(),
  50.             near clr_page(),
  51.             near clr_eoln();
  52.  
  53. #endif
  54.  
  55. void
  56. make_scr()
  57. {
  58.     register int    i;
  59.     register struct screenline    *ns;
  60.     register char    *nsp;
  61.  
  62.     /* In case we are RESHAPING the window! */
  63.     if (DesiredScreen != NULL)
  64.         free((UnivPtr) DesiredScreen);
  65.     if (PhysScreen != NULL)
  66.         free((UnivPtr) PhysScreen);
  67.     if (Savelines != NULL)
  68.         free((UnivPtr) Savelines);
  69.     if (Screen != NULL) {
  70.         free((UnivPtr) Screen->s_line);    /* free all the screen data */
  71.         free((UnivPtr) Screen);
  72.     }
  73.  
  74.     DesiredScreen = (struct scrimage *) malloc((unsigned) LI * sizeof (struct scrimage));
  75.     PhysScreen = (struct scrimage *) malloc((unsigned) LI * sizeof (struct scrimage));
  76.  
  77.     Savelines = (struct screenline *)
  78.             malloc((unsigned) LI * sizeof(struct screenline));
  79.     ns = Screen = (struct screenline *)
  80.             malloc((unsigned) LI * sizeof(struct screenline));
  81.  
  82.     nsp = (char *) malloc((unsigned)CO * LI);
  83.  
  84.     if (DesiredScreen == NULL
  85.     || PhysScreen == NULL
  86.     || Savelines == NULL
  87.     || ns == NULL
  88.     || nsp == NULL)
  89.     {
  90.         writef("\n\rCannot malloc screen!\n");
  91.         finish(SIGHUP);
  92.     }
  93.  
  94.     for (i = 0; i < LI; i++) {
  95.         ns->s_line = nsp;
  96.         nsp += CO;
  97.         ns->s_length = nsp - 1;        /* End of Line */
  98.         ns += 1;
  99.     }
  100.     cl_scr(0);
  101. }
  102.  
  103. void
  104. clrline(cp1, cp2)
  105. register char    *cp1,
  106.         *cp2;
  107. {
  108.     while (cp1 <= cp2)
  109.         *cp1++ = ' ';
  110. }
  111.  
  112.  
  113. /* Output one character (if necessary) at the current position */
  114.  
  115. #ifdef    MAC
  116.  
  117. /* Character output to bit-mapped screen is very expensive. It makes
  118.    much more sense to write the entire line at once. So, we print all
  119.    the characters, whether already there or not, once the line is
  120.    complete.  */
  121.  
  122. private char sput_buf[256];
  123. private int sput_len = 0;
  124.  
  125. private void
  126. sput_start()
  127. {
  128. /*    if (i_line != CapLine || i_col != CapCol) */
  129.         NPlacur(i_line, i_col);
  130.     sput_len = 0;
  131. }
  132.  
  133. private void
  134. sput_end()
  135. {
  136.     sput_buf[0] = (unsigned char) sput_len;
  137.     writechr(sput_buf);
  138.     sput_len = 0;
  139. }
  140.  
  141. private void
  142. sputc(c)
  143. register int c;
  144. {
  145.     if (sput_len < sizeof(sput_buf)) {
  146.         *cursor++ = c;
  147.         sput_buf[++sput_len] = (c == '0')? 0xAF /* slashed zero */ : c;
  148.         CapCol++;
  149.         i_col++;
  150.     }
  151. }
  152.  
  153. #else    /* !MAC */
  154. #ifdef    IBMPC
  155.  
  156. private bool force = NO;
  157.  
  158. private void
  159. sputc(c)
  160. register int    c;
  161. {
  162.     if (force || (*cursor != c)) {
  163.         if (i_line != CapLine || i_col != CapCol)
  164.             Placur(i_line, i_col);
  165.         *cursor++ = c;
  166.         normfun((char) c);
  167.         AbortCnt -= 1;
  168.         CapCol += 1;
  169.     } else {
  170.         cursor += 1;
  171.     }
  172.     i_col += 1;
  173. }
  174.  
  175. #else    /* !IBMPC */
  176.  
  177. #  define sputc(c)    { \
  178.     if (*cursor != (char) (c)) { \
  179.         do_sputc(c); \
  180.     } else { \
  181.         cursor++; \
  182.         i_col++; \
  183.     } \
  184. }
  185.  
  186. private void
  187. do_sputc(c)
  188. register int    c;
  189. {
  190.     if (*cursor != c) {
  191. # ifdef    ID_CHAR
  192.         if (IN_INSmode)
  193.             INSmode(OFF);
  194. # endif
  195.         if (i_line != CapLine || i_col != CapCol)
  196.             Placur(i_line, i_col);
  197.         if (UL && (c & CHARMASK) == '_' && (*cursor & CHARMASK) != ' ')
  198.             putstr(" \b");        /* Erase so '_' looks right. */
  199.         *cursor++ = c;
  200.         jputchar(c & CHARMASK);
  201.         AbortCnt -= 1;
  202.         CapCol += 1;
  203.     } else {
  204.         cursor += 1;
  205.     }
  206.     i_col += 1;
  207. }
  208.  
  209. #endif    /* !IBMPC */
  210. #endif    /* !MAC */
  211.  
  212. void
  213. cl_eol()
  214. {
  215.     if (cursor > cursend)
  216.         return;
  217.  
  218.     if (cursor < Curline->s_length) {
  219. #ifdef    TERMCAP
  220.         if (CE) {
  221.             Placur(i_line, i_col);
  222.             putpad(CE, 1);
  223.             clrline(cursor, Curline->s_length);
  224.         } else {
  225.             /* Ugh.  The slow way for dumb terminals. */
  226.             register char *savecp = cursor;
  227.  
  228.             while (cursor <= Curline->s_length)
  229.                 sputc(' ');
  230.             cursor = savecp;
  231.         }
  232. #else    /* !TERMCAP */
  233.         Placur(i_line, i_col);
  234.         clr_eoln();
  235.         clrline(cursor, Curline->s_length);
  236. #endif    /* !TERMCAP */
  237.         Curline->s_length = cursor;
  238.     }
  239. }
  240.  
  241. void
  242. cl_scr(doit)
  243. bool doit;
  244. {
  245.     register int    i;
  246.     register struct screenline    *sp = Screen;
  247.  
  248.     for (i = 0; i < LI; i++, sp++) {
  249.         clrline(sp->s_line, sp->s_length);
  250.         sp->s_length = sp->s_line;
  251.         PhysScreen[i].s_id = 0;
  252.     }
  253.     if (doit) {
  254. #ifdef    TERMCAP
  255.         putpad(CL, LI);
  256. #else    /* !TERMCAP */
  257.         clr_page();
  258. #endif    /* !TERMCAP */
  259.         CapCol = CapLine = 0;
  260.         UpdMesg = YES;
  261.     }
  262. }
  263.  
  264. /* Write `line' at the current position of `cursor'.  Stop when we
  265.    reach the end of the screen.  Aborts if there is a character
  266.    waiting.  */
  267.  
  268.  
  269. bool
  270. swrite(line, inversep, abortable)
  271. register char    *line;
  272. bool    inversep;
  273. bool    abortable;
  274. {
  275.     register int    n = cursend - cursor;
  276.     bool    aborted = NO;
  277.  
  278.     if (n > 0) {
  279.  
  280.         register int    c;
  281.         int    col = i_col;
  282. #ifdef    MAC
  283. #        define    spit(c)    sputc(c)
  284. #else    /* !MAC */
  285. #ifdef    IBMPC
  286. #        define    spit(c)    sputc(c)
  287. #else    /* !IBMPC */
  288.         int    or_byte = inversep ? 0200 : 0;
  289. #        define    spit(c)    { int temp = (c) | or_byte; sputc(temp); }
  290. #endif    /* !IBMPC */
  291. #endif    /* !MAC */
  292.  
  293. #ifdef    MAC
  294.         sput_start();    /* Okay, because no interruption possible */
  295. #endif    /* MAC */
  296. #ifdef    IBMPC
  297.         force = inversep;  /* to force a redraw of the modeline */
  298. #endif
  299.         while ((c = *line++) != '\0') {
  300. #            define  spot(c) { if (--n <= 0) break; spit(c); col += 1; }
  301.  
  302.             if (abortable && AbortCnt < 0) {
  303.                 AbortCnt = BufSize;
  304.                 if ((InputPending = charp()) != NO) {
  305.                     aborted = YES;
  306.                     break;
  307.                 }
  308.             }
  309.             if (c == '\t') {
  310.                 int    nchars;
  311.  
  312.                 nchars = (tabstop - (col % tabstop));
  313.                 while (--nchars > 0)
  314.                     spot(' ');
  315.                 c = ' ';
  316.             } else if (jiscntrl(c)) {
  317.                 spot('^');
  318.                 c = (c == '\177') ? '?' : c + '@';
  319. #ifdef    TERMCAP
  320.             } else if (Hazeltine && c == '~') {
  321.                 c = '`';
  322. #endif
  323. #ifdef    IBMPC
  324.             } else if (c == 255) {
  325.                 c = 1;
  326.             } else if (c == ' ' && inversep) {
  327.                 c = 255;
  328. #endif    /* IBMPC */
  329.             }
  330.             spot(c);
  331. #            undef    spot
  332.         }
  333.         if (n <= 0)
  334.             spit(((*line=='\0') && (c!='\t') && !jiscntrl(c))? c : '!');
  335.         if (cursor > Curline->s_length)
  336.             Curline->s_length = cursor;
  337. #ifdef    MAC
  338.         sput_end();
  339. #endif    /* MAC */
  340. #ifdef    IBMPC
  341.         force = NO;
  342. #endif
  343. #        undef    spit
  344.     }
  345.     return !aborted;
  346. }
  347.  
  348. /* This is for writing a buffer line to the screen.  This is to
  349.    minimize the amount of copying from one buffer to another buffer.
  350.    This gets the info directly from the disk buffers. */
  351.  
  352.  
  353. bool
  354. BufSwrite(linenum)
  355. int linenum;
  356. {
  357.     register int    n = cursend - cursor,
  358.             col = 0,
  359.             c = -1;
  360.     register char    *bp;
  361.     int    StartCol = DesiredScreen[linenum].s_offset,
  362.         visspace = DesiredScreen[linenum].s_window->w_flags & W_VISSPACE;
  363.     bool    aborted = NO;
  364.  
  365.     bp = lcontents(DesiredScreen[linenum].s_lp);
  366.     if (*bp) {
  367.         for (;;) {
  368.             if (col >= StartCol) {
  369.                 DesiredScreen[linenum].s_offset = col;
  370.                 break;
  371.             }
  372.  
  373.             c = *bp++ & CHARMASK;
  374.             if (c == '\0')
  375.                 break;
  376.             if (c == '\t')
  377.                 col += (tabstop - (col % tabstop));
  378.             else if (jiscntrl(c))
  379.                 col += 2;
  380.             else
  381.                 col += 1;
  382.         }
  383.     }
  384. #ifdef    MAC
  385.     sput_start();    /* Okay because we can't be interrupted */
  386. #endif
  387.     if (c != '\0') {
  388.         while ((c = *bp++) != '\0') {
  389. #            define spot(c)  { if (--n <= 0) break; sputc(c); col += 1; }
  390.  
  391.             if (AbortCnt < 0) {
  392.                 AbortCnt = BufSize;
  393.                 if ((InputPending = charp()) != NO) {
  394.                     aborted = YES;
  395.                     break;
  396.                 }
  397.             }
  398.             if (c == '\t') {
  399.                 int    nchars = (tabstop - (col % tabstop));
  400.  
  401.                 if (visspace) {
  402.                     spot('>');
  403.                     nchars -= 1;
  404.                 }
  405.                 while (--nchars > 0)
  406.                     spot(' ');
  407.                 c = ' ';
  408.             } else if (jiscntrl(c)) {
  409.                 spot('^');
  410.                 c = (c == '\177') ? '?' : c + '@';
  411.             } else if (c == ' ' && visspace) {
  412.                 c = '_';
  413. #ifdef    TERMCAP
  414.             } else if (Hazeltine && c == '~') {
  415.                 c = '`';
  416. #endif
  417. #ifdef    IBMPC
  418.             } else if (c == 255) {
  419.                    c = 1;
  420. #endif    /* IBMPC */
  421.             }
  422.             spot(c);
  423. #            undef    spot
  424.         }
  425.     }
  426.     if (n <= 0)
  427.         sputc(((*bp == '\0') && (c != '\t') && !jiscntrl(c))? c : '!');
  428.     if (cursor > Curline->s_length)
  429.         Curline->s_length = cursor;
  430. #ifdef    MAC
  431.     sput_end();
  432. #endif
  433.     return !aborted;        /* Didn't abort */
  434. }
  435.  
  436. void
  437. i_set(nline, ncol)
  438. register int    nline,
  439.         ncol;
  440. {
  441.     Curline = &Screen[nline];
  442.     cursor = Curline->s_line + ncol;
  443.     cursend = &Curline->s_line[CO - 1];
  444.     i_line = nline;
  445.     i_col = ncol;
  446. }
  447.  
  448. #ifdef    TERMCAP
  449. void
  450. SO_on()
  451. {
  452.     /* If there are magic cookies, then WHERE the SO string is
  453.        printed decides where the SO actually starts on the screen.
  454.        So it's important to make sure the cursor is positioned there
  455.        anyway.  I think this is right. */
  456.     if (SG != 0) {
  457.         Placur(i_line, i_col);
  458.         i_col += SG;
  459.         CapCol += SG;
  460.     }
  461.     putpad(SO, 1);
  462. }
  463.  
  464. void
  465. SO_off()
  466. {
  467.     /* see comment in SO_on() */
  468.     if (SG != 0) {
  469.         Placur(i_line, i_col);
  470.         i_col += SG;
  471.         CapCol += SG;
  472.     }
  473.     putpad(SE, 1);
  474. }
  475. #endif    /* TERMCAP */
  476.  
  477. /* Insert `num' lines a top, but leave all the lines BELOW `bottom'
  478.    alone (at least they won't look any different when we are done).
  479.    This changes the screen array AND does the physical changes. */
  480.  
  481. void
  482. v_ins_line(num, top, bottom)
  483. int num,
  484.     top,
  485.     bottom;
  486. {
  487.     register int    i;
  488.  
  489.     /* Save the screen pointers. */
  490.  
  491.     for(i = 0; i < num && top + i <= bottom; i++)
  492.         Savelines[i] = Screen[bottom - i];
  493.  
  494.     /* Num number of bottom lines will be lost.
  495.        Copy everything down num number of times. */
  496.  
  497.     for (i = bottom; i > top && i-num >= 0; i--)
  498.         Screen[i] = Screen[i - num];
  499.  
  500.     /* Restore the saved ones, making them blank. */
  501.  
  502.     for (i = 0; i < num; i++) {
  503.         Screen[top + i] = Savelines[i];
  504.         clrline(Screen[top + i].s_line, Screen[top + i].s_length);
  505.         Screen[top + i].s_length = Screen[top + i].s_line;
  506.     }
  507.  
  508. #ifdef    IBMPC
  509.     scr_win((int) -num, (unsigned char) top, 0, (unsigned char) bottom, CHPL-1);
  510. #else    /* !IBMPC */
  511. # ifdef    MAC
  512.     i_lines(top, bottom, num);
  513. # else    /* !MAC */
  514.     (*TTins_line)(top, bottom, num);
  515. # endif    /* !MAC */
  516. #endif    /* !IBMPC */
  517. }
  518.  
  519. /* Delete `num' lines starting at `top' leaving the lines below `bottom'
  520.    alone.  This updates the internal image as well as the physical image.  */
  521.  
  522. void
  523. v_del_line(num, top, bottom)
  524. int num,
  525.     top,
  526.     bottom;
  527. {
  528.     register int    i,
  529.             bot;
  530.  
  531.     bot = bottom;
  532.  
  533.     /* Save the lost lines. */
  534.  
  535.     for (i = 0; i < num && top + i <= bottom; i++)
  536.         Savelines[i] = Screen[top + i];
  537.  
  538.     /* Copy everything up num number of lines. */
  539.  
  540.     for (i = top; num + i <= bottom; i++)
  541.         Screen[i] = Screen[i + num];
  542.  
  543.     /* Restore the lost ones, clearing them. */
  544.  
  545.     for (i = 0; i < num; i++) {
  546.         Screen[bottom - i] = Savelines[i];
  547.         clrline(Screen[bot].s_line, Screen[bot].s_length);
  548.         Screen[bot].s_length = Screen[bot].s_line;
  549.         bot -= 1;
  550.     }
  551.  
  552. #ifdef    IBMPC
  553.     scr_win(num, (unsigned char) top, 0, (unsigned char) bottom, CHPL-1);
  554. #else    /* !IBMPC */
  555. # ifdef    MAC
  556.     d_lines(top, bottom, num);
  557. # else    /* !MAC */
  558.     (*TTdel_line)(top, bottom, num);
  559. # endif    /* !MAC */
  560. #endif    /* !IBMPC */
  561. }
  562.  
  563. #ifdef    TERMCAP    /* remainder of this file */
  564.  
  565. /* The cursor optimization happens here.  You may decide that this
  566.    is going too far with cursor optimization, or perhaps it should
  567.    limit the amount of checking to when the output speed is slow.
  568.    What ever turns you on ...   */
  569.  
  570. struct cursaddr {
  571.     int    cm_numchars;
  572.     void    (*cm_proc) ();
  573. };
  574.  
  575. private char    *Cmstr;
  576. private struct cursaddr    *HorMin,
  577.             *VertMin,
  578.             *DirectMin;
  579.  
  580. private void
  581.     GENi_lines proto((int, int, int)),
  582.     GENd_lines proto((int, int, int)),
  583.     ForMotion proto((int)),
  584.     ForTab proto((int)),
  585.     BackMotion proto((int)),
  586.     RetTab proto((int)),
  587.     DownMotion proto((int)),
  588.     UpMotion proto((int)),
  589.     GoDirect proto((int, int)),
  590.     HomeGo proto((int, int)),
  591.     BottomUp proto((int, int));
  592.  
  593.  
  594. private struct cursaddr    WarpHor[] = {
  595.     0,    ForMotion,
  596.     0,    ForTab,
  597.     0,    BackMotion,
  598.     0,    RetTab
  599. };
  600.  
  601. private struct cursaddr    WarpVert[] = {
  602.     0,    DownMotion,
  603.     0,    UpMotion
  604. };
  605.  
  606. private struct cursaddr    WarpDirect[] = {
  607.     0,    GoDirect,
  608.     0,    HomeGo,
  609.     0,    BottomUp
  610. };
  611.  
  612. #undef    FORWARD
  613. #define    FORWARD        0    /* Move forward */
  614. #define FORTAB        1    /* Forward using tabs */
  615. #undef    BACKWARD
  616. #define    BACKWARD    2    /* Move backward */
  617. #define RETFORWARD    3    /* Beginning of line and then tabs */
  618. #define NUMHOR        4
  619.  
  620. #define DOWN        0    /* Move down */
  621. #define UPMOVE        1    /* Move up */
  622. #define NUMVERT        2
  623.  
  624. #define DIRECT        0    /* Using CM */
  625. #define HOME        1    /* HOME    */
  626. #define LOWER        2    /* Lower Line */
  627. #define NUMDIRECT    3
  628.  
  629. #define    home()        Placur(0, 0)
  630. #define LowLine()    { putpad(LL, 1); CapLine = ILI; CapCol = 0; }
  631. #define PrintHo()    { putpad(HO, 1); CapLine = CapCol = 0; }
  632.  
  633. int    phystab = 8;
  634.  
  635. private void
  636. GoDirect(line, col)
  637. register int    line,
  638.         col;
  639. {
  640.     putpad(Cmstr, 1);
  641.     CapLine = line;
  642.     CapCol = col;
  643. }
  644.  
  645. private void
  646. RetTab(col)
  647. register int    col;
  648. {
  649.     jputchar('\r');
  650.     CapCol = 0;
  651.     ForTab(col);
  652. }
  653.  
  654. private void
  655. HomeGo(line, col)
  656. int line,
  657.     col;
  658. {
  659.     PrintHo();
  660.     DownMotion(line);
  661.     ForTab(col);
  662. }
  663.  
  664. private void
  665. BottomUp(line, col)
  666. register int    line,
  667.         col;
  668. {
  669.     LowLine();
  670.     UpMotion(line);
  671.     ForTab(col);
  672. }
  673.  
  674. /* Tries to move forward using tabs (if possible).  It tabs to the
  675.    closest tabstop which means it may go past 'destcol' and backspace
  676.    to it. */
  677.  
  678. private void
  679. ForTab(destcol)
  680. int    destcol;
  681. {
  682.     register int    tabgoal,
  683.             ntabs,
  684.             tabstp = phystab;
  685.  
  686.     if (TABS && (tabstp > 0)) {
  687.         tabgoal = destcol + (tabstp / 2);
  688.         tabgoal -= (tabgoal % tabstp);
  689.  
  690.         /* Don't tab to last place or else it is likely to screw up. */
  691.         if (tabgoal >= CO)
  692.             tabgoal -= tabstp;
  693.  
  694.         ntabs = (tabgoal / tabstp) - (CapCol / tabstp);
  695.         while (--ntabs >= 0)
  696.             jputchar('\t');
  697.         CapCol = tabgoal;
  698.     }
  699.     if (CapCol > destcol)
  700.         BackMotion(destcol);
  701.     else if (CapCol < destcol)
  702.         ForMotion(destcol);
  703. }
  704.  
  705. private void
  706. ForMotion(destcol)
  707. register int    destcol;
  708. {
  709.     register int    nchars = destcol - CapCol;
  710.     register char    *cp = &Screen[CapLine].s_line[CapCol];
  711.  
  712.     while (--nchars >= 0)
  713.         jputchar(*cp++ & CHARMASK);
  714.     CapCol = destcol;
  715. }
  716.  
  717. private void
  718. BackMotion(destcol)
  719. register int    destcol;
  720. {
  721.     register int    nchars = CapCol - destcol;
  722.  
  723. #ifndef MiNT
  724.     if (BC)
  725.         while (--nchars >= 0)
  726.             putpad(BC, 1);
  727.     else
  728. #endif
  729.         while (--nchars >= 0)
  730.             jputchar('\b');
  731.     CapCol = destcol;
  732. }
  733.  
  734. private void
  735. DownMotion(destline)
  736. register int    destline;
  737. {
  738.     register int    nlines = destline - CapLine;
  739.  
  740.     while (--nlines >= 0)
  741.         putpad(DO, 1);
  742.     CapLine = destline;
  743. }
  744.  
  745. private void
  746. UpMotion(destline)
  747. register int    destline;
  748. {
  749.     register int    nchars = CapLine - destline;
  750.  
  751.     while (--nchars >= 0)
  752.         putpad(SR, 1);    /* was UP */
  753.     CapLine = destline;
  754. }
  755.  
  756. #ifdef    ID_CHAR
  757. static int    EIlen;
  758. #endif
  759.  
  760. void
  761. InitCM()
  762. {
  763.     HOlen = HO ? strlen(HO) : 1000;
  764.     LLlen = LL ? strlen(LL) : 1000;
  765.     UPlen = SR ? strlen(SR) : 1000;   /*  was UP  */
  766. #ifdef    ID_CHAR
  767.     if (EI)
  768.         EIlen = strlen(EI);
  769. #endif
  770. }
  771.  
  772. private int ForNum proto((int from, int to));
  773.  
  774. void
  775. Placur(line, col)
  776. int line,
  777.     col;
  778. {
  779.     int    dline,        /* Number of lines to move */
  780.         dcol;        /* Number of columns to move */
  781.     register int    best,
  782.             i;
  783.     register struct cursaddr    *cp;
  784.     int    xtracost = 0;    /* Misc addition to cost. */
  785.  
  786. #define CursMin(which,addrs,max)    { \
  787.     for (best = 0, cp = &(addrs)[1], i = 1; i < (max); i++, cp++) \
  788.         if (cp->cm_numchars < (addrs)[best].cm_numchars) \
  789.             best = i; \
  790.     (which) = &(addrs)[best]; \
  791. }
  792.  
  793.     if (line == CapLine && col == CapCol)
  794.         return;        /* We are already there. */
  795.  
  796.     dline = line - CapLine;
  797.     dcol = col - CapCol;
  798. #ifdef    ID_CHAR
  799.     if (IN_INSmode && MI)
  800.         xtracost = EIlen + IMlen;
  801.     /* If we're already in insert mode, it is likely that we will
  802.        want to be in insert mode again, after the insert. */
  803. #endif
  804.  
  805.     /* Number of characters to move horizontally for each case.
  806.        1: Just move forward by typing the right character on the screen.
  807.        2: Print the correct number of back spaces.
  808.        3: Try tabbing to the correct place.
  809.        4: Try going to the beginning of the line, and then tab. */
  810.  
  811.     if (dcol == 1 || dcol == 0) {        /* Most common case. */
  812.         HorMin = &WarpHor[FORWARD];
  813.         HorMin->cm_numchars = dcol + xtracost;
  814.     } else {
  815.         WarpHor[FORWARD].cm_numchars = dcol >= 0 ? dcol + xtracost : 1000;
  816.         WarpHor[BACKWARD].cm_numchars = dcol < 0 ? -(dcol + xtracost) : 1000;
  817.         WarpHor[FORTAB].cm_numchars = dcol >= 0 && TABS ?
  818.                 ForNum(CapCol, col) + xtracost : 1000;
  819.         WarpHor[RETFORWARD].cm_numchars = (xtracost + 1 + (TABS ? ForNum(0, col) : col));
  820.  
  821.         /* Which is the shortest of the bunch */
  822.  
  823.         CursMin(HorMin, WarpHor, NUMHOR);
  824.     }
  825.  
  826.     /* Moving vertically is more simple. */
  827.  
  828.     WarpVert[DOWN].cm_numchars = dline >= 0 ? dline : 1000;
  829.     WarpVert[UPMOVE].cm_numchars = dline < 0 ? ((-dline) * UPlen) : 1000;
  830.  
  831.     /* Which of these is simpler */
  832.     CursMin(VertMin, WarpVert, NUMVERT);
  833.  
  834.     /* Homing first and lowering first are considered
  835.        direct motions.
  836.        Homing first's total is the sum of the cost of homing
  837.        and the sum of tabbing (if possible) to the right. */
  838.  
  839.     if (VertMin->cm_numchars + HorMin->cm_numchars <= 3) {
  840.         DirectMin = &WarpDirect[DIRECT];    /* A dummy ... */
  841.         DirectMin->cm_numchars = 100;
  842.     } else {
  843.         WarpDirect[DIRECT].cm_numchars = CM ?
  844.                 strlen(Cmstr = tgoto(CM, col, line)) : 1000;
  845.         WarpDirect[HOME].cm_numchars = HOlen + line +
  846.                 WarpHor[RETFORWARD].cm_numchars;
  847.         WarpDirect[LOWER].cm_numchars = LLlen + ((ILI - line) * UPlen) +
  848.                 WarpHor[RETFORWARD].cm_numchars;
  849.         CursMin(DirectMin, WarpDirect, NUMDIRECT);
  850.     }
  851.  
  852.     if (HorMin->cm_numchars + VertMin->cm_numchars < DirectMin->cm_numchars) {
  853.         if (line != CapLine)
  854.             (*(void (*)ptrproto((int)))VertMin->cm_proc)(line);
  855.         if (col != CapCol) {
  856. #ifdef    ID_CHAR
  857.             if (IN_INSmode)    /* We may use real characters ... */
  858.                 INSmode(OFF);
  859. #endif
  860.             (*(void (*)ptrproto((int)))HorMin->cm_proc)(col);
  861.         }
  862.     } else {
  863. #ifdef    ID_CHAR
  864.         if (IN_INSmode && !MI)
  865.             INSmode(OFF);
  866. #endif
  867.         (*(void (*)ptrproto((int, int)))DirectMin->cm_proc)(line, col);
  868.     }
  869. }
  870.  
  871. #define abs(x)    ((x) >= 0 ? (x) : -(x))
  872.  
  873. private int
  874. ForNum(from, to)
  875. register int    from;
  876. int to;
  877. {
  878.     register int    tabgoal,
  879.             tabstp = phystab;
  880.     int        numchars = 0;
  881.  
  882.     if (from >= to)
  883.         return from - to;
  884.     if (TABS && (tabstp > 0)) {
  885.         tabgoal = to + (tabstp / 2);
  886.         tabgoal -= (tabgoal % tabstp);
  887.         if (tabgoal >= CO)
  888.             tabgoal -= tabstp;
  889.         numchars = (tabgoal / tabstop) - (from / tabstp);
  890.         from = tabgoal;
  891.     }
  892.     return numchars + abs(from - to);
  893. }
  894.  
  895. #ifdef    WIRED_TERMS
  896.  
  897. private void
  898. BGi_lines(top, bottom, num)
  899. int top,
  900.     bottom,
  901.     num;
  902. {
  903.     writef("\033[%d;%dr\033[%dL\033[r", top + 1, bottom + 1, num);
  904.     CapCol = CapLine = 0;
  905. }
  906.  
  907. private void
  908. SUNi_lines(top, bottom, num)
  909. int top,
  910.     bottom,
  911.     num;
  912. {
  913.     Placur(bottom - num + 1, 0);
  914.     writef("\033[%dM", num);
  915.     Placur(top, 0);
  916.     writef("\033[%dL", num);
  917. }
  918.  
  919. private void
  920. C100i_lines(top, bottom, num)
  921. int top,
  922.     bottom,
  923.     num;
  924. {
  925.     if (num <= 1) {
  926.         GENi_lines(top, bottom, num);
  927.         return;
  928.     }
  929.     writef("\033v%c%c%c%c", ' ', ' ', ' ' + bottom + 1, ' ' + CO);
  930.     CapLine = CapCol = 0;
  931.     Placur(top, 0);
  932.     while (num--)
  933.         putpad(AL, ILI - CapLine);
  934.     writef("\033v%c%c%c%c", ' ', ' ', ' ' + LI, ' ' + CO);
  935.     CapLine = CapCol = 0;
  936. }
  937.  
  938. #endif    /* WIRED_TERMS */
  939.  
  940. private void
  941. GENi_lines(top, bottom, num)
  942. int top,
  943.     bottom,
  944.     num;
  945. {
  946.     register int    i;
  947.  
  948.     if (CS) {
  949.         putpad(tgoto(CS, bottom, top), 1);
  950.         CapCol = CapLine = 0;
  951.         Placur(top, 0);
  952.         for (i = 0; i < num; i++)
  953.             putpad(SR, bottom - top);
  954.         putpad(tgoto(CS, ILI, 0), 1);
  955.         CapCol = CapLine = 0;
  956.     } else {
  957.         Placur(bottom - num + 1, 0);
  958.         if (M_DL && (num > 1)) {
  959.             putargpad(M_DL, num, ILI - CapLine);
  960.         } else {
  961.             for (i = 0; i < num; i++)
  962.                 putpad(DL, ILI - CapLine);
  963.         }
  964.         Placur(top, 0);
  965.         if (M_AL && (num > 1)) {
  966.             putargpad(M_AL, num, ILI - CapLine);
  967.         } else {
  968.             for (i = 0; i < num; i++)
  969.                 putpad(AL, ILI - CapLine);
  970.         }
  971.     }
  972. }
  973.  
  974. #ifdef    WIRED_TERMS
  975.  
  976. private void
  977. BGd_lines(top, bottom, num)
  978. int top,
  979.     bottom,
  980.     num;
  981. {
  982.     writef("\033[%d;%dr\033[%dM\033[r", top + 1, bottom + 1, num);
  983.     CapCol = CapLine = 0;
  984. }
  985.  
  986. private void
  987. SUNd_lines(top, bottom, num)
  988. int top,
  989.     bottom,
  990.     num;
  991. {
  992.     Placur(top, 0);
  993.     writef("\033[%dM", num);
  994.     Placur(bottom + 1 - num, 0);
  995.     writef("\033[%dL", num);
  996. }
  997.  
  998. private void
  999. C100d_lines(top, bottom, num)
  1000. int top,
  1001.     bottom,
  1002.     num;
  1003. {
  1004.     if (num <= 1) {
  1005.         GENd_lines(top, bottom, num);
  1006.         return;
  1007.     }
  1008.     writef("\033v%c%c%c%c", ' ', ' ', ' ' + bottom + 1, ' ' + CO);
  1009.     CapLine = CapCol = 0;
  1010.     Placur(top, 0);
  1011.     while (num--)
  1012.         putpad(DL, ILI - CapLine);
  1013.     writef("\033v%c%c%c%c", ' ', ' ', ' ' + LI, ' ' + CO);
  1014.     CapLine = CapCol = 0;
  1015. }
  1016.  
  1017. #endif    /* WIRED_TERMS */
  1018.  
  1019. private void
  1020. GENd_lines(top, bottom, num)
  1021. int top,
  1022.     bottom,
  1023.     num;
  1024. {
  1025.     register int    i;
  1026.  
  1027.     if (CS) {
  1028.         putpad(tgoto(CS, bottom, top), 1);
  1029.         CapCol = CapLine = 0;
  1030.         Placur(bottom, 0);
  1031.         for (i = 0; i < num; i++)
  1032.             putpad(SF, bottom - top);
  1033.         putpad(tgoto(CS, ILI, 0), 1);
  1034.         CapCol = CapLine = 0;
  1035.     } else {
  1036.         Placur(top, 0);
  1037.         if (M_DL && (num > 1)) {
  1038.             putargpad(M_DL, num, ILI - top);
  1039.         } else {
  1040.             for (i = 0; i < num; i++)
  1041.                 putpad(DL, ILI - top);
  1042.         }
  1043.         Placur(bottom + 1 - num, 0);
  1044.         if (M_AL && (num > 1)) {
  1045.             putargpad(M_AL, num, ILI - CapLine);
  1046.         } else {
  1047.             for (i = 0; i < num; i++)
  1048.                 putpad(AL, ILI - CapLine);
  1049.         }
  1050.     }
  1051. }
  1052.  
  1053. private const struct ID_lookup {
  1054.     char    *ID_name;
  1055.     void    (*I_proc) proto((int, int, int));    /* proc to insert lines */
  1056.     void    (*D_proc) proto((int, int, int));    /* proc to delete lines */
  1057. } ID_trms[] = {
  1058.     "generic",    GENi_lines,    GENd_lines,    /* This should stay here */
  1059. #ifdef    WIRED_TERMS
  1060.     "sun",        SUNi_lines,    SUNd_lines,
  1061.     "bg",        BGi_lines,    BGd_lines,
  1062.     "c1",        C100i_lines,    C100d_lines,
  1063. #endif    /* WIRED_TERMS */
  1064.     NULL,        0,        0
  1065. };
  1066.  
  1067. void
  1068. IDline_setup(tname)
  1069. char    *tname;
  1070. {
  1071.     register const struct ID_lookup    *idp;
  1072.  
  1073.     for (idp = &ID_trms[1]; idp->ID_name; idp++)
  1074.         if (strncmp(idp->ID_name, tname, strlen(idp->ID_name)) == 0)
  1075.             break;
  1076.     if (idp->ID_name == NULL)
  1077.         idp = &ID_trms[0];
  1078.     TTins_line = idp->I_proc;
  1079.     TTdel_line = idp->D_proc;
  1080. }
  1081.  
  1082. #endif    /* TERMCAP */
  1083.