home *** CD-ROM | disk | FTP | other *** search
/ Best Objectech Shareware Selections / UNTITLED.iso / boss / word / text / 019 / display.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  9KB  |  375 lines

  1. /*
  2.  *  Copyright (c) 1992 John E. Davis  (davis@amy.tch.harvard.edu)
  3.  *  All Rights Reserved.
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "display.h"
  8. #include "sysdep.h"
  9.  
  10. /* extern int fprintf(FILE *, char *, ...);
  11.    extern int fputs(char *, FILE *); */
  12.  
  13. char *INS_MODE_STR = "\033[4h";   /* ins mode (im) */
  14. char *EINS_MODE_STR = "\033[4l";  /* end ins mode (ei) */
  15. char *SCROLL_R_STR = "\033[%d;%dr"; /* scroll region */
  16. char *CLS_STR = "\033[2J\033[H";  /* cl termcap STR  for ansi terminals */
  17. char *CLR_BOS_STR = "\033[1J";   /* erase to beg of screen */
  18. char *REV_INDEX_STR = "\033M";     /* sr termcap string */
  19. char *REV_VID_STR = "\033[7m";    /* mr,so termcap string */
  20. char *BOLD_VID_STR = "\033[1m";    /**/
  21. char *UNDL_VID_STR = "\033[4m";    /**/
  22. char *NORM_VID_STR = "\033[m";   /* me,se termcap string */
  23. char *DEL_BOL_STR = "\033[1K\r";  /* cb termcap entry */
  24. char *DEL_EOL_STR = "\033[K";
  25. char *HOME_CURS_STR = "\033[H";   /* ho termcap string */
  26. char *DEL_LINE_STR = "\r\033[K";   /* dl termcap string except cursor at bol */
  27. char *DEL_CHAR_STR = "\033[P";   /* dc */
  28. char *DEL_N_LINES_STR = "\033[%dM";  /* DL */
  29. char *ADD_N_LINES_STR = "\033[%dL";  /* AL */
  30. char *CURS_F_STR = "\033[%dC";    /* RI termcap string */
  31. char *CURS_B_STR = "\033[%dD";    /* RI termcap string */
  32. char *CURS_U_STR = "\033[%dA";    /* RI termcap string */
  33. char *CURS_D_STR = "\033[%dB";    /* RI termcap string */
  34. /* cm string has %i%d since termcap numbers columns from 0 */
  35. /* char *CURS_POS_STR = "\033[%d;%df";  ansi-- hor and vert pos */
  36. char *CURS_POS_STR = "\033[%d;%dH";   /* cm termcap string */
  37.  
  38. void set_scroll_region(int r1, int r2)
  39. {
  40.     fprintf(stdout,SCROLL_R_STR,r1,r2);
  41. }
  42.  
  43. void reset_scroll_region()
  44. {
  45.     set_scroll_region(1, Screen_Height);
  46. }
  47.  
  48. void goto_rc(int r, int c)
  49. {
  50.     fprintf(stdout,CURS_POS_STR,r,c);
  51. }
  52. /* void curs_bol()
  53. {
  54.     fputc('\015',stdout);
  55. }
  56.  
  57. void cursor_forward(int n)
  58. {
  59.     if (n) fprintf(stdout,CURS_F_STR,n);
  60. }
  61.  
  62. void cursor_backward(int n)
  63. {
  64.     if (n) fprintf(stdout,CURS_B_STR,n);
  65. }
  66.  
  67. void cursor_up(int n)
  68. {
  69.     fprintf(stdout,CURS_U_STR,n);
  70. }
  71.  
  72. void cursor_down(int n)
  73. {
  74.     fprintf(stdout,CURS_D_STR,n);
  75. }
  76. */
  77. void begin_insert()
  78. {
  79.     fputs(INS_MODE_STR,stdout);
  80. }
  81.  
  82. void end_insert()
  83. {
  84.     fputs(EINS_MODE_STR,stdout);
  85. }
  86.  
  87. void tt_delete_char()
  88. {
  89.     fputs(DEL_CHAR_STR,stdout);
  90. }
  91.  
  92. void tt_erase_line()
  93. {
  94.     fprintf(stdout,DEL_LINE_STR);
  95. }
  96.  
  97. void tt_delete_nlines(int n)
  98. {
  99.     if (n) fprintf(stdout,DEL_N_LINES_STR,n);
  100. }
  101.  
  102. void cls()
  103. {
  104.     fputs(CLS_STR,stdout);
  105. }
  106.  
  107. void reverse_index(int n)
  108. {
  109.     while(n--)
  110.       fputs(REV_INDEX_STR,stdout);
  111. }
  112.  
  113. int beep()
  114. {
  115.     putc('\007', stdout);
  116.     return(0);
  117. }
  118.  
  119. void tt_del_eol()
  120. {
  121.     fputs(DEL_EOL_STR, stdout);
  122. }
  123. /*
  124. void tt_open_line(int n)
  125. {
  126.     if (n) fprintf(stdout, ADD_N_LINES_STR, n);
  127. } */
  128.  
  129. void tt_reverse_video()
  130. {
  131.     fputs(REV_VID_STR, stdout);
  132. }
  133.  
  134. void tt_normal_video()
  135. {
  136.     fputs(NORM_VID_STR, stdout);
  137. }
  138.  
  139. void narrow_width()
  140. {
  141.     fputs("\033[?3l",stdout);
  142. }
  143.  
  144. void wide_width()
  145. {
  146.     fputs("\033[?3h",stdout);
  147. }
  148.  
  149. void smart_puts(char *new,char *old, int row)
  150. {
  151.     char out[250], ch, ch1, curs[20];
  152.     int ii,max_len,i,mark, curs_set = 0;
  153.  
  154.     i = 0;
  155.     ii = 0;
  156.     *curs = 0;
  157.  
  158.     /* while they match, go on */
  159.     while(ch = *new++, ch1 = *old++, (ch == ch1) && (ch != '\0')) i++;
  160.  
  161.     if (ch == '\0')
  162.     /* we are at the end of the new, so delete eond of old line */
  163.       {
  164.           if (ch1)
  165.             {
  166.                 old--;
  167.                 while (ch1 = *old++, (ch1 == ' ') && (ch1 != 0));
  168.             }
  169.  
  170.       if (ch1 == 0) return;
  171.  
  172.           goto_rc(row, i + 1);
  173.           tt_del_eol();
  174.           return;
  175.       }
  176.  
  177.     max_len = strlen(CURS_F_STR);
  178.  
  179.     if (i)
  180.       {
  181.      sprintf(curs, CURS_POS_STR, row, i + 1);
  182.      curs_set = 1;
  183.       }
  184.  
  185.     while(1)
  186.       {
  187.           i = 1; ii = 0;
  188.           ch1 = 0;
  189.           out[ii++] = ch;
  190.           while (ch = *new++, ch1 = *old++, ch != ch1 && ch != '\0') out[ii++] = ch;
  191.           mark = ii;
  192.           out[ii++] = ch;
  193.           if (ch != '\0') while (ch = *new++, ch1 = *old++, ch == ch1 && ch != '\0')
  194.             {
  195.                 i++;
  196.                 out[ii++] = ch;
  197.             }
  198.           out[ii] = '\0';
  199.           if (i > max_len)
  200.             {
  201.                 out[mark] = '\0';
  202.                 if (*curs)
  203.                   {
  204.                       fputs(curs,stdout);
  205.                       *curs = 0;
  206.                   }
  207.                 if (!curs_set)
  208.           {
  209.              goto_rc(row, 1);
  210.              curs_set = 1;
  211.           }
  212.  
  213.                 fputs(out,stdout);
  214.                 if (ch == '\0')
  215.                   {
  216.                       if (ch1)
  217.                         {
  218.                             old--;
  219.                 while (ch1 = *old++, (ch1 == ' ') && (ch1 != 0));
  220.                         }
  221.  
  222.               if (ch1 == 0) return;
  223.               if (curs_set) sprintf(curs, CURS_F_STR, i);
  224.               else
  225.             {
  226.                sprintf(curs, CURS_POS_STR, row, i + 1);
  227.                curs_set = 1;
  228.             }
  229.  
  230.               fputs(curs, stdout);
  231.               tt_del_eol();
  232.                       return;
  233.                   }
  234.  
  235.                 if (i)
  236.           {
  237.              if (curs_set) sprintf(curs, CURS_F_STR, i);
  238.              else
  239.                {
  240.               sprintf(curs, CURS_POS_STR, row, i + 1);
  241.               curs_set = 1;
  242.                }
  243.           }
  244.             }
  245.           else
  246.             {
  247.                 if (*curs)
  248.                   {
  249.                       fputs(curs,stdout);
  250.                       *curs = 0;
  251.                   }
  252.                 if (!curs_set)
  253.           {
  254.              goto_rc(row, 1);
  255.              curs_set = 1;
  256.           }
  257.                 fputs(out,stdout);
  258.                 if (ch == '\0')
  259.                   {
  260.                       if (ch1)
  261.                         {
  262.                             old--;
  263.                             while (ch1 = *old++, (ch1 == ' '));
  264.                         }
  265.  
  266.                       if (ch1 != '\0') tt_del_eol();
  267.                       return;
  268.                   }
  269.             }
  270.       }
  271. }
  272.  
  273. #if 0
  274.  
  275. void smart_puts(char *new,char *old)
  276. {
  277.     char out[250], ch, ch1, curs[20];
  278.     int ii,max_len,i,mark;
  279.  
  280.     i = 0;
  281.     ii = 0;
  282.     *curs = 0;
  283.  
  284.     /* while they match, go on */
  285.     while(ch = *new++, ch1 = *old++, (ch == ch1) && (ch != '\0')) i++;
  286.  
  287.     if (ch == '\0')
  288.     /* we are at the end of the new, so delete eond of old line */
  289.       {
  290.           if (ch1)
  291.             {
  292.                 old--;
  293.                 while (ch1 = *old++, (ch1 == ' ') && (ch1 != 0));
  294.             }
  295.  
  296.       if (ch1 == 0) return;
  297.  
  298.           if (i && ch1)  fprintf(stdout, CURS_F_STR, i);
  299.           tt_del_eol();
  300.           return;
  301.       }
  302.  
  303.     max_len = strlen(CURS_F_STR);
  304.     if (i)
  305.       {
  306.           sprintf(curs, CURS_F_STR, i);
  307.       }
  308.  
  309.     while(1)
  310.       {
  311.           i = 1; ii = 0;
  312.           ch1 = 0;
  313.           out[ii++] = ch;
  314.           while (ch = *new++, ch1 = *old++, ch != ch1 && ch != '\0') out[ii++] = ch;
  315.           mark = ii;
  316.           out[ii++] = ch;
  317.           if (ch != '\0') while (ch = *new++, ch1 = *old++, ch == ch1 && ch != '\0')
  318.             {
  319.                 i++;
  320.                 out[ii++] = ch;
  321.             }
  322.           out[ii] = '\0';
  323.           if (i > max_len)
  324.             {
  325.                 out[mark] = '\0';
  326.                 if (*curs)
  327.                   {
  328.                       fputs(curs,stdout);
  329.                       *curs = 0;
  330.                   }
  331.  
  332.                 fputs(out,stdout);
  333.                 if (ch == '\0')
  334.                   {
  335.                       if (ch1)
  336.                         {
  337.                             old--;
  338.                 while (ch1 = *old++, (ch1 == ' ') && (ch1 != 0));
  339.                         }
  340.  
  341.               if (ch1 == 0) return;
  342.               sprintf(curs, CURS_F_STR, i);
  343.               fputs(curs, stdout);
  344.               tt_del_eol();
  345.                       return;
  346.                   }
  347.  
  348.                 if (i) sprintf(curs, CURS_F_STR, i);
  349.             }
  350.           else
  351.             {
  352.                 if (*curs)
  353.                   {
  354.                       fputs(curs,