home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1708 / misc.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.9 KB  |  140 lines

  1. /* misc.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    16820 SW Tallac Way
  6.  *    Beaverton, OR 97006
  7.  *    kirkenda@jove.cs.pdx.edu, or ...uunet!tektronix!psueea!jove!kirkenda
  8.  */
  9.  
  10.  
  11. /* This file contains functions which didn't seem happy anywhere else */
  12.  
  13. #include "config.h"
  14. #include "vi.h"
  15.  
  16.  
  17. /* find a particular line & return a pointer to a copy of its text */
  18. char *fetchline(line)
  19.     long    line;    /* line number of the line to fetch */
  20. {
  21.     int        i;
  22.     register char    *scan;    /* used to search for the line in a BLK */
  23.     long        l;    /* line number counter */
  24.     static BLK    buf;    /* holds ONLY the selected line (as string) */
  25.     register char    *cpy;    /* used while copying the line */
  26.     static long    nextline;    /* }  These four variables are used */
  27.     static long    chglevel;    /*  } to implement a shortcut when  */
  28.     static char    *nextscan;    /*  } consecutive lines are fetched */
  29.     static long    nextlnum;    /* }                                */
  30.  
  31.     /* can we do a shortcut? */
  32.     if (changes == chglevel && line == nextline)
  33.     {
  34.         scan = nextscan;
  35.     }
  36.     else
  37.     {
  38.         /* scan lnum[] to determine which block its in */
  39.         for (i = 1; line > lnum[i]; i++)
  40.         {
  41.         }
  42.         nextlnum = lnum[i];
  43.  
  44.         /* fetch text of the block containing that line */
  45.         scan = blkget(i)->c;
  46.  
  47.         /* find the line in the block */
  48.         for (l = lnum[i - 1]; ++l < line; )
  49.         {
  50.             while (*scan++ != '\n')
  51.             {
  52.             }
  53.         }
  54.     }
  55.  
  56.     /* copy it into a block by itself, with no newline */
  57.     for (cpy = buf.c; *scan != '\n'; )
  58.     {
  59.         *cpy++ = *scan++;
  60.     }
  61.     *cpy = '\0';
  62.  
  63.     /* maybe speed up the next call to fetchline() ? */
  64.     if (line < nextlnum)
  65.     {
  66.         nextline = line + 1;
  67.         chglevel = changes;
  68.         nextscan = scan + 1;
  69.     }
  70.     else
  71.     {
  72.         nextline = 0;
  73.     }
  74.  
  75.     /* Calls to fetchline() interfere with calls to pfetch().  Make sure
  76.      * that pfetch() resets itself on its next invocation.
  77.      */
  78.     pchgs = 0L;
  79.  
  80.     /* Return a pointer to the line's text */
  81.     return buf.c;
  82. }
  83.  
  84. /* find a particular line & delete it */
  85. deleteline(line)
  86.     long    line;    /* line number of the line to fetch */
  87. {
  88.     MARK    frommark, tomark;
  89.  
  90.     frommark = MARK_AT_LINE(line);
  91.     tomark = frommark + BLKSIZE;
  92.     delete(frommark, tomark);
  93. }
  94.  
  95.  
  96. /* insert a given line at a particular line number */
  97. addline(l, txt)
  98.     long    l;    /* line number where the new line should go */
  99.     char    *txt;    /* text of line, terminated with '\0' (not '\n') */
  100. {
  101.     MARK    atmark;
  102.     BLK    newtext;
  103.  
  104.     strcpy(newtext.c, txt);
  105.     strcat(newtext.c, "\n");
  106.     atmark = MARK_AT_LINE(l);
  107.     add(atmark, newtext.c);
  108. }
  109.  
  110.  
  111. /* replace one version of a line with another */
  112. changeline(l, txt)
  113.     long    l;    /* line# of line to change */
  114.     char    *txt;    /* new version of line, terminated with '\0' */
  115. {
  116.     deleteline(l);
  117.     addline(l, txt);
  118. }
  119.  
  120.  
  121. /* error message from the regexp code */
  122. void regerror(txt)
  123.     char    *txt;    /* an error message */
  124. {
  125.     msg("RE error: %s", txt);
  126. }
  127.  
  128. /* This function is equivelent to the pfetch() macro */
  129. void    pfetch(l)
  130.     long    l;    /* line number of line to fetch */
  131. {
  132.     if(l != pline || changes != pchgs)
  133.     {
  134.         pline = (l);
  135.         ptext = fetchline(pline);
  136.         plen = strlen(ptext);
  137.         pchgs = changes;
  138.     }
  139. }
  140.