home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 100-199 / ff119.lzh / MicroEMACS / src / src.zoo / basic.c < prev    next >
C/C++ Source or Header  |  1987-12-09  |  12KB  |  423 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        <stdio.h>
  9. #include    "estruct.h"
  10. #include        "edef.h"
  11.  
  12. /*
  13.  * Move the cursor to the
  14.  * beginning of the current line.
  15.  * Trivial.
  16.  */
  17. gotobol(f, n)
  18. {
  19.         curwp->w_doto  = 0;
  20.         return (TRUE);
  21. }
  22.  
  23. /*
  24.  * Move the cursor backwards by "n" characters. If "n" is less than zero call
  25.  * "forwchar" to actually do the move. Otherwise compute the new cursor
  26.  * location. Error if you try and move out of the buffer. Set the flag if the
  27.  * line pointer for dot changes.
  28.  */
  29. backchar(f, n)
  30. register int    n;
  31. {
  32.         register LINE   *lp;
  33.  
  34.         if (n < 0)
  35.                 return (forwchar(f, -n));
  36.         while (n--) {
  37.                 if (curwp->w_doto == 0) {
  38.                         if ((lp=lback(curwp->w_dotp)) == curbp->b_linep)
  39.                                 return (FALSE);
  40.                         curwp->w_dotp  = lp;
  41.                         curwp->w_doto  = llength(lp);
  42.                         curwp->w_flag |= WFMOVE;
  43.                 } else
  44.                         curwp->w_doto--;
  45.         }
  46.         return (TRUE);
  47. }
  48.  
  49. /*
  50.  * Move the cursor to the end of the current line. Trivial. No errors.
  51.  */
  52. gotoeol(f, n)
  53. {
  54.         curwp->w_doto  = llength(curwp->w_dotp);
  55.         return (TRUE);
  56. }
  57.  
  58. /*
  59.  * Move the cursor forwards by "n" characters. If "n" is less than zero call
  60.  * "backchar" to actually do the move. Otherwise compute the new cursor
  61.  * location, and move ".". Error if you try and move off the end of the
  62.  * buffer. Set the flag if the line pointer for dot changes.
  63.  */
  64. forwchar(f, n)
  65. register int    n;
  66. {
  67.         if (n < 0)
  68.                 return (backchar(f, -n));
  69.         while (n--) {
  70.                 if (curwp->w_doto == llength(curwp->w_dotp)) {
  71.                         if (curwp->w_dotp == curbp->b_linep)
  72.                                 return (FALSE);
  73.                         curwp->w_dotp  = lforw(curwp->w_dotp);
  74.                         curwp->w_doto  = 0;
  75.                         curwp->w_flag |= WFMOVE;
  76.                 } else
  77.                         curwp->w_doto++;
  78.         }
  79.         return (TRUE);
  80. }
  81.  
  82. gotoline(f, n)        /* move to a particular line.
  83.                argument (n) must be a positive integer for
  84.                this to actually do anything        */
  85.  
  86. {
  87.     register int status;    /* status return */
  88.     char arg[NSTRING];    /* buffer to hold argument */
  89.  
  90.     /* get an argument if one doesnt exist */
  91.     if (f == FALSE) {
  92.         if ((status = mlreply("Line to GOTO: ", arg, NSTRING)) != TRUE) {
  93.             mlwrite("[Aborted]");
  94.             return(status);
  95.         }
  96.         n = atoi(arg);
  97.     }
  98.  
  99.     if (n < 1)        /* if a bogus argument...then leave */
  100.         return(FALSE);
  101.  
  102.     /* first, we go to the start of the buffer */
  103.         curwp->w_dotp  = lforw(curbp->b_linep);
  104.         curwp->w_doto  = 0;
  105.     return(forwline(f, n-1));
  106. }
  107.  
  108. /*
  109.  * Goto the beginning of the buffer. Massive adjustment of dot. This is
  110.  * considered to be hard motion; it really isn't if the original value of dot
  111.  * is the same as the new value of dot. Normally bound to "M-<".
  112.  */
  113. gotobob(f, n)
  114. {
  115.         curwp->w_dotp  = lforw(curbp->b_linep);
  116.         curwp->w_doto  = 0;
  117.         curwp->w_flag |= WFHARD;
  118.         return (TRUE);
  119. }
  120.  
  121. /*
  122.  * Move to the end of the buffer. Dot is always put at the end of the file
  123.  * (ZJ). The standard screen code does most of the hard parts of update.
  124.  * Bound to "M->".
  125.  */
  126. gotoeob(f, n)
  127. {
  128.         curwp->w_dotp  = curbp->b_linep;
  129.         curwp->w_doto  = 0;
  130.         curwp->w_flag |= WFHARD;
  131.         return (TRUE);
  132. }
  133.  
  134. /*
  135.  * Move forward by full lines. If the number of lines to move is less than
  136.  * zero, call the backward line function to actually do it. The last command
  137.  * controls how the goal column is set. Bound to "C-N". No errors are
  138.  * possible.
  139.  */
  140. forwline(f, n)
  141. {
  142.         register LINE   *dlp;
  143.  
  144.         if (n < 0)
  145.                 return (backline(f, -n));
  146.  
  147.     /* if we are on the last line as we start....fail the command */
  148.     if (curwp->w_dotp == curbp->b_linep)
  149.         return(FALSE);
  150.  
  151.     /* if the last command was not note a line move,
  152.        reset the goal column */
  153.         if ((lastflag&CFCPCN) == 0)
  154.                 curgoal = getccol(FALSE);
  155.  
  156.     /* flag this command as a line move */
  157.         thisflag |= CFCPCN;
  158.  
  159.     /* and move the point down */
  160.         dlp = curwp->w_dotp;
  161.         while (n-- && dlp!=curbp->b_linep)
  162.                 dlp = lforw(dlp);
  163.  
  164.     /* reseting the current position */
  165.         curwp->w_dotp  = dlp;
  166.         curwp->w_doto  = getgoal(dlp);
  167.         curwp->w_flag |= WFMOVE;
  168.         return (TRUE);
  169. }
  170.  
  171. /*
  172.  * This function is like "forwline", but goes backwards. The scheme is exactly
  173.  * the same. Check for arguments that are less than zero and call your
  174.  * alternate. Figure out the new line and call "movedot" to perform the
  175.  * motion. No errors are possible. Bound to "C-P".
  176.  */
  177. backline(f, n)
  178. {
  179.         register LINE   *dlp;
  180.  
  181.         if (n < 0)
  182.                 return (forwline(f, -n));
  183.  
  184.  
  185.     /* if we are on the last line as we start....fail the command */
  186.     if (lback(curwp->w_dotp) == curbp->b_linep)
  187.         return(FALSE);
  188.  
  189.     /* if the last command was not note a line move,
  190.        reset the goal column */
  191.         if ((lastflag&CFCPCN) == 0)
  192.                 curgoal = getccol(FALSE);
  193.  
  194.     /* flag this command as a line move */
  195.         thisflag |= CFCPCN;
  196.  
  197.     /* and move the point up */
  198.         dlp = curwp->w_dotp;
  199.         while (n-- && lback(dlp)!=curbp->b_linep)
  200.                 dlp = lback(dlp);
  201.  
  202.     /* reseting the current position */
  203.         curwp->w_dotp  = dlp;
  204.         curwp->w_doto  = getgoal(dlp);
  205.         curwp->w_flag |= WFMOVE;
  206.         return (TRUE);
  207. }
  208.  
  209. #if    WORDPRO
  210. gotobop(f, n)    /* go back to the beginning of the current paragraph
  211.            here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  212.            combination to delimit the beginning of a paragraph    */
  213.  
  214. int f, n;    /* default Flag & Numeric argument */
  215.  
  216. {
  217.     register int suc;    /* success of last backchar */
  218.  
  219.     if (n < 0)    /* the other way...*/
  220.         return(gotoeop(f, -n));
  221.  
  222.     while (n-- > 0) {    /* for each one asked for */
  223.  
  224.         /* first scan back until we are in a word */
  225.         suc = backchar(FALSE, 1);
  226.         while (!inword() && suc)
  227.             suc = backchar(FALSE, 1);
  228.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  229.  
  230.         /* and scan back until we hit a <NL><NL> or <NL><TAB>
  231.            or a <NL><SPACE>                    */
  232.         while (lback(curwp->w_dotp) != curbp->b_linep)
  233.             if (llength(curwp->w_dotp) != 0 &&
  234.                 lgetc(curwp->w_dotp, curwp->w_doto) != TAB &&
  235.                 lgetc(curwp->w_dotp, curwp->w_doto) != ' ')
  236.                 curwp->w_dotp = lback(curwp->w_dotp);
  237.             else
  238.                 break;
  239.  
  240.         /* and then forward until we are in a word */
  241.         suc = forwchar(FALSE, 1);
  242.         while (suc && !inword())
  243.             suc = forwchar(FALSE, 1);
  244.     }
  245.     curwp->w_flag |= WFMOVE;    /* force screen update */
  246.     return(TRUE);
  247. }
  248.  
  249. gotoeop(f, n)    /* go forword to the end of the current paragraph
  250.            here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  251.            combination to delimit the beginning of a paragraph    */
  252.  
  253. int f, n;    /* default Flag & Numeric argument */
  254.  
  255. {
  256.     register int suc;    /* success of last backchar */
  257.  
  258.     if (n < 0)    /* the other way...*/
  259.         return(gotobop(f, -n));
  260.  
  261.     while (n-- > 0) {    /* for each one asked for */
  262.  
  263.         /* first scan forward until we are in a word */
  264.         suc = forwchar(FALSE, 1);
  265.         while (!inword() && suc)
  266.             suc = forwchar(FALSE, 1);
  267.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  268.         if (suc)    /* of next line if not at EOF */
  269.             curwp->w_dotp = lforw(curwp->w_dotp);
  270.  
  271.         /* and scan forword until we hit a <NL><NL> or <NL><TAB>
  272.            or a <NL><SPACE>                    */
  273.         while (curwp->w_dotp != curbp->b_linep) {
  274.             if (llength(curwp->w_dotp) != 0 &&
  275.                 lgetc(curwp->w_dotp, curwp->w_doto) != TAB &&
  276.                 lgetc(curwp->w_dotp, curwp->w_doto) != ' ')
  277.                 curwp->w_dotp = lforw(curwp->w_dotp);
  278.             else
  279.                 break;
  280.         }
  281.  
  282.         /* and then backward until we are in a word */
  283.         suc = backchar(FALSE, 1);
  284.         while (suc && !inword()) {
  285.             suc = backchar(FALSE, 1);
  286.         }
  287.         curwp->w_doto = llength(curwp->w_dotp);    /* and to the EOL */
  288.     }
  289.     curwp->w_flag |= WFMOVE;    /* force screen update */
  290.     return(TRUE);
  291. }
  292. #endif
  293.  
  294. /*
  295.  * This routine, given a pointer to a LINE, and the current cursor goal
  296.  * column, return the best choice for the offset. The offset is returned.
  297.  * Used by "C-N" and "C-P".
  298.  */
  299. getgoal(dlp)
  300. register LINE   *dlp;
  301. {
  302.         register int    c;
  303.         register int    col;
  304.         register int    newcol;
  305.         register int    dbo;
  306.  
  307.         col = 0;
  308.         dbo = 0;
  309.         while (dbo != llength(dlp)) {
  310.                 c = lgetc(dlp, dbo);
  311.                 newcol = col;
  312.                 if (c == '\t')
  313.                         newcol |= 0x07;
  314.                 else if (c<0x20 || c==0x7F)
  315.                         ++newcol;
  316.                 ++newcol;
  317.                 if (newcol > curgoal)
  318.                         break;
  319.                 col = newcol;
  320.                 ++dbo;
  321.         }
  322.         return (dbo);
  323. }
  324.  
  325. /*
  326.  * Scroll forward by a specified number of lines, or by a full page if no
  327.  * argument. Bound to "C-V". The "2" in the arithmetic on the window size is
  328.  * the overlap; this value is the default overlap value in ITS EMACS. Because
  329.  * this zaps the top line in the display window, we have to do a hard update.
  330.  */
  331. forwpage(f, n)
  332. register int    n;
  333. {
  334.         register LINE   *lp;
  335.  
  336.         if (f == FALSE) {
  337.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  338.                 if (n <= 0)                     /* Forget the overlap   */
  339.                         n = 1;                  /* if tiny window.      */
  340.         } else if (n < 0)
  341.                 return (backpage(f, -n));
  342. #if     CVMVAS
  343.         else                                    /* Convert from pages   */
  344.                 n *= curwp->w_ntrows;           /* to lines.            */
  345. #endif
  346.         lp = curwp->w_linep;
  347.         while (n-- && lp!=curbp->b_linep)
  348.                 lp = lforw(lp);
  349.         curwp->w_linep = lp;
  350.         curwp->w_dotp  = lp;
  351.         curwp->w_doto  = 0;
  352.         curwp->w_flag |= WFHARD;
  353.         return (TRUE);
  354. }
  355.  
  356. /*
  357.  * This command is like "forwpage", but it goes backwards. The "2", like
  358.  * above, is the overlap between the two windows. The value is from the ITS
  359.  * EMACS manual. Bound to "M-V". We do a hard update for exactly the same
  360.  * reason.
  361.  */
  362. backpage(f, n)
  363. register int    n;
  364. {
  365.         register LINE   *lp;
  366.  
  367.         if (f == FALSE) {
  368.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  369.                 if (n <= 0)                     /* Don't blow up if the */
  370.                         n = 1;                  /* window is tiny.      */
  371.         } else if (n < 0)
  372.                 return (forwpage(f, -n));
  373. #if     CVMVAS
  374.         else                                    /* Convert from pages   */
  375.                 n *= curwp->w_ntrows;           /* to lines.            */
  376. #endif
  377.         lp = curwp->w_linep;
  378.         while (n-- && lback(lp)!=curbp->b_linep)
  379.                 lp = lback(lp);
  380.         curwp->w_linep = lp;
  381.         curwp->w_dotp  = lp;
  382.         curwp->w_doto  = 0;
  383.         curwp->w_flag |= WFHARD;
  384.         return (TRUE);
  385. }
  386.  
  387. /*
  388.  * Set the mark in the current window to the value of "." in the window. No
  389.  * errors are possible. Bound to "M-.".
  390.  */
  391. setmark(f, n)
  392. {
  393.         curwp->w_markp = curwp->w_dotp;
  394.         curwp->w_marko = curwp->w_doto;
  395.         mlwrite("[Mark set]");
  396.         return (TRUE);
  397. }
  398.  
  399. /*
  400.  * Swap the values of "." and "mark" in the current window. This is pretty
  401.  * easy, bacause all of the hard work gets done by the standard routine
  402.  * that moves the mark about. The only possible error is "no mark". Bound to
  403.  * "C-X C-X".
  404.  */
  405. swapmark(f, n)
  406. {
  407.         register LINE   *odotp;
  408.         register int    odoto;
  409.  
  410.         if (curwp->w_markp == NULL) {
  411.                 mlwrite("No mark in this window");
  412.                 return (FALSE);
  413.         }
  414.         odotp = curwp->w_dotp;
  415.         odoto = curwp->w_doto;
  416.         curwp->w_dotp  = curwp->w_markp;
  417.         curwp->w_doto  = curwp->w_marko;
  418.         curwp->w_markp = odotp;
  419.         curwp->w_marko = odoto;
  420.         curwp->w_flag |= WFMOVE;
  421.         return (TRUE);
  422. }
  423.