home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / memacs / ue311c.arc / BASIC.C < prev    next >
C/C++ Source or Header  |  1990-08-16  |  15KB  |  560 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 |= WFHARD;
  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 |= WFHARD;
  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 <NL><NL> or <NL><TAB> or <NL><SPACE>
  258.            combination to delimit the beginning of a paragraph    */
  259.  
  260. int f, n;    /* default Flag & Numeric argument */
  261.  
  262. {
  263.     register int suc;    /* success of last backchar */
  264.  
  265.     if (n < 0)    /* the other way...*/
  266.         return(gotoeop(f, -n));
  267.  
  268.     while (n-- > 0) {    /* for each one asked for */
  269.  
  270.         /* first scan back until we are in a word */
  271.         suc = backchar(FALSE, 1);
  272.         while (!inword() && suc)
  273.             suc = backchar(FALSE, 1);
  274.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  275.  
  276.         /* and scan back until we hit a <NL><NL> or <NL><TAB>
  277.            or a <NL><SPACE>                    */
  278.         while (lback(curwp->w_dotp) != curbp->b_linep)
  279.             if (llength(curwp->w_dotp) != 0 &&
  280.                 lgetc(curwp->w_dotp, curwp->w_doto) != TAB &&
  281.                 lgetc(curwp->w_dotp, curwp->w_doto) != ' ')
  282.                 curwp->w_dotp = lback(curwp->w_dotp);
  283.             else
  284.                 break;
  285.  
  286.         /* and then forward until we are in a word */
  287.         suc = forwchar(FALSE, 1);
  288.         while (suc && !inword())
  289.             suc = forwchar(FALSE, 1);
  290.     }
  291.     curwp->w_flag |= WFMOVE;    /* force screen update */
  292.     return(TRUE);
  293. }
  294.  
  295. PASCAL NEAR gotoeop(f, n) /* go forword to the end of the current paragraph
  296.            here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  297.            combination to delimit the beginning of a paragraph    */
  298.  
  299. int f, n;    /* default Flag & Numeric argument */
  300.  
  301. {
  302.     register int suc;    /* success of last backchar */
  303.  
  304.     if (n < 0)    /* the other way...*/
  305.         return(gotobop(f, -n));
  306.  
  307.     while (n-- > 0) {    /* for each one asked for */
  308.  
  309.         /* first scan forward until we are in a word */
  310.         suc = forwchar(FALSE, 1);
  311.         while (!inword() && suc)
  312.             suc = forwchar(FALSE, 1);
  313.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  314.         if (suc)    /* of next line if not at EOF */
  315.             curwp->w_dotp = lforw(curwp->w_dotp);
  316.  
  317.         /* and scan forword until we hit a <NL><NL> or <NL><TAB>
  318.            or a <NL><SPACE>                    */
  319.         while (curwp->w_dotp != curbp->b_linep) {
  320.             if (llength(curwp->w_dotp) != 0 &&
  321.                 lgetc(curwp->w_dotp, curwp->w_doto) != TAB &&
  322.                 lgetc(curwp->w_dotp, curwp->w_doto) != ' ')
  323.                 curwp->w_dotp = lforw(curwp->w_dotp);
  324.             else
  325.                 break;
  326.         }
  327.  
  328.         /* and then backward until we are in a word */
  329.         suc = backchar(FALSE, 1);
  330.         while (suc && !inword()) {
  331.             suc = backchar(FALSE, 1);
  332.         }
  333.         curwp->w_doto = llength(curwp->w_dotp);    /* and to the EOL */
  334.     }
  335.     curwp->w_flag |= WFMOVE;    /* force screen update */
  336.     return(TRUE);
  337. }
  338.  
  339. /*
  340.  * This routine, given a pointer to a LINE, and the current cursor goal
  341.  * column, return the best choice for the offset. The offset is returned.
  342.  * Used by "C-N" and "C-P".
  343.  */
  344.  
  345. int PASCAL NEAR getgoal(dlp)
  346.  
  347. register LINE   *dlp;
  348.  
  349. {
  350.         register int    c;
  351.         register int    col;
  352.         register int    newcol;
  353.         register int    dbo;
  354.  
  355.         col = 0;
  356.         dbo = 0;
  357.         while (dbo != llength(dlp)) {
  358.                 c = lgetc(dlp, dbo);
  359.                 newcol = col;
  360.                 if (c == '\t')
  361.             newcol += -(newcol % tabsize) + (tabsize - 1);
  362.                 else if (c<0x20 || c==0x7F)
  363.                         ++newcol;
  364.                 ++newcol;
  365.                 if (newcol > curgoal)
  366.                         break;
  367.                 col = newcol;
  368.                 ++dbo;
  369.         }
  370.         return(dbo);
  371. }
  372.  
  373. /*
  374.  * Scroll forward by a specified number of lines, or by a full page if no
  375.  * argument. Bound to "C-V". The "2" in the arithmetic on the window size is
  376.  * the overlap; this value is the default overlap value in ITS EMACS. Because
  377.  * this zaps the top line in the display window, we have to do a hard update.
  378.  */
  379. PASCAL NEAR forwpage(f, n)
  380.  
  381. int f,n;    /* prefix flag and argument */
  382.  
  383. {
  384.         register LINE   *lp;
  385.  
  386.         if (f == FALSE) {
  387.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  388.                 if (n <= 0)                     /* Forget the overlap   */
  389.                         n = 1;                  /* if tiny window.      */
  390.         } else if (n < 0)
  391.                 return(backpage(f, -n));
  392.         lp = curwp->w_linep;
  393.         while (n-- && lp!=curbp->b_linep)
  394.                 lp = lforw(lp);
  395.         curwp->w_linep = lp;
  396.         curwp->w_dotp  = lp;
  397.         curwp->w_doto  = 0;
  398.         curwp->w_flag |= WFHARD;
  399.         return(TRUE);
  400. }
  401.  
  402. /*
  403.  * This command is like "forwpage", but it goes backwards. The "2", like
  404.  * above, is the overlap between the two windows. The value is from the ITS
  405.  * EMACS manual. Bound to "M-V". We do a hard update for exactly the same
  406.  * reason.
  407.  */
  408. PASCAL NEAR backpage(f, n)
  409.  
  410. register int f;
  411. register int n;
  412.  
  413. {
  414.         register LINE   *lp;
  415.  
  416.         if (f == FALSE) {
  417.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  418.                 if (n <= 0)                     /* Don't blow up if the */
  419.                         n = 1;                  /* window is tiny.      */
  420.         } else if (n < 0)
  421.                 return(forwpage(f, -n));
  422.         lp = curwp->w_linep;
  423.         while (n-- && lback(lp)!=curbp->b_linep)
  424.                 lp = lback(lp);
  425.         curwp->w_linep = lp;
  426.         curwp->w_dotp  = lp;
  427.         curwp->w_doto  = 0;
  428.         curwp->w_flag |= WFHARD;
  429.         return(TRUE);
  430. }
  431.  
  432. /*
  433.  * Set the mark in the current window to the value of "." in the window. No
  434.  * errors are possible. Bound to "M-.".
  435.  */
  436. PASCAL NEAR setmark(f, n)
  437.  
  438. int f,n;    /* argument falg and num */
  439.  
  440. {
  441.     /* make sure it is in range */
  442.     if (f == FALSE)
  443.         n = 0;
  444.     n %= NMARKS;
  445.  
  446.         curwp->w_markp[n] = curwp->w_dotp;
  447.         curwp->w_marko[n] = curwp->w_doto;
  448.         mlwrite(TEXT9, n);
  449. /*              "[Mark %d set]" */
  450.         return(TRUE);
  451. }
  452.  
  453. /*
  454.  * Remove the mark in the current window.
  455.  * Bound to ^X <space> 
  456.  */
  457. PASCAL NEAR remmark(f, n)
  458.  
  459. int f,n;    /* argument falg and num */
  460.  
  461. {
  462.     /* make sure it is in range */
  463.     if (f == FALSE)
  464.         n = 0;
  465.     n %= NMARKS;
  466.  
  467.         curwp->w_markp[n] = NULL;
  468.         curwp->w_marko[n] = 0;
  469.         mlwrite(TEXT10, n);
  470. /*              "[Mark %d removed]" */
  471.         return(TRUE);
  472. }
  473.  
  474. /*
  475.  * Swap the values of "." and "mark" in the current window. This is pretty
  476.  * easy, bacause all of the hard work gets done by the standard routine
  477.  * that moves the mark about. The only possible error is "no mark". Bound to
  478.  * "C-X C-X".
  479.  */
  480. PASCAL NEAR swapmark(f, n)
  481.  
  482. int f,n;    /* argument falg and num */
  483.  
  484. {
  485.         register LINE   *odotp;
  486.         register int    odoto;
  487.  
  488.     /* make sure it is in range */
  489.     if (f == FALSE)
  490.         n = 0;
  491.     n %= NMARKS;
  492.  
  493.         if (curwp->w_markp[n] == NULL) {
  494.                 mlwrite(TEXT11, n);
  495. /*                      "No mark %d in this window" */
  496.                 return(FALSE);
  497.         }
  498.         odotp = curwp->w_dotp;
  499.         odoto = curwp->w_doto;
  500.         curwp->w_dotp  = curwp->w_markp[n];
  501.         curwp->w_doto  = curwp->w_marko[n];
  502.         curwp->w_markp[n] = odotp;
  503.         curwp->w_marko[n] = odoto;
  504.         curwp->w_flag |= WFMOVE;
  505.         return(TRUE);
  506. }
  507.  
  508. /*
  509.  * Goto a mark in the current window. This is pretty easy, bacause all of
  510.  * the hard work gets done by the standard routine that moves the mark
  511.  * about. The only possible error is "no mark". Bound to "M-^G".
  512.  */
  513. PASCAL NEAR gotomark(f, n)
  514.  
  515. int f, n;    /* default and nemeric args */
  516.  
  517. {
  518.     /* make sure it is in range */
  519.     if (f == FALSE)
  520.         n = 0;
  521.     n %= NMARKS;
  522.  
  523.         if (curwp->w_markp[n] == NULL) {
  524.                 mlwrite(TEXT11, n);
  525. /*                      "No mark %d in this window" */
  526.                 return(FALSE);
  527.         }
  528.         curwp->w_dotp  = curwp->w_markp[n];
  529.         curwp->w_doto  = curwp->w_marko[n];
  530.         curwp->w_flag |= WFMOVE;
  531.         return(TRUE);
  532. }
  533.  
  534. #if    DBCS
  535. /* advance a char if we are on the second byte of a DBCS character */
  536.  
  537. int PASCAL NEAR stopforw()
  538.  
  539. {
  540.     /* don't stop on the second byte of a 2 byte character */
  541.     if (curwp->w_doto > 0 && is2byte(curwp->w_dotp->l_text,
  542.         curwp->w_dotp->l_text + curwp->w_doto - 1))
  543.             return(forwchar(TRUE, 1));
  544.     return(TRUE);
  545. }
  546.  
  547. /* retreat a char if we are on the second byte of a DBCS character */
  548.  
  549. int PASCAL NEAR stopback()
  550.  
  551. {
  552.     /* don't stop on the second byte of a 2 byte character */
  553.     if (curwp->w_doto > 0 && is2byte(curwp->w_dotp->l_text,
  554.         curwp->w_dotp->l_text + curwp->w_doto - 1))
  555.             return(backchar(TRUE, 1));
  556.     return(TRUE);
  557. }
  558. #endif
  559.  
  560.