home *** CD-ROM | disk | FTP | other *** search
- /*
- * This file contains the
- * command processing functions for
- * a number of random commands. There is
- * no functional grouping here, for sure.
- */
- #include <stdio.h>
- #include "ed.h"
-
- extern int kused;
- extern int gotobuf();
-
- /* mb: added function to toggle insert/overstrike */
-
- int instog(f,n)
- {
- if (ovrstrk)
- mlwrite("[insert]");
- else
- mlwrite("[overstrike]");
- ovrstrk = !ovrstrk;
- #if AtST
- Cursconf(ovrstrk?3:2,0); /* dal: non-blink cursor in ovrstrk */
- #endif
- return (TRUE);
- }
-
- /*
- * Set fill column to n. mb: added the ifs.
- */
- setfillcol(f, n)
- {
- if (n==1) n=0;
- if ( (n!=0) && (n < lmargin + tabsize) )
- {
- mlwrite("fillcol must be >= leftmargin + tabsize");
- return(ABORT);
- }
- fillcol = n;
- mlwrite(( n ? "[right margin: %d]" : "[wrap off]" ), n );
- return(TRUE);
- }
-
- /*
- * Set lmargin to n. mb: added.
- */
- setlmargin(f, n)
- {
- if (n==1) n=0;
- if ( (n!=0) && (n > fillcol - tabsize) )
- {
- mlwrite("lmargin must be <= fillcol - tabsize");
- return(ABORT);
- }
- lmargin = n;
- mlwrite( "[left margin: %d]", n );
- return(TRUE);
- }
-
- /*
- * Display the current position of the cursor,
- * in origin 1 X-Y coordinates, the character that is
- * under the cursor (in hex), and the fraction of the
- * text that is before the cursor. The displayed column
- * is not the current column, but the column that would
- * be used on an infinite width display. Normally this
- * is bound to "C-X =".
- * dal: added buffer space available display.
- */
- showcpos(f, n)
- {
- register LINE *clp;
- register int cbo;
- register long nch;
- register long nbc;
- register int nli; /* mb: line-no. counter */
- int cac, ratio, col; /* mb: were register */
- /* mb: deleted i,c; */
-
- clp = lforw(curbp->b_linep); /* Grovel the data. */
- cbo = 0;
- nch = 0;
- nbc = -1; /* mb: not-at-cursor-yet flag */
- nli = 0; /* mb: */
- for (;;)
- {
- if (clp==curwp->w_dotp && cbo==curwp->w_doto)
- {
- nbc = nch;
- if (cbo == llength(clp))
- cac = '\n';
- else
- cac = lgetc(clp, cbo);
- }
- if (cbo == llength(clp))
- {
- if (clp == curbp->b_linep)
- break;
- clp = lforw(clp);
- cbo = 0;
- if (nbc == -1) ++nli; /* mb: */
- }
- else
- ++cbo;
- ++nch;
- }
- col = getccol(FALSE); /* Get real column. */
- ratio = 0; /* Ratio before dot. */
- if (nch != 0)
- ratio = (100L*nbc) / nch;
- #ifdef AtST
- mlwrite("\
- X=%d Y=%d CH=0x%x .=%ld (%d%% of %ld) %ld bytes free, mouse dx=%d dy=%d",
- col+1, nli+1, cac, nbc, ratio, nch, memavail(), mcur_dx, mcur_dy);
- #else
- mlwrite("X=%d Y=%d CH=0x%x .=%ld (%d%% of %ld)",
- col+1, nli+1, cac, nbc, ratio, nch );
- #endif
- return (TRUE);
- }
-
- /*
- * Return current column. Stop at first non-blank given TRUE argument.
- */
- getccol(bflg)
- int bflg;
- {
- register int c, i, col;
- col = 0;
- for (i=0; i<curwp->w_doto; ++i)
- {
- c = lgetc(curwp->w_dotp, i);
- if (c!=' ' && c!='\t' && bflg)
- break;
- if (c == '\t')
- col += tabsize-(col%tabsize)-1; /* mb: */
- else if (iscntrl(c)) /* (c<0x20 || c==0x7F) */
- ++col;
- ++col;
- }
- return(col);
- }
-
- /*
- * Twiddle the two characters on either side of
- * dot. If dot is at the end of the line twiddle the
- * two characters before it. Return with an error if dot
- * is at the beginning of line; it seems to be a bit
- * pointless to make this work. This fixes up a very
- * common typo with a single stroke. Normally bound
- * to "C-T". This always works within a line, so
- * "WFEDIT" is good enough.
- */
- twiddle(f, n)
- {
- register LINE *dotp;
- register int doto;
- register int cl;
- register int cr;
-
- dotp = curwp->w_dotp;
- doto = curwp->w_doto;
- if (doto==llength(dotp) && --doto<0)
- return (FALSE);
- cr = lgetc(dotp, doto);
- if (--doto < 0)
- return (FALSE);
- cl = lgetc(dotp, doto);
- lputc(dotp, doto+0, cr);
- lputc(dotp, doto+1, cl);
- lchange(WFEDIT);
- return (TRUE);
- }
-
- /*
- * Set tab size if given non-default argument (n <> 1). Otherwise, insert a
- * tab into file. If given argument, n, of zero, change to true tabs.
- * This has to be done in this slightly funny way because the
- * tab (in ASCII) has been turned into "C-I" (in 10
- * bit code) already. Bound to "C-I".
- */
- tab(f, n) /* mb: simplified */
- register int n;
- {
- if (n < 1)
- return (FALSE);
- if (n > 1)
- {
- if (n != tabsize)
- {
- tabsize = n;
- sgarbf = TRUE;
- update();
- }
- mlwrite("[tabsize: %d]", n);
- return(TRUE);
- }
- /* if (n==1) */
- return(linsert(1, '\t', 0));
- }
-
- /*
- * Open up some blank space. The basic plan
- * is to insert a bunch of newlines, and then back
- * up over them. Everything is done by the subcommand
- * procerssors. They even handle the looping. Normally
- * this is bound to "C-O".
- */
- openline(f, n)
- {
- register int i;
- register int s;
-
- if (n < 0)
- return (FALSE);
- if (n == 0)
- return (TRUE);
- i = n; /* Insert newlines. */
- do
- {
- s = lnewline();
- }
- while ((s==TRUE) && (--i));
- if (s == TRUE) /* Then back up overtop */
- s = backchar(f, n); /* of them all. */
- return (s);
- }
-
- /*
- * dal: The following two functions handle swapping bindings for
- * ^M and ^J when in autoindent mode. The underlying functions
- * left untouched since they are called from places other than
- * just the key bindings.
- */
- ctrlm(f, n)
- int f, n;
- {
- return(autoindent ? indent(f, n) : newline(f, n));
- }
-
- ctrlj(f, n)
- int f, n;
- {
- return(autoindent ? newline(f, n) : indent(f, n));
- }
-
- /*
- * Insert a newline. Bound to "C-M".
- * If you are at the end of the line and the
- * next line is a blank line, just move into the
- * blank line. This makes "C-O" and "C-X C-O" work
- * nicely, and reduces the amount of screen
- * update that has to be done. This would not be
- * as critical if screen update were a lot
- * more efficient.
- */
- newline(f, n)
- int f, n;
- {
- register int nicol;
- register LINE *lp;
- register int s;
-
- if (n < 0)
- return (FALSE);
- while (n--)
- {
- lp = curwp->w_dotp;
- if ((llength(lp) == curwp->w_doto)
- && (lp != curbp->b_linep)
- && (llength(lforw(lp)) == 0))
- {
- if ((s=forwchar(FALSE, 1)) != TRUE)
- return (s);
- }
- else if ((s=lnewline()) != TRUE)
- return (s);
- }
- return (TRUE);
- }
-
- #if EXTRA
- /*
- * Delete blank lines around dot.
- * What this command does depends if dot is
- * sitting on a blank line. If dot is sitting on a
- * blank line, this command deletes all the blank lines
- * above and below the current line. If it is sitting
- * on a non blank line then it deletes all of the
- * blank lines after the line. Normally this command
- * is bound to "C-X C-O". Any argument is ignored.
- */
- deblank(f, n)
- {
- register LINE *lp1;
- register LINE *lp2;
- register int nld;
-
- lp1 = curwp->w_dotp;
- while (llength(lp1)==0 && (lp2=lback(lp1))!=curbp->b_linep)
- lp1 = lp2;
- lp2 = lp1;
- nld = 0;
- while ((lp2=lforw(lp2))!=curbp->b_linep && llength(lp2)==0)
- ++nld;
- if (nld == 0)
- return (TRUE);
- curwp->w_dotp = lforw(lp1);
- curwp->w_doto = 0;
- return (ldelete(nld, TRUE));
- }
- #endif
-
- /*
- * Insert a newline, then enough
- * tabs and spaces to duplicate the indentation
- * of the previous line. Figure out the indentation
- * of the current line. Insert a newline by calling
- * the standard routine. Insert the indentation by
- * inserting the right number of tabs and spaces.
- * Return TRUE if all ok. Return FALSE if one
- * of the subcomands failed. Normally bound
- * to "C-J".
- */
- indent(f, n)
- int f, n;
- {
- register int nicol;
- register int c;
- register int i;
-
- if (n < 0)
- return (FALSE);
- while (n--)
- {
- nicol = getccol(TRUE); /* mb: instead of doing it here */
- /* mb: 'real' tabs always: */
- if (lnewline() == FALSE
- || ((i=nicol/tabsize)!=0 && linsert(i, '\t', 0)==FALSE)
- || ((i=nicol%tabsize)!=0 && linsert(i, ' ', 0)==FALSE))
- return (FALSE);
- }
- return (TRUE);
- }
-
- /*
- * Delete forward. This is real
- * easy, because the basic delete routine does
- * all of the work. Watches for negative arguments,
- * and does the right thing. If any argument is
- * present, it kills rather than deletes, to prevent
- * loss of text if typed with a big argument.
- * Normally bound to "C-D".
- * mb: generalized for n<0.
- */
- forwdel(f, n)
- {
- int s;
-
- if (n < 0)
- {
- n = -n;
- if ((s=backchar(f, n)) != TRUE)
- return (s);
- }
- return (ldelete(n, f));
- }
-
- /*
- * mb: simplified.
- * Bound to both "RUBOUT" and "C-H".
- */
- backdel(f, n)
- {
- return (forwdel(f, -n));
- }
-
- /*
- * Kill text. If called without an argument,
- * it kills from dot to the end of the line, unless it
- * is at the end of the line, when it kills the newline.
- * If called with an argument of 0, it kills from the
- * start of the line to dot. If called with a positive
- * argument, it kills from dot forward over that number
- * of newlines. If called with a negative argument it
- * kills backwards that number of newlines. Normally
- * bound to "C-K".
- */
- kill(f, n)
- {
- register int chunk;
- register LINE *nextp;
-
- if (f == FALSE)
- {
- chunk = llength(curwp->w_dotp)-curwp->w_doto;
- if (chunk <= 0)
- chunk = 1;
- }
- else if (n == 0)
- {
- chunk = curwp->w_doto;
- curwp->w_doto = 0;
- }
- else if (n > 0)
- {
- chunk = llength(curwp->w_dotp)-curwp->w_doto+1;
- nextp = lforw(curwp->w_dotp);
- while (--n)
- {
- if (nextp == curbp->b_linep)
- return (FALSE);
- chunk += llength(nextp)+1;
- nextp = lforw(nextp);
- }
- }
- else
- {
- mlwrite("neg kill?");
- return (FALSE);
- }
- return (ldelete(chunk, TRUE));
- }
-
- /* mb: added.
- */
- bkill(f, n)
- {
- return (kill(TRUE, 0));
- }
-
- /*
- * Yank text back from the kill buffer. This
- * is really easy. All of the work is done by the
- * standard insert routines. All you do is run the loop,
- * and check for errors. Bound to "C-Y". The blank
- * lines are inserted with a call to "newline"
- * instead of a call to "lnewline" so that the magic
- * stuff that happens when you type a carriage
- * return also happens when a carriage return is
- * yanked back from the kill buffer.
- */
- yank(f, n)
- {
- register int c;
- register int i;
-
- if (n <= 0)
- return (FALSE);
- if( kused > 512 )
- mlwrite("[yanking...]"); /* mb: added */
- while (n--)
- {
- i = 0;
- while ((c=kremove(i++)) >= 0)
- {
- if (c == '\n')
- {
- if (newline(FALSE, 1) != TRUE)
- return (FALSE);
- }
- else
- {
- if (linsert(1, c, 0) != TRUE)
- return (FALSE);
- }
- }
- }
- thisflag |= CFYANK;
- if( kused > 512 )
- mlerase();
- return (TRUE);
- }
-
- /* mb: added.
- * Deletes backwards kused chars.
- * Bound to C-X C-Y.
- */
- unyank(f, n)
- {
- if ((lastflag&CFYANK) == 0) /* If last command wasn't Yank */
- return (FALSE);
- return (backdel(TRUE, kused));
- }
-
- /* mb: added.
- * Bound to E-Y.
- */
- flush_kbuf(f, n)
- {
- BUFFER *bp;
- BUFFER *tbp;
- int s;
-
- if ((bp=bfind("[]", TRUE, (BFTEMP|BFEDIT|BFCHG))) == NULL)
- return (FALSE);
- tbp = curbp;
- gotobuf(bp);
- s = yank(FALSE, 1);
- gotobuf(tbp);
- if (s == TRUE)
- {
- kdelete();
- mlwrite("[flushed]");
- }
- return (s);
- }
-