home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / v / vim_src.zip / LINEFUNC.C < prev    next >
C/C++ Source or Header  |  1993-01-12  |  3KB  |  161 lines

  1. /* vi:ts=4:sw=4
  2.  *
  3.  * VIM - Vi IMitation
  4.  *
  5.  * Code Contributions By:    Bram Moolenaar            mool@oce.nl
  6.  *                            Tim Thompson            twitch!tjt
  7.  *                            Tony Andrews            onecom!wldrdg!tony 
  8.  *                            G. R. (Fred) Walter        watmath!watcgl!grwalter 
  9.  */
  10.  
  11. /*
  12.  * linefunc.c: some functions to move to the next/previous line and
  13.  *               to the next/previous character
  14.  */
  15.  
  16. #include "vim.h"
  17. #include "globals.h"
  18. #include "proto.h"
  19.  
  20. /*
  21.  * coladvance(col)
  22.  *
  23.  * Try to advance the Cursor to the specified column.
  24.  */
  25.  
  26.     void
  27. coladvance(wcol)
  28.     colnr_t         wcol;
  29. {
  30.     int                 index;
  31.     register u_char        *ptr;
  32.     register colnr_t    col;
  33.  
  34.     ptr = (u_char *)nr2ptr(Curpos.lnum);
  35.  
  36.     /* try to advance to the specified column */
  37.     index = -1;
  38.     col = 0;
  39.     while (col <= wcol && *ptr)
  40.     {
  41.         ++index;
  42.         /* Count a tab for what it's worth (if list mode not on) */
  43.         col += chartabsize(*ptr, col);
  44.         ++ptr;
  45.     }
  46.     if (index < 0)
  47.         Curpos.col = 0;
  48.     else
  49.         Curpos.col = index;
  50. }
  51.  
  52. /*
  53.  * inc(p)
  54.  *
  55.  * Increment the line pointer 'p' crossing line boundaries as necessary. Return
  56.  * 1 when crossing a line, -1 when at end of file, 0 otherwise.
  57.  */
  58.     int
  59. inc(lp)
  60.     register FPOS  *lp;
  61. {
  62.     register char  *p = pos2ptr(lp);
  63.  
  64.     if (*p != NUL)
  65.     {            /* still within line */
  66.         lp->col++;
  67.         return ((p[1] != NUL) ? 0 : 1);
  68.     }
  69.     if (lp->lnum != line_count)
  70.     {            /* there is a next line */
  71.         lp->col = 0;
  72.         lp->lnum++;
  73.         return 1;
  74.     }
  75.     return -1;
  76. }
  77.  
  78.     int
  79. incCurpos()
  80. {
  81.     return inc(&Curpos);
  82. }
  83.  
  84. /*
  85.  * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
  86.  */
  87.     int
  88. incl(lp)
  89.         register FPOS *lp;
  90. {
  91.         register int r;
  92.  
  93.         if ((r = inc(lp)) == 1 && lp->col)
  94.                 r = inc(lp);
  95.         return r;
  96. }
  97.  
  98. /*
  99.  * dec(p)
  100.  *
  101.  * Decrement the line pointer 'p' crossing line boundaries as necessary. Return
  102.  * 1 when crossing a line, -1 when at start of file, 0 otherwise.
  103.  */
  104.     int
  105. dec(lp)
  106.     register FPOS  *lp;
  107. {
  108.     if (lp->col > 0)
  109.     {            /* still within line */
  110.         lp->col--;
  111.         return 0;
  112.     }
  113.     if (lp->lnum > 1)
  114.     {            /* there is a prior line */
  115.         lp->lnum--;
  116.         lp->col = strlen(nr2ptr(lp->lnum));
  117.         return 1;
  118.     }
  119.     return -1;                    /* at start of file */
  120. }
  121.  
  122.     int
  123. decCurpos()
  124. {
  125.     return dec(&Curpos);
  126. }
  127.  
  128. /*
  129.  * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
  130.  */
  131.     int
  132. decl(lp)
  133.         register FPOS *lp;
  134. {
  135.         register int r;
  136.  
  137.         if ((r = dec(lp)) == 1 && lp->col)
  138.                 r = dec(lp);
  139.         return r;
  140. }
  141.  
  142. /*
  143.  * make sure Curpos in on a valid character
  144.  */
  145.     void
  146. adjustCurpos()
  147. {
  148.     int len;
  149.  
  150.     if (Curpos.lnum == 0)
  151.         Curpos.lnum = 1;
  152.     if (Curpos.lnum > line_count)
  153.         Curpos.lnum = line_count;
  154.  
  155.     len = strlen(nr2ptr(Curpos.lnum));
  156.     if (len == 0)
  157.         Curpos.col = 0;
  158.     else if (Curpos.col >= len)
  159.         Curpos.col = len - 1;
  160. }
  161.