2013-10-08 04:39:15 -04:00
|
|
|
/* execute.c -- implements execute.h */
|
|
|
|
#include "execute.h"
|
|
|
|
|
2013-10-08 21:43:15 -04:00
|
|
|
#include <stdlib.h>
|
2016-03-01 06:17:39 -05:00
|
|
|
#include <unistd.h>
|
2013-10-08 21:43:15 -04:00
|
|
|
|
2013-10-09 01:43:32 -04:00
|
|
|
#include "estruct.h"
|
2013-06-03 23:52:28 -04:00
|
|
|
#include "bind.h"
|
|
|
|
#include "random.h"
|
|
|
|
#include "display.h"
|
|
|
|
#include "file.h"
|
2015-02-12 23:31:59 -05:00
|
|
|
#include "mlout.h"
|
2016-03-01 06:17:39 -05:00
|
|
|
#include "search.h"
|
|
|
|
#include "terminal.h"
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "window.h"
|
2013-06-03 23:52:28 -04:00
|
|
|
|
2013-10-08 04:39:15 -04:00
|
|
|
int gasave = 256 ; /* global ASAVE size */
|
|
|
|
int gacount = 256 ; /* count until next ASAVE */
|
|
|
|
|
2016-02-24 07:00:44 -05:00
|
|
|
|
|
|
|
/* insert a # into the text here...we are in CMODE */
|
2016-02-24 08:43:11 -05:00
|
|
|
static int inspound( int n) {
|
2016-02-24 07:00:44 -05:00
|
|
|
/* if we are at the beginning of the line, no go */
|
2016-02-24 08:43:11 -05:00
|
|
|
if( n == 1 && curwp->w_doto != 0) {
|
2016-02-24 07:00:44 -05:00
|
|
|
int i ;
|
|
|
|
|
|
|
|
/* scan to see if all space before this is white space */
|
|
|
|
for( i = curwp->w_doto - 1 ; i >= 0 ; i -= 1) {
|
|
|
|
int ch ; /* last character before input */
|
|
|
|
|
|
|
|
ch = lgetc( curwp->w_dotp, i) ;
|
|
|
|
if( ch != ' ' && ch != '\t')
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete back first */
|
|
|
|
if( i < 0)
|
|
|
|
while( getccol( FALSE) >= 1)
|
|
|
|
backdel( FALSE, 1) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and insert the required pound */
|
2016-02-24 08:43:11 -05:00
|
|
|
return linsert( n, '#') ;
|
2016-02-24 07:00:44 -05:00
|
|
|
}
|
|
|
|
|
2016-03-01 06:17:39 -05:00
|
|
|
/*
|
|
|
|
* insert a brace into the text here...we are in CMODE
|
|
|
|
*
|
|
|
|
* int n; repeat count
|
|
|
|
* int c; brace to insert (if not }, just normal insertion).
|
|
|
|
*/
|
|
|
|
static int insbrace( int n, int c) {
|
|
|
|
int ch ; /* last character before input */
|
|
|
|
int oc ; /* caractere oppose a c */
|
|
|
|
int i, count ;
|
|
|
|
int target ; /* column brace should go after */
|
|
|
|
struct line *oldlp ;
|
|
|
|
int oldoff ;
|
|
|
|
|
|
|
|
/* if not called with {, acts as insertion */
|
|
|
|
if( c == '}')
|
|
|
|
oc = '{' ;
|
|
|
|
else
|
|
|
|
return linsert( n, c) ;
|
|
|
|
|
|
|
|
/* scan to see if all preceding spaces are white spaces, if not, insert */
|
|
|
|
for( i = curwp->w_doto - 1 ; i >= 0 ; --i) {
|
|
|
|
ch = lgetc( curwp->w_dotp, i) ;
|
|
|
|
if( ch != ' ' && ch != '\t')
|
|
|
|
return linsert( n, c) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
oldlp = curwp->w_dotp ;
|
|
|
|
oldoff = curwp->w_doto ;
|
|
|
|
|
|
|
|
count = 1 ;
|
|
|
|
do {
|
|
|
|
if( boundry( curwp->w_dotp, curwp->w_doto, REVERSE)) {
|
|
|
|
/* at beginning of buffer, no match to be found */
|
|
|
|
curwp->w_dotp = oldlp ;
|
|
|
|
curwp->w_doto = oldoff ;
|
|
|
|
return linsert( n, c) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
backchar( FALSE, 1) ;
|
|
|
|
|
|
|
|
/* if not eol */
|
|
|
|
if( curwp->w_doto != llength( curwp->w_dotp)) {
|
|
|
|
ch = lgetc( curwp->w_dotp, curwp->w_doto) ;
|
|
|
|
if( ch == c)
|
|
|
|
++count ;
|
|
|
|
else if( ch == oc)
|
|
|
|
--count ;
|
|
|
|
}
|
|
|
|
} while( count > 0) ;
|
|
|
|
|
|
|
|
curwp->w_doto = 0 ; /* debut de ligne */
|
|
|
|
/* aller au debut de la ligne apres la tabulation */
|
|
|
|
while( (ch = lgetc( curwp->w_dotp, curwp->w_doto)) == ' '
|
|
|
|
|| ch == '\t')
|
|
|
|
forwchar( FALSE, 1) ;
|
|
|
|
|
|
|
|
/* delete back first */
|
|
|
|
target = getccol( FALSE) ; /* c'est l'indent que l'on doit avoir */
|
|
|
|
curwp->w_dotp = oldlp ;
|
|
|
|
curwp->w_doto = oldoff ;
|
|
|
|
|
|
|
|
while( target != getccol( FALSE)) {
|
|
|
|
if( target < getccol( FALSE)) /* on doit detruire des caracteres */
|
|
|
|
while( getccol( FALSE) > target)
|
|
|
|
backdel( FALSE, 1) ;
|
|
|
|
else { /* on doit en inserer */
|
|
|
|
while( target - getccol( FALSE) >= tabwidth)
|
|
|
|
insert_tab( FALSE, 1) ;
|
|
|
|
|
|
|
|
linsert( target - getccol( FALSE), ' ') ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and insert the required brace(s) */
|
|
|
|
return linsert( n, c) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CFENCE
|
|
|
|
/*
|
|
|
|
* Close fences are matched against their partners, and if
|
|
|
|
* on screen the cursor briefly lights there
|
|
|
|
*
|
|
|
|
* char ch; fence type to match against
|
|
|
|
*/
|
|
|
|
static void fmatch( int ch) {
|
|
|
|
struct line *oldlp ; /* original line pointer */
|
|
|
|
int oldoff ; /* and offset */
|
|
|
|
struct line *toplp ; /* top line in current window */
|
|
|
|
int count ; /* current fence level count */
|
|
|
|
int opench ; /* open fence */
|
|
|
|
|
|
|
|
/* $tpause <= 0 disable fmatch */
|
|
|
|
if( term.t_pause <= 0)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
/* first get the display update out there */
|
|
|
|
update( FALSE) ;
|
|
|
|
|
|
|
|
/* save the original cursor position */
|
|
|
|
oldlp = curwp->w_dotp ;
|
|
|
|
oldoff = curwp->w_doto ;
|
|
|
|
|
|
|
|
/* setup proper open fence for passed close fence */
|
|
|
|
if( ch == ')')
|
|
|
|
opench = '(' ;
|
|
|
|
else if( ch == '}')
|
|
|
|
opench = '{' ;
|
|
|
|
else
|
|
|
|
opench = '[' ;
|
|
|
|
|
|
|
|
/* find the top line and set up for scan */
|
|
|
|
toplp = curwp->w_linep->l_bp ;
|
|
|
|
backchar( FALSE, 1) ; /* . was after the }, move back */
|
|
|
|
|
|
|
|
/* scan back until we find it, or reach past the top of the window */
|
|
|
|
count = 1 ;
|
|
|
|
do {
|
|
|
|
/* At beginning of window or buffer, no match to be found */
|
|
|
|
if( curwp->w_dotp == toplp
|
|
|
|
|| boundry( curwp->w_dotp, curwp->w_doto, REVERSE))
|
|
|
|
break ;
|
|
|
|
|
|
|
|
backchar( FALSE, 1) ;
|
|
|
|
|
|
|
|
/* if not eol */
|
|
|
|
if( curwp->w_doto != llength(curwp->w_dotp)) {
|
|
|
|
int c ; /* current character in scan */
|
|
|
|
|
|
|
|
c = lgetc( curwp->w_dotp, curwp->w_doto) ;
|
|
|
|
if( c == ch)
|
|
|
|
++count ;
|
|
|
|
else if( c == opench)
|
|
|
|
--count ;
|
|
|
|
}
|
|
|
|
} while( count > 0) ;
|
|
|
|
|
|
|
|
/* if count is zero, we have a match, display the sucker */
|
|
|
|
if( count == 0) {
|
|
|
|
int i ;
|
|
|
|
|
|
|
|
/* there is a real machine dependant timing problem here we have
|
|
|
|
yet to solve......... */
|
|
|
|
for( i = 0 ; i < term.t_pause ; i++) {
|
|
|
|
update( FALSE) ;
|
|
|
|
usleep( 10000L) ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore the current position */
|
|
|
|
curwp->w_dotp = oldlp ;
|
|
|
|
curwp->w_doto = oldoff ;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-06-03 23:52:28 -04:00
|
|
|
/*
|
|
|
|
* This is the general command execution routine. It handles the fake binding
|
|
|
|
* of all the keys to "self-insert". It also clears out the "thisflag" word,
|
|
|
|
* and arranges to move it to the "lastflag", so that the next command can
|
|
|
|
* look at it. Return the status of command.
|
|
|
|
*/
|
|
|
|
int execute(int c, int f, int n)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
fn_t execfunc;
|
|
|
|
|
2016-03-01 06:17:39 -05:00
|
|
|
/* if the keystroke is a bound function...do it */
|
2013-06-03 23:52:28 -04:00
|
|
|
execfunc = getbind(c);
|
|
|
|
if (execfunc != NULL) {
|
|
|
|
thisflag = 0;
|
|
|
|
status = (*execfunc) (f, n);
|
|
|
|
lastflag = thisflag;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:17:39 -05:00
|
|
|
/* keystroke not bound => self insert, check if buffer is read only */
|
|
|
|
if (curbp->b_mode & MDVIEW)
|
|
|
|
return rdonly() ;
|
|
|
|
|
2013-06-03 23:52:28 -04:00
|
|
|
/*
|
|
|
|
* If a space was typed, fill column is defined, the argument is non-
|
|
|
|
* negative, wrap mode is enabled, and we are now past fill column,
|
|
|
|
* and we are not read-only, perform word wrap.
|
|
|
|
*/
|
|
|
|
if (c == ' ' && (curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
|
|
|
|
n >= 0 && getccol(FALSE) > fillcol &&
|
|
|
|
(curwp->w_bufp->b_mode & MDVIEW) == FALSE)
|
|
|
|
execute(META | SPEC | 'W', FALSE, 1);
|
|
|
|
|
|
|
|
#if PKCODE
|
|
|
|
if ((c >= 0x20 && c <= 0x7E) /* Self inserting. */
|
|
|
|
#if IBMPC
|
|
|
|
|| (c >= 0x80 && c <= 0xFE)) {
|
|
|
|
#else
|
|
|
|
#if VMS || BSD || USG /* 8BIT P.K. */
|
|
|
|
|| (c >= 0xA0 && c <= 0x10FFFF)) {
|
|
|
|
#else
|
|
|
|
) {
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
if ((c >= 0x20 && c <= 0xFF)) { /* Self inserting. */
|
|
|
|
#endif
|
|
|
|
if (n <= 0) { /* Fenceposts. */
|
|
|
|
lastflag = 0;
|
|
|
|
return n < 0 ? FALSE : TRUE;
|
|
|
|
}
|
|
|
|
thisflag = 0; /* For the future. */
|
|
|
|
|
|
|
|
/* if we are in overwrite mode, not at eol,
|
|
|
|
and next char is not a tab or we are at a tab stop,
|
|
|
|
delete a char forword */
|
|
|
|
if (curwp->w_bufp->b_mode & MDOVER &&
|
|
|
|
curwp->w_doto < curwp->w_dotp->l_used &&
|
|
|
|
(lgetc(curwp->w_dotp, curwp->w_doto) != '\t' ||
|
2016-02-24 00:22:54 -05:00
|
|
|
((curwp->w_doto) % tabwidth) == (tabwidth - 1)))
|
2013-06-03 23:52:28 -04:00
|
|
|
ldelchar(1, FALSE);
|
|
|
|
|
|
|
|
/* do the appropriate insertion */
|
2016-03-01 06:17:39 -05:00
|
|
|
switch( c) {
|
|
|
|
case '}':
|
|
|
|
case ']':
|
|
|
|
case ')':
|
|
|
|
case '#':
|
|
|
|
if( (curbp->b_mode & MDCMOD) != 0) {
|
|
|
|
if( c == '#')
|
|
|
|
status = inspound( n) ;
|
|
|
|
else {
|
|
|
|
status = insbrace( n, c) ;
|
2013-06-03 23:52:28 -04:00
|
|
|
#if CFENCE
|
2016-03-01 06:17:39 -05:00
|
|
|
if( status == TRUE)
|
|
|
|
fmatch( c) ; /* check for CMODE fence matching */
|
2013-06-03 23:52:28 -04:00
|
|
|
#endif
|
2016-03-01 06:17:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
status = linsert( n, c) ;
|
|
|
|
}
|
2013-06-03 23:52:28 -04:00
|
|
|
|
|
|
|
/* check auto-save mode */
|
|
|
|
if (curbp->b_mode & MDASAVE)
|
|
|
|
if (--gacount == 0) {
|
|
|
|
/* and save the file if needed */
|
|
|
|
upscreen(FALSE, 0);
|
|
|
|
filesave(FALSE, 0);
|
|
|
|
gacount = gasave;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastflag = thisflag;
|
|
|
|
return status;
|
|
|
|
}
|
2015-01-20 09:34:28 -05:00
|
|
|
|
|
|
|
lastflag = 0 ; /* Fake last flags. */
|
2015-02-12 23:31:59 -05:00
|
|
|
mloutfmt( "%B(Key not bound)") ; /* Complain */
|
2015-01-21 08:30:01 -05:00
|
|
|
return FALSE ;
|
2013-06-03 23:52:28 -04:00
|
|
|
}
|
|
|
|
|
2016-03-01 06:17:39 -05:00
|
|
|
|
|
|
|
/* end of execute.c */
|