home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 001-099 / ff093.lzh / MicroEmacs / source / src.arc / random.c < prev    next >
C/C++ Source or Header  |  1987-08-16  |  28KB  |  1,067 lines

  1. /*
  2.  * This file contains the command processing functions for a number of random
  3.  * commands. There is no functional grouping here, for sure.
  4.  */
  5.  
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #include        "edef.h"
  9.  
  10. int     tabsize;                        /* Tab size (0: use real tabs)  */
  11.  
  12. /*
  13.  * Set fill column to n.
  14.  */
  15. setfillcol(f, n)
  16. {
  17.         fillcol = n;
  18.     mlwrite("[Fill column is %d]",n);
  19.         return(TRUE);
  20. }
  21.  
  22. /*
  23.  * Display the current position of the cursor, in origin 1 X-Y coordinates,
  24.  * the character that is under the cursor (in hex), and the fraction of the
  25.  * text that is before the cursor. The displayed column is not the current
  26.  * column, but the column that would be used on an infinite width display.
  27.  * Normally this is bound to "C-X =".
  28.  */
  29. showcpos(f, n)
  30. {
  31.         register LINE   *lp;        /* current line */
  32.         register long   numchars;    /* # of chars in file */
  33.         register int    numlines;    /* # of lines in file */
  34.         register long   predchars;    /* # chars preceding point */
  35.         register int    predlines;    /* # lines preceding point */
  36.         register int    curchar;    /* character under cursor */
  37.         int ratio;
  38.         int col;
  39.     int savepos;            /* temp save for current offset */
  40.     int ecol;            /* column pos/end of current line */
  41.  
  42.     /* starting at the beginning of the buffer */
  43.         lp = lforw(curbp->b_linep);
  44.  
  45.     /* start counting chars and lines */
  46.         numchars = 0;
  47.         numlines = 0;
  48.         while (lp != curbp->b_linep) {
  49.         /* if we are on the current line, record it */
  50.         if (lp == curwp->w_dotp) {
  51.             predlines = numlines;
  52.             predchars = numchars + curwp->w_doto;
  53.             if ((curwp->w_doto) == llength(lp))
  54.                 curchar = '\n';
  55.             else
  56.                 curchar = lgetc(lp, curwp->w_doto);
  57.         }
  58.         /* on to the next line */
  59.         ++numlines;
  60.         numchars += llength(lp) + 1;
  61.         lp = lforw(lp);
  62.         }
  63.  
  64.     /* if at end of file, record it */
  65.     if (curwp->w_dotp == curbp->b_linep) {
  66.         predlines = numlines;
  67.         predchars = numchars;
  68.     }
  69.  
  70.     /* Get real column and end-of-line column. */
  71.     col = getccol(FALSE);
  72.     savepos = curwp->w_doto;
  73.     curwp->w_doto = llength(curwp->w_dotp);
  74.     ecol = getccol(FALSE);
  75.     curwp->w_doto = savepos;
  76.  
  77.         ratio = 0;              /* Ratio before dot. */
  78.         if (numchars != 0)
  79.                 ratio = (100L*predchars) / numchars;
  80.  
  81.     /* summarize and report the info */
  82.     mlwrite("Line %d/%d Col %d/%d Char %D/%D (%d%%) char = 0x%x",
  83.         predlines+1, numlines+1, col, ecol,
  84.         predchars, numchars, ratio, curchar);
  85.         return (TRUE);
  86. }
  87.  
  88. getcline()    /* get the current line number */
  89.  
  90. {
  91.         register LINE   *lp;        /* current line */
  92.         register int    numlines;    /* # of lines before point */
  93.  
  94.     /* starting at the beginning of the buffer */
  95.         lp = lforw(curbp->b_linep);
  96.  
  97.     /* start counting lines */
  98.         numlines = 0;
  99.         while (lp != curbp->b_linep) {
  100.         /* if we are on the current line, record it */
  101.         if (lp == curwp->w_dotp)
  102.             break;
  103.         ++numlines;
  104.         lp = lforw(lp);
  105.         }
  106.  
  107.     /* and return the resulting count */
  108.     return(numlines + 1);
  109. }
  110.  
  111. /*
  112.  * Return current column.  Stop at first non-blank given TRUE argument.
  113.  */
  114. getccol(bflg)
  115. int bflg;
  116. {
  117.         register int c, i, col;
  118.         col = 0;
  119.         for (i=0; i<curwp->w_doto; ++i) {
  120.                 c = lgetc(curwp->w_dotp, i);
  121.                 if (c!=' ' && c!='\t' && bflg)
  122.                         break;
  123.                 if (c == '\t')
  124.                         col |= 0x07;
  125.                 else if (c<0x20 || c==0x7F)
  126.                         ++col;
  127.                 ++col;
  128.         }
  129.         return(col);
  130. }
  131.  
  132. /*
  133.  * Set current column.
  134.  */
  135. setccol(pos)
  136.  
  137. int pos;    /* position to set cursor */
  138.  
  139. {
  140.         register int c;        /* character being scanned */
  141.     register int i;        /* index into current line */
  142.     register int col;    /* current cursor column   */
  143.     register int llen;    /* length of line in bytes */
  144.  
  145.     col = 0;
  146.     llen = llength(curwp->w_dotp);
  147.  
  148.     /* scan the line until we are at or past the target column */
  149.     for (i = 0; i < llen; ++i) {
  150.         /* upon reaching the target, drop out */
  151.         if (col >= pos)
  152.             break;
  153.  
  154.         /* advance one character */
  155.                 c = lgetc(curwp->w_dotp, i);
  156.                 if (c == '\t')
  157.                         col |= 0x07;
  158.                 else if (c<0x20 || c==0x7F)
  159.                         ++col;
  160.                 ++col;
  161.         }
  162.     /* if not long enough... */
  163.     if (col < pos)
  164.         return(FALSE);
  165.  
  166.     /* otherwise...set us at the new position */
  167.     curwp->w_doto = i;
  168.     return(TRUE);
  169. }
  170.  
  171. /*
  172.  * Twiddle the two characters on either side of dot. If dot is at the end of
  173.  * the line twiddle the two characters before it. Return with an error if dot
  174.  * is at the beginning of line; it seems to be a bit pointless to make this
  175.  * work. This fixes up a very common typo with a single stroke. Normally bound
  176.  * to "C-T". This always works within a line, so "WFEDIT" is good enough.
  177.  */
  178. twiddle(f, n)
  179. {
  180.         register LINE   *dotp;
  181.         register int    doto;
  182.         register int    cl;
  183.         register int    cr;
  184.  
  185.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  186.         return(rdonly());    /* we are in read only mode    */
  187.         dotp = curwp->w_dotp;
  188.         doto = curwp->w_doto;
  189.         if (doto==llength(dotp) && --doto<0)
  190.                 return (FALSE);
  191.         cr = lgetc(dotp, doto);
  192.         if (--doto < 0)
  193.                 return (FALSE);
  194.         cl = lgetc(dotp, doto);
  195.         lputc(dotp, doto+0, cr);
  196.         lputc(dotp, doto+1, cl);
  197.         lchange(WFEDIT);
  198.         return (TRUE);
  199. }
  200.  
  201. /*
  202.  * Quote the next character, and insert it into the buffer. All the characters
  203.  * are taken literally, with the exception of the newline, which always has
  204.  * its line splitting meaning. The character is always read, even if it is
  205.  * inserted 0 times, for regularity. Bound to "C-Q"
  206.  */
  207. quote(f, n)
  208. {
  209.         register int    s;
  210.         register int    c;
  211.  
  212.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  213.         return(rdonly());    /* we are in read only mode    */
  214.         c = tgetc();
  215.         if (n < 0)
  216.                 return (FALSE);
  217.         if (n == 0)
  218.                 return (TRUE);
  219.         if (c == '\n') {
  220.                 do {
  221.                         s = lnewline();
  222.                 } while (s==TRUE && --n);
  223.                 return (s);
  224.         }
  225.         return (linsert(n, c));
  226. }
  227.  
  228. /*
  229.  * Set tab size if given non-default argument (n <> 1).  Otherwise, insert a
  230.  * tab into file.  If given argument, n, of zero, change to true tabs.
  231.  * If n > 1, simulate tab stop every n-characters using spaces. This has to be
  232.  * done in this slightly funny way because the tab (in ASCII) has been turned
  233.  * into "C-I" (in 10 bit code) already. Bound to "C-I".
  234.  */
  235. tab(f, n)
  236. {
  237.         if (n < 0)
  238.                 return (FALSE);
  239.         if (n == 0 || n > 1) {
  240.                 tabsize = n;
  241.                 return(TRUE);
  242.         }
  243.         if (! tabsize)
  244.                 return(linsert(1, '\t'));
  245.         return(linsert(tabsize - (getccol(FALSE) % tabsize), ' '));
  246. }
  247.  
  248. #if    AEDIT
  249. detab(f, n)        /* change tabs to spaces */
  250.  
  251. int f,n;    /* default flag and numeric repeat count */
  252.  
  253. {
  254.     register int inc;    /* increment to next line [sgn(n)] */
  255.  
  256.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  257.         return(rdonly());    /* we are in read only mode    */
  258.  
  259.     if (f == FALSE)
  260.         n = 1;
  261.  
  262.     /* loop thru detabbing n lines */
  263.     inc = ((n > 0) ? 1 : -1);
  264.     while (n) {
  265.         curwp->w_doto = 0;    /* start at the beginning */
  266.  
  267.         /* detab the entire current line */
  268.         while (curwp->w_doto < llength(curwp->w_dotp)) {
  269.             /* if we have a tab */
  270.             if (lgetc(curwp->w_dotp, curwp->w_doto) == '\t') {
  271.                 ldelete(1, FALSE);
  272.                 insspace(TRUE, 8 - (curwp->w_doto & 7));
  273.             }
  274.             forwchar(FALSE, 1);
  275.         }
  276.  
  277.         /* advance/or back to the next line */
  278.         forwline(TRUE, inc);
  279.         n -= inc;
  280.     }
  281.     curwp->w_doto = 0;    /* to the begining of the line */
  282.     thisflag &= ~CFCPCN;    /* flag that this resets the goal column */
  283.     lchange(WFEDIT);    /* yes, we have made at least an edit */
  284.     return(TRUE);
  285. }
  286.  
  287. entab(f, n)        /* change spaces to tabs where posible */
  288.  
  289. int f,n;    /* default flag and numeric repeat count */
  290.  
  291. {
  292.     register int inc;    /* increment to next line [sgn(n)] */
  293.     register int fspace;    /* pointer to first space if in a run */
  294.     register int ccol;    /* current cursor column */
  295.     register char cchar;    /* current character */
  296.  
  297.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  298.         return(rdonly());    /* we are in read only mode    */
  299.  
  300.     if (f == FALSE)
  301.         n = 1;
  302.  
  303.     /* loop thru entabbing n lines */
  304.     inc = ((n > 0) ? 1 : -1);
  305.     while (n) {
  306.         curwp->w_doto = 0;    /* start at the beginning */
  307.  
  308.         /* entab the entire current line */
  309.         fspace = -1;
  310.         ccol = 0;
  311.         while (curwp->w_doto < llength(curwp->w_dotp)) {
  312.             /* see if it is time to compress */
  313.             if ((fspace >= 0) && (nextab(fspace) <= ccol))
  314.                 if (ccol - fspace < 2)
  315.                     fspace = -1;
  316.                 else {
  317.         /* there is a bug here dealing with mixed space/tabed
  318.            lines.......it will get fixed        */
  319.                     backchar(TRUE, ccol - fspace);
  320.                     ldelete(ccol - fspace, FALSE);
  321.                     linsert(1, '\t');    
  322.                     fspace = -1;
  323.                 }
  324.  
  325.             /* get the current character */
  326.             cchar = lgetc(curwp->w_dotp, curwp->w_doto);
  327.  
  328.             switch (cchar) {
  329.                 case '\t': /* a tab...count em up */
  330.                     ccol = nextab(ccol);
  331.                     break;
  332.  
  333.                 case ' ':  /* a space...compress? */
  334.                     if (fspace == -1)
  335.                         fspace = ccol;
  336.                     ccol++;
  337.                     break;
  338.  
  339.                 default:   /* any other char...just count */
  340.                     ccol++;
  341.                     fspace = -1;
  342.                     break;
  343.             }
  344.             forwchar(FALSE, 1);
  345.         }
  346.  
  347.         /* advance/or back to the next line */
  348.         forwline(TRUE, inc);
  349.         n -= inc;
  350.     }
  351.     curwp->w_doto = 0;    /* to the begining of the line */
  352.     thisflag &= ~CFCPCN;    /* flag that this resets the goal column */
  353.     lchange(WFEDIT);    /* yes, we have made at least an edit */
  354.     return(TRUE);
  355. }
  356.  
  357. trim(f, n)    /* trim trailing whitespace from the point to eol */
  358.  
  359. int f,n;    /* default flag and numeric repeat count */
  360.  
  361. {
  362.     register LINE *lp;    /* current line pointer */
  363.     register int offset;    /* original line offset position */
  364.     register int length;    /* current length */
  365.     register int inc;    /* increment to next line [sgn(n)] */
  366.  
  367.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  368.         return(rdonly());    /* we are in read only mode    */
  369.  
  370.     if (f == FALSE)
  371.         n = 1;
  372.  
  373.     /* loop thru trimming n lines */
  374.     inc = ((n > 0) ? 1 : -1);
  375.     while (n) {
  376.         lp = curwp->w_dotp;        /* find current line text */
  377.         offset = curwp->w_doto;        /* save original offset */
  378.         length = lp->l_used;        /* find current length */
  379.  
  380.         /* trim the current line */
  381.         while (length > offset) {
  382.             if (lgetc(lp, length-1) != ' ' &&
  383.                 lgetc(lp, length-1) != '\t')
  384.                     break;
  385.             length--;
  386.         }
  387.         lp->l_used = length;
  388.  
  389.         /* advance/or back to the next line */
  390.         forwline(TRUE, inc);
  391.         n -= inc;
  392.     }
  393.     lchange(WFEDIT);
  394.     thisflag &= ~CFCPCN;    /* flag that this resets the goal column */
  395.     return(TRUE);
  396. }
  397. #endif
  398.  
  399. /*
  400.  * Open up some blank space. The basic plan is to insert a bunch of newlines,
  401.  * and then back up over them. Everything is done by the subcommand
  402.  * procerssors. They even handle the looping. Normally this is bound to "C-O".
  403.  */
  404. openline(f, n)
  405. {
  406.         register int    i;
  407.         register int    s;
  408.  
  409.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  410.         return(rdonly());    /* we are in read only mode    */
  411.         if (n < 0)
  412.                 return (FALSE);
  413.         if (n == 0)
  414.                 return (TRUE);
  415.         i = n;                                  /* Insert newlines.     */
  416.         do {
  417.                 s = lnewline();
  418.         } while (s==TRUE && --i);
  419.         if (s == TRUE)                          /* Then back up overtop */
  420.                 s = backchar(f, n);             /* of them all.         */
  421.         return (s);
  422. }
  423.  
  424. /*
  425.  * Insert a newline. Bound to "C-M". If we are in CMODE, do automatic
  426.  * indentation as specified.
  427.  */
  428. newline(f, n)
  429. {
  430.     register int    s;
  431.  
  432.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  433.         return(rdonly());    /* we are in read only mode    */
  434.     if (n < 0)
  435.         return (FALSE);
  436.  
  437.     /* if we are in C mode and this is a default <NL> */
  438.     if (n == 1 && (curbp->b_mode & MDCMOD) &&
  439.         curwp->w_dotp != curbp->b_linep)
  440.         return(cinsert());
  441.  
  442.         /*
  443.          * If a newline was typed, fill column is defined, the argument is non-
  444.          * negative, wrap mode is enabled, and we are now past fill column,
  445.      * and we are not read-only, perform word wrap.
  446.          */
  447.         if ((curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
  448.         getccol(FALSE) > fillcol &&
  449.         (curwp->w_bufp->b_mode & MDVIEW) == FALSE)
  450.         execute(META|SPEC|'W', FALSE, 1);
  451.  
  452.     /* insert some lines */
  453.     while (n--) {
  454.         if ((s=lnewline()) != TRUE)
  455.             return (s);
  456.     }
  457.     return (TRUE);
  458. }
  459.  
  460. cinsert()    /* insert a newline and indentation for C */
  461.  
  462. {
  463.     register char *cptr;    /* string pointer into text to copy */
  464.     register int tptr;    /* index to scan into line */
  465.     register int bracef;    /* was there a brace at the end of line? */
  466.     register int i;
  467.     char ichar[NSTRING];    /* buffer to hold indent of last line */
  468.  
  469.     /* grab a pointer to text to copy indentation from */
  470.     cptr = &curwp->w_dotp->l_text[0];
  471.  
  472.     /* check for a brace */
  473.     tptr = curwp->w_doto - 1;
  474.     bracef = (cptr[tptr] == '{');
  475.  
  476.     /* save the indent of the previous line */
  477.     i = 0;
  478.     while ((i < tptr) && (cptr[i] == ' ' || cptr[i] == '\t')
  479.         && (i < NSTRING - 1)) {
  480.         ichar[i] = cptr[i];
  481.         ++i;
  482.     }
  483.     ichar[i] = 0;        /* terminate it */
  484.  
  485.     /* put in the newline */
  486.     if (lnewline() == FALSE)
  487.         return(FALSE);
  488.  
  489.     /* and the saved indentation */
  490.     i = 0;
  491.     while (ichar[i])
  492.         linsert(1, ichar[i++]);
  493.  
  494.     /* and one more tab for a brace */
  495.     if (bracef)
  496.         tab(FALSE, 1);
  497.  
  498.     return(TRUE);
  499. }
  500.  
  501. insbrace(n, c)    /* insert a brace into the text here...we are in CMODE */
  502.  
  503. int n;    /* repeat count */
  504. int c;    /* brace to insert (always { for now) */
  505.  
  506. {
  507.     register int ch;    /* last character before input */
  508.     register int i;
  509.     register int target;    /* column brace should go after */
  510.  
  511.     /* if we are at the beginning of the line, no go */
  512.     if (curwp->w_doto == 0)
  513.         return(linsert(n,c));
  514.  
  515.     /* scan to see if all space before this is white space */
  516.     for (i = curwp->w_doto - 1; i >= 0; --i) {
  517.         ch = lgetc(curwp->w_dotp, i);
  518.         if (ch != ' ' && ch != '\t')
  519.             return(linsert(n, c));
  520.     }
  521.  
  522.     /* delete back first */
  523.     target = getccol(FALSE);    /* calc where we will delete to */
  524.     target -= 1;
  525.     target -= target % (tabsize == 0 ? 8 : tabsize);
  526.     while (getccol(FALSE) > target)
  527.         backdel(FALSE, 1);
  528.  
  529.     /* and insert the required brace(s) */
  530.     return(linsert(n, c));
  531. }
  532.  
  533. inspound()    /* insert a # into the text here...we are in CMODE */
  534.  
  535. {
  536.     register int ch;    /* last character before input */
  537.     register int i;
  538.  
  539.     /* if we are at the beginning of the line, no go */
  540.     if (curwp->w_doto == 0)
  541.         return(linsert(1,'#'));
  542.  
  543.     /* scan to see if all space before this is white space */
  544.     for (i = curwp->w_doto - 1; i >= 0; --i) {
  545.         ch = lgetc(curwp->w_dotp, i);
  546.         if (ch != ' ' && ch != '\t')
  547.             return(linsert(1, '#'));
  548.     }
  549.  
  550.     /* delete back first */
  551.     while (getccol(FALSE) >= 1)
  552.         backdel(FALSE, 1);
  553.  
  554.     /* and insert the required pound */
  555.     return(linsert(1, '#'));
  556. }
  557.  
  558. /*
  559.  * Delete blank lines around dot. What this command does depends if dot is
  560.  * sitting on a blank line. If dot is sitting on a blank line, this command
  561.  * deletes all the blank lines above and below the current line. If it is
  562.  * sitting on a non blank line then it deletes all of the blank lines after
  563.  * the line. Normally this command is bound to "C-X C-O". Any argument is
  564.  * ignored.
  565.  */
  566. deblank(f, n)
  567. {
  568.         register LINE   *lp1;
  569.         register LINE   *lp2;
  570.         long nld;
  571.  
  572.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  573.         return(rdonly());    /* we are in read only mode    */
  574.         lp1 = curwp->w_dotp;
  575.         while (llength(lp1)==0 && (lp2=lback(lp1))!=curbp->b_linep)
  576.                 lp1 = lp2;
  577.         lp2 = lp1;
  578.         nld = 0;
  579.         while ((lp2=lforw(lp2))!=curbp->b_linep && llength(lp2)==0)
  580.                 ++nld;
  581.         if (nld == 0)
  582.                 return (TRUE);
  583.         curwp->w_dotp = lforw(lp1);
  584.         curwp->w_doto = 0;
  585.         return (ldelete(nld, FALSE));
  586. }
  587.  
  588. /*
  589.  * Insert a newline, then enough tabs and spaces to duplicate the indentation
  590.  * of the previous line. Assumes tabs are every eight characters. Quite simple.
  591.  * Figure out the indentation of the current line. Insert a newline by calling
  592.  * the standard routine. Insert the indentation by inserting the right number
  593.  * of tabs and spaces. Return TRUE if all ok. Return FALSE if one of the
  594.  * subcomands failed. Normally bound to "C-J".
  595.  */
  596. indent(f, n)
  597. {
  598.         register int    nicol;
  599.         register int    c;
  600.         register int    i;
  601.  
  602.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  603.         return(rdonly());    /* we are in read only mode    */
  604.         if (n < 0)
  605.                 return (FALSE);
  606.         while (n--) {
  607.                 nicol = 0;
  608.                 for (i=0; i<llength(curwp->w_dotp); ++i) {
  609.                         c = lgetc(curwp->w_dotp, i);
  610.                         if (c!=' ' && c!='\t')
  611.                                 break;
  612.                         if (c == '\t')
  613.                                 nicol |= 0x07;
  614.                         ++nicol;
  615.                 }
  616.                 if (lnewline() == FALSE
  617.                 || ((i=nicol/8)!=0 && linsert(i, '\t')==FALSE)
  618.                 || ((i=nicol%8)!=0 && linsert(i,  ' ')==FALSE))
  619.                         return (FALSE);
  620.         }
  621.         return (TRUE);
  622. }
  623.  
  624. /*
  625.  * Delete forward. This is real easy, because the basic delete routine does
  626.  * all of the work. Watches for negative arguments, and does the right thing.
  627.  * If any argument is present, it kills rather than deletes, to prevent loss
  628.  * of text if typed with a big argument. Normally bound to "C-D".
  629.  */
  630. forwdel(f, n)
  631. {
  632.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  633.         return(rdonly());    /* we are in read only mode    */
  634.         if (n < 0)
  635.                 return (backdel(f, -n));
  636.         if (f != FALSE) {                       /* Really a kill.       */
  637.                 if ((lastflag&CFKILL) == 0)
  638.                         kdelete();
  639.                 thisflag |= CFKILL;
  640.         }
  641.         return (ldelete((long)n, f));
  642. }
  643.  
  644. /*
  645.  * Delete backwards. This is quite easy too, because it's all done with other
  646.  * functions. Just move the cursor back, and delete forwards. Like delete
  647.  * forward, this actually does a kill if presented with an argument. Bound to
  648.  * both "RUBOUT" and "C-H".
  649.  */
  650. backdel(f, n)
  651. {
  652.         register int    s;
  653.  
  654.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  655.         return(rdonly());    /* we are in read only mode    */
  656.         if (n < 0)
  657.                 return (forwdel(f, -n));
  658.         if (f != FALSE) {                       /* Really a kill.       */
  659.                 if ((lastflag&CFKILL) == 0)
  660.                         kdelete();
  661.                 thisflag |= CFKILL;
  662.         }
  663.         if ((s=backchar(f, n)) == TRUE)
  664.                 s = ldelete((long)n, f);
  665.         return (s);
  666. }
  667.  
  668. /*
  669.  * Kill text. If called without an argument, it kills from dot to the end of
  670.  * the line, unless it is at the end of the line, when it kills the newline.
  671.  * If called with an argument of 0, it kills from the start of the line to dot.
  672.  * If called with a positive argument, it kills from dot forward over that
  673.  * number of newlines. If called with a negative argument it kills backwards
  674.  * that number of newlines. Normally bound to "C-K".
  675.  */
  676. killtext(f, n)
  677. {
  678.         register LINE   *nextp;
  679.         long chunk;
  680.  
  681.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  682.         return(rdonly());    /* we are in read only mode    */
  683.         if ((lastflag&CFKILL) == 0)             /* Clear kill buffer if */
  684.                 kdelete();                      /* last wasn't a kill.  */
  685.         thisflag |= CFKILL;
  686.         if (f == FALSE) {
  687.                 chunk = llength(curwp->w_dotp)-curwp->w_doto;
  688.                 if (chunk == 0)
  689.                         chunk = 1;
  690.         } else if (n == 0) {
  691.                 chunk = curwp->w_doto;
  692.                 curwp->w_doto = 0;
  693.         } else if (n > 0) {
  694.                 chunk = llength(curwp->w_dotp)-curwp->w_doto+1;
  695.                 nextp = lforw(curwp->w_dotp);
  696.                 while (--n) {
  697.                         if (nextp == curbp->b_linep)
  698.                                 return (FALSE);
  699.                         chunk += llength(nextp)+1;
  700.                         nextp = lforw(nextp);
  701.                 }
  702.         } else {
  703.                 mlwrite("neg kill");
  704.                 return (FALSE);
  705.         }
  706.         return(ldelete(chunk, TRUE));
  707. }
  708.  
  709. setmode(f, n)    /* prompt and set an editor mode */
  710.  
  711. int f, n;    /* default and argument */
  712.  
  713. {
  714.     adjustmode(TRUE, FALSE);
  715. }
  716.  
  717. delmode(f, n)    /* prompt and delete an editor mode */
  718.  
  719. int f, n;    /* default and argument */
  720.  
  721. {
  722.     adjustmode(FALSE, FALSE);
  723. }
  724.  
  725. setgmode(f, n)    /* prompt and set a global editor mode */
  726.  
  727. int f, n;    /* default and argument */
  728.  
  729. {
  730.     adjustmode(TRUE, TRUE);
  731. }
  732.  
  733. delgmode(f, n)    /* prompt and delete a global editor mode */
  734.  
  735. int f, n;    /* default and argument */
  736.  
  737. {
  738.     adjustmode(FALSE, TRUE);
  739. }
  740.  
  741. adjustmode(kind, global)    /* change the editor mode status */
  742.  
  743. int kind;    /* true = set,        false = delete */
  744. int global;    /* true = global flag,    false = current buffer flag */
  745. {
  746.     register char *scan;        /* scanning pointer to convert prompt */
  747.     register int i;            /* loop index */
  748.     register status;        /* error return on input */
  749. #if    COLOR
  750.     register int uflag;        /* was modename uppercase?    */
  751. #endif
  752.     char prompt[50];    /* string to prompt user with */
  753.     char cbuf[NPAT];        /* buffer to recieve mode name into */
  754.  
  755.     /* build the proper prompt string */
  756.     if (global)
  757.         strcpy(prompt,"Global mode to ");
  758.     else
  759.         strcpy(prompt,"Mode to ");
  760.  
  761.     if (kind == TRUE)
  762.         strcat(prompt, "add: ");
  763.     else
  764.         strcat(prompt, "delete: ");
  765.  
  766.     /* prompt the user and get an answer */
  767.  
  768.     status = mlreply(prompt, cbuf, NPAT - 1);
  769.     if (status != TRUE)
  770.         return(status);
  771.  
  772.     /* make it uppercase */
  773.  
  774.     scan = cbuf;
  775. #if    COLOR
  776.     uflag = (*scan >= 'A' && *scan <= 'Z');
  777. #endif
  778.     while (*scan != 0) {
  779.         if (*scan >= 'a' && *scan <= 'z')
  780.             *scan = *scan - 32;
  781.         scan++;
  782.     }
  783.  
  784.     /* test it first against the colors we know */
  785.     for (i=0; i<NCOLORS; i++) {
  786.         if (strcmp(cbuf, cname[i]) == 0) {
  787.             /* finding the match, we set the color */
  788. #if    COLOR
  789.             if (uflag)
  790.                 if (global)
  791.                     gfcolor = i;
  792.                 else
  793.                     curwp->w_fcolor = i;
  794.             else
  795.                 if (global)
  796.                     gbcolor = i;
  797.                 else
  798.                     curwp->w_bcolor = i;
  799.  
  800.             curwp->w_flag |= WFCOLR;
  801. #endif
  802.             mlerase();
  803.             return(TRUE);
  804.         }
  805.     }
  806.  
  807.     /* test it against the modes we know */
  808.  
  809.     for (i=0; i < NUMMODES; i++) {
  810.         if (strcmp(cbuf, modename[i]) == 0) {
  811.             /* finding a match, we process it */
  812.             if (kind == TRUE)
  813.                 if (global)
  814.                     gmode |= (1 << i);
  815.                 else
  816.                     curwp->w_bufp->b_mode |= (1 << i);
  817.             else
  818.                 if (global)
  819.                     gmode &= ~(1 << i);
  820.                 else
  821.                     curwp->w_bufp->b_mode &= ~(1 << i);
  822.             /* display new mode line */
  823.             if (global == 0)
  824.                 upmode();
  825.             mlerase();    /* erase the junk */
  826.             return(TRUE);
  827.         }
  828.     }
  829.  
  830.     mlwrite("No such mode!");
  831.     return(FALSE);
  832. }
  833.  
  834. /*    This function simply clears the message line,
  835.         mainly for macro usage            */
  836.  
  837. clrmes(f, n)
  838.  
  839. int f, n;    /* arguments ignored */
  840.  
  841. {
  842.     mlwrite("");
  843.     return(TRUE);
  844. }
  845.  
  846. /*    This function writes a string on the message line
  847.         mainly for macro usage            */
  848.  
  849. writemsg(f, n)
  850.  
  851. int f, n;    /* arguments ignored */
  852.  
  853. {
  854.     register char *sp;    /* pointer into buf to expand %s */
  855.     register char *np;    /* ptr into nbuf */
  856.     register int status;
  857.     char buf[NPAT];        /* buffer to recieve message into */
  858.     char nbuf[NPAT*2];    /* buffer to expand string into */
  859.     int oldcmd;        /* discmd global save value */
  860.  
  861.     if ((status = mlreply("Message to write: ", buf, NPAT - 1)) != TRUE)
  862.         return(status);
  863.  
  864.     /* expand all '%' to "%%" so mlwrite won't expect arguments */
  865.     sp = buf;
  866.     np = nbuf;
  867.     while (*sp) {
  868.         *np++ = *sp;
  869.         if (*sp++ == '%')
  870.             *np++ = '%';
  871.     }
  872.     *np = '\0';
  873.  
  874.     /* save discmd and turn it on */
  875.     oldcmd = discmd;
  876.     discmd = TRUE;
  877.  
  878.     mlwrite(nbuf);
  879.     discmd = oldcmd;    /* restore discmd */
  880.     return(TRUE);
  881. }
  882.  
  883. #if    CFENCE
  884. /*    the cursor is moved to a matching fence    */
  885.  
  886. getfence(f, n)
  887.  
  888. int f, n;    /* not used */
  889.  
  890. {
  891.     register LINE *oldlp;    /* original line pointer */
  892.     register int oldoff;    /* and offset */
  893.     register int sdir;    /* direction of search (1/-1) */
  894.     register int count;    /* current fence level count */
  895.     register char ch;    /* fence type to match against */
  896.     register char ofence;    /* open fence */
  897.     register char c;    /* current character in scan */
  898.  
  899.     /* save the original cursor position */
  900.     oldlp = curwp->w_dotp;
  901.     oldoff = curwp->w_doto;
  902.  
  903.     /* get the current character */
  904.     if (oldoff == llength(oldlp))
  905.         ch = '\n';
  906.     else
  907.         ch = lgetc(oldlp, oldoff);
  908.  
  909.     /* setup proper matching fence */
  910.     switch (ch) {
  911.         case '(': ofence = ')'; sdir = FORWARD; break;
  912.         case '{': ofence = '}'; sdir = FORWARD; break;
  913.         case '[': ofence = ']'; sdir = FORWARD; break;
  914.         case ')': ofence = '('; sdir = REVERSE; break;
  915.         case '}': ofence = '{'; sdir = REVERSE; break;
  916.         case ']': ofence = '['; sdir = REVERSE; break;
  917.         default: TTbeep(); return(FALSE);
  918.     }
  919.  
  920.     /* set up for scan */
  921.     count = 1;
  922.     if (sdir == REVERSE)
  923.         backchar(FALSE, 1);
  924.     else
  925.         forwchar(FALSE, 1);
  926.  
  927.     /* scan until we find it, or reach the end of file */
  928.     while (count > 0) {
  929.         if (curwp->w_doto == llength(curwp->w_dotp))
  930.             c = '\n';
  931.         else
  932.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  933.         if (c == ch)
  934.             ++count;
  935.         if (c == ofence)
  936.             --count;
  937.         if (sdir == FORWARD)
  938.             forwchar(FALSE, 1);
  939.         else
  940.             backchar(FALSE, 1);
  941.         if (boundry(curwp->w_dotp, curwp->w_doto, sdir))
  942.             break;
  943.     }
  944.  
  945.     /* if count is zero, we have a match, move the sucker */
  946.     if (count == 0) {
  947.         if (sdir == FORWARD)
  948.             backchar(FALSE, 1);
  949.         else
  950.             forwchar(FALSE, 1);
  951.         curwp->w_flag |= WFMOVE;
  952.         return(TRUE);
  953.     }
  954.  
  955.     /* restore the current position */
  956.     curwp->w_dotp = oldlp;
  957.     curwp->w_doto = oldoff;
  958.     TTbeep();
  959.     return(FALSE);
  960. }
  961. #endif
  962.  
  963. /*    Close fences are matched against their partners, and if
  964.     on screen the cursor briefly lights there        */
  965.  
  966. fmatch(ch)
  967.  
  968. char ch;    /* fence type to match against */
  969.  
  970. {
  971.     register LINE *oldlp;    /* original line pointer */
  972.     register int oldoff;    /* and offset */
  973.     register LINE *toplp;    /* top line in current window */
  974.     register int count;    /* current fence level count */
  975.     register char opench;    /* open fence */
  976.     register char c;    /* current character in scan */
  977.     register int i;
  978.  
  979.     /* first get the display update out there */
  980.     update(FALSE);
  981.  
  982.     /* save the original cursor position */
  983.     oldlp = curwp->w_dotp;
  984.     oldoff = curwp->w_doto;
  985.  
  986.     /* setup proper open fence for passed close fence */
  987.     if (ch == ')')
  988.         opench = '(';
  989.     else if (ch == '}')
  990.         opench = '{';
  991.     else
  992.         opench = '[';
  993.  
  994.     /* find the top line and set up for scan */
  995.     toplp = curwp->w_linep->l_bp;
  996.     count = 1;
  997.     backchar(FALSE, 2);
  998.  
  999.     /* scan back until we find it, or reach past the top of the window */
  1000.     while (count > 0 && curwp->w_dotp != toplp) {
  1001.         if (curwp->w_doto == llength(curwp->w_dotp))
  1002.             c = '\n';
  1003.         else
  1004.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  1005.         if (c == ch)
  1006.             ++count;
  1007.         if (c == opench)
  1008.             --count;
  1009.         backchar(FALSE, 1);
  1010.         if (curwp->w_dotp == curwp->w_bufp->b_linep->l_fp &&
  1011.             curwp->w_doto == 0)
  1012.             break;
  1013.     }
  1014.  
  1015.     /* if count is zero, we have a match, display the sucker */
  1016.     /* there is a real machine dependant timing problem here we have
  1017.        yet to solve......... */
  1018.     if (count == 0) {
  1019.         forwchar(FALSE, 1);
  1020.         for (i = 0; i < term.t_pause; i++)
  1021.             update(FALSE);
  1022.     }
  1023.  
  1024.     /* restore the current position */
  1025.     curwp->w_dotp = oldlp;
  1026.     curwp->w_doto = oldoff;
  1027.     return(TRUE);
  1028. }
  1029.  
  1030. istring(f, n)    /* ask for and insert a string into the current
  1031.            buffer at the current point */
  1032.  
  1033. int f, n;    /* ignored arguments */
  1034.  
  1035. {
  1036.     register char *tp;    /* pointer into string to add */
  1037.     register int status;    /* status return code */
  1038.     char tstring[NPAT+1];    /* string to add */
  1039.  
  1040.     /* ask for string to insert */
  1041.     status = mlreplyt("String to insert<META>: ", tstring, NPAT, metac);
  1042.     if (status != TRUE)
  1043.         return(status);
  1044.  
  1045.     if (f == FALSE)
  1046.         n = 1;
  1047.  
  1048.     if (n < 0)
  1049.         n = - n;
  1050.  
  1051.     /* insert it */
  1052.     while (n--) {
  1053.         tp = &tstring[0];
  1054.         while (*tp) {
  1055.             if (*tp == 0x0a)
  1056.                 status = lnewline();
  1057.             else
  1058.                 status = linsert(1, *tp);
  1059.             ++tp;
  1060.             if (status != TRUE)
  1061.                 return(status);
  1062.         }
  1063.     }
  1064.  
  1065.     return(TRUE);
  1066. }
  1067.