home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / UE311C.ZIP / BASIC.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  16KB  |  617 lines

  1. /*    basic.c:    Basic movement functions for
  2.  *            MicroEMACS
  3.  *            (C)Copyright 1990 by Daniel Lawrence
  4.  *
  5.  * The routines in this file move the cursor around on the screen. They
  6.  * compute a new value for the cursor, then adjust ".". The display code
  7.  * always updates the cursor location, so only moves between lines, or
  8.  * functions that adjust the top line in the window and invalidate the
  9.  * framing, are hard.
  10.  */
  11. #include    <stdio.h>
  12. #include    "estruct.h"
  13. #include    "eproto.h"
  14. #include    "edef.h"
  15. #include    "elang.h"
  16.  
  17. /*
  18.  * Move the cursor to the
  19.  * beginning of the current line.
  20.  * Trivial.
  21.  */
  22. PASCAL NEAR gotobol(f, n)
  23.  
  24. int f,n;    /* argument falg and num */
  25.  
  26. {
  27.         curwp->w_doto  = 0;
  28.         return(TRUE);
  29. }
  30.  
  31. /*
  32.  * Move the cursor backwards by "n" characters. If "n" is less than zero call
  33.  * "forwchar" to actually do the move. Otherwise compute the new cursor
  34.  * location. Error if you try and move out of the buffer. Set the flag if the
  35.  * line pointer for dot changes.
  36.  */
  37. PASCAL NEAR backchar(f, n)
  38.  
  39. int f,n;    /* prefix flag and argument */
  40.  
  41. {
  42.         register LINE   *lp;
  43.  
  44.         if (n < 0)
  45.                 return(forwchar(f, -n));
  46.         while (n--) {
  47.                 if (curwp->w_doto == 0) {
  48.                         if ((lp=lback(curwp->w_dotp)) == curbp->b_linep)
  49.                                 return(FALSE);
  50.                         curwp->w_dotp  = lp;
  51.                         curwp->w_doto  = llength(lp);
  52.                         curwp->w_flag |= WFMOVE;
  53.                 } else
  54.                         curwp->w_doto--;
  55.         }
  56. #if    DBCS
  57.     return(stopback());
  58. #else
  59.         return(TRUE);
  60. #endif
  61. }
  62.  
  63. /*
  64.  * Move the cursor to the end of the current line. Trivial. No errors.
  65.  */
  66. PASCAL NEAR gotoeol(f, n)
  67.  
  68. int f,n;    /* argument falg and num */
  69.  
  70. {
  71.         curwp->w_doto  = llength(curwp->w_dotp);
  72.         return(TRUE);
  73. }
  74.  
  75. /*
  76.  * Move the cursor forwards by "n" characters. If "n" is less than zero call
  77.  * "backchar" to actually do the move. Otherwise compute the new cursor
  78.  * location, and move ".". Error if you try and move off the end of the
  79.  * buffer. Set the flag if the line pointer for dot changes.
  80.  */
  81. PASCAL NEAR forwchar(f, n)
  82.  
  83. int f,n;    /* prefix flag and argument */
  84.  
  85. {
  86.         if (n < 0)
  87.                 return(backchar(f, -n));
  88.         while (n--) {
  89.                 if (curwp->w_doto == llength(curwp->w_dotp)) {
  90.                         if (curwp->w_dotp == curbp->b_linep)
  91.                                 return(FALSE);
  92.                         curwp->w_dotp  = lforw(curwp->w_dotp);
  93.                         curwp->w_doto  = 0;
  94.                         curwp->w_flag |= WFMOVE;
  95.                 } else
  96.                         curwp->w_doto++;
  97.         }
  98. #if    DBCS
  99.     return(stopforw());
  100. #else
  101.         return(TRUE);
  102. #endif
  103. }
  104.  
  105. PASCAL NEAR gotoline(f, n)    /* move to a particular line.
  106.                argument (n) must be a positive integer for
  107.                this to actually do anything        */
  108.  
  109. int f,n;    /* prefix flag and argument */
  110.  
  111. {
  112.     register int status;    /* status return */
  113.     char arg[NSTRING];    /* buffer to hold argument */
  114.  
  115.     /* get an argument if one doesnt exist */
  116.     if (f == FALSE) {
  117.         if ((status = mlreply(TEXT7, arg, NSTRING)) != TRUE) {
  118. /*                                    "Line to GOTO: " */
  119.             mlwrite(TEXT8);
  120. /*                              "[Aborted]" */
  121.             return(status);
  122.         }
  123.         n = asc_int(arg);
  124.     }
  125.  
  126.     if (n < 1)        /* if a bogus argument...then leave */
  127.         return(FALSE);
  128.  
  129.     /* first, we go to the start of the buffer */
  130.         curwp->w_dotp  = lforw(curbp->b_linep);
  131.         curwp->w_doto  = 0;
  132.     return(forwline(f, n-1));
  133. }
  134.  
  135. /*
  136.  * Goto the beginning of the buffer. Massive adjustment of dot. This is
  137.  * considered to be hard motion; it really isn't if the original value of dot
  138.  * is the same as the new value of dot. Normally bound to "M-<".
  139.  */
  140. PASCAL NEAR gotobob(f, n)
  141.  
  142. int f,n;    /* argument flag and num */
  143.  
  144. {
  145.         curwp->w_dotp  = lforw(curbp->b_linep);
  146.         curwp->w_doto  = 0;
  147.         curwp->w_flag |= WFMOVE;
  148.         return(TRUE);
  149. }
  150.  
  151. /*
  152.  * Move to the end of the buffer. Dot is always put at the end of the file
  153.  * (ZJ). The standard screen code does most of the hard parts of update.
  154.  * Bound to "M->".
  155.  */
  156. PASCAL NEAR gotoeob(f, n)
  157.  
  158. int f,n;    /* argument falg and num */
  159.  
  160. {
  161.         curwp->w_dotp  = curbp->b_linep;
  162.         curwp->w_doto  = 0;
  163.         curwp->w_flag |= WFMOVE;
  164.         return(TRUE);
  165. }
  166.  
  167. /*
  168.  * Move forward by full lines. If the number of lines to move is less than
  169.  * zero, call the backward line function to actually do it. The last command
  170.  * controls how the goal column is set. Bound to "C-N". No errors are
  171.  * possible.
  172.  */
  173. PASCAL NEAR forwline(f, n)
  174.  
  175. int f,n;    /* argument falg and num */
  176.  
  177. {
  178.         register LINE   *dlp;
  179.  
  180.         if (n < 0)
  181.                 return(backline(f, -n));
  182.  
  183.     /* if we are on the last line as we start....fail the command */
  184.     if (curwp->w_dotp == curbp->b_linep)
  185.         return(FALSE);
  186.  
  187.     /* if the last command was not note a line move,
  188.        reset the goal column */
  189.         if ((lastflag&CFCPCN) == 0)
  190.                 curgoal = getccol(FALSE);
  191.  
  192.     /* flag this command as a line move */
  193.         thisflag |= CFCPCN;
  194.  
  195.     /* and move the point down */
  196.         dlp = curwp->w_dotp;
  197.         while (n-- && dlp!=curbp->b_linep)
  198.                 dlp = lforw(dlp);
  199.  
  200.     /* reseting the current position */
  201.         curwp->w_dotp  = dlp;
  202.         curwp->w_doto  = getgoal(dlp);
  203.         curwp->w_flag |= WFMOVE;
  204. #if    DBCS
  205.     return(stopback());
  206. #else
  207.         return(TRUE);
  208. #endif
  209. }
  210.  
  211. /*
  212.  * This function is like "forwline", but goes backwards. The scheme is exactly
  213.  * the same. Check for arguments that are less than zero and call your
  214.  * alternate. Figure out the new line and call "movedot" to perform the
  215.  * motion. No errors are possible. Bound to "C-P".
  216.  */
  217. PASCAL NEAR backline(f, n)
  218.  
  219. int f,n;    /* argument falg and num */
  220.  
  221. {
  222.         register LINE   *dlp;
  223.  
  224.         if (n < 0)
  225.                 return(forwline(f, -n));
  226.  
  227.  
  228.     /* if we are on the last line as we start....fail the command */
  229.     if (lback(curwp->w_dotp) == curbp->b_linep)
  230.         return(FALSE);
  231.  
  232.     /* if the last command was not note a line move,
  233.        reset the goal column */
  234.         if ((lastflag&CFCPCN) == 0)
  235.                 curgoal = getccol(FALSE);
  236.  
  237.     /* flag this command as a line move */
  238.         thisflag |= CFCPCN;
  239.  
  240.     /* and move the point up */
  241.         dlp = curwp->w_dotp;
  242.         while (n-- && lback(dlp)!=curbp->b_linep)
  243.                 dlp = lback(dlp);
  244.  
  245.     /* reseting the current position */
  246.         curwp->w_dotp  = dlp;
  247.         curwp->w_doto  = getgoal(dlp);
  248.         curwp->w_flag |= WFMOVE;
  249. #if    DBCS
  250.     return(stopback());
  251. #else
  252.         return(TRUE);
  253. #endif
  254. }
  255.  
  256. PASCAL NEAR gotobop(f, n) /* go back to the beginning of the current paragraph
  257.            here we look for a blank line or a character from
  258.            $paralead to delimit the beginning of a paragraph or
  259.            $fmtlead to delimit a line before the paragraph */
  260.  
  261. int f, n;    /* default Flag & Numeric argument */
  262.  
  263. {
  264.     register int suc;    /* success of last backchar */
  265.     register int c;        /* current character in scan */
  266.     register char *sp;    /* ptr into character leadin lists */
  267.  
  268.     if (n < 0)    /* the other way...*/
  269.         return(gotoeop(f, -n));
  270.  
  271.     while (n-- > 0) {    /* for each one asked for */
  272.  
  273.         /* first scan back until we are in a word */
  274.         suc = backchar(FALSE, 1);
  275.         while (!inword() && suc)
  276.             suc = backchar(FALSE, 1);
  277.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  278.  
  279.         /* scan back through the text */
  280.         while (lback(curwp->w_dotp) != curbp->b_linep) {
  281.  
  282.             /* at blank line */
  283.             if (llength(curwp->w_dotp) == 0)
  284.                 break;
  285.  
  286.             /* current line start with member of $paralead? */
  287.             c = lgetc(curwp->w_dotp, 0);
  288.             sp = paralead;
  289.             while (*sp) {
  290.                 if (c == *sp)
  291.                     break;
  292.                 ++sp;
  293.             }
  294.             if (c == *sp)
  295.                 break;            
  296.  
  297.             /* last line start with member of $fmtlead? */
  298.             c = lgetc(lback(curwp->w_dotp), 0);
  299.             sp = fmtlead;
  300.             while (*sp) {
  301.                 if (c == *sp)
  302.                     break;
  303.                 ++sp;
  304.             }
  305.             if (c == *sp)
  306.                 break;            
  307.  
  308.             /* back one line... */
  309.             curwp->w_dotp = lback(curwp->w_dotp);
  310.         }
  311.  
  312.         /* and then forward until we are in a word */
  313. /*        suc = forwchar(FALSE, 1); */
  314.         suc = TRUE;
  315.         while (suc && !inword())
  316.             suc = forwchar(FALSE, 1);
  317.     }
  318.     curwp->w_flag |= WFMOVE;    /* force screen update */
  319.     return(TRUE);
  320. }
  321.  
  322. PASCAL NEAR gotoeop(f, n) /* go forword to the end of the current paragraph
  323.                  looking for a member of $paralead or $fmtlead
  324.                  or a blank line to delimit the start of the
  325.                  next paragraph
  326. */
  327.  
  328. int f, n;    /* default Flag & Numeric argument */
  329.  
  330. {
  331.     register int suc;    /* success of last backchar */
  332.     register int c;        /* current character in scan */
  333.     register char *sp;    /* ptr into character leadin lists */
  334.  
  335.     if (n < 0)    /* the other way...*/
  336.         return(gotobop(f, -n));
  337.  
  338.     while (n-- > 0) {    /* for each one asked for */
  339.  
  340.         /* first scan forward until we are in a word */
  341.         suc = forwchar(FALSE, 1);
  342.         while (!inword() && suc)
  343.             suc = forwchar(FALSE, 1);
  344.  
  345.         /* and go to the B-O-Line */
  346.         curwp->w_doto = 0;
  347.  
  348.         /* of next line if not at EOF */
  349.         if (suc)
  350.             curwp->w_dotp = lforw(curwp->w_dotp);
  351.  
  352.         /* scan forward */
  353.         while (curwp->w_dotp != curbp->b_linep) {
  354.  
  355.             /* at blank line */
  356.             if (llength(curwp->w_dotp) == 0)
  357.                 break;
  358.  
  359.             /* current line start with member of $paralead? */
  360.             c = lgetc(curwp->w_dotp, 0);
  361.             sp = paralead;
  362.             while (*sp) {
  363.                 if (c == *sp)
  364.                     break;
  365.                 ++sp;
  366.             }
  367.             if (c == *sp)
  368.                 break;            
  369.  
  370.             /* current line start with member of $fmtlead? */
  371.             c = lgetc(curwp->w_dotp, 0);
  372.             sp = fmtlead;
  373.             while (*sp) {
  374.                 if (c == *sp)
  375.                     break;
  376.                 ++sp;
  377.             }
  378.             if (c == *sp)
  379.                 break;            
  380.  
  381.             /* forward one line... */
  382.             curwp->w_dotp = lforw(curwp->w_dotp);
  383.         }
  384.  
  385.         /* and then backward until we are in a word */
  386.         suc = backchar(FALSE, 1);
  387.         while (suc && !inword()) {
  388.             suc = backchar(FALSE, 1);
  389.         }
  390.         curwp->w_doto = llength(curwp->w_dotp);    /* and to the EOL */
  391.     }
  392.     curwp->w_flag |= WFMOVE;    /* force screen update */
  393.     return(TRUE);
  394. }
  395.  
  396. /*
  397.  * This routine, given a pointer to a LINE, and the current cursor goal
  398.  * column, return the best choice for the offset. The offset is returned.
  399.  * Used by "C-N" and "C-P".
  400.  */
  401.  
  402. int PASCAL NEAR getgoal(dlp)
  403.  
  404. register LINE   *dlp;
  405.  
  406. {
  407.         register int    c;
  408.         register int    col;
  409.         register int    newcol;
  410.         register int    dbo;
  411.  
  412.         col = 0;
  413.         dbo = 0;
  414.         while (dbo != llength(dlp)) {
  415.                 c = lgetc(dlp, dbo);
  416.                 newcol = col;
  417.                 if (c == '\t')
  418.             newcol += -(newcol % tabsize) + (tabsize - 1);
  419.                 else if (c<0x20 || c==0x7F)
  420.                         ++newcol;
  421.                 ++newcol;
  422.                 if (newcol > curgoal)
  423.                         break;
  424.                 col = newcol;
  425.                 ++dbo;
  426.         }
  427.         return(dbo);
  428. }
  429.  
  430. /*
  431.  * Scroll forward by a specified number of lines, or by a full page if no
  432.  * argument. Bound to "C-V". The "2" in the arithmetic on the window size is
  433.  * the overlap; this value is the default overlap value in ITS EMACS. Because
  434.  * this zaps the top line in the display window, we have to do a hard update.
  435.  */
  436. PASCAL NEAR forwpage(f, n)
  437.  
  438. int f,n;    /* prefix flag and argument */
  439.  
  440. {
  441.         register LINE   *lp;
  442.  
  443.         if (f == FALSE) {
  444.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  445.                 if (n <= 0)                     /* Forget the overlap   */
  446.                         n = 1;                  /* if tiny window.      */
  447.         } else if (n < 0)
  448.                 return(backpage(f, -n));
  449.         lp = curwp->w_linep;
  450.         while (n-- && lp!=curbp->b_linep)
  451.                 lp = lforw(lp);
  452.         curwp->w_linep = lp;
  453.         curwp->w_dotp  = lp;
  454.         curwp->w_doto  = 0;
  455.         curwp->w_flag |= WFHARD;
  456.         return(TRUE);
  457. }
  458.  
  459. /*
  460.  * This command is like "forwpage", but it goes backwards. The "2", like
  461.  * above, is the overlap between the two windows. The value is from the ITS
  462.  * EMACS manual. Bound to "M-V". We do a hard update for exactly the same
  463.  * reason.
  464.  */
  465. PASCAL NEAR backpage(f, n)
  466.  
  467. register int f;
  468. register int n;
  469.  
  470. {
  471.         register LINE   *lp;
  472.  
  473.         if (f == FALSE) {
  474.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  475.                 if (n <= 0)                     /* Don't blow up if the */
  476.                         n = 1;                  /* window is tiny.      */
  477.         } else if (n < 0)
  478.                 return(forwpage(f, -n));
  479.         lp = curwp->w_linep;
  480.         while (n-- && lback(lp)!=curbp->b_linep)
  481.                 lp = lback(lp);
  482.         curwp->w_linep = lp;
  483.         curwp->w_dotp  = lp;
  484.         curwp->w_doto  = 0;
  485.         curwp->w_flag |= WFHARD;
  486.         return(TRUE);
  487. }
  488.  
  489. /*
  490.  * Set the mark in the current window to the value of "." in the window. No
  491.  * errors are possible. Bound to "M-.".
  492.  */
  493. PASCAL NEAR setmark(f, n)
  494.  
  495. int f,n;    /* argument falg and num */
  496.  
  497. {
  498.     /* make sure it is in range */
  499.     if (f == FALSE)
  500.         n = 0;
  501.     n %= NMARKS;
  502.  
  503.         curwp->w_markp[n] = curwp->w_dotp;
  504.         curwp->w_marko[n] = curwp->w_doto;
  505.         mlwrite(TEXT9, n);
  506. /*              "[Mark %d set]" */
  507.         return(TRUE);
  508. }
  509.  
  510. /*
  511.  * Remove the mark in the current window.
  512.  * Bound to ^X <space> 
  513.  */
  514. PASCAL NEAR remmark(f, n)
  515.  
  516. int f,n;    /* argument falg and num */
  517.  
  518. {
  519.     /* make sure it is in range */
  520.     if (f == FALSE)
  521.         n = 0;
  522.     n %= NMARKS;
  523.  
  524.         curwp->w_markp[n] = NULL;
  525.         curwp->w_marko[n] = 0;
  526.         mlwrite(TEXT10, n);
  527. /*              "[Mark %d removed]" */
  528.         return(TRUE);
  529. }
  530.  
  531. /*
  532.  * Swap the values of "." and "mark" in the current window. This is pretty
  533.  * easy, bacause all of the hard work gets done by the standard routine
  534.  * that moves the mark about. The only possible error is "no mark". Bound to
  535.  * "C-X C-X".
  536.  */
  537. PASCAL NEAR swapmark(f, n)
  538.  
  539. int f,n;    /* argument falg and num */
  540.  
  541. {
  542.         register LINE   *odotp;
  543.         register int    odoto;
  544.  
  545.     /* make sure it is in range */
  546.     if (f == FALSE)
  547.         n = 0;
  548.     n %= NMARKS;
  549.  
  550.         if (curwp->w_markp[n] == NULL) {
  551.                 mlwrite(TEXT11, n);
  552. /*                      "No mark %d in this window" */
  553.                 return(FALSE);
  554.         }
  555.         odotp = curwp->w_dotp;
  556.         odoto = curwp->w_doto;
  557.         curwp->w_dotp  = curwp->w_markp[n];
  558.         curwp->w_doto  = curwp->w_marko[n];
  559.         curwp->w_markp[n] = odotp;
  560.         curwp->w_marko[n] = odoto;
  561.         curwp->w_flag |= WFMOVE;
  562.         return(TRUE);
  563. }
  564.  
  565. /*
  566.  * Goto a mark in the current window. This is pretty easy, bacause all of
  567.  * the hard work gets done by the standard routine that moves the mark
  568.  * about. The only possible error is "no mark". Bound to "M-^G".
  569.  */
  570. PASCAL NEAR gotomark(f, n)
  571.  
  572. int f, n;    /* default and nemeric args */
  573.  
  574. {
  575.     /* make sure it is in range */
  576.     if (f == FALSE)
  577.         n = 0;
  578.     n %= NMARKS;
  579.  
  580.         if (curwp->w_markp[n] == NULL) {
  581.                 mlwrite(TEXT11, n);
  582. /*                      "No mark %d in this window" */
  583.                 return(FALSE);
  584.         }
  585.         curwp->w_dotp  = curwp->w_markp[n];
  586.         curwp->w_doto  = curwp->w_marko[n];
  587.         curwp->w_flag |= WFMOVE;
  588.         return(TRUE);
  589. }
  590.  
  591. #if    DBCS
  592. /* advance a char if we are on the second byte of a DBCS character */
  593.  
  594. int PASCAL NEAR stopforw()
  595.  
  596. {
  597.     /* don't stop on the second byte of a 2 byte character */
  598.     if (curwp->w_doto > 0 && is2byte(curwp->w_dotp->l_text,
  599.         curwp->w_dotp->l_text + curwp->w_doto - 1))
  600.             return(forwchar(TRUE, 1));
  601.     return(TRUE);
  602. }
  603.  
  604. /* retreat a char if we are on the second byte of a DBCS character */
  605.  
  606. int PASCAL NEAR stopback()
  607.  
  608. {
  609.     /* don't stop on the second byte of a 2 byte character */
  610.     if (curwp->w_doto > 0 && is2byte(curwp->w_dotp->l_text,
  611.         curwp->w_dotp->l_text + curwp->w_doto - 1))
  612.             return(backchar(TRUE, 1));
  613.     return(TRUE);
  614. }
  615. #endif
  616.  
  617.