home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vile-src.zip / vile-8.1 / ansi.c < prev    next >
C/C++ Source or Header  |  1998-05-30  |  7KB  |  393 lines

  1. /*
  2.  * The routines in this file provide support for ANSI style terminals
  3.  * over a serial line. The serial I/O services are provided by routines in
  4.  * "termio.c". It compiles into nothing if not an ANSI device.
  5.  *
  6.  *
  7.  * $Header: /usr/build/vile/vile/RCS/ansi.c,v 1.33 1998/05/30 17:37:58 tom Exp $
  8.  */
  9.  
  10.  
  11. #define termdef 1            /* don't define "term" external */
  12.  
  13. #include    "estruct.h"
  14. #include    "edef.h"
  15.  
  16. #if    DISP_ANSI
  17.  
  18. #define SCROLL_REG 1
  19.  
  20.  
  21. #if    SYS_MSDOS
  22. #define NROW    25
  23. #define NCOL    80
  24. #define MAXNROW    60
  25. #define MAXNCOL    132
  26. #undef SCROLL_REG            /* ANSI.SYS can't do scrolling */
  27. #define SCROLL_REG 0
  28. #endif
  29.  
  30. #if    defined(linux)
  31. #define NROW    25            /* Screen size.            */
  32. #define NCOL    80            /* Edit if you want to.        */
  33. #endif
  34.  
  35. #ifndef NROW
  36. #define NROW    24            /* Screen size.            */
  37. #define NCOL    80            /* Edit if you want to.        */
  38. #endif
  39. #ifndef MAXNROW
  40. #define MAXNROW    NROW
  41. #define MAXNCOL    NCOL
  42. #endif
  43.  
  44. #define NPAUSE    100            /* # times thru update to pause */
  45. #define MARGIN    8            /* size of minimim margin and    */
  46. #define SCRSIZ    64            /* scroll size for extended lines */
  47.  
  48. static    void    ansimove   (int row, int col);
  49. static    void    ansieeol   (void);
  50. static    void    ansieeop   (void);
  51. static    void    ansibeep   (void);
  52. static    void    ansiopen   (void);
  53. static    void    ansirev    (UINT state);
  54. static    void    ansiclose  (void);
  55. static    void    ansikopen  (void);
  56. static    void    ansikclose (void);
  57. static    int    ansicres   (const char *flag);
  58. static    void    ansiscroll (int from, int to, int n);
  59.  
  60. #if    OPT_COLOR
  61. static    void    ansifcol (int color);
  62. static    void    ansibcol (int color);
  63.  
  64. static    int    cfcolor = -1;        /* current forground color */
  65. static    int    cbcolor = -1;        /* current background color */
  66.  
  67. #endif
  68.  
  69. /*
  70.  * Standard terminal interface dispatch table. Most of the fields point into
  71.  * "termio" code.
  72.  */
  73. TERM    term    = {
  74.     MAXNROW,    /* max */
  75.     NROW,        /* current */
  76.     MAXNCOL,    /* max */
  77.     NCOL,        /* current */
  78.     MARGIN,
  79.     SCRSIZ,
  80.     NPAUSE,
  81.     ansiopen,
  82.     ansiclose,
  83.     ansikopen,
  84.     ansikclose,
  85.     ttgetc,
  86.     ttputc,
  87.     tttypahead,
  88.     ttflush,
  89.     ansimove,
  90.     ansieeol,
  91.     ansieeop,
  92.     ansibeep,
  93.     ansirev,
  94.     ansicres,
  95. #if    OPT_COLOR
  96.     ansifcol,
  97.     ansibcol,
  98. #else
  99.     null_t_setfor,
  100.     null_t_setback,
  101. #endif
  102.     null_t_setpal,            /* no palette */
  103.     ansiscroll,
  104.     null_t_pflush,
  105.     null_t_icursor,
  106.     null_t_title,
  107.     null_t_watchfd,
  108.     null_t_unwatchfd,
  109. };
  110.  
  111. static    void    ansiparm (int n);
  112. #if SCROLL_REG
  113. static    void    ansiscrollregion (int top, int bot);
  114. #endif
  115.  
  116. static void
  117. csi (void)
  118. {
  119.     ttputc(ESC);
  120.     ttputc('[');
  121. }
  122.  
  123. #if    OPT_COLOR
  124. static void
  125. ansifcol(int color)    /* set the current output color */
  126. {
  127.     if (color == cfcolor)
  128.         return;
  129.     csi();
  130.     ansiparm(color+30);
  131.     ttputc('m');
  132.     cfcolor = color;
  133. }
  134.  
  135. static void
  136. ansibcol(int color)    /* set the current background color */
  137. {
  138.     if (color == cbcolor)
  139.         return;
  140.     csi();
  141.     ansiparm(color+40);
  142.     ttputc('m');
  143.     cbcolor = color;
  144. }
  145. #endif
  146.  
  147. static void
  148. ansimove(int row, int col)
  149. {
  150.     csi();
  151.     ansiparm(row+1);
  152.     ttputc(';');
  153.     ansiparm(col+1);
  154.     ttputc('H');
  155. }
  156.  
  157. static void
  158. ansieeol(void)
  159. {
  160.     csi();
  161.     ttputc('K');
  162. }
  163.  
  164. static void
  165. ansieeop(void)
  166. {
  167. #if    OPT_COLOR
  168.     ansifcol(gfcolor);
  169.     ansibcol(gbcolor);
  170. #endif
  171.     csi();
  172.     ttputc('2');
  173.     ttputc('J');
  174. }
  175.  
  176. #if OPT_COLOR
  177. static void
  178. force_colors(int fc, int bc)
  179. {
  180.     cfcolor =
  181.     cbcolor = -1;
  182.     ansifcol(fc);
  183.     ansibcol(bc);
  184. }
  185. #endif
  186.  
  187. #if BROKEN_REVERSE_VIDEO
  188. /* there was something wrong with this "fix".  the "else" of
  189.         the ifdef just uses "ESC [ 7 m" to set reverse
  190.         video, and it works under DOS for me....  but then, i
  191.         use an "after-market" ansi driver -- nnansi593.zip, from
  192.         oak.oakland.edu, or any simtel mirror. */
  193. static void
  194. ansirev(    /* change reverse video state */
  195. UINT state)    /* TRUE = reverse, FALSE = normal */
  196. {
  197. #if    !OPT_COLOR
  198.     static UINT revstate = SORTOFTRUE;
  199.     if (state == revstate)
  200.         return;
  201.     revstate = state;
  202. #endif
  203.  
  204.     csi();
  205. #if OPT_COLOR && SYS_MSDOS
  206.     ttputc('1');    /* bold-on */
  207. #else
  208.     if (state) ttputc('7');    /* reverse-video on */
  209. #endif
  210.     ttputc('m');
  211.  
  212. #if    OPT_COLOR
  213. #if    SYS_MSDOS
  214.     /*
  215.      * Setting reverse-video with ANSI.SYS seems to reset the colors to
  216.      * monochrome.  Using the colors directly to simulate reverse video
  217.      * works better. Bold-face makes the foreground colors "look" right.
  218.      */
  219.     if (state)
  220.         force_colors(cbcolor, cfcolor);
  221.     else
  222.         force_colors(cfcolor, cbcolor);
  223. #else    /* normal ANSI-reverse */
  224.     if (state == FALSE) {
  225.         force_colors(cfcolor, cbcolor);
  226.     }
  227. #endif    /* MSDOS vs ANSI-reverse */
  228. #endif    /* OPT_COLOR */
  229. }
  230.  
  231. #else
  232.  
  233. static void
  234. ansirev(    /* change reverse video state */
  235. UINT state)    /* TRUE = reverse, FALSE = normal */
  236. {
  237.     static UINT revstate = SORTOFTRUE;
  238.     if (state == revstate)
  239.         return;
  240.     revstate = state;
  241.  
  242.     csi();
  243.     if (state) ttputc('7');    /* reverse-video on */
  244.     ttputc('m');
  245. #if OPT_COLOR
  246.     force_colors(cfcolor, cbcolor);
  247. #endif
  248. }
  249.  
  250. #endif
  251.  
  252. static int
  253. ansicres(const char *flag)    /* change screen resolution */
  254. {
  255.     return(FALSE);
  256. }
  257.  
  258. static void
  259. ansibeep(void)
  260. {
  261.     ttputc(BEL);
  262.     ttflush();
  263. }
  264.  
  265.  
  266. /* if your ansi terminal can scroll regions, like the vt100, then define
  267.     SCROLL_REG.  If not, you can use delete/insert line code, which
  268.     is prettier but slower if you do it a line at a time instead of
  269.     all at once.
  270. */
  271.  
  272. /* move howmany lines starting at from to to */
  273. static void
  274. ansiscroll(int from, int to, int n)
  275. {
  276.     int i;
  277.     if (to == from) return;
  278. #if SCROLL_REG
  279.     if (to < from) {
  280.         ansiscrollregion(to, from + n - 1);
  281.         ansimove(from + n - 1,0);
  282.         for (i = from - to; i > 0; i--)
  283.             ttputc('\n');
  284.     } else { /* from < to */
  285.         ansiscrollregion(from, to + n - 1);
  286.         ansimove(from,0);
  287.         for (i = to - from; i > 0; i--) {
  288.             ttputc(ESC);
  289.             ttputc('M');
  290.         }
  291.     }
  292.     ansiscrollregion(0, term.t_mrow-1);
  293.  
  294. #else /* use insert and delete line */
  295. #if OPT_PRETTIER_SCROLL
  296.     if (absol(from-to) > 1) {
  297.         ansiscroll(from, (from<to) ? to-1:to+1, n);
  298.         if (from < to)
  299.             from = to-1;
  300.         else
  301.             from = to+1;
  302.     }
  303. #endif
  304.     if (to < from) {
  305.         ansimove(to,0);
  306.         csi();
  307.         ansiparm(from - to);
  308.         ttputc('M'); /* delete */
  309.         ansimove(to+n,0);
  310.         csi();
  311.         ansiparm(from - to);
  312.         ttputc('L'); /* insert */
  313.     } else {
  314.         ansimove(from+n,0);
  315.         csi();
  316.         ansiparm(to - from);
  317.         ttputc('M'); /* delete */
  318.         ansimove(from,0);
  319.         csi();
  320.         ansiparm(to - from);
  321.         ttputc('L'); /* insert */
  322.     }
  323. #endif
  324. }
  325.  
  326. #if SCROLL_REG
  327. static void
  328. ansiscrollregion(int top, int bot)
  329. {
  330.     csi();
  331.     ansiparm(top + 1);
  332.     ttputc(';');
  333.     if (bot != term.t_nrow-1) ansiparm(bot + 1);
  334.     ttputc('r');
  335. }
  336. #endif
  337.  
  338.  
  339. static void
  340. ansiparm(register int n)
  341. {
  342.     register int q,r;
  343.  
  344. #if optimize_works /* i don't think it does, although it should, to be ANSI */
  345.     if (n == 1) return;
  346. #endif
  347.  
  348.     q = n/10;
  349.     if (q != 0) {
  350.         r = q/10;
  351.         if (r != 0) {
  352.             ttputc((r%10)+'0');
  353.         }
  354.         ttputc((q%10) + '0');
  355.     }
  356.     ttputc((n%10) + '0');
  357. }
  358.  
  359. static void
  360. ansiopen(void)
  361. {
  362.     static int already_open = FALSE;
  363.     if (!already_open) {
  364.         already_open = TRUE;
  365.         strcpy(sres, "NORMAL");
  366.         revexist = TRUE;
  367.         ttopen();
  368.     }
  369. }
  370.  
  371. static void
  372. ansiclose(void)
  373. {
  374.     TTmove(term.t_nrow-1, 0);    /* cf: dumbterm.c */
  375.     ansieeol();
  376. #if    OPT_COLOR
  377.     ansifcol(C_WHITE);
  378.     ansibcol(C_BLACK);
  379. #endif
  380. }
  381.  
  382. static void
  383. ansikopen(void)        /* open the keyboard (a noop here) */
  384. {
  385. }
  386.  
  387. static void
  388. ansikclose(void)    /* close the keyboard (a noop here) */
  389. {
  390. }
  391.  
  392. #endif    /* DISP_ANSI */
  393.