home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / elvis / Source / c / misc < prev    next >
Encoding:
Text File  |  1989-12-31  |  2.9 KB  |  141 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 "vi.h"
  14.  
  15.  
  16. /* find a particular line & return a pointer to a copy of its text */
  17. char *fetchline(line)
  18.     long    line;    /* line number of the line to fetch */
  19. {
  20.     int        i;
  21.     register char    *scan;    /* used to search for the line in a BLK */
  22.     long        l;    /* line number counter */
  23.     static BLK    buf;    /* holds ONLY the selected line (as string) */
  24.     register char    *cpy;    /* used while copying the line */
  25.     static long    nextline;    /* }  These four variables are used */
  26.     static long    chglevel;    /*  } to implement a shortcut when  */
  27.     static char    *nextscan;    /*  } consecutive lines are fetched */
  28.     static long    nextlnum;    /* }                                */
  29.  
  30.     /* can we do a shortcut? */
  31.     if (changes == chglevel && line == nextline)
  32.     {
  33.         scan = nextscan;
  34.     }
  35.     else
  36.     {
  37.         /* scan lnum[] to determine which block its in */
  38.         for (i = 1; line > lnum[i]; i++)
  39.         {
  40.         }
  41.         nextlnum = lnum[i];
  42.  
  43.         /* fetch text of the block containing that line */
  44.         scan = blkget(i)->c;
  45.  
  46.         /* find the line in the block */
  47.         for (l = lnum[i - 1]; ++l < line; )
  48.         {
  49.             while (*scan++ != '\n')
  50.             {
  51.             }
  52.         }
  53.     }
  54.  
  55.     /* copy it into a block by itself, with no newline */
  56.     for (cpy = buf.c; *scan != '\n'; )
  57.     {
  58.         *cpy++ = *scan++;
  59.     }
  60.     *cpy = '\0';
  61.  
  62.     /* maybe speed up the next call to fetchline() ? */
  63.     if (line < nextlnum)
  64.     {
  65.         nextline = line + 1;
  66.         chglevel = changes;
  67.         nextscan = scan + 1;
  68.     }
  69.     else
  70.     {
  71.         nextline = 0;
  72.     }
  73.  
  74.     /* Calls to fetchline() interfere with calls to pfetch().  Make sure
  75.      * that pfetch() resets itself on its next invocation.
  76.      */
  77.     pchgs = 0L;
  78.  
  79.     /* Return a pointer to the line's text */
  80.     return buf.c;
  81. }
  82.  
  83. /* find a particular line & delete it */
  84. deleteline(line)
  85.     long    line;    /* line number of the line to fetch */
  86. {
  87.     MARK    frommark, tomark;
  88.  
  89.     frommark = MARK_AT_LINE(line);
  90.     tomark = frommark + BLKSIZE;
  91.     delete(frommark, tomark);
  92. }
  93.  
  94.  
  95. /* insert a given line at a particular line number */
  96. addline(l, txt)
  97.     long    l;    /* line number where the new line should go */
  98.     char    *txt;    /* text of line, terminated with '\0' (not '\n') */
  99. {
  100.     MARK    atmark;
  101.     BLK    newtext;
  102.  
  103.     strcpy(newtext.c, txt);
  104.     strcat(newtext.c, "\n");
  105.     atmark = MARK_AT_LINE(l);
  106.     add(atmark, newtext.c);
  107. }
  108.  
  109.  
  110. /* replace one version of a line with another */
  111. changeline(l, txt)
  112.     long    l;    /* line# of line to change */
  113.     char    *txt;    /* new version of line, terminated with '\0' */
  114. {
  115.     deleteline(l);
  116.     addline(l, txt);
  117. }
  118.  
  119.  
  120. /* error message from the regexp code */
  121. void regerror(txt)
  122.     char    *txt;    /* an error message */
  123. {
  124.     msg("RE error: %s", txt);
  125. }
  126.  
  127. #ifdef CRUNCH
  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. #endif
  141.