home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 21 / emacsrc / basic.c next >
Encoding:
C/C++ Source or Header  |  1986-05-14  |  8.7 KB  |  285 lines

  1. /*
  2.  * The routines in this file move the cursor around on the screen. They
  3.  * compute a new value for the cursor, then adjust ".". The display code
  4.  * always updates the cursor location, so only moves between lines, or
  5.  * functions that adjust the top line in the window and invalidate the
  6.  * framing, are hard.
  7.  */
  8. #include        "ed.h"
  9.  
  10. /*
  11.  * Move the cursor to the
  12.  * beginning of the current line.
  13.  * Trivial.
  14.  */
  15. gotobol(f, n)
  16. {
  17.         curwp->w_doto  = 0;
  18.         return (TRUE);
  19. }
  20.  
  21. /*
  22.  * Move the cursor backwards by "n" characters. If "n" is less than zero call
  23.  * "forwchar" to actually do the move. Otherwise compute the new cursor
  24.  * location. Error if you try and move out of the buffer. Set the flag if the
  25.  * line pointer for dot changes.
  26.  */
  27. backchar(f, n)
  28. register int    n;
  29. {
  30.         register LINE   *lp;
  31.  
  32.         if (n < 0)
  33.                 return (forwchar(f, -n));
  34.         while (n--) {
  35.                 if (curwp->w_doto == 0) {
  36.                         if ((lp=lback(curwp->w_dotp)) == curbp->b_linep)
  37.                                 return (FALSE);
  38.                         curwp->w_dotp  = lp;
  39.                         curwp->w_doto  = llength(lp);
  40.                         curwp->w_flag |= WFMOVE;
  41.                 } else
  42.                         curwp->w_doto--;
  43.         }
  44.         return (TRUE);
  45. }
  46.  
  47. /*
  48.  * Move the cursor to the end of the current line. Trivial. No errors.
  49.  */
  50. gotoeol(f, n)
  51. {
  52.         curwp->w_doto  = llength(curwp->w_dotp);
  53.         return (TRUE);
  54. }
  55.  
  56. /*
  57.  * Move the cursor forwwards by "n" characters. If "n" is less than zero call
  58.  * "backchar" to actually do the move. Otherwise compute the new cursor
  59.  * location, and move ".". Error if you try and move off the end of the
  60.  * buffer. Set the flag if the line pointer for dot changes.
  61.  */
  62. forwchar(f, n)
  63. register int    n;
  64. {
  65.         if (n < 0)
  66.                 return (backchar(f, -n));
  67.         while (n--) {
  68.                 if (curwp->w_doto == llength(curwp->w_dotp)) {
  69.                         if (curwp->w_dotp == curbp->b_linep)
  70.                                 return (FALSE);
  71.                         curwp->w_dotp  = lforw(curwp->w_dotp);
  72.                         curwp->w_doto  = 0;
  73.                         curwp->w_flag |= WFMOVE;
  74.                 } else
  75.                         curwp->w_doto++;
  76.         }
  77.         return (TRUE);
  78. }
  79.  
  80. /*
  81.  * Goto the beginning of the buffer. Massive adjustment of dot. This is
  82.  * considered to be hard motion; it really isn't if the original value of dot
  83.  * is the same as the new value of dot. Normally bound to "M-<".
  84.  */
  85. gotobob(f, n)
  86. {
  87.         curwp->w_dotp  = lforw(curbp->b_linep);
  88.         curwp->w_doto  = 0;
  89.         curwp->w_flag |= WFHARD;
  90.         return (TRUE);
  91. }
  92.  
  93. /*
  94.  * Move to the end of the buffer. Dot is always put at the end of the file
  95.  * (ZJ). The standard screen code does most of the hard parts of update.
  96.  * Bound to "M->".
  97.  */
  98. gotoeob(f, n)
  99. {
  100.         curwp->w_dotp  = curbp->b_linep;
  101.         curwp->w_doto  = 0;
  102.         curwp->w_flag |= WFHARD;
  103.         return (TRUE);
  104. }
  105.  
  106. /*
  107.  * Move forward by full lines. If the number of lines to move is less than
  108.  * zero, call the backward line function to actually do it. The last command
  109.  * controls how the goal column is set. Bound to "C-N". No errors are
  110.  * possible.
  111.  */
  112. forwline(f, n)
  113. {
  114.         register LINE   *dlp;
  115.  
  116.         if (n < 0)
  117.                 return (backline(f, -n));
  118.         if ((lastflag&CFCPCN) == 0)             /* Reset goal if last   */
  119.                 curgoal = curcol;               /* not C-P or C-N       */
  120.         thisflag |= CFCPCN;
  121.         dlp = curwp->w_dotp;
  122.         while (n-- && dlp!=curbp->b_linep)
  123.                 dlp = lforw(dlp);
  124.         curwp->w_dotp  = dlp;
  125.         curwp->w_doto  = getgoal(dlp);
  126.         curwp->w_flag |= WFMOVE;
  127.         return (TRUE);
  128. }
  129.  
  130. /*
  131.  * This function is like "forwline", but goes backwards. The scheme is exactly
  132.  * the same. Check for arguments that are less than zero and call your
  133.  * alternate. Figure out the new line and call "movedot" to perform the
  134.  * motion. No errors are possible. Bound to "C-P".
  135.  */
  136. backline(f, n)
  137. {
  138.         register LINE   *dlp;
  139.  
  140.         if (n < 0)
  141.                 return (forwline(f, -n));
  142.         if ((lastflag&CFCPCN) == 0)             /* Reset goal if the    */
  143.                 curgoal = curcol;               /* last isn't C-P, C-N  */
  144.         thisflag |= CFCPCN;
  145.         dlp = curwp->w_dotp;
  146.         while (n-- && lback(dlp)!=curbp->b_linep)
  147.                 dlp = lback(dlp);
  148.         curwp->w_dotp  = dlp;
  149.         curwp->w_doto  = getgoal(dlp);
  150.         curwp->w_flag |= WFMOVE;
  151.         return (TRUE);
  152. }
  153.  
  154. /*
  155.  * This routine, given a pointer to a LINE, and the current cursor goal
  156.  * column, return the best choice for the offset. The offset is returned.
  157.  * Used by "C-N" and "C-P".
  158.  */
  159. getgoal(dlp)
  160. register LINE   *dlp;
  161. {
  162.         register int    c;
  163.         register int    col;
  164.         register int    newcol;
  165.         register int    dbo;
  166.  
  167.         col = 0;
  168.         dbo = 0;
  169.         while (dbo != llength(dlp)) {
  170.                 c = lgetc(dlp, dbo);
  171.                 newcol = col;
  172.                 if (c == '\t')
  173.                         newcol |= 0x07;
  174.                 else if (c<0x20 || c==0x7F)
  175.                         ++newcol;
  176.                 ++newcol;
  177.                 if (newcol > curgoal)
  178.                         break;
  179.                 col = newcol;
  180.                 ++dbo;
  181.         }
  182.         return (dbo);
  183. }
  184.  
  185. /*
  186.  * Scroll forward by a specified number of lines, or by a full page if no
  187.  * argument. Bound to "C-V". The "2" in the arithmetic on the window size is
  188.  * the overlap; this value is the default overlap value in ITS EMACS. Because
  189.  * this zaps the top line in the display window, we have to do a hard update.
  190.  */
  191. forwpage(f, n)
  192. register int    n;
  193. {
  194.         register LINE   *lp;
  195.  
  196.         if (f == FALSE) {
  197.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  198.                 if (n <= 0)                     /* Forget the overlap   */
  199.                         n = 1;                  /* if tiny window.      */
  200.         } else if (n < 0)
  201.                 return (backpage(f, -n));
  202. #if     CVMVAS
  203.         else                                    /* Convert from pages   */
  204.                 n *= curwp->w_ntrows;           /* to lines.            */
  205. #endif
  206.         lp = curwp->w_linep;
  207.         while (n-- && lp!=curbp->b_linep)
  208.                 lp = lforw(lp);
  209.         curwp->w_linep = lp;
  210.         curwp->w_dotp  = lp;
  211.         curwp->w_doto  = 0;
  212.         curwp->w_flag |= WFHARD;
  213.         return (TRUE);
  214. }
  215.  
  216. /*
  217.  * This command is like "forwpage", but it goes backwards. The "2", like
  218.  * above, is the overlap between the two windows. The value is from the ITS
  219.  * EMACS manual. Bound to "M-V". We do a hard update for exactly the same
  220.  * reason.
  221.  */
  222. backpage(f, n)
  223. register int    n;
  224. {
  225.         register LINE   *lp;
  226.  
  227.         if (f == FALSE) {
  228.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  229.                 if (n <= 0)                     /* Don't blow up if the */
  230.                         n = 1;                  /* window is tiny.      */
  231.         } else if (n < 0)
  232.                 return (forwpage(f, -n));
  233. #if     CVMVAS
  234.         else                                    /* Convert from pages   */
  235.                 n *= curwp->w_ntrows;           /* to lines.            */
  236. #endif
  237.         lp = curwp->w_linep;
  238.         while (n-- && lback(lp)!=curbp->b_linep)
  239.                 lp = lback(lp);
  240.         curwp->w_linep = lp;
  241.         curwp->w_dotp  = lp;
  242.         curwp->w_doto  = 0;
  243.         curwp->w_flag |= WFHARD;
  244.         return (TRUE);
  245. }
  246.  
  247. /*
  248.  * Set the mark in the current window to the value of "." in the window. No
  249.  * errors are possible. Bound to "M-.".
  250.  */
  251. setmark(f, n)
  252. {
  253.         curwp->w_markp = curwp->w_dotp;
  254.         curwp->w_marko = curwp->w_doto;
  255.         mlwrite("[Mark set]");
  256.         return (TRUE);
  257. }
  258.  
  259. /*
  260.  * Swap the values of "." and "mark" in the current window. This is pretty
  261.  * easy, bacause all of the hard work gets done by the standard routine
  262.  * that moves the mark about. The only possible error is "no mark". Bound to
  263.  * "C-X C-X".
  264.  */
  265. swapmark(f, n)
  266. {
  267.         register LINE   *odotp;
  268.         register int    odoto;
  269.  
  270.         if (curwp->w_markp == NULL) {
  271.                 mlwrite("No mark in this window");
  272.                 return (FALSE);
  273.         }
  274.         odotp = curwp->w_dotp;
  275.         odoto = curwp->w_doto;
  276.         curwp->w_dotp  = curwp->w_markp;
  277.         curwp->w_doto  = curwp->w_marko;
  278.         curwp->w_markp = odotp;
  279.         curwp->w_marko = odoto;
  280.         curwp->w_flag |= WFMOVE;
  281.         return (TRUE);
  282. }
  283.  
  284. /* -eof- */
  285.