home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / STVI369G.ZIP / LINEFUNC.C < prev    next >
C/C++ Source or Header  |  1990-05-01  |  3KB  |  147 lines

  1. /* $Header: /nw/tony/src/stevie/src/RCS/linefunc.c,v 1.2 89/03/11 22:42:32 tony Exp $
  2.  *
  3.  * Basic line-oriented motions.
  4.  */
  5.  
  6. #include "stevie.h"
  7. #include "ops.h"
  8.  
  9. /*
  10.  * nextline(curr)
  11.  *
  12.  * Return a pointer to the beginning of the next line after the one
  13.  * referenced by 'curr'. Return NULL if there is no next line (at EOF).
  14.  */
  15.  
  16. LPTR *
  17. nextline(curr)
  18. LPTR    *curr;
  19. {
  20.     static    LPTR    next;
  21.  
  22.     if (curr->linep->next != Fileend->linep) {
  23.         next.index = 0;
  24.         next.linep = curr->linep->next;
  25.         return &next;
  26.     }
  27.     return (LPTR *) NULL;
  28. }
  29.  
  30. /*
  31.  * prevline(curr)
  32.  *
  33.  * Return a pointer to the beginning of the line before the one
  34.  * referenced by 'curr'. Return NULL if there is no prior line.
  35.  */
  36.  
  37. LPTR *
  38. prevline(curr)
  39. LPTR    *curr;
  40. {
  41.     static    LPTR    prev;
  42.  
  43.     if (curr->linep->prev != Filetop->linep) {
  44.         prev.index = 0;
  45.         prev.linep = curr->linep->prev;
  46.         return &prev;
  47.     }
  48.     return (LPTR *) NULL;
  49. }
  50.  
  51. /*
  52.  * coladvance(p,col)
  53.  *
  54.  * Try to advance to the specified column, starting at p.
  55.  */
  56.  
  57. LPTR *
  58. coladvance(p, col)
  59. LPTR    *p;
  60. register int    col;
  61. {
  62.     static    LPTR    lp;
  63.     register int    c, in;
  64.  
  65.     lp.linep = p->linep;
  66.     lp.index = p->index;
  67.  
  68.     /* If we're on a blank ('\n' only) line, we can't do anything */
  69.     if (lp.linep->s[lp.index] == '\0')
  70.         return &lp;
  71.     /* try to advance to the specified column */
  72.     for ( c=0; col-- > 0; c++ ) {
  73.         /* Count a tab for what it's worth (if list mode not on) */
  74.         if ( gchar(&lp) == TAB && !P(P_LS) ) {
  75.             in = ((P(P_TS)-1) - c%P(P_TS));
  76.             col -= in;
  77.             c += in;
  78.         }
  79.         /* Don't go past the end of */
  80.         /* the file or the line. */
  81.         if (inc(&lp)) {
  82.             dec(&lp);
  83.             break;
  84.         }
  85.     }
  86.     return &lp;
  87. }
  88.  
  89.  
  90. /*
  91.  * nextchar(curr)
  92.  *
  93.  * Return a line pointer to the next character after the
  94.  * one referenced by 'curr'. Return NULL if there is no next one (at EOF).
  95.  * NOTE: this COULD point to a \n or \0 character.
  96.  */
  97.  
  98. LPTR *
  99. nextchar(curr)
  100. LPTR    *curr;
  101. {
  102.     static    LPTR    *next;
  103.     char    c;
  104.  
  105.     next = curr;
  106.     c = CHAR( next );
  107.     if (c=='\n' || c=='\0')        /* end of line */
  108.         next = nextline (next);
  109.     else
  110.         next->index++;
  111.  
  112.     return (next);
  113. }
  114.  
  115.  
  116. /*
  117.  * prevchar(curr)
  118.  *
  119.  * Return a line pointer to the previous character before the
  120.  * one referenced by 'curr'. Return NULL if there is no previous one.
  121.  * Note: this COULD point to a \n or \0 character.
  122.  */
  123.  
  124. LPTR *
  125. prevchar(curr)
  126. LPTR    *curr;
  127. {
  128.     static    LPTR    *prev;
  129.     char    c;
  130.  
  131.     prev = curr;
  132.     if (prev->index == 0) {        /* beginning of line */
  133.         prev = prevline (prev);        /* jump back */
  134.         c = CHAR( prev );
  135.         while (c!='\n' && c!= '\0') {    /* go to end of line */
  136.             prev->index++;
  137.             c = CHAR( prev );
  138.         }
  139.     }
  140.     else
  141.         prev->index--;
  142.  
  143.     return (prev);
  144. }
  145.  
  146.  
  147.