home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / less / line.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-27  |  10.8 KB  |  534 lines

  1. /*
  2.  * Routines to manipulate the "line buffer".
  3.  * The line buffer holds a line of output as it is being built
  4.  * in preparation for output to the screen.
  5.  */
  6.  
  7. #include "less.h"
  8.  
  9. static char linebuf[1024];    /* Buffer which holds the current output line */
  10. static char attr[1024];        /* Extension of linebuf to hold attributes */
  11. static int curr;        /* Index into linebuf */
  12. static int column;        /* Printable length, accounting for
  13.                    backspaces, etc. */
  14. static int overstrike;        /* Next char should overstrike previous char */
  15. static int is_null_line;    /* There is no current line */
  16. static char pendc;
  17.  
  18. static int do_append();
  19.  
  20. extern int bs_mode;
  21. extern int tabstop;
  22. extern int linenums;
  23. extern int ctldisp;
  24. extern int twiddle;
  25. extern int binattr;
  26. extern int auto_wrap, ignaw;
  27. extern int bo_s_width, bo_e_width;
  28. extern int ul_s_width, ul_e_width;
  29. extern int bl_s_width, bl_e_width;
  30. extern int sc_width, sc_height;
  31.  
  32. #ifdef OS2
  33. static int do_append();
  34. #endif
  35.  
  36. /*
  37.  * Rewind the line buffer.
  38.  */
  39.     public void
  40. prewind()
  41. {
  42.     curr = 0;
  43.     column = 0;
  44.     overstrike = 0;
  45.     is_null_line = 0;
  46.     pendc = '\0';
  47. }
  48.  
  49. /*
  50.  * Insert the line number (of the given position) into the line buffer.
  51.  */
  52.     public void
  53. plinenum(pos)
  54.     POSITION pos;
  55. {
  56.     register int lno;
  57.     register int i;
  58.     register int n;
  59.  
  60.     /*
  61.      * We display the line number at the start of each line
  62.      * only if the -N option is set.
  63.      */
  64.     if (linenums != 2)
  65.         return;
  66.  
  67.     /*
  68.      * Get the line number and put it in the current line.
  69.      * {{ Note: since find_linenum calls forw_raw_line,
  70.      *    it may seek in the input file, requiring the caller
  71.      *    of plinenum to re-seek if necessary. }}
  72.      */
  73.     lno = find_linenum(pos);
  74.  
  75.     sprintf(&linebuf[curr], "%6d", lno);
  76.     n = strlen(&linebuf[curr]);
  77.     column += n;
  78.     for (i = 0;  i < n;  i++)
  79.         attr[curr++] = 0;
  80.  
  81.     /*
  82.      * Append enough spaces to bring us to the next tab stop.
  83.      * {{ We could avoid this at the cost of adding some
  84.      *    complication to the tab stop logic in pappend(). }}
  85.      */
  86.     do
  87.     {
  88.         linebuf[curr] = ' ';
  89.         attr[curr++] = 0;
  90.         column++;
  91.     } while ((column % tabstop) != 0);
  92. }
  93.  
  94. /*
  95.  * Return the printing width of the start (enter) sequence
  96.  * for a given character attribute.
  97.  */
  98.     int
  99. attr_swidth(a)
  100.     int a;
  101. {
  102.     switch (a)
  103.     {
  104.     case BOLD:    return (bo_s_width);
  105.     case UNDERLINE:    return (ul_s_width);
  106.     case BLINK:    return (bl_s_width);
  107.     }
  108.     return (0);
  109. }
  110.  
  111. /*
  112.  * Return the printing width of the end (exit) sequence
  113.  * for a given character attribute.
  114.  */
  115.     int
  116. attr_ewidth(a)
  117.     int a;
  118. {
  119.     switch (a)
  120.     {
  121.     case BOLD:    return (bo_e_width);
  122.     case UNDERLINE:    return (ul_e_width);
  123.     case BLINK:    return (bl_e_width);
  124.     }
  125.     return (0);
  126. }
  127.  
  128. /*
  129.  * Return the printing width of a given character and attribute,
  130.  * if the character were added to the current position in the line buffer.
  131.  * Adding a character with a given attribute may cause an enter or exit
  132.  * attribute sequence to be inserted, so this must be taken into account.
  133.  */
  134.     static int
  135. pwidth(c, a)
  136.     int c;
  137.     int a;
  138. {
  139.     register int w;
  140.  
  141.     if (c == '\b')
  142.         /*
  143.          * Backspace moves backwards one position.
  144.          */
  145.         return (-1);
  146.  
  147.     if (control_char(c))
  148.         /*
  149.          * Control characters do unpredicatable things,
  150.          * so we don't even try to guess; say it doesn't move.
  151.          * This can only happen if the -r flag is in effect.
  152.          */
  153.         return (0);
  154.  
  155.     /*
  156.      * Other characters take one space,
  157.      * plus the width of any attribute enter/exit sequence.
  158.      */
  159.     w = 1;
  160.     if (curr > 0 && attr[curr-1] != a)
  161.         w += attr_ewidth(attr[curr-1]);
  162.     if (a && (curr == 0 || attr[curr-1] != a))
  163.         w += attr_swidth(a);
  164.     return (w);
  165. }
  166.  
  167. /*
  168.  * Delete the previous character in the line buffer.
  169.  */
  170.     static void
  171. backc()
  172. {
  173.     curr--;
  174.     column -= pwidth(linebuf[curr], attr[curr]);
  175. }
  176.  
  177. /*
  178.  * Append a character and attribute to the line buffer.
  179.  */
  180.     static int
  181. storec(c, a)
  182.     int c;
  183.     int a;
  184. {
  185.     register int w;
  186.  
  187.     w = pwidth(c, a);
  188.     if (ctldisp > 0 && column + w + attr_ewidth(a) > sc_width)
  189.         /*
  190.          * Won't fit on screen.
  191.          */
  192.         return (1);
  193.  
  194.     if (curr >= sizeof(linebuf)-2)
  195.         /*
  196.          * Won't fit in line buffer.
  197.          */
  198.         return (1);
  199.  
  200.     /*
  201.      * Special handling for "magic cookie" terminals.
  202.      * If an attribute enter/exit sequence has a printing width > 0,
  203.      * and the sequence is adjacent to a space, delete the space.
  204.      * We just mark the space as invisible, to avoid having too
  205.      * many spaces deleted.
  206.      * {{ Note that even if the attribute width is > 1, we
  207.      *    delete only one space.  It's not worth trying to do more.
  208.      *    It's hardly worth doing this much. }}
  209.      */
  210.     if (curr > 0 && a != NORMAL &&
  211.         linebuf[curr-1] == ' ' && attr[curr-1] == NORMAL &&
  212.         attr_swidth(a) > 0)
  213.     {
  214.         /*
  215.          * We are about to append an enter-attribute sequence
  216.          * just after a space.  Delete the space.
  217.          */
  218.         attr[curr-1] = INVIS;
  219.         column--;
  220.     } else if (curr > 0 && attr[curr-1] != NORMAL &&
  221.         attr[curr-1] != INVIS && c == ' ' && a == NORMAL &&
  222.         attr_ewidth(attr[curr-1]) > 0)
  223.     {
  224.         /*
  225.          * We are about to append a space just after an
  226.          * exit-attribute sequence.  Delete the space.
  227.          */
  228.         a = INVIS;
  229.         column--;
  230.     }
  231.     /* End of magic cookie handling. */
  232.  
  233.     linebuf[curr] = c;
  234.     attr[curr] = a;
  235.     column += w;
  236.     return (0);
  237. }
  238.  
  239. /*
  240.  * Append a character to the line buffer.
  241.  * Expand tabs into spaces, handle underlining, boldfacing, etc.
  242.  * Returns 0 if ok, 1 if couldn't fit in buffer.
  243.  */
  244.     public int
  245. pappend(c)
  246.     register int c;
  247. {
  248.     if (pendc)
  249.     {
  250.         if (do_append(pendc))
  251.             /*
  252.              * Oops.  We've probably lost the char which
  253.              * was in pendc, since caller won't back up.
  254.              */
  255.             return (1);
  256.         pendc = '\0';
  257.     }
  258.  
  259.     if (c == '\r' && bs_mode == BS_SPECIAL)
  260.     {
  261.         /*
  262.          * Don't put the CR into the buffer until we see
  263.          * the next char.  If the next char is a newline,
  264.          * discard the CR.
  265.          */
  266.         pendc = c;
  267.         return (0);
  268.     }
  269.  
  270.     return (do_append(c));
  271. }
  272.  
  273.     static int
  274. do_append(c)
  275.     int c;
  276. {
  277.     register char *s;
  278.     register int a;
  279.  
  280. #define    STOREC(c,a)    if (storec((c),(a))) return (1); else curr++
  281.  
  282.     if (overstrike)
  283.     {
  284.         /*
  285.          * Overstrike the character at the current position
  286.          * in the line buffer.  This will cause either
  287.          * underline (if a "_" is overstruck),
  288.          * bold (if an identical character is overstruck),
  289.          * or just deletion of the character in the buffer.
  290.          */
  291.         overstrike = 0;
  292.         if (c == linebuf[curr])
  293.             STOREC(linebuf[curr], BOLD);
  294.         else if (c == '_')
  295.             STOREC(linebuf[curr], UNDERLINE);
  296.         else if (linebuf[curr] == '_')
  297.             STOREC(c, UNDERLINE);
  298.         else if (control_char(c))
  299.             goto do_control_char;
  300.         else
  301.             STOREC(c, NORMAL);
  302.     } else if (c == '\b')
  303.     {
  304.         switch (bs_mode)
  305.         {
  306.         case BS_NORMAL:
  307.             STOREC(c, NORMAL);
  308.             break;
  309.         case BS_CONTROL:
  310.             goto do_control_char;
  311.         case BS_SPECIAL:
  312.             if (curr == 0)
  313.                 break;
  314.             backc();
  315.             overstrike = 1;
  316.             break;
  317.         }
  318.     } else if (c == '\t')
  319.     {
  320.         /*
  321.          * Expand a tab into spaces.
  322.          */
  323.         do
  324.         {
  325.             STOREC(' ', NORMAL);
  326.         } while ((column % tabstop) != 0);
  327.     }
  328.         /* else if (c == '\r')
  329.           return(0); */
  330.         else if (control_char(c))
  331.     {
  332.     do_control_char:
  333.         if (ctldisp == 0)
  334.         {
  335.             /*
  336.              * Output as a normal character.
  337.              */
  338.             STOREC(c, NORMAL);
  339.         } else
  340.         {
  341.             /*
  342.              * Output in the (blinking) ^X format.
  343.              */
  344.             s = prchar(c);
  345.             a = binattr;
  346.  
  347.             /*
  348.              * Make sure we can get the entire representation
  349.              * the character on this line.
  350.              */
  351.             if (column + strlen(s) +
  352.                 attr_swidth(a) + attr_ewidth(a) > sc_width)
  353.                 return (1);
  354.  
  355.             for ( ;  *s != 0;  s++)
  356.                 STOREC(*s, a);
  357.         }
  358.     } else
  359.     {
  360.         STOREC(c, NORMAL);
  361.     }
  362.  
  363.     return (0);
  364. }
  365.  
  366. /*
  367.  * Terminate the line in the line buffer.
  368.  */
  369.     public void
  370. pdone(endline)
  371.     int endline;
  372. {
  373.     if (pendc && (pendc != '\r' || !endline))
  374.         /*
  375.          * If we had a pending character, put it in the buffer.
  376.          * But discard a pending CR if we are at end of line
  377.          * (that is, discard the CR in a CR/LF sequence).
  378.          */
  379.         (void) do_append(pendc);
  380.  
  381.     /*
  382.      * Add a newline if necessary,
  383.      * and append a '\0' to the end of the line.
  384.      */
  385.     if (column < sc_width || !auto_wrap || ignaw || ctldisp == 0)
  386.     {
  387.         linebuf[curr] = '\n';
  388.         attr[curr] = NORMAL;
  389.         curr++;
  390.     }
  391.     linebuf[curr] = '\0';
  392.     attr[curr] = NORMAL;
  393. }
  394.  
  395. /*
  396.  * Get a character from the current line.
  397.  * Return the character as the function return value,
  398.  * and the character attribute in *ap.
  399.  */
  400.     public int
  401. gline(i, ap)
  402.     register int i;
  403.     register int *ap;
  404. {
  405.     if (is_null_line)
  406.     {
  407.         /*
  408.          * If there is no current line, we pretend the line is
  409.          * either "~" or "", depending on the "twiddle" flag.
  410.          */
  411.         *ap = NORMAL;
  412.         if (twiddle)
  413.             return ("~\n"[i]);
  414.         return ("\n"[i]);
  415.     }
  416.  
  417.     *ap = attr[i];
  418.     return (linebuf[i] & 0377);
  419. }
  420.  
  421. /*
  422.  * Indicate that there is no current line.
  423.  */
  424.     public void
  425. null_line()
  426. {
  427.     is_null_line = 1;
  428. }
  429.  
  430. /*
  431.  * Analogous to forw_line(), but deals with "raw lines":
  432.  * lines which are not split for screen width.
  433.  * {{ This is supposed to be more efficient than forw_line(). }}
  434.  */
  435.     public POSITION
  436. forw_raw_line(curr_pos, linep)
  437.     POSITION curr_pos;
  438.     char **linep;
  439. {
  440.     register char *p;
  441.     register int c;
  442.     POSITION new_pos;
  443.  
  444.     if (curr_pos == NULL_POSITION || ch_seek(curr_pos) ||
  445.         (c = ch_forw_get()) == EOI)
  446.         return (NULL_POSITION);
  447.  
  448.     p = linebuf;
  449.  
  450.     for (;;)
  451.     {
  452.         if (c == '\n' || c == EOI)
  453.         {
  454.             new_pos = ch_tell();
  455.             break;
  456.         }
  457.         if (p >= &linebuf[sizeof(linebuf)-1])
  458.         {
  459.             /*
  460.              * Overflowed the input buffer.
  461.              * Pretend the line ended here.
  462.              * {{ The line buffer is supposed to be big
  463.              *    enough that this never happens. }}
  464.              */
  465.             new_pos = ch_tell() - 1;
  466.             break;
  467.         }
  468.         *p++ = c;
  469.         c = ch_forw_get();
  470.     }
  471.     *p = '\0';
  472.     if (linep != NULL)
  473.         *linep = linebuf;
  474.     return (new_pos);
  475. }
  476.  
  477. /*
  478.  * Analogous to back_line(), but deals with "raw lines".
  479.  * {{ This is supposed to be more efficient than back_line(). }}
  480.  */
  481.     public POSITION
  482. back_raw_line(curr_pos, linep)
  483.     POSITION curr_pos;
  484.     char **linep;
  485. {
  486.     register char *p;
  487.     register int c;
  488.     POSITION new_pos;
  489.  
  490.     if (curr_pos == NULL_POSITION || curr_pos <= ch_zero() ||
  491.         ch_seek(curr_pos-1))
  492.         return (NULL_POSITION);
  493.  
  494.     p = &linebuf[sizeof(linebuf)];
  495.     *--p = '\0';
  496.  
  497.     for (;;)
  498.     {
  499.         c = ch_back_get();
  500.         if (c == '\n')
  501.         {
  502.             /*
  503.              * This is the newline ending the previous line.
  504.              * We have hit the beginning of the line.
  505.              */
  506.             new_pos = ch_tell() + 1;
  507.             break;
  508.         }
  509.         if (c == EOI)
  510.         {
  511.             /*
  512.              * We have hit the beginning of the file.
  513.              * This must be the first line in the file.
  514.              * This must, of course, be the beginning of the line.
  515.              */
  516.             new_pos = ch_zero();
  517.             break;
  518.         }
  519.         if (p <= linebuf)
  520.         {
  521.             /*
  522.              * Overflowed the input buffer.
  523.              * Pretend the line ended here.
  524.              */
  525.             new_pos = ch_tell() + 1;
  526.             break;
  527.         }
  528.         *--p = c;
  529.     }
  530.     if (linep != NULL)
  531.         *linep = p;
  532.     return (new_pos);
  533. }
  534.