From ee667b25ed0c3cea7c0e8acaaee8624127634c1e Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 1 Mar 2016 19:17:39 +0800 Subject: [PATCH] Move insbrace and fmatch to execute. Review execute. --- Makefile | 4 +- execute.c | 197 ++++++++++++++++++++++++++++++++++++++++++++++--- random.c | 214 +----------------------------------------------------- random.h | 2 - 4 files changed, 191 insertions(+), 226 deletions(-) diff --git a/Makefile b/Makefile index bdb6b05..3f79a21 100644 --- a/Makefile +++ b/Makefile @@ -153,8 +153,8 @@ exec.o: exec.c exec.h retcode.h buffer.h crypt.h line.h utf8.h bind.h \ display.h estruct.h eval.h file.h flook.h input.h random.h window.h \ defines.h execute.o: execute.c execute.h estruct.h bind.h random.h retcode.h \ - display.h file.h buffer.h crypt.h line.h utf8.h mlout.h window.h \ - defines.h + display.h file.h buffer.h crypt.h line.h utf8.h mlout.h search.h \ + terminal.h defines.h window.h file.o: file.c file.h buffer.h crypt.h line.h retcode.h utf8.h defines.h \ display.h estruct.h execute.h fileio.h input.h bind.h lock.h mlout.h \ window.h diff --git a/execute.c b/execute.c index 618f97a..2f4c1ff 100644 --- a/execute.c +++ b/execute.c @@ -2,6 +2,7 @@ #include "execute.h" #include +#include #include "estruct.h" #include "bind.h" @@ -9,6 +10,8 @@ #include "display.h" #include "file.h" #include "mlout.h" +#include "search.h" +#include "terminal.h" #include "window.h" int gasave = 256 ; /* global ASAVE size */ @@ -40,6 +43,162 @@ static int inspound( int n) { return linsert( n, '#') ; } +/* + * 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 + + /* * 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, @@ -51,7 +210,7 @@ int execute(int c, int f, int n) int status; fn_t execfunc; - /* if the keystroke is a bound function...do it */ +/* if the keystroke is a bound function...do it */ execfunc = getbind(c); if (execfunc != NULL) { thisflag = 0; @@ -60,6 +219,10 @@ int execute(int c, int f, int n) return status; } +/* keystroke not bound => self insert, check if buffer is read only */ + if (curbp->b_mode & MDVIEW) + return rdonly() ; + /* * 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, @@ -100,19 +263,27 @@ int execute(int c, int f, int n) ldelchar(1, FALSE); /* do the appropriate insertion */ - if (c == '}' && (curbp->b_mode & MDCMOD) != 0) - status = insbrace(n, c); - else if (c == '#' && (curbp->b_mode & MDCMOD) != 0) - status = inspound( n) ; - else - status = linsert(n, c); - + switch( c) { + case '}': + case ']': + case ')': + case '#': + if( (curbp->b_mode & MDCMOD) != 0) { + if( c == '#') + status = inspound( n) ; + else { + status = insbrace( n, c) ; #if CFENCE - /* check for CMODE fence matching */ - if ((c == '}' || c == ')' || c == ']') && - (curbp->b_mode & MDCMOD) != 0) - fmatch(c); + if( status == TRUE) + fmatch( c) ; /* check for CMODE fence matching */ #endif + } + + break ; + } + default: + status = linsert( n, c) ; + } /* check auto-save mode */ if (curbp->b_mode & MDASAVE) @@ -132,3 +303,5 @@ int execute(int c, int f, int n) return FALSE ; } + +/* end of execute.c */ diff --git a/random.c b/random.c index 1ad9bec..d03bd1e 100644 --- a/random.c +++ b/random.c @@ -1,8 +1,6 @@ /* random.c -- implements random.h */ #include "random.h" -#define NBRACE 1 /* new style brace matching command */ - /* random.c * * This file contains the command processing functions for a number of @@ -14,7 +12,6 @@ #include #include #include -#include #include "basic.h" #include "buffer.h" @@ -503,8 +500,6 @@ int openline(int f, int n) */ int insert_newline(int f, int n) { - int s; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ return rdonly(); /* we are in read only mode */ if (n < 0) @@ -527,6 +522,8 @@ int insert_newline(int f, int n) /* insert some lines */ while (n--) { + int s ; + if ((s = lnewline()) != TRUE) return s; #if SCROLLCODE @@ -553,7 +550,7 @@ static int cinsert(void) /* save the indent of the previous line */ nicol = 0 ; for( i = 0 ; i < tptr ; i += 1) { - char ch ; + int ch ; ch = cptr[ i] ; if( ch == ' ') @@ -563,7 +560,7 @@ static int cinsert(void) else break ; } - + if( i == tptr) { /* all line is blank */ curwp->w_doto = 0 ; /* gotobol */ lnewline() ; @@ -594,135 +591,6 @@ static int cinsert(void) return TRUE; } -#if NBRACE -/* - * insert a brace into the text here...we are in CMODE - * - * int n; repeat count - * int c; brace to insert (always } for now) - */ -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 we aren't at the beginning of the line... */ - if (curwp->w_doto != 0) - - /* scan to see if all space before this is white space */ - for (i = curwp->w_doto - 1; i >= 0; --i) { - ch = lgetc(curwp->w_dotp, i); - if (ch != ' ' && ch != '\t') - return linsert(n, c); - } - - /* chercher le caractere oppose correspondant */ - switch (c) { - case '}': - oc = '{'; - break; - case ']': - oc = '['; - break; - case ')': - oc = '('; - break; - default: - return FALSE; - } - - 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); -} - -#else - -/* - * insert a brace into the text here...we are in CMODE - * - * int n; repeat count - * int c; brace to insert (always { for now) - */ -int insbrace(int n, int c) -{ - int i; - int target; /* column brace should go after */ - - /* if we are at the beginning of the line, no go */ - if (curwp->w_doto == 0) - return linsert(n, c); - - /* scan to see if all space before this is white space */ - for (i = curwp->w_doto - 1; i >= 0; --i) { - int ch; /* last character before input */ - - ch = lgetc(curwp->w_dotp, i); - if (ch != ' ' && ch != '\t') - return linsert(n, c); - } - - /* delete back first */ - target = getccol(FALSE); /* calc where we will delete to */ - i = target % tabwidth ; - target -= ( i != 0) ? i : tabwidth ; - while (getccol(FALSE) > target) - backdel(FALSE, 1); - - /* and insert the required brace(s) */ - return linsert(n, c); -} -#endif /* * Delete blank lines around dot. What this command does depends if dot is @@ -1114,80 +982,6 @@ int getfence(int f, int n) } #endif -/* - * Close fences are matched against their partners, and if - * on screen the cursor briefly lights there - * - * char ch; fence type to match against - */ -int 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 */ - char opench; /* open fence */ - char c; /* current character in scan */ - int i; - - /* $tpause <= 0 disable fmatch */ - if( term.t_pause <= 0) - return TRUE ; - - /* 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)) { - 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) { - /* 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; - return TRUE; -} static int iovstring( int f, int n, const char *prompt, int (*fun)( char *)) { int status ; /* status return code */ diff --git a/random.h b/random.h index 0830368..f00e74c 100644 --- a/random.h +++ b/random.h @@ -34,7 +34,6 @@ int trim( int f, int n) ; #endif int openline( int f, int n) ; int insert_newline( int f, int n) ; -int insbrace( int n, int c) ; int deblank( int f, int n) ; int indent( int f, int n) ; int forwdel( int f, int n) ; @@ -45,7 +44,6 @@ int delmode( int f, int n) ; int setgmode( int f, int n) ; int delgmode( int f, int n) ; int getfence( int f, int n) ; -int fmatch( int ch) ; int istring( int f, int n) ; int ovstring( int f, int n) ;