From 92c9208cd447e75edd321c20574f63eeadd90e94 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 19 Jul 2021 15:39:00 +0800 Subject: [PATCH 01/37] start tagging uEMACS functions that are not compatible with view mode. --- .gitattributes | 1 + bind.c | 150 ++++++++------------- bind.h | 10 +- display.c | 4 +- display.h | 4 +- ebind.c | 54 ++++---- ebind.h | 18 ++- exec.c | 35 +++-- execute.c | 12 +- fnp_t.h | 7 + input.c | 46 +++++-- input.h | 9 +- names.c | 344 ++++++++++++++++++++++++------------------------- names.h | 17 ++- word.c | 15 ++- 15 files changed, 372 insertions(+), 354 deletions(-) create mode 100644 .gitattributes create mode 100644 fnp_t.h diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/bind.c b/bind.c index f7fddf9..7227b7d 100644 --- a/bind.c +++ b/bind.c @@ -31,14 +31,13 @@ #if APROP static int buildlist( char *mstring) ; -static int strinc( char *source, char *sub) ; #endif static void cmdstr( int c, char *seq) ; static unsigned int getckey( int mflag) ; static unsigned int stock( char *keyname) ; static int unbindchar( unsigned c) ; -static char *getfname( fn_t) ; +static const char *getfname( unsigned keycode, char *failmsg) ; int help(int f, int n) @@ -77,10 +76,9 @@ int help(int f, int n) return TRUE; } -int deskey(int f, int n) -{ /* describe the command for a certain key */ +int deskey( int f, int n) { +/* describe the command for a certain key */ int c; /* key to describe */ - char *ptr; /* string pointer to scan output strings */ char outseq[NSTRING]; /* output buffer for command sequence */ /* prompt the user to type us a key to describe */ @@ -96,12 +94,8 @@ int deskey(int f, int n) ostring(outseq); ostring(" "); - /* find the right ->function */ - if ((ptr = getfname(getbind(c))) == NULL) - ptr = "Not Bound"; - /* output the command sequence */ - ostring(ptr); + ostring( getfname( c, "Not Bound")) ; return TRUE; } @@ -114,7 +108,7 @@ int deskey(int f, int n) int bindtokey(int f, int n) { unsigned int c; /* command key to bind */ - fn_t kfunc; /* ptr to the requested function to bind to */ + fnp_t kfunc; /* ptr to the requested function to bind to */ struct key_tab *ktp; /* pointer into the command table */ int found; /* matched command flag */ char outseq[80]; /* output buffer for keystroke sequence */ @@ -123,7 +117,7 @@ int bindtokey(int f, int n) mlwrite(": bind-to-key "); /* get the function name to bind it to */ - kfunc = getname(); + kfunc = getname()->n_func ; if (kfunc == NULL) { mlwrite("(No such function)"); return FALSE; @@ -266,6 +260,35 @@ static int unbindchar( unsigned c) { return TRUE; } +#if APROP +/* + * does source include sub? + * + * char *source; string to search in + * char *sub; substring to look for + */ +static boolean strinc( const char *source, const char *sub) { + /* for each character in the source string */ + for( ; *source ; source++) { + const char *nxtsp ; /* next ptr into source */ + const char *tp ; /* ptr into substring */ + + nxtsp = source; + + /* is the substring here? */ + for( tp = sub ; *tp ; tp++) + if( *nxtsp++ != *tp) + break ; + + /* yes, return a success */ + if( *tp == 0) + return TRUE ; + } + + return FALSE ; +} +#endif + /* describe bindings * bring up a fake buffer and list the key bindings * into it with view mode @@ -299,7 +322,7 @@ static int buildlist( char *mstring) { #endif struct window *wp; /* scanning pointer to windows */ struct key_tab *ktp; /* pointer into the command table */ - struct name_bind *nptr; /* pointer into the name binding table */ + const name_bind *nptr;/* pointer into the name binding table */ struct buffer *bp; /* buffer to put binding list into */ char outseq[80]; /* output buffer for keystroke sequence */ @@ -339,7 +362,7 @@ static int buildlist( char *mstring) { wp->w_marko = 0; /* build the contents of this window, inserting it line by line */ - for( nptr = &names[ 0] ; nptr->n_func != NULL ; nptr++) { + for( nptr = names ; nptr->n_func != NULL ; nptr++) { int cpos ; /* current position to use in outseq */ #if APROP @@ -391,36 +414,6 @@ static int buildlist( char *mstring) { return TRUE; } -#if APROP - -/* - * does source include sub? - * - * char *source; string to search in - * char *sub; substring to look for - */ -static int strinc( char *source, char *sub) { - /* for each character in the source string */ - for( ; *source ; source++) { - char *nxtsp ; /* next ptr into source */ - char *tp ; /* ptr into substring */ - - nxtsp = source; - - /* is the substring here? */ - for( tp = sub ; *tp ; tp++) - if( *nxtsp++ != *tp) - break ; - - /* yes, return a success */ - if( *tp == 0) - return TRUE ; - } - - return FALSE ; -} -#endif - /* * get a command key sequence from the keyboard * @@ -512,57 +505,25 @@ static void cmdstr( int c, char *seq) { * * int c; key to find what is bound to it */ -fn_t getbind( unsigned c) { - struct key_tab *ktp; +fnp_t getbind( unsigned c) { + struct key_tab *ktp ; - ktp = &keytab[0]; /* Look in key table. */ - while (ktp->k_fp != NULL) { + for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) if (ktp->k_code == c) - return ktp->k_fp; - ++ktp; - } + return ktp->k_fp ; /* no such binding */ - return NULL; + return NULL ; } -/* - * getfname: - * This function takes a ptr to function and gets the name - * associated with it. - */ -static char *getfname(fn_t func) -{ - struct name_bind *nptr; /* pointer into the name binding table */ +static const char *getfname( unsigned keycode, char *failmsg) { +/* takes a key code and gets the name of the function bound to it */ + fnp_t func = getbind( keycode) ; + if( func == NULL) + return failmsg ; - /* skim through the table, looking for a match */ - nptr = &names[0]; - while (nptr->n_func != NULL) { - if (nptr->n_func == func) - return nptr->n_name; - ++nptr; - } - return NULL; -} - -/* - * match fname to a function in the names table - * and return any match or NULL if none - * - * char *fname; name to attempt to match - */ -int (*fncmatch(char *fname)) (int, int) -{ - struct name_bind *ffp; /* pointer to entry in name binding table */ - - /* scan through the table, returning any match */ - ffp = &names[0]; - while (ffp->n_func != NULL) { - if (strcmp(fname, ffp->n_name) == 0) - return ffp->n_func; - ++ffp; - } - return NULL; + const char *found = getnamebind( func)->n_name ; + return *found ? found : failmsg ; } /* @@ -618,15 +579,8 @@ static unsigned int stock( char *keyname) { /* * string key name to binding name.... * - * char *skey; name of keey to get binding for + * char *skey; name of key to get binding for */ -char *transbind(char *skey) -{ - char *bindname; - - bindname = getfname(getbind(stock(skey))); - if (bindname == NULL) - bindname = "ERROR"; - - return bindname; +const char *transbind( char *skey) { + return getfname( stock( skey), "ERROR") ; } diff --git a/bind.h b/bind.h index 7a543c1..141c3f4 100644 --- a/bind.h +++ b/bind.h @@ -1,23 +1,21 @@ #ifndef _BIND_H_ #define _BIND_H_ +#include "fnp_t.h" + #define APROP 1 /* Add code for Apropos command */ #if APROP int apro( int f, int n) ; #endif -/* Some global fuction declarations. */ -typedef int (*fn_t)(int, int); - int help( int f, int n) ; int deskey( int f, int n) ; int bindtokey( int f, int n) ; int unbindkey( int f, int n) ; int desbind( int f, int n) ; int startup( const char *fname) ; -fn_t getbind( unsigned keycode) ; -fn_t fncmatch( char *) ; -char *transbind( char *skey) ; +fnp_t getbind( unsigned keycode) ; +const char *transbind( char *skey) ; #endif diff --git a/display.c b/display.c index 3cd42d5..70f4b3c 100644 --- a/display.c +++ b/display.c @@ -1283,7 +1283,7 @@ static void mlputc( unicode_t c) { * * char *s; string to output */ -void ostring( char *s) { +void ostring( const char *s) { unsigned char c ; if( discmd) @@ -1553,7 +1553,7 @@ void echoc( unicode_t c) { * * char *s; string to output */ -void echos( char *s) { +void echos( const char *s) { unicode_t c ; if( disinp) diff --git a/display.h b/display.h index 0f94d1a..b244df1 100644 --- a/display.h +++ b/display.h @@ -27,9 +27,9 @@ void movecursor( int row, int col) ; void mlerase( void) ; void vmlwrite( const char *fmt, va_list ap) ; void mlwrite( const char *fmt, ...) ; -void ostring( char *s) ; +void ostring( const char *s) ; void echoc( unicode_t c) ; -void echos( char *s) ; +void echos( const char *s) ; void rubout( void) ; void getscreensize( int *widthp, int *heightp) ; diff --git a/ebind.c b/ebind.c index 04df09d..9f1c78a 100644 --- a/ebind.c +++ b/ebind.c @@ -34,19 +34,19 @@ * characters of the command. This explains the funny location of the * control-X commands. */ -struct key_tab keytab[NBINDS] = { +key_tab keytab[ NBINDS] = { {CONTROL | '?', backdel}, - {CONTROL | 'A', (fn_t) gotobol} + {CONTROL | 'A', (fnp_t) gotobol} , - {CONTROL | 'B', (fn_t) backchar} + {CONTROL | 'B', (fnp_t) backchar} , {CONTROL | 'C', insspace} , {CONTROL | 'D', forwdel} , - {CONTROL | 'E', (fn_t) gotoeol} + {CONTROL | 'E', (fnp_t) gotoeol} , - {CONTROL | 'F', (fn_t) forwchar} + {CONTROL | 'F', (fnp_t) forwchar} , {CONTROL | 'G', ctrlg} , @@ -62,11 +62,11 @@ struct key_tab keytab[NBINDS] = { , {CONTROL | 'M', insert_newline} , - {CONTROL | 'N', (fn_t) forwline} + {CONTROL | 'N', (fnp_t) forwline} , {CONTROL | 'O', openline} , - {CONTROL | 'P', (fn_t) backline} + {CONTROL | 'P', (fnp_t) backline} , {CONTROL | 'Q', quote} , @@ -74,11 +74,11 @@ struct key_tab keytab[NBINDS] = { , {CONTROL | 'S', forwsearch} , - {CONTROL | 'T', (fn_t) twiddle} + {CONTROL | 'T', (fnp_t) twiddle} , {CONTROL | 'U', unarg} , - {CONTROL | 'V', (fn_t) forwpage} + {CONTROL | 'V', (fnp_t) forwpage} , {CONTROL | 'W', killregion} , @@ -86,7 +86,7 @@ struct key_tab keytab[NBINDS] = { , {CONTROL | 'Y', yank} , - {CONTROL | 'Z', (fn_t) backpage} + {CONTROL | 'Z', (fnp_t) backpage} , {CONTROL | ']', metafn} , @@ -139,7 +139,7 @@ struct key_tab keytab[NBINDS] = { , {CTLX | CONTROL | 'W', filewrite} , - {CTLX | CONTROL | 'X', (fn_t) swapmark} + {CTLX | CONTROL | 'X', (fnp_t) swapmark} , {CTLX | CONTROL | 'Z', shrinkwind} , @@ -248,17 +248,17 @@ struct key_tab keytab[NBINDS] = { #endif {META | CONTROL | 'Z', scrnextup} , - {META | ' ', (fn_t) setmark} + {META | ' ', (fnp_t) setmark} , {META | '?', help} , {META | '!', reposition} , - {META | '.', (fn_t) setmark} + {META | '.', (fnp_t) setmark} , - {META | '>', (fn_t) gotoeob} + {META | '>', (fnp_t) gotoeob} , - {META | '<', (fn_t) gotobob} + {META | '<', (fnp_t) gotobob} , {META | '~', unmark} , @@ -309,7 +309,7 @@ struct key_tab keytab[NBINDS] = { #endif {META | 'U', upperword} , - {META | 'V', (fn_t) backpage} + {META | 'V', (fnp_t) backpage} , {META | 'W', copyregion} , @@ -319,33 +319,33 @@ struct key_tab keytab[NBINDS] = { , #if VT220 - {SPEC | '1', (fn_t) gotobob /* fisearch */} + {SPEC | '1', (fnp_t) gotobob /* fisearch */} , /* VT220 keys */ {SPEC | '2', yank} , {SPEC | '3', forwdel /* killregion */} , - {SPEC | '4', (fn_t) gotoeob /* setmark */} + {SPEC | '4', (fnp_t) gotoeob /* setmark */} , - {SPEC | '5', (fn_t) backpage} + {SPEC | '5', (fnp_t) backpage} , - {SPEC | '6', (fn_t) forwpage} + {SPEC | '6', (fnp_t) forwpage} , - {SPEC | 'A', (fn_t) backline} + {SPEC | 'A', (fnp_t) backline} , - {SPEC | 'B', (fn_t) forwline} + {SPEC | 'B', (fnp_t) forwline} , - {SPEC | 'C', (fn_t) forwchar} + {SPEC | 'C', (fnp_t) forwchar} , - {SPEC | 'D', (fn_t) backchar} + {SPEC | 'D', (fnp_t) backchar} , {SPEC | 'c', metafn} , - {SPEC | 'd', (fn_t) backchar} + {SPEC | 'd', (fnp_t) backchar} , - {SPEC | 'e', (fn_t) forwline} + {SPEC | 'e', (fnp_t) forwline} , - {SPEC | 'f', (fn_t) gotobob} + {SPEC | 'f', (fnp_t) gotobob} , {SPEC | 'h', help} , diff --git a/ebind.h b/ebind.h index 0b0f1d4..83dd710 100644 --- a/ebind.h +++ b/ebind.h @@ -1,9 +1,15 @@ +#ifndef _EBIND_H_ +#define _EBIND_H_ + +#include "fnp_t.h" + /* Structure for the table of initial key bindings. */ -struct key_tab { - unsigned k_code ; /* Key code */ - int (*k_fp)( int, int) ; /* Routine to handle it */ -} ; +typedef struct key_tab { + unsigned k_code ; /* Key code */ + fnp_t k_fp ; /* Routine to handle it */ +} key_tab ; -#define NBINDS 256 /* max # of bound keys */ -extern struct key_tab keytab[ NBINDS] ; /* key bind to functions table */ +#define NBINDS 256 /* max # of bound keys */ +extern key_tab keytab[ NBINDS] ; /* key bind to functions table */ +#endif diff --git a/exec.c b/exec.c index 6b01bd3..bef62b7 100644 --- a/exec.c +++ b/exec.c @@ -85,22 +85,23 @@ static int macarg( char *tok, int toksz) ; /* * Execute a named command even if it is not bound. */ -int namedcmd(int f, int n) -{ - fn_t kfunc; /* ptr to the requexted function to bind to */ - +int namedcmd( int f, int n) { /* prompt the user to type a named command */ mlwrite(": "); /* and now get the function name to execute */ - kfunc = getname(); + const name_bind *nbp = getname() ; + fnp_t kfunc = nbp->n_func ; if (kfunc == NULL) { mlwrite("(No such function)"); return FALSE; } + if( nbp->tag && (curbp->b_mode & MDVIEW)) + return rdonly() ; + /* and then execute the command */ - return kfunc(f, n); + return kfunc(f, n) ; } static int docmd( char *cline) ; @@ -145,7 +146,6 @@ int execcmd( int f, int n) { static int docmd( char *cline) { int f; /* default argument flag */ int n; /* numeric repeat value */ - fn_t fnc; /* function to execute */ int status; /* return status of function */ boolean oldcle ; /* old contents of clexec flag */ char *oldestr; /* original exec string */ @@ -187,19 +187,26 @@ static int docmd( char *cline) { } /* and match the token to see if it exists */ - if ((fnc = fncmatch(tkn)) == NULL) { + const name_bind *nbp = fncmatch( tkn) ; + fnp_t fnc = nbp->n_func ; + if( fnc == NULL) { mlwrite("(No such Function)"); execstr = oldestr; return FALSE; } - /* save the arguments and go execute the command */ - oldcle = clexec; /* save old clexec flag */ - clexec = TRUE; /* in cline execution */ - status = (*fnc) (f, n); /* call the function */ + if( nbp->tag && (curbp->b_mode & MDVIEW)) + status = rdonly() ; + else { + /* save the arguments and go execute the command */ + oldcle = clexec; /* save old clexec flag */ + clexec = TRUE; /* in cline execution */ + status = fnc( f, n) ; /* call the function */ + clexec = oldcle; /* restore clexec flag */ + execstr = oldestr; + } + cmdstatus = status; /* save the status */ - clexec = oldcle; /* restore clexec flag */ - execstr = oldestr; return status; } diff --git a/execute.c b/execute.c index 03fd733..365860f 100644 --- a/execute.c +++ b/execute.c @@ -210,13 +210,17 @@ static void fmatch( int ch) { */ int execute( int c, int f, int n) { int status ; - fn_t execfunc ; /* if the keystroke is a bound function...do it */ - execfunc = getbind( c) ; + fnp_t execfunc = getbind( c) ; if( execfunc != NULL) { thisflag = 0 ; - status = execfunc( f, n) ; + const name_bind *nbp = getnamebind( execfunc) ; + if( nbp->tag && curbp->b_mode & MDVIEW) + status = rdonly() ; + else + status = execfunc( f, n) ; + lastflag = thisflag ; return status ; } @@ -330,7 +334,7 @@ void kbd_loop( void) { newc = getcmd() ; update( FALSE) ; do { - fn_t execfunc ; + fnp_t execfunc ; if( c == newc && (execfunc = getbind( c)) != NULL diff --git a/fnp_t.h b/fnp_t.h new file mode 100644 index 0000000..87e50c0 --- /dev/null +++ b/fnp_t.h @@ -0,0 +1,7 @@ +#ifndef __FNP_T_H__ +#define __FNP_T_H__ + +/* Generic uEMACS function pointer type */ +typedef int (*fnp_t)( int, int) ; + +#endif diff --git a/input.c b/input.c index fe127b4..ec5f05e 100644 --- a/input.c +++ b/input.c @@ -157,17 +157,45 @@ int ectoc( int c) { return c ; } +/* + * match fname to a function in the names table + * and return any match or NULL if none + * + * char *fname; name to attempt to match + */ +const name_bind *fncmatch( char *fname) { + const name_bind *ffp ; /* pointer to entry in name binding table */ + + /* scan through the table, returning any match */ + for( ffp = names ; ffp->n_func != NULL ; ffp++) + if( strcmp( fname, ffp->n_name) == 0) + break ; + + return ffp ; +} + + +const name_bind *getnamebind( fnp_t func) { + const name_bind *nptr ; /* pointer into the name binding table */ + + /* skim through the table, looking for a match */ + for( nptr = names ; nptr->n_func != NULL ; nptr++) + if (nptr->n_func == func) + break ; + + return nptr ; +} + /* * get a command name from the command line. Command completion means * that pressing a will attempt to complete an unfinished command * name if it is unique. */ -fn_t getname(void) -{ +const name_bind *getname( void) { int cpos; /* current column on screen output */ - struct name_bind *ffp; /* first ptr to entry in name binding table */ - struct name_bind *cffp; /* current ptr to entry in name binding table */ - struct name_bind *lffp; /* last ptr to entry in name binding table */ + const name_bind *ffp; /* first ptr to entry in name binding table */ + const name_bind *cffp; /* current ptr to entry in name binding table */ + const name_bind *lffp; /* last ptr to entry in name binding table */ char buf[NSTRING]; /* buffer to hold tentative command name */ /* starting at the beginning of the string buffer */ @@ -177,7 +205,7 @@ fn_t getname(void) if (clexec) { if( TRUE != gettokval( buf, sizeof buf)) return NULL; - return fncmatch(&buf[0]); + return fncmatch( buf) ; } /* build a name string from the keyboard */ @@ -191,7 +219,7 @@ fn_t getname(void) buf[cpos] = 0; /* and match it off */ - return fncmatch(&buf[0]); + return fncmatch( buf) ; } else if (c == ectoc(abortc)) { /* Bell, abort */ ctrlg(FALSE, 0); @@ -217,7 +245,7 @@ fn_t getname(void) /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */ /* attempt a completion */ buf[cpos] = 0; /* terminate it for us */ - ffp = &names[0]; /* scan for matches */ + ffp = names ; /* scan for matches */ while (ffp->n_func != NULL) { if (strncmp(buf, ffp->n_name, strlen(buf)) == 0) { @@ -229,7 +257,7 @@ fn_t getname(void) /* no...we match, print it */ echos( ffp->n_name + cpos) ; TTflush(); - return ffp->n_func; + return ffp ; } else { /* << << << << << << << << << << << << << << << << << */ /* try for a partial match against the list */ diff --git a/input.h b/input.h index 487cb0f..2bc53de 100644 --- a/input.h +++ b/input.h @@ -1,7 +1,7 @@ #ifndef _INPUT_H_ #define _INPUT_H_ -#include "bind.h" +#include "names.h" typedef enum { @@ -26,7 +26,12 @@ int mlyesno( const char *prompt) ; int newmlarg( char **outbufref, const char *prompt, int size) ; int newmlargt( char **outbufref, const char *prompt, int size) ; int ectoc( int c) ; -fn_t getname( void) ; + +/* Look up in names to function association table */ +const name_bind *fncmatch( char *) ; +const name_bind *getname( void) ; +const name_bind *getnamebind( fnp_t func) ; + int tgetc( void) ; int get1key( void) ; int getcmd( void) ; diff --git a/names.c b/names.c index 7403f41..6530670 100644 --- a/names.c +++ b/names.c @@ -8,8 +8,6 @@ * function. */ -#include - #include "basic.h" #include "bind.h" #include "bindable.h" @@ -27,209 +25,211 @@ #include "window.h" #include "word.h" -struct name_bind names[] = { - {"abort-command", ctrlg}, - {"add-mode", setemode}, - {"add-global-mode", setgmode}, +const name_bind names[] = { + {"abort-command", ctrlg, 0} , + {"add-mode", setemode, 0} , + {"add-global-mode", setgmode, 0} , #if APROP - {"apropos", apro}, + {"apropos", apro, 0} , #endif - {"backward-character", (fn_t) backchar}, - {"begin-macro", ctlxlp}, - {"beginning-of-file", (fn_t) gotobob}, - {"beginning-of-line", (fn_t) gotobol}, - {"bind-to-key", bindtokey}, - {"buffer-position", showcpos}, - {"case-region-lower", lowerregion}, - {"case-region-upper", upperregion}, - {"case-word-capitalize", capword}, - {"case-word-lower", lowerword}, - {"case-word-upper", upperword}, - {"change-file-name", filename}, - {"change-screen-size", newsize}, - {"change-screen-width", newwidth}, - {"clear-and-redraw", redraw}, - {"clear-message-line", clrmes}, - {"copy-region", copyregion}, + {"backward-character", (fnp_t) backchar, 0} , + {"begin-macro", ctlxlp, 0} , + {"beginning-of-file", (fnp_t) gotobob, 0} , + {"beginning-of-line", (fnp_t) gotobol, 0} , + {"bind-to-key", bindtokey, 0} , + {"buffer-position", showcpos, 0} , + {"case-region-lower", lowerregion, 0} , + {"case-region-upper", upperregion, 0} , + {"case-word-capitalize", capword, 0} , + {"case-word-lower", lowerword, 0} , + {"case-word-upper", upperword, 0} , + {"change-file-name", filename, 0} , + {"change-screen-size", newsize, 0} , + {"change-screen-width", newwidth, 0} , + {"clear-and-redraw", redraw, 0} , + {"clear-message-line", clrmes, 0} , + {"copy-region", copyregion, 0} , #if WORDPRO - {"count-words", wordcount}, + {"count-words", wordcount, 0} , #endif - {"ctlx-prefix", cex}, - {"delete-blank-lines", deblank}, - {"delete-buffer", killbuffer}, - {"delete-mode", delmode}, - {"delete-global-mode", delgmode}, - {"delete-next-character", forwdel}, - {"delete-next-word", delfword}, - {"delete-other-windows", onlywind}, - {"delete-previous-character", backdel}, - {"delete-previous-word", delbword}, - {"delete-window", delwind}, - {"describe-bindings", desbind}, - {"describe-key", deskey}, + {"ctlx-prefix", cex, 0} , + {"delete-blank-lines", deblank, 0} , + {"delete-buffer", killbuffer, 0} , + {"delete-mode", delmode, 0} , + {"delete-global-mode", delgmode, 0} , + {"delete-next-character", forwdel, 0} , + {"delete-next-word", delfword, 0} , + {"delete-other-windows", onlywind, 0} , + {"delete-previous-character", backdel, 0} , + {"delete-previous-word", delbword, 0} , + {"delete-window", delwind, 0} , + {"describe-bindings", desbind, 0} , + {"describe-key", deskey, 0} , #if AEDIT - {"detab-line", detab}, + {"detab-line", detab, 0} , #endif - {"end-macro", ctlxrp}, - {"end-of-file", (fn_t) gotoeob}, - {"end-of-line", (fn_t) gotoeol}, + {"end-macro", ctlxrp, 0} , + {"end-of-file", (fnp_t) gotoeob, 0} , + {"end-of-line", (fnp_t) gotoeol, 0} , #if AEDIT - {"entab-line", entab}, + {"entab-line", entab, 0} , #endif - {"exchange-point-and-mark", (fn_t) swapmark}, - {"execute-buffer", execbuf}, - {"execute-command-line", execcmd}, - {"execute-file", execfile}, - {"execute-macro", ctlxe}, - {"execute-macro-1", cbuf1}, - {"execute-macro-2", cbuf2}, - {"execute-macro-3", cbuf3}, - {"execute-macro-4", cbuf4}, - {"execute-macro-5", cbuf5}, - {"execute-macro-6", cbuf6}, - {"execute-macro-7", cbuf7}, - {"execute-macro-8", cbuf8}, - {"execute-macro-9", cbuf9}, - {"execute-macro-10", cbuf10}, - {"execute-macro-11", cbuf11}, - {"execute-macro-12", cbuf12}, - {"execute-macro-13", cbuf13}, - {"execute-macro-14", cbuf14}, - {"execute-macro-15", cbuf15}, - {"execute-macro-16", cbuf16}, - {"execute-macro-17", cbuf17}, - {"execute-macro-18", cbuf18}, - {"execute-macro-19", cbuf19}, - {"execute-macro-20", cbuf20}, - {"execute-macro-21", cbuf21}, - {"execute-macro-22", cbuf22}, - {"execute-macro-23", cbuf23}, - {"execute-macro-24", cbuf24}, - {"execute-macro-25", cbuf25}, - {"execute-macro-26", cbuf26}, - {"execute-macro-27", cbuf27}, - {"execute-macro-28", cbuf28}, - {"execute-macro-29", cbuf29}, - {"execute-macro-30", cbuf30}, - {"execute-macro-31", cbuf31}, - {"execute-macro-32", cbuf32}, - {"execute-macro-33", cbuf33}, - {"execute-macro-34", cbuf34}, - {"execute-macro-35", cbuf35}, - {"execute-macro-36", cbuf36}, - {"execute-macro-37", cbuf37}, - {"execute-macro-38", cbuf38}, - {"execute-macro-39", cbuf39}, - {"execute-macro-40", cbuf40}, - {"execute-named-command", namedcmd}, + {"exchange-point-and-mark", (fnp_t) swapmark, 0} , + {"execute-buffer", execbuf, 0} , + {"execute-command-line", execcmd, 0} , + {"execute-file", execfile, 0} , + {"execute-macro", ctlxe, 0} , + {"execute-macro-1", cbuf1, 0} , + {"execute-macro-2", cbuf2, 0} , + {"execute-macro-3", cbuf3, 0} , + {"execute-macro-4", cbuf4, 0} , + {"execute-macro-5", cbuf5, 0} , + {"execute-macro-6", cbuf6, 0} , + {"execute-macro-7", cbuf7, 0} , + {"execute-macro-8", cbuf8, 0} , + {"execute-macro-9", cbuf9, 0} , + {"execute-macro-10", cbuf10, 0} , + {"execute-macro-11", cbuf11, 0} , + {"execute-macro-12", cbuf12, 0} , + {"execute-macro-13", cbuf13, 0} , + {"execute-macro-14", cbuf14, 0} , + {"execute-macro-15", cbuf15, 0} , + {"execute-macro-16", cbuf16, 0} , + {"execute-macro-17", cbuf17, 0} , + {"execute-macro-18", cbuf18, 0} , + {"execute-macro-19", cbuf19, 0} , + {"execute-macro-20", cbuf20, 0} , + {"execute-macro-21", cbuf21, 0} , + {"execute-macro-22", cbuf22, 0} , + {"execute-macro-23", cbuf23, 0} , + {"execute-macro-24", cbuf24, 0} , + {"execute-macro-25", cbuf25, 0} , + {"execute-macro-26", cbuf26, 0} , + {"execute-macro-27", cbuf27, 0} , + {"execute-macro-28", cbuf28, 0} , + {"execute-macro-29", cbuf29, 0} , + {"execute-macro-30", cbuf30, 0} , + {"execute-macro-31", cbuf31, 0} , + {"execute-macro-32", cbuf32, 0} , + {"execute-macro-33", cbuf33, 0} , + {"execute-macro-34", cbuf34, 0} , + {"execute-macro-35", cbuf35, 0} , + {"execute-macro-36", cbuf36, 0} , + {"execute-macro-37", cbuf37, 0} , + {"execute-macro-38", cbuf38, 0} , + {"execute-macro-39", cbuf39, 0} , + {"execute-macro-40", cbuf40, 0} , + {"execute-named-command", namedcmd, 0} , #if PROC - {"execute-procedure", execproc}, + {"execute-procedure", execproc, 0} , #endif - {"execute-program", execprg}, - {"exit-emacs", quit}, + {"execute-program", execprg, 0} , + {"exit-emacs", quit, 0} , #if WORDPRO - {"fill-paragraph", fillpara}, + {"fill-paragraph", fillpara, 1} , #endif - {"filter-buffer", filter_buffer}, - {"find-file", filefind}, - {"forward-character", (fn_t) forwchar}, - {"goto-line", gotoline}, + {"filter-buffer", filter_buffer, 0} , + {"find-file", filefind, 0} , + {"forward-character", (fnp_t) forwchar, 0} , + {"goto-line", gotoline, 0} , #if CFENCE - {"goto-matching-fence", getfence}, + {"goto-matching-fence", getfence, 0} , #endif - {"grow-window", enlargewind}, - {"handle-tab", insert_tab}, - {"hunt-forward", forwhunt}, - {"hunt-backward", backhunt}, - {"help", help}, - {"i-shell", spawncli}, + {"grow-window", enlargewind, 0} , + {"handle-tab", insert_tab, 0} , + {"hunt-forward", forwhunt, 0} , + {"hunt-backward", backhunt, 0} , + {"help", help, 0} , + {"i-shell", spawncli, 0} , #if ISRCH - {"incremental-search", fisearch}, + {"incremental-search", fisearch, 0} , #endif - {"insert-file", insfile}, - {"insert-space", insspace}, - {"insert-string", istring}, + {"insert-file", insfile, 0} , + {"insert-space", insspace, 0} , + {"insert-string", istring, 1} , #if WORDPRO #if PKCODE - {"justify-paragraph", justpara}, + {"justify-paragraph", justpara, 1} , #endif - {"kill-paragraph", killpara}, + {"kill-paragraph", killpara, 1} , #endif - {"kill-region", killregion}, - {"kill-to-end-of-line", killtext}, - {"list-buffers", listbuffers}, - {"meta-prefix", metafn}, - {"move-window-down", mvdnwind}, - {"move-window-up", mvupwind}, - {"name-buffer", namebuffer}, - {"newline", insert_newline}, - {"newline-and-indent", indent}, - {"next-buffer", nextbuffer}, - {"next-line", (fn_t) forwline}, - {"next-page", (fn_t) forwpage}, + {"kill-region", killregion, 0} , + {"kill-to-end-of-line", killtext, 0} , + {"list-buffers", listbuffers, 0} , + {"meta-prefix", metafn, 0} , + {"move-window-down", mvdnwind, 0} , + {"move-window-up", mvupwind, 0} , + {"name-buffer", namebuffer, 0} , + {"newline", insert_newline, 0} , + {"newline-and-indent", indent, 0} , + {"next-buffer", nextbuffer, 0} , + {"next-line", (fnp_t) forwline, 0} , + {"next-page", (fnp_t) forwpage, 0} , #if WORDPRO - {"next-paragraph", gotoeop}, + {"next-paragraph", gotoeop, 0} , #endif - {"next-window", nextwind}, - {"next-word", forwword}, - {"nop", nullproc}, - {"open-line", openline}, - {"overwrite-string", ovstring}, - {"pipe-command", pipecmd}, - {"previous-line", (fn_t) backline}, - {"previous-page", (fn_t) backpage}, + {"next-window", nextwind, 0} , + {"next-word", forwword, 0} , + {"nop", nullproc, 0} , + {"open-line", openline, 0} , + {"overwrite-string", ovstring, 0} , + {"pipe-command", pipecmd, 0} , + {"previous-line", (fnp_t) backline, 0} , + {"previous-page", (fnp_t) backpage, 0} , #if WORDPRO - {"previous-paragraph", gotobop}, + {"previous-paragraph", gotobop, 0} , #endif - {"previous-window", prevwind}, - {"previous-word", backword}, - {"query-replace-string", qreplace}, - {"quick-exit", quickexit}, - {"quote-character", quote}, - {"read-file", fileread}, - {"redraw-display", reposition}, - {"resize-window", resize}, - {"restore-window", restwnd}, - {"replace-string", sreplace}, + {"previous-window", prevwind, 0} , + {"previous-word", backword, 0} , + {"query-replace-string", qreplace, 0} , + {"quick-exit", quickexit, 0} , + {"quote-character", quote, 0} , + {"read-file", fileread, 0} , + {"redraw-display", reposition, 0} , + {"resize-window", resize, 0} , + {"restore-window", restwnd, 0} , + {"replace-string", sreplace, 0} , #if ISRCH - {"reverse-incremental-search", risearch}, + {"reverse-incremental-search", risearch, 0} , #endif #if PROC - {"run", execproc}, + {"run", execproc, 0} , #endif - {"save-file", filesave}, - {"save-window", savewnd}, - {"scroll-next-up", scrnextup}, - {"scroll-next-down", scrnextdw}, - {"search-forward", forwsearch}, - {"search-reverse", backsearch}, - {"select-buffer", usebuffer}, - {"set", setvar}, - {"set-fill-column", setfillcol}, - {"set-mark", (fn_t) setmark}, - {"shell-command", spawn}, - {"shrink-window", shrinkwind}, - {"split-current-window", splitwind}, - {"store-macro", storemac}, + {"save-file", filesave, 0} , + {"save-window", savewnd, 0} , + {"scroll-next-up", scrnextup, 0} , + {"scroll-next-down", scrnextdw, 0} , + {"search-forward", forwsearch, 0} , + {"search-reverse", backsearch, 0} , + {"select-buffer", usebuffer, 0} , + {"set", setvar, 0} , + {"set-fill-column", setfillcol, 0} , + {"set-mark", (fnp_t) setmark, 0} , + {"shell-command", spawn, 0} , + {"shrink-window", shrinkwind, 0} , + {"split-current-window", splitwind, 0} , + {"store-macro", storemac, 0} , #if PROC - {"store-procedure", storeproc}, + {"store-procedure", storeproc, 0} , #endif #if BSD | SVR4 - {"suspend-emacs", bktoshell}, + {"suspend-emacs", bktoshell, 0} , #endif - {"transpose-characters", (fn_t) twiddle}, + {"transpose-characters", (fnp_t) twiddle, 0} , #if AEDIT - {"trim-line", trim}, + {"trim-line", trim, 0} , #endif - {"unbind-key", unbindkey}, - {"universal-argument", unarg}, - {"unmark-buffer", unmark}, - {"update-screen", upscreen}, - {"view-file", viewfile}, - {"wrap-word", wrapword}, - {"write-file", filewrite}, - {"write-message", writemsg}, - {"yank", yank}, + {"unbind-key", unbindkey, 0} , + {"universal-argument", unarg, 0} , + {"unmark-buffer", unmark, 0} , + {"update-screen", upscreen, 0} , + {"view-file", viewfile, 0} , + {"wrap-word", wrapword, 0} , + {"write-file", filewrite, 0} , + {"write-message", writemsg, 0} , + {"yank", yank, 0} , - {"", NULL} + {"", NULL, 0} }; + +/* end of names.c */ diff --git a/names.h b/names.h index 700b304..6bc1944 100644 --- a/names.h +++ b/names.h @@ -1,8 +1,15 @@ +#ifndef _NAMES_H_ +#define _NAMES_H_ + +#include "fnp_t.h" + /* Structure for the name binding table. */ -struct name_bind { - char *n_name; /* name of function key */ - int (*n_func)(int, int); /* function name is bound to */ -}; +typedef struct name_bind { + const char *n_name ; /* name of function key */ + fnp_t n_func ; /* function name is bound to */ + char tag ; /* view mode compatibility tag */ +} name_bind ; -extern struct name_bind names[];/* name to function table */ +extern const name_bind names[] ; /* name to function table */ +#endif diff --git a/word.c b/word.c index adf4356..8f1d83c 100644 --- a/word.c +++ b/word.c @@ -10,6 +10,7 @@ * Modified by Petri Kutvonen */ +#include #include #include /* malloc, free */ #include /* memcpy */ @@ -358,8 +359,8 @@ static int parafillnjustify( int f, int n, int justify_f) { int dotflag = 0 ; /* was the last char a period? */ int leftmarg = 0 ; /* left marginal */ - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + if (fillcol == 0) { /* no fill column set */ mloutstr( "No fill column set") ; return FALSE; @@ -507,8 +508,6 @@ int justpara( int f, int n) { */ int killpara(int f, int n) { - int status; /* returned status of functions */ - while (n--) { /* for each paragraph to delete */ /* mark out the end and beginning of the para to delete */ @@ -523,13 +522,15 @@ int killpara(int f, int n) curwp->w_doto = 0; /* force us to the beginning of line */ /* and delete it */ - if ((status = killregion(FALSE, 1)) != TRUE) - return status; + int status = killregion( FALSE, 1) ; + if( status != TRUE) + return status ; /* and clean up the 2 extra lines */ ldelete(2L, TRUE); } - return TRUE; + + return TRUE ; } From 00b85fab9f6d699fefe2560d9cf990fdb126e354 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 19 Jul 2021 16:36:14 +0800 Subject: [PATCH 02/37] Fix warning triggered by enforcing const on function names table. --- eval.c | 6 +++--- eval.h | 2 +- exec.c | 3 +-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/eval.c b/eval.c index 9b17ddd..5b06a06 100644 --- a/eval.c +++ b/eval.c @@ -316,12 +316,12 @@ void varinit(void) * * @fname: name of function to evaluate. */ -static char *gtfun( char *fname) { +static const char *gtfun( char *fname) { unsigned fnum ; /* index to function to eval */ char *arg1 ; /* value of first argument */ char *arg2 ; /* value of second argument */ char *arg3 ; /* last argument */ - char *retstr ; /* return value */ + const char *retstr ; /* return value */ int low, high ; /* binary search indexes */ /* look the function up in the function table */ @@ -1206,7 +1206,7 @@ int is_it_cmd( char *token) { * * char *token; token to evaluate */ -char *getval(char *token) +const char *getval(char *token) { int status; /* error return */ struct buffer *bp; /* temp buffer pointer */ diff --git a/eval.h b/eval.h index 701c2b8..e6fac0d 100644 --- a/eval.h +++ b/eval.h @@ -19,7 +19,7 @@ int is_it_cmd( char *token) ; void varinit( void) ; int setvar( int f, int n) ; -char *getval( char *token) ; +const char *getval( char *token) ; int stol( char *val) ; char *mklower( char *str) ; diff --git a/exec.c b/exec.c index bef62b7..db51781 100644 --- a/exec.c +++ b/exec.c @@ -348,7 +348,6 @@ boolean gettokval( char *tok, int size) { char *getnewtokval( void) { char *tmpbuf ; - char *tmpval ; char *valbuf ; /* grab token and advance past */ @@ -357,7 +356,7 @@ char *getnewtokval( void) { return NULL ; /* evaluate it */ - tmpval = getval( tmpbuf) ; + const char *tmpval = getval( tmpbuf) ; valbuf = malloc( strlen( tmpval) + 1 ) ; if( valbuf != NULL) strcpy( valbuf, tmpval) ; From f0fe1ec19496d4fe2e979ab53abdaf8f24547faf Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 19 Jul 2021 16:37:57 +0800 Subject: [PATCH 03/37] Avoid extra empty line at EOF. --- screensize.cmd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/screensize.cmd b/screensize.cmd index f37eba7..0bef7f3 100644 --- a/screensize.cmd +++ b/screensize.cmd @@ -3,8 +3,7 @@ insert-string &cat $curwidth &cat "x" $pagelen insert-string &rig "---------+" &sub 10 $curcol &sub &div $curwidth 10 1 insert-string "---------+" insert-string &lef "1234567890" &mod $curwidth 10 -end-of-file -&sub $pagelen 3 execute-command-line "insert-string &cat $curline ~n" +&sub $pagelen 3 execute-command-line "insert-string &cat ~n &add $curline 1" beginning-of-file unmark-buffer write-message $line From 695b5d37da8b7d6e17e1b4960101231edb775b50 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 19 Jul 2021 20:50:32 +0800 Subject: [PATCH 04/37] Tag most view mode incompatible functions in function names table. --- file.c | 14 +++++++---- line.c | 23 +++++++++++------ names.c | 56 ++++++++++++++++++++--------------------- random.c | 76 +++++++++++++++++++++++++++++++++++++------------------- region.c | 19 +++++++++----- search.c | 8 +++--- spawn.c | 6 +++-- word.c | 22 ++++++++++------ 8 files changed, 138 insertions(+), 86 deletions(-) diff --git a/file.c b/file.c index 323f3c6..fbdfe84 100644 --- a/file.c +++ b/file.c @@ -11,6 +11,7 @@ * modified by Petri Kutvonen */ +#include #include #include #include @@ -101,8 +102,9 @@ int insfile( int f, int n) { if( restflag) /* don't allow this command if restricted */ return resterr() ; - if( curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly() ; /* we are in read only mode */ +// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly() ; /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; status = newmlarg( &fname, "Insert file: ", sizeof( fname_t)) ; if( status == TRUE) { @@ -119,7 +121,7 @@ int insfile( int f, int n) { /* * Select a file for editing. * Look around to see if you can find the - * fine in another buffer; if you can find it + * file in another buffer; if you can find it * just switch to the buffer. If you cannot find * the file, create a new buffer, read in the * text, and switch to the new buffer. @@ -476,8 +478,10 @@ int filewrite( int f, int n) { * get called by "C-Z". */ int filesave( int f, int n) { - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + if ((curbp->b_flag & BFCHG) == 0) /* Return, no changes. */ return TRUE; if (curbp->b_fname[0] == 0) { /* Must have a name. */ diff --git a/line.c b/line.c index 436d9a7..30bcb98 100644 --- a/line.c +++ b/line.c @@ -251,6 +251,7 @@ void lchange(int flag) */ int insspace(int f, int n) { + assert( !(curbp->b_mode & MDVIEW)) ; linsert(n, ' '); backchar(f, n); return TRUE; @@ -373,8 +374,9 @@ boolean linsert_byte( int n, int c) { int linsert( int n, unicode_t c) { assert( n >= 0) ; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; if( n > 0) { char utf8[ 4] ; @@ -453,8 +455,10 @@ int lnewline(void) int doto; struct window *wp; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + #if SCROLLCODE lchange(WFHARD | WFINS); #else @@ -540,8 +544,9 @@ boolean ldelete( long n, boolean kflag) { int chunk; struct window *wp; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; while( n > 0) { dotp = curwp->w_dotp; @@ -792,8 +797,10 @@ int yank(int f, int n) char *sp; /* pointer into string to insert */ struct kill *kp; /* pointer into kill buffer */ - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + if (n < 0) return FALSE; /* make sure there is something to yank */ diff --git a/names.c b/names.c index 6530670..85d7ffe 100644 --- a/names.c +++ b/names.c @@ -38,11 +38,11 @@ const name_bind names[] = { {"beginning-of-line", (fnp_t) gotobol, 0} , {"bind-to-key", bindtokey, 0} , {"buffer-position", showcpos, 0} , - {"case-region-lower", lowerregion, 0} , - {"case-region-upper", upperregion, 0} , - {"case-word-capitalize", capword, 0} , - {"case-word-lower", lowerword, 0} , - {"case-word-upper", upperword, 0} , + {"case-region-lower", lowerregion, 1} , + {"case-region-upper", upperregion, 1} , + {"case-word-capitalize", capword, 1} , + {"case-word-lower", lowerword, 1} , + {"case-word-upper", upperword, 1} , {"change-file-name", filename, 0} , {"change-screen-size", newsize, 0} , {"change-screen-width", newwidth, 0} , @@ -53,26 +53,26 @@ const name_bind names[] = { {"count-words", wordcount, 0} , #endif {"ctlx-prefix", cex, 0} , - {"delete-blank-lines", deblank, 0} , + {"delete-blank-lines", deblank, 1} , {"delete-buffer", killbuffer, 0} , {"delete-mode", delmode, 0} , {"delete-global-mode", delgmode, 0} , - {"delete-next-character", forwdel, 0} , - {"delete-next-word", delfword, 0} , + {"delete-next-character", forwdel, 1} , + {"delete-next-word", delfword, 1} , {"delete-other-windows", onlywind, 0} , - {"delete-previous-character", backdel, 0} , - {"delete-previous-word", delbword, 0} , + {"delete-previous-character", backdel, 1} , + {"delete-previous-word", delbword, 1} , {"delete-window", delwind, 0} , {"describe-bindings", desbind, 0} , {"describe-key", deskey, 0} , #if AEDIT - {"detab-line", detab, 0} , + {"detab-line", detab, 1} , #endif {"end-macro", ctlxrp, 0} , {"end-of-file", (fnp_t) gotoeob, 0} , {"end-of-line", (fnp_t) gotoeol, 0} , #if AEDIT - {"entab-line", entab, 0} , + {"entab-line", entab, 1} , #endif {"exchange-point-and-mark", (fnp_t) swapmark, 0} , {"execute-buffer", execbuf, 0} , @@ -128,7 +128,7 @@ const name_bind names[] = { #if WORDPRO {"fill-paragraph", fillpara, 1} , #endif - {"filter-buffer", filter_buffer, 0} , + {"filter-buffer", filter_buffer, 1} , {"find-file", filefind, 0} , {"forward-character", (fnp_t) forwchar, 0} , {"goto-line", gotoline, 0} , @@ -144,8 +144,8 @@ const name_bind names[] = { #if ISRCH {"incremental-search", fisearch, 0} , #endif - {"insert-file", insfile, 0} , - {"insert-space", insspace, 0} , + {"insert-file", insfile, 1} , + {"insert-space", insspace, 1} , {"insert-string", istring, 1} , #if WORDPRO #if PKCODE @@ -153,15 +153,15 @@ const name_bind names[] = { #endif {"kill-paragraph", killpara, 1} , #endif - {"kill-region", killregion, 0} , - {"kill-to-end-of-line", killtext, 0} , + {"kill-region", killregion, 1} , + {"kill-to-end-of-line", killtext, 1} , {"list-buffers", listbuffers, 0} , {"meta-prefix", metafn, 0} , {"move-window-down", mvdnwind, 0} , {"move-window-up", mvupwind, 0} , {"name-buffer", namebuffer, 0} , - {"newline", insert_newline, 0} , - {"newline-and-indent", indent, 0} , + {"newline", insert_newline, 1} , + {"newline-and-indent", indent, 1} , {"next-buffer", nextbuffer, 0} , {"next-line", (fnp_t) forwline, 0} , {"next-page", (fnp_t) forwpage, 0} , @@ -171,7 +171,7 @@ const name_bind names[] = { {"next-window", nextwind, 0} , {"next-word", forwword, 0} , {"nop", nullproc, 0} , - {"open-line", openline, 0} , + {"open-line", openline, 1} , {"overwrite-string", ovstring, 0} , {"pipe-command", pipecmd, 0} , {"previous-line", (fnp_t) backline, 0} , @@ -181,21 +181,21 @@ const name_bind names[] = { #endif {"previous-window", prevwind, 0} , {"previous-word", backword, 0} , - {"query-replace-string", qreplace, 0} , + {"query-replace-string", qreplace, 1} , {"quick-exit", quickexit, 0} , - {"quote-character", quote, 0} , - {"read-file", fileread, 0} , + {"quote-character", quote, 1} , + {"read-file", fileread, 1} , {"redraw-display", reposition, 0} , {"resize-window", resize, 0} , {"restore-window", restwnd, 0} , - {"replace-string", sreplace, 0} , + {"replace-string", sreplace, 1} , #if ISRCH {"reverse-incremental-search", risearch, 0} , #endif #if PROC {"run", execproc, 0} , #endif - {"save-file", filesave, 0} , + {"save-file", filesave, 1} , {"save-window", savewnd, 0} , {"scroll-next-up", scrnextup, 0} , {"scroll-next-down", scrnextdw, 0} , @@ -215,9 +215,9 @@ const name_bind names[] = { #if BSD | SVR4 {"suspend-emacs", bktoshell, 0} , #endif - {"transpose-characters", (fnp_t) twiddle, 0} , + {"transpose-characters", (fnp_t) twiddle, 1} , #if AEDIT - {"trim-line", trim, 0} , + {"trim-line", trim, 1} , #endif {"unbind-key", unbindkey, 0} , {"universal-argument", unarg, 0} , @@ -227,7 +227,7 @@ const name_bind names[] = { {"wrap-word", wrapword, 0} , {"write-file", filewrite, 0} , {"write-message", writemsg, 0} , - {"yank", yank, 0} , + {"yank", yank, 1} , {"", NULL, 0} }; diff --git a/random.c b/random.c index e8d8def..957a785 100644 --- a/random.c +++ b/random.c @@ -9,6 +9,7 @@ * Modified by Petri Kutvonen */ +#include #include #include #include @@ -226,8 +227,9 @@ boolean twiddle( int f, int n) { int len ; boolean eof_f = FALSE ; - if( curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly() ; /* we are in read only mode */ +// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly() ; /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; len = llength( curwp->w_dotp) ; if( len < 2 || curwp->w_doto == 0) /* at least 2 chars & not bol */ @@ -261,11 +263,13 @@ boolean twiddle( int f, int n) { */ int quote(int f, int n) { - int c; +// int c; +// +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ - c = tgetc(); + int c = tgetc(); if (n < 0) return FALSE; if (n == 0) @@ -315,8 +319,10 @@ int detab(int f, int n) { int inc; /* increment to next line [sgn(n)] */ - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + + assert( !(curbp->b_mode & MDVIEW)) ; if (f == FALSE) n = 1; @@ -363,8 +369,10 @@ int entab(int f, int n) int inc; /* increment to next line [sgn(n)] */ - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + + assert( !(curbp->b_mode & MDVIEW)) ; if (f == FALSE) n = 1; @@ -436,8 +444,10 @@ int trim(int f, int n) { int inc; /* increment to next line [sgn(n)] */ - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + + assert( !(curbp->b_mode & MDVIEW)) ; if (f == FALSE) n = 1; @@ -483,8 +493,10 @@ int openline(int f, int n) int i; int s; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + if (n < 0) return FALSE; if (n == 0) @@ -504,8 +516,10 @@ int openline(int f, int n) */ int insert_newline(int f, int n) { - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + if (n < 0) return FALSE; @@ -610,8 +624,10 @@ int deblank(int f, int n) struct line *lp2; long nld; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + lp1 = curwp->w_dotp; while (llength(lp1) == 0 && (lp2 = lback(lp1)) != curbp->b_linep) lp1 = lp2; @@ -638,8 +654,10 @@ int indent( int f, int n) { int nicol ; int i ; - if( curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly() ; /* we are in read only mode */ +// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly() ; /* we are in read only mode */ + + assert( !(curbp->b_mode & MDVIEW)) ; if( n < 0) return FALSE ; @@ -676,8 +694,10 @@ int indent( int f, int n) { * of text if typed with a big argument. Normally bound to "C-D". */ int forwdel( int f, int n) { - if( curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly() ; /* we are in read only mode */ +// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly() ; /* we are in read only mode */ + + assert( !(curbp->b_mode & MDVIEW)) ; if( n == 0) return TRUE ; @@ -700,8 +720,10 @@ int forwdel( int f, int n) { * both "RUBOUT" and "C-H". */ int backdel( int f, int n) { - if( curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly() ; /* we are in read only mode */ +// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly() ; /* we are in read only mode */ + + assert( !(curbp->b_mode & MDVIEW)) ; if( n == 0) return TRUE ; @@ -730,8 +752,10 @@ int killtext(int f, int n) struct line *nextp; long chunk; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + if ((lastflag & CFKILL) == 0) /* Clear kill buffer if */ kdelete(); /* last wasn't a kill. */ thisflag |= CFKILL; diff --git a/region.c b/region.c index de6c164..fcf5d71 100644 --- a/region.c +++ b/region.c @@ -10,6 +10,7 @@ * Modified by Petri Kutvonen */ +#include #include #include "buffer.h" @@ -30,8 +31,10 @@ int killregion(int f, int n) int s; struct region region; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + if ((s = getregion(®ion)) != TRUE) return s; if ((lastflag & CFKILL) == 0) /* This is a kill type */ @@ -94,8 +97,10 @@ int lowerregion(int f, int n) int s; struct region region; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + if ((s = getregion(®ion)) != TRUE) return s; lchange(WFHARD); @@ -131,8 +136,10 @@ int upperregion(int f, int n) int s; struct region region; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; + if ((s = getregion(®ion)) != TRUE) return s; lchange(WFHARD); diff --git a/search.c b/search.c index 6aa13ef..029becf 100644 --- a/search.c +++ b/search.c @@ -60,6 +60,7 @@ * Modified by Petri Kutvonen */ +#include #include #include #include @@ -832,12 +833,13 @@ static int replaces(int kind, int f, int n) struct line *lastline; /* position of last replace and */ int lastoff; /* offset (for 'u' query option) */ -/* rfi */ +///* rfi */ lastline = NULL ; lastoff = 0 ; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ +// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly(); /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; /* Check for negative repetitions. */ diff --git a/spawn.c b/spawn.c index 30ccc69..a073159 100644 --- a/spawn.c +++ b/spawn.c @@ -8,6 +8,7 @@ * Modified by Petri Kutvonen */ +#include #include #include #include @@ -289,8 +290,9 @@ int filter_buffer( int f, int n) { if( restflag) return resterr() ; - if( curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly() ; /* we are in read only mode */ +// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly() ; /* we are in read only mode */ + assert( !(curbp->b_mode & MDVIEW)) ; /* get the filter name and its args */ s = newmlarg( &mlarg, "#", 0) ; diff --git a/word.c b/word.c index 8f1d83c..52f2e0e 100644 --- a/word.c +++ b/word.c @@ -152,8 +152,10 @@ static boolean uniflip( boolean toupper_f) { /* flip unicode case and forward */ } static boolean capcapword( int n, boolean first_f, boolean rest_f) { - if( curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly() ; /* we are in read only mode */ +// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ +// return rdonly() ; /* we are in read only mode */ + + assert( !(curbp->b_mode & MDVIEW)) ; if( n < 0) return FALSE ; @@ -213,9 +215,11 @@ int delfword(int f, int n) int c; /* temp char */ long size; /* # of chars to delete */ - /* don't allow this command if we are in read only mode */ - if (curbp->b_mode & MDVIEW) - return rdonly(); +// /* don't allow this command if we are in read only mode */ +// if (curbp->b_mode & MDVIEW) +// return rdonly(); + + assert( !(curbp->b_mode & MDVIEW)) ; /* ignore the command if there is a negative argument */ if (n < 0) @@ -299,9 +303,11 @@ int delbword(int f, int n) { long size; - /* don't allow this command if we are in read only mode */ - if (curbp->b_mode & MDVIEW) - return rdonly(); +// /* don't allow this command if we are in read only mode */ +// if (curbp->b_mode & MDVIEW) +// return rdonly(); + + assert( !(curbp->b_mode & MDVIEW)) ; /* ignore the command if there is a nonpositive argument */ if (n <= 0) From 4f90e847f80c739c90b8712b5c4902c6226a17ed Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 20 Jul 2021 11:24:32 +0800 Subject: [PATCH 05/37] Tag uEMACS functions using first character of name string. --- bind.c | 6 +- exec.c | 4 +- execute.c | 4 +- input.c | 74 +++++------- input.h | 2 +- names.c | 346 +++++++++++++++++++++++++++--------------------------- names.h | 8 +- 7 files changed, 213 insertions(+), 231 deletions(-) diff --git a/bind.c b/bind.c index 7227b7d..839dc8c 100644 --- a/bind.c +++ b/bind.c @@ -368,11 +368,11 @@ static int buildlist( char *mstring) { #if APROP /* if we are executing an apropos command..... */ /* and current string doesn't include the search string */ - if( *mstring && strinc( nptr->n_name, mstring) == FALSE) + if( *mstring && strinc( bind_name( nptr), mstring) == FALSE) continue ; #endif /* add in the command name */ - mystrscpy( outseq, nptr->n_name, sizeof outseq) ; + mystrscpy( outseq, bind_name( nptr), sizeof outseq) ; cpos = strlen(outseq); /* search down any keys bound to this */ @@ -522,7 +522,7 @@ static const char *getfname( unsigned keycode, char *failmsg) { if( func == NULL) return failmsg ; - const char *found = getnamebind( func)->n_name ; + const char *found = getfncname( func) ; return *found ? found : failmsg ; } diff --git a/exec.c b/exec.c index db51781..3681c67 100644 --- a/exec.c +++ b/exec.c @@ -97,7 +97,7 @@ int namedcmd( int f, int n) { return FALSE; } - if( nbp->tag && (curbp->b_mode & MDVIEW)) + if( (bind_tag( nbp) & 1) && (curbp->b_mode & MDVIEW)) return rdonly() ; /* and then execute the command */ @@ -195,7 +195,7 @@ static int docmd( char *cline) { return FALSE; } - if( nbp->tag && (curbp->b_mode & MDVIEW)) + if( (bind_tag( nbp) & 1) && (curbp->b_mode & MDVIEW)) status = rdonly() ; else { /* save the arguments and go execute the command */ diff --git a/execute.c b/execute.c index 365860f..efe84b0 100644 --- a/execute.c +++ b/execute.c @@ -215,8 +215,8 @@ int execute( int c, int f, int n) { fnp_t execfunc = getbind( c) ; if( execfunc != NULL) { thisflag = 0 ; - const name_bind *nbp = getnamebind( execfunc) ; - if( nbp->tag && curbp->b_mode & MDVIEW) + const char *sp = getfncname( execfunc) ; + if( (sp[ -1] & 1) && (curbp->b_mode & MDVIEW)) status = rdonly() ; else status = execfunc( f, n) ; diff --git a/input.c b/input.c index ec5f05e..e0ecb01 100644 --- a/input.c +++ b/input.c @@ -168,14 +168,14 @@ const name_bind *fncmatch( char *fname) { /* scan through the table, returning any match */ for( ffp = names ; ffp->n_func != NULL ; ffp++) - if( strcmp( fname, ffp->n_name) == 0) + if( strcmp( fname, bind_name( ffp)) == 0) break ; return ffp ; } -const name_bind *getnamebind( fnp_t func) { +const char *getfncname( fnp_t func) { const name_bind *nptr ; /* pointer into the name binding table */ /* skim through the table, looking for a match */ @@ -183,7 +183,7 @@ const name_bind *getnamebind( fnp_t func) { if (nptr->n_func == func) break ; - return nptr ; + return bind_name( nptr) ; } /* @@ -243,60 +243,41 @@ const name_bind *getname( void) { } else if (c == ' ' || c == 0x1b || c == 0x09) { /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */ - /* attempt a completion */ - buf[cpos] = 0; /* terminate it for us */ - ffp = names ; /* scan for matches */ - while (ffp->n_func != NULL) { - if (strncmp(buf, ffp->n_name, strlen(buf)) - == 0) { + /* attempt a completion */ + buf[ cpos] = 0 ; /* terminate it for us */ + int buflen = strlen( buf) ; + /* scan for matches */ + for( ffp = names ; ffp->n_func != NULL ; ffp++) { + if( strncmp( buf, bind_name( ffp), buflen) == 0) { /* a possible match! More than one? */ - if ((ffp + 1)->n_func == NULL || - (strncmp - (buf, (ffp + 1)->n_name, - strlen(buf)) != 0)) { + if( (ffp + 1)->n_func == NULL || + (strncmp( buf, bind_name( ffp + 1), buflen) != 0)) { /* no...we match, print it */ - echos( ffp->n_name + cpos) ; - TTflush(); + echos( &bind_name( ffp)[ cpos]) ; + TTflush() ; return ffp ; } else { /* << << << << << << << << << << << << << << << << << */ /* try for a partial match against the list */ - /* first scan down until we no longer match the current input */ - lffp = (ffp + 1); - while ((lffp + - 1)->n_func != - NULL) { - if (strncmp - (buf, - (lffp + - 1)->n_name, - strlen(buf)) - != 0) - break; - ++lffp; - } + /* first scan down until we no longer match the + * current input */ + for( lffp = ffp + 1 ; (lffp + 1)->n_func != NULL ; + lffp++) + if( strncmp( buf, bind_name( lffp + 1), + buflen) != 0) + break ; - /* and now, attempt to partial complete the string, char at a time */ + /* and now, attempt to partial complete the string, + * one char at a time */ while (TRUE) { /* add the next char in */ - buf[cpos] = - ffp-> - n_name[cpos]; + buf[ cpos] = bind_name( ffp)[ cpos] ; /* scan through the candidates */ - cffp = ffp + 1; - while (cffp <= - lffp) { - if (cffp-> - n_name - [cpos] - != - buf - [cpos]) - goto onward; - ++cffp; - } + for( cffp = ffp + 1 ; cffp <= lffp ; cffp++) + if( bind_name( cffp)[ cpos] != buf[ cpos]) + goto onward ; /* add the character */ echoc( buf[ cpos++]) ; @@ -304,12 +285,11 @@ const name_bind *getname( void) { /* << << << << << << << << << << << << << << << << << */ } } - ++ffp; } /* no match.....beep and onward */ TTbeep(); - onward:; + onward: TTflush(); /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */ } else { diff --git a/input.h b/input.h index 2bc53de..041b306 100644 --- a/input.h +++ b/input.h @@ -30,7 +30,7 @@ int ectoc( int c) ; /* Look up in names to function association table */ const name_bind *fncmatch( char *) ; const name_bind *getname( void) ; -const name_bind *getnamebind( fnp_t func) ; +const char *getfncname( fnp_t func) ; int tgetc( void) ; int get1key( void) ; diff --git a/names.c b/names.c index 85d7ffe..f341e38 100644 --- a/names.c +++ b/names.c @@ -3,10 +3,10 @@ /* Name to function binding table. * - * This table gives the names of all the bindable functions - * and their C function address. These are used for the bind-to-key - * function. -*/ + * This table gives the names of all the bindable functions and their C + * function address. These are used for the bind-to-key function and macro + * processing. + */ #include "basic.h" #include "bind.h" @@ -26,210 +26,210 @@ #include "word.h" const name_bind names[] = { - {"abort-command", ctrlg, 0} , - {"add-mode", setemode, 0} , - {"add-global-mode", setgmode, 0} , + {" abort-command", ctrlg} , + {" add-mode", setemode} , + {" add-global-mode", setgmode} , #if APROP - {"apropos", apro, 0} , + {" apropos", apro} , #endif - {"backward-character", (fnp_t) backchar, 0} , - {"begin-macro", ctlxlp, 0} , - {"beginning-of-file", (fnp_t) gotobob, 0} , - {"beginning-of-line", (fnp_t) gotobol, 0} , - {"bind-to-key", bindtokey, 0} , - {"buffer-position", showcpos, 0} , - {"case-region-lower", lowerregion, 1} , - {"case-region-upper", upperregion, 1} , - {"case-word-capitalize", capword, 1} , - {"case-word-lower", lowerword, 1} , - {"case-word-upper", upperword, 1} , - {"change-file-name", filename, 0} , - {"change-screen-size", newsize, 0} , - {"change-screen-width", newwidth, 0} , - {"clear-and-redraw", redraw, 0} , - {"clear-message-line", clrmes, 0} , - {"copy-region", copyregion, 0} , + {" backward-character", (fnp_t) backchar} , + {" begin-macro", ctlxlp} , + {" beginning-of-file", (fnp_t) gotobob} , + {" beginning-of-line", (fnp_t) gotobol} , + {" bind-to-key", bindtokey} , + {" buffer-position", showcpos} , + {"!case-region-lower", lowerregion} , + {"!case-region-upper", upperregion} , + {"!case-word-capitalize", capword} , + {"!case-word-lower", lowerword} , + {"!case-word-upper", upperword} , + {" change-file-name", filename} , + {" change-screen-size", newsize} , + {" change-screen-width", newwidth} , + {" clear-and-redraw", redraw} , + {" clear-message-line", clrmes} , + {" copy-region", copyregion} , #if WORDPRO - {"count-words", wordcount, 0} , + {" count-words", wordcount} , #endif - {"ctlx-prefix", cex, 0} , - {"delete-blank-lines", deblank, 1} , - {"delete-buffer", killbuffer, 0} , - {"delete-mode", delmode, 0} , - {"delete-global-mode", delgmode, 0} , - {"delete-next-character", forwdel, 1} , - {"delete-next-word", delfword, 1} , - {"delete-other-windows", onlywind, 0} , - {"delete-previous-character", backdel, 1} , - {"delete-previous-word", delbword, 1} , - {"delete-window", delwind, 0} , - {"describe-bindings", desbind, 0} , - {"describe-key", deskey, 0} , + {" ctlx-prefix", cex} , + {"!delete-blank-lines", deblank} , + {" delete-buffer", killbuffer} , + {" delete-mode", delmode} , + {" delete-global-mode", delgmode} , + {"!delete-next-character", forwdel} , + {"!delete-next-word", delfword} , + {" delete-other-windows", onlywind} , + {"!delete-previous-character", backdel} , + {"!delete-previous-word", delbword} , + {" delete-window", delwind} , + {" describe-bindings", desbind} , + {" describe-key", deskey} , #if AEDIT - {"detab-line", detab, 1} , + {"!detab-line", detab} , #endif - {"end-macro", ctlxrp, 0} , - {"end-of-file", (fnp_t) gotoeob, 0} , - {"end-of-line", (fnp_t) gotoeol, 0} , + {" end-macro", ctlxrp} , + {" end-of-file", (fnp_t) gotoeob} , + {" end-of-line", (fnp_t) gotoeol} , #if AEDIT - {"entab-line", entab, 1} , + {"!entab-line", entab} , #endif - {"exchange-point-and-mark", (fnp_t) swapmark, 0} , - {"execute-buffer", execbuf, 0} , - {"execute-command-line", execcmd, 0} , - {"execute-file", execfile, 0} , - {"execute-macro", ctlxe, 0} , - {"execute-macro-1", cbuf1, 0} , - {"execute-macro-2", cbuf2, 0} , - {"execute-macro-3", cbuf3, 0} , - {"execute-macro-4", cbuf4, 0} , - {"execute-macro-5", cbuf5, 0} , - {"execute-macro-6", cbuf6, 0} , - {"execute-macro-7", cbuf7, 0} , - {"execute-macro-8", cbuf8, 0} , - {"execute-macro-9", cbuf9, 0} , - {"execute-macro-10", cbuf10, 0} , - {"execute-macro-11", cbuf11, 0} , - {"execute-macro-12", cbuf12, 0} , - {"execute-macro-13", cbuf13, 0} , - {"execute-macro-14", cbuf14, 0} , - {"execute-macro-15", cbuf15, 0} , - {"execute-macro-16", cbuf16, 0} , - {"execute-macro-17", cbuf17, 0} , - {"execute-macro-18", cbuf18, 0} , - {"execute-macro-19", cbuf19, 0} , - {"execute-macro-20", cbuf20, 0} , - {"execute-macro-21", cbuf21, 0} , - {"execute-macro-22", cbuf22, 0} , - {"execute-macro-23", cbuf23, 0} , - {"execute-macro-24", cbuf24, 0} , - {"execute-macro-25", cbuf25, 0} , - {"execute-macro-26", cbuf26, 0} , - {"execute-macro-27", cbuf27, 0} , - {"execute-macro-28", cbuf28, 0} , - {"execute-macro-29", cbuf29, 0} , - {"execute-macro-30", cbuf30, 0} , - {"execute-macro-31", cbuf31, 0} , - {"execute-macro-32", cbuf32, 0} , - {"execute-macro-33", cbuf33, 0} , - {"execute-macro-34", cbuf34, 0} , - {"execute-macro-35", cbuf35, 0} , - {"execute-macro-36", cbuf36, 0} , - {"execute-macro-37", cbuf37, 0} , - {"execute-macro-38", cbuf38, 0} , - {"execute-macro-39", cbuf39, 0} , - {"execute-macro-40", cbuf40, 0} , - {"execute-named-command", namedcmd, 0} , + {" exchange-point-and-mark", (fnp_t) swapmark} , + {" execute-buffer", execbuf} , + {" execute-command-line", execcmd} , + {" execute-file", execfile} , + {" execute-macro", ctlxe} , + {" execute-macro-1", cbuf1} , + {" execute-macro-2", cbuf2} , + {" execute-macro-3", cbuf3} , + {" execute-macro-4", cbuf4} , + {" execute-macro-5", cbuf5} , + {" execute-macro-6", cbuf6} , + {" execute-macro-7", cbuf7} , + {" execute-macro-8", cbuf8} , + {" execute-macro-9", cbuf9} , + {" execute-macro-10", cbuf10} , + {" execute-macro-11", cbuf11} , + {" execute-macro-12", cbuf12} , + {" execute-macro-13", cbuf13} , + {" execute-macro-14", cbuf14} , + {" execute-macro-15", cbuf15} , + {" execute-macro-16", cbuf16} , + {" execute-macro-17", cbuf17} , + {" execute-macro-18", cbuf18} , + {" execute-macro-19", cbuf19} , + {" execute-macro-20", cbuf20} , + {" execute-macro-21", cbuf21} , + {" execute-macro-22", cbuf22} , + {" execute-macro-23", cbuf23} , + {" execute-macro-24", cbuf24} , + {" execute-macro-25", cbuf25} , + {" execute-macro-26", cbuf26} , + {" execute-macro-27", cbuf27} , + {" execute-macro-28", cbuf28} , + {" execute-macro-29", cbuf29} , + {" execute-macro-30", cbuf30} , + {" execute-macro-31", cbuf31} , + {" execute-macro-32", cbuf32} , + {" execute-macro-33", cbuf33} , + {" execute-macro-34", cbuf34} , + {" execute-macro-35", cbuf35} , + {" execute-macro-36", cbuf36} , + {" execute-macro-37", cbuf37} , + {" execute-macro-38", cbuf38} , + {" execute-macro-39", cbuf39} , + {" execute-macro-40", cbuf40} , + {" execute-named-command", namedcmd} , #if PROC - {"execute-procedure", execproc, 0} , + {" execute-procedure", execproc} , #endif - {"execute-program", execprg, 0} , - {"exit-emacs", quit, 0} , + {" execute-program", execprg} , + {" exit-emacs", quit} , #if WORDPRO - {"fill-paragraph", fillpara, 1} , + {"!fill-paragraph", fillpara} , #endif - {"filter-buffer", filter_buffer, 1} , - {"find-file", filefind, 0} , - {"forward-character", (fnp_t) forwchar, 0} , - {"goto-line", gotoline, 0} , + {"!filter-buffer", filter_buffer} , + {" find-file", filefind} , + {" forward-character", (fnp_t) forwchar} , + {" goto-line", gotoline} , #if CFENCE - {"goto-matching-fence", getfence, 0} , + {" goto-matching-fence", getfence} , #endif - {"grow-window", enlargewind, 0} , - {"handle-tab", insert_tab, 0} , - {"hunt-forward", forwhunt, 0} , - {"hunt-backward", backhunt, 0} , - {"help", help, 0} , - {"i-shell", spawncli, 0} , + {" grow-window", enlargewind} , + {"!handle-tab", insert_tab} , + {" hunt-forward", forwhunt} , + {" hunt-backward", backhunt} , + {" help", help} , + {" i-shell", spawncli} , #if ISRCH - {"incremental-search", fisearch, 0} , + {" incremental-search", fisearch} , #endif - {"insert-file", insfile, 1} , - {"insert-space", insspace, 1} , - {"insert-string", istring, 1} , + {"!insert-file", insfile} , + {"!insert-space", insspace} , + {"!insert-string", istring} , #if WORDPRO #if PKCODE - {"justify-paragraph", justpara, 1} , + {"!justify-paragraph", justpara} , #endif - {"kill-paragraph", killpara, 1} , + {"!kill-paragraph", killpara} , #endif - {"kill-region", killregion, 1} , - {"kill-to-end-of-line", killtext, 1} , - {"list-buffers", listbuffers, 0} , - {"meta-prefix", metafn, 0} , - {"move-window-down", mvdnwind, 0} , - {"move-window-up", mvupwind, 0} , - {"name-buffer", namebuffer, 0} , - {"newline", insert_newline, 1} , - {"newline-and-indent", indent, 1} , - {"next-buffer", nextbuffer, 0} , - {"next-line", (fnp_t) forwline, 0} , - {"next-page", (fnp_t) forwpage, 0} , + {"!kill-region", killregion} , + {"!kill-to-end-of-line", killtext} , + {" list-buffers", listbuffers} , + {" meta-prefix", metafn} , + {" move-window-down", mvdnwind} , + {" move-window-up", mvupwind} , + {" name-buffer", namebuffer} , + {"!newline", insert_newline} , + {"!newline-and-indent", indent} , + {" next-buffer", nextbuffer} , + {" next-line", (fnp_t) forwline} , + {" next-page", (fnp_t) forwpage} , #if WORDPRO - {"next-paragraph", gotoeop, 0} , + {" next-paragraph", gotoeop} , #endif - {"next-window", nextwind, 0} , - {"next-word", forwword, 0} , - {"nop", nullproc, 0} , - {"open-line", openline, 1} , - {"overwrite-string", ovstring, 0} , - {"pipe-command", pipecmd, 0} , - {"previous-line", (fnp_t) backline, 0} , - {"previous-page", (fnp_t) backpage, 0} , + {" next-window", nextwind} , + {" next-word", forwword} , + {" nop", nullproc} , + {"!open-line", openline} , + {" overwrite-string", ovstring} , + {" pipe-command", pipecmd} , + {" previous-line", (fnp_t) backline} , + {" previous-page", (fnp_t) backpage} , #if WORDPRO - {"previous-paragraph", gotobop, 0} , + {" previous-paragraph", gotobop} , #endif - {"previous-window", prevwind, 0} , - {"previous-word", backword, 0} , - {"query-replace-string", qreplace, 1} , - {"quick-exit", quickexit, 0} , - {"quote-character", quote, 1} , - {"read-file", fileread, 1} , - {"redraw-display", reposition, 0} , - {"resize-window", resize, 0} , - {"restore-window", restwnd, 0} , - {"replace-string", sreplace, 1} , + {" previous-window", prevwind} , + {" previous-word", backword} , + {"!query-replace-string", qreplace} , + {" quick-exit", quickexit} , + {"!quote-character", quote} , + {"!read-file", fileread} , + {" redraw-display", reposition} , + {" resize-window", resize} , + {" restore-window", restwnd} , + {"!replace-string", sreplace} , #if ISRCH - {"reverse-incremental-search", risearch, 0} , + {" reverse-incremental-search", risearch} , #endif #if PROC - {"run", execproc, 0} , + {" run", execproc} , #endif - {"save-file", filesave, 1} , - {"save-window", savewnd, 0} , - {"scroll-next-up", scrnextup, 0} , - {"scroll-next-down", scrnextdw, 0} , - {"search-forward", forwsearch, 0} , - {"search-reverse", backsearch, 0} , - {"select-buffer", usebuffer, 0} , - {"set", setvar, 0} , - {"set-fill-column", setfillcol, 0} , - {"set-mark", (fnp_t) setmark, 0} , - {"shell-command", spawn, 0} , - {"shrink-window", shrinkwind, 0} , - {"split-current-window", splitwind, 0} , - {"store-macro", storemac, 0} , + {"!save-file", filesave} , + {" save-window", savewnd} , + {" scroll-next-up", scrnextup} , + {" scroll-next-down", scrnextdw} , + {" search-forward", forwsearch} , + {" search-reverse", backsearch} , + {" select-buffer", usebuffer} , + {" set", setvar} , + {" set-fill-column", setfillcol} , + {" set-mark", (fnp_t) setmark} , + {" shell-command", spawn} , + {" shrink-window", shrinkwind} , + {" split-current-window", splitwind} , + {" store-macro", storemac} , #if PROC - {"store-procedure", storeproc, 0} , + {" store-procedure", storeproc} , #endif #if BSD | SVR4 - {"suspend-emacs", bktoshell, 0} , + {" suspend-emacs", bktoshell} , #endif - {"transpose-characters", (fnp_t) twiddle, 1} , + {"!transpose-characters", (fnp_t) twiddle} , #if AEDIT - {"trim-line", trim, 1} , + {"!trim-line", trim} , #endif - {"unbind-key", unbindkey, 0} , - {"universal-argument", unarg, 0} , - {"unmark-buffer", unmark, 0} , - {"update-screen", upscreen, 0} , - {"view-file", viewfile, 0} , - {"wrap-word", wrapword, 0} , - {"write-file", filewrite, 0} , - {"write-message", writemsg, 0} , - {"yank", yank, 1} , + {" unbind-key", unbindkey} , + {" universal-argument", unarg} , + {" unmark-buffer", unmark} , + {" update-screen", upscreen} , + {" view-file", viewfile} , + {" wrap-word", wrapword} , + {" write-file", filewrite} , + {" write-message", writemsg} , + {"!yank", yank} , - {"", NULL, 0} + {" ", NULL} }; /* end of names.c */ diff --git a/names.h b/names.h index 6bc1944..9355ca3 100644 --- a/names.h +++ b/names.h @@ -5,11 +5,13 @@ /* Structure for the name binding table. */ typedef struct name_bind { - const char *n_name ; /* name of function key */ - fnp_t n_func ; /* function name is bound to */ - char tag ; /* view mode compatibility tag */ + const char *n_name ; /* name starting with one tag character */ + fnp_t n_func ; /* function the name is bound to */ } name_bind ; +#define bind_name( p) (&(p)->n_name[ 1]) +#define bind_tag( p) (p)->n_name[ 0] + extern const name_bind names[] ; /* name to function table */ #endif From c093b7064bc9677161b19bc73858cbc390781077 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 20 Jul 2021 17:34:35 +0800 Subject: [PATCH 06/37] Cache the result of function to name mapping lookup when doing keycode to function mapping lookup. --- bind.c | 168 +++++++++++++++----------------- bind.h | 10 +- ebind.c | 286 +++++++++++++++++++++++++++--------------------------- ebind.h | 10 +- execute.c | 14 ++- fnp_t.h | 7 -- input.c | 4 +- input.h | 9 +- names.h | 5 +- 9 files changed, 254 insertions(+), 259 deletions(-) delete mode 100644 fnp_t.h diff --git a/bind.c b/bind.c index 839dc8c..460bbfa 100644 --- a/bind.c +++ b/bind.c @@ -10,6 +10,7 @@ * Modified by Petri Kutvonen */ +#include #include #include #include @@ -36,7 +37,7 @@ static int buildlist( char *mstring) ; static void cmdstr( int c, char *seq) ; static unsigned int getckey( int mflag) ; static unsigned int stock( char *keyname) ; -static int unbindchar( unsigned c) ; +static boolean unbindchar( unsigned c) ; static const char *getfname( unsigned keycode, char *failmsg) ; @@ -105,47 +106,39 @@ int deskey( int f, int n) { * * int f, n; command arguments [IGNORED] */ -int bindtokey(int f, int n) -{ - unsigned int c; /* command key to bind */ - fnp_t kfunc; /* ptr to the requested function to bind to */ - struct key_tab *ktp; /* pointer into the command table */ - int found; /* matched command flag */ - char outseq[80]; /* output buffer for keystroke sequence */ +int bindtokey(int f, int n) { + key_tab *ktp ; /* pointer into the command table */ + char outseq[ 80] ; /* output buffer for keystroke sequence */ - /* prompt the user to type in a key to bind */ +/* prompt the user to type in a key to bind */ mlwrite(": bind-to-key "); - /* get the function name to bind it to */ - kfunc = getname()->n_func ; - if (kfunc == NULL) { - mlwrite("(No such function)"); - return FALSE; +/* get the function name to bind it to */ + fnp_t kfunc = getname()->n_func ; + if( kfunc == NULL) { + mlwrite( "(No such function)") ; + return FALSE ; } - ostring(" "); - /* get the command sequence to bind */ - c = getckey((kfunc == metafn) || (kfunc == cex) || - (kfunc == unarg) || (kfunc == ctrlg)); + ostring( " ") ; - /* change it to something we can print as well */ - cmdstr(c, &outseq[0]); +/* get the command sequence to bind */ + boolean prefix_f = (kfunc == metafn) || (kfunc == cex) || + (kfunc == unarg) || (kfunc == ctrlg) ; + unsigned c = getckey( prefix_f) ; - /* and dump it out */ - ostring(outseq); +/* change it to something we can print as well */ + cmdstr( c, outseq) ; - /* if the function is a prefix key */ - if (kfunc == metafn || kfunc == cex || - kfunc == unarg || kfunc == ctrlg) { +/* and dump it out */ + ostring( outseq) ; - /* search for an existing binding for the prefix key */ - ktp = &keytab[0]; - found = FALSE; - while (ktp->k_fp != NULL) { - if (ktp->k_fp == kfunc) - unbindchar(ktp->k_code); - ++ktp; - } +/* if the function is a prefix key */ + if( prefix_f) { + /* search for an existing binding for the prefix key */ + for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) + if( ktp->k_fp == kfunc) + unbindchar( ktp->k_code) ; /* reset the appropriate global prefix variable */ if (kfunc == metafn) @@ -158,32 +151,31 @@ int bindtokey(int f, int n) abortc = c; } - /* search the table to see if it exists */ - ktp = &keytab[0]; - found = FALSE; - while (ktp->k_fp != NULL) { - if (ktp->k_code == c) { - found = TRUE; - break; +/* search the table to see if it exists */ + for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) { + if( ktp->k_code == c) { + /* it exists, just change it then */ + ktp->k_fp = kfunc ; + ktp->k_nbp = NULL ; + return TRUE ; } - ++ktp; } - if (found) { /* it exists, just change it then */ - ktp->k_fp = kfunc; - } else { /* otherwise we need to add it to the end */ - /* if we run out of binding room, bitch */ - if (ktp >= &keytab[NBINDS]) { - mlwrite("Binding table FULL!"); - return FALSE; - } - - ktp->k_code = c; /* add keycode */ - ktp->k_fp = kfunc; /* and the function pointer */ - ++ktp; /* and make sure the next is null */ - ktp->k_code = 0; - ktp->k_fp = NULL; +/* otherwise we need to add it to the end */ + /* if we run out of binding room, bitch */ + if( ktp >= &keytab[ NBINDS]) { + mlwrite( "Binding table FULL!") ; + return FALSE ; } + + ktp->k_code = c; /* add keycode */ + ktp->k_fp = kfunc; /* and the function pointer */ + ktp->k_nbp = NULL ; + ++ktp; /* and make sure the next is null */ + ktp->k_code = 0; + ktp->k_fp = NULL; + ktp->k_nbp = NULL ; + return TRUE; } @@ -224,40 +216,31 @@ int unbindkey(int f, int n) * * int c; command key to unbind */ -static int unbindchar( unsigned c) { - struct key_tab *ktp; /* pointer into the command table */ - struct key_tab *sktp; /* saved pointer into the command table */ - int found; /* matched command flag */ +static boolean unbindchar( unsigned c) { + key_tab *ktp ; /* pointer into the command table */ - /* search the table to see if the key exists */ - ktp = &keytab[0]; - found = FALSE; - while (ktp->k_fp != NULL) { +/* search the table to see if the key exists */ + for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) { if (ktp->k_code == c) { - found = TRUE; - break; + /* save the pointer and scan to the end of the table */ + key_tab *sktp = ktp ; + while( (++ktp)->k_fp != NULL) ; + ktp -= 1 ; /* backup to the last legit entry */ + + /* copy the last entry to the current one */ + sktp->k_code = ktp->k_code ; + sktp->k_fp = ktp->k_fp ; + sktp->k_nbp = ktp->k_nbp ; + + /* null out the last one */ + ktp->k_code = 0 ; + ktp->k_fp = NULL ; + ktp->k_nbp = NULL ; + return TRUE ; } - ++ktp; } - /* if it isn't bound, bitch */ - if (!found) - return FALSE; - - /* save the pointer and scan to the end of the table */ - sktp = ktp; - while (ktp->k_fp != NULL) - ++ktp; - --ktp; /* backup to the last legit entry */ - - /* copy the last entry to the current one */ - sktp->k_code = ktp->k_code; - sktp->k_fp = ktp->k_fp; - - /* null out the last one */ - ktp->k_code = 0; - ktp->k_fp = NULL; - return TRUE; + return FALSE ; } #if APROP @@ -505,24 +488,29 @@ static void cmdstr( int c, char *seq) { * * int c; key to find what is bound to it */ -fnp_t getbind( unsigned c) { +key_tab *getkeybind( unsigned c) { struct key_tab *ktp ; for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) if (ktp->k_code == c) - return ktp->k_fp ; + break ; - /* no such binding */ - return NULL ; + return ktp ; } static const char *getfname( unsigned keycode, char *failmsg) { /* takes a key code and gets the name of the function bound to it */ - fnp_t func = getbind( keycode) ; + key_tab *kbp = getkeybind( keycode) ; + fnp_t func = kbp->k_fp ; if( func == NULL) return failmsg ; - const char *found = getfncname( func) ; + if( kbp->k_nbp == NULL) + kbp->k_nbp = getfncnb( func) ; + + assert( func == kbp->k_nbp->n_func) ; + const char *found = bind_name( kbp->k_nbp) ; + assert( *found) ; return *found ? found : failmsg ; } diff --git a/bind.h b/bind.h index 141c3f4..93200e0 100644 --- a/bind.h +++ b/bind.h @@ -1,7 +1,7 @@ #ifndef _BIND_H_ #define _BIND_H_ -#include "fnp_t.h" +#include "ebind.h" #define APROP 1 /* Add code for Apropos command */ @@ -9,13 +9,17 @@ int apro( int f, int n) ; #endif +/* uEMACS functions */ int help( int f, int n) ; int deskey( int f, int n) ; int bindtokey( int f, int n) ; int unbindkey( int f, int n) ; int desbind( int f, int n) ; + int startup( const char *fname) ; -fnp_t getbind( unsigned keycode) ; -const char *transbind( char *skey) ; + +/* find a key to function association in the key to function mapping table */ +key_tab *getkeybind( unsigned keycode) ; /* by key code */ +const char *transbind( char *skey) ; /* by string representation of key */ #endif diff --git a/ebind.c b/ebind.c index 9f1c78a..7ee5ce7 100644 --- a/ebind.c +++ b/ebind.c @@ -35,329 +35,329 @@ * control-X commands. */ key_tab keytab[ NBINDS] = { - {CONTROL | '?', backdel}, - {CONTROL | 'A', (fnp_t) gotobol} + {CONTROL | '?', backdel, NULL}, + {CONTROL | 'A', (fnp_t) gotobol, NULL} , - {CONTROL | 'B', (fnp_t) backchar} + {CONTROL | 'B', (fnp_t) backchar, NULL} , - {CONTROL | 'C', insspace} + {CONTROL | 'C', insspace, NULL} , - {CONTROL | 'D', forwdel} + {CONTROL | 'D', forwdel, NULL} , - {CONTROL | 'E', (fnp_t) gotoeol} + {CONTROL | 'E', (fnp_t) gotoeol, NULL} , - {CONTROL | 'F', (fnp_t) forwchar} + {CONTROL | 'F', (fnp_t) forwchar, NULL} , - {CONTROL | 'G', ctrlg} + {CONTROL | 'G', ctrlg, NULL} , - {CONTROL | 'H', backdel} + {CONTROL | 'H', backdel, NULL} , - {CONTROL | 'I', insert_tab} + {CONTROL | 'I', insert_tab, NULL} , - {CONTROL | 'J', indent} + {CONTROL | 'J', indent, NULL} , - {CONTROL | 'K', killtext} + {CONTROL | 'K', killtext, NULL} , - {CONTROL | 'L', redraw} + {CONTROL | 'L', redraw, NULL} , - {CONTROL | 'M', insert_newline} + {CONTROL | 'M', insert_newline, NULL} , - {CONTROL | 'N', (fnp_t) forwline} + {CONTROL | 'N', (fnp_t) forwline, NULL} , - {CONTROL | 'O', openline} + {CONTROL | 'O', openline, NULL} , - {CONTROL | 'P', (fnp_t) backline} + {CONTROL | 'P', (fnp_t) backline, NULL} , - {CONTROL | 'Q', quote} + {CONTROL | 'Q', quote, NULL} , - {CONTROL | 'R', backsearch} + {CONTROL | 'R', backsearch, NULL} , - {CONTROL | 'S', forwsearch} + {CONTROL | 'S', forwsearch, NULL} , - {CONTROL | 'T', (fnp_t) twiddle} + {CONTROL | 'T', (fnp_t) twiddle, NULL} , - {CONTROL | 'U', unarg} + {CONTROL | 'U', unarg, NULL} , - {CONTROL | 'V', (fnp_t) forwpage} + {CONTROL | 'V', (fnp_t) forwpage, NULL} , - {CONTROL | 'W', killregion} + {CONTROL | 'W', killregion, NULL} , - {CONTROL | 'X', cex} + {CONTROL | 'X', cex, NULL} , - {CONTROL | 'Y', yank} + {CONTROL | 'Y', yank, NULL} , - {CONTROL | 'Z', (fnp_t) backpage} + {CONTROL | 'Z', (fnp_t) backpage, NULL} , - {CONTROL | ']', metafn} + {CONTROL | ']', metafn, NULL} , - {CTLX | CONTROL | 'B', listbuffers} + {CTLX | CONTROL | 'B', listbuffers, NULL} , - {CTLX | CONTROL | 'C', quit} + {CTLX | CONTROL | 'C', quit, NULL} , /* Hard quit. */ #if PKCODE & AEDIT - {CTLX | CONTROL | 'A', detab} + {CTLX | CONTROL | 'A', detab, NULL} , #endif #if PKCODE - {CTLX | CONTROL | 'D', filesave} + {CTLX | CONTROL | 'D', filesave, NULL} , /* alternative */ #else #if AEDIT - {CTLX | CONTROL | 'D', detab} + {CTLX | CONTROL | 'D', detab, NULL} , #endif #endif #if AEDIT - {CTLX | CONTROL | 'E', entab} + {CTLX | CONTROL | 'E', entab, NULL} , #endif - {CTLX | CONTROL | 'F', filefind} + {CTLX | CONTROL | 'F', filefind, NULL} , - {CTLX | CONTROL | 'I', insfile} + {CTLX | CONTROL | 'I', insfile, NULL} , - {CTLX | CONTROL | 'L', lowerregion} + {CTLX | CONTROL | 'L', lowerregion, NULL} , - {CTLX | CONTROL | 'M', delmode} + {CTLX | CONTROL | 'M', delmode, NULL} , - {CTLX | CONTROL | 'N', mvdnwind} + {CTLX | CONTROL | 'N', mvdnwind, NULL} , - {CTLX | CONTROL | 'O', deblank} + {CTLX | CONTROL | 'O', deblank, NULL} , - {CTLX | CONTROL | 'P', mvupwind} + {CTLX | CONTROL | 'P', mvupwind, NULL} , - {CTLX | CONTROL | 'R', fileread} + {CTLX | CONTROL | 'R', fileread, NULL} , - {CTLX | CONTROL | 'S', filesave} + {CTLX | CONTROL | 'S', filesave, NULL} , #if AEDIT - {CTLX | CONTROL | 'T', trim} + {CTLX | CONTROL | 'T', trim, NULL} , #endif - {CTLX | CONTROL | 'U', upperregion} + {CTLX | CONTROL | 'U', upperregion, NULL} , - {CTLX | CONTROL | 'V', viewfile} + {CTLX | CONTROL | 'V', viewfile, NULL} , - {CTLX | CONTROL | 'W', filewrite} + {CTLX | CONTROL | 'W', filewrite, NULL} , - {CTLX | CONTROL | 'X', (fnp_t) swapmark} + {CTLX | CONTROL | 'X', (fnp_t) swapmark, NULL} , - {CTLX | CONTROL | 'Z', shrinkwind} + {CTLX | CONTROL | 'Z', shrinkwind, NULL} , - {CTLX | '?', deskey} + {CTLX | '?', deskey, NULL} , - {CTLX | '!', spawn} + {CTLX | '!', spawn, NULL} , - {CTLX | '@', pipecmd} + {CTLX | '@', pipecmd, NULL} , - {CTLX | '#', filter_buffer} + {CTLX | '#', filter_buffer, NULL} , - {CTLX | '$', execprg} + {CTLX | '$', execprg, NULL} , - {CTLX | '=', showcpos} + {CTLX | '=', showcpos, NULL} , - {CTLX | '(', ctlxlp} + {CTLX | '(', ctlxlp, NULL} , - {CTLX | ')', ctlxrp} + {CTLX | ')', ctlxrp, NULL} , - {CTLX | '^', enlargewind} + {CTLX | '^', enlargewind, NULL} , - {CTLX | '0', delwind} + {CTLX | '0', delwind, NULL} , - {CTLX | '1', onlywind} + {CTLX | '1', onlywind, NULL} , - {CTLX | '2', splitwind} + {CTLX | '2', splitwind, NULL} , - {CTLX | 'A', setvar} + {CTLX | 'A', setvar, NULL} , - {CTLX | 'B', usebuffer} + {CTLX | 'B', usebuffer, NULL} , - {CTLX | 'C', spawncli} + {CTLX | 'C', spawncli, NULL} , #if BSD | SVR4 - {CTLX | 'D', bktoshell} + {CTLX | 'D', bktoshell, NULL} , #endif - {CTLX | 'E', ctlxe} + {CTLX | 'E', ctlxe, NULL} , - {CTLX | 'F', setfillcol} + {CTLX | 'F', setfillcol, NULL} , - {CTLX | 'K', killbuffer} + {CTLX | 'K', killbuffer, NULL} , - {CTLX | 'M', setemode} + {CTLX | 'M', setemode, NULL} , - {CTLX | 'N', filename} + {CTLX | 'N', filename, NULL} , - {CTLX | 'O', nextwind} + {CTLX | 'O', nextwind, NULL} , - {CTLX | 'P', prevwind} + {CTLX | 'P', prevwind, NULL} , #if PKCODE - {CTLX | 'Q', quote} + {CTLX | 'Q', quote, NULL} , /* alternative */ #endif #if ISRCH - {CTLX | 'R', risearch} + {CTLX | 'R', risearch, NULL} , - {CTLX | 'S', fisearch} + {CTLX | 'S', fisearch, NULL} , #endif - {CTLX | 'W', resize} + {CTLX | 'W', resize, NULL} , - {CTLX | 'X', nextbuffer} + {CTLX | 'X', nextbuffer, NULL} , - {CTLX | 'Z', enlargewind} + {CTLX | 'Z', enlargewind, NULL} , - {META | CONTROL | '?', delbword}, + {META | CONTROL | '?', delbword, NULL}, #if WORDPRO - {META | CONTROL | 'C', wordcount} + {META | CONTROL | 'C', wordcount, NULL} , #endif #if PKCODE - {META | CONTROL | 'D', newsize} + {META | CONTROL | 'D', newsize, NULL} , #endif #if PROC - {META | CONTROL | 'E', execproc} + {META | CONTROL | 'E', execproc, NULL} , #endif #if CFENCE - {META | CONTROL | 'F', getfence} + {META | CONTROL | 'F', getfence, NULL} , #endif - {META | CONTROL | 'H', delbword} + {META | CONTROL | 'H', delbword, NULL} , - {META | CONTROL | 'K', unbindkey} + {META | CONTROL | 'K', unbindkey, NULL} , - {META | CONTROL | 'L', reposition} + {META | CONTROL | 'L', reposition, NULL} , - {META | CONTROL | 'M', delgmode} + {META | CONTROL | 'M', delgmode, NULL} , - {META | CONTROL | 'N', namebuffer} + {META | CONTROL | 'N', namebuffer, NULL} , - {META | CONTROL | 'R', qreplace} + {META | CONTROL | 'R', qreplace, NULL} , - {META | CONTROL | 'S', newsize} + {META | CONTROL | 'S', newsize, NULL} , - {META | CONTROL | 'T', newwidth} + {META | CONTROL | 'T', newwidth, NULL} , - {META | CONTROL | 'V', scrnextdw} + {META | CONTROL | 'V', scrnextdw, NULL} , #if WORDPRO - {META | CONTROL | 'W', killpara} + {META | CONTROL | 'W', killpara, NULL} , #endif - {META | CONTROL | 'Z', scrnextup} + {META | CONTROL | 'Z', scrnextup, NULL} , - {META | ' ', (fnp_t) setmark} + {META | ' ', (fnp_t) setmark, NULL} , - {META | '?', help} + {META | '?', help, NULL} , - {META | '!', reposition} + {META | '!', reposition, NULL} , - {META | '.', (fnp_t) setmark} + {META | '.', (fnp_t) setmark, NULL} , - {META | '>', (fnp_t) gotoeob} + {META | '>', (fnp_t) gotoeob, NULL} , - {META | '<', (fnp_t) gotobob} + {META | '<', (fnp_t) gotobob, NULL} , - {META | '~', unmark} + {META | '~', unmark, NULL} , #if APROP - {META | 'A', apro} + {META | 'A', apro, NULL} , #endif - {META | 'B', backword} + {META | 'B', backword, NULL} , - {META | 'C', capword} + {META | 'C', capword, NULL} , - {META | 'D', delfword} + {META | 'D', delfword, NULL} , - {META | 'F', forwword} + {META | 'F', forwword, NULL} , - {META | 'G', gotoline} + {META | 'G', gotoline, NULL} , #if PKCODE #if WORDPRO - {META | 'J', justpara} + {META | 'J', justpara, NULL} , #endif #endif - {META | 'K', bindtokey} + {META | 'K', bindtokey, NULL} , - {META | 'L', lowerword} + {META | 'L', lowerword, NULL} , - {META | 'M', setgmode} + {META | 'M', setgmode, NULL} , #if WORDPRO - {META | 'N', gotoeop} + {META | 'N', gotoeop, NULL} , - {META | 'P', gotobop} + {META | 'P', gotobop, NULL} , - {META | 'Q', fillpara} + {META | 'Q', fillpara, NULL} , #endif - {META | 'R', sreplace} + {META | 'R', sreplace, NULL} , #if PKCODE - {META | 'S', forwhunt} + {META | 'S', forwhunt, NULL} , #else #if BSD - {META | 'S', bktoshell} + {META | 'S', bktoshell, NULL} , #endif #endif - {META | 'U', upperword} + {META | 'U', upperword, NULL} , - {META | 'V', (fnp_t) backpage} + {META | 'V', (fnp_t) backpage, NULL} , - {META | 'W', copyregion} + {META | 'W', copyregion, NULL} , - {META | 'X', namedcmd} + {META | 'X', namedcmd, NULL} , - {META | 'Z', quickexit} + {META | 'Z', quickexit, NULL} , #if VT220 - {SPEC | '1', (fnp_t) gotobob /* fisearch */} + {SPEC | '1', (fnp_t) gotobob /* fisearch */, NULL} , /* VT220 keys */ - {SPEC | '2', yank} + {SPEC | '2', yank, NULL} , - {SPEC | '3', forwdel /* killregion */} + {SPEC | '3', forwdel /* killregion */, NULL} , - {SPEC | '4', (fnp_t) gotoeob /* setmark */} + {SPEC | '4', (fnp_t) gotoeob /* setmark */, NULL} , - {SPEC | '5', (fnp_t) backpage} + {SPEC | '5', (fnp_t) backpage, NULL} , - {SPEC | '6', (fnp_t) forwpage} + {SPEC | '6', (fnp_t) forwpage, NULL} , - {SPEC | 'A', (fnp_t) backline} + {SPEC | 'A', (fnp_t) backline, NULL} , - {SPEC | 'B', (fnp_t) forwline} + {SPEC | 'B', (fnp_t) forwline, NULL} , - {SPEC | 'C', (fnp_t) forwchar} + {SPEC | 'C', (fnp_t) forwchar, NULL} , - {SPEC | 'D', (fnp_t) backchar} + {SPEC | 'D', (fnp_t) backchar, NULL} , - {SPEC | 'c', metafn} + {SPEC | 'c', metafn, NULL} , - {SPEC | 'd', (fnp_t) backchar} + {SPEC | 'd', (fnp_t) backchar, NULL} , - {SPEC | 'e', (fnp_t) forwline} + {SPEC | 'e', (fnp_t) forwline, NULL} , - {SPEC | 'f', (fnp_t) gotobob} + {SPEC | 'f', (fnp_t) gotobob, NULL} , - {SPEC | 'h', help} + {SPEC | 'h', help, NULL} , - {SPEC | 'i', cex} + {SPEC | 'i', cex, NULL} , #endif /* special internal bindings */ - { SPEC | META | 'W', wrapword }, /* called on word wrap */ - { SPEC | META | 'C', nullproc }, /* every command input */ - { SPEC | META | 'R', nullproc }, /* on file read */ - { SPEC | META | 'X', nullproc }, /* on window change P.K. */ + { SPEC | META | 'W', wrapword , NULL}, /* called on word wrap */ + { SPEC | META | 'C', nullproc , NULL}, /* every command input */ + { SPEC | META | 'R', nullproc , NULL}, /* on file read */ + { SPEC | META | 'X', nullproc , NULL}, /* on window change P.K. */ - {0, NULL} + {0, NULL, NULL} }; diff --git a/ebind.h b/ebind.h index 83dd710..4ab490f 100644 --- a/ebind.h +++ b/ebind.h @@ -1,14 +1,16 @@ #ifndef _EBIND_H_ #define _EBIND_H_ -#include "fnp_t.h" +#include "names.h" -/* Structure for the table of initial key bindings. */ +/* Structure for the key bindings table. */ typedef struct key_tab { - unsigned k_code ; /* Key code */ - fnp_t k_fp ; /* Routine to handle it */ + unsigned k_code ; /* Key code */ + fnp_t k_fp ; /* Routine to handle it */ + const name_bind *k_nbp ; /* entry in name to function map table */ } key_tab ; +/* keycode to function mapping table */ #define NBINDS 256 /* max # of bound keys */ extern key_tab keytab[ NBINDS] ; /* key bind to functions table */ diff --git a/execute.c b/execute.c index efe84b0..5210577 100644 --- a/execute.c +++ b/execute.c @@ -3,6 +3,7 @@ #define CLRMSG 0 /* space clears the message line with no insert */ +#include #include #include @@ -212,11 +213,16 @@ int execute( int c, int f, int n) { int status ; /* if the keystroke is a bound function...do it */ - fnp_t execfunc = getbind( c) ; + key_tab *ktp = getkeybind( c) ; + fnp_t execfunc = ktp->k_fp ; if( execfunc != NULL) { thisflag = 0 ; - const char *sp = getfncname( execfunc) ; - if( (sp[ -1] & 1) && (curbp->b_mode & MDVIEW)) + if( ktp->k_nbp == NULL) + ktp->k_nbp = getfncnb( execfunc) ; + + assert( ktp->k_nbp->n_func == execfunc) ; + char tag = bind_tag( ktp->k_nbp) ; + if( (tag & 1) && (curbp->b_mode & MDVIEW)) status = rdonly() ; else status = execfunc( f, n) ; @@ -337,7 +343,7 @@ void kbd_loop( void) { fnp_t execfunc ; if( c == newc - && (execfunc = getbind( c)) != NULL + && (execfunc = getkeybind( c)->k_fp) != NULL && execfunc != insert_newline && execfunc != insert_tab) newc = getcmd() ; diff --git a/fnp_t.h b/fnp_t.h deleted file mode 100644 index 87e50c0..0000000 --- a/fnp_t.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef __FNP_T_H__ -#define __FNP_T_H__ - -/* Generic uEMACS function pointer type */ -typedef int (*fnp_t)( int, int) ; - -#endif diff --git a/input.c b/input.c index e0ecb01..e972dd9 100644 --- a/input.c +++ b/input.c @@ -175,7 +175,7 @@ const name_bind *fncmatch( char *fname) { } -const char *getfncname( fnp_t func) { +const name_bind *getfncnb( fnp_t func) { const name_bind *nptr ; /* pointer into the name binding table */ /* skim through the table, looking for a match */ @@ -183,7 +183,7 @@ const char *getfncname( fnp_t func) { if (nptr->n_func == func) break ; - return bind_name( nptr) ; + return nptr ; } /* diff --git a/input.h b/input.h index 041b306..d633b64 100644 --- a/input.h +++ b/input.h @@ -3,10 +3,11 @@ #include "names.h" - typedef enum { STOP, PLAY, RECORD } kbdstate ; + + extern kbdstate kbdmode ; /* current keyboard macro mode */ extern int lastkey ; /* last keystoke */ extern int kbdrep ; /* number of repetitions */ @@ -28,9 +29,9 @@ int newmlargt( char **outbufref, const char *prompt, int size) ; int ectoc( int c) ; /* Look up in names to function association table */ -const name_bind *fncmatch( char *) ; -const name_bind *getname( void) ; -const char *getfncname( fnp_t func) ; +const name_bind *fncmatch( char *name) ; /* by name */ +const name_bind *getname( void) ; /* interactively */ +const name_bind *getfncnb( fnp_t func) ; /* by function */ int tgetc( void) ; int get1key( void) ; diff --git a/names.h b/names.h index 9355ca3..0ee79c8 100644 --- a/names.h +++ b/names.h @@ -1,7 +1,8 @@ #ifndef _NAMES_H_ #define _NAMES_H_ -#include "fnp_t.h" +/* Generic uEMACS function pointer type */ +typedef int (*fnp_t)( int, int) ; /* Structure for the name binding table. */ typedef struct name_bind { @@ -12,6 +13,6 @@ typedef struct name_bind { #define bind_name( p) (&(p)->n_name[ 1]) #define bind_tag( p) (p)->n_name[ 0] -extern const name_bind names[] ; /* name to function table */ +extern const name_bind names[] ; /* name to function mapping table */ #endif From 6f7d89b1ac44cf9ec1023b333785873ea8a48e27 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Wed, 21 Jul 2021 08:16:11 +0800 Subject: [PATCH 07/37] Fix regression: checking abort when prompting for command name. --- bind.c | 8 ++++++-- exec.c | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/bind.c b/bind.c index 460bbfa..e1ba41a 100644 --- a/bind.c +++ b/bind.c @@ -106,7 +106,7 @@ int deskey( int f, int n) { * * int f, n; command arguments [IGNORED] */ -int bindtokey(int f, int n) { +int bindtokey( int f, int n) { key_tab *ktp ; /* pointer into the command table */ char outseq[ 80] ; /* output buffer for keystroke sequence */ @@ -114,7 +114,11 @@ int bindtokey(int f, int n) { mlwrite(": bind-to-key "); /* get the function name to bind it to */ - fnp_t kfunc = getname()->n_func ; + const name_bind *nbp = getname() ; + if( nbp == NULL) /* abort */ + return FALSE ; + + fnp_t kfunc = nbp->n_func ; if( kfunc == NULL) { mlwrite( "(No such function)") ; return FALSE ; diff --git a/exec.c b/exec.c index 3681c67..b8f86ad 100644 --- a/exec.c +++ b/exec.c @@ -87,10 +87,13 @@ static int macarg( char *tok, int toksz) ; */ int namedcmd( int f, int n) { /* prompt the user to type a named command */ - mlwrite(": "); + mlwrite(": execute-named-cmd "); /* and now get the function name to execute */ const name_bind *nbp = getname() ; + if( nbp == NULL) /* abort */ + return FALSE ; + fnp_t kfunc = nbp->n_func ; if (kfunc == NULL) { mlwrite("(No such function)"); @@ -118,7 +121,7 @@ int execcmd( int f, int n) { char *cmdstr ; /* string holding command to execute */ /* get the line wanted */ - status = newmlarg( &cmdstr, ": ", 0) ; + status = newmlarg( &cmdstr, ": execute-command-line ", 0) ; if( status != TRUE) return status ; From 521d96fbdab843ac938dc3eec877651d31c210ad Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Wed, 21 Jul 2021 16:40:36 +0800 Subject: [PATCH 08/37] Use binary search for name to function lookup. Fix name mapping table order. Rework test scripts. --- blindmaz.cmd | 1 + eval.c | 2 -- floodmaz.cmd | 5 +++++ input.c | 18 ------------------ input.h | 1 - names.c | 51 +++++++++++++++++++++++++++++++++++++-------------- names.h | 2 ++ 7 files changed, 45 insertions(+), 35 deletions(-) diff --git a/blindmaz.cmd b/blindmaz.cmd index d883e6a..ecc67cc 100644 --- a/blindmaz.cmd +++ b/blindmaz.cmd @@ -67,3 +67,4 @@ set %D 0 # looking EAST beginning-of-file set $curline 3 set $curcol 1 +unmark-buffer diff --git a/eval.c b/eval.c index 5b06a06..689a74d 100644 --- a/eval.c +++ b/eval.c @@ -35,8 +35,6 @@ #define MAXVARS 255 -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - /* Macro argument token types */ #define TKNUL 0 /* end-of-string */ diff --git a/floodmaz.cmd b/floodmaz.cmd index fb83593..5d2d20a 100644 --- a/floodmaz.cmd +++ b/floodmaz.cmd @@ -61,3 +61,8 @@ set %NC &asc "█" !endwhile set $curline 3 set $curcol 1 +select-buffer stack +unmark-buffer +select-buffer %thisbuf +unmark-buffer +delete-buffer stack diff --git a/input.c b/input.c index e972dd9..fc67ece 100644 --- a/input.c +++ b/input.c @@ -157,24 +157,6 @@ int ectoc( int c) { return c ; } -/* - * match fname to a function in the names table - * and return any match or NULL if none - * - * char *fname; name to attempt to match - */ -const name_bind *fncmatch( char *fname) { - const name_bind *ffp ; /* pointer to entry in name binding table */ - - /* scan through the table, returning any match */ - for( ffp = names ; ffp->n_func != NULL ; ffp++) - if( strcmp( fname, bind_name( ffp)) == 0) - break ; - - return ffp ; -} - - const name_bind *getfncnb( fnp_t func) { const name_bind *nptr ; /* pointer into the name binding table */ diff --git a/input.h b/input.h index d633b64..25a907f 100644 --- a/input.h +++ b/input.h @@ -29,7 +29,6 @@ int newmlargt( char **outbufref, const char *prompt, int size) ; int ectoc( int c) ; /* Look up in names to function association table */ -const name_bind *fncmatch( char *name) ; /* by name */ const name_bind *getname( void) ; /* interactively */ const name_bind *getfncnb( fnp_t func) ; /* by function */ diff --git a/names.c b/names.c index f341e38..f50fd42 100644 --- a/names.c +++ b/names.c @@ -8,6 +8,8 @@ * processing. */ +#include + #include "basic.h" #include "bind.h" #include "bindable.h" @@ -22,13 +24,14 @@ #include "random.h" #include "search.h" #include "spawn.h" +#include "util.h" #include "window.h" #include "word.h" const name_bind names[] = { {" abort-command", ctrlg} , - {" add-mode", setemode} , {" add-global-mode", setgmode} , + {" add-mode", setemode} , #if APROP {" apropos", apro} , #endif @@ -55,8 +58,8 @@ const name_bind names[] = { {" ctlx-prefix", cex} , {"!delete-blank-lines", deblank} , {" delete-buffer", killbuffer} , - {" delete-mode", delmode} , {" delete-global-mode", delgmode} , + {" delete-mode", delmode} , {"!delete-next-character", forwdel} , {"!delete-next-word", delfword} , {" delete-other-windows", onlywind} , @@ -80,14 +83,6 @@ const name_bind names[] = { {" execute-file", execfile} , {" execute-macro", ctlxe} , {" execute-macro-1", cbuf1} , - {" execute-macro-2", cbuf2} , - {" execute-macro-3", cbuf3} , - {" execute-macro-4", cbuf4} , - {" execute-macro-5", cbuf5} , - {" execute-macro-6", cbuf6} , - {" execute-macro-7", cbuf7} , - {" execute-macro-8", cbuf8} , - {" execute-macro-9", cbuf9} , {" execute-macro-10", cbuf10} , {" execute-macro-11", cbuf11} , {" execute-macro-12", cbuf12} , @@ -98,6 +93,7 @@ const name_bind names[] = { {" execute-macro-17", cbuf17} , {" execute-macro-18", cbuf18} , {" execute-macro-19", cbuf19} , + {" execute-macro-2", cbuf2} , {" execute-macro-20", cbuf20} , {" execute-macro-21", cbuf21} , {" execute-macro-22", cbuf22} , @@ -108,6 +104,7 @@ const name_bind names[] = { {" execute-macro-27", cbuf27} , {" execute-macro-28", cbuf28} , {" execute-macro-29", cbuf29} , + {" execute-macro-3", cbuf3} , {" execute-macro-30", cbuf30} , {" execute-macro-31", cbuf31} , {" execute-macro-32", cbuf32} , @@ -118,7 +115,13 @@ const name_bind names[] = { {" execute-macro-37", cbuf37} , {" execute-macro-38", cbuf38} , {" execute-macro-39", cbuf39} , + {" execute-macro-4", cbuf4} , {" execute-macro-40", cbuf40} , + {" execute-macro-5", cbuf5} , + {" execute-macro-6", cbuf6} , + {" execute-macro-7", cbuf7} , + {" execute-macro-8", cbuf8} , + {" execute-macro-9", cbuf9} , {" execute-named-command", namedcmd} , #if PROC {" execute-procedure", execproc} , @@ -137,9 +140,9 @@ const name_bind names[] = { #endif {" grow-window", enlargewind} , {"!handle-tab", insert_tab} , - {" hunt-forward", forwhunt} , - {" hunt-backward", backhunt} , {" help", help} , + {" hunt-backward", backhunt} , + {" hunt-forward", forwhunt} , {" i-shell", spawncli} , #if ISRCH {" incremental-search", fisearch} , @@ -186,9 +189,9 @@ const name_bind names[] = { {"!quote-character", quote} , {"!read-file", fileread} , {" redraw-display", reposition} , + {"!replace-string", sreplace} , {" resize-window", resize} , {" restore-window", restwnd} , - {"!replace-string", sreplace} , #if ISRCH {" reverse-incremental-search", risearch} , #endif @@ -197,8 +200,8 @@ const name_bind names[] = { #endif {"!save-file", filesave} , {" save-window", savewnd} , - {" scroll-next-up", scrnextup} , {" scroll-next-down", scrnextdw} , + {" scroll-next-up", scrnextup} , {" search-forward", forwsearch} , {" search-reverse", backsearch} , {" select-buffer", usebuffer} , @@ -232,4 +235,24 @@ const name_bind names[] = { {" ", NULL} }; + +const name_bind *fncmatch( char *name) { + int found = ARRAY_SIZE( names) - 1 ; /* index of last entry/ catch all */ + int low = 0 ; + int high = found - 1 ; + do { + int cur = (high + low) / 2 ; + int s = strcmp( name, bind_name( &names[ cur])) ; + if( s < 0) + high = cur - 1 ; + else if( s == 0) { + found = cur ; + break ; + } else + low = cur + 1 ; + } while( low <= high) ; + + return &names[ found] ; +} + /* end of names.c */ diff --git a/names.h b/names.h index 0ee79c8..ba763a4 100644 --- a/names.h +++ b/names.h @@ -15,4 +15,6 @@ typedef struct name_bind { extern const name_bind names[] ; /* name to function mapping table */ +const name_bind *fncmatch( char *name) ; /* look up by name */ + #endif From f30ef38bc8c669944ee7a8fbd19a235cf1ddcc2a Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Fri, 23 Jul 2021 10:47:58 +0800 Subject: [PATCH 09/37] Merge name to function and key code to function table initialization. --- bind.c | 5 +- bind.h | 4 +- ebind.c | 363 ------------------------------------------- ebind.h | 17 --- exec.c | 4 +- main.c | 1 + names.c | 465 +++++++++++++++++++++++++++++++++++--------------------- names.h | 24 ++- 8 files changed, 321 insertions(+), 562 deletions(-) delete mode 100644 ebind.c delete mode 100644 ebind.h diff --git a/bind.c b/bind.c index e1ba41a..ffe9010 100644 --- a/bind.c +++ b/bind.c @@ -19,7 +19,6 @@ #include "bindable.h" #include "buffer.h" #include "display.h" -#include "ebind.h" #include "exec.h" #include "file.h" #include "flook.h" @@ -308,7 +307,7 @@ int apro( int f, int n) { static int buildlist( char *mstring) { #endif struct window *wp; /* scanning pointer to windows */ - struct key_tab *ktp; /* pointer into the command table */ + key_tab *ktp; /* pointer into the command table */ const name_bind *nptr;/* pointer into the name binding table */ struct buffer *bp; /* buffer to put binding list into */ char outseq[80]; /* output buffer for keystroke sequence */ @@ -493,7 +492,7 @@ static void cmdstr( int c, char *seq) { * int c; key to find what is bound to it */ key_tab *getkeybind( unsigned c) { - struct key_tab *ktp ; + key_tab *ktp ; for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) if (ktp->k_code == c) diff --git a/bind.h b/bind.h index 93200e0..8a4fe90 100644 --- a/bind.h +++ b/bind.h @@ -1,15 +1,15 @@ #ifndef _BIND_H_ #define _BIND_H_ -#include "ebind.h" +#include "names.h" #define APROP 1 /* Add code for Apropos command */ +/* uEMACS functions */ #if APROP int apro( int f, int n) ; #endif -/* uEMACS functions */ int help( int f, int n) ; int deskey( int f, int n) ; int bindtokey( int f, int n) ; diff --git a/ebind.c b/ebind.c deleted file mode 100644 index 7ee5ce7..0000000 --- a/ebind.c +++ /dev/null @@ -1,363 +0,0 @@ -/* ebind.c -- implements ebind.h */ -#include "ebind.h" - -/* ebind.c - * - * Initial default key to function bindings - * - * Modified by Petri Kutvonen - */ - -#include - - -#include "basic.h" -#include "bind.h" -#include "estruct.h" -#include "bindable.h" -#include "buffer.h" -#include "eval.h" -#include "exec.h" -#include "file.h" -#include "isearch.h" -#include "line.h" -#include "random.h" -#include "region.h" -#include "search.h" -#include "spawn.h" -#include "window.h" -#include "word.h" - -/* - * Command table. - * This table is *roughly* in ASCII order, left to right across the - * characters of the command. This explains the funny location of the - * control-X commands. - */ -key_tab keytab[ NBINDS] = { - {CONTROL | '?', backdel, NULL}, - {CONTROL | 'A', (fnp_t) gotobol, NULL} - , - {CONTROL | 'B', (fnp_t) backchar, NULL} - , - {CONTROL | 'C', insspace, NULL} - , - {CONTROL | 'D', forwdel, NULL} - , - {CONTROL | 'E', (fnp_t) gotoeol, NULL} - , - {CONTROL | 'F', (fnp_t) forwchar, NULL} - , - {CONTROL | 'G', ctrlg, NULL} - , - {CONTROL | 'H', backdel, NULL} - , - {CONTROL | 'I', insert_tab, NULL} - , - {CONTROL | 'J', indent, NULL} - , - {CONTROL | 'K', killtext, NULL} - , - {CONTROL | 'L', redraw, NULL} - , - {CONTROL | 'M', insert_newline, NULL} - , - {CONTROL | 'N', (fnp_t) forwline, NULL} - , - {CONTROL | 'O', openline, NULL} - , - {CONTROL | 'P', (fnp_t) backline, NULL} - , - {CONTROL | 'Q', quote, NULL} - , - {CONTROL | 'R', backsearch, NULL} - , - {CONTROL | 'S', forwsearch, NULL} - , - {CONTROL | 'T', (fnp_t) twiddle, NULL} - , - {CONTROL | 'U', unarg, NULL} - , - {CONTROL | 'V', (fnp_t) forwpage, NULL} - , - {CONTROL | 'W', killregion, NULL} - , - {CONTROL | 'X', cex, NULL} - , - {CONTROL | 'Y', yank, NULL} - , - {CONTROL | 'Z', (fnp_t) backpage, NULL} - , - {CONTROL | ']', metafn, NULL} - , - {CTLX | CONTROL | 'B', listbuffers, NULL} - , - {CTLX | CONTROL | 'C', quit, NULL} - , /* Hard quit. */ -#if PKCODE & AEDIT - {CTLX | CONTROL | 'A', detab, NULL} - , -#endif -#if PKCODE - {CTLX | CONTROL | 'D', filesave, NULL} - , /* alternative */ -#else -#if AEDIT - {CTLX | CONTROL | 'D', detab, NULL} - , -#endif -#endif -#if AEDIT - {CTLX | CONTROL | 'E', entab, NULL} - , -#endif - {CTLX | CONTROL | 'F', filefind, NULL} - , - {CTLX | CONTROL | 'I', insfile, NULL} - , - {CTLX | CONTROL | 'L', lowerregion, NULL} - , - {CTLX | CONTROL | 'M', delmode, NULL} - , - {CTLX | CONTROL | 'N', mvdnwind, NULL} - , - {CTLX | CONTROL | 'O', deblank, NULL} - , - {CTLX | CONTROL | 'P', mvupwind, NULL} - , - {CTLX | CONTROL | 'R', fileread, NULL} - , - {CTLX | CONTROL | 'S', filesave, NULL} - , -#if AEDIT - {CTLX | CONTROL | 'T', trim, NULL} - , -#endif - {CTLX | CONTROL | 'U', upperregion, NULL} - , - {CTLX | CONTROL | 'V', viewfile, NULL} - , - {CTLX | CONTROL | 'W', filewrite, NULL} - , - {CTLX | CONTROL | 'X', (fnp_t) swapmark, NULL} - , - {CTLX | CONTROL | 'Z', shrinkwind, NULL} - , - {CTLX | '?', deskey, NULL} - , - {CTLX | '!', spawn, NULL} - , - {CTLX | '@', pipecmd, NULL} - , - {CTLX | '#', filter_buffer, NULL} - , - {CTLX | '$', execprg, NULL} - , - {CTLX | '=', showcpos, NULL} - , - {CTLX | '(', ctlxlp, NULL} - , - {CTLX | ')', ctlxrp, NULL} - , - {CTLX | '^', enlargewind, NULL} - , - {CTLX | '0', delwind, NULL} - , - {CTLX | '1', onlywind, NULL} - , - {CTLX | '2', splitwind, NULL} - , - {CTLX | 'A', setvar, NULL} - , - {CTLX | 'B', usebuffer, NULL} - , - {CTLX | 'C', spawncli, NULL} - , -#if BSD | SVR4 - {CTLX | 'D', bktoshell, NULL} - , -#endif - {CTLX | 'E', ctlxe, NULL} - , - {CTLX | 'F', setfillcol, NULL} - , - {CTLX | 'K', killbuffer, NULL} - , - {CTLX | 'M', setemode, NULL} - , - {CTLX | 'N', filename, NULL} - , - {CTLX | 'O', nextwind, NULL} - , - {CTLX | 'P', prevwind, NULL} - , -#if PKCODE - {CTLX | 'Q', quote, NULL} - , /* alternative */ -#endif -#if ISRCH - {CTLX | 'R', risearch, NULL} - , - {CTLX | 'S', fisearch, NULL} - , -#endif - {CTLX | 'W', resize, NULL} - , - {CTLX | 'X', nextbuffer, NULL} - , - {CTLX | 'Z', enlargewind, NULL} - , - {META | CONTROL | '?', delbword, NULL}, -#if WORDPRO - {META | CONTROL | 'C', wordcount, NULL} - , -#endif -#if PKCODE - {META | CONTROL | 'D', newsize, NULL} - , -#endif -#if PROC - {META | CONTROL | 'E', execproc, NULL} - , -#endif -#if CFENCE - {META | CONTROL | 'F', getfence, NULL} - , -#endif - {META | CONTROL | 'H', delbword, NULL} - , - {META | CONTROL | 'K', unbindkey, NULL} - , - {META | CONTROL | 'L', reposition, NULL} - , - {META | CONTROL | 'M', delgmode, NULL} - , - {META | CONTROL | 'N', namebuffer, NULL} - , - {META | CONTROL | 'R', qreplace, NULL} - , - {META | CONTROL | 'S', newsize, NULL} - , - {META | CONTROL | 'T', newwidth, NULL} - , - {META | CONTROL | 'V', scrnextdw, NULL} - , -#if WORDPRO - {META | CONTROL | 'W', killpara, NULL} - , -#endif - {META | CONTROL | 'Z', scrnextup, NULL} - , - {META | ' ', (fnp_t) setmark, NULL} - , - {META | '?', help, NULL} - , - {META | '!', reposition, NULL} - , - {META | '.', (fnp_t) setmark, NULL} - , - {META | '>', (fnp_t) gotoeob, NULL} - , - {META | '<', (fnp_t) gotobob, NULL} - , - {META | '~', unmark, NULL} - , -#if APROP - {META | 'A', apro, NULL} - , -#endif - {META | 'B', backword, NULL} - , - {META | 'C', capword, NULL} - , - {META | 'D', delfword, NULL} - , - {META | 'F', forwword, NULL} - , - {META | 'G', gotoline, NULL} - , -#if PKCODE -#if WORDPRO - {META | 'J', justpara, NULL} - , -#endif -#endif - {META | 'K', bindtokey, NULL} - , - {META | 'L', lowerword, NULL} - , - {META | 'M', setgmode, NULL} - , -#if WORDPRO - {META | 'N', gotoeop, NULL} - , - {META | 'P', gotobop, NULL} - , - {META | 'Q', fillpara, NULL} - , -#endif - {META | 'R', sreplace, NULL} - , -#if PKCODE - {META | 'S', forwhunt, NULL} - , -#else -#if BSD - {META | 'S', bktoshell, NULL} - , -#endif -#endif - {META | 'U', upperword, NULL} - , - {META | 'V', (fnp_t) backpage, NULL} - , - {META | 'W', copyregion, NULL} - , - {META | 'X', namedcmd, NULL} - , - {META | 'Z', quickexit, NULL} - , - -#if VT220 - {SPEC | '1', (fnp_t) gotobob /* fisearch */, NULL} - , /* VT220 keys */ - {SPEC | '2', yank, NULL} - , - {SPEC | '3', forwdel /* killregion */, NULL} - , - {SPEC | '4', (fnp_t) gotoeob /* setmark */, NULL} - , - {SPEC | '5', (fnp_t) backpage, NULL} - , - {SPEC | '6', (fnp_t) forwpage, NULL} - , - {SPEC | 'A', (fnp_t) backline, NULL} - , - {SPEC | 'B', (fnp_t) forwline, NULL} - , - {SPEC | 'C', (fnp_t) forwchar, NULL} - , - {SPEC | 'D', (fnp_t) backchar, NULL} - , - {SPEC | 'c', metafn, NULL} - , - {SPEC | 'd', (fnp_t) backchar, NULL} - , - {SPEC | 'e', (fnp_t) forwline, NULL} - , - {SPEC | 'f', (fnp_t) gotobob, NULL} - , - {SPEC | 'h', help, NULL} - , - {SPEC | 'i', cex, NULL} - , -#endif - - /* special internal bindings */ - { SPEC | META | 'W', wrapword , NULL}, /* called on word wrap */ - { SPEC | META | 'C', nullproc , NULL}, /* every command input */ - { SPEC | META | 'R', nullproc , NULL}, /* on file read */ - { SPEC | META | 'X', nullproc , NULL}, /* on window change P.K. */ - - {0, NULL, NULL} -}; diff --git a/ebind.h b/ebind.h deleted file mode 100644 index 4ab490f..0000000 --- a/ebind.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _EBIND_H_ -#define _EBIND_H_ - -#include "names.h" - -/* Structure for the key bindings table. */ -typedef struct key_tab { - unsigned k_code ; /* Key code */ - fnp_t k_fp ; /* Routine to handle it */ - const name_bind *k_nbp ; /* entry in name to function map table */ -} key_tab ; - -/* keycode to function mapping table */ -#define NBINDS 256 /* max # of bound keys */ -extern key_tab keytab[ NBINDS] ; /* key bind to functions table */ - -#endif diff --git a/exec.c b/exec.c index b8f86ad..0bbf488 100644 --- a/exec.c +++ b/exec.c @@ -87,7 +87,7 @@ static int macarg( char *tok, int toksz) ; */ int namedcmd( int f, int n) { /* prompt the user to type a named command */ - mlwrite(": execute-named-cmd "); + mlwrite("execute-named-cmd: "); /* and now get the function name to execute */ const name_bind *nbp = getname() ; @@ -121,7 +121,7 @@ int execcmd( int f, int n) { char *cmdstr ; /* string holding command to execute */ /* get the line wanted */ - status = newmlarg( &cmdstr, ": execute-command-line ", 0) ; + status = newmlarg( &cmdstr, "execute-command-line: ", 0) ; if( status != TRUE) return status ; diff --git a/main.c b/main.c index 50c1757..ec50fa9 100644 --- a/main.c +++ b/main.c @@ -165,6 +165,7 @@ int main(int argc, char **argv) } /* Initialize the editor. */ + init_bindings() ; /* initialize mapping of function to name and key */ vtinit(); /* Display */ mloutfmt = mlwrite ; edinit("main"); /* Buffers, windows */ diff --git a/names.c b/names.c index f50fd42..6494479 100644 --- a/names.c +++ b/names.c @@ -1,13 +1,18 @@ /* names.c -- implements names.h */ #include "names.h" +#define PARANOID 1 + /* Name to function binding table. * * This table gives the names of all the bindable functions and their C - * function address. These are used for the bind-to-key function and macro - * processing. + * function address. These are used for the bind-to-key function and + * command line parsing. */ +#ifdef PARANOID +#include +#endif #include #include "basic.h" @@ -28,216 +33,322 @@ #include "window.h" #include "word.h" + +#define CTL_ CONTROL + const name_bind names[] = { - {" abort-command", ctrlg} , - {" add-global-mode", setgmode} , - {" add-mode", setemode} , + {" abort-command", ctrlg, CTL_ | 'G'} , + {" add-global-mode", setgmode, META | 'M'} , + {" add-mode", setemode, CTLX | 'M'} , #if APROP - {" apropos", apro} , + {" apropos", apro, META | 'A'} , #endif - {" backward-character", (fnp_t) backchar} , - {" begin-macro", ctlxlp} , - {" beginning-of-file", (fnp_t) gotobob} , - {" beginning-of-line", (fnp_t) gotobol} , - {" bind-to-key", bindtokey} , - {" buffer-position", showcpos} , - {"!case-region-lower", lowerregion} , - {"!case-region-upper", upperregion} , - {"!case-word-capitalize", capword} , - {"!case-word-lower", lowerword} , - {"!case-word-upper", upperword} , - {" change-file-name", filename} , - {" change-screen-size", newsize} , - {" change-screen-width", newwidth} , - {" clear-and-redraw", redraw} , - {" clear-message-line", clrmes} , - {" copy-region", copyregion} , + {" backward-character", (fnp_t) backchar, CTL_ | 'B'} , + {" begin-macro", ctlxlp, CTLX | '('} , + {" beginning-of-file", (fnp_t) gotobob, META | '<'} , + {" beginning-of-line", (fnp_t) gotobol, CTL_ | 'A'} , + {" bind-to-key", bindtokey, META | 'K'} , + {" buffer-position", showcpos, CTLX | '='} , + {"!case-region-lower", lowerregion, CTLX | CTL_ | 'L'} , + {"!case-region-upper", upperregion, CTLX | CTL_ | 'U'} , + {"!case-word-capitalize", capword, META | 'C'} , + {"!case-word-lower", lowerword, META | 'L'} , + {"!case-word-upper", upperword, META | 'U'} , + {" change-file-name", filename, CTLX | 'N'} , + {" change-screen-size", newsize, META | CTL_ | 'D'} , /* M^S */ + {" change-screen-width", newwidth, META | CTL_ | 'T'} , + {" clear-and-redraw", redraw, CTL_ | 'L'} , + {" clear-message-line", clrmes, 0} , + {" copy-region", copyregion, META | 'W'} , #if WORDPRO - {" count-words", wordcount} , + {" count-words", wordcount, META | CTL_ | 'C'} , #endif - {" ctlx-prefix", cex} , - {"!delete-blank-lines", deblank} , - {" delete-buffer", killbuffer} , - {" delete-global-mode", delgmode} , - {" delete-mode", delmode} , - {"!delete-next-character", forwdel} , - {"!delete-next-word", delfword} , - {" delete-other-windows", onlywind} , - {"!delete-previous-character", backdel} , - {"!delete-previous-word", delbword} , - {" delete-window", delwind} , - {" describe-bindings", desbind} , - {" describe-key", deskey} , + {" ctlx-prefix", cex, CTL_ | 'X'} , + {"!delete-blank-lines", deblank, CTLX | CTL_ | 'O'} , + {" delete-buffer", killbuffer, CTLX | 'K'} , + {" delete-global-mode", delgmode, META | CTL_ | 'M'} , + {" delete-mode", delmode, CTLX | CTL_ | 'M'} , + {"!delete-next-character", forwdel, CTL_ | 'D'} , + {"!delete-next-word", delfword, META | 'D'} , + {" delete-other-windows", onlywind, CTLX | '1'} , + {"!delete-previous-character", backdel, CTL_ | 'H'} , /* ^? */ + {"!delete-previous-word", delbword, META | CTL_ | 'H'} , /* M^? */ + {" delete-window", delwind, CTLX | '0'} , + {" describe-bindings", desbind, 0} , + {" describe-key", deskey, CTLX | '?'} , #if AEDIT - {"!detab-line", detab} , + {"!detab-line", detab, CTLX | CTL_ | 'D'} , /* X^A */ #endif - {" end-macro", ctlxrp} , - {" end-of-file", (fnp_t) gotoeob} , - {" end-of-line", (fnp_t) gotoeol} , + {" end-macro", ctlxrp, CTLX | ')'} , + {" end-of-file", (fnp_t) gotoeob, META | '>'} , + {" end-of-line", (fnp_t) gotoeol, CTL_ | 'E'} , #if AEDIT - {"!entab-line", entab} , + {"!entab-line", entab, CTLX | CTL_ | 'E'} , #endif - {" exchange-point-and-mark", (fnp_t) swapmark} , - {" execute-buffer", execbuf} , - {" execute-command-line", execcmd} , - {" execute-file", execfile} , - {" execute-macro", ctlxe} , - {" execute-macro-1", cbuf1} , - {" execute-macro-10", cbuf10} , - {" execute-macro-11", cbuf11} , - {" execute-macro-12", cbuf12} , - {" execute-macro-13", cbuf13} , - {" execute-macro-14", cbuf14} , - {" execute-macro-15", cbuf15} , - {" execute-macro-16", cbuf16} , - {" execute-macro-17", cbuf17} , - {" execute-macro-18", cbuf18} , - {" execute-macro-19", cbuf19} , - {" execute-macro-2", cbuf2} , - {" execute-macro-20", cbuf20} , - {" execute-macro-21", cbuf21} , - {" execute-macro-22", cbuf22} , - {" execute-macro-23", cbuf23} , - {" execute-macro-24", cbuf24} , - {" execute-macro-25", cbuf25} , - {" execute-macro-26", cbuf26} , - {" execute-macro-27", cbuf27} , - {" execute-macro-28", cbuf28} , - {" execute-macro-29", cbuf29} , - {" execute-macro-3", cbuf3} , - {" execute-macro-30", cbuf30} , - {" execute-macro-31", cbuf31} , - {" execute-macro-32", cbuf32} , - {" execute-macro-33", cbuf33} , - {" execute-macro-34", cbuf34} , - {" execute-macro-35", cbuf35} , - {" execute-macro-36", cbuf36} , - {" execute-macro-37", cbuf37} , - {" execute-macro-38", cbuf38} , - {" execute-macro-39", cbuf39} , - {" execute-macro-4", cbuf4} , - {" execute-macro-40", cbuf40} , - {" execute-macro-5", cbuf5} , - {" execute-macro-6", cbuf6} , - {" execute-macro-7", cbuf7} , - {" execute-macro-8", cbuf8} , - {" execute-macro-9", cbuf9} , - {" execute-named-command", namedcmd} , + {" exchange-point-and-mark", (fnp_t) swapmark, CTLX | CTL_ | 'X'} , + {" execute-buffer", execbuf, 0} , + {" execute-command-line", execcmd, 0} , + {" execute-file", execfile, 0} , + {" execute-macro", ctlxe, CTLX | 'E'} , + {" execute-macro-1", cbuf1, 0} , + {" execute-macro-10", cbuf10, 0} , + {" execute-macro-11", cbuf11, 0} , + {" execute-macro-12", cbuf12, 0} , + {" execute-macro-13", cbuf13, 0} , + {" execute-macro-14", cbuf14, 0} , + {" execute-macro-15", cbuf15, 0} , + {" execute-macro-16", cbuf16, 0} , + {" execute-macro-17", cbuf17, 0} , + {" execute-macro-18", cbuf18, 0} , + {" execute-macro-19", cbuf19, 0} , + {" execute-macro-2", cbuf2, 0} , + {" execute-macro-20", cbuf20, 0} , + {" execute-macro-21", cbuf21, 0} , + {" execute-macro-22", cbuf22, 0} , + {" execute-macro-23", cbuf23, 0} , + {" execute-macro-24", cbuf24, 0} , + {" execute-macro-25", cbuf25, 0} , + {" execute-macro-26", cbuf26, 0} , + {" execute-macro-27", cbuf27, 0} , + {" execute-macro-28", cbuf28, 0} , + {" execute-macro-29", cbuf29, 0} , + {" execute-macro-3", cbuf3, 0} , + {" execute-macro-30", cbuf30, 0} , + {" execute-macro-31", cbuf31, 0} , + {" execute-macro-32", cbuf32, 0} , + {" execute-macro-33", cbuf33, 0} , + {" execute-macro-34", cbuf34, 0} , + {" execute-macro-35", cbuf35, 0} , + {" execute-macro-36", cbuf36, 0} , + {" execute-macro-37", cbuf37, 0} , + {" execute-macro-38", cbuf38, 0} , + {" execute-macro-39", cbuf39, 0} , + {" execute-macro-4", cbuf4, 0} , + {" execute-macro-40", cbuf40, 0} , + {" execute-macro-5", cbuf5, 0} , + {" execute-macro-6", cbuf6, 0} , + {" execute-macro-7", cbuf7, 0} , + {" execute-macro-8", cbuf8, 0} , + {" execute-macro-9", cbuf9, 0} , + {" execute-named-command", namedcmd, META | 'X'} , #if PROC - {" execute-procedure", execproc} , + {" execute-procedure", execproc, META | CTL_ | 'E'} , #endif - {" execute-program", execprg} , - {" exit-emacs", quit} , + {" execute-program", execprg, CTLX | '$'} , + {" exit-emacs", quit, CTLX | CTL_ | 'C'} , #if WORDPRO - {"!fill-paragraph", fillpara} , + {"!fill-paragraph", fillpara, META | 'Q'} , #endif - {"!filter-buffer", filter_buffer} , - {" find-file", filefind} , - {" forward-character", (fnp_t) forwchar} , - {" goto-line", gotoline} , + {"!filter-buffer", filter_buffer, CTLX | '#'} , + {" find-file", filefind, CTLX | CTL_ | 'F'} , + {" forward-character", (fnp_t) forwchar, CTL_ | 'F'} , + {" goto-line", gotoline, META | 'G'} , #if CFENCE - {" goto-matching-fence", getfence} , + {" goto-matching-fence", getfence, META | CTL_ | 'F'} , #endif - {" grow-window", enlargewind} , - {"!handle-tab", insert_tab} , - {" help", help} , - {" hunt-backward", backhunt} , - {" hunt-forward", forwhunt} , - {" i-shell", spawncli} , + {" grow-window", enlargewind, CTLX | 'Z'} , /* X^ */ + {"!handle-tab", insert_tab, CTL_ | 'I'} , + {" help", help, META | '?'} , + {" hunt-backward", backhunt, 0} , + {" hunt-forward", forwhunt, META | 'S'} , + {" i-shell", spawncli, CTLX | 'C'} , #if ISRCH - {" incremental-search", fisearch} , + {" incremental-search", fisearch, CTLX | 'S'} , #endif - {"!insert-file", insfile} , - {"!insert-space", insspace} , - {"!insert-string", istring} , + {"!insert-file", insfile, CTLX | CTL_ | 'I'} , + {"!insert-space", insspace, CTL_ | 'C'} , + {"!insert-string", istring, 0} , #if WORDPRO #if PKCODE - {"!justify-paragraph", justpara} , + {"!justify-paragraph", justpara, META | 'J'} , #endif - {"!kill-paragraph", killpara} , + {"!kill-paragraph", killpara, META | CTL_ | 'W'} , #endif - {"!kill-region", killregion} , - {"!kill-to-end-of-line", killtext} , - {" list-buffers", listbuffers} , - {" meta-prefix", metafn} , - {" move-window-down", mvdnwind} , - {" move-window-up", mvupwind} , - {" name-buffer", namebuffer} , - {"!newline", insert_newline} , - {"!newline-and-indent", indent} , - {" next-buffer", nextbuffer} , - {" next-line", (fnp_t) forwline} , - {" next-page", (fnp_t) forwpage} , + {"!kill-region", killregion, CTL_ | 'W'} , + {"!kill-to-end-of-line", killtext, CTL_ | 'K'} , + {" list-buffers", listbuffers, CTLX | CTL_ | 'B'} , + {" meta-prefix", metafn, CTL_ | '['} , + {" move-window-down", mvdnwind, CTLX | CTL_ | 'N'} , + {" move-window-up", mvupwind, CTLX | CTL_ | 'P'} , + {" name-buffer", namebuffer, META | CTL_ | 'N'} , + {"!newline", insert_newline, CTL_ | 'M'} , + {"!newline-and-indent", indent, CTL_ | 'J'} , + {" next-buffer", nextbuffer, CTLX | 'X'} , + {" next-line", (fnp_t) forwline, CTL_ | 'N'} , + {" next-page", (fnp_t) forwpage, CTL_ | 'V'} , #if WORDPRO - {" next-paragraph", gotoeop} , + {" next-paragraph", gotoeop, META | 'N'} , #endif - {" next-window", nextwind} , - {" next-word", forwword} , - {" nop", nullproc} , - {"!open-line", openline} , - {" overwrite-string", ovstring} , - {" pipe-command", pipecmd} , - {" previous-line", (fnp_t) backline} , - {" previous-page", (fnp_t) backpage} , + {" next-window", nextwind, CTLX | 'O'} , + {" next-word", forwword, META | 'F'} , + {" nop", nullproc, SPEC | META | 'C'}, /* hook */ + {"!open-line", openline, CTL_ | 'O'} , + {" overwrite-string", ovstring, 0} , + {" pipe-command", pipecmd, CTLX | '@'} , + {" previous-line", (fnp_t) backline, CTL_ | 'P'} , + {" previous-page", (fnp_t) backpage, CTL_ | 'Z'} , /* MV */ #if WORDPRO - {" previous-paragraph", gotobop} , + {" previous-paragraph", gotobop, META | 'P'} , #endif - {" previous-window", prevwind} , - {" previous-word", backword} , - {"!query-replace-string", qreplace} , - {" quick-exit", quickexit} , - {"!quote-character", quote} , - {"!read-file", fileread} , - {" redraw-display", reposition} , - {"!replace-string", sreplace} , - {" resize-window", resize} , - {" restore-window", restwnd} , + {" previous-window", prevwind, CTLX | 'P'} , + {" previous-word", backword, META | 'B'} , + {"!query-replace-string", qreplace, META | CTL_ | 'R'} , + {" quick-exit", quickexit, META | 'Z'} , + {"!quote-character", quote, CTL_ | 'Q'} , /* also XQ */ + {"!read-file", fileread, CTLX | CTL_ | 'R'} , + {" redraw-display", reposition, META | CTL_ | 'L'} , /* M! */ + {"!replace-string", sreplace, META | 'R'} , + {" resize-window", resize, CTLX | 'W'} , + {" restore-window", restwnd, 0} , #if ISRCH - {" reverse-incremental-search", risearch} , + {" reverse-incremental-search", risearch, CTLX | 'R'} , #endif #if PROC - {" run", execproc} , + {" run", execproc, 0} , #endif - {"!save-file", filesave} , - {" save-window", savewnd} , - {" scroll-next-down", scrnextdw} , - {" scroll-next-up", scrnextup} , - {" search-forward", forwsearch} , - {" search-reverse", backsearch} , - {" select-buffer", usebuffer} , - {" set", setvar} , - {" set-fill-column", setfillcol} , - {" set-mark", (fnp_t) setmark} , - {" shell-command", spawn} , - {" shrink-window", shrinkwind} , - {" split-current-window", splitwind} , - {" store-macro", storemac} , + {"!save-file", filesave, CTLX | CTL_ | 'S'} , /* also X^D */ + {" save-window", savewnd, 0} , + {" scroll-next-down", scrnextdw, META | CTL_ | 'V'} , + {" scroll-next-up", scrnextup, META | CTL_ | 'Z'} , + {" search-forward", forwsearch, CTL_ | 'S'} , + {" search-reverse", backsearch, CTL_ | 'R'} , + {" select-buffer", usebuffer, CTLX | 'B'} , + {" set", setvar, CTLX | 'A'} , + {" set-fill-column", setfillcol, CTLX | 'F'} , + {" set-mark", (fnp_t) setmark, META | ' '} , /* M. */ + {" shell-command", spawn, CTLX | '?'} , + {" shrink-window", shrinkwind, CTLX | CTL_ | 'Z'} , + {" split-current-window", splitwind, CTLX | '2'} , + {" store-macro", storemac, 0} , #if PROC - {" store-procedure", storeproc} , + {" store-procedure", storeproc, 0} , #endif #if BSD | SVR4 - {" suspend-emacs", bktoshell} , + {" suspend-emacs", bktoshell, CTLX | 'D'} , /* BSD MS */ #endif - {"!transpose-characters", (fnp_t) twiddle} , + {"!transpose-characters", (fnp_t) twiddle, CTL_ | 'T'} , #if AEDIT - {"!trim-line", trim} , + {"!trim-line", trim, CTLX | CTL_ | 'T'} , #endif - {" unbind-key", unbindkey} , - {" universal-argument", unarg} , - {" unmark-buffer", unmark} , - {" update-screen", upscreen} , - {" view-file", viewfile} , - {" wrap-word", wrapword} , - {" write-file", filewrite} , - {" write-message", writemsg} , - {"!yank", yank} , + {" unbind-key", unbindkey, META | CTL_ | 'K'} , + {" universal-argument", unarg, CTL_ | 'U'} , + {" unmark-buffer", unmark, META | '~'} , + {" update-screen", upscreen, 0} , + {" view-file", viewfile, CTLX | CTL_ | 'V'} , + {"!wrap-word", wrapword, SPEC | META | 'W'} , /* hook */ + {" write-file", filewrite, CTLX | CTL_ | 'W'} , + {" write-message", writemsg, 0} , + {"!yank", yank, CTL_ | 'Y'} , - {" ", NULL} -}; + {" ", NULL, 0}, +/* extra key mapping */ +// { NULL, newsize, META | CTL_ | 'S'}, + { NULL, backdel, CTL_ | '?'}, + { NULL, delbword, META | CTL_ | '?'}, + { NULL, detab, CTLX | CTL_ | 'A'}, + { NULL, enlargewind, CTLX | '^'}, + { NULL, (fnp_t) backpage, META | 'V'}, + { NULL, quote, CTLX | 'Q'}, + { NULL, reposition, META | '!'}, +//detab { NULL, filesave, CTLX | CTL_ | 'D'}, + { NULL, (fnp_t) setmark, META | '.'}, +// { NULL, bktoshell, META | 'S'}, +#if VT220 + { NULL, yank, SPEC | '2'}, /* Insert */ + { NULL, forwdel /* killregion */, SPEC | '3'}, /* Delete */ + { NULL, (fnp_t) backpage, SPEC | '5'}, /* Page Up */ + { NULL, (fnp_t) forwpage, SPEC | '6'}, /* Page Down */ + { NULL, (fnp_t) backline, SPEC | 'A'}, /* Up */ + { NULL, (fnp_t) forwline, SPEC | 'B'}, /* Down */ + { NULL, (fnp_t) forwchar, SPEC | 'C'}, /* Right */ + { NULL, (fnp_t) backchar, SPEC | 'D'}, /* Left */ + { NULL, (fnp_t) gotoeob, SPEC | 'F'}, /* End */ + { NULL, (fnp_t) gotobob, SPEC | 'H'}, /* Home */ + { NULL, help, SPEC | 'P'}, /* F1 */ +#endif + +/* hooks */ + { NULL, nullproc, SPEC | META | 'R'}, /* hook */ + { NULL, nullproc, SPEC | META | 'X'}, /* hook */ + + { NULL, NULL, 0} +} ; + +static int lastidx = 0 ; + +key_tab keytab[ NBINDS] = { + {0, NULL, NULL} +} ; + + +void init_bindings( void) { +/* Add default key bindings */ + key_tab *ktp = keytab ; + const name_bind *nbp ; + for( nbp = names ; nbp->n_func != NULL ; nbp++) { +#ifdef PARANOID + /* Check entries and strict order */ + assert( (nbp->n_name != NULL) && + ((nbp == names) || + (0 > strcmp( bind_name( nbp - 1), bind_name( nbp))) + ) + ) ; +#endif + + /* Add key definition */ + if( nbp->n_keycode) { + ktp->k_code = nbp->n_keycode ; + ktp->k_fp = nbp->n_func ; + ktp->k_nbp = nbp ; + ktp += 1 ; + } + } + +/* memorize position after last valid function entry */ + lastidx = nbp - names ; + +/* Process extra key bindings if any */ + for( nbp++ ; nbp->n_func != NULL ; nbp++) { +#ifdef PARANOID + /* Check entry */ + assert( nbp->n_keycode && (nbp->n_name == NULL)) ; +#endif + + /* Look for corresponding function and add extra key binding */ + const name_bind *fnbp ; + for( fnbp = names ; fnbp->n_func != NULL ; fnbp++) + if( fnbp->n_func == nbp->n_func) { + ktp->k_code = nbp->n_keycode ; + ktp->k_fp = nbp->n_func ; + ktp->k_nbp = fnbp ; + ktp += 1 ; + break ; + } + +#ifdef PARANOID + /* Insure there is a name entry for the keycode and no double keycode */ + assert( fnbp->n_func != NULL) ; + int kcnt = 0 ; + for( key_tab *kktp = keytab ; kktp < ktp ; kktp++) + if( kktp->k_code == (ktp - 1)->k_code) + kcnt += 1 ; + + assert( kcnt == 1) ; +#endif + } + +/* Mark position after last valid key entry */ + ktp->k_code = 0 ; + ktp->k_fp = NULL ; + ktp->k_nbp = NULL ; +} + +#define BINARY 1 const name_bind *fncmatch( char *name) { - int found = ARRAY_SIZE( names) - 1 ; /* index of last entry/ catch all */ +#ifdef BINARY + int found = lastidx ; int low = 0 ; int high = found - 1 ; do { @@ -253,6 +364,14 @@ const name_bind *fncmatch( char *name) { } while( low <= high) ; return &names[ found] ; +#else + const name_bind *nbp ; + for( nbp = names ; nbp->n_func != NULL ; nbp++) + if( !strcmp( name, bind_name( nbp))) + break ; + + return nbp ; +#endif } /* end of names.c */ diff --git a/names.h b/names.h index ba763a4..3e7ce88 100644 --- a/names.h +++ b/names.h @@ -1,20 +1,40 @@ +/* names.h -- mapping of functions to names and keys */ + #ifndef _NAMES_H_ #define _NAMES_H_ /* Generic uEMACS function pointer type */ typedef int (*fnp_t)( int, int) ; + /* Structure for the name binding table. */ -typedef struct name_bind { +typedef struct { const char *n_name ; /* name starting with one tag character */ - fnp_t n_func ; /* function the name is bound to */ + fnp_t n_func ; /* function the name is bound to */ + unsigned n_keycode ; /* default key assignment, 0 when none */ } name_bind ; #define bind_name( p) (&(p)->n_name[ 1]) #define bind_tag( p) (p)->n_name[ 0] +/* Structure for the key bindings table. */ +typedef struct { + unsigned k_code ; /* Key code */ + fnp_t k_fp ; /* Routine to handle it */ + const name_bind *k_nbp ; /* entry in name to function map table */ +} key_tab ; + + extern const name_bind names[] ; /* name to function mapping table */ +/* keycode to function mapping table */ +#define NBINDS 256 /* max # of bound keys */ +extern key_tab keytab[ NBINDS] ; /* key bind to functions table */ + + +void init_bindings( void) ; const name_bind *fncmatch( char *name) ; /* look up by name */ #endif + +/* end of names.h */ From 1aadb53956bf533d8c020d3a17ba22f1322ef66d Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 24 Jul 2021 08:58:23 +0800 Subject: [PATCH 10/37] Remove function pointer from key to bindable table. Emphasize bindable functions in code. Use function name based prompts. --- bind.c | 101 ++++++++++++++++++++++------------------------------- bindable.c | 32 +++-------------- bindable.h | 18 +++++----- exec.c | 2 +- execute.c | 17 +++++---- file.c | 2 +- input.c | 11 ------ input.h | 19 +++++----- names.c | 28 ++++++++++++--- names.h | 17 ++++++--- 10 files changed, 109 insertions(+), 138 deletions(-) diff --git a/bind.c b/bind.c index ffe9010..9734e3c 100644 --- a/bind.c +++ b/bind.c @@ -76,27 +76,22 @@ int help(int f, int n) return TRUE; } -int deskey( int f, int n) { /* describe the command for a certain key */ - int c; /* key to describe */ - char outseq[NSTRING]; /* output buffer for command sequence */ +BINDABLE( deskey) { + char outseq[ NSTRING] ; /* output buffer for command sequence */ - /* prompt the user to type us a key to describe */ - mlwrite(": describe-key "); +/* prompt the user to type a key to describe */ + mlwrite( "describe-key: "); - /* get the command sequence to describe - change it to something we can print as well */ - c = getckey( FALSE) ; - mlwrite( ": describe-key 0x%x, ", c) ; - cmdstr( c, &outseq[ 0]) ; +/* get the command sequence to describe + * change it to something we can print as well */ + int c = getckey( FALSE) ; + cmdstr( c, outseq) ; - /* and dump it out */ - ostring(outseq); - ostring(" "); - - /* output the command sequence */ - ostring( getfname( c, "Not Bound")) ; - return TRUE; +/* output the command sequence */ + mlwrite( "describe-key %s: 0x%x, %s", + outseq, c, getfname( c, "Not Bound")) ; + return TRUE ; } /* @@ -105,12 +100,12 @@ int deskey( int f, int n) { * * int f, n; command arguments [IGNORED] */ -int bindtokey( int f, int n) { +BINDABLE( bindtokey) { key_tab *ktp ; /* pointer into the command table */ char outseq[ 80] ; /* output buffer for keystroke sequence */ /* prompt the user to type in a key to bind */ - mlwrite(": bind-to-key "); + mlwrite("bind-to-key: "); /* get the function name to bind it to */ const name_bind *nbp = getname() ; @@ -123,7 +118,7 @@ int bindtokey( int f, int n) { return FALSE ; } - ostring( " ") ; + mlwrite( "bind-to-key %s: ", bind_name( nbp)) ; /* get the command sequence to bind */ boolean prefix_f = (kfunc == metafn) || (kfunc == cex) || @@ -138,28 +133,27 @@ int bindtokey( int f, int n) { /* if the function is a prefix key */ if( prefix_f) { - /* search for an existing binding for the prefix key */ - for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) - if( ktp->k_fp == kfunc) + /* search and remove all existing binding for the prefix */ + for( ktp = keytab ; ktp->k_code != 0 ; ktp++) + if( ktp->k_nbp == nbp) unbindchar( ktp->k_code) ; - /* reset the appropriate global prefix variable */ - if (kfunc == metafn) - metac = c; - if (kfunc == cex) - ctlxc = c; - if (kfunc == unarg) - reptc = c; - if (kfunc == ctrlg) - abortc = c; + /* set the appropriate global prefix variable */ + if( kfunc == metafn) + metac = c ; + else if( kfunc == cex) + ctlxc = c ; + if( kfunc == unarg) + reptc = c ; + if( kfunc == ctrlg) + abortc = c ; } /* search the table to see if it exists */ - for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) { + for( ktp = keytab ; ktp->k_code != 0 ; ktp++) { if( ktp->k_code == c) { /* it exists, just change it then */ - ktp->k_fp = kfunc ; - ktp->k_nbp = NULL ; + ktp->k_nbp = nbp ; return TRUE ; } } @@ -172,11 +166,9 @@ int bindtokey( int f, int n) { } ktp->k_code = c; /* add keycode */ - ktp->k_fp = kfunc; /* and the function pointer */ - ktp->k_nbp = NULL ; + ktp->k_nbp = nbp ; ++ktp; /* and make sure the next is null */ ktp->k_code = 0; - ktp->k_fp = NULL; ktp->k_nbp = NULL ; return TRUE; @@ -194,7 +186,7 @@ int unbindkey(int f, int n) char outseq[80]; /* output buffer for keystroke sequence */ /* prompt the user to type in a key to unbind */ - mlwrite(": unbind-key "); + mlwrite("unbind-key: "); /* get the command sequence to unbind */ c = getckey(FALSE); /* get a command sequence */ @@ -223,21 +215,19 @@ static boolean unbindchar( unsigned c) { key_tab *ktp ; /* pointer into the command table */ /* search the table to see if the key exists */ - for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) { + for( ktp = keytab ; ktp->k_code != 0 ; ktp++) { if (ktp->k_code == c) { /* save the pointer and scan to the end of the table */ - key_tab *sktp = ktp ; - while( (++ktp)->k_fp != NULL) ; + key_tab *sav_ktp = ktp ; + while( (++ktp)->k_code != 0) ; ktp -= 1 ; /* backup to the last legit entry */ /* copy the last entry to the current one */ - sktp->k_code = ktp->k_code ; - sktp->k_fp = ktp->k_fp ; - sktp->k_nbp = ktp->k_nbp ; + sav_ktp->k_code = ktp->k_code ; + sav_ktp->k_nbp = ktp->k_nbp ; /* null out the last one */ ktp->k_code = 0 ; - ktp->k_fp = NULL ; ktp->k_nbp = NULL ; return TRUE ; } @@ -289,7 +279,7 @@ int apro( int f, int n) { char *mstring ; /* string to match cmd names to */ int status ; /* status return */ - status = newmlarg( &mstring, "Apropos string: ", 0) ; + status = newmlarg( &mstring, "apropos: ", 0) ; if( status == TRUE) { status = buildlist( mstring) ; free( mstring) ; @@ -362,9 +352,8 @@ static int buildlist( char *mstring) { cpos = strlen(outseq); /* search down any keys bound to this */ - ktp = &keytab[0]; - while (ktp->k_fp != NULL) { - if (ktp->k_fp == nptr->n_func) { + for( ktp = keytab ; ktp->k_code != 0 ; ktp++) { + if( ktp->k_nbp == nptr) { /* padd out some spaces */ while (cpos < 28) outseq[cpos++] = ' '; @@ -379,7 +368,6 @@ static int buildlist( char *mstring) { cpos = 0; /* and clear the line */ } - ++ktp; } /* if no key was bound, we need to dump it anyway */ @@ -494,7 +482,7 @@ static void cmdstr( int c, char *seq) { key_tab *getkeybind( unsigned c) { key_tab *ktp ; - for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) + for( ktp = keytab ; ktp->k_code != 0 ; ktp++) if (ktp->k_code == c) break ; @@ -504,17 +492,12 @@ key_tab *getkeybind( unsigned c) { static const char *getfname( unsigned keycode, char *failmsg) { /* takes a key code and gets the name of the function bound to it */ key_tab *kbp = getkeybind( keycode) ; - fnp_t func = kbp->k_fp ; - if( func == NULL) + if( kbp->k_code == 0) return failmsg ; - if( kbp->k_nbp == NULL) - kbp->k_nbp = getfncnb( func) ; - - assert( func == kbp->k_nbp->n_func) ; const char *found = bind_name( kbp->k_nbp) ; assert( *found) ; - return *found ? found : failmsg ; + return found ; } /* diff --git a/bindable.c b/bindable.c index bdf15f3..12a2706 100644 --- a/bindable.c +++ b/bindable.c @@ -1,4 +1,4 @@ -/* bindable.h -- implements bindable.c */ +/* bindable.c -- implements bindable.h */ #include "bindable.h" #include @@ -135,9 +135,9 @@ int ctlxe(int f, int n) } /* - * Abort. - * Beep the beeper. Kill off any keyboard macro, etc., that is in progress. - * Sometimes called as a routine, to do general aborting of stuff. + * abort: + * Beep the beeper. Kill off any keyboard macro, etc., that is in progress. + * Sometimes called as a routine, to do general aborting of stuff. */ int ctrlg( int f, int n) { kbdmode = STOP ; @@ -145,26 +145,4 @@ int ctrlg( int f, int n) { return ABORT ; } -/* user function that does NOTHING */ -int nullproc(int f, int n) -{ - return TRUE; -} - -/* dummy function for binding to meta prefix */ -int metafn(int f, int n) -{ - return TRUE; -} - -/* dummy function for binding to control-x prefix */ -int cex(int f, int n) -{ - return TRUE; -} - -/* dummy function for binding to universal-argument */ -int unarg(int f, int n) -{ - return TRUE; -} +/* end of bindable.c */ diff --git a/bindable.h b/bindable.h index 8e10ac6..fb96884 100644 --- a/bindable.h +++ b/bindable.h @@ -1,11 +1,9 @@ +#include "names.h" + /* functions that can be bound to keys or procedure names */ -int quickexit( int f, int n) ; -int quit( int f, int n) ; -int ctlxlp( int f, int n) ; -int ctlxrp( int f, int n) ; -int ctlxe( int f, int n) ; -int ctrlg( int f, int n) ; -int nullproc( int f, int n) ; -int metafn( int f, int n) ; -int cex( int f, int n) ; -int unarg( int f, int n) ; +BINDABLE( quickexit) ; +BINDABLE( quit) ; +BINDABLE( ctlxlp) ; +BINDABLE( ctlxrp) ; +BINDABLE( ctlxe) ; +BINDABLE( ctrlg) ; diff --git a/exec.c b/exec.c index 0bbf488..d9d82b2 100644 --- a/exec.c +++ b/exec.c @@ -511,7 +511,7 @@ int execproc( int f, int n) { char *name ; /* find out what buffer the user wants to execute */ - status = newmlarg( &name, "Execute procedure: ", sizeof bufn - 2) ; + status = newmlarg( &name, "execute-procedure: ", sizeof bufn - 2) ; if( status != TRUE) return status ; diff --git a/execute.c b/execute.c index 5210577..d1e3ac7 100644 --- a/execute.c +++ b/execute.c @@ -214,18 +214,16 @@ int execute( int c, int f, int n) { /* if the keystroke is a bound function...do it */ key_tab *ktp = getkeybind( c) ; - fnp_t execfunc = ktp->k_fp ; - if( execfunc != NULL) { + if( ktp->k_code != 0) { thisflag = 0 ; - if( ktp->k_nbp == NULL) - ktp->k_nbp = getfncnb( execfunc) ; - - assert( ktp->k_nbp->n_func == execfunc) ; + assert( ktp->k_nbp != NULL) ; char tag = bind_tag( ktp->k_nbp) ; if( (tag & 1) && (curbp->b_mode & MDVIEW)) status = rdonly() ; - else + else { + fnp_t execfunc = ktp->k_nbp->n_func ; status = execfunc( f, n) ; + } lastflag = thisflag ; return status ; @@ -340,11 +338,12 @@ void kbd_loop( void) { newc = getcmd() ; update( FALSE) ; do { + key_tab *ktp ; fnp_t execfunc ; if( c == newc - && (execfunc = getkeybind( c)->k_fp) != NULL - && execfunc != insert_newline + && (ktp = getkeybind( c))->k_code == c + && (execfunc = ktp->k_nbp->k_func) != insert_newline && execfunc != insert_tab) newc = getcmd() ; else diff --git a/file.c b/file.c index fbdfe84..68fe898 100644 --- a/file.c +++ b/file.c @@ -134,7 +134,7 @@ int filefind( int f, int n) { if( restflag) /* don't allow this command if restricted */ return resterr() ; - status = newmlarg( &fname, "Find file: ", sizeof( fname_t)) ; + status = newmlarg( &fname, "find-file: ", sizeof( fname_t)) ; if( status == TRUE) { status = getfile( fname, TRUE) ; free( fname) ; diff --git a/input.c b/input.c index fc67ece..aee6f90 100644 --- a/input.c +++ b/input.c @@ -157,17 +157,6 @@ int ectoc( int c) { return c ; } -const name_bind *getfncnb( fnp_t func) { - const name_bind *nptr ; /* pointer into the name binding table */ - - /* skim through the table, looking for a match */ - for( nptr = names ; nptr->n_func != NULL ; nptr++) - if (nptr->n_func == func) - break ; - - return nptr ; -} - /* * get a command name from the command line. Command completion means * that pressing a will attempt to complete an unfinished command diff --git a/input.h b/input.h index 25a907f..bcb125a 100644 --- a/input.h +++ b/input.h @@ -10,16 +10,16 @@ typedef enum { extern kbdstate kbdmode ; /* current keyboard macro mode */ extern int lastkey ; /* last keystoke */ -extern int kbdrep ; /* number of repetitions */ -extern int kbdm[] ; /* Holds kayboard macro data */ +extern int kbdrep ; /* number of repetitions */ +extern int kbdm[] ; /* Holds kayboard macro data */ extern int *kbdptr ; /* current position in keyboard buf */ extern int *kbdend ; /* ptr to end of the keyboard */ -extern int metac; /* current meta character */ -extern int ctlxc; /* current control X prefix char */ -extern int reptc; /* current universal repeat char */ -extern int abortc; /* current abort command char */ -extern const int nlc ; /* end of input char */ +extern int metac ; /* current meta character */ +extern int ctlxc ; /* current control X prefix char */ +extern int reptc ; /* current universal repeat char */ +extern int abortc ; /* current abort command char */ +extern const int nlc ; /* end of input char */ void ue_system( const char *cmd) ; @@ -28,9 +28,8 @@ int newmlarg( char **outbufref, const char *prompt, int size) ; int newmlargt( char **outbufref, const char *prompt, int size) ; int ectoc( int c) ; -/* Look up in names to function association table */ -const name_bind *getname( void) ; /* interactively */ -const name_bind *getfncnb( fnp_t func) ; /* by function */ +/* Get a command name from the command line or interactively */ +const name_bind *getname( void) ; int tgetc( void) ; int get1key( void) ; diff --git a/names.c b/names.c index 6494479..bb8faa1 100644 --- a/names.c +++ b/names.c @@ -204,7 +204,7 @@ const name_bind names[] = { {" reverse-incremental-search", risearch, CTLX | 'R'} , #endif #if PROC - {" run", execproc, 0} , + {" run", execproc, 0} , // alias of execute-procedure #endif {"!save-file", filesave, CTLX | CTL_ | 'S'} , /* also X^D */ {" save-window", savewnd, 0} , @@ -278,7 +278,7 @@ const name_bind names[] = { static int lastidx = 0 ; key_tab keytab[ NBINDS] = { - {0, NULL, NULL} + {0, NULL} } ; @@ -299,7 +299,6 @@ void init_bindings( void) { /* Add key definition */ if( nbp->n_keycode) { ktp->k_code = nbp->n_keycode ; - ktp->k_fp = nbp->n_func ; ktp->k_nbp = nbp ; ktp += 1 ; } @@ -320,7 +319,6 @@ void init_bindings( void) { for( fnbp = names ; fnbp->n_func != NULL ; fnbp++) if( fnbp->n_func == nbp->n_func) { ktp->k_code = nbp->n_keycode ; - ktp->k_fp = nbp->n_func ; ktp->k_nbp = fnbp ; ktp += 1 ; break ; @@ -340,7 +338,6 @@ void init_bindings( void) { /* Mark position after last valid key entry */ ktp->k_code = 0 ; - ktp->k_fp = NULL ; ktp->k_nbp = NULL ; } @@ -374,4 +371,25 @@ const name_bind *fncmatch( char *name) { #endif } + +/* user function that does NOTHING */ +BINDABLE( nullproc) { + return TRUE ; +} + +/* dummy function for binding to meta prefix */ +BINDABLE( metafn) { + return TRUE ; +} + +/* dummy function for binding to control-x prefix */ +BINDABLE( cex) { + return TRUE ; +} + +/* dummy function for binding to universal-argument */ +BINDABLE( unarg) { + return TRUE ; +} + /* end of names.c */ diff --git a/names.h b/names.h index 3e7ce88..1d0b4ac 100644 --- a/names.h +++ b/names.h @@ -3,8 +3,10 @@ #ifndef _NAMES_H_ #define _NAMES_H_ -/* Generic uEMACS function pointer type */ -typedef int (*fnp_t)( int, int) ; +/* Bindable uEMACS function pointer type and definition template */ +#define BINDABLE( fname) int fname( int f, int n) + +typedef BINDABLE( (*fnp_t)) ; /* Structure for the name binding table. */ @@ -14,13 +16,12 @@ typedef struct { unsigned n_keycode ; /* default key assignment, 0 when none */ } name_bind ; -#define bind_name( p) (&(p)->n_name[ 1]) -#define bind_tag( p) (p)->n_name[ 0] +#define bind_name( p) (&( p)->n_name[ 1]) +#define bind_tag( p) ( p)->n_name[ 0] /* Structure for the key bindings table. */ typedef struct { unsigned k_code ; /* Key code */ - fnp_t k_fp ; /* Routine to handle it */ const name_bind *k_nbp ; /* entry in name to function map table */ } key_tab ; @@ -35,6 +36,12 @@ extern key_tab keytab[ NBINDS] ; /* key bind to functions table */ void init_bindings( void) ; const name_bind *fncmatch( char *name) ; /* look up by name */ +/* bindable functions mapped to prefix keys and hooks */ +BINDABLE( nullproc) ; +BINDABLE( metafn) ; +BINDABLE( cex) ; +BINDABLE( unarg) ; + #endif /* end of names.h */ From ba1bfcd0db884e2a055d05130468293bbc245860 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 24 Jul 2021 13:57:51 +0800 Subject: [PATCH 11/37] Fix filename completion after prompt adaptation. --- input.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/input.c b/input.c index aee6f90..616d670 100644 --- a/input.c +++ b/input.c @@ -520,9 +520,9 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) static char tmp[] = "/tmp/meXXXXXX"; FILE *tmpf = NULL; #endif -/* Look for "Find file: ", "View file: ", "Insert file: ", "Write file: ", +/* Look for "find-file: ", "View file: ", "Insert file: ", "Write file: ", ** "Read file: ", "Execute file: " */ - file_f = NULL != strstr( prompt, " file: ") ; + file_f = NULL != strstr( prompt, "file: ") ; #endif cpos = 0; From 735aefc166a048401788b13e8628dc5703bb781b Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 24 Jul 2021 16:34:54 +0800 Subject: [PATCH 12/37] Manage key binding table dynamically. Avoid deleting or binding to active prefix keys. --- bind.c | 127 ++++++++++++++++++++++---------------------------------- main.c | 5 ++- names.c | 119 ++++++++++++++++++++++++++++++++++++---------------- names.h | 14 ++++--- 4 files changed, 146 insertions(+), 119 deletions(-) diff --git a/bind.c b/bind.c index 9734e3c..b2c7ba7 100644 --- a/bind.c +++ b/bind.c @@ -36,7 +36,6 @@ static int buildlist( char *mstring) ; static void cmdstr( int c, char *seq) ; static unsigned int getckey( int mflag) ; static unsigned int stock( char *keyname) ; -static boolean unbindchar( unsigned c) ; static const char *getfname( unsigned keycode, char *failmsg) ; @@ -123,7 +122,7 @@ BINDABLE( bindtokey) { /* get the command sequence to bind */ boolean prefix_f = (kfunc == metafn) || (kfunc == cex) || (kfunc == unarg) || (kfunc == ctrlg) ; - unsigned c = getckey( prefix_f) ; + int c = getckey( prefix_f) ; /* change it to something we can print as well */ cmdstr( c, outseq) ; @@ -131,12 +130,26 @@ BINDABLE( bindtokey) { /* and dump it out */ ostring( outseq) ; +/* key sequence can't be an active prefix key */ + if( c == metac || c == ctlxc || c == reptc || c == abortc) { + if( (c == metac && kfunc == metafn) + || (c == ctlxc && kfunc == cex) + || (c == reptc && kfunc == unarg) + || (c == abortc && kfunc == ctrlg)) + return TRUE ; + + mlwrite( "(Can't bind to active prefix)") ; + return FALSE ; + } + /* if the function is a prefix key */ if( prefix_f) { - /* search and remove all existing binding for the prefix */ + /* remove existing binding for the prefix */ for( ktp = keytab ; ktp->k_code != 0 ; ktp++) - if( ktp->k_nbp == nbp) - unbindchar( ktp->k_code) ; + if( ktp->k_nbp == nbp) { + delkeybinding( ktp->k_code) ; + break ; + } /* set the appropriate global prefix variable */ if( kfunc == metafn) @@ -149,29 +162,13 @@ BINDABLE( bindtokey) { abortc = c ; } -/* search the table to see if it exists */ - for( ktp = keytab ; ktp->k_code != 0 ; ktp++) { - if( ktp->k_code == c) { - /* it exists, just change it then */ - ktp->k_nbp = nbp ; - return TRUE ; - } - } - -/* otherwise we need to add it to the end */ - /* if we run out of binding room, bitch */ - if( ktp >= &keytab[ NBINDS]) { + ktp = setkeybinding( c, nbp) ; + if( ktp->k_code == 0) { mlwrite( "Binding table FULL!") ; - return FALSE ; - } + return FALSE ; + } - ktp->k_code = c; /* add keycode */ - ktp->k_nbp = nbp ; - ++ktp; /* and make sure the next is null */ - ktp->k_code = 0; - ktp->k_nbp = NULL ; - - return TRUE; + return TRUE ; } /* @@ -180,60 +177,34 @@ BINDABLE( bindtokey) { * * int f, n; command arguments [IGNORED] */ -int unbindkey(int f, int n) -{ - int c; /* command key to unbind */ - char outseq[80]; /* output buffer for keystroke sequence */ +BINDABLE( unbindkey) { + char outseq[ 80] ; /* output buffer for keystroke sequence */ - /* prompt the user to type in a key to unbind */ - mlwrite("unbind-key: "); +/* prompt the user to type in a key to unbind */ + mlwrite( "unbind-key: ") ; - /* get the command sequence to unbind */ - c = getckey(FALSE); /* get a command sequence */ +/* get the command sequence to unbind */ + int c = getckey( FALSE) ; /* get a command sequence */ - /* change it to something we can print as well */ - cmdstr(c, &outseq[0]); +/* change it to something we can print as well */ + cmdstr( c, outseq) ; - /* and dump it out */ - ostring(outseq); +/* and dump it out */ + ostring( outseq) ; - /* if it isn't bound, bitch */ - if (unbindchar(c) == FALSE) { - mlwrite("(Key not bound)"); - return FALSE; +/* prefix key sequence can't be undound, just redefined */ + if( c == reptc || c == abortc) { + mlwrite( "(Can't unbind prefix)") ; + return FALSE ; + } + +/* if it isn't bound, bitch */ + if( delkeybinding( c) == FALSE) { + mlwrite( "(Key not bound)") ; + return FALSE ; } - return TRUE; -} - - -/* - * unbindchar() - * - * int c; command key to unbind - */ -static boolean unbindchar( unsigned c) { - key_tab *ktp ; /* pointer into the command table */ - -/* search the table to see if the key exists */ - for( ktp = keytab ; ktp->k_code != 0 ; ktp++) { - if (ktp->k_code == c) { - /* save the pointer and scan to the end of the table */ - key_tab *sav_ktp = ktp ; - while( (++ktp)->k_code != 0) ; - ktp -= 1 ; /* backup to the last legit entry */ - - /* copy the last entry to the current one */ - sav_ktp->k_code = ktp->k_code ; - sav_ktp->k_nbp = ktp->k_nbp ; - - /* null out the last one */ - ktp->k_code = 0 ; - ktp->k_nbp = NULL ; - return TRUE ; - } - } - - return FALSE ; + + return TRUE ; } #if APROP @@ -452,8 +423,10 @@ static void cmdstr( int c, char *seq) { /* apply ^X sequence if needed */ if (c & CTLX) { - *ptr++ = '^'; - *ptr++ = 'X'; + if( ctlxc & CONTROL) + *ptr++ = '^' ; + + *ptr++ = ctlxc & 0x1FFFFF ; } /* apply SPEC sequence if needed */ @@ -469,7 +442,7 @@ static void cmdstr( int c, char *seq) { /* and output the final sequence */ - *ptr++ = c & 255; /* strip the prefixes */ + *ptr++ = c & 0x1FFFFF ; /* strip the prefixes */ *ptr = 0; /* terminate the string */ } diff --git a/main.c b/main.c index ec50fa9..dd507c2 100644 --- a/main.c +++ b/main.c @@ -165,7 +165,10 @@ int main(int argc, char **argv) } /* Initialize the editor. */ - init_bindings() ; /* initialize mapping of function to name and key */ + if( !init_bindings()) { /* initialize mapping of function to name and key */ + return( EXIT_FAILURE) ; + } + vtinit(); /* Display */ mloutfmt = mlwrite ; edinit("main"); /* Buffers, windows */ diff --git a/names.c b/names.c index bb8faa1..4b94876 100644 --- a/names.c +++ b/names.c @@ -1,8 +1,6 @@ /* names.c -- implements names.h */ #include "names.h" -#define PARANOID 1 - /* Name to function binding table. * * This table gives the names of all the bindable functions and their C @@ -10,9 +8,8 @@ * command line parsing. */ -#ifdef PARANOID #include -#endif +#include #include #include "basic.h" @@ -216,7 +213,7 @@ const name_bind names[] = { {" set", setvar, CTLX | 'A'} , {" set-fill-column", setfillcol, CTLX | 'F'} , {" set-mark", (fnp_t) setmark, META | ' '} , /* M. */ - {" shell-command", spawn, CTLX | '?'} , + {" shell-command", spawn, CTLX | '!'} , {" shrink-window", shrinkwind, CTLX | CTL_ | 'Z'} , {" split-current-window", splitwind, CTLX | '2'} , {" store-macro", storemac, 0} , @@ -275,77 +272,129 @@ const name_bind names[] = { { NULL, NULL, 0} } ; -static int lastidx = 0 ; - -key_tab keytab[ NBINDS] = { - {0, NULL} -} ; +static int lastnmidx = 0 ; /* index of last name entry */ -void init_bindings( void) { +key_tab *keytab ; +static int ktsize = 140 ; /* last check: need at least 133 + 1 */ + + +boolean init_bindings( void) { +/* allocate table */ + keytab = malloc( ktsize * sizeof *keytab) ; + if( keytab == NULL) + return FALSE ; + +/* insert end of table mark */ + keytab->k_code = 0 ; + keytab->k_nbp = NULL ; + /* Add default key bindings */ - key_tab *ktp = keytab ; const name_bind *nbp ; for( nbp = names ; nbp->n_func != NULL ; nbp++) { -#ifdef PARANOID /* Check entries and strict order */ assert( (nbp->n_name != NULL) && ((nbp == names) || (0 > strcmp( bind_name( nbp - 1), bind_name( nbp))) ) ) ; -#endif /* Add key definition */ if( nbp->n_keycode) { - ktp->k_code = nbp->n_keycode ; - ktp->k_nbp = nbp ; - ktp += 1 ; + key_tab *ktp = setkeybinding( nbp->n_keycode, nbp) ; + /* check it was indeed an insertion at end of table not a + * key code re-definition */ + assert( (++ktp)->k_code == 0) ; } } /* memorize position after last valid function entry */ - lastidx = nbp - names ; + lastnmidx = nbp - names ; /* Process extra key bindings if any */ for( nbp++ ; nbp->n_func != NULL ; nbp++) { -#ifdef PARANOID /* Check entry */ assert( nbp->n_keycode && (nbp->n_name == NULL)) ; -#endif /* Look for corresponding function and add extra key binding */ const name_bind *fnbp ; for( fnbp = names ; fnbp->n_func != NULL ; fnbp++) if( fnbp->n_func == nbp->n_func) { - ktp->k_code = nbp->n_keycode ; - ktp->k_nbp = fnbp ; - ktp += 1 ; + setkeybinding( nbp->n_keycode, fnbp) ; break ; } -#ifdef PARANOID - /* Insure there is a name entry for the keycode and no double keycode */ + /* Insure there is a name entry for the keycode */ assert( fnbp->n_func != NULL) ; - int kcnt = 0 ; - for( key_tab *kktp = keytab ; kktp < ktp ; kktp++) - if( kktp->k_code == (ktp - 1)->k_code) - kcnt += 1 ; - - assert( kcnt == 1) ; -#endif } -/* Mark position after last valid key entry */ - ktp->k_code = 0 ; + return TRUE ; +} + +key_tab *setkeybinding( unsigned key, const name_bind *nbp) { + key_tab *ktp ; + +/* search the table to see if it exists */ + for( ktp = keytab ; ktp->k_code != 0 ; ktp++) + if( ktp->k_code == key) { + /* it exists, just change it then */ + ktp->k_nbp = nbp ; + return ktp ; + } + +/* otherwise we need to add it to the end */ + /* check if the end marker is at the end of the table */ + if( ktp == &keytab[ ktsize - 1]) { + /* out of binding room */ + int newsize = ktsize + 10 ; + key_tab *newkeytab = realloc( keytab, newsize * sizeof *keytab) ; + if( newkeytab == NULL) + /* out of space */ + return ktp ; + + keytab = newkeytab ; + ktp = &keytab[ ktsize - 1] ; + ktsize = newsize ; + } + + ktp->k_code = key ; /* add keycode */ + ktp->k_nbp = nbp ; + ++ktp ; /* and make sure the next is null */ + ktp->k_code = 0 ; ktp->k_nbp = NULL ; + return ktp - 1 ; +} + +boolean delkeybinding( unsigned key) { + key_tab *ktp ; /* pointer into the key binding table */ + +/* search the table to see if the key exists */ + for( ktp = keytab ; ktp->k_code != 0 ; ktp++) { + if( ktp->k_code == key) { + /* save the pointer and scan to the end of the table */ + key_tab *sav_ktp = ktp ; + while( (++ktp)->k_code != 0) ; + ktp -= 1 ; /* backup to the last legit entry */ + + /* copy the last entry to the current one */ + sav_ktp->k_code = ktp->k_code ; + sav_ktp->k_nbp = ktp->k_nbp ; + + /* null out the last one */ + ktp->k_code = 0 ; + ktp->k_nbp = NULL ; + return TRUE ; + } + } + + return FALSE ; } #define BINARY 1 const name_bind *fncmatch( char *name) { #ifdef BINARY - int found = lastidx ; + int found = lastnmidx ; int low = 0 ; int high = found - 1 ; do { diff --git a/names.h b/names.h index 1d0b4ac..11ed227 100644 --- a/names.h +++ b/names.h @@ -3,6 +3,9 @@ #ifndef _NAMES_H_ #define _NAMES_H_ +#include "retcode.h" + + /* Bindable uEMACS function pointer type and definition template */ #define BINDABLE( fname) int fname( int f, int n) @@ -26,14 +29,13 @@ typedef struct { } key_tab ; -extern const name_bind names[] ; /* name to function mapping table */ - -/* keycode to function mapping table */ -#define NBINDS 256 /* max # of bound keys */ -extern key_tab keytab[ NBINDS] ; /* key bind to functions table */ +extern const name_bind names[] ; /* name to function mapping table */ +extern key_tab *keytab ; /* key bind to functions table */ -void init_bindings( void) ; +boolean init_bindings( void) ; +key_tab *setkeybinding( unsigned key, const name_bind *nbp) ; +boolean delkeybinding( unsigned key) ; const name_bind *fncmatch( char *name) ; /* look up by name */ /* bindable functions mapped to prefix keys and hooks */ From 22bbd0417c50bea8bd86e8e86d36faf4409f440c Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Fri, 30 Jul 2021 09:30:12 +0800 Subject: [PATCH 13/37] Revise types for names and keys bindings. --- bind.c | 37 +++++++++++++++++++------------------ bind.h | 2 +- exec.c | 4 ++-- execute.c | 4 ++-- input.c | 8 ++++---- input.h | 2 +- main.c | 23 +++++++++-------------- names.c | 22 +++++++++++----------- names.h | 25 ++++++++++++++----------- 9 files changed, 63 insertions(+), 64 deletions(-) diff --git a/bind.c b/bind.c index b2c7ba7..1821861 100644 --- a/bind.c +++ b/bind.c @@ -39,15 +39,15 @@ static unsigned int stock( char *keyname) ; static const char *getfname( unsigned keycode, char *failmsg) ; -int help(int f, int n) -{ /* give me some help!!!! - bring up a fake buffer and read the help file - into it with view mode */ - struct buffer *bp; /* buffer pointer to help */ +BINDABLE( help) { +/* give me some help!!!! + bring up a fake buffer and read the help file into it with view mode */ char *fname = NULL; /* ptr to file returned by flook() */ - /* first check if we are already here */ - bp = bfind( hlpfname, FALSE, BFINVS); +/* first check if we are already here */ + buffer_p bp = bfind( hlpfname, FALSE, BFINVS); + if( bp == curbp) + return TRUE ; if (bp == NULL) { fname = flook( hlpfname, FALSE); @@ -57,16 +57,17 @@ int help(int f, int n) } } - /* split the current window to make room for the help stuff */ - if (splitwind(FALSE, 1) == FALSE) - return FALSE; +/* split the current window to make room for the help stuff */ + if( wheadp->w_wndp == NULL /* One window */ + && splitwind( FALSE, 1) == FALSE) /* Split it */ + return FALSE ; if (bp == NULL) { /* and read the stuff in */ if (getfile(fname, FALSE) == FALSE) return FALSE; } else - swbuffer(bp); + swbuffer( bp) ; /* make this window in VIEW mode, update all mode lines */ curwp->w_bufp->b_mode |= MDVIEW; @@ -100,14 +101,14 @@ BINDABLE( deskey) { * int f, n; command arguments [IGNORED] */ BINDABLE( bindtokey) { - key_tab *ktp ; /* pointer into the command table */ + kbind_p ktp ; /* pointer into the command table */ char outseq[ 80] ; /* output buffer for keystroke sequence */ /* prompt the user to type in a key to bind */ mlwrite("bind-to-key: "); /* get the function name to bind it to */ - const name_bind *nbp = getname() ; + nbind_p nbp = getname() ; if( nbp == NULL) /* abort */ return FALSE ; @@ -268,8 +269,8 @@ int apro( int f, int n) { static int buildlist( char *mstring) { #endif struct window *wp; /* scanning pointer to windows */ - key_tab *ktp; /* pointer into the command table */ - const name_bind *nptr;/* pointer into the name binding table */ + kbind_p ktp; /* pointer into the command table */ + nbind_p nptr;/* pointer into the name binding table */ struct buffer *bp; /* buffer to put binding list into */ char outseq[80]; /* output buffer for keystroke sequence */ @@ -452,8 +453,8 @@ static void cmdstr( int c, char *seq) { * * int c; key to find what is bound to it */ -key_tab *getkeybind( unsigned c) { - key_tab *ktp ; +kbind_p getkeybind( unsigned c) { + kbind_p ktp ; for( ktp = keytab ; ktp->k_code != 0 ; ktp++) if (ktp->k_code == c) @@ -464,7 +465,7 @@ key_tab *getkeybind( unsigned c) { static const char *getfname( unsigned keycode, char *failmsg) { /* takes a key code and gets the name of the function bound to it */ - key_tab *kbp = getkeybind( keycode) ; + kbind_p kbp = getkeybind( keycode) ; if( kbp->k_code == 0) return failmsg ; diff --git a/bind.h b/bind.h index 8a4fe90..bacba1e 100644 --- a/bind.h +++ b/bind.h @@ -19,7 +19,7 @@ int desbind( int f, int n) ; int startup( const char *fname) ; /* find a key to function association in the key to function mapping table */ -key_tab *getkeybind( unsigned keycode) ; /* by key code */ +kbind_p getkeybind( unsigned keycode) ; /* by key code */ const char *transbind( char *skey) ; /* by string representation of key */ #endif diff --git a/exec.c b/exec.c index d9d82b2..0f2f3a1 100644 --- a/exec.c +++ b/exec.c @@ -90,7 +90,7 @@ int namedcmd( int f, int n) { mlwrite("execute-named-cmd: "); /* and now get the function name to execute */ - const name_bind *nbp = getname() ; + nbind_p nbp = getname() ; if( nbp == NULL) /* abort */ return FALSE ; @@ -190,7 +190,7 @@ static int docmd( char *cline) { } /* and match the token to see if it exists */ - const name_bind *nbp = fncmatch( tkn) ; + nbind_p nbp = fncmatch( tkn) ; fnp_t fnc = nbp->n_func ; if( fnc == NULL) { mlwrite("(No such Function)"); diff --git a/execute.c b/execute.c index d1e3ac7..f0738a6 100644 --- a/execute.c +++ b/execute.c @@ -213,7 +213,7 @@ int execute( int c, int f, int n) { int status ; /* if the keystroke is a bound function...do it */ - key_tab *ktp = getkeybind( c) ; + kbind_p ktp = getkeybind( c) ; if( ktp->k_code != 0) { thisflag = 0 ; assert( ktp->k_nbp != NULL) ; @@ -338,7 +338,7 @@ void kbd_loop( void) { newc = getcmd() ; update( FALSE) ; do { - key_tab *ktp ; + kbind_p ktp ; fnp_t execfunc ; if( c == newc diff --git a/input.c b/input.c index 616d670..a095cc2 100644 --- a/input.c +++ b/input.c @@ -162,11 +162,11 @@ int ectoc( int c) { * that pressing a will attempt to complete an unfinished command * name if it is unique. */ -const name_bind *getname( void) { +nbind_p getname( void) { int cpos; /* current column on screen output */ - const name_bind *ffp; /* first ptr to entry in name binding table */ - const name_bind *cffp; /* current ptr to entry in name binding table */ - const name_bind *lffp; /* last ptr to entry in name binding table */ + nbind_p ffp; /* first ptr to entry in name binding table */ + nbind_p cffp; /* current ptr to entry in name binding table */ + nbind_p lffp; /* last ptr to entry in name binding table */ char buf[NSTRING]; /* buffer to hold tentative command name */ /* starting at the beginning of the string buffer */ diff --git a/input.h b/input.h index bcb125a..b9bd06d 100644 --- a/input.h +++ b/input.h @@ -29,7 +29,7 @@ int newmlargt( char **outbufref, const char *prompt, int size) ; int ectoc( int c) ; /* Get a command name from the command line or interactively */ -const name_bind *getname( void) ; +nbind_p getname( void) ; int tgetc( void) ; int get1key( void) ; diff --git a/main.c b/main.c index dd507c2..253fa7a 100644 --- a/main.c +++ b/main.c @@ -164,15 +164,10 @@ int main(int argc, char **argv) } } - /* Initialize the editor. */ - if( !init_bindings()) { /* initialize mapping of function to name and key */ - return( EXIT_FAILURE) ; - } - - vtinit(); /* Display */ + vtinit() ; /* Display */ mloutfmt = mlwrite ; - edinit("main"); /* Buffers, windows */ - varinit(); /* user variables */ + edinit( "main") ; /* Bindings, buffers, windows */ + varinit() ; /* user variables */ viewflag = FALSE; /* view mode defaults off in command line */ gotoflag = FALSE; /* set to off to begin with */ @@ -331,14 +326,14 @@ int main(int argc, char **argv) * as an argument, because the main routine may have been told to read in a * file by default, and we want the buffer name to be right. */ -static void edinit(char *bname) -{ - struct buffer *bp; +static void edinit( char *bname) { + buffer_p bp; struct window *wp; - if( NULL == (bp = bfind( bname, TRUE, 0)) /* First buffer */ - || NULL == (blistp = bfind( "*List*", TRUE, BFINVS)) /* Buffer list buffer */ - || NULL == (wp = (struct window *) malloc( sizeof( struct window)))) { /* First window */ + if( !init_bindings() /* initialize mapping of function to name and key */ + || NULL == (bp = bfind( bname, TRUE, 0)) /* First buffer */ + || NULL == (blistp = bfind( "*List*", TRUE, BFINVS)) /* Buffer list */ + || NULL == (wp = malloc( sizeof *wp))) { /* First window */ fputs( "First initialisation failed!\n", stderr) ; exit( EXIT_FAILURE) ; } diff --git a/names.c b/names.c index 4b94876..c6b93e2 100644 --- a/names.c +++ b/names.c @@ -275,7 +275,7 @@ const name_bind names[] = { static int lastnmidx = 0 ; /* index of last name entry */ -key_tab *keytab ; +kbind_p keytab ; static int ktsize = 140 ; /* last check: need at least 133 + 1 */ @@ -290,7 +290,7 @@ boolean init_bindings( void) { keytab->k_nbp = NULL ; /* Add default key bindings */ - const name_bind *nbp ; + nbind_p nbp ; for( nbp = names ; nbp->n_func != NULL ; nbp++) { /* Check entries and strict order */ assert( (nbp->n_name != NULL) && @@ -301,7 +301,7 @@ boolean init_bindings( void) { /* Add key definition */ if( nbp->n_keycode) { - key_tab *ktp = setkeybinding( nbp->n_keycode, nbp) ; + kbind_p ktp = setkeybinding( nbp->n_keycode, nbp) ; /* check it was indeed an insertion at end of table not a * key code re-definition */ assert( (++ktp)->k_code == 0) ; @@ -317,7 +317,7 @@ boolean init_bindings( void) { assert( nbp->n_keycode && (nbp->n_name == NULL)) ; /* Look for corresponding function and add extra key binding */ - const name_bind *fnbp ; + nbind_p fnbp ; for( fnbp = names ; fnbp->n_func != NULL ; fnbp++) if( fnbp->n_func == nbp->n_func) { setkeybinding( nbp->n_keycode, fnbp) ; @@ -331,8 +331,8 @@ boolean init_bindings( void) { return TRUE ; } -key_tab *setkeybinding( unsigned key, const name_bind *nbp) { - key_tab *ktp ; +kbind_p setkeybinding( unsigned key, nbind_p nbp) { + kbind_p ktp ; /* search the table to see if it exists */ for( ktp = keytab ; ktp->k_code != 0 ; ktp++) @@ -347,7 +347,7 @@ key_tab *setkeybinding( unsigned key, const name_bind *nbp) { if( ktp == &keytab[ ktsize - 1]) { /* out of binding room */ int newsize = ktsize + 10 ; - key_tab *newkeytab = realloc( keytab, newsize * sizeof *keytab) ; + kbind_p newkeytab = realloc( keytab, newsize * sizeof *keytab) ; if( newkeytab == NULL) /* out of space */ return ktp ; @@ -366,13 +366,13 @@ key_tab *setkeybinding( unsigned key, const name_bind *nbp) { } boolean delkeybinding( unsigned key) { - key_tab *ktp ; /* pointer into the key binding table */ + kbind_p ktp ; /* pointer into the key binding table */ /* search the table to see if the key exists */ for( ktp = keytab ; ktp->k_code != 0 ; ktp++) { if( ktp->k_code == key) { /* save the pointer and scan to the end of the table */ - key_tab *sav_ktp = ktp ; + kbind_p sav_ktp = ktp ; while( (++ktp)->k_code != 0) ; ktp -= 1 ; /* backup to the last legit entry */ @@ -392,7 +392,7 @@ boolean delkeybinding( unsigned key) { #define BINARY 1 -const name_bind *fncmatch( char *name) { +nbind_p fncmatch( char *name) { #ifdef BINARY int found = lastnmidx ; int low = 0 ; @@ -411,7 +411,7 @@ const name_bind *fncmatch( char *name) { return &names[ found] ; #else - const name_bind *nbp ; + nbind_p nbp ; for( nbp = names ; nbp->n_func != NULL ; nbp++) if( !strcmp( name, bind_name( nbp))) break ; diff --git a/names.h b/names.h index 11ed227..64bf210 100644 --- a/names.h +++ b/names.h @@ -7,36 +7,39 @@ /* Bindable uEMACS function pointer type and definition template */ -#define BINDABLE( fname) int fname( int f, int n) +#define BINDABLE( fname) int fname( int f, int n) typedef BINDABLE( (*fnp_t)) ; /* Structure for the name binding table. */ typedef struct { - const char *n_name ; /* name starting with one tag character */ - fnp_t n_func ; /* function the name is bound to */ - unsigned n_keycode ; /* default key assignment, 0 when none */ + const char *n_name ; /* name starting with one tag character */ + fnp_t n_func ; /* function the name is bound to */ + unsigned n_keycode ; /* default key assignment, 0 when none */ } name_bind ; +typedef const name_bind *nbind_p ; + #define bind_name( p) (&( p)->n_name[ 1]) #define bind_tag( p) ( p)->n_name[ 0] + /* Structure for the key bindings table. */ typedef struct { - unsigned k_code ; /* Key code */ - const name_bind *k_nbp ; /* entry in name to function map table */ -} key_tab ; + unsigned k_code ; /* Key code */ + nbind_p k_nbp ; /* entry in name to function map table */ +} *kbind_p ; -extern const name_bind names[] ; /* name to function mapping table */ -extern key_tab *keytab ; /* key bind to functions table */ +extern const name_bind names[] ; /* name to function mapping table */ +extern kbind_p keytab ; /* key bind to functions table */ boolean init_bindings( void) ; -key_tab *setkeybinding( unsigned key, const name_bind *nbp) ; +kbind_p setkeybinding( unsigned key, nbind_p nbp) ; boolean delkeybinding( unsigned key) ; -const name_bind *fncmatch( char *name) ; /* look up by name */ +nbind_p fncmatch( char *name) ; /* look up by name */ /* bindable functions mapped to prefix keys and hooks */ BINDABLE( nullproc) ; From c4fab606d1b88605f1e479ec6564c6aac7e3a208 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Fri, 30 Jul 2021 16:24:52 +0800 Subject: [PATCH 14/37] Revise keycode encoding. Gather APROP and CFENCE conditional code. --- bind.c | 49 +++++------------- bind.h | 20 +++----- defines.h | 9 +--- estruct.h | 1 - execute.c | 96 ++++++++++++++++++++++++++++++++-- execute.h | 21 ++++++-- input.c | 38 +++++++------- names.c | 150 +++++++++++++++++++++++++++++------------------------- names.h | 10 ++++ random.c | 90 -------------------------------- random.h | 1 - 11 files changed, 242 insertions(+), 243 deletions(-) diff --git a/bind.c b/bind.c index 1821861..7a479b4 100644 --- a/bind.c +++ b/bind.c @@ -29,10 +29,7 @@ #include "window.h" -#if APROP static int buildlist( char *mstring) ; -#endif - static void cmdstr( int c, char *seq) ; static unsigned int getckey( int mflag) ; static unsigned int stock( char *keyname) ; @@ -208,7 +205,6 @@ BINDABLE( unbindkey) { return TRUE ; } -#if APROP /* * does source include sub? * @@ -235,23 +231,20 @@ static boolean strinc( const char *source, const char *sub) { return FALSE ; } -#endif /* describe bindings * bring up a fake buffer and list the key bindings * into it with view mode */ -int desbind( int f, int n) { -#if APROP +BINDABLE( desbind) { return buildlist( "") ; } /* Apropos (List functions that match a substring) */ -int apro( int f, int n) { +BINDABLE( apro) { char *mstring ; /* string to match cmd names to */ - int status ; /* status return */ - status = newmlarg( &mstring, "apropos: ", 0) ; + int status = newmlarg( &mstring, "apropos: ", 0) ; if( status == TRUE) { status = buildlist( mstring) ; free( mstring) ; @@ -267,7 +260,6 @@ int apro( int f, int n) { * char *mstring; match string if a partial list, "" matches all */ static int buildlist( char *mstring) { -#endif struct window *wp; /* scanning pointer to windows */ kbind_p ktp; /* pointer into the command table */ nbind_p nptr;/* pointer into the name binding table */ @@ -275,7 +267,8 @@ static int buildlist( char *mstring) { char outseq[80]; /* output buffer for keystroke sequence */ /* split the current window to make room for the binding list */ - if (splitwind(FALSE, 1) == FALSE) + if( wheadp->w_wndp == NULL /* One window */ + && splitwind( FALSE, 1) == FALSE) /* Split it */ return FALSE; /* and get a buffer for it */ @@ -288,7 +281,7 @@ static int buildlist( char *mstring) { /* let us know this is in progress */ mlwrite("(Building binding list)"); - /* disconect the current buffer */ + /* disconnect the current buffer */ if (--curbp->b_nwnd == 0) { /* Last use. */ curbp->b_dotp = curwp->w_dotp; curbp->b_doto = curwp->w_doto; @@ -313,12 +306,11 @@ static int buildlist( char *mstring) { for( nptr = names ; nptr->n_func != NULL ; nptr++) { int cpos ; /* current position to use in outseq */ -#if APROP /* if we are executing an apropos command..... */ - /* and current string doesn't include the search string */ + /* and current string doesn't include the search string */ if( *mstring && strinc( bind_name( nptr), mstring) == FALSE) continue ; -#endif + /* add in the command name */ mystrscpy( outseq, bind_name( nptr), sizeof outseq) ; cpos = strlen(outseq); @@ -424,7 +416,7 @@ static void cmdstr( int c, char *seq) { /* apply ^X sequence if needed */ if (c & CTLX) { - if( ctlxc & CONTROL) + if( ctlxc & CTRL) *ptr++ = '^' ; *ptr++ = ctlxc & 0x1FFFFF ; @@ -437,7 +429,7 @@ static void cmdstr( int c, char *seq) { } /* apply control sequence if needed */ - if (c & CONTROL) { + if (c & CTRL) { *ptr++ = '^'; } @@ -448,24 +440,9 @@ static void cmdstr( int c, char *seq) { *ptr = 0; /* terminate the string */ } -/* - * This function looks a key binding up in the binding table - * - * int c; key to find what is bound to it - */ -kbind_p getkeybind( unsigned c) { - kbind_p ktp ; - - for( ktp = keytab ; ktp->k_code != 0 ; ktp++) - if (ktp->k_code == c) - break ; - - return ktp ; -} - static const char *getfname( unsigned keycode, char *failmsg) { /* takes a key code and gets the name of the function bound to it */ - kbind_p kbp = getkeybind( keycode) ; + kbind_p kbp = getkeybinding( keycode) ; if( kbp->k_code == 0) return failmsg ; @@ -506,11 +483,11 @@ static unsigned int stock( char *keyname) { /* a control char? */ if (*keyname == '^' && *(keyname + 1) != 0) { - c |= CONTROL; + c |= CTRL; ++keyname; } if (*keyname < 32) { - c |= CONTROL; + c |= CTRL; *keyname += 'A'; } diff --git a/bind.h b/bind.h index bacba1e..dcb1ad6 100644 --- a/bind.h +++ b/bind.h @@ -3,23 +3,17 @@ #include "names.h" -#define APROP 1 /* Add code for Apropos command */ - -/* uEMACS functions */ -#if APROP -int apro( int f, int n) ; -#endif - -int help( int f, int n) ; -int deskey( int f, int n) ; -int bindtokey( int f, int n) ; -int unbindkey( int f, int n) ; -int desbind( int f, int n) ; +/* Bindable uEMACS functions */ +BINDABLE( apro) ; +BINDABLE( bindtokey) ; +BINDABLE( desbind) ; +BINDABLE( deskey) ; +BINDABLE( help) ; +BINDABLE( unbindkey) ; int startup( const char *fname) ; /* find a key to function association in the key to function mapping table */ -kbind_p getkeybind( unsigned keycode) ; /* by key code */ const char *transbind( char *skey) ; /* by string representation of key */ #endif diff --git a/defines.h b/defines.h index b6074ed..d62ac3a 100644 --- a/defines.h +++ b/defines.h @@ -5,7 +5,7 @@ /* Must define one of - USG | BSD + USG | BSD */ #define USG 1 @@ -13,12 +13,7 @@ #define SCROLLCODE 1 /* scrolling code P.K. */ #define ENVFUNC 1 -#define NSTRING 128 /* # of bytes, string buffers */ - -#define CONTROL 0x01000000 /* Control flag, or'ed in */ -#define META 0x02000000 /* Meta flag, or'ed in */ -#define CTLX 0x04000000 /* ^X flag, or'ed in */ -#define SPEC 0x08000000 /* special key (function keys) */ +#define NSTRING 128 /* # of bytes, string buffers */ #endif diff --git a/estruct.h b/estruct.h index 26abded..22d1e7b 100644 --- a/estruct.h +++ b/estruct.h @@ -93,7 +93,6 @@ /* Configuration options */ -#define CFENCE 1 /* fench matching in CMODE */ #define VISMAC 0 /* update display during keyboard macros */ #ifndef AUTOCONF diff --git a/execute.c b/execute.c index f0738a6..d96bcf3 100644 --- a/execute.c +++ b/execute.c @@ -8,7 +8,6 @@ #include #include "estruct.h" -#include "bind.h" #include "random.h" #include "display.h" #include "file.h" @@ -203,17 +202,106 @@ static void fmatch( int ch) { #endif +#if CFENCE +/* + * the cursor is moved to a matching fence + * + * int f, n; not used + */ +BINDABLE( getfence) { + struct line *oldlp; /* original line pointer */ + int oldoff; /* and offset */ + int sdir; /* direction of search (1/-1) */ + int count; /* current fence level count */ + char ch; /* fence type to match against */ + char ofence; /* open fence */ + char c; /* current character in scan */ + + /* save the original cursor position */ + oldlp = curwp->w_dotp; + oldoff = curwp->w_doto; + + /* get the current character */ + if (oldoff == llength(oldlp)) + ch = '\n'; + else + ch = lgetc(oldlp, oldoff); + + /* setup proper matching fence */ + switch (ch) { + case '(': + ofence = ')'; + sdir = FORWARD; + break; + case '{': + ofence = '}'; + sdir = FORWARD; + break; + case '[': + ofence = ']'; + sdir = FORWARD; + break; + case ')': + ofence = '('; + sdir = REVERSE; + break; + case '}': + ofence = '{'; + sdir = REVERSE; + break; + case ']': + ofence = '['; + sdir = REVERSE; + break; + default: + TTbeep(); + return FALSE; + } + + /* scan until we find a match, or reach the end of file */ + count = 1 ; + do { + if( boundry( curwp->w_dotp, curwp->w_doto, sdir)) { + /* at buffer limit, no match to be found */ + /* restore the current position */ + curwp->w_dotp = oldlp ; + curwp->w_doto = oldoff ; + TTbeep() ; + return FALSE ; + } + + if( sdir == FORWARD) + forwchar( FALSE, 1) ; + else + backchar( FALSE, 1) ; + + /* if no eol */ + if( curwp->w_doto != llength(curwp->w_dotp)) { + c = curwbyte() ; + if( c == ch) + ++count ; + else if( c == ofence) + --count ; + } + } while( count > 0) ; + + /* we have a match, move the sucker */ + curwp->w_flag |= WFMOVE ; + return TRUE ; +} +#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, * 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 execute( unsigned c, int f, int n) { int status ; /* if the keystroke is a bound function...do it */ - kbind_p ktp = getkeybind( c) ; + kbind_p ktp = getkeybinding( c) ; if( ktp->k_code != 0) { thisflag = 0 ; assert( ktp->k_nbp != NULL) ; @@ -342,7 +430,7 @@ void kbd_loop( void) { fnp_t execfunc ; if( c == newc - && (ktp = getkeybind( c))->k_code == c + && (ktp = getkeybinding( c))->k_code == c && (execfunc = ktp->k_nbp->k_func) != insert_newline && execfunc != insert_tab) newc = getcmd() ; diff --git a/execute.h b/execute.h index 35fa658..9949149 100644 --- a/execute.h +++ b/execute.h @@ -1,5 +1,20 @@ -extern int gasave ; /* global ASAVE size */ -extern int gacount ; /* count until next ASAVE */ +/* execute.h -- */ -int execute( int c, int f, int n) ; +#define CFENCE 1 /* fence matching in CMODE */ + + +#include "names.h" /* key code */ + + +extern int gasave ; /* global ASAVE size */ +extern int gacount ; /* count until next ASAVE */ + + +int execute( unsigned keycode, int f, int n) ; void kbd_loop( void) ; + +#if CFENCE +BINDABLE( getfence) ; +#endif + +/* end of execute.h */ diff --git a/input.c b/input.c index a095cc2..e67c197 100644 --- a/input.c +++ b/input.c @@ -41,12 +41,12 @@ kbdstate kbdmode = STOP ; /* current keyboard macro mode */ int lastkey = 0 ; /* last keystoke */ int kbdrep = 0 ; /* number of repetitions */ -int metac = CONTROL | '[' ; /* current meta character */ -int ctlxc = CONTROL | 'X' ; /* current control X prefix char */ -int reptc = CONTROL | 'U' ; /* current universal repeat char */ -int abortc = CONTROL | 'G' ; /* current abort command char */ +int metac = CTRL | '[' ; /* current meta character */ +int ctlxc = CTRL | 'X' ; /* current control X prefix char */ +int reptc = CTRL | 'U' ; /* current universal repeat char */ +int abortc = CTRL | 'G' ; /* current abort command char */ -const int nlc = CONTROL | 'J' ; /* end of input char */ +const int nlc = CTRL | 'J' ; /* end of input char */ void ue_system( const char *cmd) { @@ -145,11 +145,11 @@ int newmlargt( char **outbufref, const char *prompt, int size) { /* * ectoc: * expanded character to character - * collapse the CONTROL and SPEC flags back into an ascii code + * collapse the CTRL and SPEC flags back into an ascii code */ int ectoc( int c) { - if( c & CONTROL) - c ^= CONTROL | 0x40 ; + if( c & CTRL) + c ^= CTRL | 0x40 ; if( c & SPEC) c &= 255 ; @@ -325,7 +325,7 @@ int tgetc(void) } /* GET1KEY: Get one keystroke. The only prefixs legal here are the SPEC - and CONTROL prefixes. */ + and CTRL prefixes. */ int get1key( void) { int c ; @@ -333,7 +333,7 @@ int get1key( void) { c = tgetc(); if( (c >= 0x00 && c <= 0x1F) || c == 0x7F) /* C0 control -> C- */ - c ^= CONTROL | 0x40 ; + c ^= CTRL | 0x40 ; return c ; } @@ -377,7 +377,7 @@ int getcmd(void) if (c == 128+27) /* CSI */ goto handle_CSI; /* process META prefix */ - if (c == (CONTROL | '[')) { + if (c == (CTRL | '[')) { c = get1key(); #if VT220 if (c == '[' || c == 'O') { /* CSI P.K. */ @@ -416,7 +416,7 @@ handle_CSI: } #endif #if VT220 - if (c == (CONTROL | '[')) { + if (c == (CTRL | '[')) { cmask = META; goto proc_metac; } @@ -424,14 +424,14 @@ handle_CSI: if( islower( c)) /* Force to upper */ c = flipcase( c) ; else if( c >= 0x00 && c <= 0x1F) /* control key */ - c = CONTROL | (c + '@'); + c = CTRL | (c + '@'); return META | c; } #if PKCODE else if (c == metac) { c = get1key(); #if VT220 - if (c == (CONTROL | '[')) { + if (c == (CTRL | '[')) { cmask = META; goto proc_metac; } @@ -439,7 +439,7 @@ handle_CSI: if( islower( c)) /* Force to upper */ c = flipcase( c) ; else if( c >= 0x00 && c <= 0x1F) /* control key */ - c = CONTROL | (c + '@'); + c = CTRL | (c + '@'); return META | c; } #endif @@ -452,7 +452,7 @@ handle_CSI: if (c == ctlxc) { c = get1key(); #if VT220 - if (c == (CONTROL | '[')) { + if (c == (CTRL | '[')) { cmask = CTLX; goto proc_metac; } @@ -460,7 +460,7 @@ handle_CSI: if (c >= 'a' && c <= 'z') /* Force to upper */ c -= 0x20; if (c >= 0x00 && c <= 0x1F) /* control key */ - c = CONTROL | (c + '@'); + c = CTRL | (c + '@'); return CTLX | c; } @@ -554,8 +554,8 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) } /* If it is a , change it to a */ - if( c == (CONTROL | 'M')) - c = CONTROL | 0x40 | '\n' ; + if( c == (CTRL | 'M')) + c = CTRL | 0x40 | '\n' ; if( c == eolchar) { /* if they hit the line terminator, wrap it up */ diff --git a/names.c b/names.c index c6b93e2..13044eb 100644 --- a/names.c +++ b/names.c @@ -19,6 +19,7 @@ #include "display.h" #include "eval.h" #include "exec.h" +#include "execute.h" #include "file.h" #include "isearch.h" #include "line.h" @@ -31,58 +32,54 @@ #include "word.h" -#define CTL_ CONTROL - const name_bind names[] = { - {" abort-command", ctrlg, CTL_ | 'G'} , + {" abort-command", ctrlg, CTRL | 'G'} , {" add-global-mode", setgmode, META | 'M'} , {" add-mode", setemode, CTLX | 'M'} , -#if APROP {" apropos", apro, META | 'A'} , -#endif - {" backward-character", (fnp_t) backchar, CTL_ | 'B'} , + {" backward-character", (fnp_t) backchar, CTRL | 'B'} , {" begin-macro", ctlxlp, CTLX | '('} , {" beginning-of-file", (fnp_t) gotobob, META | '<'} , - {" beginning-of-line", (fnp_t) gotobol, CTL_ | 'A'} , + {" beginning-of-line", (fnp_t) gotobol, CTRL | 'A'} , {" bind-to-key", bindtokey, META | 'K'} , {" buffer-position", showcpos, CTLX | '='} , - {"!case-region-lower", lowerregion, CTLX | CTL_ | 'L'} , - {"!case-region-upper", upperregion, CTLX | CTL_ | 'U'} , + {"!case-region-lower", lowerregion, CTLX | CTRL | 'L'} , + {"!case-region-upper", upperregion, CTLX | CTRL | 'U'} , {"!case-word-capitalize", capword, META | 'C'} , {"!case-word-lower", lowerword, META | 'L'} , {"!case-word-upper", upperword, META | 'U'} , {" change-file-name", filename, CTLX | 'N'} , - {" change-screen-size", newsize, META | CTL_ | 'D'} , /* M^S */ - {" change-screen-width", newwidth, META | CTL_ | 'T'} , - {" clear-and-redraw", redraw, CTL_ | 'L'} , + {" change-screen-size", newsize, META | CTRL | 'D'} , /* M^S */ + {" change-screen-width", newwidth, META | CTRL | 'T'} , + {" clear-and-redraw", redraw, CTRL | 'L'} , {" clear-message-line", clrmes, 0} , {" copy-region", copyregion, META | 'W'} , #if WORDPRO - {" count-words", wordcount, META | CTL_ | 'C'} , + {" count-words", wordcount, META | CTRL | 'C'} , #endif - {" ctlx-prefix", cex, CTL_ | 'X'} , - {"!delete-blank-lines", deblank, CTLX | CTL_ | 'O'} , + {" ctlx-prefix", cex, CTRL | 'X'} , + {"!delete-blank-lines", deblank, CTLX | CTRL | 'O'} , {" delete-buffer", killbuffer, CTLX | 'K'} , - {" delete-global-mode", delgmode, META | CTL_ | 'M'} , - {" delete-mode", delmode, CTLX | CTL_ | 'M'} , - {"!delete-next-character", forwdel, CTL_ | 'D'} , + {" delete-global-mode", delgmode, META | CTRL | 'M'} , + {" delete-mode", delmode, CTLX | CTRL | 'M'} , + {"!delete-next-character", forwdel, CTRL | 'D'} , {"!delete-next-word", delfword, META | 'D'} , {" delete-other-windows", onlywind, CTLX | '1'} , - {"!delete-previous-character", backdel, CTL_ | 'H'} , /* ^? */ - {"!delete-previous-word", delbword, META | CTL_ | 'H'} , /* M^? */ + {"!delete-previous-character", backdel, CTRL | 'H'} , /* ^? */ + {"!delete-previous-word", delbword, META | CTRL | 'H'} , /* M^? */ {" delete-window", delwind, CTLX | '0'} , {" describe-bindings", desbind, 0} , {" describe-key", deskey, CTLX | '?'} , #if AEDIT - {"!detab-line", detab, CTLX | CTL_ | 'D'} , /* X^A */ + {"!detab-line", detab, CTLX | CTRL | 'D'} , /* X^A */ #endif {" end-macro", ctlxrp, CTLX | ')'} , {" end-of-file", (fnp_t) gotoeob, META | '>'} , - {" end-of-line", (fnp_t) gotoeol, CTL_ | 'E'} , + {" end-of-line", (fnp_t) gotoeol, CTRL | 'E'} , #if AEDIT - {"!entab-line", entab, CTLX | CTL_ | 'E'} , + {"!entab-line", entab, CTLX | CTRL | 'E'} , #endif - {" exchange-point-and-mark", (fnp_t) swapmark, CTLX | CTL_ | 'X'} , + {" exchange-point-and-mark", (fnp_t) swapmark, CTLX | CTRL | 'X'} , {" execute-buffer", execbuf, 0} , {" execute-command-line", execcmd, 0} , {" execute-file", execfile, 0} , @@ -129,22 +126,22 @@ const name_bind names[] = { {" execute-macro-9", cbuf9, 0} , {" execute-named-command", namedcmd, META | 'X'} , #if PROC - {" execute-procedure", execproc, META | CTL_ | 'E'} , + {" execute-procedure", execproc, META | CTRL | 'E'} , #endif {" execute-program", execprg, CTLX | '$'} , - {" exit-emacs", quit, CTLX | CTL_ | 'C'} , + {" exit-emacs", quit, CTLX | CTRL | 'C'} , #if WORDPRO {"!fill-paragraph", fillpara, META | 'Q'} , #endif {"!filter-buffer", filter_buffer, CTLX | '#'} , - {" find-file", filefind, CTLX | CTL_ | 'F'} , - {" forward-character", (fnp_t) forwchar, CTL_ | 'F'} , + {" find-file", filefind, CTLX | CTRL | 'F'} , + {" forward-character", (fnp_t) forwchar, CTRL | 'F'} , {" goto-line", gotoline, META | 'G'} , #if CFENCE - {" goto-matching-fence", getfence, META | CTL_ | 'F'} , + {" goto-matching-fence", getfence, META | CTRL | 'F'} , #endif {" grow-window", enlargewind, CTLX | 'Z'} , /* X^ */ - {"!handle-tab", insert_tab, CTL_ | 'I'} , + {"!handle-tab", insert_tab, CTRL | 'I'} , {" help", help, META | '?'} , {" hunt-backward", backhunt, 0} , {" hunt-forward", forwhunt, META | 'S'} , @@ -152,48 +149,48 @@ const name_bind names[] = { #if ISRCH {" incremental-search", fisearch, CTLX | 'S'} , #endif - {"!insert-file", insfile, CTLX | CTL_ | 'I'} , - {"!insert-space", insspace, CTL_ | 'C'} , + {"!insert-file", insfile, CTLX | CTRL | 'I'} , + {"!insert-space", insspace, CTRL | 'C'} , {"!insert-string", istring, 0} , #if WORDPRO #if PKCODE {"!justify-paragraph", justpara, META | 'J'} , #endif - {"!kill-paragraph", killpara, META | CTL_ | 'W'} , + {"!kill-paragraph", killpara, META | CTRL | 'W'} , #endif - {"!kill-region", killregion, CTL_ | 'W'} , - {"!kill-to-end-of-line", killtext, CTL_ | 'K'} , - {" list-buffers", listbuffers, CTLX | CTL_ | 'B'} , - {" meta-prefix", metafn, CTL_ | '['} , - {" move-window-down", mvdnwind, CTLX | CTL_ | 'N'} , - {" move-window-up", mvupwind, CTLX | CTL_ | 'P'} , - {" name-buffer", namebuffer, META | CTL_ | 'N'} , - {"!newline", insert_newline, CTL_ | 'M'} , - {"!newline-and-indent", indent, CTL_ | 'J'} , + {"!kill-region", killregion, CTRL | 'W'} , + {"!kill-to-end-of-line", killtext, CTRL | 'K'} , + {" list-buffers", listbuffers, CTLX | CTRL | 'B'} , + {" meta-prefix", metafn, CTRL | '['} , + {" move-window-down", mvdnwind, CTLX | CTRL | 'N'} , + {" move-window-up", mvupwind, CTLX | CTRL | 'P'} , + {" name-buffer", namebuffer, META | CTRL | 'N'} , + {"!newline", insert_newline, CTRL | 'M'} , + {"!newline-and-indent", indent, CTRL | 'J'} , {" next-buffer", nextbuffer, CTLX | 'X'} , - {" next-line", (fnp_t) forwline, CTL_ | 'N'} , - {" next-page", (fnp_t) forwpage, CTL_ | 'V'} , + {" next-line", (fnp_t) forwline, CTRL | 'N'} , + {" next-page", (fnp_t) forwpage, CTRL | 'V'} , #if WORDPRO {" next-paragraph", gotoeop, META | 'N'} , #endif {" next-window", nextwind, CTLX | 'O'} , {" next-word", forwword, META | 'F'} , {" nop", nullproc, SPEC | META | 'C'}, /* hook */ - {"!open-line", openline, CTL_ | 'O'} , + {"!open-line", openline, CTRL | 'O'} , {" overwrite-string", ovstring, 0} , {" pipe-command", pipecmd, CTLX | '@'} , - {" previous-line", (fnp_t) backline, CTL_ | 'P'} , - {" previous-page", (fnp_t) backpage, CTL_ | 'Z'} , /* MV */ + {" previous-line", (fnp_t) backline, CTRL | 'P'} , + {" previous-page", (fnp_t) backpage, CTRL | 'Z'} , /* MV */ #if WORDPRO {" previous-paragraph", gotobop, META | 'P'} , #endif {" previous-window", prevwind, CTLX | 'P'} , {" previous-word", backword, META | 'B'} , - {"!query-replace-string", qreplace, META | CTL_ | 'R'} , + {"!query-replace-string", qreplace, META | CTRL | 'R'} , {" quick-exit", quickexit, META | 'Z'} , - {"!quote-character", quote, CTL_ | 'Q'} , /* also XQ */ - {"!read-file", fileread, CTLX | CTL_ | 'R'} , - {" redraw-display", reposition, META | CTL_ | 'L'} , /* M! */ + {"!quote-character", quote, CTRL | 'Q'} , /* also XQ */ + {"!read-file", fileread, CTLX | CTRL | 'R'} , + {" redraw-display", reposition, META | CTRL | 'L'} , /* M! */ {"!replace-string", sreplace, META | 'R'} , {" resize-window", resize, CTLX | 'W'} , {" restore-window", restwnd, 0} , @@ -203,18 +200,18 @@ const name_bind names[] = { #if PROC {" run", execproc, 0} , // alias of execute-procedure #endif - {"!save-file", filesave, CTLX | CTL_ | 'S'} , /* also X^D */ + {"!save-file", filesave, CTLX | CTRL | 'S'} , /* also X^D */ {" save-window", savewnd, 0} , - {" scroll-next-down", scrnextdw, META | CTL_ | 'V'} , - {" scroll-next-up", scrnextup, META | CTL_ | 'Z'} , - {" search-forward", forwsearch, CTL_ | 'S'} , - {" search-reverse", backsearch, CTL_ | 'R'} , + {" scroll-next-down", scrnextdw, META | CTRL | 'V'} , + {" scroll-next-up", scrnextup, META | CTRL | 'Z'} , + {" search-forward", forwsearch, CTRL | 'S'} , + {" search-reverse", backsearch, CTRL | 'R'} , {" select-buffer", usebuffer, CTLX | 'B'} , {" set", setvar, CTLX | 'A'} , {" set-fill-column", setfillcol, CTLX | 'F'} , {" set-mark", (fnp_t) setmark, META | ' '} , /* M. */ {" shell-command", spawn, CTLX | '!'} , - {" shrink-window", shrinkwind, CTLX | CTL_ | 'Z'} , + {" shrink-window", shrinkwind, CTLX | CTRL | 'Z'} , {" split-current-window", splitwind, CTLX | '2'} , {" store-macro", storemac, 0} , #if PROC @@ -223,31 +220,31 @@ const name_bind names[] = { #if BSD | SVR4 {" suspend-emacs", bktoshell, CTLX | 'D'} , /* BSD MS */ #endif - {"!transpose-characters", (fnp_t) twiddle, CTL_ | 'T'} , + {"!transpose-characters", (fnp_t) twiddle, CTRL | 'T'} , #if AEDIT - {"!trim-line", trim, CTLX | CTL_ | 'T'} , + {"!trim-line", trim, CTLX | CTRL | 'T'} , #endif - {" unbind-key", unbindkey, META | CTL_ | 'K'} , - {" universal-argument", unarg, CTL_ | 'U'} , + {" unbind-key", unbindkey, META | CTRL | 'K'} , + {" universal-argument", unarg, CTRL | 'U'} , {" unmark-buffer", unmark, META | '~'} , {" update-screen", upscreen, 0} , - {" view-file", viewfile, CTLX | CTL_ | 'V'} , + {" view-file", viewfile, CTLX | CTRL | 'V'} , {"!wrap-word", wrapword, SPEC | META | 'W'} , /* hook */ - {" write-file", filewrite, CTLX | CTL_ | 'W'} , + {" write-file", filewrite, CTLX | CTRL | 'W'} , {" write-message", writemsg, 0} , - {"!yank", yank, CTL_ | 'Y'} , + {"!yank", yank, CTRL | 'Y'} , {" ", NULL, 0}, /* extra key mapping */ -// { NULL, newsize, META | CTL_ | 'S'}, - { NULL, backdel, CTL_ | '?'}, - { NULL, delbword, META | CTL_ | '?'}, - { NULL, detab, CTLX | CTL_ | 'A'}, +// { NULL, newsize, META | CTRL | 'S'}, + { NULL, backdel, CTRL | '?'}, + { NULL, delbword, META | CTRL | '?'}, + { NULL, detab, CTLX | CTRL | 'A'}, { NULL, enlargewind, CTLX | '^'}, { NULL, (fnp_t) backpage, META | 'V'}, { NULL, quote, CTLX | 'Q'}, { NULL, reposition, META | '!'}, -//detab { NULL, filesave, CTLX | CTL_ | 'D'}, +//detab { NULL, filesave, CTLX | CTRL | 'D'}, { NULL, (fnp_t) setmark, META | '.'}, // { NULL, bktoshell, META | 'S'}, @@ -390,6 +387,21 @@ boolean delkeybinding( unsigned key) { return FALSE ; } +/* + * This function looks a key binding up in the binding table + * + * int c; key to find what is bound to it + */ +kbind_p getkeybinding( unsigned c) { + kbind_p ktp ; + + for( ktp = keytab ; ktp->k_code != 0 ; ktp++) + if (ktp->k_code == c) + break ; + + return ktp ; +} + #define BINARY 1 nbind_p fncmatch( char *name) { diff --git a/names.h b/names.h index 64bf210..0d045f9 100644 --- a/names.h +++ b/names.h @@ -6,6 +6,12 @@ #include "retcode.h" +#define CTRL 0x01000000 /* Control flag, or'ed in */ +#define META 0x02000000 /* Meta flag, or'ed in */ +#define CTLX 0x04000000 /* ^X flag, or'ed in */ +#define SPEC 0x08000000 /* special key (function keys) */ + + /* Bindable uEMACS function pointer type and definition template */ #define BINDABLE( fname) int fname( int f, int n) @@ -39,8 +45,12 @@ extern kbind_p keytab ; /* key bind to functions table */ boolean init_bindings( void) ; kbind_p setkeybinding( unsigned key, nbind_p nbp) ; boolean delkeybinding( unsigned key) ; +kbind_p getkeybinding( unsigned key) ; /* look up by key code */ + +/* find a name to function association in the name to function mapping table */ nbind_p fncmatch( char *name) ; /* look up by name */ + /* bindable functions mapped to prefix keys and hooks */ BINDABLE( nullproc) ; BINDABLE( metafn) ; diff --git a/random.c b/random.c index 957a785..785c353 100644 --- a/random.c +++ b/random.c @@ -924,96 +924,6 @@ static int adjustmode( int kind, int global) { return FALSE; } -#if CFENCE -/* - * the cursor is moved to a matching fence - * - * int f, n; not used - */ -int getfence(int f, int n) -{ - struct line *oldlp; /* original line pointer */ - int oldoff; /* and offset */ - int sdir; /* direction of search (1/-1) */ - int count; /* current fence level count */ - char ch; /* fence type to match against */ - char ofence; /* open fence */ - char c; /* current character in scan */ - - /* save the original cursor position */ - oldlp = curwp->w_dotp; - oldoff = curwp->w_doto; - - /* get the current character */ - if (oldoff == llength(oldlp)) - ch = '\n'; - else - ch = lgetc(oldlp, oldoff); - - /* setup proper matching fence */ - switch (ch) { - case '(': - ofence = ')'; - sdir = FORWARD; - break; - case '{': - ofence = '}'; - sdir = FORWARD; - break; - case '[': - ofence = ']'; - sdir = FORWARD; - break; - case ')': - ofence = '('; - sdir = REVERSE; - break; - case '}': - ofence = '{'; - sdir = REVERSE; - break; - case ']': - ofence = '['; - sdir = REVERSE; - break; - default: - TTbeep(); - return FALSE; - } - - /* scan until we find a match, or reach the end of file */ - count = 1 ; - do { - if( boundry( curwp->w_dotp, curwp->w_doto, sdir)) { - /* at buffer limit, no match to be found */ - /* restore the current position */ - curwp->w_dotp = oldlp ; - curwp->w_doto = oldoff ; - TTbeep() ; - return FALSE ; - } - - if( sdir == FORWARD) - forwchar( FALSE, 1) ; - else - backchar( FALSE, 1) ; - - /* if no eol */ - if( curwp->w_doto != llength(curwp->w_dotp)) { - c = curwbyte() ; - if( c == ch) - ++count ; - else if( c == ofence) - --count ; - } - } while( count > 0) ; - - /* we have a match, move the sucker */ - curwp->w_flag |= WFMOVE ; - return TRUE ; -} -#endif - 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 f666c4c..eb772d7 100644 --- a/random.h +++ b/random.h @@ -43,7 +43,6 @@ int setemode( int f, int n) ; 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 istring( int f, int n) ; int ovstring( int f, int n) ; From d48120a55727f3eee61aa989f5b1df386c322c7a Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 31 Jul 2021 12:35:39 +0800 Subject: [PATCH 15/37] Improve support for Unicode in describe-key. --- bind.c | 60 ++++++++++++++++++++++++------------------------------- display.c | 56 +++++++++++++++++++-------------------------------- names.h | 9 +++++---- 3 files changed, 52 insertions(+), 73 deletions(-) diff --git a/bind.c b/bind.c index 7a479b4..ff7e255 100644 --- a/bind.c +++ b/bind.c @@ -30,7 +30,7 @@ static int buildlist( char *mstring) ; -static void cmdstr( int c, char *seq) ; +static char *cmdstr( unsigned c, char *seq) ; static unsigned int getckey( int mflag) ; static unsigned int stock( char *keyname) ; static const char *getfname( unsigned keycode, char *failmsg) ; @@ -75,19 +75,19 @@ BINDABLE( help) { /* describe the command for a certain key */ BINDABLE( deskey) { + const char cmdname[] = "describe-key" ; char outseq[ NSTRING] ; /* output buffer for command sequence */ /* prompt the user to type a key to describe */ - mlwrite( "describe-key: "); + mlwrite( "%s: ", cmdname) ; /* get the command sequence to describe * change it to something we can print as well */ - int c = getckey( FALSE) ; - cmdstr( c, outseq) ; + unsigned keycode = getckey( FALSE) ; /* output the command sequence */ - mlwrite( "describe-key %s: 0x%x, %s", - outseq, c, getfname( c, "Not Bound")) ; + mlwrite( "%s %s: 0x%x, %s", cmdname, cmdstr( keycode, outseq), keycode, + getfname( keycode, "Not Bound")) ; return TRUE ; } @@ -123,10 +123,8 @@ BINDABLE( bindtokey) { int c = getckey( prefix_f) ; /* change it to something we can print as well */ - cmdstr( c, outseq) ; - /* and dump it out */ - ostring( outseq) ; + ostring( cmdstr( c, outseq)) ; /* key sequence can't be an active prefix key */ if( c == metac || c == ctlxc || c == reptc || c == abortc) { @@ -185,10 +183,8 @@ BINDABLE( unbindkey) { int c = getckey( FALSE) ; /* get a command sequence */ /* change it to something we can print as well */ - cmdstr( c, outseq) ; - /* and dump it out */ - ostring( outseq) ; + ostring( cmdstr( c, outseq)) ; /* prefix key sequence can't be undound, just redefined */ if( c == reptc || c == abortc) { @@ -403,41 +399,37 @@ int startup( const char *fname) { * int c; sequence to translate * char *seq; destination string for sequence */ -static void cmdstr( int c, char *seq) { - char *ptr; /* pointer into current position in sequence */ +static char *cmdstr( unsigned c, char *seq) { + char *ptr = seq ; /* pointer into current position in sequence */ - ptr = seq; - - /* apply meta sequence if needed */ - if (c & META) { +/* apply meta sequence if needed */ + if( c & META) { *ptr++ = 'M'; *ptr++ = '-'; } - /* apply ^X sequence if needed */ - if (c & CTLX) { +/* apply ^X sequence if needed */ + if( c & CTLX) { if( ctlxc & CTRL) *ptr++ = '^' ; - *ptr++ = ctlxc & 0x1FFFFF ; + *ptr++ = ctlxc & ~PRFXMASK ; } - /* apply SPEC sequence if needed */ - if (c & SPEC) { - *ptr++ = 'F'; - *ptr++ = 'N'; +/* apply SPEC sequence if needed */ + if( c & SPEC) { + *ptr++ = 'F' ; + *ptr++ = 'N' ; } - /* apply control sequence if needed */ - if (c & CTRL) { - *ptr++ = '^'; - } +/* apply control sequence if needed */ + if( c & CTRL) + *ptr++ = '^' ; - /* and output the final sequence */ - - *ptr++ = c & 0x1FFFFF ; /* strip the prefixes */ - - *ptr = 0; /* terminate the string */ +/* and output the final sequence */ + ptr += unicode_to_utf8( c & ~PRFXMASK, ptr) ; + *ptr = 0 ; /* terminate the string */ + return seq ; } static const char *getfname( unsigned keycode, char *failmsg) { diff --git a/display.c b/display.c index 70f4b3c..35a3559 100644 --- a/display.c +++ b/display.c @@ -89,7 +89,7 @@ static void modeline(struct window *wp); static void mlputi(int i, int r); static void mlputli(long l, int r); static void mlputf(int s); -static void mlputs( unsigned char *s) ; +static void mlputs( const char *s) ; #if SIGWINCH static int newscreensize(int h, int w); #endif @@ -1274,7 +1274,7 @@ void mlerase( void) { static void mlputc( unicode_t c) { if( ttcol < term.t_ncol) { TTputc( c) ; - ++ttcol ; + ttcol += utf8_width( c) ; } } @@ -1284,11 +1284,8 @@ static void mlputc( unicode_t c) { * char *s; string to output */ void ostring( const char *s) { - unsigned char c ; - if( discmd) - while( (c = *s++) != 0) - mlputc( c) ; + mlputs( s) ; } @@ -1302,8 +1299,6 @@ void ostring( const char *s) { * char *arg; pointer to first argument to print */ void vmlwrite( const char *fmt, va_list ap) { - int c; /* current char in format string */ - /* if we are not currently echoing on the command line, abort this */ if (discmd == FALSE) { movecursor(term.t_nrow, 0); @@ -1322,13 +1317,17 @@ void vmlwrite( const char *fmt, va_list ap) { movecursor( term.t_nrow, 0) ; mpresf = *fmt ? TRUE : FALSE ; /* flag if line has content or not */ - while( ( c = *fmt++) != 0) + while( *fmt) { + unicode_t c ; + + fmt += utf8_to_unicode( fmt, 0, 4, &c) ; if( c != '%') mlputc( c) ; - else if( ( c = *fmt++) == 0) { + else if( *fmt == 0) { mlputc( '%') ; break ; - } else + } else { + fmt += utf8_to_unicode( fmt, 0, 4, &c) ; switch( c) { case 'd': mlputi(va_arg(ap, int), 10); @@ -1347,7 +1346,7 @@ void vmlwrite( const char *fmt, va_list ap) { break; case 's': - mlputs( (unsigned char *) va_arg( ap, char *)) ; + mlputs( (char *) va_arg( ap, char *)) ; break; case 'f': @@ -1364,6 +1363,8 @@ void vmlwrite( const char *fmt, va_list ap) { case '%': mlputc( c) ; } + } + } /* if we can, erase to the end of screen */ if( eolexist == TRUE && ttcol < term.t_ncol) @@ -1385,31 +1386,16 @@ void mlwrite( const char *fmt, ...) { * the characters in the string all have width "1"; if this is not the case * things will get screwed up a little. */ -static void mlputs( unsigned char *s) { - unicode_t c ; +static void mlputs( const char *s) { + while( *s && (ttcol < term.t_ncol)) { + unicode_t uc ; - while( ((c = *s++) != 0) && (ttcol < term.t_ncol)) { - if( c == '\t') /* Don't render tabulation */ - c = ' ' ; - else if( c > 0xC1 && c <= 0xF4) { /* Accept UTF-8 sequence */ - char utf[ 4] ; - char cc ; - int bytes ; + s += utf8_to_unicode( (char *) s, 0, 4, &uc) ; + if( uc == '\t') /* Don't render tabulation */ + uc = ' ' ; - utf[ 0] = c ; - utf[ 1] = cc = *s ; - if( (c & 0x20) && ((cc & 0xC0) == 0x80)) { /* at least 3 bytes and a valid encoded char */ - utf[ 2] = cc = s[ 1] ; - if( (c & 0x10) && ((cc & 0xC0) == 0x80)) /* at least 4 bytes and a valid encoded char */ - utf[ 3] = s[ 2] ; - } - - bytes = utf8_to_unicode( utf, 0, sizeof utf, (unicode_t *) &c) ; - s += bytes - 1 ; - } - - TTputc( c) ; - ++ttcol ; + TTputc( uc) ; + ttcol += utf8_width( uc) ; } } diff --git a/names.h b/names.h index 0d045f9..47d5277 100644 --- a/names.h +++ b/names.h @@ -6,10 +6,11 @@ #include "retcode.h" -#define CTRL 0x01000000 /* Control flag, or'ed in */ -#define META 0x02000000 /* Meta flag, or'ed in */ -#define CTLX 0x04000000 /* ^X flag, or'ed in */ -#define SPEC 0x08000000 /* special key (function keys) */ +#define CTRL 0x01000000 /* Control flag, or'ed in */ +#define META 0x02000000 /* Meta flag, or'ed in */ +#define CTLX 0x04000000 /* ^X flag, or'ed in */ +#define SPEC 0x08000000 /* special key (function keys) */ +#define PRFXMASK 0x0F000000 /* prefix mask */ /* Bindable uEMACS function pointer type and definition template */ From 486d01297d641545a77c91fbb43c7f2f419bec6d Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 31 Jul 2021 14:45:35 +0800 Subject: [PATCH 16/37] Improve keyboard input of UTF-8. --- input.c | 54 ++++++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/input.c b/input.c index e67c197..936db50 100644 --- a/input.c +++ b/input.c @@ -324,13 +324,32 @@ int tgetc(void) return c; } -/* GET1KEY: Get one keystroke. The only prefixs legal here are the SPEC +static int get1unicode( int *k) { +/* Accept UTF-8 sequence */ + int c = *k ; + if( c > 0xC1 && c <= 0xF4) { + char utf[ 4] ; + char cc ; + + utf[ 0] = c ; + utf[ 1] = cc = tgetc() ; + if( (c & 0x20) && ((cc & 0xC0) == 0x80)) { /* at least 3 bytes and a valid encoded char */ + utf[ 2] = cc = tgetc() ; + if( (c & 0x10) && ((cc & 0xC0) == 0x80)) /* at least 4 bytes and a valid encoded char */ + utf[ 3] = tgetc() ; + } + + return utf8_to_unicode( utf, 0, sizeof utf, (unicode_t *) k) ; + } else + return 1 ; +} + +/* GET1KEY: Get one keystroke. The only prefixes legal here are the SPEC and CTRL prefixes. */ int get1key( void) { - int c ; - /* get a keystroke */ - c = tgetc(); + int c = tgetc() ; + get1unicode( &c) ; if( (c >= 0x00 && c <= 0x1F) || c == 0x7F) /* C0 control -> C- */ c ^= CTRL | 0x40 ; @@ -341,26 +360,6 @@ int get1key( void) { /* GETCMD: Get a command from the keyboard. Process all applicable prefix keys */ -static int get1unicode( int *k) { -/* Accept UTF-8 sequence */ - int c = *k ; - if( c > 0xC1 && c <= 0xF4) { - char utf[ 4] ; - char cc ; - - utf[ 0] = c ; - utf[ 1] = cc = get1key() ; - if( (c & 0x20) && ((cc & 0xC0) == 0x80)) { /* at least 3 bytes and a valid encoded char */ - utf[ 2] = cc = get1key() ; - if( (c & 0x10) && ((cc & 0xC0) == 0x80)) /* at least 4 bytes and a valid encoded char */ - utf[ 3] = get1key() ; - } - - return utf8_to_unicode( utf, 0, sizeof utf, (unicode_t *) k) ; - } else - return 1 ; -} - int getcmd(void) { int c; /* fetched keystroke */ @@ -465,7 +464,7 @@ handle_CSI: } #ifdef CYGWIN - get1unicode( &c) ; +// get1unicode( &c) ; #endif /* otherwise, just return it */ @@ -696,9 +695,8 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) quote_f = TRUE ; else { /* store as it is */ - int n ; - - n = get1unicode( &c) ; /* fetch multiple bytes */ +// n = get1unicode( &c) ; /* fetch multiple bytes */ + int n = 4 ; /* long utf-8 encoding */ if( cpos + n < nbuf) { cpos += unicode_to_utf8( c, &buf[ cpos]) ; echov( c) ; From 27f30e48d20b4a617d6d742902aca734f3af0bd2 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 31 Jul 2021 15:28:32 +0800 Subject: [PATCH 17/37] Name pointer type to struct. --- buffer.h | 24 ++++++++++++------------ line.h | 16 +++++++++------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/buffer.h b/buffer.h index 403b580..f8e93c5 100644 --- a/buffer.h +++ b/buffer.h @@ -17,11 +17,11 @@ typedef char bname_t[ 16] ; /* buffer name type */ * Buffers may be "Inactive" which means the files associated with them * have not been read in yet. These get read in at "use buffer" time. */ -struct buffer { +typedef struct buffer { struct buffer *b_bufp; /* Link to next struct buffer */ - struct line *b_dotp; /* Link to "." struct line structure */ - struct line *b_markp; /* The same as the above two, */ - struct line *b_linep; /* Link to the header struct line */ + line_p b_dotp ; /* Link to "." struct line structure */ + line_p b_markp ; /* The same as the above two, */ + line_p b_linep ; /* Link to the header struct line */ int b_doto; /* Offset of "." in above struct line */ int b_marko; /* but for the "mark" */ int b_mode; /* editor mode of this buffer */ @@ -30,11 +30,11 @@ struct buffer { char b_flag; /* Flags */ fname_t b_fname ; /* File name */ bname_t b_bname ; /* Buffer name */ -}; +} *buffer_p ; -extern struct buffer *curbp ; /* Current buffer */ -extern struct buffer *bheadp ; /* Head of list of buffers */ -extern struct buffer *blistp ; /* Buffer for C-X C-B */ +extern buffer_p curbp ; /* Current buffer */ +extern buffer_p bheadp ; /* Head of list of buffers */ +extern buffer_p blistp ; /* Buffer for C-X C-B */ #define BFINVS 0x01 /* Internal invisable buffer */ #define BFCHG 0x02 /* Changed since last write */ @@ -60,15 +60,15 @@ extern int gmode ; /* global editor mode */ int usebuffer( int f, int n) ; int nextbuffer( int f, int n) ; -int swbuffer( struct buffer *bp) ; +int swbuffer( buffer_p bp) ; int killbuffer( int f, int n) ; -int zotbuf( struct buffer *bp) ; +int zotbuf( buffer_p bp) ; int namebuffer( int f, int n) ; int listbuffers( int f, int n) ; int anycb( void) ; -int bclear( struct buffer *bp) ; +int bclear( buffer_p bp) ; int unmark( int f, int n) ; /* Lookup a buffer by name. */ -struct buffer *bfind( const char *bname, int cflag, int bflag) ; +buffer_p bfind( const char *bname, int cflag, int bflag) ; #endif diff --git a/line.h b/line.h index 1234524..18ed04f 100644 --- a/line.h +++ b/line.h @@ -6,11 +6,12 @@ /* * All text is kept in circularly linked lists of "struct line" structures. - * These begin at the header line (which is the blank line beyond the end of the - * buffer). This line is pointed to by the "struct buffer". Each line contains a - * number of bytes in the line (the "used" size), the size of the text array, - * and the text. The end of line is not stored as a byte; it's implied. Future - * additions will include update hints, and a list of marks into the line. + * These begin at the header line (which is the blank line beyond the end + * of the buffer). This line is pointed to by the "struct buffer". Each + * line contains a number of bytes in the line (the "used" size), the size + * of the text array, and the text. The end of line is not stored as a + * byte; it's implied. Future additions will include update hints, and a + * list of marks into the line. */ typedef struct line { struct line *l_fp ; /* Forward link to the next line */ @@ -30,12 +31,14 @@ extern int tabwidth ; /* Map to $tab, default to 8, can be set to [1, .. */ char *getkill( void) ; +/* Bindable functions */ boolean backchar( int f, int n) ; boolean forwchar( int f, int n) ; +int insspace( int f, int n) ; +int yank( int f, int n) ; void lfree( line_p lp) ; void lchange( int flag) ; -int insspace( int f, int n) ; int linstr( char *instr) ; int linsert( int n, unicode_t c) ; boolean linsert_byte( int n, int c) ; @@ -47,7 +50,6 @@ int lgetchar( unicode_t *) ; char *getctext( void) ; void kdelete( void) ; int kinsert( int c) ; -int yank( int f, int n) ; line_p lalloc( int minsize) ; /* Allocate a line of at least minsize chars. */ boolean rdonly( void) ; /* Read Only error message */ From 7730a4e730f714f36e618a2eaa288457422c374b Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sun, 1 Aug 2021 15:07:06 +0800 Subject: [PATCH 18/37] Improve handling of UTF-8 interactive input of strings. --- input.c | 64 ++++++++++++++++++++++++++++++-------------------------- names.c | 2 +- random.c | 4 ++-- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/input.c b/input.c index 936db50..053881d 100644 --- a/input.c +++ b/input.c @@ -324,9 +324,13 @@ int tgetc(void) return c; } -static int get1unicode( int *k) { +/* GET1KEY: Get one keystroke. The only prefixes legal here are the SPEC + and CTRL prefixes. */ +static int get1unicode( int *up) { /* Accept UTF-8 sequence */ - int c = *k ; + int bytes ; + + int c = tgetc() ; if( c > 0xC1 && c <= 0xF4) { char utf[ 4] ; char cc ; @@ -339,22 +343,23 @@ static int get1unicode( int *k) { utf[ 3] = tgetc() ; } - return utf8_to_unicode( utf, 0, sizeof utf, (unicode_t *) k) ; - } else - return 1 ; + bytes = utf8_to_unicode( utf, 0, sizeof utf, (unicode_t *) up) ; + } else { + if( (c >= 0x00 && c <= 0x1F) || c == 0x7F) /* C0 control -> C- */ + c ^= CTRL | 0x40 ; + + *up = c ; + bytes = 1 ; + } + + return bytes ; } -/* GET1KEY: Get one keystroke. The only prefixes legal here are the SPEC - and CTRL prefixes. */ int get1key( void) { - /* get a keystroke */ - int c = tgetc() ; + int c ; + get1unicode( &c) ; - - if( (c >= 0x00 && c <= 0x1F) || c == 0x7F) /* C0 control -> C- */ - c ^= CTRL | 0x40 ; - - return c ; + return c ; } /* GETCMD: Get a command from the keyboard. Process all applicable @@ -463,12 +468,8 @@ handle_CSI: return CTLX | c; } -#ifdef CYGWIN -// get1unicode( &c) ; -#endif - - /* otherwise, just return it */ - return c; +/* otherwise, just return it */ + return c ; } /* A more generalized prompt/reply function allowing the caller @@ -490,16 +491,17 @@ static void echov( int c) { } } -static void rubc( char c) { +static void rubc( int c) { rubout() ; if( (c >= 0 && c < ' ') || c == 0x7F) { /* ^x range */ rubout() ; - if( c == '\n') { + if( c == '\n') { /* */ rubout() ; rubout() ; } - } + } else if( utf8_width( c) == 2) + rubout() ; } int getstring( const char *prompt, char *buf, int nbuf, int eolchar) @@ -537,14 +539,14 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) didtry = 0; #endif /* get a character from the user */ - c = get1key(); + int bytes = get1unicode( &c) ; /* Quoting? Store as it is */ if( quote_f == TRUE) { quote_f = FALSE ; - if( cpos < nbuf - 1) { + if( cpos < nbuf - bytes) { c = ectoc( c) ; - buf[ cpos++] = c ; + cpos += unicode_to_utf8( c, &buf[ cpos]) ; echov( c) ; TTflush() ; } @@ -579,8 +581,12 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) if( c == 0x7F || c == 0x08) { /* rubout/erase */ if (cpos != 0) { - rubc( buf[ --cpos]) ; + int c ; + + cpos -= 1 ; cpos -= utf8_revdelta( (unsigned char *) &buf[ cpos], cpos) ; + utf8_to_unicode( &buf[ cpos], 0, 4, (unicode_t *) &c) ; + rubc( c) ; TTflush(); } } else if( c == 0x15) { @@ -695,9 +701,7 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) quote_f = TRUE ; else { /* store as it is */ -// n = get1unicode( &c) ; /* fetch multiple bytes */ - int n = 4 ; /* long utf-8 encoding */ - if( cpos + n < nbuf) { + if( cpos + bytes < nbuf) { cpos += unicode_to_utf8( c, &buf[ cpos]) ; echov( c) ; TTflush() ; diff --git a/names.c b/names.c index 13044eb..a5c219d 100644 --- a/names.c +++ b/names.c @@ -177,7 +177,7 @@ const name_bind names[] = { {" next-word", forwword, META | 'F'} , {" nop", nullproc, SPEC | META | 'C'}, /* hook */ {"!open-line", openline, CTRL | 'O'} , - {" overwrite-string", ovstring, 0} , + {"!overwrite-string", ovstring, 0} , {" pipe-command", pipecmd, CTLX | '@'} , {" previous-line", (fnp_t) backline, CTRL | 'P'} , {" previous-page", (fnp_t) backpage, CTRL | 'Z'} , /* MV */ diff --git a/random.c b/random.c index 785c353..841ffa9 100644 --- a/random.c +++ b/random.c @@ -954,7 +954,7 @@ static int iovstring( int f, int n, const char *prompt, int (*fun)( char *)) { * int f, n; ignored arguments */ int istring( int f, int n) { - return iovstring( f, n, "String to insert: ", linstr) ; + return iovstring( f, n, "insert-string: ", linstr) ; } /* @@ -964,7 +964,7 @@ int istring( int f, int n) { * int f, n; ignored arguments */ int ovstring( int f, int n) { - return iovstring( f, n, "String to overwrite: ", lover) ; + return iovstring( f, n, "overwrite-string: ", lover) ; } /* end of random.c */ From d890880a520e9f904058951f27a6cab9703b4605 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 3 Aug 2021 13:37:06 +0800 Subject: [PATCH 19/37] Revise bindable functions usage of struct to named types. --- execute.c | 6 +- file.c | 130 +++++++++++---------------- file.h | 22 +++-- line.c | 169 +++++++++++++++++------------------ line.h | 18 ++-- names.c | 16 ---- random.c | 258 ++++++++++++++++++++---------------------------------- random.h | 67 +++++++------- region.c | 97 ++++++++------------ region.h | 27 +++--- search.c | 70 +++++++-------- search.h | 20 +++-- spawn.c | 6 +- spawn.h | 20 +++-- word.c | 32 ++----- word.h | 36 ++++---- 16 files changed, 427 insertions(+), 567 deletions(-) diff --git a/execute.c b/execute.c index d96bcf3..875f91d 100644 --- a/execute.c +++ b/execute.c @@ -78,7 +78,7 @@ static int insbrace( int n, int c) { count = 1 ; do { - if( boundry( curwp->w_dotp, curwp->w_doto, REVERSE)) { + if( boundary( curwp->w_dotp, curwp->w_doto, REVERSE)) { /* at beginning of buffer, no match to be found */ curwp->w_dotp = oldlp ; curwp->w_doto = oldoff ; @@ -166,7 +166,7 @@ static void fmatch( int ch) { 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)) + || boundary( curwp->w_dotp, curwp->w_doto, REVERSE)) break ; backchar( FALSE, 1) ; @@ -261,7 +261,7 @@ BINDABLE( getfence) { /* scan until we find a match, or reach the end of file */ count = 1 ; do { - if( boundry( curwp->w_dotp, curwp->w_doto, sdir)) { + if( boundary( curwp->w_dotp, curwp->w_doto, sdir)) { /* at buffer limit, no match to be found */ /* restore the current position */ curwp->w_dotp = oldlp ; diff --git a/file.c b/file.c index 68fe898..d372541 100644 --- a/file.c +++ b/file.c @@ -1,5 +1,4 @@ /* file.c -- implements file.h */ - #include "file.h" /* file.c @@ -65,21 +64,17 @@ boolean resterr( void) { return FALSE ; } -/* - * Read a file into the current - * buffer. This is really easy; all you do is - * find the name of the file, and call the standard - * "read a file into the current buffer" code. - * Bound to "C-X C-R". +/* Read a file into the current buffer. This is really easy; all you do is + * find the name of the file, and call the standard "read a file into the + * current buffer" code. Bound to C-X C-R read-file. */ -int fileread( int f, int n) { - int status ; +BINDABLE( fileread) { char *fname ; if( restflag) /* don't allow this command if restricted */ return resterr() ; - status = newmlarg( &fname, "Read file: ", sizeof( fname_t)) ; + int status = newmlarg( &fname, "read-file: ", sizeof( fname_t)) ; if( status == TRUE) { status = readin( fname, TRUE) ; free( fname) ; @@ -88,25 +83,19 @@ int fileread( int f, int n) { return status ; } -/* - * Insert a file into the current - * buffer. This is really easy; all you do is - * find the name of the file, and call the standard - * "insert a file into the current buffer" code. - * Bound to "C-X C-I". +/* Insert a file into the current buffer. This is really easy; all you do + * is find the name of the file, and call the standard "insert a file into + * the current buffer" code. Bound to C-X C-I insert-file. */ -int insfile( int f, int n) { - int status ; +BINDABLE( insfile) { char *fname ; if( restflag) /* don't allow this command if restricted */ return resterr() ; -// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly() ; /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; - status = newmlarg( &fname, "Insert file: ", sizeof( fname_t)) ; + int status = newmlarg( &fname, "insert-file: ", sizeof( fname_t)) ; if( status == TRUE) { status = ifile( fname) ; free( fname) ; @@ -118,23 +107,18 @@ int insfile( int f, int n) { return reposition( TRUE, -1) ; /* Redraw with dot at bottom of window */ } -/* - * Select a file for editing. - * Look around to see if you can find the - * file in another buffer; if you can find it - * just switch to the buffer. If you cannot find - * the file, create a new buffer, read in the - * text, and switch to the new buffer. - * Bound to C-X C-F. +/* Select a file for editing. Look around to see if you can find the file + * in another buffer; if you can find it just switch to the buffer. If you + * cannot find the file, create a new buffer, read in the text, and switch + * to the new buffer. Bound to C-X C-F find-file. */ -int filefind( int f, int n) { +BINDABLE( filefind) { char *fname ; /* file user wishes to find */ - int status ; /* status return */ if( restflag) /* don't allow this command if restricted */ return resterr() ; - status = newmlarg( &fname, "find-file: ", sizeof( fname_t)) ; + int status = newmlarg( &fname, "find-file: ", sizeof( fname_t)) ; if( status == TRUE) { status = getfile( fname, TRUE) ; free( fname) ; @@ -152,16 +136,15 @@ static void upd_mode( void) { wp->w_flag |= WFMODE ; } -int viewfile( int f, int n) { /* visit a file in VIEW mode */ +BINDABLE( viewfile) { /* visit a file in VIEW mode */ char *fname ; /* file user wishes to find */ - int status ; /* status return */ if( restflag) /* don't allow this command if restricted */ return resterr() ; - status = newmlarg( &fname, "View file: ", sizeof( fname_t)) ; + int status = newmlarg( &fname, "view-file: ", sizeof( fname_t)) ; if( status == TRUE) { - status = getfile(fname, FALSE) ; + status = getfile( fname, FALSE) ; free( fname) ; if( status == TRUE) { /* if we succeed, put it in view mode */ @@ -180,7 +163,7 @@ int viewfile( int f, int n) { /* visit a file in VIEW mode */ * boolean lockfl; check the file for locks? */ int getfile( const char *fname, boolean lockfl) { - struct buffer *bp; + buffer_p bp; int s; bname_t bname ; /* buffer name to put file */ @@ -264,7 +247,7 @@ int getfile( const char *fname, boolean lockfl) { int readin(const char *fname, boolean lockfl) { struct window *wp; - struct buffer *bp; + buffer_p bp; int status ; fio_code s ; @@ -437,23 +420,17 @@ void unqname(char *name) } } -/* - * Ask for a file name, and write the - * contents of the current buffer to that file. - * Update the remembered file name and clear the - * buffer changed flag. This handling of file names - * is different from the earlier versions, and - * is more compatible with Gosling EMACS than - * with ITS EMACS. Bound to "C-X C-W". +/* Ask for a file name, and write the content of the current buffer to that + * file. Update the remembered file name and clear the buffer changed + * flag. Bound to C-X C-W write-file. */ -int filewrite( int f, int n) { - int status ; +BINDABLE( filewrite) { char *fname ; if( restflag) /* don't allow this command if restricted */ return resterr() ; - status = newmlarg( &fname, "Write file: ", sizeof( fname_t)) ; + int status = newmlarg( &fname, "write-file: ", sizeof( fname_t)) ; if( status == TRUE) { if( strlen( fname) > sizeof( fname_t) - 1) status = FALSE ; @@ -469,32 +446,27 @@ int filewrite( int f, int n) { return status ; } -/* - * Save the contents of the current - * buffer in its associated file. Do nothing - * if nothing has changed (this may be a bug, not a - * feature). Error if there is no remembered file - * name for the buffer. Bound to "C-X C-S". May - * get called by "C-Z". +/* Save the content of the current buffer in its associated file. Do + * nothing if nothing has changed (this may be a bug, not a feature). + * Error if there is no remembered file name for the buffer. Bound to "C-X + * C-S save-file". May get called by "M-Z quick-exit". */ -int filesave( int f, int n) { -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ +BINDABLE( filesave) { assert( !(curbp->b_mode & MDVIEW)) ; - if ((curbp->b_flag & BFCHG) == 0) /* Return, no changes. */ - return TRUE; - if (curbp->b_fname[0] == 0) { /* Must have a name. */ - mloutstr( "No file name") ; - return FALSE; + if( (curbp->b_flag & BFCHG) == 0) /* Return, no changes. */ + return TRUE ; + + if( curbp->b_fname[0] == 0) { /* Must have a name. */ + mloutfmt( "%BNo file name") ; + return FALSE ; } /* complain about truncated files */ - if ((curbp->b_flag & BFTRUNC) != 0) { - if (mlyesno("Truncated file ... write it out") == FALSE) { - mloutstr( "(Aborted)") ; - return FALSE; - } + if( (curbp->b_flag & BFTRUNC) != 0 + && mlyesno("Truncated file ... write it out") == FALSE) { + mloutfmt( "%B(Aborted)") ; + return FALSE ; } return writeout( curbp->b_fname) ; @@ -544,23 +516,19 @@ int writeout( const char *fn) { return FALSE ; } -/* - * The command allows the user - * to modify the file name associated with - * the current buffer. It is like the "f" command - * in UNIX "ed". The operation is simple; just zap - * the name in the buffer structure, and mark the windows - * as needing an update. You can type a blank line at the - * prompt if you wish. +/* The command allows the user to modify the file name associated with the + current buffer. It is like the "f" command in UNIX "ed". The operation + is simple; just zap the name in the buffer structure, and mark the + windows as needing an update. You can type a blank line at the prompt + if you wish. Bound to C-X N change-file-name. */ -int filename( int f, int n) { - int status ; +BINDABLE( filename) { char *fname ; if( restflag) /* don't allow this command if restricted */ return resterr() ; - status = newmlarg( &fname, "Name: ", sizeof( fname_t)) ; + int status = newmlarg( &fname, "Name: ", sizeof( fname_t)) ; if( status == ABORT) return status ; else if( status == FALSE) @@ -653,3 +621,5 @@ static int ifile( const char *fname) { return (s == FIOERR) ? FALSE : TRUE ; } + +/* end of file.c */ diff --git a/file.h b/file.h index 68d649e..b168a4a 100644 --- a/file.h +++ b/file.h @@ -1,23 +1,29 @@ +/* file.h -- file centric commands */ + #ifndef _FILE_H_ #define _FILE_H_ #include "buffer.h" -#include "retcode.h" +#include "names.h" extern boolean restflag ; /* restricted use? */ boolean resterr( void) ; /* restricted error message */ -int fileread( int f, int n) ; -int insfile( int f, int n) ; -int filefind( int f, int n) ; -int viewfile( int f, int n) ; +/* Bindable functions */ +BINDABLE( filefind) ; +BINDABLE( fileread) ; +BINDABLE( filename) ; +BINDABLE( filesave) ; +BINDABLE( filewrite) ; +BINDABLE( insfile) ; +BINDABLE( viewfile) ; + int getfile( const char *fname, boolean lockfl) ; int readin( const char *fname, boolean lockfl) ; void makename( bname_t bname, const char *fname) ; void unqname( char *name) ; -int filewrite( int f, int n) ; -int filesave( int f, int n) ; int writeout( const char *fn) ; -int filename( int f, int n) ; #endif + +/* end of file.h */ diff --git a/line.c b/line.c index 30bcb98..f093e1d 100644 --- a/line.c +++ b/line.c @@ -1,6 +1,7 @@ -/* line.c - * - * The functions in this file are a general set of line management utilities. +/* line.c -- implements line.h */ +#include "line.h" + +/* The functions in this file are a general set of line management utilities. * They are the only routines that touch the text. They also touch the buffer * and window structures, to make sure that the necessary updating gets done. * There are routines in this file that handle the kill buffer too. It isn't @@ -13,12 +14,9 @@ * */ -#include "line.h" - #include -#include -#include -#include +#include /* NULL, offsetof() */ +#include /* malloc(), free() */ #include #include "buffer.h" @@ -41,13 +39,13 @@ static int ldelnewline( void) ; #define KBLOCK 250 /* sizeof kill buffer chunks */ -struct kill { +typedef struct kill { struct kill *d_next; /* Link to next chunk, NULL if last. */ char d_chunk[KBLOCK]; /* Deleted text. */ -}; +} *kill_p ; -static struct kill *kbufp = NULL ; /* current kill buffer chunk pointer */ -static struct kill *kbufh = NULL ; /* kill buffer header pointer */ +static kill_p kbufp = NULL ; /* current kill buffer chunk pointer */ +static kill_p kbufh = NULL ; /* kill buffer header pointer */ static int kused = KBLOCK ; /* # of bytes used in kill buffer */ static int klen ; /* length of kill buffer content */ static char *value = NULL ; /* temp buffer for value */ @@ -56,7 +54,7 @@ static char *value = NULL ; /* temp buffer for value */ * return some of the contents of the kill buffer */ char *getkill( void) { - struct kill *kp ; + kill_p kp ; char *cp ; if (kbufh == NULL) @@ -160,12 +158,14 @@ boolean forwchar( int f, int n) { */ line_p lalloc( int used) { #define BLOCK_SIZE 16 /* Line block chunk size. */ - line_p lp ; - int size ; -/* size = used + BLOCK_SIZE - used % BLOCK_SIZE ; */ - size = (used + BLOCK_SIZE) & ~(BLOCK_SIZE - 1) ; /* as BLOCK_SIZE is power of 2 */ - lp = (line_p) malloc( offsetof( struct line, l_text) + size) ; +/* rounding down use masking instead or modulo when BLOCK_SIZE is power of 2 */ +#if (BLOCK_SIZE & -BLOCK_SIZE) == BLOCK_SIZE + int size = (used + BLOCK_SIZE) & ~(BLOCK_SIZE - 1) ; +#else + int size = used + BLOCK_SIZE - used % BLOCK_SIZE ; +#endif + line_p lp = (line_p) malloc( offsetof( struct line, l_text) + size) ; if( lp == NULL) mloutstr( "(OUT OF MEMORY)") ; else { @@ -183,7 +183,7 @@ line_p lalloc( int used) { * conditions described in the above comments don't hold here. */ void lfree( line_p lp) { - struct buffer *bp; + buffer_p bp; struct window *wp; wp = wheadp; @@ -295,18 +295,13 @@ int linstr( char *instr) { boolean linsert_byte( int n, int c) { char *cp1; char *cp2; - struct line *lp1; - struct line *lp2; - struct line *lp3; + line_p lp1, lp2, lp3 ; int doto; int i; struct window *wp; assert( (curbp->b_mode & MDVIEW) == 0) ; -#if 0 - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ -#endif + lchange(WFEDIT); lp1 = curwp->w_dotp; /* Current line */ if (lp1 == curbp->b_linep) { /* At the end: special */ @@ -314,8 +309,11 @@ boolean linsert_byte( int n, int c) { mloutstr( "bug: linsert") ; return FALSE; } - if ((lp2 = lalloc(n)) == NULL) /* Allocate new line */ - return FALSE; + + lp2 = lalloc( n) ; /* Allocate new line */ + if( lp2 == NULL) + return FALSE ; + lp3 = lp1->l_bp; /* Previous line */ lp3->l_fp = lp2; /* Link in */ lp2->l_fp = lp1; @@ -329,8 +327,10 @@ boolean linsert_byte( int n, int c) { } doto = curwp->w_doto; /* Save for later. */ if (lp1->l_used + n > lp1->l_size) { /* Hard: reallocate */ - if ((lp2 = lalloc(lp1->l_used + n)) == NULL) - return FALSE; + lp2 = lalloc( lp1->l_used + n) ; + if( lp2 == NULL) + return FALSE ; + cp1 = &lp1->l_text[0]; cp2 = &lp2->l_text[0]; while (cp1 != &lp1->l_text[doto]) @@ -374,8 +374,6 @@ boolean linsert_byte( int n, int c) { int linsert( int n, unicode_t c) { assert( n >= 0) ; -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; if( n > 0) { @@ -398,16 +396,14 @@ int linsert( int n, unicode_t c) { return TRUE; } -/* - * Overwrite a character into the current line at the current position +/* Overwrite a character into the current line at the current position * - * int c; character to overwrite on current position + * int c ; character to overwrite on current position */ static int lowrite( int c) { if( curwp->w_doto < curwp->w_dotp->l_used - && ( - lgetc(curwp->w_dotp, curwp->w_doto) != '\t' || - ((curwp->w_doto) % tabwidth) == (tabwidth - 1) + && ( lgetc( curwp->w_dotp, curwp->w_doto) != '\t' + || (curwp->w_doto % tabwidth) == (tabwidth - 1) )) ldelchar( 1, FALSE) ; @@ -420,15 +416,12 @@ static int lowrite( int c) { int lover( char *ostr) { int status = TRUE ; - if (ostr != NULL) { + if( ostr != NULL) { char tmpc ; while( (tmpc = *ostr++)) { - status = - (tmpc == '\n' ? lnewline() : lowrite(tmpc)); - - /* Insertion error? */ - if( status != TRUE) { + status = (tmpc == '\n' ? lnewline() : lowrite( tmpc)) ; + if( status != TRUE) { /* Insertion error? */ mloutstr( "%Out of memory while overwriting") ; return status ; } @@ -446,17 +439,13 @@ int lover( char *ostr) { * update of dot and mark is a bit easier then in the above case, because the * split forces more updating. */ -int lnewline(void) -{ +int lnewline( void) { char *cp1; char *cp2; - struct line *lp1; - struct line *lp2; + line_p lp1, lp2 ; int doto; struct window *wp; -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; #if SCROLLCODE @@ -466,8 +455,10 @@ int lnewline(void) #endif lp1 = curwp->w_dotp; /* Get the address and */ doto = curwp->w_doto; /* offset of "." */ - if ((lp2 = lalloc(doto)) == NULL) /* New first half line */ - return FALSE; + lp2 = lalloc( doto) ; /* New first half line */ + if( lp2 == NULL) + return FALSE ; + cp1 = &lp1->l_text[0]; /* Shuffle text around */ cp2 = &lp2->l_text[0]; while (cp1 != &lp1->l_text[doto]) @@ -519,12 +510,14 @@ int lgetchar( unicode_t *c) { */ boolean ldelchar( long n, boolean kflag) { /* testing for read only mode is done by ldelete() */ - while (n-- > 0) { + while( n-- > 0) { unicode_t c; - if (!ldelete(lgetchar(&c), kflag)) - return FALSE; + + if( !ldelete( lgetchar( &c), kflag)) + return FALSE ; } - return TRUE; + + return TRUE ; } /* @@ -539,13 +532,11 @@ boolean ldelchar( long n, boolean kflag) { boolean ldelete( long n, boolean kflag) { char *cp1; char *cp2; - struct line *dotp; + line_p dotp; int doto; int chunk; struct window *wp; -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; while( n > 0) { @@ -641,20 +632,14 @@ char *getctext( void) { * about in memory. Return FALSE on error and TRUE if all looks ok. Called by * "ldelete" only. */ -static int ldelnewline(void) -{ +static int ldelnewline( void) { char *cp1; char *cp2; - struct line *lp1; - struct line *lp2; - struct line *lp3; + line_p lp1, lp2, lp3 ; struct window *wp; assert( (curbp->b_mode & MDVIEW) == 0) ; -#if 0 - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ -#endif + lp1 = curwp->w_dotp; lp2 = lp1->l_fp; if (lp2 == curbp->b_linep) { /* At the buffer end. */ @@ -687,8 +672,11 @@ static int ldelnewline(void) free((char *) lp2); return TRUE; } - if ((lp3 = lalloc(lp1->l_used + lp2->l_used)) == NULL) - return FALSE; + + lp3 = lalloc( lp1->l_used + lp2->l_used) ; + if( lp3 == NULL) + return FALSE ; + cp1 = &lp1->l_text[0]; cp2 = &lp3->l_text[0]; while (cp1 != &lp1->l_text[lp1->l_used]) @@ -730,7 +718,7 @@ static int ldelnewline(void) */ void kdelete(void) { - struct kill *kp; /* ptr to scan kill buffer chunk list */ + kill_p kp; /* ptr to scan kill buffer chunk list */ if (kbufh != NULL) { @@ -759,30 +747,30 @@ void kdelete(void) * * int c; character to insert in the kill buffer */ -int kinsert(int c) -{ - struct kill *nchunk; /* ptr to newly malloced chunk */ - +int kinsert( int c) { /* check to see if we need a new chunk */ - if (kused >= KBLOCK) { - if ((nchunk = (struct kill *)malloc(sizeof(struct kill))) == NULL) - return FALSE; + if( kused >= KBLOCK) { + kill_p nchunk = malloc( sizeof *nchunk) ; + if( nchunk == NULL) + return FALSE ; + if( kbufh == NULL) { /* set head ptr if first time */ - kbufh = nchunk; + kbufh = nchunk ; klen = 0 ; } - if (kbufp != NULL) /* point the current to this new one */ - kbufp->d_next = nchunk; - kbufp = nchunk; - kbufp->d_next = NULL; - kused = 0; + if( kbufp != NULL) /* point the current to this new one */ + kbufp->d_next = nchunk ; + + kbufp = nchunk ; + kbufp->d_next = NULL ; + kused = 0 ; } /* and now insert the character */ - kbufp->d_chunk[kused++] = c; + kbufp->d_chunk[ kused++] = c ; klen += 1 ; - return TRUE; + return TRUE ; } /* @@ -790,15 +778,12 @@ int kinsert(int c) * is done by the standard insert routines. All you do is run the loop, and * check for errors. Bound to "C-Y". */ -int yank(int f, int n) -{ +BINDABLE( yank) { int c; int i; char *sp; /* pointer into string to insert */ - struct kill *kp; /* pointer into kill buffer */ + kill_p kp; /* pointer into kill buffer */ -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; if (n < 0) @@ -839,3 +824,5 @@ boolean rdonly( void) { mloutfmt( "%B(Key illegal in VIEW mode)") ; return FALSE ; } + +/* end of line.c */ diff --git a/line.h b/line.h index 18ed04f..656a83b 100644 --- a/line.h +++ b/line.h @@ -1,7 +1,9 @@ -#ifndef LINE_H_ -#define LINE_H_ +/* line.h -- line centric interface */ -#include "retcode.h" +#ifndef _LINE_H_ +#define _LINE_H_ + +#include "names.h" #include "utf8.h" /* @@ -34,8 +36,8 @@ char *getkill( void) ; /* Bindable functions */ boolean backchar( int f, int n) ; boolean forwchar( int f, int n) ; -int insspace( int f, int n) ; -int yank( int f, int n) ; +BINDABLE( insspace) ; +BINDABLE( yank) ; void lfree( line_p lp) ; void lchange( int flag) ; @@ -46,7 +48,7 @@ int lover( char *ostr) ; int lnewline( void) ; boolean ldelete( long n, boolean kflag) ; boolean ldelchar( long n, boolean kflag) ; -int lgetchar( unicode_t *) ; +int lgetchar( unicode_t *cref) ; char *getctext( void) ; void kdelete( void) ; int kinsert( int c) ; @@ -54,4 +56,6 @@ line_p lalloc( int minsize) ; /* Allocate a line of at least minsize chars. */ boolean rdonly( void) ; /* Read Only error message */ -#endif /* LINE_H_ */ +#endif /* _LINE_H_ */ + +/* end of line.h */ diff --git a/names.c b/names.c index a5c219d..f357a99 100644 --- a/names.c +++ b/names.c @@ -54,9 +54,7 @@ const name_bind names[] = { {" clear-and-redraw", redraw, CTRL | 'L'} , {" clear-message-line", clrmes, 0} , {" copy-region", copyregion, META | 'W'} , -#if WORDPRO {" count-words", wordcount, META | CTRL | 'C'} , -#endif {" ctlx-prefix", cex, CTRL | 'X'} , {"!delete-blank-lines", deblank, CTLX | CTRL | 'O'} , {" delete-buffer", killbuffer, CTLX | 'K'} , @@ -70,15 +68,11 @@ const name_bind names[] = { {" delete-window", delwind, CTLX | '0'} , {" describe-bindings", desbind, 0} , {" describe-key", deskey, CTLX | '?'} , -#if AEDIT {"!detab-line", detab, CTLX | CTRL | 'D'} , /* X^A */ -#endif {" end-macro", ctlxrp, CTLX | ')'} , {" end-of-file", (fnp_t) gotoeob, META | '>'} , {" end-of-line", (fnp_t) gotoeol, CTRL | 'E'} , -#if AEDIT {"!entab-line", entab, CTLX | CTRL | 'E'} , -#endif {" exchange-point-and-mark", (fnp_t) swapmark, CTLX | CTRL | 'X'} , {" execute-buffer", execbuf, 0} , {" execute-command-line", execcmd, 0} , @@ -130,9 +124,7 @@ const name_bind names[] = { #endif {" execute-program", execprg, CTLX | '$'} , {" exit-emacs", quit, CTLX | CTRL | 'C'} , -#if WORDPRO {"!fill-paragraph", fillpara, META | 'Q'} , -#endif {"!filter-buffer", filter_buffer, CTLX | '#'} , {" find-file", filefind, CTLX | CTRL | 'F'} , {" forward-character", (fnp_t) forwchar, CTRL | 'F'} , @@ -152,12 +144,10 @@ const name_bind names[] = { {"!insert-file", insfile, CTLX | CTRL | 'I'} , {"!insert-space", insspace, CTRL | 'C'} , {"!insert-string", istring, 0} , -#if WORDPRO #if PKCODE {"!justify-paragraph", justpara, META | 'J'} , #endif {"!kill-paragraph", killpara, META | CTRL | 'W'} , -#endif {"!kill-region", killregion, CTRL | 'W'} , {"!kill-to-end-of-line", killtext, CTRL | 'K'} , {" list-buffers", listbuffers, CTLX | CTRL | 'B'} , @@ -170,9 +160,7 @@ const name_bind names[] = { {" next-buffer", nextbuffer, CTLX | 'X'} , {" next-line", (fnp_t) forwline, CTRL | 'N'} , {" next-page", (fnp_t) forwpage, CTRL | 'V'} , -#if WORDPRO {" next-paragraph", gotoeop, META | 'N'} , -#endif {" next-window", nextwind, CTLX | 'O'} , {" next-word", forwword, META | 'F'} , {" nop", nullproc, SPEC | META | 'C'}, /* hook */ @@ -181,9 +169,7 @@ const name_bind names[] = { {" pipe-command", pipecmd, CTLX | '@'} , {" previous-line", (fnp_t) backline, CTRL | 'P'} , {" previous-page", (fnp_t) backpage, CTRL | 'Z'} , /* MV */ -#if WORDPRO {" previous-paragraph", gotobop, META | 'P'} , -#endif {" previous-window", prevwind, CTLX | 'P'} , {" previous-word", backword, META | 'B'} , {"!query-replace-string", qreplace, META | CTRL | 'R'} , @@ -221,9 +207,7 @@ const name_bind names[] = { {" suspend-emacs", bktoshell, CTLX | 'D'} , /* BSD MS */ #endif {"!transpose-characters", (fnp_t) twiddle, CTRL | 'T'} , -#if AEDIT {"!trim-line", trim, CTLX | CTRL | 'T'} , -#endif {" unbind-key", unbindkey, META | CTRL | 'K'} , {" universal-argument", unarg, CTRL | 'U'} , {" unmark-buffer", unmark, META | '~'} , diff --git a/random.c b/random.c index 841ffa9..98441d3 100644 --- a/random.c +++ b/random.c @@ -51,26 +51,22 @@ int lastflag ; /* Flags, last command */ static int adjustmode( int kind, int global) ; static int cinsert( void) ; -/* - * Set fill column to n. +/* Set fill column to n. Bound to C-X F set-fill-column. */ -int setfillcol(int f, int n) -{ - fillcol = n; - mlwrite("(Fill column is %d)", n); - return TRUE; +BINDABLE( setfillcol) { + fillcol = n ; + mlwrite( "(Fill column is %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 =". +/* 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 = buffer-position. */ -int showcpos(int f, int n) -{ - struct line *lp; /* current line */ +BINDABLE( showcpos) { + line_p lp ; /* current line */ long numchars; /* # of chars in file */ int numlines; /* # of lines in file */ long predchars; /* # chars preceding point */ @@ -128,7 +124,7 @@ int showcpos(int f, int n) int getcline(void) { /* get the current line number */ - struct line *lp; /* current line */ + line_p lp ; /* current line */ int numlines; /* # of lines before point */ /* starting at the beginning of the buffer */ @@ -154,7 +150,7 @@ int getcline(void) int getccol(int bflg) { int i, col; - struct line *dlp = curwp->w_dotp; + line_p dlp = curwp->w_dotp ; int byte_offset = curwp->w_doto; int len = llength(dlp); @@ -224,14 +220,11 @@ boolean setccol( int pos) { */ boolean twiddle( int f, int n) { unicode_t c ; - int len ; boolean eof_f = FALSE ; -// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly() ; /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; - len = llength( curwp->w_dotp) ; + int len = llength( curwp->w_dotp) ; if( len < 2 || curwp->w_doto == 0) /* at least 2 chars & not bol */ return FALSE ; @@ -255,44 +248,36 @@ boolean twiddle( int f, int n) { return TRUE ; } -/* - * Quote the next character, and insert it into the buffer. All the characters - * are taken literally, with the exception of the newline, which always has - * its line splitting meaning. The character is always read, even if it is - * inserted 0 times, for regularity. Bound to "C-Q" +/* Quote the next character, and insert it into the buffer. All the + characters are taken literally, with the exception of the newline, which + always has its line splitting meaning. The character is always read, + even if it is inserted 0 times, for regularity. Bound to C-Q + quote-character. */ -int quote(int f, int n) -{ -// int c; -// -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ +BINDABLE( quote) { + int ret ; + assert( !(curbp->b_mode & MDVIEW)) ; - int c = tgetc(); - if (n < 0) - return FALSE; - if (n == 0) - return TRUE; - if (c == '\n') { - int s ; + int c = ectoc( get1key()) ; + if( n < 0) + ret = FALSE ; + else if( n == 0) + ret = TRUE ; + else if( c == '\n') + do + ret = lnewline() ; + while( ret == TRUE && --n) ; + else + ret = linsert( n, c) ; - do { - s = lnewline(); - } while (s == TRUE && --n); - return s; - } - return linsert(n, c); + return ret ; } -/* - * 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. - * If n > 1, simulate tab stop every n-characters using spaces. 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". +/* Insert tab/blank/space up to nth next tabulation according to hard/soft + tab current state and tab width. Bound to C-I handle-tab. */ -int insert_tab( int f, int n) { +BINDABLE( insert_tab) { int status ; if( n < 0) @@ -309,27 +294,20 @@ int insert_tab( int f, int n) { return status ; } -#if AEDIT /* * change tabs to spaces * * int f, n; default flag and numeric repeat count */ -int detab(int f, int n) -{ - int inc; /* increment to next line [sgn(n)] */ - -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ - +BINDABLE( detab) { assert( !(curbp->b_mode & MDVIEW)) ; - if (f == FALSE) - n = 1; + if( f == FALSE) + n = 1 ; /* loop thru detabbing n lines */ - inc = ((n > 0) ? 1 : -1); - while (n) { + int inc = (n > 0) ? 1 : -1 ; /* increment to next line [sgn(n)] */ + for( ; n ; n -= inc) { curwp->w_doto = 0; /* start at the beginning */ /* detab the entire current line */ @@ -349,9 +327,8 @@ int detab(int f, int n) /* advance/or back to the next line */ if( forwline( TRUE, inc) == FALSE) break ; - - n -= inc; } + curwp->w_doto = 0; /* to the begining of the line */ thisflag &= ~CFCPCN; /* flag that this resets the goal column */ lchange(WFEDIT); /* yes, we have made at least an edit */ @@ -359,27 +336,21 @@ int detab(int f, int n) } /* - * change spaces to tabs where posible + * change spaces to tabs where possible * * int f, n; default flag and numeric repeat count */ -int entab(int f, int n) -{ +BINDABLE( entab) { #define nextab(a) (a + tabwidth - a % tabwidth) - int inc; /* increment to next line [sgn(n)] */ - -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ - assert( !(curbp->b_mode & MDVIEW)) ; - if (f == FALSE) - n = 1; + if( f == FALSE) + n = 1 ; /* loop thru entabbing n lines */ - inc = ((n > 0) ? 1 : -1); - while (n) { + int inc = (n > 0) ? 1 : -1 ; /* increment to next line [sgn(n)] */ + for( ; n ; n -= inc) { int fspace ; /* pointer to first space if in a run */ int ccol ; /* current cursor column */ @@ -426,9 +397,8 @@ int entab(int f, int n) /* advance/or back to the next line */ if( forwline( TRUE, inc) == FALSE) break ; - - n -= inc; } + curwp->w_doto = 0; /* to the begining of the line */ thisflag &= ~CFCPCN; /* flag that this resets the goal column */ lchange(WFEDIT); /* yes, we have made at least an edit */ @@ -440,21 +410,15 @@ int entab(int f, int n) * * int f, n; default flag and numeric repeat count */ -int trim(int f, int n) -{ - int inc; /* increment to next line [sgn(n)] */ - -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ - +BINDABLE( trim) { assert( !(curbp->b_mode & MDVIEW)) ; - if (f == FALSE) - n = 1; + if( f == FALSE) + n = 1 ; /* loop thru trimming n lines */ - inc = ((n > 0) ? 1 : -1); - while (n) { + int inc = (n > 0) ? 1 : -1 ; /* increment to next line [sgn(n)] */ + for( ; n ; n -= inc) { line_p lp ; /* current line pointer */ int offset ; /* original line offset position */ int length ; /* current length */ @@ -468,60 +432,46 @@ int trim(int f, int n) if( c != ' ' && c != '\t') break ; } - + lp->l_used = length; /* advance/or back to the next line */ if( forwline( TRUE, inc) == FALSE) break ; - - n -= inc; } + lchange(WFEDIT); thisflag &= ~CFCPCN; /* flag that this resets the goal column */ return (n == 0) ? TRUE : FALSE ; } -#endif /* * 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". */ -int openline(int f, int n) -{ - int i; - int s; - -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ +BINDABLE( openline) { assert( !(curbp->b_mode & MDVIEW)) ; - 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; + int ret = (n < 0) ? FALSE : TRUE ; + for( int i = n ; ret == TRUE && i ; i--) /* Insert newlines. */ + ret = lnewline() ; + + if( ret == TRUE) /* Then back up overtop */ + ret = backchar( f, n) ; /* of them all. */ + + return ret ; } /* * Insert a newline. Bound to "C-M". If we are in CMODE, do automatic * indentation as specified. */ -int insert_newline(int f, int n) -{ -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ +BINDABLE( insert_newline) { assert( !(curbp->b_mode & MDVIEW)) ; - if (n < 0) - return FALSE; + if( n < 0) + return FALSE ; /* if we are in C mode and this is a default */ if (n == 1 && (curbp->b_mode & MDCMOD) && @@ -569,7 +519,7 @@ static int cinsert(void) nicol = 0 ; for( i = 0 ; i < tptr ; i += 1) { int ch ; - + ch = cptr[ i] ; if( ch == ' ') nicol += 1 ; @@ -610,22 +560,17 @@ static int cinsert(void) } -/* - * 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. +/* 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 + * delete-blank-lines. Any argument is ignored. */ -int deblank(int f, int n) -{ - struct line *lp1; - struct line *lp2; - long nld; +BINDABLE( deblank) { + line_p lp1, lp2 ; + long nld ; -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; lp1 = curwp->w_dotp; @@ -642,28 +587,24 @@ int deblank(int f, int n) return ldelete(nld, FALSE); } -/* - * Insert a newline, then enough tabs and spaces to duplicate the indentation - * of the previous line. Assumes tabs are every tabwidth characters. - * 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". +/* Insert a newline, then enough tabs and spaces to duplicate the + * indentation of the previous line. Assumes tabs are every tabwidth + * characters. 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 + * newline-and-indent. */ -int indent( int f, int n) { - int nicol ; +BINDABLE( indent) { int i ; -// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly() ; /* we are in read only mode */ - assert( !(curbp->b_mode & MDVIEW)) ; if( n < 0) return FALSE ; /* number of columns to indent */ - nicol = 0 ; + int nicol = 0 ; for( i = 0 ; i < llength( curwp->w_dotp) ; i += 1) { int c ; @@ -693,10 +634,7 @@ int indent( int f, int n) { * 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". */ -int forwdel( int f, int n) { -// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly() ; /* we are in read only mode */ - +BINDABLE( forwdel) { assert( !(curbp->b_mode & MDVIEW)) ; if( n == 0) @@ -719,10 +657,7 @@ int forwdel( int f, int n) { * forward, this actually does a kill if presented with an argument. Bound to * both "RUBOUT" and "C-H". */ -int backdel( int f, int n) { -// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly() ; /* we are in read only mode */ - +BINDABLE( backdel) { assert( !(curbp->b_mode & MDVIEW)) ; if( n == 0) @@ -747,13 +682,10 @@ int backdel( int f, int n) { * number of newlines. If called with a negative argument it kills backwards * that number of newlines. Normally bound to "C-K". */ -int killtext(int f, int n) -{ - struct line *nextp; +BINDABLE( killtext) { + line_p nextp ; long chunk; -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; if ((lastflag & CFKILL) == 0) /* Clear kill buffer if */ @@ -953,7 +885,7 @@ static int iovstring( int f, int n, const char *prompt, int (*fun)( char *)) { * * int f, n; ignored arguments */ -int istring( int f, int n) { +BINDABLE( istring) { return iovstring( f, n, "insert-string: ", linstr) ; } @@ -963,7 +895,7 @@ int istring( int f, int n) { * * int f, n; ignored arguments */ -int ovstring( int f, int n) { +BINDABLE( ovstring) { return iovstring( f, n, "overwrite-string: ", lover) ; } diff --git a/random.h b/random.h index eb772d7..6f7e657 100644 --- a/random.h +++ b/random.h @@ -1,49 +1,48 @@ +/* random.h -- various commands */ + #ifndef _RANDOM_H_ #define _RANDOM_H_ +#include "names.h" -#include "retcode.h" +/* Command flags */ +#define CFCPCN 0x0001 /* Flag that last command was C-P, C-N */ +#define CFKILL 0x0002 /* Flag that last command was a kill */ +extern int thisflag ; /* Flags, this command */ +extern int lastflag ; /* Flags, last command */ -#define AEDIT 1 - -extern int fillcol ; /* Fill column */ +extern int fillcol ; /* Fill column */ extern boolean hardtab ; /* Use hard tab instead of soft tab */ -/* Uninitialized global external declarations. */ - -#define CFCPCN 0x0001 /* Last command was C-P, C-N */ -#define CFKILL 0x0002 /* Last command was a kill */ - -extern int thisflag ; /* Flags, this command */ -extern int lastflag ; /* Flags, last command */ - -int setfillcol( int f, int n) ; -int showcpos( int f, int n) ; int getcline( void) ; int getccol( int bflg) ; boolean setccol( int pos) ; + +/* Bindable functions */ +BINDABLE( setfillcol) ; +BINDABLE( showcpos) ; boolean twiddle( int f, int n) ; -int quote( int f, int n) ; -int insert_tab( int f, int n) ; -#if AEDIT -int detab( int f, int n) ; -int entab( int f, int n) ; -int trim( int f, int n) ; -#endif -int openline( int f, int n) ; -int insert_newline( int f, int n) ; -int deblank( int f, int n) ; -int indent( int f, int n) ; -int forwdel( int f, int n) ; -int backdel( int f, int n) ; -int killtext( int f, int n) ; -int setemode( int f, int n) ; -int delmode( int f, int n) ; -int setgmode( int f, int n) ; -int delgmode( int f, int n) ; -int istring( int f, int n) ; -int ovstring( int f, int n) ; +BINDABLE( quote) ; +BINDABLE( insert_tab) ; +BINDABLE( detab) ; +BINDABLE( entab) ; +BINDABLE( trim) ; +BINDABLE( openline) ; +BINDABLE( insert_newline) ; +BINDABLE( deblank) ; +BINDABLE( indent) ; +BINDABLE( forwdel) ; +BINDABLE( backdel) ; +BINDABLE( killtext) ; +BINDABLE( setemode) ; +BINDABLE( delmode) ; +BINDABLE( setgmode) ; +BINDABLE( delgmode) ; +BINDABLE( istring) ; +BINDABLE( ovstring) ; #endif + +/* end of random.h */ diff --git a/region.c b/region.c index fcf5d71..00944c5 100644 --- a/region.c +++ b/region.c @@ -20,19 +20,14 @@ #include "random.h" #include "window.h" -/* - * Kill the region. Ask "getregion" - * to figure out the bounds of the region. - * Move "." to the start, and kill the characters. - * Bound to "C-W". +/* Kill the region. Ask "getregion" to figure out the bounds of the + * region. Move "." to the start, and kill the characters. Bound to + * "C-W". */ -int killregion(int f, int n) -{ +BINDABLE( killregion) { int s; - struct region region; + region_t region; -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; if ((s = getregion(®ion)) != TRUE) @@ -45,18 +40,15 @@ int killregion(int f, int n) return ldelete(region.r_size, TRUE); } -/* - * Copy all of the characters in the - * region to the kill buffer. Don't move dot - * at all. This is a bit like a kill region followed - * by a yank. Bound to "M-W". +/* Copy all of the characters in the region to the kill buffer. Don't move + * dot at all. This is a bit like a kill region followed by a yank. Bound + * to "M-W". */ -int copyregion(int f, int n) -{ - struct line *linep; +BINDABLE( copyregion) { + line_p linep; int loffs; int s; - struct region region; + region_t region; if ((s = getregion(®ion)) != TRUE) return s; @@ -81,24 +73,18 @@ int copyregion(int f, int n) return TRUE; } -/* - * Lower case region. Zap all of the upper - * case characters in the region to lower case. Use - * the region code to set the limits. Scan the buffer, - * doing the changes. Call "lchange" to ensure that - * redisplay is done in all buffers. Bound to - * "C-X C-L". +/* Lower case region. Zap all of the upper case characters in the region + * to lower case. Use the region code to set the limits. Scan the buffer, + * doing the changes. Call "lchange" to ensure that redisplay is done in + * all buffers. Bound to "C-X C-L". */ -int lowerregion(int f, int n) -{ - struct line *linep; +BINDABLE( lowerregion) { + line_p linep; int loffs; int c; int s; - struct region region; + region_t region; -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; if ((s = getregion(®ion)) != TRUE) @@ -120,24 +106,18 @@ int lowerregion(int f, int n) return TRUE; } -/* - * Upper case region. Zap all of the lower - * case characters in the region to upper case. Use - * the region code to set the limits. Scan the buffer, - * doing the changes. Call "lchange" to ensure that - * redisplay is done in all buffers. Bound to - * "C-X C-L". +/* Upper case region. Zap all of the lower case characters in the region + * to upper case. Use the region code to set the limits. Scan the buffer, + * doing the changes. Call "lchange" to ensure that redisplay is done in + * all buffers. Bound to "C-X C-L". */ -int upperregion(int f, int n) -{ - struct line *linep; +BINDABLE( upperregion) { + line_p linep; int loffs; int c; int s; - struct region region; + region_t region; -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; if ((s = getregion(®ion)) != TRUE) @@ -159,23 +139,16 @@ int upperregion(int f, int n) return TRUE; } -/* - * This routine figures out the - * bounds of the region in the current window, and - * fills in the fields of the "struct region" structure pointed - * to by "rp". Because the dot and mark are usually very - * close together, we scan outward from dot looking for - * mark. This should save time. Return a standard code. - * Callers of this routine should be prepared to get - * an "ABORT" status; we might make this have the - * conform thing later. +/* This routine figures out the bounds of the region in the current window, + * and fills in the fields of the "region_t" structure pointed to by "rp". + * Because the dot and mark are usually very close together, we scan + * outward from dot looking for mark. This should save time. Return a + * standard code. Callers of this routine should be prepared to get an + * "ABORT" status; we might make this have the conform thing later. */ -int getregion(struct region *rp) -{ - struct line *flp; - struct line *blp; - long fsize; - long bsize; +int getregion( region_p rp) { + line_p flp, blp ; + long fsize, bsize ; if (curwp->w_markp == NULL) { mloutstr( "No mark set in this window") ; @@ -223,3 +196,5 @@ int getregion(struct region *rp) mloutstr( "Bug: lost mark") ; return FALSE; } + +/* end of region.c */ diff --git a/region.h b/region.h index 1e46e7f..991d1a0 100644 --- a/region.h +++ b/region.h @@ -1,3 +1,4 @@ +/* region.h -- a region starts at the mark and end at the dot */ #ifndef _REGION_H_ #define _REGION_H_ @@ -7,16 +8,22 @@ * The starting position of a region, and the size of the region in * characters, is kept in a region structure. Used by the region commands. */ -struct region { - struct line *r_linep; /* Origin struct line address. */ - int r_offset; /* Origin struct line offset. */ - long r_size; /* Length in characters. */ -}; +typedef struct { + line_p r_linep ; /* Origin struct line address. */ + int r_offset ; /* Origin struct line offset. */ + long r_size ; /* Length in characters. */ +} region_t ; -int killregion( int f, int n) ; -int copyregion( int f, int n) ; -int lowerregion( int f, int n) ; -int upperregion( int f, int n) ; -int getregion( struct region *rp) ; +typedef region_t *region_p ; + +/* Bindable functions */ +BINDABLE( killregion) ; +BINDABLE( copyregion) ; +BINDABLE( lowerregion) ; +BINDABLE( upperregion) ; + +int getregion( region_p rp) ; #endif + +/* end of region.h */ diff --git a/search.c b/search.c index 029becf..6e0dace 100644 --- a/search.c +++ b/search.c @@ -87,7 +87,7 @@ unsigned int matchlen = 0 ; static unsigned int mlenold = 0 ; char *patmatch = NULL ; -static struct line *matchline = NULL; +static line_p matchline = NULL; static int matchoff = 0; spat_t pat ; /* Search pattern */ @@ -165,10 +165,10 @@ static struct magic_replacement rmcpat[NPAT]; /* The replacement magic array. */ static int mcscanner( struct magic *mcpatrn, int direct, int beg_or_end) ; #endif -static int amatch(struct magic *mcptr, int direct, struct line **pcwline, int *pcwoff); +static int amatch(struct magic *mcptr, int direct, line_p *pcwline, int *pcwoff); static int readpattern(char *prompt, char *apat, int srch); static int replaces(int kind, int f, int n); -static int nextch(struct line **pcurline, int *pcuroff, int dir); +static int nextch( line_p *pcurline, int *pcuroff, int dir); static int mcstr(void); static int rmcstr(void); static int mceq(int bc, struct magic *mt); @@ -390,7 +390,7 @@ int backhunt(int f, int n) */ static int mcscanner(struct magic *mcpatrn, int direct, int beg_or_end) { - struct line *curline; /* current line during scan */ + line_p curline; /* current line during scan */ int curoff; /* position within current line */ /* If we are going in reverse, then the 'end' is actually @@ -413,7 +413,7 @@ static int mcscanner(struct magic *mcpatrn, int direct, int beg_or_end) /* Scan each character until we hit the head link record. */ - while (!boundry(curline, curoff, direct)) { + while (!boundary(curline, curoff, direct)) { /* Save the current position in case we need to * restore it on a match, and initialize matchlen to * zero in case we are doing a search for replacement. @@ -454,13 +454,13 @@ static int mcscanner(struct magic *mcpatrn, int direct, int beg_or_end) * * struct magic *mcptr; string to scan for * int direct; which way to go. - * struct line **pcwline; current line during scan + * line_p *pcwline; current line during scan * int *pcwoff; position within current line */ -static int amatch(struct magic *mcptr, int direct, struct line **pcwline, int *pcwoff) +static int amatch(struct magic *mcptr, int direct, line_p *pcwline, int *pcwoff) { int c; /* character at current position */ - struct line *curline; /* current line during scan */ + line_p curline; /* current line during scan */ int curoff; /* position within current line */ int nchars; @@ -600,9 +600,9 @@ int scanner(const char *patrn, int direct, int beg_or_end) { int c; /* character at current position */ const char *patptr; /* pointer into pattern */ - struct line *curline; /* current line during scan */ + line_p curline; /* current line during scan */ int curoff; /* position within current line */ - struct line *scanline; /* current line during scanning */ + line_p scanline; /* current line during scanning */ int scanoff; /* position in scanned line */ /* If we are going in reverse, then the 'end' is actually @@ -617,7 +617,7 @@ int scanner(const char *patrn, int direct, int beg_or_end) /* Scan each character until we hit the head link record. */ - while (!boundry(curline, curoff, direct)) { + while (!boundary(curline, curoff, direct)) { /* Save the current position in case we match * the search string at this point. */ @@ -749,7 +749,7 @@ static int readpattern(char *prompt, char *apat, int srch) void savematch(void) { char *ptr; /* pointer to last match string */ - struct line *curline; /* line of last match */ + line_p curline; /* line of last match */ int curoff; /* offset " " */ /* Free any existing match string, then @@ -828,17 +828,15 @@ static int replaces(int kind, int f, int n) int nlrepl; /* was a replace done on the last line? */ char c; /* input char for query */ spat_t tpat ; /* temporary to hold search pattern */ - struct line *origline; /* original "." position */ + line_p origline; /* original "." position */ int origoff; /* and offset (for . query option) */ - struct line *lastline; /* position of last replace and */ + line_p lastline; /* position of last replace and */ int lastoff; /* offset (for 'u' query option) */ ///* rfi */ lastline = NULL ; lastoff = 0 ; -// if (curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly(); /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; /* Check for negative repetitions. @@ -1116,25 +1114,23 @@ int expandp(char *srcstr, char *deststr, int maxlength) return TRUE; } -/* - * boundry -- Return information depending on whether we may search no - * further. Beginning of file and end of file are the obvious - * cases, but we may want to add further optional boundry restrictions - * in future, a' la VMS EDT. At the moment, just return TRUE or - * FALSE depending on if a boundry is hit (ouch). - */ -int boundry(struct line *curline, int curoff, int dir) -{ - int border; - if (dir == FORWARD) { - border = (curoff == llength(curline)) && - (lforw(curline) == curbp->b_linep); - } else { - border = (curoff == 0) && - (lback(curline) == curbp->b_linep); - } - return border; +/* boundary -- Returns information depending on whether we may search no + further. Beginning of file and end of file are the obvious cases, but + we may want to add further optional boundary restrictions in future, a' + la VMS EDT. At the moment, just return TRUE or FALSE depending on if a + boundary is hit (ouch). + */ +int boundary( line_p curline, int curoff, int dir) { + int border ; + + if( dir == FORWARD) + border = (curoff == llength( curline)) && + (lforw( curline) == curbp->b_linep) ; + else + border = (curoff == 0) && (lback( curline) == curbp->b_linep) ; + + return border ; } /* @@ -1145,9 +1141,9 @@ int boundry(struct line *curline, int curoff, int dir) * the current character and move, reverse searches move and * look at the character. */ -static int nextch(struct line **pcurline, int *pcuroff, int dir) +static int nextch( line_p *pcurline, int *pcuroff, int dir) { - struct line *curline; + line_p curline; int curoff; int c; @@ -1606,3 +1602,5 @@ static void setbit(int bc, char *cclmap) *(cclmap + (bc >> 3)) |= BIT(bc & 7); } #endif + +/* end of search.c */ diff --git a/search.h b/search.h index 6f8c7a5..9b3d596 100644 --- a/search.h +++ b/search.h @@ -1,3 +1,5 @@ +/* search.h -- */ + #ifndef _SEARCH_H_ #define _SEARCH_H_ @@ -26,18 +28,20 @@ extern spat_t rpat ; /* replacement pattern */ int scanner( const char *patrn, int direct, int beg_or_end) ; -int forwsearch( int f, int n) ; -int forwhunt( int f, int n) ; -int backsearch( int f, int n) ; -int backhunt( int f, int n) ; +/* Bindable functions */ +BINDABLE( forwsearch) ; +BINDABLE( forwhunt) ; +BINDABLE( backsearch) ; +BINDABLE( backhunt) ; +BINDABLE( sreplace) ; +BINDABLE( qreplace) ; + int eq( unsigned char bc, unsigned char pc) ; void savematch( void) ; void rvstrcpy( char *rvstr, char *str) ; -int sreplace( int f, int n) ; -int qreplace( int f, int n) ; int delins( int dlength, char *instr, int use_meta) ; int expandp( char *srcstr, char *deststr, int maxlength) ; -int boundry( struct line *curline, int curoff, int dir) ; +int boundary( line_p curline, int curoff, int dir) ; void setprompt( char *tpat, unsigned tpat_size, char *prompt, char *apat) ; @@ -47,3 +51,5 @@ void rmcclear( void) ; #endif #endif + +/* end of search.h */ diff --git a/spawn.c b/spawn.c index a073159..ff6aa18 100644 --- a/spawn.c +++ b/spawn.c @@ -183,7 +183,7 @@ int execprg( int f, int n) { int pipecmd( int f, int n) { int s ; /* return status from CLI */ struct window *wp ; /* pointer to new window */ - struct buffer *bp ; /* pointer to buffer to zot */ + buffer_p bp ; /* pointer to buffer to zot */ char *mlarg ; char *line ; /* command line send to shell */ static char bname[] = "command" ; @@ -277,7 +277,7 @@ int pipecmd( int f, int n) { */ int filter_buffer( int f, int n) { int s ; /* return status from CLI */ - struct buffer *bp ; /* pointer to buffer to zot */ + buffer_p bp ; /* pointer to buffer to zot */ char *mlarg ; char *line ; /* command line send to shell */ fname_t tmpnam ; /* place to store real file name */ @@ -290,8 +290,6 @@ int filter_buffer( int f, int n) { if( restflag) return resterr() ; -// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly() ; /* we are in read only mode */ assert( !(curbp->b_mode & MDVIEW)) ; /* get the filter name and its args */ diff --git a/spawn.h b/spawn.h index db8d7c2..cc219f9 100644 --- a/spawn.h +++ b/spawn.h @@ -1,7 +1,15 @@ -int spawncli( int f, int n) ; -int bktoshell( int f, int n) ; +/* spawn.h -- various operating system access commands */ + +#include "names.h" + +/* Bindable functions */ +BINDABLE( spawncli) ; +BINDABLE( bktoshell) ; +BINDABLE( spawn) ; +BINDABLE( execprg) ; +BINDABLE( pipecmd) ; +BINDABLE( filter_buffer) ; + void rtfrmshell( void) ; -int spawn( int f, int n) ; -int execprg( int f, int n) ; -int pipecmd( int f, int n) ; -int filter_buffer( int f, int n) ; + +/* end of spawn.h */ diff --git a/word.c b/word.c index 52f2e0e..cc7ce03 100644 --- a/word.c +++ b/word.c @@ -11,7 +11,6 @@ */ #include -#include #include /* malloc, free */ #include /* memcpy */ @@ -152,9 +151,6 @@ static boolean uniflip( boolean toupper_f) { /* flip unicode case and forward */ } static boolean capcapword( int n, boolean first_f, boolean rest_f) { -// if( curbp->b_mode & MDVIEW) /* don't allow this command if */ -// return rdonly() ; /* we are in read only mode */ - assert( !(curbp->b_mode & MDVIEW)) ; if( n < 0) @@ -210,14 +206,9 @@ int capword( int f, int n) { */ int delfword(int f, int n) { - struct line *dotp; /* original cursor line */ + line_p dotp; /* original cursor line */ int doto; /* and row */ int c; /* temp char */ - long size; /* # of chars to delete */ - -// /* don't allow this command if we are in read only mode */ -// if (curbp->b_mode & MDVIEW) -// return rdonly(); assert( !(curbp->b_mode & MDVIEW)) ; @@ -235,7 +226,7 @@ int delfword(int f, int n) doto = curwp->w_doto; /* figure out how many characters to give the axe */ - size = 0; + long size = 0 ; /* get us into a word.... */ while (inword() == FALSE) { @@ -301,12 +292,6 @@ int delfword(int f, int n) */ int delbword(int f, int n) { - long size; - -// /* don't allow this command if we are in read only mode */ -// if (curbp->b_mode & MDVIEW) -// return rdonly(); - assert( !(curbp->b_mode & MDVIEW)) ; /* ignore the command if there is a nonpositive argument */ @@ -320,7 +305,8 @@ int delbword(int f, int n) if (backchar(FALSE, 1) == FALSE) return FALSE; - size = 0; + + long size = 0 ; while (n--) { while (inword() == FALSE) { if (backchar(FALSE, 1) == FALSE) @@ -352,7 +338,6 @@ static int inword( void) { return isletter( c) || ( c >= '0' && c <= '9') ; } -#if WORDPRO static int parafillnjustify( int f, int n, int justify_f) { unicode_t c; /* current char during scan */ unicode_t *wbuf ; /* buffer for current word */ @@ -361,7 +346,7 @@ static int parafillnjustify( int f, int n, int justify_f) { int clength; /* position on line during fill */ int eopflag; /* Are we at the End-Of-Paragraph? */ int firstflag = TRUE ; /* first word? (needs no space) */ - struct line *eopline; /* pointer to line just past EOP */ + line_p eopline; /* pointer to line just past EOP */ int dotflag = 0 ; /* was the last char a period? */ int leftmarg = 0 ; /* left marginal */ @@ -549,7 +534,7 @@ int killpara(int f, int n) */ int wordcount(int f, int n) { - struct line *lp; /* current line to scan */ + line_p lp; /* current line to scan */ int offset; /* current char to scan */ long size; /* size of region left to count */ int ch; /* current character to scan */ @@ -560,10 +545,10 @@ int wordcount(int f, int n) int nlines; /* total number of lines in region */ int avgch; /* average number of chars/word */ int status; /* status return code */ - struct region region; /* region to look at */ + region_t region ; /* region to look at */ /* make sure we have a region to count */ - if ((status = getregion(®ion)) != TRUE) + if( (status = getregion( ®ion)) != TRUE) return status; lp = region.r_linep; offset = region.r_offset; @@ -684,6 +669,5 @@ int gotoeop(int f, int n) curwp->w_flag |= WFMOVE; /* force screen update */ return TRUE; } -#endif /* end of word.c */ diff --git a/word.h b/word.h index e89d095..abdb507 100644 --- a/word.h +++ b/word.h @@ -1,23 +1,25 @@ +/* word.h -- word processing functions */ #ifndef _WORD_H_ #define _WORD_H_ -#define WORDPRO 1 +#include "names.h" -int wrapword( int f, int n) ; -int backword( int f, int n) ; -int forwword( int f, int n) ; -int upperword( int f, int n) ; -int lowerword( int f, int n) ; -int capword( int f, int n) ; -int delfword( int f, int n) ; -int delbword( int f, int n) ; -#if WORDPRO -int gotobop( int f, int n) ; -int gotoeop( int f, int n) ; -int fillpara( int f, int n) ; -int justpara( int f, int n) ; -int killpara( int f, int n) ; -int wordcount( int f, int n) ; -#endif +/* Bindable functions */ +BINDABLE( wrapword) ; +BINDABLE( backword) ; +BINDABLE( forwword) ; +BINDABLE( upperword) ; +BINDABLE( lowerword) ; +BINDABLE( capword) ; +BINDABLE( delfword) ; +BINDABLE( delbword) ; +BINDABLE( gotobop) ; +BINDABLE( gotoeop) ; +BINDABLE( fillpara) ; +BINDABLE( justpara) ; +BINDABLE( killpara) ; +BINDABLE( wordcount) ; #endif + +/* end of word.h */ From 7f3f498f3fd45e0bd03a408e71a7de76ada6f0f2 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Wed, 4 Aug 2021 10:54:39 +0800 Subject: [PATCH 20/37] Rewrite terminal sequence handling while getting key sequence: getcmd(). --- input.c | 170 ++++++++++++++++++++++---------------------------------- 1 file changed, 67 insertions(+), 103 deletions(-) diff --git a/input.c b/input.c index 053881d..69926c0 100644 --- a/input.c +++ b/input.c @@ -355,121 +355,85 @@ static int get1unicode( int *up) { return bytes ; } +static int keystack[ 8] ; +static int *stackptr = &keystack[ 8] ; +#define KPUSH( c) *(--stackptr) = (c) + int get1key( void) { int c ; +/* fetch from queue if any were pushed back */ + if( stackptr != &keystack[ 8]) + return *(stackptr++) ; + +/* fetch from keyboard */ get1unicode( &c) ; return c ; } -/* GETCMD: Get a command from the keyboard. Process all applicable - prefix keys */ +/* GETCMD: Get a command from the keyboard. Process all applicable prefix + keys. +*/ +int getcmd( void) { + int cmask = 0 ; + int c, d, e, f ; /* read up to ^[[24~ */ -int getcmd(void) -{ - int c; /* fetched keystroke */ -#if VT220 - int d; /* second character P.K. */ - int cmask = 0; -#endif - /* get initial character */ - c = get1key(); + for( ;;) { + c = get1key() ; + if( c == (CTRL | '[')) { + /* fetch terminal sequence */ + c = get1key() ; + if( c == 'O') { /* F1 .. F4 */ + d = get1key() ; + if( d >= 'P' && d <= 'S') { + c = d | SPEC ; + break ; + } + + KPUSH( d) ; + } else if( c == '[') { + d = get1key() ; + if( d >= 'A' && d <= 'Z') { /* Arrows, Home, End, ReverseTab */ + c = d | SPEC ; + break ; + } else if( d >= '0' && d <= '9') { + e = get1key() ; + if( e == '~') { /* Insert, Delete, PageUp, PageDown */ + c = d | SPEC ; + break ; + } else if( e >= '0' && e <= '9') { + f = get1key() ; + if( f == '~') { /* F5 .. F12 */ + c = (16 + (d - '0') * 16 + e) | SPEC ; + break ; + } -#if VT220 - proc_metac: -#endif - if (c == 128+27) /* CSI */ - goto handle_CSI; - /* process META prefix */ - if (c == (CTRL | '[')) { - c = get1key(); -#if VT220 - if (c == '[' || c == 'O') { /* CSI P.K. */ -handle_CSI: - c = get1key(); - if (c >= 'A' && c <= 'D') - return SPEC | c | cmask; - if (c >= 'E' && c <= 'z' && c != 'i' && c != 'c') - return SPEC | c | cmask; - d = get1key(); - if (d == '~') /* ESC [ n ~ P.K. */ - return SPEC | c | cmask; - switch (c) { /* ESC [ n n ~ P.K. */ - case '1': - c = d + 32; - break; - case '2': - c = d + 48; - break; - case '3': - c = d + 64; - break; - default: - c = '?'; - break; - } - if (d != '~') /* eat tilde P.K. */ - get1key(); - if (c == 'i') { /* DO key P.K. */ - c = ctlxc; - goto proc_ctlxc; - } else if (c == 'c') /* ESC key P.K. */ - c = get1key(); - else - return SPEC | c | cmask; - } -#endif -#if VT220 - if (c == (CTRL | '[')) { - cmask = META; - goto proc_metac; - } -#endif - if( islower( c)) /* Force to upper */ - c = flipcase( c) ; - else if( c >= 0x00 && c <= 0x1F) /* control key */ - c = CTRL | (c + '@'); - return META | c; - } -#if PKCODE - else if (c == metac) { - c = get1key(); -#if VT220 - if (c == (CTRL | '[')) { - cmask = META; - goto proc_metac; - } -#endif - if( islower( c)) /* Force to upper */ - c = flipcase( c) ; - else if( c >= 0x00 && c <= 0x1F) /* control key */ - c = CTRL | (c + '@'); - return META | c; - } -#endif + KPUSH( f) ; + } + + KPUSH( e) ; + } + KPUSH( d) ; + } -#if VT220 - proc_ctlxc: -#endif - /* process CTLX prefix */ - if (c == ctlxc) { - c = get1key(); -#if VT220 - if (c == (CTRL | '[')) { - cmask = CTLX; - goto proc_metac; - } -#endif - if (c >= 'a' && c <= 'z') /* Force to upper */ - c -= 0x20; - if (c >= 0x00 && c <= 0x1F) /* control key */ - c = CTRL | (c + '@'); - return CTLX | c; - } + KPUSH( c) ; -/* otherwise, just return it */ - return c ; + c = CTRL | '[' ; + } + + if( c == metac) { + cmask |= META ; + } else if( c == ctlxc) { + cmask |= CTLX ; + } else + break ; + } + + if( cmask && islower( c)) + c = flipcase( c) ; + + return c | cmask ; } /* A more generalized prompt/reply function allowing the caller From 500289770586df54486cf14d48713a5e73e0fa99 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Thu, 5 Aug 2021 12:09:21 +0800 Subject: [PATCH 21/37] Rewrite keycode command input from command line according to terminal sequence handling. --- bind.c | 151 ++++++++++++++++++++++++++++++-------------------------- input.c | 89 ++++++++++++++++++--------------- 2 files changed, 132 insertions(+), 108 deletions(-) diff --git a/bind.c b/bind.c index ff7e255..cd31305 100644 --- a/bind.c +++ b/bind.c @@ -1,9 +1,7 @@ /* bind.c -- implements bind.h */ #include "bind.h" -/* bind.c - * - * This file is for functions having to do with key bindings, +/* This file is for functions having to do with key bindings, * descriptions, help commands and startup file. * * Written 11-feb-86 by Daniel Lawrence @@ -33,9 +31,14 @@ static int buildlist( char *mstring) ; static char *cmdstr( unsigned c, char *seq) ; static unsigned int getckey( int mflag) ; static unsigned int stock( char *keyname) ; -static const char *getfname( unsigned keycode, char *failmsg) ; +static const char *getfname( unsigned keycode, const char *failmsg) ; +static boolean cmdfail( const char *msg) { + mlwrite( "%s", msg) ; + return FALSE ; +} + BINDABLE( help) { /* give me some help!!!! bring up a fake buffer and read the help file into it with view mode */ @@ -46,12 +49,10 @@ BINDABLE( help) { if( bp == curbp) return TRUE ; - if (bp == NULL) { - fname = flook( hlpfname, FALSE); - if (fname == NULL) { - mlwrite("(Help file is not online)"); - return FALSE; - } + if( bp == NULL) { + fname = flook( hlpfname, FALSE) ; + if( fname == NULL) + return cmdfail( "(Help file is not online)") ; } /* split the current window to make room for the help stuff */ @@ -73,6 +74,10 @@ BINDABLE( help) { return TRUE; } +static boolean invalidkey( void) { + return cmdfail( "(Invalid key sequence)") ; +} + /* describe the command for a certain key */ BINDABLE( deskey) { const char cmdname[] = "describe-key" ; @@ -84,6 +89,8 @@ BINDABLE( deskey) { /* get the command sequence to describe * change it to something we can print as well */ unsigned keycode = getckey( FALSE) ; + if( keycode == (unsigned) ~0) + return invalidkey() ; /* output the command sequence */ mlwrite( "%s %s: 0x%x, %s", cmdname, cmdstr( keycode, outseq), keycode, @@ -110,10 +117,8 @@ BINDABLE( bindtokey) { return FALSE ; fnp_t kfunc = nbp->n_func ; - if( kfunc == NULL) { - mlwrite( "(No such function)") ; - return FALSE ; - } + if( kfunc == NULL) + return cmdfail( "(No such function)") ; mlwrite( "bind-to-key %s: ", bind_name( nbp)) ; @@ -121,6 +126,8 @@ BINDABLE( bindtokey) { boolean prefix_f = (kfunc == metafn) || (kfunc == cex) || (kfunc == unarg) || (kfunc == ctrlg) ; int c = getckey( prefix_f) ; + if( c == ~0) + return invalidkey() ; /* change it to something we can print as well */ /* and dump it out */ @@ -134,8 +141,7 @@ BINDABLE( bindtokey) { || (c == abortc && kfunc == ctrlg)) return TRUE ; - mlwrite( "(Can't bind to active prefix)") ; - return FALSE ; + return cmdfail( "(Can't bind to active prefix)") ; } /* if the function is a prefix key */ @@ -159,10 +165,8 @@ BINDABLE( bindtokey) { } ktp = setkeybinding( c, nbp) ; - if( ktp->k_code == 0) { - mlwrite( "Binding table FULL!") ; - return FALSE ; - } + if( ktp->k_code == 0) + return cmdfail( "Binding table FULL!") ; return TRUE ; } @@ -181,16 +185,16 @@ BINDABLE( unbindkey) { /* get the command sequence to unbind */ int c = getckey( FALSE) ; /* get a command sequence */ + if( c == ~0) + return invalidkey() ; /* change it to something we can print as well */ /* and dump it out */ ostring( cmdstr( c, outseq)) ; /* prefix key sequence can't be undound, just redefined */ - if( c == reptc || c == abortc) { - mlwrite( "(Can't unbind prefix)") ; - return FALSE ; - } + if( c == reptc || c == abortc) + return cmdfail( "(Can't unbind prefix)") ; /* if it isn't bound, bitch */ if( delkeybinding( c) == FALSE) { @@ -269,10 +273,8 @@ static int buildlist( char *mstring) { /* and get a buffer for it */ bp = bfind("*Binding list*", TRUE, 0); - if (bp == NULL || bclear(bp) == FALSE) { - mlwrite("Can not display binding list"); - return FALSE; - } + if( bp == NULL || bclear( bp) == FALSE) + return cmdfail( "Can't display binding list") ; /* let us know this is in progress */ mlwrite("(Building binding list)"); @@ -352,17 +354,16 @@ static int buildlist( char *mstring) { * get a command key sequence from the keyboard * * int mflag; going for a meta sequence? + * returns ~0 on failure */ static unsigned int getckey( int mflag) { unsigned int c ; /* character fetched */ /* check to see if we are executing a command line */ if( clexec) { - char *tok ; /* command incoming */ - - tok = getnewtokval() ; /* get the next token */ + char *tok = getnewtokval() ; /* get the next token */ if( tok == NULL) - c = 0 ; /* return dummy key on failure */ + c = ~0 ; /* return invalid key on failure */ else { c = stock( tok) ; free( tok) ; @@ -416,23 +417,23 @@ static char *cmdstr( unsigned c, char *seq) { *ptr++ = ctlxc & ~PRFXMASK ; } +/* apply control sequence if needed */ + if( c & CTRL) + *ptr++ = '^' ; + /* apply SPEC sequence if needed */ if( c & SPEC) { *ptr++ = 'F' ; *ptr++ = 'N' ; } -/* apply control sequence if needed */ - if( c & CTRL) - *ptr++ = '^' ; - /* and output the final sequence */ ptr += unicode_to_utf8( c & ~PRFXMASK, ptr) ; *ptr = 0 ; /* terminate the string */ return seq ; } -static const char *getfname( unsigned keycode, char *failmsg) { +static const char *getfname( unsigned keycode, const char *failmsg) { /* takes a key code and gets the name of the function bound to it */ kbind_p kbp = getkeybinding( keycode) ; if( kbp->k_code == 0) @@ -443,54 +444,58 @@ static const char *getfname( unsigned keycode, char *failmsg) { return found ; } -/* - * stock: +/* stock: * String key name TO Command Key * * char *keyname; name of key to translate to Command key form + * fmt: [M-][^X][^][FN]X + * returns ~0 on invalid sequence */ static unsigned int stock( char *keyname) { - unsigned int c; /* key sequence to return */ +/* parse it up */ + unsigned c = 0 ; - /* parse it up */ - c = 0; - - /* first, the META prefix */ - if (*keyname == 'M' && *(keyname + 1) == '-') { - c = META; - keyname += 2; +/* first, the META prefix */ + if( *keyname == 'M' && keyname[ 1] == '-') { + c = META ; + keyname += 2 ; } - /* next the function prefix */ - if (*keyname == 'F' && *(keyname + 1) == 'N') { - c |= SPEC; - keyname += 2; +/* control-x prefix */ + if( *keyname == '^' && keyname[ 1] == 'X') { + c |= CTLX ; + keyname += 2 ; } - /* control-x as well... (but not with FN) */ - if (*keyname == '^' && *(keyname + 1) == 'X' && !(c & SPEC)) { - c |= CTLX; - keyname += 2; +/* a control char? */ + if( *keyname == '^' && keyname[ 1] != 0) { + c |= CTRL ; + ++keyname ; } - /* a control char? */ - if (*keyname == '^' && *(keyname + 1) != 0) { - c |= CTRL; - ++keyname; - } - if (*keyname < 32) { - c |= CTRL; - *keyname += 'A'; +/* next the function prefix */ + if( *keyname == 'F' && keyname[ 1] == 'N') { + c |= SPEC ; + keyname += 2 ; } +/* only one character left to parse */ + if( !*keyname || keyname[1]) + return ~0 ; - /* make sure we are not lower case (not with function keys) */ - if (*keyname >= 'a' && *keyname <= 'z' && !(c & SPEC)) - *keyname -= 32; +/* only way to redefine ^X is by quoting binary value */ + if( *keyname < 32 || *keyname == 0x7F) { + c |= CTRL ; + *keyname ^= 0x40 ; + } - /* the final sequence... */ +/* make sure we are not lower case (not with function keys) */ + if( *keyname >= 'a' && *keyname <= 'z' && !(c & SPEC)) + *keyname -= 32 ; + +/* the final sequence... */ c |= *keyname & 0xFFU ; - return c; + return c ; } /* @@ -499,5 +504,13 @@ static unsigned int stock( char *keyname) { * char *skey; name of key to get binding for */ const char *transbind( char *skey) { - return getfname( stock( skey), "ERROR") ; + static const char failmsg[] = "ERROR" ; + + unsigned c = stock( skey) ; + if( c == (unsigned) ~0) + return failmsg ; + else + return getfname( c, failmsg) ; } + +/* end of bind.c */ diff --git a/input.c b/input.c index 69926c0..d324251 100644 --- a/input.c +++ b/input.c @@ -1,10 +1,7 @@ /* input.c -- implements input.h */ - #include "input.h" -/* input.c - * - * Various input routines +/* Various input routines * * written by Daniel Lawrence 5/9/86 * modified by Petri Kutvonen @@ -355,15 +352,17 @@ static int get1unicode( int *up) { return bytes ; } -static int keystack[ 8] ; -static int *stackptr = &keystack[ 8] ; +/* Terminal sequences need up to 6 read ahead characters */ +#define STACKSIZE 6 +static int keystack[ STACKSIZE] ; +static int *stackptr = &keystack[ STACKSIZE] ; #define KPUSH( c) *(--stackptr) = (c) int get1key( void) { int c ; /* fetch from queue if any were pushed back */ - if( stackptr != &keystack[ 8]) + if( stackptr != &keystack[ STACKSIZE]) return *(stackptr++) ; /* fetch from keyboard */ @@ -372,52 +371,62 @@ int get1key( void) { } /* GETCMD: Get a command from the keyboard. Process all applicable prefix - keys. + keys. Handle alted and controlled FNx, not shifted. Allow double + prefix M-^X. */ int getcmd( void) { - int cmask = 0 ; - int c, d, e, f ; /* read up to ^[[24~ */ + int cmask = 0 ; /* prefixes M- & ^X */ + int keyread[ STACKSIZE] ; /* room to process sequences like ^[[24;2~ */ + int *kptr = keyread ; + int c ; for( ;;) { c = get1key() ; if( c == (CTRL | '[')) { /* fetch terminal sequence */ - c = get1key() ; + c = *(kptr++) = get1key() ; if( c == 'O') { /* F1 .. F4 */ - d = get1key() ; - if( d >= 'P' && d <= 'S') { - c = d | SPEC ; - break ; - } - - KPUSH( d) ; + c = *(kptr++) = get1key() ; + if( c >= 'P' && c <= 'S') + return c | SPEC | cmask ; } else if( c == '[') { - d = get1key() ; - if( d >= 'A' && d <= 'Z') { /* Arrows, Home, End, ReverseTab */ - c = d | SPEC ; - break ; - } else if( d >= '0' && d <= '9') { - e = get1key() ; - if( e == '~') { /* Insert, Delete, PageUp, PageDown */ - c = d | SPEC ; - break ; - } else if( e >= '0' && e <= '9') { - f = get1key() ; - if( f == '~') { /* F5 .. F12 */ - c = (16 + (d - '0') * 16 + e) | SPEC ; - break ; + int v = 0 ; /* ^[[v1;v~ or ^[[v~ */ + int v1 = 0 ; + while( kptr < &keyread[ STACKSIZE]) { + c = *(kptr++) = get1key() ; + if( (c == '~') || (c >= 'A' && c <= 'Z')) { + /* Found end of sequence */ + if( v1) { /* Handle ALT/CTL, not SHFT */ + if( (v - 1) & 4) + cmask |= CTRL ; + + if( (v - 1) & 2) + cmask |= META ; + + v = v1 ; } - KPUSH( f) ; - } - - KPUSH( e) ; - } + if( c == '~') { + if( v) + c = v + ((v <= 9) ? '0' : 'a' - 10) ; + else + break ; + } - KPUSH( d) ; + return c | SPEC | cmask ; + } else if( c == ';') { /* Start of SHFT/ALT/CTL state */ + v1 = v ; + v = 0 ; + } else if( c >= '0' && c <= '9') + v = v * 10 + c - '0' ; + else + break ; + } } - KPUSH( c) ; + /* not a match, unget the keys read so far */ + while( kptr > keyread) + KPUSH( *(--kptr)) ; c = CTRL | '[' ; } @@ -681,3 +690,5 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) return retval ; } + +/* end of input.c */ From 4b45ca231ee8ce86fbd93e6ba73d9fd6bf4e0076 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Fri, 6 Aug 2021 15:21:00 +0800 Subject: [PATCH 22/37] Fix regression: CSI character instead of ^{[ in terminal special key sequence. Remove terminal special key conditional compilation. --- estruct.h | 31 ++++++------------------------- input.c | 18 +++++++++++------- names.c | 3 +-- 3 files changed, 18 insertions(+), 34 deletions(-) diff --git a/estruct.h b/estruct.h index 22d1e7b..a5c2ae5 100644 --- a/estruct.h +++ b/estruct.h @@ -3,13 +3,11 @@ #ifndef _ESTRUCT_H_ #define _ESTRUCT_H_ -/* ESTRUCT.H - * - * Structure and preprocessor defines +/* Structure and preprocessor defines * * written by Dave G. Conroy * modified by Steve Wilhite, George Jones - * substantially modified by Daniel Lawrence + * substantially modified by Daniel Lawrence * modified by Petri Kutvonen */ @@ -66,29 +64,12 @@ #endif #ifndef AUTOCONF - -/* Special keyboard definitions */ - -#define VT220 0 /* Use keypad escapes P.K. */ -#define VT100 0 /* Handle VT100 style keypad. */ - /* Terminal Output definitions */ - -#define ANSI 0 /* ANSI escape sequences */ -#define VT52 0 /* VT52 terminal (Zenith). */ -#define TERMCAP 0 /* Use TERMCAP */ -#define IBMPC 1 /* IBM-PC CGA/MONO/EGA driver */ - +# define TERMCAP 0 /* Use TERMCAP */ +# define IBMPC 1 /* IBM-PC CGA/MONO/EGA driver */ #else - -#define VT220 UNIX -#define VT100 0 - -#define ANSI 0 -#define VT52 0 -#define TERMCAP UNIX -#define IBMPC MSDOS - +# define TERMCAP UNIX +# define IBMPC MSDOS #endif /* Autoconf. */ /* Configuration options */ diff --git a/input.c b/input.c index d324251..ae0b958 100644 --- a/input.c +++ b/input.c @@ -352,8 +352,8 @@ static int get1unicode( int *up) { return bytes ; } -/* Terminal sequences need up to 6 read ahead characters */ -#define STACKSIZE 6 +/* Terminal sequences need up to 7 read ahead characters */ +#define STACKSIZE 7 static int keystack[ STACKSIZE] ; static int *stackptr = &keystack[ STACKSIZE] ; #define KPUSH( c) *(--stackptr) = (c) @@ -381,8 +381,10 @@ int getcmd( void) { int c ; for( ;;) { - c = get1key() ; - if( c == (CTRL | '[')) { + c = *(kptr++) = get1key() ; + if( c == 0x9B) + goto foundCSI ; + else if( c == (CTRL | '[')) { /* fetch terminal sequence */ c = *(kptr++) = get1key() ; if( c == 'O') { /* F1 .. F4 */ @@ -390,8 +392,10 @@ int getcmd( void) { if( c >= 'P' && c <= 'S') return c | SPEC | cmask ; } else if( c == '[') { - int v = 0 ; /* ^[[v1;v~ or ^[[v~ */ - int v1 = 0 ; + int v1, v ; /* ^[[v1;v~ or ^[[v~ */ + + foundCSI: + v1 = v = 0 ; while( kptr < &keyread[ STACKSIZE]) { c = *(kptr++) = get1key() ; if( (c == '~') || (c >= 'A' && c <= 'Z')) { @@ -428,7 +432,7 @@ int getcmd( void) { while( kptr > keyread) KPUSH( *(--kptr)) ; - c = CTRL | '[' ; + c = get1key() ; } if( c == metac) { diff --git a/names.c b/names.c index f357a99..ae7c45a 100644 --- a/names.c +++ b/names.c @@ -232,7 +232,7 @@ const name_bind names[] = { { NULL, (fnp_t) setmark, META | '.'}, // { NULL, bktoshell, META | 'S'}, -#if VT220 +/* special key mapping */ { NULL, yank, SPEC | '2'}, /* Insert */ { NULL, forwdel /* killregion */, SPEC | '3'}, /* Delete */ { NULL, (fnp_t) backpage, SPEC | '5'}, /* Page Up */ @@ -244,7 +244,6 @@ const name_bind names[] = { { NULL, (fnp_t) gotoeob, SPEC | 'F'}, /* End */ { NULL, (fnp_t) gotobob, SPEC | 'H'}, /* Home */ { NULL, help, SPEC | 'P'}, /* F1 */ -#endif /* hooks */ { NULL, nullproc, SPEC | META | 'R'}, /* hook */ From 3551d2b8d1edbb531fb83f658af35fe402b4e67d Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 7 Aug 2021 10:28:20 +0800 Subject: [PATCH 23/37] Simplify Makefile by removing obsolete rules (lint, splint, sparse, tags). --- .gitignore | 4 +++- Makefile | 42 ++++-------------------------------------- 2 files changed, 7 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 844faff..b9f99e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -em +depend.mak +ue *.o +*.exe diff --git a/Makefile b/Makefile index 097b7a8..14ee038 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -# Makefile -- uEMACS -# Copyright (c) 2014-2021 Renaud Fivet +# Makefile -- µEMACS +# Copyright © 2013-2021 Renaud Fivet # Make the build silent by default V = @@ -25,7 +25,7 @@ WARNINGS=-pedantic -Wall -Wextra -Wstrict-prototypes -Wno-unused-parameter CFLAGS=-O2 $(WARNINGS) LDFLAGS=-s LIBS=-lcurses -DEFINES=-DAUTOCONF -DPROGRAM=$(PROGRAM) +DEFINES=-DAUTOCONF -DPROGRAM=$(PROGRAM) # -DNDEBUG ifeq ($(uname_S),Linux) DEFINES += -DPOSIX -DUSG else ifeq ($(uname_S),CYGWIN) @@ -38,18 +38,6 @@ else $(error $(uname_S) needs configuration) endif -#ifeq ($(uname_S),FreeBSD) -# DEFINES=-DAUTOCONF -DPOSIX -DSYSV -D_FREEBSD_C_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_XOPEN_SOURCE=600 -#endif -#ifeq ($(uname_S),Darwin) -# DEFINES=-DAUTOCONF -DPOSIX -DSYSV -D_DARWIN_C_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_XOPEN_SOURCE=600 -#endif - -#LIBS=-ltermcap # BSD -#LIBS=-lcurses # SYSV -#LIBS=-ltermlib -#LIBS=-L/usr/lib/termcap -ltermcap -LFLAGS=-hbx BINDIR=/usr/bin LIBDIR=/usr/lib @@ -60,15 +48,9 @@ $(PROGRAM): $(OBJS) $(E) " LINK " $@ $(Q) $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) -SPARSE=sparse -SPARSE_FLAGS=-D__LITTLE_ENDIAN__ -D__x86_64__ -D__linux__ -D__unix__ - -sparse: - $(SPARSE) $(SPARSE_FLAGS) $(DEFINES) $(SRCS) - clean: $(E) " CLEAN" - $(Q) rm -f $(PROGRAM) core lintout makeout tags *.o + $(Q) rm -f $(PROGRAM) depend.mak *.o install: $(PROGRAM) strip $(PROGRAM) @@ -78,22 +60,6 @@ install: $(PROGRAM) chmod 755 ${BINDIR}/$(PROGRAM) chmod 644 ${LIBDIR}/emacs.hlp ${LIBDIR}/.emacsrc -lint: ${SRC} - @rm -f lintout - lint ${LFLAGS} ${SRC} >lintout - cat lintout - -splint: - splint -weak $(DEFINES) $(SRCS) -booltype boolean -booltrue TRUE -boolfalse FALSE +posixlib +matchanyintegral - -errs: - @rm -f makeout - make $(PROGRAM) >makeout 2>&1 - -tags: ${SRC} - @rm -f tags - ctags ${SRC} - .c.o: $(E) " CC " $@ $(Q) ${CC} ${CFLAGS} ${DEFINES} -c $*.c From c55a72ad26088162d55a339f03b09658f3c3359b Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 7 Aug 2021 10:29:23 +0800 Subject: [PATCH 24/37] Use explicit buffer name as output for the script. --- maze.cmd | 4 ++++ screensize.cmd | 1 + 2 files changed, 5 insertions(+) diff --git a/maze.cmd b/maze.cmd index fcf221e..18b49d7 100644 --- a/maze.cmd +++ b/maze.cmd @@ -9,6 +9,8 @@ set %D3 0 set %D4 -1 set %D5 0 +select-buffer maze + # draw the maze layout $curwidth insert-string " " newline @@ -98,3 +100,5 @@ write-message $line set $curline 3 set $curcol 1 set $curchar 32 + +unmark-buffer diff --git a/screensize.cmd b/screensize.cmd index 0bef7f3..47799eb 100644 --- a/screensize.cmd +++ b/screensize.cmd @@ -1,4 +1,5 @@ # Visualize Screen Dimensions +select-buffer screensize insert-string &cat $curwidth &cat "x" $pagelen insert-string &rig "---------+" &sub 10 $curcol &sub &div $curwidth 10 1 insert-string "---------+" From 1cdc889f6ff2fa8a6a895a86df10a5742ddf07c9 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 7 Aug 2021 13:46:14 +0800 Subject: [PATCH 25/37] Fix regression on key command input for M-^X and ^X^X. --- bind.c | 18 +++++++----------- input.c | 36 +++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/bind.c b/bind.c index cd31305..3406fe8 100644 --- a/bind.c +++ b/bind.c @@ -448,22 +448,19 @@ static const char *getfname( unsigned keycode, const char *failmsg) { * String key name TO Command Key * * char *keyname; name of key to translate to Command key form - * fmt: [M-][^X][^][FN]X + * fmt: [M-|^X][^][FN]X * returns ~0 on invalid sequence */ static unsigned int stock( char *keyname) { /* parse it up */ unsigned c = 0 ; -/* first, the META prefix */ +/* first, the prefix META or ^X */ if( *keyname == 'M' && keyname[ 1] == '-') { c = META ; keyname += 2 ; - } - -/* control-x prefix */ - if( *keyname == '^' && keyname[ 1] == 'X') { - c |= CTLX ; + } else if( *keyname == '^' && keyname[ 1] == 'X') { + c = CTLX ; keyname += 2 ; } @@ -487,10 +484,9 @@ static unsigned int stock( char *keyname) { if( *keyname < 32 || *keyname == 0x7F) { c |= CTRL ; *keyname ^= 0x40 ; - } - -/* make sure we are not lower case (not with function keys) */ - if( *keyname >= 'a' && *keyname <= 'z' && !(c & SPEC)) + } else if( c && !(c & SPEC) + && *keyname >= 'a' && *keyname <= 'z') + /* make sure we are not lower case (not with function keys) */ *keyname -= 32 ; /* the final sequence... */ diff --git a/input.c b/input.c index ae0b958..5ce7d44 100644 --- a/input.c +++ b/input.c @@ -371,11 +371,10 @@ int get1key( void) { } /* GETCMD: Get a command from the keyboard. Process all applicable prefix - keys. Handle alted and controlled FNx, not shifted. Allow double - prefix M-^X. + keys. Handle alted and controlled FNx, not shifted. */ int getcmd( void) { - int cmask = 0 ; /* prefixes M- & ^X */ + int prefix = 0 ; /* prefixes M- or ^X */ int keyread[ STACKSIZE] ; /* room to process sequences like ^[[24;2~ */ int *kptr = keyread ; int c ; @@ -390,7 +389,7 @@ int getcmd( void) { if( c == 'O') { /* F1 .. F4 */ c = *(kptr++) = get1key() ; if( c >= 'P' && c <= 'S') - return c | SPEC | cmask ; + return c | SPEC | prefix ; } else if( c == '[') { int v1, v ; /* ^[[v1;v~ or ^[[v~ */ @@ -398,14 +397,17 @@ int getcmd( void) { v1 = v = 0 ; while( kptr < &keyread[ STACKSIZE]) { c = *(kptr++) = get1key() ; - if( (c == '~') || (c >= 'A' && c <= 'Z')) { + if( (c == '~') + || (c >= 'A' && c <= 'Z') + || (c >= 'a' && c <= 'z')) { /* Found end of sequence */ + int mask = prefix ; if( v1) { /* Handle ALT/CTL, not SHFT */ - if( (v - 1) & 4) - cmask |= CTRL ; - if( (v - 1) & 2) - cmask |= META ; + mask = META ; + + if( (v - 1) & 4) + mask |= CTRL ; v = v1 ; } @@ -417,7 +419,7 @@ int getcmd( void) { break ; } - return c | SPEC | cmask ; + return c | SPEC | mask ; } else if( c == ';') { /* Start of SHFT/ALT/CTL state */ v1 = v ; v = 0 ; @@ -433,20 +435,24 @@ int getcmd( void) { KPUSH( *(--kptr)) ; c = get1key() ; - } + } else + kptr-- ; if( c == metac) { - cmask |= META ; + prefix = META ; } else if( c == ctlxc) { - cmask |= CTLX ; + if( prefix) + break ; /* ^X^X or M-^X */ + else + prefix = CTLX ; } else break ; } - if( cmask && islower( c)) + if( prefix && islower( c)) c = flipcase( c) ; - return c | cmask ; + return c | prefix ; } /* A more generalized prompt/reply function allowing the caller From 1fbb2fc565c8eb46ba164d2846ecc1c3327f042d Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 7 Aug 2021 21:34:13 +0800 Subject: [PATCH 26/37] Merge common implementation of case-region-lower and case-region-upper. --- region.c | 195 ++++++++++++++++++++++++++----------------------------- 1 file changed, 92 insertions(+), 103 deletions(-) diff --git a/region.c b/region.c index 00944c5..7504416 100644 --- a/region.c +++ b/region.c @@ -1,13 +1,11 @@ /* region.c -- implements region.h */ #include "region.h" -/* region.c - * - * The routines in this file deal with the region, that magic space - * between "." and mark. Some functions are commands. Some functions are - * just for internal use. - * - * Modified by Petri Kutvonen +/* The routines in this file deal with the region, that magic space between + "." and mark. Some functions are commands. Some functions are just for + internal use. + + Modified by Petri Kutvonen */ #include @@ -20,123 +18,114 @@ #include "random.h" #include "window.h" -/* Kill the region. Ask "getregion" to figure out the bounds of the - * region. Move "." to the start, and kill the characters. Bound to - * "C-W". +/* Kill the region. Ask getregion() to figure out the bounds of the + region. Move "." to the start, and kill the characters. Bound to C-W + kill-region. */ BINDABLE( killregion) { - int s; - region_t region; + region_t region ; assert( !(curbp->b_mode & MDVIEW)) ; - - if ((s = getregion(®ion)) != TRUE) - return s; - if ((lastflag & CFKILL) == 0) /* This is a kill type */ - kdelete(); /* command, so do magic */ - thisflag |= CFKILL; /* kill buffer stuff. */ - curwp->w_dotp = region.r_linep; - curwp->w_doto = region.r_offset; - return ldelete(region.r_size, TRUE); + + int ret = getregion( ®ion) ; + if( ret != TRUE) + return ret ; + + if( (lastflag & CFKILL) == 0) /* This is a kill type */ + kdelete() ; /* command, so do magic */ + + thisflag |= CFKILL ; /* kill buffer stuff. */ + curwp->w_dotp = region.r_linep ; + curwp->w_doto = region.r_offset ; + return ldelete( region.r_size, TRUE) ; } /* Copy all of the characters in the region to the kill buffer. Don't move - * dot at all. This is a bit like a kill region followed by a yank. Bound - * to "M-W". + dot at all. This is a bit like a kill region followed by a yank. Bound + to M-W copy-region. */ BINDABLE( copyregion) { - line_p linep; - int loffs; - int s; - region_t region; + region_t region ; - if ((s = getregion(®ion)) != TRUE) - return s; - if ((lastflag & CFKILL) == 0) /* Kill type command. */ - kdelete(); - thisflag |= CFKILL; - linep = region.r_linep; /* Current line. */ - loffs = region.r_offset; /* Current offset. */ - while (region.r_size--) { - if (loffs == llength(linep)) { /* End of line. */ - if ((s = kinsert('\n')) != TRUE) - return s; - linep = lforw(linep); - loffs = 0; + int ret = getregion( ®ion) ; + if( ret != TRUE) + return ret ; + + if( (lastflag & CFKILL) == 0) /* Kill type command. */ + kdelete() ; + + thisflag |= CFKILL ; + line_p linep = region.r_linep ; /* Current line. */ + int loffs = region.r_offset ; /* Current offset. */ + while( region.r_size--) { + if( loffs == llength( linep)) { /* End of line. */ + ret = kinsert( '\n') ; + if( ret != TRUE) + return ret ; + + linep = lforw( linep) ; + loffs = 0 ; } else { /* Middle of line. */ - if ((s = kinsert(lgetc(linep, loffs))) != TRUE) - return s; - ++loffs; + ret = kinsert( lgetc( linep, loffs)) ; + if( ret != TRUE) + return ret ; + + ++loffs ; } } + mloutstr( "(region copied)") ; - return TRUE; + return TRUE ; } -/* Lower case region. Zap all of the upper case characters in the region - * to lower case. Use the region code to set the limits. Scan the buffer, - * doing the changes. Call "lchange" to ensure that redisplay is done in - * all buffers. Bound to "C-X C-L". +/* Lower case region & Upper case region. Zap all of the upper/lower case + characters in the region to lower/upper case. Use the region code to + set the limits. Scan the buffer, doing the changes. Call "lchange" to + ensure that redisplay is done in all buffers. Bound to C-X C-L + case-region-lower and C-X C-U case-region-upper. */ +static int utol( int c) { + return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : -1 ; +} + +static int ltou( int c) { + return (c >= 'a' && c <= 'z') ? c + 'A' - 'a' : -1 ; +} + +static int caseregion( int (* cconv)( int)) { + region_t region ; + + assert( !(curbp->b_mode & MDVIEW)) ; + + int ret = getregion( ®ion) ; + if( ret != TRUE) + return ret ; + + lchange( WFHARD) ; + line_p linep = region.r_linep ; + int loffs = region.r_offset ; + while( region.r_size--) { + if( loffs == llength( linep)) { + linep = lforw( linep) ; + loffs = 0 ; + } else { + int c = cconv( lgetc( linep, loffs)) ; + if( c != -1) + lputc( linep, loffs, c) ; + + ++loffs ; + } + } + + return TRUE ; +} + BINDABLE( lowerregion) { - line_p linep; - int loffs; - int c; - int s; - region_t region; - - assert( !(curbp->b_mode & MDVIEW)) ; - - if ((s = getregion(®ion)) != TRUE) - return s; - lchange(WFHARD); - linep = region.r_linep; - loffs = region.r_offset; - while (region.r_size--) { - if (loffs == llength(linep)) { - linep = lforw(linep); - loffs = 0; - } else { - c = lgetc(linep, loffs); - if (c >= 'A' && c <= 'Z') - lputc(linep, loffs, c + 'a' - 'A'); - ++loffs; - } - } - return TRUE; + return caseregion( utol) ; } -/* Upper case region. Zap all of the lower case characters in the region - * to upper case. Use the region code to set the limits. Scan the buffer, - * doing the changes. Call "lchange" to ensure that redisplay is done in - * all buffers. Bound to "C-X C-L". - */ BINDABLE( upperregion) { - line_p linep; - int loffs; - int c; - int s; - region_t region; - - assert( !(curbp->b_mode & MDVIEW)) ; - - if ((s = getregion(®ion)) != TRUE) - return s; - lchange(WFHARD); - linep = region.r_linep; - loffs = region.r_offset; - while (region.r_size--) { - if (loffs == llength(linep)) { - linep = lforw(linep); - loffs = 0; - } else { - c = lgetc(linep, loffs); - if (c >= 'a' && c <= 'z') - lputc(linep, loffs, c - 'a' + 'A'); - ++loffs; - } - } - return TRUE; + return caseregion( ltou) ; } /* This routine figures out the bounds of the region in the current window, From 5c65613f03eb3b4f85416ea01d005001dc9eb302 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 7 Aug 2021 21:46:58 +0800 Subject: [PATCH 27/37] Replace compilation check by estruct.h header inclusion. --- lock.c | 2 -- lock.h | 8 +++++--- pklock.c | 1 - pklock.h | 8 +++++--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lock.c b/lock.c index 4c0f40e..df7f034 100644 --- a/lock.c +++ b/lock.c @@ -1,6 +1,4 @@ /* lock.c -- implements lock.h */ - -#include "estruct.h" #include "lock.h" /* LOCK.C diff --git a/lock.h b/lock.h index aa04d75..f7907c0 100644 --- a/lock.h +++ b/lock.h @@ -1,9 +1,9 @@ +/* lock.h -- */ + #ifndef _LOCK_H_ #define _LOCK_H_ -#ifndef _ESTRUCT_H_ -#error uEmacs compilation settings needs to be done! -#endif +#include "estruct.h" #if BSD | SVR4 @@ -14,3 +14,5 @@ int unlock( const char *fname) ; #endif #endif + +/* end of lock.h */ diff --git a/pklock.c b/pklock.c index 6f98f57..3354946 100644 --- a/pklock.c +++ b/pklock.c @@ -1,5 +1,4 @@ /* pklock.c -- implements pklock.h */ -#include "estruct.h" #include "pklock.h" /* PKLOCK.C diff --git a/pklock.h b/pklock.h index 7cba16c..81955ee 100644 --- a/pklock.h +++ b/pklock.h @@ -1,9 +1,9 @@ +/* pklock.h -- */ + #ifndef _PKLOCK_H_ #define _PKLOCK_H_ -#ifndef _ESTRUCT_H_ -#error uEmacs compilation settings needs to be done! -#endif +#include "estruct.h" #if (FILOCK && BSD) || SVR4 @@ -13,3 +13,5 @@ char *undolock( const char *fname) ; #endif #endif + +/* end of pklock.h */ From 893e34b74063e7cecdabf6ef39e5dfeeb678d785 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 7 Aug 2021 21:54:09 +0800 Subject: [PATCH 28/37] Remove obsolete terminal implementations. --- fridge/ansi.c | 241 ----------------------- fridge/ibmpc.c | 494 ----------------------------------------------- fridge/mingw32.c | 197 ------------------- fridge/vmsvt.c | 488 ---------------------------------------------- fridge/vt52.c | 161 --------------- fridge/wscreen.c | 117 ----------- fridge/wscreen.h | 14 -- 7 files changed, 1712 deletions(-) delete mode 100644 fridge/ansi.c delete mode 100644 fridge/ibmpc.c delete mode 100644 fridge/mingw32.c delete mode 100644 fridge/vmsvt.c delete mode 100644 fridge/vt52.c delete mode 100644 fridge/wscreen.c delete mode 100644 fridge/wscreen.h diff --git a/fridge/ansi.c b/fridge/ansi.c deleted file mode 100644 index 425d96c..0000000 --- a/fridge/ansi.c +++ /dev/null @@ -1,241 +0,0 @@ -/* ANSI.C - * - * The routines in this file provide support for ANSI style terminals - * over a serial line. The serial I/O services are provided by routines in - * "termio.c". It compiles into nothing if not an ANSI device. - * - * modified by Petri Kutvonen - */ - -#define termdef 1 /* don't define "term" external */ - -#include -#include "estruct.h" - - -#if ANSI - -#define NROW 25 /* Screen size. */ -#define NCOL 80 /* Edit if you want to. */ - -#if PKCODE -#define MROW 64 -#endif -#define NPAUSE 100 /* # times thru update to pause */ -#define MARGIN 8 /* size of minimim margin and */ -#define SCRSIZ 64 /* scroll size for extended lines */ -#define BEL 0x07 /* BEL character. */ -#define ESC 0x1B /* ESC character. */ - -extern int ttopen(); /* Forward references. */ -extern int ttgetc(); -extern int ttputc(); -extern int ttflush(); -extern int ttclose(); -extern int ansimove(); -extern int ansieeol(); -extern int ansieeop(); -extern int ansibeep(); -extern int ansiopen(); -extern int ansirev(); -extern int ansiclose(); -extern int ansikopen(); -extern int ansikclose(); -extern int ansicres(); - -#if COLOR -extern int ansifcol(); -extern int ansibcol(); - -int cfcolor = -1; /* current forground color */ -int cbcolor = -1; /* current background color */ - -#endif - -/* - * Standard terminal interface dispatch table. Most of the fields point into - * "termio" code. - */ -struct terminal term = { -#if PKCODE - MROW - 1, -#else - NROW - 1, -#endif - NROW - 1, - NCOL, - NCOL, - MARGIN, - SCRSIZ, - NPAUSE, - ansiopen, - ansiclose, - ansikopen, - ansikclose, - ttgetc, - ttputc, - ttflush, - ansimove, - ansieeol, - ansieeop, - ansibeep, - ansirev, - ansicres -#if COLOR - , ansifcol, - ansibcol -#endif -#if SCROLLCODE - , NULL -#endif -}; - -#if COLOR -ansifcol(color) - /* set the current output color */ -int color; /* color to set */ - -{ - if (color == cfcolor) - return; - ttputc(ESC); - ttputc('['); - ansiparm(color + 30); - ttputc('m'); - cfcolor = color; -} - -/* Set the current background color. - * color: color to set. - */ -void ansibcol(int color) -{ - if (color == cbcolor) - return; - ttputc(ESC); - ttputc('['); - ansiparm(color + 40); - ttputc('m'); - cbcolor = color; -} -#endif - -ansimove(row, col) -{ - ttputc(ESC); - ttputc('['); - ansiparm(row + 1); - ttputc(';'); - ansiparm(col + 1); - ttputc('H'); -} - -void ansieeol(void) -{ - ttputc(ESC); - ttputc('['); - ttputc('K'); -} - -void ansieeop(void) -{ -#if COLOR - ansifcol(gfcolor); - ansibcol(gbcolor); -#endif - ttputc(ESC); - ttputc('['); - ttputc('J'); -} - -/* Change reverse video state. - * state: TRUE = reverse, FALSE = normal - */ -void ansirev(int state) -{ -#if COLOR - int ftmp, btmp; /* temporaries for colors */ -#endif - - ttputc(ESC); - ttputc('['); - ttputc(state ? '7' : '0'); - ttputc('m'); -#if COLOR - if (state == FALSE) { - ftmp = cfcolor; - btmp = cbcolor; - cfcolor = -1; - cbcolor = -1; - ansifcol(ftmp); - ansibcol(btmp); - } -#endif -} - -/* Change screen resolution. */ -int ansicres() -{ - return TRUE; -} - -void ansibeep(void) -{ - ttputc(BEL); - ttflush(); -} - -void ansiparm(int n) -{ - int q, r; - - q = n / 10; - if (q != 0) { - r = q / 10; - if (r != 0) { - ttputc((r % 10) + '0'); - } - ttputc((q % 10) + '0'); - } - ttputc((n % 10) + '0'); -} - -void ansiopen(void) -{ -#if V7 | USG | BSD - char *cp; - - if ((cp = getenv("TERM")) == NULL) { - puts("Shell variable TERM not defined!"); - exit(1); - } - if (strcmp(cp, "vt100") != 0) { - puts("Terminal type not 'vt100'!"); - exit(1); - } -#endif - strcpy(sres, "NORMAL"); - revexist = TRUE; - ttopen(); -} - -void ansiclose(void) -{ -#if COLOR - ansifcol(7); - ansibcol(0); -#endif - ttclose(); -} - -/* Open the keyboard (a noop here). */ -void ansikopen(void) -{ -} - -/* Close the keyboard (a noop here). */ -void ansikclose(void) -{ -} - -#endif diff --git a/fridge/ibmpc.c b/fridge/ibmpc.c deleted file mode 100644 index 7cdd3bc..0000000 --- a/fridge/ibmpc.c +++ /dev/null @@ -1,494 +0,0 @@ -/* ibmpc.c - * - * The routines in this file provide support for the IBM-PC and other - * compatible terminals. It goes directly to the graphics RAM to do - * screen output. It compiles into nothing if not an IBM-PC driver - * Supported monitor cards include CGA, MONO and EGA. - * - * modified by Petri Kutvonen - */ - -#define termdef 1 /* don't define "term" external */ - -#include -#include "estruct.h" - -#if IBMPC -#if PKCODE -#define NROW 50 -#else -#define NROW 43 /* Max Screen size. */ -#endif -#define NCOL 80 /* Edit if you want to. */ -#define MARGIN 8 /* size of minimim margin and */ -#define SCRSIZ 64 /* scroll size for extended lines */ -#define NPAUSE 200 /* # times thru update to pause */ -#define BEL 0x07 /* BEL character. */ -#define ESC 0x1B /* ESC character. */ -#define SPACE 32 /* space character */ - -#define SCADC 0xb8000000L /* CGA address of screen RAM */ -#define SCADM 0xb0000000L /* MONO address of screen RAM */ -#define SCADE 0xb8000000L /* EGA address of screen RAM */ - -#define MONOCRSR 0x0B0D /* monochrome cursor */ -#define CGACRSR 0x0607 /* CGA cursor */ -#define EGACRSR 0x0709 /* EGA cursor */ - -#define CDCGA 0 /* color graphics card */ -#define CDMONO 1 /* monochrome text card */ -#define CDEGA 2 /* EGA color adapter */ -#if PKCODE -#define CDVGA 3 -#endif -#define CDSENSE 9 /* detect the card type */ - -#if PKCODE -#define NDRIVE 4 -#else -#define NDRIVE 3 /* number of screen drivers */ -#endif - -int dtype = -1; /* current display type */ -char drvname[][8] = { /* screen resolution names */ - "CGA", "MONO", "EGA" -#if PKCODE - , "VGA" -#endif -}; -long scadd; /* address of screen ram */ -int *scptr[NROW]; /* pointer to screen lines */ -unsigned int sline[NCOL]; /* screen line image */ -int egaexist = FALSE; /* is an EGA card available? */ -extern union REGS rg; /* cpu register for use of DOS calls */ - -extern int ttopen(); /* Forward references. */ -extern int ttgetc(); -extern int ttputc(); -extern int ttflush(); -extern int ttclose(); -extern int ibmmove(); -extern int ibmeeol(); -extern int ibmeeop(); -extern int ibmbeep(); -extern int ibmopen(); -extern int ibmrev(); -extern int ibmcres(); -extern int ibmclose(); -extern int ibmputc(); -extern int ibmkopen(); -extern int ibmkclose(); - -#if COLOR -extern int ibmfcol(); -extern int ibmbcol(); -extern int ibmscroll_reg(); - -int cfcolor = -1; /* current forground color */ -int cbcolor = -1; /* current background color */ -int ctrans[] = /* ansi to ibm color translation table */ -#if PKCODE -{ 0, 4, 2, 6, 1, 5, 3, 7, 15 }; -#else -{ 0, 4, 2, 6, 1, 5, 3, 7 }; -#endif -#endif - -/* - * Standard terminal interface dispatch table. Most of the fields point into - * "termio" code. - */ -struct terminal term = { - NROW - 1, - NROW - 1, - NCOL, - NCOL, - MARGIN, - SCRSIZ, - NPAUSE, - ibmopen, - ibmclose, - ibmkopen, - ibmkclose, - ttgetc, - ibmputc, - ttflush, - ibmmove, - ibmeeol, - ibmeeop, - ibmbeep, - ibmrev, - ibmcres -#if COLOR - , ibmfcol, - ibmbcol -#endif -#if SCROLLCODE - , ibmscroll_reg -#endif -}; - -#if COLOR -/* Set the current output color. - * - * @color: color to set. - */ -void ibmfcol(int color) -{ - cfcolor = ctrans[color]; -} - -/* Set the current background color. - * - * @color: color to set. - */ -void ibmbcol(int color) -{ - cbcolor = ctrans[color]; -} -#endif - -void ibmmove(int row, int col) -{ - rg.h.ah = 2; /* set cursor position function code */ - rg.h.dl = col; - rg.h.dh = row; - rg.h.bh = 0; /* set screen page number */ - int86(0x10, &rg, &rg); -} - -void ibmeeol(void) -{ /* erase to the end of the line */ - unsigned int attr; /* attribute byte mask to place in RAM */ - unsigned int *lnptr; /* pointer to the destination line */ - int i; - int ccol; /* current column cursor lives */ - int crow; /* row */ - - /* find the current cursor position */ - rg.h.ah = 3; /* read cursor position function code */ - rg.h.bh = 0; /* current video page */ - int86(0x10, &rg, &rg); - ccol = rg.h.dl; /* record current column */ - crow = rg.h.dh; /* and row */ - - /* build the attribute byte and setup the screen pointer */ -#if COLOR - if (dtype != CDMONO) - attr = (((cbcolor & 15) << 4) | (cfcolor & 15)) << 8; - else - attr = 0x0700; -#else - attr = 0x0700; -#endif - lnptr = &sline[0]; - for (i = 0; i < term.t_ncol; i++) - *lnptr++ = SPACE | attr; - - if (flickcode && (dtype == CDCGA)) { - /* wait for vertical retrace to be off */ - while ((inp(0x3da) & 8)); - - /* and to be back on */ - while ((inp(0x3da) & 8) == 0); - } - - /* and send the string out */ - movmem(&sline[0], scptr[crow] + ccol, (term.t_ncol - ccol) * 2); - -} - -/* Put a character at the current position in the current colors */ -void ibmputc(int ch) -{ - rg.h.ah = 14; /* write char to screen with current attrs */ - rg.h.al = ch; -#if COLOR - if (dtype != CDMONO) - rg.h.bl = cfcolor; - else - rg.h.bl = 0x07; -#else - rg.h.bl = 0x07; -#endif - int86(0x10, &rg, &rg); -} - -void ibmeeop(void) -{ - int attr; /* attribute to fill screen with */ - - rg.h.ah = 6; /* scroll page up function code */ - rg.h.al = 0; /* # lines to scroll (clear it) */ - rg.x.cx = 0; /* upper left corner of scroll */ - rg.x.dx = (term.t_nrow << 8) | (term.t_ncol - 1); - /* lower right corner of scroll */ -#if COLOR - if (dtype != CDMONO) - attr = - ((ctrans[gbcolor] & 15) << 4) | (ctrans[gfcolor] & 15); - else - attr = 0; -#else - attr = 0; -#endif - rg.h.bh = attr; - int86(0x10, &rg, &rg); -} - -/* Change reverse video state. - * - * @state: TRUE = reverse, FALSE = normal. - */ -void ibmrev(int state) -{ - /* This never gets used under the IBM-PC driver */ -} - -/* Change screen resolution. - * - * @res: resolution to change to. - */ -void ibmcres(char *res) -{ - int i; - for (i = 0; i < NDRIVE; i++) { - if (strcmp(res, drvname[i]) == 0) { - scinit(i); - return TRUE; - } - } - return FALSE; -} - -#if SCROLLCODE - -/* Move howmany lines starting at from to to. */ -void ibmscroll_reg(from, to, howmany) -{ - int i; - - if (to < from) { - for (i = 0; i < howmany; i++) { - movmem(scptr[from + i], scptr[to + i], - term.t_ncol * 2); - } - } - else if (to > from) { - for (i = howmany - 1; i >= 0; i--) { - movmem(scptr[from + i], scptr[to + i], - term.t_ncol * 2); - } - } -} - -#endif - -void ibmbeep(void) -{ - bdos(6, BEL, 0); -} - -void ibmopen(void) -{ - scinit(CDSENSE); - revexist = TRUE; - ttopen(); -} - -void ibmclose(void) -{ -#if COLOR - ibmfcol(7); - ibmbcol(0); -#endif - /* if we had the EGA open... close it */ - if (dtype == CDEGA) - egaclose(); -#if PKCODE - if (dtype == CDVGA) - egaclose(); -#endif - - ttclose(); -} - -/* Open the keyboard. */ -void ibmkopen(void) -{ -} - -/* Close the keyboard. */ -void ibmkclose(void) -{ -} - -/* Initialize the screen head pointers. - * - * @type: type of adapter to init for. - */ -static int scinit(int type) -{ - union { - long laddr; /* long form of address */ - int *paddr; /* pointer form of address */ - } addr; - int i; - - /* if asked...find out what display is connected */ - if (type == CDSENSE) - type = getboard(); - - /* if we have nothing to do....don't do it */ - if (dtype == type) - return TRUE; - - /* if we try to switch to EGA and there is none, don't */ - if (type == CDEGA && egaexist != TRUE) - return FALSE; - - /* if we had the EGA open... close it */ - if (dtype == CDEGA) - egaclose(); -#if PKCODE - if (dtype == CDVGA) - egaclose(); -#endif - - /* and set up the various parameters as needed */ - switch (type) { - case CDMONO: /* Monochrome adapter */ - scadd = SCADM; - newsize(TRUE, 25); - break; - - case CDCGA: /* Color graphics adapter */ - scadd = SCADC; - newsize(TRUE, 25); - break; - - case CDEGA: /* Enhanced graphics adapter */ - scadd = SCADE; - egaopen(); - newsize(TRUE, 43); - break; - case CDVGA: /* Enhanced graphics adapter */ - scadd = SCADE; - egaopen(); - newsize(TRUE, 50); - break; - } - - /* reset the $sres environment variable */ - strcpy(sres, drvname[type]); - dtype = type; - - /* initialize the screen pointer array */ - for (i = 0; i < NROW; i++) { - addr.laddr = scadd + (long) (NCOL * i * 2); - scptr[i] = addr.paddr; - } - return TRUE; -} - -/* getboard: Determine which type of display board is attached. - Current known types include: - - CDMONO Monochrome graphics adapter - CDCGA Color Graphics Adapter - CDEGA Extended graphics Adapter -*/ - -/* getboard: Detect the current display adapter - if MONO set to MONO - CGA set to CGA EGAexist = FALSE - EGA set to CGA EGAexist = TRUE -*/ -int getboard(void) -{ - int type; /* board type to return */ - - type = CDCGA; - int86(0x11, &rg, &rg); - if ((((rg.x.ax >> 4) & 3) == 3)) - type = CDMONO; - - /* test if EGA present */ - rg.x.ax = 0x1200; - rg.x.bx = 0xff10; - int86(0x10, &rg, &rg); /* If EGA, bh=0-1 and bl=0-3 */ - egaexist = !(rg.x.bx & 0xfefc); /* Yes, it's EGA */ - return type; -} - -/* init the computer to work with the EGA */ -void egaopen(void) -{ - /* put the beast into EGA 43 row mode */ - rg.x.ax = 3; - int86(16, &rg, &rg); - - rg.h.ah = 17; /* set char. generator function code */ - rg.h.al = 18; /* to 8 by 8 double dot ROM */ - rg.h.bl = 0; /* block 0 */ - int86(16, &rg, &rg); - - rg.h.ah = 18; /* alternate select function code */ - rg.h.al = 0; /* clear AL for no good reason */ - rg.h.bl = 32; /* alt. print screen routine */ - int86(16, &rg, &rg); - - rg.h.ah = 1; /* set cursor size function code */ - rg.x.cx = 0x0607; /* turn cursor on code */ - int86(0x10, &rg, &rg); - - outp(0x3d4, 10); /* video bios bug patch */ - outp(0x3d5, 6); -} - -void egaclose(void) -{ - /* put the beast into 80 column mode */ - rg.x.ax = 3; - int86(16, &rg, &rg); -} - -/* Write a line out. - * - * @row: row of screen to place outstr on. - * @outstr: string to write out (must be term.t_ncol long). - * @forg: forground color of string to write. - * @bacg: background color. - */ -void scwrite(int row, char *outstr, int forg, int bacg) -{ - unsigned int attr; /* attribute byte mask to place in RAM */ - unsigned int *lnptr; /* pointer to the destination line */ - int i; - - /* build the attribute byte and setup the screen pointer */ -#if COLOR - if (dtype != CDMONO) - attr = (((ctrans[bacg] & 15) << 4) | (ctrans[forg] & 15)) << 8; - else - attr = (((bacg & 15) << 4) | (forg & 15)) << 8; -#else - attr = (((bacg & 15) << 4) | (forg & 15)) << 8; -#endif - lnptr = &sline[0]; - for (i = 0; i < term.t_ncol; i++) - *lnptr++ = (outstr[i] & 255) | attr; - - if (flickcode && (dtype == CDCGA)) { - /* wait for vertical retrace to be off */ - while ((inp(0x3da) & 8)); - - /* and to be back on */ - while ((inp(0x3da) & 8) == 0); - } - - /* and send the string out */ - movmem(&sline[0], scptr[row], term.t_ncol * 2); -} - -#endif diff --git a/fridge/mingw32.c b/fridge/mingw32.c deleted file mode 100644 index c827873..0000000 --- a/fridge/mingw32.c +++ /dev/null @@ -1,197 +0,0 @@ -/* mingw32.c -- */ - -#ifdef MINGW32 -#include "termio.h" -#include "terminal.h" - -#include -#include -#include -#include -#include - -#include "utf8.h" -#include "wscreen.h" - -static void vv( void) {} -static void vi( int i) {} -static int is( char *s) { return *s ; } - -static void ttmove( int l, int c) ; - -#define MARGIN 8 -#define SCRSIZ 64 -#define NPAUSE 10 /* # times thru update to pause. */ - -struct terminal term = { - 24, /* These four values are set dynamically at open time. */ - 24, - 80, - 80, - MARGIN, - SCRSIZ, - NPAUSE, - ttopen, -#if PKCODE - ttclose, -#else - ttclose, -#endif - vv, /* ttkopen, */ - vv, /* ttkclose, */ - ttgetc, - ttputc, - ttflush, - ttmove, - vv, /* tteeol, */ - vv, /* tteeop, */ - vv, /* ttbeep, */ - vi, /* ttrev, */ - is /* ttcres */ -#if COLOR - , iv, /* ttfcol, */ - iv /* ttbcol */ -#endif -#if SCROLLCODE - , NULL /* set dynamically at open time */ -#endif -} ; - - -int ttrow ; /* Row location of HW cursor */ -int ttcol ; /* Column location of HW cursor */ - -boolean eolexist = TRUE ; /* does clear to EOL exist? */ -boolean revexist = FALSE ; /* does reverse video exist? */ -boolean sgarbf = TRUE ; /* State of screen unknown */ - -char sres[ 16] ; /* Current screen resolution. */ - /* NORMAL, CGA, EGA, VGA */ - -void ttopen( void) { - winit() ; - wcls() ; - term.t_mrow = term.t_nrow = wbottom() - wtop() ; - term.t_mcol = term.t_ncol = wright() - wleft() + 1 ; - wtitle( "uEMACS") ; -} - -void ttclose( void) { -} - -int ttputc( unicode_t c) { - char utf8[ 6] ; - int bytes ; - - bytes = unicode_to_utf8( c, utf8) ; - fwrite( utf8, 1, bytes, stdout); - return 0 ; -} - -void ttflush( void) { - int status ; - - status = fflush( stdout); - while( status < 0 && errno == EAGAIN) { - _sleep( 1) ; - status = fflush( stdout) ; - } - - if( status < 0) - exit( 15) ; -} - -int ttgetc( void) { - static char buffer[ 32] ; - static int pending ; - unicode_t c ; - int count, bytes = 1, expected ; - - count = pending ; - if( !count) { - count = read( 0, buffer, sizeof( buffer)) ; - if( count <= 0) - return 0 ; - - pending = count ; - } - - c = (unsigned char) buffer[ 0] ; - if( c >= 32 && c < 128) - goto done ; - - /* - * Lazy. We don't bother calculating the exact - * expected length. We want at least two characters - * for the special character case (ESC+[) and for - * the normal short UTF8 sequence that starts with - * the 110xxxxx pattern. - * - * But if we have any of the other patterns, just - * try to get more characters. At worst, that will - * just result in a barely perceptible 0.1 second - * delay for some *very* unusual utf8 character - * input. - */ - expected = 2 ; - if( (c & 0xe0) == 0xe0) - expected = 6 ; - - /* Special character - try to fill buffer */ - if( count < expected) { - int n; -#if 0 - ntermios.c_cc[VMIN] = 0; - ntermios.c_cc[VTIME] = 1; /* A .1 second lag */ - tcsetattr(0, TCSANOW, &ntermios); -#endif - n = read(0, buffer + count, sizeof(buffer) - count); - - /* Undo timeout */ -#if 0 - ntermios.c_cc[VMIN] = 1; - ntermios.c_cc[VTIME] = 0; - tcsetattr(0, TCSANOW, &ntermios); -#endif - if (n > 0) - pending += n; - } - - if( pending > 1) { - unsigned char second = buffer[1]; - - /* Turn ESC+'[' into CSI */ - if (c == 27 && second == '[') { - bytes = 2; - c = 128+27; - goto done; - } - } - - bytes = utf8_to_unicode( buffer, 0, pending, &c) ; - -done: - pending -= bytes ; - memmove( buffer, buffer+bytes, pending) ; - return c ; -} - -int typahead( void) { - int x ; /* holds # of pending chars */ - -#ifdef FIONREAD - if( ioctl( 0, FIONREAD, &x) < 0) -#endif - x = 0 ; - return x ; -} - -static void ttmove( int l, int c) { - wgoxy( c, l) ; -} - -#else -typedef void _pedantic_empty_translation_unit ; -#endif - -/* end of mingw32.c */ diff --git a/fridge/vmsvt.c b/fridge/vmsvt.c deleted file mode 100644 index b02bd1b..0000000 --- a/fridge/vmsvt.c +++ /dev/null @@ -1,488 +0,0 @@ -/* VMSVT.C - * - * Advanced VMS terminal driver - * - * Knows about any terminal defined in SMGTERMS.TXT and TERMTABLE.TXT - * located in SYS$SYSTEM. - * - * Author: Curtis Smith - * modified by Petri Kutvonen - */ - -#include /* Standard I/O package */ -#include "estruct.h" /* Emacs' structures */ - -#if VMSVT - -#include /* Descriptor definitions */ - -/* These would normally come from iodef.h and ttdef.h */ -#define IO$_SENSEMODE 0x27 /* Sense mode of terminal */ -#define TT$_UNKNOWN 0x00 /* Unknown terminal */ -#define TT$_VT100 96 - -/** Forward references **/ -int vmsopen(), ttclose(), vmskopen(), vmskclose(), ttgetc(), ttputc(); -int ttflush(), vmsmove(), vmseeol(), vmseeop(), vmsbeep(), vmsrev(); -int vmscres(); -extern int eolexist, revexist; -extern char sres[]; - -#if COLOR -int vmsfcol(), vmsbcol(); -#endif - -/** SMG stuff **/ -static char *begin_reverse, *end_reverse, *erase_to_end_line; -static char *erase_whole_display; -static int termtype; - -#define SMG$K_BEGIN_REVERSE 0x1bf -#define SMG$K_END_REVERSE 0x1d6 -#define SMG$K_SET_CURSOR_ABS 0x23a -#define SMG$K_ERASE_WHOLE_DISPLAY 0x1da -#define SMG$K_ERASE_TO_END_LINE 0x1d9 - -#if SCROLLCODE - -#define SMG$K_SCROLL_FORWARD 561 /* from sys$library:smgtrmptr.h */ -#define SMG$K_SCROLL_REVERSE 562 -#define SMG$K_SET_SCROLL_REGION 572 - -static char *scroll_forward, *scroll_reverse; - -#endif - -/* Dispatch table. All hard fields just point into the terminal I/O code. */ -struct terminal term = { -#if PKCODE - MAXROW, -#else - 24 - 1, /* Max number of rows allowable */ -#endif - /* Filled in */ -1, - /* Current number of rows used */ - MAXCOL, /* Max number of columns */ - /* Filled in */ 0, - /* Current number of columns */ - 64, /* Min margin for extended lines */ - 8, /* Size of scroll region */ - 100, /* # times thru update to pause */ - vmsopen, /* Open terminal at the start */ - ttclose, /* Close terminal at end */ - vmskopen, /* Open keyboard */ - vmskclose, /* Close keyboard */ - ttgetc, /* Get character from keyboard */ - ttputc, /* Put character to display */ - ttflush, /* Flush output buffers */ - vmsmove, /* Move cursor, origin 0 */ - vmseeol, /* Erase to end of line */ - vmseeop, /* Erase to end of page */ - vmsbeep, /* Beep */ - vmsrev, /* Set reverse video state */ - vmscres /* Change screen resolution */ -#if COLOR - , vmsfcol, /* Set forground color */ - vmsbcol /* Set background color */ -#endif -#if SCROLLCODE - , NULL -#endif -}; - -/*** - * ttputs - Send a string to ttputc - * - * Nothing returned - ***/ -ttputs(string) -char *string; /* String to write */ -{ - if (string) - while (*string != '\0') - ttputc(*string++); -} - - -/*** - * vmsmove - Move the cursor (0 origin) - * - * Nothing returned - ***/ -vmsmove(row, col) -int row; /* Row position */ -int col; /* Column position */ -{ - char buffer[32]; - int ret_length; - static int request_code = SMG$K_SET_CURSOR_ABS; - static int max_buffer_length = sizeof(buffer); - static int arg_list[3] = { 2 }; - char *cp; - - int i; - - /* Set the arguments into the arg_list array - * SMG assumes the row/column positions are 1 based (boo!) - */ - arg_list[1] = row + 1; - arg_list[2] = col + 1; - - if ((smg$get_term_data( /* Get terminal data */ - &termtype, /* Terminal table address */ - &request_code, /* Request code */ - &max_buffer_length, /* Maximum buffer length */ - &ret_length, /* Return length */ - buffer, /* Capability data buffer */ - arg_list) - - /* Argument list array */ - /* We'll know soon enough if this doesn't work */ - &1) == 0) { - ttputs("OOPS"); - return; - } - - /* Send out resulting sequence */ - i = ret_length; - cp = buffer; - while (i-- > 0) - ttputc(*cp++); -} - -#if SCROLLCODE - -vmsscroll_reg(from, to, howmany) -{ - int i; - if (to == from) - return; - if (to < from) { - vmsscrollregion(to, from + howmany - 1); - vmsmove(from + howmany - 1, 0); - for (i = from - to; i > 0; i--) - ttputs(scroll_forward); - } else { /* from < to */ - vmsscrollregion(from, to + howmany - 1); - vmsmove(from, 0); - for (i = to - from; i > 0; i--) - ttputs(scroll_reverse); - } - vmsscrollregion(-1, -1); -} - -vmsscrollregion(top, bot) -int top; /* Top position */ -int bot; /* Bottom position */ -{ - char buffer[32]; - int ret_length; - static int request_code = SMG$K_SET_SCROLL_REGION; - static int max_buffer_length = sizeof(buffer); - static int arg_list[3] = { 2 }; - char *cp; - - int i; - - /* Set the arguments into the arg_list array - * SMG assumes the row/column positions are 1 based (boo!) - */ - arg_list[1] = top + 1; - arg_list[2] = bot + 1; - - if ((smg$get_term_data( /* Get terminal data */ - &termtype, /* Terminal table address */ - &request_code, /* Request code */ - &max_buffer_length, /* Maximum buffer length */ - &ret_length, /* Return length */ - buffer, /* Capability data buffer */ - arg_list) - - /* Argument list array */ - /* We'll know soon enough if this doesn't work */ - &1) == 0) { - ttputs("OOPS"); - return; - } - - ttputc(0); - /* Send out resulting sequence */ - i = ret_length; - cp = buffer; - while (i-- > 0) - ttputc(*cp++); -} -#endif - -/*** - * vmsrev - Set the reverse video status - * - * Nothing returned - ***/ -vmsrev(status) -int status; /* TRUE if setting reverse */ -{ - if (status) - ttputs(begin_reverse); - else - ttputs(end_reverse); -} - -/*** - * vmscres - Change screen resolution (which it doesn't) - * - * Nothing returned - ***/ -vmscres() -{ - /* But it could. For vt100/vt200s, one could switch from - 80 and 132 columns modes */ -} - - -#if COLOR -/*** - * vmsfcol - Set the forground color (not implimented) - * - * Nothing returned - ***/ -vmsfcol() -{ -} - -/*** - * vmsbcol - Set the background color (not implimented) - * - * Nothing returned - ***/ -vmsbcol() -{ -} -#endif - -/*** - * vmseeol - Erase to end of line - * - * Nothing returned - ***/ -vmseeol() -{ - ttputs(erase_to_end_line); -} - - -/*** - * vmseeop - Erase to end of page (clear screen) - * - * Nothing returned - ***/ -vmseeop() -{ - ttputs(erase_whole_display); -} - - -/*** - * vmsbeep - Ring the bell - * - * Nothing returned - ***/ -vmsbeep() -{ - ttputc('\007'); -} - - -/*** - * vmsgetstr - Get an SMG string capability by name - * - * Returns: Escape sequence - * NULL No escape sequence available - ***/ -char *vmsgetstr(request_code) -int request_code; /* Request code */ -{ - char *result; - static char seq_storage[1024]; - static char *buffer = seq_storage; - static int arg_list[2] = { 1, 1 }; - int max_buffer_length, ret_length; - - /* Precompute buffer length */ - - max_buffer_length = (seq_storage + sizeof(seq_storage)) - buffer; - - /* Get terminal commands sequence from master table */ - - if ((smg$get_term_data( /* Get terminal data */ - &termtype, /* Terminal table address */ - &request_code, /* Request code */ - &max_buffer_length, /* Maximum buffer length */ - &ret_length, /* Return length */ - buffer, /* Capability data buffer */ - arg_list) - - - /* Argument list array */ - /* If this doesn't work, try again with no arguments */ - &1) == 0 && (smg$get_term_data( /* Get terminal data */ - &termtype, /* Terminal table address */ - &request_code, /* Request code */ - &max_buffer_length, /* Maximum buffer length */ - &ret_length, /* Return length */ - buffer) - - - /* Capability data buffer */ - /* Return NULL pointer if capability is not available */ - &1) == 0) - return NULL; - - /* Check for empty result */ - if (ret_length == 0) - return NULL; - - /* Save current position so we can return it to caller */ - - result = buffer; - - /* NIL terminate the sequence for return */ - - buffer[ret_length] = 0; - - /* Advance buffer */ - - buffer += ret_length + 1; - - /* Return capability to user */ - return result; -} - - -/** I/O information block definitions **/ -struct iosb { /* I/O status block */ - short i_cond; /* Condition value */ - short i_xfer; /* Transfer count */ - long i_info; /* Device information */ -}; -struct termchar { /* Terminal characteristics */ - char t_class; /* Terminal class */ - char t_type; /* Terminal type */ - short t_width; /* Terminal width in characters */ - long t_mandl; /* Terminal's mode and length */ - long t_extend; /* Extended terminal characteristics */ -}; -static struct termchar tc; /* Terminal characteristics */ - -/*** - * vmsgtty - Get terminal type from system control block - * - * Nothing returned - ***/ -vmsgtty() -{ - short fd; - int status; - struct iosb iostatus; - $DESCRIPTOR(devnam, "SYS$INPUT"); - - /* Assign input to a channel */ - status = sys$assign(&devnam, &fd, 0, 0); - if ((status & 1) == 0) - exit(status); - - /* Get terminal characteristics */ - status = sys$qiow( /* Queue and wait */ - 0, /* Wait on event flag zero */ - fd, /* Channel to input terminal */ - IO$_SENSEMODE, /* Get current characteristic */ - &iostatus, /* Status after operation */ - 0, 0, /* No AST service */ - &tc, /* Terminal characteristics buf */ - sizeof(tc), /* Size of the buffer */ - 0, 0, 0, 0); /* P3-P6 unused */ - - /* De-assign the input device */ - if ((sys$dassgn(fd) & 1) == 0) - exit(status); - - /* Jump out if bad status */ - if ((status & 1) == 0) - exit(status); - if ((iostatus.i_cond & 1) == 0) - exit(iostatus.i_cond); -} - - -/*** - * vmsopen - Get terminal type and open terminal - * - * Nothing returned - ***/ -vmsopen() -{ - /* Get terminal type */ - vmsgtty(); - if (tc.t_type == TT$_UNKNOWN) { - printf("Terminal type is unknown!\n"); - printf - ("Try set your terminal type with SET TERMINAL/INQUIRE\n"); - printf("Or get help on SET TERMINAL/DEVICE_TYPE\n"); - exit(3); - } - - /* Access the system terminal definition table for the */ - /* information of the terminal type returned by IO$_SENSEMODE */ - if ((smg$init_term_table_by_type(&tc.t_type, &termtype) & 1) == 0) - return -1; - - /* Set sizes */ - term.t_nrow = ((unsigned int) tc.t_mandl >> 24) - 1; - term.t_ncol = tc.t_width; - - /* Get some capabilities */ - begin_reverse = vmsgetstr(SMG$K_BEGIN_REVERSE); - end_reverse = vmsgetstr(SMG$K_END_REVERSE); - revexist = begin_reverse != NULL && end_reverse != NULL; - erase_to_end_line = vmsgetstr(SMG$K_ERASE_TO_END_LINE); - eolexist = erase_to_end_line != NULL; - erase_whole_display = vmsgetstr(SMG$K_ERASE_WHOLE_DISPLAY); - -#if SCROLLCODE - scroll_forward = vmsgetstr(SMG$K_SCROLL_FORWARD); - scroll_reverse = vmsgetstr(SMG$K_SCROLL_REVERSE); - if (tc.t_type < TT$_VT100 || scroll_reverse == NULL || - scroll_forward == NULL) - term.t_scroll = NULL; - else - term.t_scroll = vmsscroll_reg; -#endif - - /* Set resolution */ - strcpy(sres, "NORMAL"); - - /* Open terminal I/O drivers */ - ttopen(); -} - - -/*** - * vmskopen - Open keyboard (not used) - * - * Nothing returned - ***/ -vmskopen() -{ -} - - -/*** - * vmskclose - Close keyboard (not used) - * - * Nothing returned - ***/ -vmskclose() -{ -} - -#endif diff --git a/fridge/vt52.c b/fridge/vt52.c deleted file mode 100644 index c11149b..0000000 --- a/fridge/vt52.c +++ /dev/null @@ -1,161 +0,0 @@ -/* vt52.c - * - * The routines in this file - * provide support for VT52 style terminals - * over a serial line. The serial I/O services are - * provided by routines in "termio.c". It compiles - * into nothing if not a VT52 style device. The - * bell on the VT52 is terrible, so the "beep" - * routine is conditionalized on defining BEL. - * - * modified by Petri Kutvonen - */ - -#define termdef 1 /* don't define "term" external */ - -#include -#include "estruct.h" - -#if VT52 - -#define NROW 24 /* Screen size. */ -#define NCOL 80 /* Edit if you want to. */ -#define MARGIN 8 /* size of minimim margin and */ -#define SCRSIZ 64 /* scroll size for extended lines */ -#define NPAUSE 100 /* # times thru update to pause */ -#define BIAS 0x20 /* Origin 0 coordinate bias. */ -#define ESC 0x1B /* ESC character. */ -#define BEL 0x07 /* ascii bell character */ - -extern int ttopen(); /* Forward references. */ -extern int ttgetc(); -extern int ttputc(); -extern int ttflush(); -extern int ttclose(); -extern int vt52move(); -extern int vt52eeol(); -extern int vt52eeop(); -extern int vt52beep(); -extern int vt52open(); -extern int vt52rev(); -extern int vt52cres(); -extern int vt52kopen(); -extern int vt52kclose(); - -#if COLOR -extern int vt52fcol(); -extern int vt52bcol(); -#endif - -/* - * Dispatch table. - * All the hard fields just point into the terminal I/O code. - */ -struct terminal term = { - NROW - 1, - NROW - 1, - NCOL, - NCOL, - MARGIN, - SCRSIZ, - NPAUSE, - &vt52open, - &ttclose, - &vt52kopen, - &vt52kclose, - &ttgetc, - &ttputc, - &ttflush, - &vt52move, - &vt52eeol, - &vt52eeop, - &vt52beep, - &vt52rev, - &vt52cres -#if COLOR - , &vt52fcol, - &vt52bcol -#endif -#if SCROLLCODE - , NULL -#endif -}; - -vt52move(row, col) -{ - ttputc(ESC); - ttputc('Y'); - ttputc(row + BIAS); - ttputc(col + BIAS); -} - -vt52eeol() -{ - ttputc(ESC); - ttputc('K'); -} - -vt52eeop() -{ - ttputc(ESC); - ttputc('J'); -} - -vt52rev(status) - /* set the reverse video state */ -int status; /* TRUE = reverse video, FALSE = normal video */ - -{ - /* can't do this here, so we won't */ -} - -vt52cres() -{ /* change screen resolution - (not here though) */ - return TRUE; -} - -#if COLOR -vt52fcol() -{ /* set the forground color [NOT IMPLIMENTED] */ -} - -vt52bcol() -{ /* set the background color [NOT IMPLIMENTED] */ -} -#endif - -vt52beep() -{ -#ifdef BEL - ttputc(BEL); - ttflush(); -#endif -} - -vt52open() -{ -#if V7 | BSD - char *cp; - char *getenv(); - - if ((cp = getenv("TERM")) == NULL) { - puts("Shell variable TERM not defined!"); - exit(1); - } - if (strcmp(cp, "vt52") != 0 && strcmp(cp, "z19") != 0) { - puts("Terminal type not 'vt52'or 'z19' !"); - exit(1); - } -#endif - ttopen(); -} - -vt52kopen() -{ -} - -vt52kclose() -{ -} - -#endif diff --git a/fridge/wscreen.c b/fridge/wscreen.c deleted file mode 100644 index aa983fa..0000000 --- a/fridge/wscreen.c +++ /dev/null @@ -1,117 +0,0 @@ -/* wscreen.c -- windows screen console for MINGW32 */ -#include "wscreen.h" - -#ifdef MINGW32 - -#include -#include -#include - -/* Standard error macro for reporting API errors */ -#define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %ld from %s \ - on line %d\n", __FILE__, GetLastError(), api, __LINE__);} - -static void cls( HANDLE hConsole ) -{ - COORD coordScreen = { 0, 0 }; /* here's where we'll home the - cursor */ - BOOL bSuccess; - DWORD cCharsWritten; - CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ - DWORD dwConSize; /* number of character cells in - the current buffer */ - - /* get the number of character cells in the current buffer */ - - bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi ); - PERR( bSuccess, "GetConsoleScreenBufferInfo" ); - dwConSize = csbi.dwSize.X * csbi.dwSize.Y; - - /* fill the entire screen with blanks */ - - bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', - dwConSize, coordScreen, &cCharsWritten ); - PERR( bSuccess, "FillConsoleOutputCharacter" ); - - /* get the current text attribute */ - - bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi ); - PERR( bSuccess, "ConsoleScreenBufferInfo" ); - - /* now set the buffer's attributes accordingly */ - - bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes, - dwConSize, coordScreen, &cCharsWritten ); - PERR( bSuccess, "FillConsoleOutputAttribute" ); - - /* put the cursor at (0, 0) */ - - bSuccess = SetConsoleCursorPosition( hConsole, coordScreen ); - PERR( bSuccess, "SetConsoleCursorPosition" ); - return; -} - -void wcls( void) { - cls( GetStdHandle( STD_OUTPUT_HANDLE)) ; -} - -static struct { - int width ; - int height ; - int curTop, curBot, curRight, curLeft ; -} Screen ; - -void winit( void) { - CONSOLE_SCREEN_BUFFER_INFO csbInfo ; - - wcls() ; - if( GetConsoleScreenBufferInfo( - GetStdHandle( STD_OUTPUT_HANDLE), &csbInfo)) { - Screen.width = csbInfo.dwSize.X ; - Screen.height = csbInfo.dwSize.Y ; - Screen.curLeft = csbInfo.srWindow.Left ; - Screen.curTop = csbInfo.srWindow.Top ; - Screen.curRight = csbInfo.srWindow.Right ; - Screen.curBot = csbInfo.srWindow.Bottom ; - } -} - -int wwidth( void) { - return Screen.width ; -} - -int wheight( void) { - return Screen.height ; -} - -int wleft( void) { - return Screen.curLeft ; -} - -int wtop( void) { - return Screen.curTop ; -} - -int wright( void) { - return Screen.curRight ; -} - -int wbottom( void) { - return Screen.curBot ; -} - -void wgoxy( int x, int y) { - COORD coord ; - - coord.X = x ; - coord.Y = y ; - SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE), coord ); -} - -void wtitle( const char *title) { - SetConsoleTitle( title) ; -} - -#endif /* MINGW32 */ - -/* end of wscreen.c */ diff --git a/fridge/wscreen.h b/fridge/wscreen.h deleted file mode 100644 index 65080f4..0000000 --- a/fridge/wscreen.h +++ /dev/null @@ -1,14 +0,0 @@ -/* wscreen.h -- character screen drawing */ - -void winit( void) ; -void wcls( void) ; -void wgoxy( int x, int y) ; -void wtitle( const char *title) ; -int wwidth( void) ; -int wheight( void) ; -int wleft( void) ; -int wright( void) ; -int wtop( void) ; -int wbottom( void) ; - -/* end of wscreen.h */ From eaf516110f0020418550ef13c5d4a367dccfe2b9 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 9 Aug 2021 12:06:07 +0800 Subject: [PATCH 29/37] basic: code review and minor refactoring. --- basic.c | 485 +++++++++++++++++++++++++++----------------------------- basic.h | 19 ++- 2 files changed, 241 insertions(+), 263 deletions(-) diff --git a/basic.c b/basic.c index 6d04337..c87cf4c 100644 --- a/basic.c +++ b/basic.c @@ -1,19 +1,16 @@ /* basic.c -- implements basic.h */ - #include "basic.h" -/* basic.c - * - * The routines in this file move the cursor around on the screen. They - * compute a new value for the cursor, then adjust ".". The display code - * always updates the cursor location, so only moves between lines, or - * functions that adjust the top line in the window and invalidate the - * framing, are hard. - * - * modified by Petri Kutvonen +/* The routines in this file move the cursor around on the screen. They + compute a new value for the cursor, then adjust ".". The display code + always updates the cursor location, so only moves between lines, or + functions that adjust the top line in the window and invalidate the + framing, are hard. + + modified by Petri Kutvonen */ -#include +#include #include #include "input.h" @@ -22,340 +19,322 @@ #include "terminal.h" #include "window.h" +#define CVMVAS 1 /* arguments to page forward/back in pages */ -#define CVMVAS 1 /* arguments to page forward/back in pages */ +int overlap = 0 ; /* $overlap: line overlap in forw/back page */ +int curgoal ; /* $target: Goal for C-P, C-N */ -int overlap = DEFAULT_OVERLAP ; /* line overlap in forw/back page */ -int curgoal ; /* Goal for C-P, C-N */ - - -/* - * This routine, given a pointer to a struct line, and the current cursor goal - * column, return the best choice for the offset. The offset is returned. - * Used by "C-N" and "C-P". +/* This routine, given a pointer to a struct line, and the current cursor + goal column, return the best choice for the offset. The offset is + returned. Used by "C-N" and "C-P". */ static unsigned getgoal( line_p dlp) { - int col ; - unsigned idx ; - const unsigned len = llength( dlp) ; + int col = 0 ; + const unsigned len = llength( dlp) ; + unsigned idx = 0 ; + while( idx < len) { + unicode_t c ; + unsigned width = utf8_to_unicode( dlp->l_text, idx, len, &c) ; - col = 0 ; - idx = 0 ; - while( idx < len) { - unicode_t c ; - unsigned width = utf8_to_unicode( dlp->l_text, idx, len, &c) ; + /* Take tabs, ^X and \xx hex characters into account */ + if( c == '\t') + col += tabwidth - col % tabwidth ; + else if( c < 0x20 || c == 0x7F) /* ^x */ + col += 2 ; + else if( c >= 0x80 && c <= 0xA0) /* \xx */ + col += 3 ; + else { + int w = utf8_width( c) ; /* work around */ + col += (w < 0) ? 2 : w ; /* unknown unicode width as \u */ + } - /* Take tabs, ^X and \xx hex characters into account */ - if( c == '\t') - col += tabwidth - col % tabwidth ; - else if( c < 0x20 || c == 0x7F) - col += 2 ; - else if( c >= 0x80 && c <= 0xA0) - col += 3 ; - else - col += 1 ; + if( col > curgoal) + break ; + else + idx += width ; + } - if( col > curgoal) - break ; - - idx += width ; - } - - return idx ; + return idx ; } -/* - * Move the cursor to the beginning of the current line of active window. - */ + +/* Move the cursor to the beginning of the current line of active window. */ boolean gotobol( int f, int n) { - curwp->w_doto = 0 ; - return TRUE ; + curwp->w_doto = 0 ; + return TRUE ; } -/* - * Move the cursor to the end of the current line of active window. - */ + +/* Move the cursor to the end of the current line of active window. */ boolean gotoeol( int f, int n) { - curwp->w_doto = llength( curwp->w_dotp) ; - return TRUE ; + curwp->w_doto = llength( curwp->w_dotp) ; + return TRUE ; } -/* - * Goto the beginning of the buffer. Massive adjustment of dot. This is - * considered to be hard motion; it really isn't if the original value of dot - * is the same as the new value of dot. Normally bound to "M-<". + +/* Goto the beginning of the buffer. Massive adjustment of dot. This is + considered to be hard motion; it really isn't if the original value of + dot is the same as the new value of dot. Normally bound to "M-<". */ boolean gotobob( int f, int n) { - curwp->w_dotp = lforw( curbp->b_linep) ; - curwp->w_doto = 0 ; - curwp->w_flag |= WFHARD ; - return TRUE ; + curwp->w_dotp = lforw( curbp->b_linep) ; + curwp->w_doto = 0 ; + curwp->w_flag |= WFHARD ; + return TRUE ; } -/* - * Move to the end of the buffer. Dot is always put at the end of the file - * (ZJ). The standard screen code does most of the hard parts of update. - * Bound to "M->". + +/* Move to the end of the buffer. Dot is always put at the end of the file + (ZJ). The standard screen code does most of the hard parts of update. + Bound to "M->". */ boolean gotoeob( int f, int n) { - curwp->w_dotp = curbp->b_linep ; - curwp->w_doto = 0 ; - curwp->w_flag |= WFHARD ; - return TRUE ; + curwp->w_dotp = curbp->b_linep ; + curwp->w_doto = 0 ; + curwp->w_flag |= WFHARD ; + return TRUE ; } -/* - * Move forward by full lines. If the number of lines to move is less than - * zero, call the backward line function to actually do it. The last command - * controls how the goal column is set. Bound to "C-N". No errors are - * possible. + +/* Move forward by full lines. If the number of lines to move is less than + zero, call the backward line function to actually do it. The last + command controls how the goal column is set. Bound to "C-N". No errors + are possible. */ boolean forwline( int f, int n) { - line_p dlp ; + assert( f == TRUE || n == 1) ; + if( n < 0) + return backline( f, -n) ; - if (n < 0) - return backline(f, -n); +/* if we are on the last line as we start....fail the command */ + if( n && curwp->w_dotp == curbp->b_linep) + return FALSE ; - /* if we are on the last line as we start....fail the command */ - if (curwp->w_dotp == curbp->b_linep) - return FALSE; +/* if the last command was not a line move, reset the goal column */ + if( (lastflag & CFCPCN) == 0) + curgoal = getccol( FALSE) ; - /* if the last command was not a line move, reset the goal column */ - if ((lastflag & CFCPCN) == 0) - curgoal = getccol(FALSE); +/* flag this command as a line move */ + thisflag |= CFCPCN ; - /* flag this command as a line move */ - thisflag |= CFCPCN; +/* and move the point down */ + line_p dlp = curwp->w_dotp ; + while( n && dlp != curbp->b_linep) { + dlp = lforw( dlp) ; + n -= 1 ; + } - /* and move the point down */ - dlp = curwp->w_dotp; - while( n && dlp != curbp->b_linep) { - dlp = lforw( dlp) ; - n -= 1 ; - } - - /* reseting the current position */ - curwp->w_dotp = dlp; - curwp->w_doto = getgoal(dlp); - curwp->w_flag |= WFMOVE; - return (n == 0) ? TRUE : FALSE ; +/* reseting the current position */ + curwp->w_dotp = dlp ; + curwp->w_doto = getgoal( dlp) ; + curwp->w_flag |= WFMOVE ; + return (n == 0) ? TRUE : FALSE ; } -/* - * This function is like "forwline", but goes backwards. The scheme is exactly - * the same. Check for arguments that are less than zero and call your - * alternate. Figure out the new line and call "movedot" to perform the - * motion. No errors are possible. Bound to "C-P". + +/* This function is like "forwline", but goes backwards. The scheme is + exactly the same. Check for arguments that are less than zero and call + your alternate. Figure out the new line and call "movedot" to perform + the motion. No errors are possible. Bound to "C-P". */ boolean backline( int f, int n) { - line_p dlp ; + if( n < 0) + return forwline( f, -n) ; - if (n < 0) - return forwline(f, -n); +/* if we are on the first line as we start....fail the command */ + if( n && lback( curwp->w_dotp) == curbp->b_linep) + return FALSE ; - /* if we are on the first line as we start....fail the command */ - if (lback(curwp->w_dotp) == curbp->b_linep) - return FALSE; +/* if the last command was not a line move, reset the goal column */ + if( (lastflag & CFCPCN) == 0) + curgoal = getccol( FALSE) ; - /* if the last command was not a line move, reset the goal column */ - if ((lastflag & CFCPCN) == 0) - curgoal = getccol(FALSE); +/* flag this command as a line move */ + thisflag |= CFCPCN ; - /* flag this command as a line move */ - thisflag |= CFCPCN; +/* and move the point up */ + line_p dlp = curwp->w_dotp ; + while( n && lback( dlp) != curbp->b_linep) { + dlp = lback( dlp) ; + n -= 1 ; + } - /* and move the point up */ - dlp = curwp->w_dotp; - while( n && lback( dlp) != curbp->b_linep) { - dlp = lback( dlp) ; - n -= 1 ; - } - - /* reseting the current position */ - curwp->w_dotp = dlp; - curwp->w_doto = getgoal(dlp); - curwp->w_flag |= WFMOVE; - return (n == 0) ? TRUE : FALSE ; +/* reseting the current position */ + curwp->w_dotp = dlp ; + curwp->w_doto = getgoal( dlp) ; + curwp->w_flag |= WFMOVE ; + return (n == 0) ? TRUE : FALSE ; } -/* - * Move to a particular line. + +/* Move to a particular line. * * @n: The specified line position at the current buffer. */ -int gotoline( int f, int n) { - /* Get an argument if one doesnt exist. */ - if( f == FALSE) { - int status ; - char *arg ; /* Buffer to hold argument. */ +BINDABLE( gotoline) { +/* Get an argument if one doesn't exist. */ + if( f == FALSE) { + char *arg ; /* Buffer to hold argument. */ - status = newmlarg( &arg, "Line to GOTO: ", 0) ; - if( status != TRUE) { - mloutstr( "(Aborted)") ; - return status ; - } + int status = newmlarg( &arg, "goto-line: ", 0) ; + if( status != TRUE) + return status ; - n = atoi( arg) ; - free( arg) ; - } + n = atoi( arg) ; + free( arg) ; + f = TRUE ; + } - /* Handle the case where the user may be passed something like this: - * em filename + - * In this case we just go to the end of the buffer. - */ - if (n == 0) - return gotoeob(f, n); +/* Handle the case where the user may be passed something like this: + * ue filename + + * In this case we just go to the end of the buffer. + */ + if( n == 0) + return gotoeob( f, n) ; - /* If a bogus argument was passed, then returns false. */ - if (n < 0) - return FALSE; +/* If a bogus argument was passed, then returns false. */ + if( n < 0) + return FALSE ; - /* First, we go to the begin of the buffer. */ - gotobob(f, n); - return (n == 1) ? TRUE : forwline( f, n - 1) ; +/* First, we go to the begin of the buffer. */ + gotobob( f, n) ; + return (n == 1) ? TRUE : forwline( TRUE, n - 1) ; } -/* - * Scroll forward by a specified number of lines, or by a full page if no - * argument. Bound to "C-V". The "2" in the arithmetic on the window size is - * the overlap; this value is the default overlap value in ITS EMACS. Because - * this zaps the top line in the display window, we have to do a hard update. + +/* Scroll forward by a specified number of lines, or by a full page if no + argument. Bound to "C-V". The "2" in the arithmetic on the window size + is the overlap; this value is the default overlap value in ITS EMACS. + Because this zaps the top line in the display window, we have to do a + hard update. */ boolean forwpage( int f, int n) { - line_p lp ; + line_p lp ; - if (f == FALSE) { + if( f == FALSE) { #if SCROLLCODE - if (term.t_scroll != NULL) /* $scroll == FALSE */ - if (overlap == 0) /* $overlap == 0 */ - n = curwp->w_ntrows * 2 / 3 ; - else - n = curwp->w_ntrows - overlap; - else + if (term.t_scroll != NULL) /* $scroll == FALSE */ + if (overlap == 0) /* $overlap == 0 */ + n = curwp->w_ntrows * 2 / 3 ; + else + n = curwp->w_ntrows - overlap; + else #endif - n = curwp->w_ntrows - 2; /* Default scroll. */ + n = curwp->w_ntrows - 2; /* Default scroll. */ - if (n <= 0) /* Forget the overlap. */ - n = 1; /* If tiny window. */ - } else if (n < 0) - return backpage(f, -n); + if (n <= 0) /* Forget the overlap. */ + n = 1; /* If tiny window. */ + } else if( n < 0) + return backpage(f, -n); #if CVMVAS - else /* Convert from pages. */ - n *= curwp->w_ntrows; /* To lines. */ + else /* Convert from pages. */ + n *= curwp->w_ntrows; /* To lines. */ #endif -/* lp = curwp->w_linep; */ - lp = curwp->w_dotp ; - while( n && lp != curbp->b_linep) { - lp = lforw( lp) ; - n -= 1 ; - } +/* lp = curwp->w_linep; */ + lp = curwp->w_dotp ; + while( n && lp != curbp->b_linep) { + lp = lforw( lp) ; + n -= 1 ; + } -/* curwp->w_linep = lp; */ - curwp->w_dotp = lp; - curwp->w_doto = 0; - reposition( TRUE, 0) ; +/* curwp->w_linep = lp; */ + curwp->w_dotp = lp; + curwp->w_doto = 0; + reposition( TRUE, 0) ; #if SCROLLCODE - curwp->w_flag |= WFHARD | WFKILLS; + curwp->w_flag |= WFHARD | WFKILLS; #else - curwp->w_flag |= WFHARD; + curwp->w_flag |= WFHARD; #endif - return TRUE; + return TRUE ; } -/* - * This command is like "forwpage", but it goes backwards. The "2", like - * above, is the overlap between the two windows. The value is from the ITS - * EMACS manual. Bound to "M-V". We do a hard update for exactly the same - * reason. + +/* This command is like "forwpage", but it goes backwards. The "2", like + above, is the overlap between the two windows. The value is from the + ITS EMACS manual. Bound to "M-V". We do a hard update for exactly the + same reason. */ boolean backpage( int f, int n) { - line_p lp ; + line_p lp ; - if (f == FALSE) { /* interactive, default n = 1 supplied */ - /* in interactive mode, first move dot to top of window */ - if( curwp->w_dotp != curwp->w_linep) { - curwp->w_dotp = curwp->w_linep ; - curwp->w_doto = 0 ; -/* curwp->w_flag |= WFMOVE ; */ - return TRUE ; - } + if( f == FALSE) { /* interactive, default n = 1 supplied */ + /* in interactive mode, first move dot to top of window */ + if( curwp->w_dotp != curwp->w_linep) { + curwp->w_dotp = curwp->w_linep ; + curwp->w_doto = 0 ; +/* curwp->w_flag |= WFMOVE ; */ + return TRUE ; + } #if SCROLLCODE - if (term.t_scroll != NULL) /* $scroll != FALSE */ - if (overlap == 0) /* $overlap == 0 */ - n = curwp->w_ntrows * 2 / 3 ; - else - n = curwp->w_ntrows - overlap; - else + if (term.t_scroll != NULL) /* $scroll != FALSE */ + if (overlap == 0) /* $overlap == 0 */ + n = curwp->w_ntrows * 2 / 3 ; + else + n = curwp->w_ntrows - overlap; + else #endif - n = curwp->w_ntrows - 2; /* Default scroll. */ + n = curwp->w_ntrows - 2; /* Default scroll. */ - if (n <= 0) /* Don't blow up if the. */ - n = 1; /* Window is tiny. */ - } else if (n < 0) - return forwpage(f, -n); + if (n <= 0) /* Don't blow up if the. */ + n = 1; /* Window is tiny. */ + } else if (n < 0) + return forwpage(f, -n); #if CVMVAS - else /* Convert from pages. */ - n *= curwp->w_ntrows; /* To lines. */ + else /* Convert from pages. */ + n *= curwp->w_ntrows; /* To lines. */ #endif -/* lp = curwp->w_linep; */ - lp = curwp->w_dotp ; - while( n && lback( lp) != curbp->b_linep) { - lp = lback( lp) ; - n -= 1 ; - } +/* lp = curwp->w_linep; */ + lp = curwp->w_dotp ; + while( n && lback( lp) != curbp->b_linep) { + lp = lback( lp) ; + n -= 1 ; + } -/* curwp->w_linep = lp; */ - curwp->w_dotp = lp; - curwp->w_doto = 0; - reposition( TRUE, (f == FALSE) ? 1 : 0) ; +/* curwp->w_linep = lp; */ + curwp->w_dotp = lp; + curwp->w_doto = 0; + reposition( TRUE, (f == FALSE) ? 1 : 0) ; #if SCROLLCODE - curwp->w_flag |= WFHARD | WFINS; + curwp->w_flag |= WFHARD | WFINS; #else - curwp->w_flag |= WFHARD; + curwp->w_flag |= WFHARD; #endif - return TRUE; + return TRUE; } -/* - * Set the mark in the current window to the value of "." in the window. No - * errors are possible. Bound to "M-.". + +/* Set the mark in the current window to the value of "." in the window. + No errors are possible. Bound to M-. set-mark. */ boolean setmark( int f, int n) { - curwp->w_markp = curwp->w_dotp; - curwp->w_marko = curwp->w_doto; - mloutstr( "(Mark set)") ; - return TRUE ; + curwp->w_markp = curwp->w_dotp ; + curwp->w_marko = curwp->w_doto ; + mloutstr( "(Mark set)") ; + return TRUE ; } -/* - * Swap the values of "." and "mark" in the current window. This is pretty - * easy, because all of the hard work gets done by the standard routine - * that moves the mark about. The only possible error is "no mark". Bound to - * "C-X C-X". +/* Swap the values of "." and "mark" in the current window. If no mark as + been previously set, set it. Bound to C-X C-X exchange-point-and-mark. */ boolean swapmark( int f, int n) { - line_p odotp ; - int odoto; + line_p odotp = curwp->w_dotp ; + int odoto = curwp->w_doto ; + if( curwp->w_markp) { + curwp->w_dotp = curwp->w_markp ; + curwp->w_doto = curwp->w_marko ; + curwp->w_flag |= WFMOVE ; + } - if( curwp->w_markp == NULL) { - mloutstr( "No mark in this window") ; - return FALSE ; - } - - odotp = curwp->w_dotp; - odoto = curwp->w_doto; - curwp->w_dotp = curwp->w_markp; - curwp->w_doto = curwp->w_marko; - curwp->w_markp = odotp; - curwp->w_marko = odoto; - curwp->w_flag |= WFMOVE; - return TRUE; + curwp->w_markp = odotp ; + curwp->w_marko = odoto ; + return TRUE ; } /* end of basic.c */ diff --git a/basic.h b/basic.h index 205cbe9..5c6c8b1 100644 --- a/basic.h +++ b/basic.h @@ -3,24 +3,23 @@ #ifndef _BASIC_H_ #define _BASIC_H_ -#include "retcode.h" +#include "names.h" -/* -** $overlap is the size of the line overlap when kbd calls page forw/back -** if 0, page will move by 2/3 of the window size (1/3 page overlap) -** default to 0 -*/ -#define DEFAULT_OVERLAP 0 -extern int overlap ; /* line overlap in forw/back page */ +/* $overlap is the size of the line overlap when kbd calls page forw/back + if 0, page will move by 2/3 of the window size (1/3 page overlap) + default to 0 + */ +extern int overlap ; /* $overlap: line overlap in forw/back page */ /* $target (== curgoal) is the column target when doing line move */ -extern int curgoal ; /* Goal for C-P previous-line, C-N next-line */ +extern int curgoal ; /* $target: Goal for C-P previous-line, C-N next-line */ +/* Bindable functions */ boolean gotobol( int f, int n) ; boolean gotoeol( int f, int n) ; -int gotoline( int f, int n) ; +BINDABLE( gotoline) ; boolean gotobob( int f, int n) ; boolean gotoeob( int f, int n) ; boolean forwline( int f, int n) ; From 720603ac8e2f75c874a68f18b90dbf9f7e2fa430 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 9 Aug 2021 15:24:33 +0800 Subject: [PATCH 30/37] bind: code review and minor refactoring. basic: minor reformatting. --- basic.c | 1 + bind.c | 391 ++++++++++++++++++++++++++++---------------------------- bind.h | 6 +- 3 files changed, 199 insertions(+), 199 deletions(-) diff --git a/basic.c b/basic.c index c87cf4c..af3ea35 100644 --- a/basic.c +++ b/basic.c @@ -320,6 +320,7 @@ boolean setmark( int f, int n) { return TRUE ; } + /* Swap the values of "." and "mark" in the current window. If no mark as been previously set, set it. Bound to C-X C-X exchange-point-and-mark. */ diff --git a/bind.c b/bind.c index 3406fe8..2975702 100644 --- a/bind.c +++ b/bind.c @@ -9,24 +9,22 @@ */ #include -#include #include #include -#include "estruct.h" #include "bindable.h" #include "buffer.h" -#include "display.h" +#include "display.h" /* upmode(), ostring() */ #include "exec.h" #include "file.h" #include "flook.h" #include "input.h" #include "line.h" +#include "mlout.h" #include "names.h" #include "util.h" #include "window.h" - static int buildlist( char *mstring) ; static char *cmdstr( unsigned c, char *seq) ; static unsigned int getckey( int mflag) ; @@ -34,124 +32,122 @@ static unsigned int stock( char *keyname) ; static const char *getfname( unsigned keycode, const char *failmsg) ; -static boolean cmdfail( const char *msg) { - mlwrite( "%s", msg) ; - return FALSE ; -} - +/* give me some help!!!! bring up a fake buffer and read the help file into + it with view mode + */ BINDABLE( help) { -/* give me some help!!!! - bring up a fake buffer and read the help file into it with view mode */ char *fname = NULL; /* ptr to file returned by flook() */ /* first check if we are already here */ buffer_p bp = bfind( hlpfname, FALSE, BFINVS); - if( bp == curbp) - return TRUE ; + if( bp == curbp) + return TRUE ; if( bp == NULL) { fname = flook( hlpfname, FALSE) ; if( fname == NULL) - return cmdfail( "(Help file is not online)") ; + return mloutfail( "(Help file is not online)") ; } /* split the current window to make room for the help stuff */ - if( wheadp->w_wndp == NULL /* One window */ - && splitwind( FALSE, 1) == FALSE) /* Split it */ + if( wheadp->w_wndp == NULL /* One window */ + && splitwind( FALSE, 1) == FALSE) /* Split it */ return FALSE ; - if (bp == NULL) { - /* and read the stuff in */ - if (getfile(fname, FALSE) == FALSE) - return FALSE; + if( bp == NULL) { + /* and read the stuff in */ + if( getfile( fname, FALSE) == FALSE) + return FALSE ; } else - swbuffer( bp) ; + swbuffer( bp) ; /* make this window in VIEW mode, update all mode lines */ curwp->w_bufp->b_mode |= MDVIEW; curwp->w_bufp->b_flag |= BFINVS; upmode() ; - return TRUE; + return TRUE ; } + static boolean invalidkey( void) { - return cmdfail( "(Invalid key sequence)") ; + return mloutfail( "(Invalid key sequence)") ; } + /* describe the command for a certain key */ BINDABLE( deskey) { - const char cmdname[] = "describe-key" ; - char outseq[ NSTRING] ; /* output buffer for command sequence */ + const char cmdname[] = "describe-key" ; + char outseq[ 8] ; /* output buffer for keystroke sequence */ /* prompt the user to type a key to describe */ - mlwrite( "%s: ", cmdname) ; + mloutfmt( "%s: ", cmdname) ; /* get the command sequence to describe * change it to something we can print as well */ unsigned keycode = getckey( FALSE) ; - if( keycode == (unsigned) ~0) - return invalidkey() ; + if( keycode == (unsigned) ~0) + return invalidkey() ; /* output the command sequence */ - mlwrite( "%s %s: 0x%x, %s", cmdname, cmdstr( keycode, outseq), keycode, + mloutfmt( "%s %s: 0x%x, %s", cmdname, cmdstr( keycode, outseq), keycode, getfname( keycode, "Not Bound")) ; return TRUE ; } -/* - * bindtokey: + +/* bindtokey: * add a new key to the key binding table * * int f, n; command arguments [IGNORED] */ BINDABLE( bindtokey) { - kbind_p ktp ; /* pointer into the command table */ - char outseq[ 80] ; /* output buffer for keystroke sequence */ + kbind_p ktp ; /* pointer into the command table */ + char outseq[ 8] ; /* output buffer for keystroke sequence */ /* prompt the user to type in a key to bind */ - mlwrite("bind-to-key: "); + mloutstr( "bind-to-key: ") ; /* get the function name to bind it to */ - nbind_p nbp = getname() ; - if( nbp == NULL) /* abort */ - return FALSE ; + nbind_p nbp = getname() ; + if( nbp == NULL) /* abort */ + return FALSE ; fnp_t kfunc = nbp->n_func ; if( kfunc == NULL) - return cmdfail( "(No such function)") ; + return mloutfail( "(No such function)") ; - mlwrite( "bind-to-key %s: ", bind_name( nbp)) ; + mloutfmt( "bind-to-key %s: ", bind_name( nbp)) ; /* get the command sequence to bind */ - boolean prefix_f = (kfunc == metafn) || (kfunc == cex) || - (kfunc == unarg) || (kfunc == ctrlg) ; + boolean prefix_f = (kfunc == metafn) || (kfunc == cex) || + (kfunc == unarg) || (kfunc == ctrlg) ; int c = getckey( prefix_f) ; - if( c == ~0) - return invalidkey() ; + if( c == ~0) + return invalidkey() ; /* change it to something we can print as well */ /* and dump it out */ ostring( cmdstr( c, outseq)) ; /* key sequence can't be an active prefix key */ - if( c == metac || c == ctlxc || c == reptc || c == abortc) { - if( (c == metac && kfunc == metafn) - || (c == ctlxc && kfunc == cex) - || (c == reptc && kfunc == unarg) - || (c == abortc && kfunc == ctrlg)) - return TRUE ; + if( c == metac || c == ctlxc || c == reptc || c == abortc) { + if( (c == metac && kfunc == metafn) + || (c == ctlxc && kfunc == cex) + || (c == reptc && kfunc == unarg) + || (c == abortc && kfunc == ctrlg)) + return TRUE ; /* be silent if keep current */ - return cmdfail( "(Can't bind to active prefix)") ; - } + return mloutfail( "(Can't bind to active prefix)") ; + } /* if the function is a prefix key */ - if( prefix_f) { + if( prefix_f) { /* remove existing binding for the prefix */ for( ktp = keytab ; ktp->k_code != 0 ; ktp++) if( ktp->k_nbp == nbp) { - delkeybinding( ktp->k_code) ; - break ; - } + delkeybinding( ktp->k_code) ; + break ; + } /* set the appropriate global prefix variable */ if( kfunc == metafn) @@ -164,49 +160,47 @@ BINDABLE( bindtokey) { abortc = c ; } - ktp = setkeybinding( c, nbp) ; - if( ktp->k_code == 0) - return cmdfail( "Binding table FULL!") ; + ktp = setkeybinding( c, nbp) ; + if( ktp->k_code == 0) + return mloutfail( "Binding table FULL!") ; - return TRUE ; + return TRUE ; } -/* - * unbindkey: + +/* unbindkey: * delete a key from the key binding table * * int f, n; command arguments [IGNORED] */ BINDABLE( unbindkey) { - char outseq[ 80] ; /* output buffer for keystroke sequence */ + char outseq[ 8] ; /* output buffer for keystroke sequence */ /* prompt the user to type in a key to unbind */ - mlwrite( "unbind-key: ") ; + mloutstr( "unbind-key: ") ; /* get the command sequence to unbind */ - int c = getckey( FALSE) ; /* get a command sequence */ - if( c == ~0) - return invalidkey() ; + int c = getckey( FALSE) ; /* get a command sequence */ + if( c == ~0) + return invalidkey() ; /* change it to something we can print as well */ /* and dump it out */ ostring( cmdstr( c, outseq)) ; /* prefix key sequence can't be undound, just redefined */ - if( c == reptc || c == abortc) - return cmdfail( "(Can't unbind prefix)") ; + if( c == reptc || c == abortc) + return mloutfail( "(Can't unbind prefix)") ; /* if it isn't bound, bitch */ - if( delkeybinding( c) == FALSE) { - mlwrite( "(Key not bound)") ; - return FALSE ; - } - - return TRUE ; + if( delkeybinding( c) == FALSE) + return mloutfail( "(Key not bound)") ; + + return TRUE ; } -/* - * does source include sub? + +/* does source include sub? * * char *source; string to search in * char *sub; substring to look for @@ -214,8 +208,8 @@ BINDABLE( unbindkey) { static boolean strinc( const char *source, const char *sub) { /* for each character in the source string */ for( ; *source ; source++) { - const char *nxtsp ; /* next ptr into source */ - const char *tp ; /* ptr into substring */ + const char *nxtsp ; /* next ptr into source */ + const char *tp ; /* ptr into substring */ nxtsp = source; @@ -232,6 +226,7 @@ static boolean strinc( const char *source, const char *sub) { return FALSE ; } + /* describe bindings * bring up a fake buffer and list the key bindings * into it with view mode @@ -240,168 +235,166 @@ BINDABLE( desbind) { return buildlist( "") ; } + /* Apropos (List functions that match a substring) */ BINDABLE( apro) { - char *mstring ; /* string to match cmd names to */ + char *mstring ; /* string to match cmd names to */ int status = newmlarg( &mstring, "apropos: ", 0) ; - if( status == TRUE) { - status = buildlist( mstring) ; - free( mstring) ; - } else if( status == FALSE) - status = buildlist( "") ; /* build list of all commands */ + if( status == TRUE) { + status = buildlist( mstring) ; + free( mstring) ; + } else if( status == FALSE) + status = buildlist( "") ; /* build list of all commands */ return status ; } -/* - * build a binding list (limited or full) + +/* build a binding list (limited or full) * * char *mstring; match string if a partial list, "" matches all */ static int buildlist( char *mstring) { - struct window *wp; /* scanning pointer to windows */ - kbind_p ktp; /* pointer into the command table */ - nbind_p nptr;/* pointer into the name binding table */ - struct buffer *bp; /* buffer to put binding list into */ - char outseq[80]; /* output buffer for keystroke sequence */ +#define PADDING 28 + char outseq[ PADDING + 8] ; /* output buffer for command + keystroke */ - /* split the current window to make room for the binding list */ - if( wheadp->w_wndp == NULL /* One window */ - && splitwind( FALSE, 1) == FALSE) /* Split it */ - return FALSE; +/* split the current window to make room for the binding list */ + if( wheadp->w_wndp == NULL /* One window */ + && splitwind( FALSE, 1) == FALSE) /* Split it */ + return FALSE ; - /* and get a buffer for it */ - bp = bfind("*Binding list*", TRUE, 0); +/* and get a buffer for it */ + buffer_p bp = bfind( "*Binding list*", TRUE, 0) ; if( bp == NULL || bclear( bp) == FALSE) - return cmdfail( "Can't display binding list") ; + return mloutfail( "Can't display binding list") ; - /* let us know this is in progress */ - mlwrite("(Building binding list)"); +/* let us know this is in progress */ + mloutstr( "(Building binding list)") ; - /* disconnect the current buffer */ - if (--curbp->b_nwnd == 0) { /* Last use. */ - curbp->b_dotp = curwp->w_dotp; - curbp->b_doto = curwp->w_doto; - curbp->b_markp = curwp->w_markp; - curbp->b_marko = curwp->w_marko; +/* disconnect the current buffer */ + if( --curbp->b_nwnd == 0) { /* Last use. */ + curbp->b_dotp = curwp->w_dotp ; + curbp->b_doto = curwp->w_doto ; + curbp->b_markp = curwp->w_markp ; + curbp->b_marko = curwp->w_marko ; } - /* connect the current window to this buffer */ - curbp = bp; /* make this buffer current in current window */ - bp->b_mode = 0; /* no modes active in binding list */ - bp->b_nwnd++; /* mark us as more in use */ - wp = curwp; - wp->w_bufp = bp; - wp->w_linep = bp->b_linep; - wp->w_flag = WFHARD | WFFORCE; - wp->w_dotp = bp->b_dotp; - wp->w_doto = bp->b_doto; - wp->w_markp = NULL; - wp->w_marko = 0; +/* connect the current window to this buffer */ + curbp = bp ; /* make this buffer current in current window */ + bp->b_mode = 0 ; /* no modes active in binding list */ + bp->b_nwnd++ ; /* mark us as more in use */ + window_p wp = curwp ; + wp->w_bufp = bp ; + wp->w_linep = bp->b_linep ; + wp->w_flag = WFHARD | WFFORCE ; + wp->w_dotp = bp->b_dotp ; + wp->w_doto = bp->b_doto ; + wp->w_markp = NULL ; + wp->w_marko = 0 ; - /* build the contents of this window, inserting it line by line */ - for( nptr = names ; nptr->n_func != NULL ; nptr++) { - int cpos ; /* current position to use in outseq */ +/* build the contents of this window, inserting it line by line */ + for( nbind_p nptr = names ; nptr->n_func != NULL ; nptr++) { + int cpos ; /* current position to use in outseq */ /* if we are executing an apropos command..... */ /* and current string doesn't include the search string */ if( *mstring && strinc( bind_name( nptr), mstring) == FALSE) - continue ; + continue ; /* add in the command name */ mystrscpy( outseq, bind_name( nptr), sizeof outseq) ; - cpos = strlen(outseq); + cpos = strlen( outseq) ; /* search down any keys bound to this */ - for( ktp = keytab ; ktp->k_code != 0 ; ktp++) { + for( kbind_p ktp = keytab ; ktp->k_code != 0 ; ktp++) { if( ktp->k_nbp == nptr) { - /* padd out some spaces */ - while (cpos < 28) - outseq[cpos++] = ' '; + /* padd out some spaces */ + while( cpos < PADDING) + outseq[ cpos++] = ' ' ; - /* add in the command sequence */ - cmdstr(ktp->k_code, &outseq[cpos]); - strcat(outseq, "\n"); + /* add in the command sequence */ + cmdstr( ktp->k_code, &outseq[ cpos]) ; + strcat( outseq, "\n") ; - /* and add it as a line into the buffer */ - if (linstr(outseq) != TRUE) - return FALSE; + /* and add it as a line into the buffer */ + if( linstr( outseq) != TRUE) + return FALSE ; - cpos = 0; /* and clear the line */ + cpos = 0 ; /* and clear the line */ } } /* if no key was bound, we need to dump it anyway */ - if (cpos > 0) { - outseq[cpos++] = '\n'; - outseq[cpos] = 0; - if (linstr(outseq) != TRUE) - return FALSE; + if( cpos > 0) { + outseq[ cpos++] = '\n'; + outseq[ cpos] = 0; + if( linstr( outseq) != TRUE) + return FALSE ; } } - bp->b_mode |= MDVIEW; /* put this buffer view mode */ - bp->b_flag &= ~BFCHG; /* don't flag this as a change */ - wp->w_dotp = lforw(bp->b_linep); /* back to the beginning */ - wp->w_doto = 0; - upmode() ; /* and update ALL mode lines */ - mlwrite(""); /* clear the mode line */ - return TRUE; + bp->b_mode |= MDVIEW ; /* put this buffer view mode */ + bp->b_flag &= ~BFCHG ; /* don't flag this as a change */ + wp->w_dotp = lforw( bp->b_linep) ; /* back to the beginning */ + wp->w_doto = 0 ; + upmode() ; /* and update ALL mode lines */ + mloutstr( "") ; /* clear the mode line */ + return TRUE ; } -/* - * get a command key sequence from the keyboard + +/* get a command key sequence from the keyboard * * int mflag; going for a meta sequence? * returns ~0 on failure */ static unsigned int getckey( int mflag) { - unsigned int c ; /* character fetched */ + unsigned int c ; /* character fetched */ /* check to see if we are executing a command line */ if( clexec) { - char *tok = getnewtokval() ; /* get the next token */ - if( tok == NULL) - c = ~0 ; /* return invalid key on failure */ - else { - c = stock( tok) ; - free( tok) ; - } - } else { /* or the normal way */ - if( mflag) - c = get1key() ; - else - c = getcmd() ; - } + char *tok = getnewtokval() ; /* get the next token */ + if( tok == NULL) + c = ~0 ; /* return invalid key on failure */ + else { + c = stock( tok) ; + free( tok) ; + } + } else { /* or the normal way */ + if( mflag) + c = get1key() ; + else + c = getcmd() ; + } - return c ; + return c ; } -/* - * execute the startup file + +/* execute the startup file * * char *fname; name of startup file (null if default) */ int startup( const char *fname) { - if( !fname || *fname == 0) /* use default if empty parameter */ - fname = rcfname ; + if( !fname || *fname == 0) /* use default if empty parameter */ + fname = rcfname ; - fname = flook( fname, TRUE) ; /* look up the startup file */ - if( fname == NULL) /* if it isn't around, don't sweat it */ - return TRUE ; + fname = flook( fname, TRUE) ; /* look up the startup file */ + if( fname == NULL) /* if it isn't around, don't sweat it */ + return TRUE ; - return dofile( fname) ; /* otherwise, execute the sucker */ + return dofile( fname) ; /* otherwise, execute the sucker */ } -/* - * change a key command to a string we can print out + +/* change a key command to a string we can print out * * int c; sequence to translate * char *seq; destination string for sequence */ static char *cmdstr( unsigned c, char *seq) { - char *ptr = seq ; /* pointer into current position in sequence */ + char *ptr = seq ; /* pointer into current position in sequence */ /* apply meta sequence if needed */ if( c & META) { @@ -411,8 +404,8 @@ static char *cmdstr( unsigned c, char *seq) { /* apply ^X sequence if needed */ if( c & CTLX) { - if( ctlxc & CTRL) - *ptr++ = '^' ; + if( ctlxc & CTRL) + *ptr++ = '^' ; *ptr++ = ctlxc & ~PRFXMASK ; } @@ -428,22 +421,24 @@ static char *cmdstr( unsigned c, char *seq) { } /* and output the final sequence */ - ptr += unicode_to_utf8( c & ~PRFXMASK, ptr) ; - *ptr = 0 ; /* terminate the string */ - return seq ; + ptr += unicode_to_utf8( c & ~PRFXMASK, ptr) ; + *ptr = 0 ; /* terminate the string */ + return seq ; } + static const char *getfname( unsigned keycode, const char *failmsg) { /* takes a key code and gets the name of the function bound to it */ - kbind_p kbp = getkeybinding( keycode) ; - if( kbp->k_code == 0) - return failmsg ; + kbind_p kbp = getkeybinding( keycode) ; + if( kbp->k_code == 0) + return failmsg ; - const char *found = bind_name( kbp->k_nbp) ; - assert( *found) ; - return found ; + const char *found = bind_name( kbp->k_nbp) ; + assert( *found) ; + return found ; } + /* stock: * String key name TO Command Key * @@ -477,16 +472,16 @@ static unsigned int stock( char *keyname) { } /* only one character left to parse */ - if( !*keyname || keyname[1]) - return ~0 ; + if( !*keyname || keyname[1]) + return ~0 ; /* only way to redefine ^X is by quoting binary value */ if( *keyname < 32 || *keyname == 0x7F) { c |= CTRL ; *keyname ^= 0x40 ; - } else if( c && !(c & SPEC) - && *keyname >= 'a' && *keyname <= 'z') - /* make sure we are not lower case (not with function keys) */ + } else if( c && !(c & SPEC) + && *keyname >= 'a' && *keyname <= 'z') + /* make sure we are not lower case (not with function keys) */ *keyname -= 32 ; /* the final sequence... */ @@ -494,19 +489,19 @@ static unsigned int stock( char *keyname) { return c ; } -/* - * string key name to binding name.... + +/* string key name to binding name.... * * char *skey; name of key to get binding for */ const char *transbind( char *skey) { - static const char failmsg[] = "ERROR" ; + static const char failmsg[] = "ERROR" ; - unsigned c = stock( skey) ; - if( c == (unsigned) ~0) - return failmsg ; - else - return getfname( c, failmsg) ; + unsigned c = stock( skey) ; + if( c == (unsigned) ~0) + return failmsg ; + else + return getfname( c, failmsg) ; } /* end of bind.c */ diff --git a/bind.h b/bind.h index dcb1ad6..e58fbc9 100644 --- a/bind.h +++ b/bind.h @@ -1,3 +1,5 @@ +/* bind.h -- bindable functions dealing with name and key bindings */ + #ifndef _BIND_H_ #define _BIND_H_ @@ -14,6 +16,8 @@ BINDABLE( unbindkey) ; int startup( const char *fname) ; /* find a key to function association in the key to function mapping table */ -const char *transbind( char *skey) ; /* by string representation of key */ +const char *transbind( char *skey) ; /* by string representation of key */ #endif + +/* end of bind.h */ From 665d9ca1da8c5e0290958017bc36ad89fd5b702f Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 9 Aug 2021 15:45:01 +0800 Subject: [PATCH 31/37] bindable: code review and minor refactoring. --- bindable.c | 191 ++++++++++++++++++++++++----------------------------- bindable.h | 9 +++ 2 files changed, 95 insertions(+), 105 deletions(-) diff --git a/bindable.c b/bindable.c index 12a2706..7f42f7f 100644 --- a/bindable.c +++ b/bindable.c @@ -3,146 +3,127 @@ #include - #include "defines.h" #include "buffer.h" -#include "display.h" -#include "estruct.h" +#include "display.h" /* vttidy() */ #include "file.h" #include "input.h" #include "lock.h" #include "mlout.h" #include "terminal.h" -/* - * Fancy quit command, as implemented by Norm. If the any buffer has - * changed do a write on that buffer and exit emacs, otherwise simply exit. + +/* Fancy quit command, as implemented by Norm. If any buffer has changed + do a write on that buffer and exit emacs, otherwise simply exit. */ -int quickexit(int f, int n) -{ - struct buffer *bp; /* scanning pointer to buffers */ - struct buffer *oldcb; /* original current buffer */ - int status; +BINDABLE( quickexit) { + buffer_p oldcb = curbp ; /* save in case we fail */ + for( buffer_p bp = bheadp ; bp != NULL ; bp = bp->b_bufp) { + if( (bp->b_flag & (BFCHG | BFTRUNC | BFINVS)) == BFCHG) { + /* Changed, Not truncated and real buffer */ + curbp = bp ; /* make that buffer cur */ + mloutfmt( "(Saving %s)", bp->b_fname) ; + int status = filesave( f, n) ; + if( status != TRUE) { + curbp = oldcb ; /* restore curbp */ + return status ; + } + } + } - oldcb = curbp; /* save in case we fail */ - - bp = bheadp; - while (bp != NULL) { - if ((bp->b_flag & BFCHG) != 0 /* Changed. */ - && (bp->b_flag & BFTRUNC) == 0 /* Not truncated P.K. */ - && (bp->b_flag & BFINVS) == 0) { /* Real. */ - curbp = bp; /* make that buffer cur */ - mloutfmt( "(Saving %s)", bp->b_fname) ; -#if PKCODE -#else - mloutstr( "\n") ; -#endif - if ((status = filesave(f, n)) != TRUE) { - curbp = oldcb; /* restore curbp */ - return status; - } - } - bp = bp->b_bufp; /* on to the next buffer */ - } - quit(f, n); /* conditionally quit */ - return TRUE; + return quit( f, n) ; /* conditionally quit */ } -/* - * Quit command. If an argument, always quit. Otherwise confirm if a buffer + +/* Quit command. If an argument, always quit. Otherwise confirm if a buffer * has been changed and not written out. Normally bound to "C-X C-C". */ -int quit(int f, int n) -{ - int s; +BINDABLE( quit) { + int s ; /* status of user query */ - if (f != FALSE /* Argument forces it. */ - || anycb() == FALSE /* All buffers clean. */ - /* User says it's OK. */ - || (s = - mlyesno("Modified buffers exist. Leave anyway")) == TRUE) { -#if (FILOCK && BSD) || SVR4 - if (lockrel() != TRUE) { - TTputc('\n'); - TTputc('\r'); - TTclose(); - TTkclose(); - exit( EXIT_FAILURE) ; - } + if( f != FALSE /* Argument forces it. */ + || anycb() == FALSE /* All buffers clean. */ + /* User says it's OK. */ + || (s = mlyesno( "Modified buffers exist. Leave anyway")) == TRUE) { +#if (FILOCK && BSD) || SVR4 + if( lockrel() != TRUE) { + TTputc('\n') ; + TTputc('\r') ; + TTclose() ; + TTkclose() ; + exit( EXIT_FAILURE) ; + } #endif - vttidy(); - if (f) - exit(n); - else - exit( EXIT_SUCCESS) ; - } - mloutstr( "") ; - return s; + vttidy() ; + if( f) + exit( n) ; + else + exit( EXIT_SUCCESS) ; + } + + mloutstr( "") ; + return s ; } -/* - * Begin a keyboard macro. + +/* Begin a keyboard macro. * Error if not at the top level in keyboard processing. Set up variables and * return. */ -int ctlxlp(int f, int n) -{ - if (kbdmode != STOP) { - mloutstr( "%Macro already active") ; - return FALSE; - } - mloutstr( "(Start macro)") ; - kbdptr = &kbdm[0]; - kbdend = kbdptr; - kbdmode = RECORD; - return TRUE; +BINDABLE( ctlxlp) { + if( kbdmode != STOP) + return mloutfail( "%Macro already active") ; + + mloutstr( "(Start macro)") ; + kbdptr = kbdm ; + kbdend = kbdptr ; + kbdmode = RECORD ; + return TRUE ; } -/* - * End keyboard macro. Check for the same limit conditions as the above + +/* End keyboard macro. Check for the same limit conditions as the above * routine. Set up the variables and return to the caller. */ -int ctlxrp(int f, int n) -{ - if (kbdmode == STOP) { - mloutstr( "%Macro not active") ; - return FALSE; - } - if (kbdmode == RECORD) { - mloutstr( "(End macro)") ; - kbdmode = STOP; - } - return TRUE; +BINDABLE( ctlxrp) { + if( kbdmode == STOP) + return mloutfail( "%Macro not active") ; + + if (kbdmode == RECORD) { + mloutstr( "(End macro)") ; + kbdmode = STOP; + } + + return TRUE ; } -/* - * Execute a macro. + +/* Execute a macro. * The command argument is the number of times to loop. Quit as soon as a * command gets an error. Return TRUE if all ok, else FALSE. */ -int ctlxe(int f, int n) -{ - if (kbdmode != STOP) { - mloutstr( "%Macro already active") ; - return FALSE; - } - if (n <= 0) - return TRUE; - kbdrep = n; /* remember how many times to execute */ - kbdmode = PLAY; /* start us in play mode */ - kbdptr = &kbdm[0]; /* at the beginning */ - return TRUE; +BINDABLE( ctlxe) { + if( kbdmode != STOP) + return mloutfail( "%Macro already active") ; + + if( n <= 0) + return TRUE ; + + kbdrep = n ; /* remember how many times to execute */ + kbdmode = PLAY ; /* start us in play mode */ + kbdptr = kbdm ; /* at the beginning */ + return TRUE ; } -/* - * abort: + +/* abort: * Beep the beeper. Kill off any keyboard macro, etc., that is in progress. * Sometimes called as a routine, to do general aborting of stuff. */ -int ctrlg( int f, int n) { - kbdmode = STOP ; - mloutfmt( "%B(Aborted)") ; - return ABORT ; +BINDABLE( ctrlg) { + kbdmode = STOP ; + mloutfmt( "%B(Aborted)") ; + return ABORT ; } /* end of bindable.c */ diff --git a/bindable.h b/bindable.h index fb96884..30b5b3b 100644 --- a/bindable.h +++ b/bindable.h @@ -1,3 +1,8 @@ +/* bindable.h -- misc bindable functions */ + +#ifndef _BINDABLE_H_ +#define _BINDABLE_H_ + #include "names.h" /* functions that can be bound to keys or procedure names */ @@ -7,3 +12,7 @@ BINDABLE( ctlxlp) ; BINDABLE( ctlxrp) ; BINDABLE( ctlxe) ; BINDABLE( ctrlg) ; + +#endif + +/* end of bindable.h */ From 50b727bf7ffe7d6152c067c989e193882d3ac409 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Wed, 11 Aug 2021 17:02:19 +0800 Subject: [PATCH 32/37] Bindable functions take a boolean as flag. Emphasize which one always return TRUE. Use mloutfail() to introduce consistency when a function fails with error message. --- basic.c | 91 +++--- basic.h | 28 +- bind.c | 28 +- bindable.c | 6 +- bindable.h | 14 +- buffer.c | 875 ++++++++++++++++++++++++++--------------------------- buffer.h | 120 ++++---- display.c | 50 ++- display.h | 28 +- eval.c | 38 +-- eval.h | 28 +- exec.c | 815 +++++++++++++++++++++++-------------------------- exec.h | 114 ++++--- file.c | 35 +-- input.c | 52 ++-- isearch.c | 43 +-- isearch.h | 10 +- line.c | 12 +- mlout.c | 5 + mlout.h | 3 + names.c | 192 ++++++------ names.h | 19 +- random.c | 80 ++--- region.c | 13 +- search.c | 50 ++- spawn.c | 39 ++- util.c | 25 +- util.h | 6 +- window.c | 317 +++++++++---------- window.h | 86 +++--- word.c | 160 +++++----- 31 files changed, 1593 insertions(+), 1789 deletions(-) diff --git a/basic.c b/basic.c index af3ea35..f6e3306 100644 --- a/basic.c +++ b/basic.c @@ -22,7 +22,7 @@ #define CVMVAS 1 /* arguments to page forward/back in pages */ int overlap = 0 ; /* $overlap: line overlap in forw/back page */ -int curgoal ; /* $target: Goal for C-P, C-N */ +int curgoal ; /* $target: column goal for C-P, C-N */ /* This routine, given a pointer to a struct line, and the current cursor @@ -60,14 +60,14 @@ static unsigned getgoal( line_p dlp) { /* Move the cursor to the beginning of the current line of active window. */ -boolean gotobol( int f, int n) { +TBINDABLE( gotobol) { curwp->w_doto = 0 ; return TRUE ; } /* Move the cursor to the end of the current line of active window. */ -boolean gotoeol( int f, int n) { +TBINDABLE( gotoeol) { curwp->w_doto = llength( curwp->w_dotp) ; return TRUE ; } @@ -77,7 +77,7 @@ boolean gotoeol( int f, int n) { considered to be hard motion; it really isn't if the original value of dot is the same as the new value of dot. Normally bound to "M-<". */ -boolean gotobob( int f, int n) { +TBINDABLE( gotobob) { curwp->w_dotp = lforw( curbp->b_linep) ; curwp->w_doto = 0 ; curwp->w_flag |= WFHARD ; @@ -89,7 +89,7 @@ boolean gotobob( int f, int n) { (ZJ). The standard screen code does most of the hard parts of update. Bound to "M->". */ -boolean gotoeob( int f, int n) { +TBINDABLE( gotoeob) { curwp->w_dotp = curbp->b_linep ; curwp->w_doto = 0 ; curwp->w_flag |= WFHARD ; @@ -102,14 +102,8 @@ boolean gotoeob( int f, int n) { command controls how the goal column is set. Bound to "C-N". No errors are possible. */ -boolean forwline( int f, int n) { +BBINDABLE( forwline) { assert( f == TRUE || n == 1) ; - if( n < 0) - return backline( f, -n) ; - -/* if we are on the last line as we start....fail the command */ - if( n && curwp->w_dotp == curbp->b_linep) - return FALSE ; /* if the last command was not a line move, reset the goal column */ if( (lastflag & CFCPCN) == 0) @@ -119,16 +113,26 @@ boolean forwline( int f, int n) { thisflag |= CFCPCN ; /* and move the point down */ - line_p dlp = curwp->w_dotp ; - while( n && dlp != curbp->b_linep) { - dlp = lforw( dlp) ; - n -= 1 ; + if( n) { + line_p dlp = curwp->w_dotp ; + if( n > 0) + while( n && dlp != curbp->b_linep) { + dlp = lforw( dlp) ; + n -= 1 ; + } + else { + while( n && lback( dlp) != curbp->b_linep) { + dlp = lback( dlp) ; + n += 1 ; + } + } + + /* resetting the current position */ + curwp->w_dotp = dlp ; + curwp->w_doto = getgoal( dlp) ; + curwp->w_flag |= WFMOVE ; } -/* reseting the current position */ - curwp->w_dotp = dlp ; - curwp->w_doto = getgoal( dlp) ; - curwp->w_flag |= WFMOVE ; return (n == 0) ? TRUE : FALSE ; } @@ -138,33 +142,10 @@ boolean forwline( int f, int n) { your alternate. Figure out the new line and call "movedot" to perform the motion. No errors are possible. Bound to "C-P". */ -boolean backline( int f, int n) { - if( n < 0) - return forwline( f, -n) ; +BBINDABLE( backline) { + assert( f == TRUE || n == 1) ; -/* if we are on the first line as we start....fail the command */ - if( n && lback( curwp->w_dotp) == curbp->b_linep) - return FALSE ; - -/* if the last command was not a line move, reset the goal column */ - if( (lastflag & CFCPCN) == 0) - curgoal = getccol( FALSE) ; - -/* flag this command as a line move */ - thisflag |= CFCPCN ; - -/* and move the point up */ - line_p dlp = curwp->w_dotp ; - while( n && lback( dlp) != curbp->b_linep) { - dlp = lback( dlp) ; - n -= 1 ; - } - -/* reseting the current position */ - curwp->w_dotp = dlp ; - curwp->w_doto = getgoal( dlp) ; - curwp->w_flag |= WFMOVE ; - return (n == 0) ? TRUE : FALSE ; + return forwline( TRUE, -n) ; } @@ -209,7 +190,7 @@ BINDABLE( gotoline) { Because this zaps the top line in the display window, we have to do a hard update. */ -boolean forwpage( int f, int n) { +TBINDABLE( forwpage) { line_p lp ; if( f == FALSE) { @@ -226,23 +207,21 @@ boolean forwpage( int f, int n) { if (n <= 0) /* Forget the overlap. */ n = 1; /* If tiny window. */ } else if( n < 0) - return backpage(f, -n); + return backpage( f, -n) ; #if CVMVAS else /* Convert from pages. */ n *= curwp->w_ntrows; /* To lines. */ #endif -/* lp = curwp->w_linep; */ lp = curwp->w_dotp ; while( n && lp != curbp->b_linep) { lp = lforw( lp) ; n -= 1 ; } -/* curwp->w_linep = lp; */ - curwp->w_dotp = lp; - curwp->w_doto = 0; - reposition( TRUE, 0) ; + curwp->w_dotp = lp ; + curwp->w_doto = 0 ; + reposition( TRUE, 0) ; /* center at dot, always succeed */ #if SCROLLCODE curwp->w_flag |= WFHARD | WFKILLS; @@ -258,7 +237,7 @@ boolean forwpage( int f, int n) { ITS EMACS manual. Bound to "M-V". We do a hard update for exactly the same reason. */ -boolean backpage( int f, int n) { +TBINDABLE( backpage) { line_p lp ; if( f == FALSE) { /* interactive, default n = 1 supplied */ @@ -313,7 +292,7 @@ boolean backpage( int f, int n) { /* Set the mark in the current window to the value of "." in the window. No errors are possible. Bound to M-. set-mark. */ -boolean setmark( int f, int n) { +TBINDABLE( setmark) { curwp->w_markp = curwp->w_dotp ; curwp->w_marko = curwp->w_doto ; mloutstr( "(Mark set)") ; @@ -324,7 +303,7 @@ boolean setmark( int f, int n) { /* Swap the values of "." and "mark" in the current window. If no mark as been previously set, set it. Bound to C-X C-X exchange-point-and-mark. */ -boolean swapmark( int f, int n) { +TBINDABLE( swapmark) { line_p odotp = curwp->w_dotp ; int odoto = curwp->w_doto ; if( curwp->w_markp) { diff --git a/basic.h b/basic.h index 5c6c8b1..4c58267 100644 --- a/basic.h +++ b/basic.h @@ -1,9 +1,8 @@ /* basic.h -- basic commands for cursor movement in active window */ - #ifndef _BASIC_H_ -#define _BASIC_H_ +# define _BASIC_H_ -#include "names.h" +# include "names.h" /* $overlap is the size of the line overlap when kbd calls page forw/back if 0, page will move by 2/3 of the window size (1/3 page overlap) @@ -17,18 +16,17 @@ extern int curgoal ; /* $target: Goal for C-P previous-line, C-N next-line */ /* Bindable functions */ -boolean gotobol( int f, int n) ; -boolean gotoeol( int f, int n) ; -BINDABLE( gotoline) ; -boolean gotobob( int f, int n) ; -boolean gotoeob( int f, int n) ; -boolean forwline( int f, int n) ; -boolean backline( int f, int n) ; -boolean forwpage( int f, int n) ; -boolean backpage( int f, int n) ; -boolean setmark( int f, int n) ; -boolean swapmark( int f, int n) ; +BBINDABLE( backline) ; +TBINDABLE( backpage) ; +BBINDABLE( forwline) ; +TBINDABLE( forwpage) ; +TBINDABLE( gotobob) ; +TBINDABLE( gotobol) ; +TBINDABLE( gotoeob) ; +TBINDABLE( gotoeol) ; + BINDABLE( gotoline) ; +TBINDABLE( setmark) ; +TBINDABLE( swapmark) ; #endif - /* end of basic.h */ diff --git a/bind.c b/bind.c index 2975702..1d8e5de 100644 --- a/bind.c +++ b/bind.c @@ -119,8 +119,8 @@ BINDABLE( bindtokey) { mloutfmt( "bind-to-key %s: ", bind_name( nbp)) ; /* get the command sequence to bind */ - boolean prefix_f = (kfunc == metafn) || (kfunc == cex) || - (kfunc == unarg) || (kfunc == ctrlg) ; + boolean prefix_f = (kfunc == (fnp_t) metafn) || (kfunc == (fnp_t) cex) || + (kfunc == (fnp_t) unarg) || (kfunc == (fnp_t) ctrlg) ; int c = getckey( prefix_f) ; if( c == ~0) return invalidkey() ; @@ -131,10 +131,10 @@ BINDABLE( bindtokey) { /* key sequence can't be an active prefix key */ if( c == metac || c == ctlxc || c == reptc || c == abortc) { - if( (c == metac && kfunc == metafn) - || (c == ctlxc && kfunc == cex) - || (c == reptc && kfunc == unarg) - || (c == abortc && kfunc == ctrlg)) + if( (c == metac && kfunc == (fnp_t) metafn) + || (c == ctlxc && kfunc == (fnp_t) cex) + || (c == reptc && kfunc == (fnp_t) unarg) + || (c == abortc && kfunc == (fnp_t) ctrlg)) return TRUE ; /* be silent if keep current */ return mloutfail( "(Can't bind to active prefix)") ; @@ -150,13 +150,13 @@ BINDABLE( bindtokey) { } /* set the appropriate global prefix variable */ - if( kfunc == metafn) + if( kfunc == (fnp_t) metafn) metac = c ; - else if( kfunc == cex) + else if( kfunc == (fnp_t) cex) ctlxc = c ; - if( kfunc == unarg) + if( kfunc == (fnp_t) unarg) reptc = c ; - if( kfunc == ctrlg) + if( kfunc == (fnp_t) ctrlg) abortc = c ; } @@ -404,14 +404,14 @@ static char *cmdstr( unsigned c, char *seq) { /* apply ^X sequence if needed */ if( c & CTLX) { - if( ctlxc & CTRL) + if( ctlxc & CTL_) *ptr++ = '^' ; *ptr++ = ctlxc & ~PRFXMASK ; } /* apply control sequence if needed */ - if( c & CTRL) + if( c & CTL_) *ptr++ = '^' ; /* apply SPEC sequence if needed */ @@ -461,7 +461,7 @@ static unsigned int stock( char *keyname) { /* a control char? */ if( *keyname == '^' && keyname[ 1] != 0) { - c |= CTRL ; + c |= CTL_ ; ++keyname ; } @@ -477,7 +477,7 @@ static unsigned int stock( char *keyname) { /* only way to redefine ^X is by quoting binary value */ if( *keyname < 32 || *keyname == 0x7F) { - c |= CTRL ; + c |= CTL_ ; *keyname ^= 0x40 ; } else if( c && !(c & SPEC) && *keyname >= 'a' && *keyname <= 'z') diff --git a/bindable.c b/bindable.c index 7f42f7f..c6a96cf 100644 --- a/bindable.c +++ b/bindable.c @@ -70,7 +70,7 @@ BINDABLE( quit) { * Error if not at the top level in keyboard processing. Set up variables and * return. */ -BINDABLE( ctlxlp) { +BBINDABLE( ctlxlp) { if( kbdmode != STOP) return mloutfail( "%Macro already active") ; @@ -85,7 +85,7 @@ BINDABLE( ctlxlp) { /* End keyboard macro. Check for the same limit conditions as the above * routine. Set up the variables and return to the caller. */ -BINDABLE( ctlxrp) { +BBINDABLE( ctlxrp) { if( kbdmode == STOP) return mloutfail( "%Macro not active") ; @@ -102,7 +102,7 @@ BINDABLE( ctlxrp) { * The command argument is the number of times to loop. Quit as soon as a * command gets an error. Return TRUE if all ok, else FALSE. */ -BINDABLE( ctlxe) { +BBINDABLE( ctlxe) { if( kbdmode != STOP) return mloutfail( "%Macro already active") ; diff --git a/bindable.h b/bindable.h index 30b5b3b..21a709f 100644 --- a/bindable.h +++ b/bindable.h @@ -1,18 +1,16 @@ /* bindable.h -- misc bindable functions */ - #ifndef _BINDABLE_H_ #define _BINDABLE_H_ #include "names.h" /* functions that can be bound to keys or procedure names */ -BINDABLE( quickexit) ; -BINDABLE( quit) ; -BINDABLE( ctlxlp) ; -BINDABLE( ctlxrp) ; -BINDABLE( ctlxe) ; -BINDABLE( ctrlg) ; +BBINDABLE( ctlxe) ; +BBINDABLE( ctlxlp) ; +BBINDABLE( ctlxrp) ; + BINDABLE( ctrlg) ; /* ABORT */ + BINDABLE( quickexit) ; + BINDABLE( quit) ; #endif - /* end of bindable.h */ diff --git a/buffer.c b/buffer.c index 0fb7dbb..120fc7b 100644 --- a/buffer.c +++ b/buffer.c @@ -1,18 +1,12 @@ /* buffer.c -- implements buffer.h */ #include "buffer.h" -/* buffer.c - * - * Buffer management. - * Some of the functions are internal, - * and some are actually attached to user - * keys. Like everyone else, they set hints - * for the display system - * - * modified by Petri Kutvonen +/* Buffer management. Some of the functions are internal, and some are actually attached to + user keys. Like everyone else, they set hints for the display system. + + modified by Petri Kutvonen */ -#include #include #include @@ -26,294 +20,277 @@ #include "window.h" -struct buffer *curbp ; /* Current buffer */ -struct buffer *bheadp ; /* Head of list of buffers */ -struct buffer *blistp ; /* Buffer for C-X C-B */ +buffer_p curbp ; /* Current buffer */ +buffer_p bheadp ; /* Head of list of buffers */ +buffer_p blistp ; /* Buffer for C-X C-B */ -const char *modename[] = { /* name of modes */ - "Wrap", "Cmode", "Exact", "View", "Over", - "Magic", - "Asave", "Utf-8", "Dos" +const char *modename[ NUMMODES] = { /* name of modes */ + "Wrap", "Cmode", "Exact", "View", "Over", + "Magic", + "Asave", "Utf-8", "Dos" } ; -int gmode = 0 ; /* global editor mode */ +int gmode = 0 ; /* global editor mode */ static int makelist( int iflag) ; static int addline( char *text) ; static void l_to_a( char *buf, int width, long num) ; -/* - * Attach a buffer to a window. The - * values of dot and mark come from the buffer - * if the use count is 0. Otherwise, they come - * from some other window. +/* Attach a buffer to a window. The values of dot and mark come from the + buffer if the use count is 0. Otherwise, they come from some other + window. */ -int usebuffer( int f, int n) { - struct buffer *bp ; - int status ; - char *bufn ; +BINDABLE( usebuffer) { + char *bufn ; /* Get buffer name */ - status = newmlarg( &bufn, "Use buffer: ", sizeof( bname_t)) ; - if( status != TRUE) - return status ; + int status = newmlarg( &bufn, "select-buffer: ", sizeof( bname_t)) ; + if( status != TRUE) + return status ; /* Find buffer in list */ - bp = bfind( bufn, TRUE, 0) ; - free( bufn) ; - if( bp == NULL) - return FALSE ; + buffer_p bp = bfind( bufn, TRUE, 0) ; + free( bufn) ; + if( bp == NULL) + return FALSE ; /* Switch to buffer */ - return swbuffer( bp) ; + return swbuffer( bp) ; } -/* - * switch to the next buffer in the buffer list + +/* switch to the next buffer in the buffer list * - * int f, n; default flag, numeric argument + * int f, n; default flag, numeric argument */ -int nextbuffer(int f, int n) -{ - struct buffer *bp = NULL; /* eligable buffer to switch to */ - struct buffer *bbp; /* eligable buffer to switch to */ +BINDABLE( nextbuffer) { + buffer_p bp = NULL ; /* eligible buffer to switch to */ - /* make sure the arg is legit */ - if (f == FALSE) - n = 1; - if (n < 1) - return FALSE; + /* make sure the arg is legit */ + if( f == FALSE) + n = 1 ; - bbp = curbp; - while (n-- > 0) { - /* advance to the next buffer */ - bp = bbp->b_bufp; + if( n < 1) + return FALSE ; - /* cycle through the buffers to find an eligable one */ - while (bp == NULL || bp->b_flag & BFINVS) { - if (bp == NULL) - bp = bheadp; - else - bp = bp->b_bufp; + buffer_p bbp = curbp ; /* eligible buffer to switch to */ + while( n-- > 0) { + /* advance to the next buffer */ + bp = bbp->b_bufp ; - /* don't get caught in an infinite loop! */ - if (bp == bbp) - return FALSE; + /* cycle through the buffers to find an eligible one */ + while( bp == NULL || bp->b_flag & BFINVS) { + if (bp == NULL) + bp = bheadp ; + else + bp = bp->b_bufp ; - } + /* don't get caught in an infinite loop! */ + if( bp == bbp) + return FALSE ; + } - bbp = bp; - } + bbp = bp ; + } - return swbuffer(bp); + return swbuffer( bp) ; } -/* - * make buffer BP current - */ -int swbuffer(struct buffer *bp) -{ - struct window *wp; - if (--curbp->b_nwnd == 0) { /* Last use. */ - curbp->b_dotp = curwp->w_dotp; - curbp->b_doto = curwp->w_doto; - curbp->b_markp = curwp->w_markp; - curbp->b_marko = curwp->w_marko; - } - curbp = bp; /* Switch. */ - if (curbp->b_active != TRUE) { /* buffer not active yet */ - /* read it in and activate it */ - readin(curbp->b_fname, TRUE); - curbp->b_dotp = lforw(curbp->b_linep); - curbp->b_doto = 0; - curbp->b_active = TRUE; - curbp->b_mode |= gmode; /* P.K. */ - } - curwp->w_bufp = bp; - curwp->w_linep = bp->b_linep; /* For macros, ignored. */ - curwp->w_flag |= WFMODE | WFFORCE | WFHARD; /* Quite nasty. */ - if (bp->b_nwnd++ == 0) { /* First use. */ - curwp->w_dotp = bp->b_dotp; - curwp->w_doto = bp->b_doto; - curwp->w_markp = bp->b_markp; - curwp->w_marko = bp->b_marko; - cknewwindow(); - return TRUE; - } - wp = wheadp; /* Look for old. */ - while (wp != NULL) { - if (wp != curwp && wp->w_bufp == bp) { - curwp->w_dotp = wp->w_dotp; - curwp->w_doto = wp->w_doto; - curwp->w_markp = wp->w_markp; - curwp->w_marko = wp->w_marko; - break; - } - wp = wp->w_wndp; - } - cknewwindow(); - return TRUE; +/* make buffer BP current + */ +int swbuffer( buffer_p bp) { + window_p wp ; + + if (--curbp->b_nwnd == 0) { /* Last use. */ + curbp->b_dotp = curwp->w_dotp; + curbp->b_doto = curwp->w_doto; + curbp->b_markp = curwp->w_markp; + curbp->b_marko = curwp->w_marko; + } + curbp = bp; /* Switch. */ + if (curbp->b_active != TRUE) { /* buffer not active yet */ + /* read it in and activate it */ + readin(curbp->b_fname, TRUE); + curbp->b_dotp = lforw(curbp->b_linep); + curbp->b_doto = 0; + curbp->b_active = TRUE; + curbp->b_mode |= gmode; /* P.K. */ + } + curwp->w_bufp = bp; + curwp->w_linep = bp->b_linep; /* For macros, ignored. */ + curwp->w_flag |= WFMODE | WFFORCE | WFHARD; /* Quite nasty. */ + if (bp->b_nwnd++ == 0) { /* First use. */ + curwp->w_dotp = bp->b_dotp; + curwp->w_doto = bp->b_doto; + curwp->w_markp = bp->b_markp; + curwp->w_marko = bp->b_marko; + cknewwindow(); + return TRUE; + } + wp = wheadp; /* Look for old. */ + while (wp != NULL) { + if (wp != curwp && wp->w_bufp == bp) { + curwp->w_dotp = wp->w_dotp; + curwp->w_doto = wp->w_doto; + curwp->w_markp = wp->w_markp; + curwp->w_marko = wp->w_marko; + break; + } + wp = wp->w_wndp; + } + cknewwindow(); + return TRUE; } -/* - * Dispose of a buffer, by name. - * Ask for the name. Look it up (don't get too - * upset if it isn't there at all!). Get quite upset - * if the buffer is being displayed. Clear the buffer (ask - * if the buffer has been changed). Then free the header - * line and the buffer header. Bound to "C-X K". + +/* Dispose of a buffer, by name. Ask for the name. Look it up (don't get + too upset if it isn't there at all!). Get quite upset if the buffer is + being displayed. Clear the buffer (ask if the buffer has been changed). + Then free the header line and the buffer header. Bound to "C-X K". */ -int killbuffer( int f, int n) { - struct buffer *bp ; - int status ; - char *bufn ; +BINDABLE( killbuffer) { + buffer_p bp ; + int status ; + char *bufn ; /* Get buffer name */ - status = newmlarg( &bufn, "Kill buffer: ", sizeof( bname_t)) ; - if( status != TRUE) - return status ; + status = newmlarg( &bufn, "delete-buffer: ", sizeof( bname_t)) ; + if( status != TRUE) + return status ; /* Find buffer in list */ - bp = bfind( bufn, FALSE, 0) ; - free( bufn) ; - if( bp == NULL) /* Easy if unknown. */ - return TRUE ; + bp = bfind( bufn, FALSE, 0) ; + free( bufn) ; + if( bp == NULL) /* Easy if unknown. */ + return TRUE ; - if( bp->b_flag & BFINVS) /* Deal with special buffers */ - return TRUE ; /* by doing nothing. */ + if( bp->b_flag & BFINVS) /* Deal with special buffers */ + return TRUE ; /* by doing nothing. */ - return zotbuf( bp) ; + return zotbuf( bp) ; } -/* - * kill the buffer pointed to by bp + +/* kill the buffer pointed to by bp */ -int zotbuf(struct buffer *bp) -{ - struct buffer *bp1; - struct buffer *bp2; - int s; +int zotbuf( buffer_p bp) { + if( bp->b_nwnd != 0) /* Error if on screen. */ + return mloutfail( "Buffer is being displayed") ; - if (bp->b_nwnd != 0) { /* Error if on screen. */ - mloutstr("Buffer is being displayed"); - return FALSE; - } - if ((s = bclear(bp)) != TRUE) /* Blow text away. */ - return s; - free((char *) bp->b_linep); /* Release header line. */ - bp1 = NULL; /* Find the header. */ - bp2 = bheadp; - while (bp2 != bp) { - bp1 = bp2; - bp2 = bp2->b_bufp; - } - bp2 = bp2->b_bufp; /* Next one in chain. */ - if (bp1 == NULL) /* Unlink it. */ - bheadp = bp2; - else - bp1->b_bufp = bp2; - free((char *) bp); /* Release buffer block */ - return TRUE; -} + int s = bclear( bp) ; /* Blow text away. */ + if( s != TRUE) + return s ; -/* - * Rename the current buffer - * - * int f, n; default Flag & Numeric arg - */ -int namebuffer( int f, int n) { - struct buffer *bp ; /* pointer to scan through all buffers */ - int status ; - char *bufn ; /* buffer to hold buffer name */ + free( bp->b_linep) ; /* Release header line. */ -/* prompt for and get the new buffer name */ -ask: - status = newmlarg( &bufn, "Change buffer name to: ", sizeof( bname_t)) ; - if( status != TRUE) - return status ; - -/* and check for duplicates */ - bp = bheadp ; - while( bp != NULL) { - if( bp != curbp) { - /* retry if the names are the same */ - if( strcmp( bufn, bp->b_bname) == 0) { - free( bufn) ; - goto ask ; /* try again */ - } +/* unlink buffer from buffer chain */ + if( bheadp == bp) + bheadp = bp->b_bufp ; + else for( buffer_p prev = bheadp ; prev != NULL ; prev = prev->b_bufp) + if( prev->b_bufp == bp) { + prev->b_bufp = bp->b_bufp ; + break ; } - bp = bp->b_bufp ; /* onward */ - } + free( bp) ; /* Release buffer block */ + return TRUE ; +} + + +/* Rename the current buffer + * + * int f, n; default Flag & Numeric arg + */ +BINDABLE( namebuffer) { + char *bufn ; /* buffer to hold buffer name */ + int status ; + +/* iterate until it gets a unique new buffer name */ + do { + /* prompt for it */ + status = newmlarg( &bufn, "name-buffer: ", sizeof( bname_t)) ; + if( status != TRUE) + return status ; + + /* and check for duplicates */ + for( buffer_p bp = bheadp ; bp != NULL ; bp = bp->b_bufp) { + if( bp != curbp) { /* it's ok to rename buffer to same, so skip */ + /* retry if the names are the same */ + if( strcmp( bufn, bp->b_bname) == 0) { + free( bufn) ; + status = FALSE ; /* try again */ + break ; + } + } + } + } while( !status) ; /* copy buffer name to structure */ - mystrscpy( curbp->b_bname, bufn, sizeof( bname_t)) ; - free( bufn) ; - - curwp->w_flag |= WFMODE ; /* make mode line replot */ - mloutstr( "") ; /* erase message line */ - return TRUE ; + mystrscpy( curbp->b_bname, bufn, sizeof( bname_t)) ; + free( bufn) ; + + curwp->w_flag |= WFMODE ; /* make mode line replot */ + mloutstr( "") ; /* erase message line */ + return TRUE ; } -/* - * List all of the active buffers. First update the special - * buffer that holds the list. Next make sure at least 1 - * window is displaying the buffer list, splitting the screen - * if this is what it takes. Lastly, repaint all of the - * windows that are displaying the list. Bound to "C-X C-B". - * - * A numeric argument forces it to list invisible buffers as - * well. + +/* List all of the active buffers. First update the special buffer that + holds the list. Next make sure at least 1 window is displaying the + buffer list, splitting the screen if this is what it takes. Lastly, + repaint all of the windows that are displaying the list. Bound to "C-X + C-B". + + A numeric argument forces it to list invisible buffers as well. */ -int listbuffers(int f, int n) -{ - struct window *wp; - int s; +BINDABLE( listbuffers) { + window_p wp ; - if ((s = makelist(f)) != TRUE) - return s; - if (blistp->b_nwnd == 0) { /* Not on screen yet. */ - struct buffer *bp ; + int s = makelist( f) ; + if( s != TRUE) + return s ; - if ((wp = wpopup()) == NULL) - return FALSE; - bp = wp->w_bufp; - if (--bp->b_nwnd == 0) { - bp->b_dotp = wp->w_dotp; - bp->b_doto = wp->w_doto; - bp->b_markp = wp->w_markp; - bp->b_marko = wp->w_marko; - } - wp->w_bufp = blistp; - ++blistp->b_nwnd; - } - wp = wheadp; - while (wp != NULL) { - if (wp->w_bufp == blistp) { - wp->w_linep = lforw(blistp->b_linep); - wp->w_dotp = lforw(blistp->b_linep); - wp->w_doto = 0; - wp->w_markp = NULL; - wp->w_marko = 0; - wp->w_flag |= WFMODE | WFHARD; - } - wp = wp->w_wndp; - } - return TRUE; + if( blistp->b_nwnd == 0) { /* Not on screen yet. */ + buffer_p bp ; + + if( (wp = wpopup()) == NULL) + return FALSE ; + + bp = wp->w_bufp ; + if( --bp->b_nwnd == 0) { + bp->b_dotp = wp->w_dotp ; + bp->b_doto = wp->w_doto ; + bp->b_markp = wp->w_markp ; + bp->b_marko = wp->w_marko ; + } + + wp->w_bufp = blistp ; + ++blistp->b_nwnd ; + } + + for( wp = wheadp ; wp != NULL ; wp = wp->w_wndp) { + if( wp->w_bufp == blistp) { + wp->w_linep = lforw( blistp->b_linep) ; + wp->w_dotp = lforw( blistp->b_linep) ; + wp->w_doto = 0 ; + wp->w_markp = NULL ; + wp->w_marko = 0 ; + wp->w_flag |= WFMODE | WFHARD ; + } + } + + return TRUE ; } -/* - * This routine rebuilds the - * text in the special secret buffer - * that holds the buffer list. It is called - * by the list buffers command. Return TRUE - * if everything works. Return FALSE if there - * is an error (if there is no memory). Iflag - * indicates wether to list hidden buffers. - * - * int iflag; list hidden buffer flag + +/* This routine rebuilds the text in the special secret buffer that holds + the buffer list. It is called by the list buffers command. Return TRUE + if everything works. Return FALSE if there is an error (if there is no + memory). Iflag indicates wether to list hidden buffers. + + int iflag; list hidden buffer flag */ /* Layout: "ACT MODES Size Buffer File" AAA MMMMMMMMMSSSSSSSSSS BBBBBBBBBBBBBBB FFF... @@ -322,272 +299,262 @@ int listbuffers(int f, int n) #define FNAMSTART (3 + 1 + NUMMODES + 10 + 1 + (sizeof( bname_t) - 1) + 1) static void do_layout( char *line, int mode) { - int i ; + int i ; - /* build line to report global mode settings */ - strcpy( line, " WCEVOMAUD Global Modes") ; + /* build line to report global mode settings */ + strcpy( line, " WCEVOMAUD Global Modes") ; - /* output the mode codes */ - for( i = 0 ; i < NUMMODES ; i++) - if( 0 == (mode & (1 << i))) - line[ 4 + i] = '.' ; + /* output the mode codes */ + for( i = 0 ; i < NUMMODES ; i++) + if( 0 == (mode & (1 << i))) + line[ 4 + i] = '.' ; } + static unsigned int utf8_disp_len( const char *s) { - unsigned int len = 0 ; + unsigned int len = 0 ; - while( *s) { - unicode_t c ; + while( *s) { + unicode_t c ; - s += utf8_to_unicode( s, 0, 4, &c) ; - len += utf8_width( c) ; - } + s += utf8_to_unicode( s, 0, 4, &c) ; + len += utf8_width( c) ; + } - return len ; + return len ; } -static int makelist( int iflag) -{ - struct buffer *bp; - int s; - char line[ FNAMSTART + sizeof( fname_t)] ; - blistp->b_flag &= ~BFCHG; /* Don't complain! Mute bclear() */ - if ((s = bclear(blistp)) != TRUE) /* Blow old text away */ - return s; +static int makelist( int iflag) { + buffer_p bp; + int s; + char line[ FNAMSTART + sizeof( fname_t)] ; - blistp->b_fname[ 0] = 0 ; /* in case of user override */ + blistp->b_flag &= ~BFCHG; /* Don't complain! Mute bclear() */ + if ((s = bclear(blistp)) != TRUE) /* Blow old text away */ + return s; - if( addline("ACT MODES Size Buffer File") == FALSE - || addline("‾‾‾ ‾‾‾‾‾ ‾‾‾‾ ‾‾‾‾‾‾ ‾‾‾‾") == FALSE) - return FALSE; + blistp->b_fname[ 0] = 0 ; /* in case of user override */ + + if( addline("ACT MODES Size Buffer File") == FALSE + || addline("‾‾‾ ‾‾‾‾‾ ‾‾‾‾ ‾‾‾‾‾‾ ‾‾‾‾") == FALSE) + return FALSE ; /* report global mode settings */ - do_layout( line, gmode) ; - if( addline( line) == FALSE) - return FALSE ; + do_layout( line, gmode) ; + if( addline( line) == FALSE) + return FALSE ; /* output the list of buffers */ - for( bp = bheadp ; bp != NULL ; bp = bp->b_bufp) { /* For all buffers */ - char *cp1, *cp2 ; - int c ; - struct line *lp ; - long nbytes ; /* # of bytes in current buffer */ - long nlines ; /* # of lines in current buffer */ + for( bp = bheadp ; bp != NULL ; bp = bp->b_bufp) { /* For all buffers */ + char *cp1, *cp2 ; + int c ; + line_p lp ; + long nbytes ; /* # of bytes in current buffer */ + long nlines ; /* # of lines in current buffer */ - /* skip invisible buffers if iflag is false */ - if (((bp->b_flag & BFINVS) != 0) && (iflag != TRUE)) - continue; + /* skip invisible buffers if iflag is false */ + if (((bp->b_flag & BFINVS) != 0) && (iflag != TRUE)) + continue; - do_layout( line, bp->b_mode) ; - cp1 = line ; /* Start at left edge */ + do_layout( line, bp->b_mode) ; + cp1 = line ; /* Start at left edge */ - /* output status of ACTIVE flag ('@' when the file has been read in) */ - *cp1++ = (bp->b_active == TRUE) ? '@' : ' ' ; + /* output status of ACTIVE flag ('@' when the file has been read in) */ + *cp1++ = (bp->b_active == TRUE) ? '@' : ' ' ; - /* report if the file is truncated */ - *cp1++ = ((bp->b_flag & BFTRUNC) != 0) ? '#' : ' ' ; + /* report if the file is truncated */ + *cp1++ = ((bp->b_flag & BFTRUNC) != 0) ? '#' : ' ' ; - /* output status of changed flag ('*' when the buffer is changed) */ - *cp1 = ((bp->b_flag & BFCHG) != 0) ? '*' : ' ' ; + /* output status of changed flag ('*' when the buffer is changed) */ + *cp1 = ((bp->b_flag & BFCHG) != 0) ? '*' : ' ' ; - /* Buffer size */ - nbytes = 0L; /* Count bytes in buf. */ - nlines = 0 ; - for( lp = lforw( bp->b_linep) ; lp != bp->b_linep ; lp = lforw( lp)) { - nbytes += (long) llength(lp) + 1L; - nlines += 1 ; - } + /* Buffer size */ + nbytes = 0L; /* Count bytes in buf. */ + nlines = 0 ; + for( lp = lforw( bp->b_linep) ; lp != bp->b_linep ; lp = lforw( lp)) { + nbytes += (long) llength(lp) + 1L; + nlines += 1 ; + } - if( bp->b_mode & MDDOS) - nbytes += nlines ; + if( bp->b_mode & MDDOS) + nbytes += nlines ; - l_to_a( &line[ 13], 10 + 1, nbytes) ; /* "%10d" formatted numbers */ - cp1 = &line[ 23] ; - *cp1++ = ' ' ; + l_to_a( &line[ 13], 10 + 1, nbytes) ; /* "%10d" formatted numbers */ + cp1 = &line[ 23] ; + *cp1++ = ' ' ; - /* Display buffer name */ - cp2 = &bp->b_bname[ 0] ; - while ((c = *cp2++) != 0) - *cp1++ = c; + /* Display buffer name */ + cp2 = &bp->b_bname[ 0] ; + while ((c = *cp2++) != 0) + *cp1++ = c; - /* Pad with spaces to max buffer name length */ - int len = sizeof bp->b_bname ; - len -= utf8_disp_len( bp->b_bname) ; - while( len--) - *cp1++ = ' ' ; + /* Pad with spaces to max buffer name length */ + int len = sizeof bp->b_bname ; + len -= utf8_disp_len( bp->b_bname) ; + while( len--) + *cp1++ = ' ' ; - /* Display filename if any */ - if( bp->b_fname[ 0] != 0) - mystrscpy( cp1, bp->b_fname, &line[ sizeof line] - cp1) ; - else - *cp1 = 0 ; /* Terminate string */ + /* Display filename if any */ + if( bp->b_fname[ 0] != 0) + mystrscpy( cp1, bp->b_fname, &line[ sizeof line] - cp1) ; + else + *cp1 = 0 ; /* Terminate string */ - if( addline( line) == FALSE) /* Add to the buffer. */ - return FALSE ; - } + if( addline( line) == FALSE) /* Add to the buffer. */ + return FALSE ; + } - return TRUE ; /* All done */ + return TRUE ; /* All done */ } -static void l_to_a(char *buf, int width, long num) -{ - buf[ --width] = 0 ; /* End of string. */ - while (num >= 10) { /* Conditional digits. */ - buf[--width] = (int) (num % 10L) + '0'; - num /= 10L; - } - buf[--width] = (int) num + '0'; /* Always 1 digit. */ - while( width > 0) /* Pad with blanks. */ - buf[--width] = ' '; + +static void l_to_a(char *buf, int width, long num) { + buf[ --width] = 0 ; /* End of string. */ + while (num >= 10) { /* Conditional digits. */ + buf[--width] = (int) (num % 10L) + '0'; + num /= 10L; + } + buf[--width] = (int) num + '0'; /* Always 1 digit. */ + while( width > 0) /* Pad with blanks. */ + buf[--width] = ' '; } -/* - * The argument "text" points to - * a string. Append this line to the - * buffer list buffer. Handcraft the EOL - * on the end. Return TRUE if it worked and - * FALSE if you ran out of room. + +/* The argument "text" points to a string. Append this line to the buffer + list buffer. Handcraft the EOL on the end. Return TRUE if it worked + and FALSE if you ran out of room. */ -static int addline( char *text) -{ - struct line *lp; - int i; - int ntext; +static int addline( char *text) { + int ntext = strlen( text) ; + line_p lp = lalloc( ntext) ; + if( lp == NULL) + return FALSE ; - ntext = strlen(text); - if ((lp = lalloc(ntext)) == NULL) - return FALSE; - for (i = 0; i < ntext; ++i) - lputc(lp, i, text[i]); - blistp->b_linep->l_bp->l_fp = lp; /* Hook onto the end */ - lp->l_bp = blistp->b_linep->l_bp; - blistp->b_linep->l_bp = lp; - lp->l_fp = blistp->b_linep; - if (blistp->b_dotp == blistp->b_linep) /* If "." is at the end */ - blistp->b_dotp = lp; /* move it to new line */ - return TRUE; + for( int i = 0 ; i < ntext ; ++i) + lputc( lp, i, text[ i]) ; + + blistp->b_linep->l_bp->l_fp = lp ; /* Hook onto the end */ + lp->l_bp = blistp->b_linep->l_bp ; + blistp->b_linep->l_bp = lp ; + lp->l_fp = blistp->b_linep ; + if( blistp->b_dotp == blistp->b_linep) /* If "." is at the end */ + blistp->b_dotp = lp ; /* move it to new line */ + + return TRUE ; } -/* - * Look through the list of - * buffers. Return TRUE if there - * are any changed buffers. Buffers - * that hold magic internal stuff are - * not considered; who cares if the - * list of buffer names is hacked. - * Return FALSE if no buffers - * have been changed. + +/* Look through the list of buffers. Return TRUE if there are any changed + buffers. Buffers that hold magic internal stuff are not considered; who + cares if the list of buffer names is hacked. Return FALSE if no buffers + have been changed. */ -int anycb(void) -{ - struct buffer *bp; +boolean anycb( void) { + for( buffer_p bp = bheadp ; bp != NULL ; bp = bp->b_bufp) { + if( (bp->b_flag & (BFINVS | BFCHG)) == BFCHG) + return TRUE ; + } - bp = bheadp; - while (bp != NULL) { - if ((bp->b_flag & BFINVS) == 0 - && (bp->b_flag & BFCHG) != 0) - return TRUE; - bp = bp->b_bufp; - } - return FALSE; + return FALSE ; } -/* - * Find a buffer, by name. Return a pointer - * to the buffer structure associated with it. - * If the buffer is not found - * and the "cflag" is TRUE, create it. The "bflag" is - * the settings for the flags in in buffer. + +/* Find a buffer, by name. Return a pointer to the buffer structure + associated with it. If the buffer is not found and the "create_f" is + TRUE, create it. The "flags" is the settings for the buffer flags. */ -struct buffer *bfind( const char *bname, int cflag, int bflag) -{ - struct buffer *bp; - struct line *lp; +buffer_p bfind( const char *bname, boolean create_f, int flags) { + buffer_p bp ; - for( bp = bheadp ; bp != NULL ; bp = bp->b_bufp) - if( strcmp( bname, bp->b_bname) == 0) - return bp ; + for( bp = bheadp ; bp != NULL ; bp = bp->b_bufp) + if( strcmp( bname, bp->b_bname) == 0) + return bp ; - if (cflag != FALSE) { - if ((bp = (struct buffer *)malloc(sizeof(struct buffer))) == NULL) - return NULL; - if ((lp = lalloc(0)) == NULL) { - free((char *) bp); - return NULL; - } - /* find the place in the list to insert this buffer */ - if (bheadp == NULL || strcmp(bheadp->b_bname, bname) > 0) { - /* insert at the beginning */ - bp->b_bufp = bheadp; - bheadp = bp; - } else { - struct buffer *sb; /* buffer to insert after */ + if( create_f != FALSE) { + /* allocate empty buffer */ + bp = malloc( sizeof *bp) ; + if( bp == NULL) + return NULL ; - for( sb = bheadp ; sb->b_bufp != NULL ; sb = sb->b_bufp) - if( strcmp( sb->b_bufp->b_bname, bname) > 0) - break ; + line_p lp = lalloc( 0) ; + if( lp == NULL) { + free( bp) ; + return NULL ; + } - /* and insert it */ - bp->b_bufp = sb->b_bufp ; - sb->b_bufp = bp ; - } + lp->l_fp = lp ; + lp->l_bp = lp ; + bp->b_linep = lp ; + bp->b_dotp = lp ; + bp->b_doto = 0 ; - /* and set up the other buffer fields */ - bp->b_active = TRUE; - bp->b_dotp = lp; - bp->b_doto = 0; - bp->b_markp = NULL; - bp->b_marko = 0; - bp->b_flag = bflag; - bp->b_mode = gmode; - bp->b_nwnd = 0; - bp->b_linep = lp; - bp->b_fname[ 0] = '\0' ; - mystrscpy( bp->b_bname, bname, sizeof( bname_t)) ; - lp->l_fp = lp; - lp->l_bp = lp; - } - return bp; + /* find the place in the list to insert this buffer */ + if( bheadp == NULL || strcmp( bheadp->b_bname, bname) > 0) { + /* insert at the beginning */ + bp->b_bufp = bheadp ; + bheadp = bp ; + } else { + buffer_p sb ; /* buffer to insert after */ + + for( sb = bheadp ; sb->b_bufp != NULL ; sb = sb->b_bufp) + if( strcmp( sb->b_bufp->b_bname, bname) > 0) + break ; + + /* and insert it */ + bp->b_bufp = sb->b_bufp ; + sb->b_bufp = bp ; + } + + /* and set up the other buffer fields */ + bp->b_active = TRUE ; + bp->b_markp = NULL ; + bp->b_marko = 0 ; + bp->b_flag = flags ; + bp->b_mode = gmode ; + bp->b_nwnd = 0 ; + bp->b_fname[ 0] = '\0' ; + mystrscpy( bp->b_bname, bname, sizeof( bname_t)) ; + } + + return bp ; } -/* - * This routine blows away all of the text - * in a buffer. If the buffer is marked as changed - * then we ask if it is ok to blow it away; this is - * to save the user the grief of losing text. The - * window chain is nearly always wrong if this gets - * called; the caller must arrange for the updates - * that are required. Return TRUE if everything - * looks good. + +/* This routine blows away all of the text in a buffer. If the buffer is + marked as changed then we ask if it is ok to blow it away; this is to + save the user the grief of losing text. The window chain is nearly + always wrong if this gets called; the caller must arrange for the + updates that are required. Return TRUE if everything looks good. */ -int bclear(struct buffer *bp) -{ - struct line *lp; - int s; +int bclear( buffer_p bp) { + line_p lp ; + int s ; - if ((bp->b_flag & BFINVS) == 0 /* Not scratch buffer. */ - && (bp->b_flag & BFCHG) != 0 /* Something changed */ - && (s = mlyesno("Discard changes")) != TRUE) - return s; - bp->b_flag &= ~BFCHG; /* Not changed */ - while ((lp = lforw(bp->b_linep)) != bp->b_linep) - lfree(lp); - bp->b_dotp = bp->b_linep; /* Fix "." */ - bp->b_doto = 0; - bp->b_markp = NULL; /* Invalidate "mark" */ - bp->b_marko = 0; - return TRUE; + if( (bp->b_flag & (BFINVS | BFCHG)) == BFCHG /* regular and changed */ + && (s = mlyesno( "Discard changes")) != TRUE) + return s ; + + bp->b_flag &= ~BFCHG ; /* Not changed */ + while( (lp = lforw( bp->b_linep)) != bp->b_linep) + lfree( lp) ; + + bp->b_dotp = bp->b_linep ; /* Fix "." */ + bp->b_doto = 0 ; + bp->b_markp = NULL ; /* Invalidate "mark" */ + bp->b_marko = 0 ; + return TRUE ; } -/* - * unmark the current buffers change flag + +/* unmark the current buffers change flag * - * int f, n; unused command arguments + * int f, n; unused command arguments */ -int unmark(int f, int n) -{ - curbp->b_flag &= ~BFCHG; - curwp->w_flag |= WFMODE; - return TRUE; +BINDABLE( unmark) { + curbp->b_flag &= ~BFCHG ; + curwp->w_flag |= WFMODE ; + return TRUE ; } + +/* end of buffer.c */ diff --git a/buffer.h b/buffer.h index f8e93c5..50f3b75 100644 --- a/buffer.h +++ b/buffer.h @@ -1,74 +1,82 @@ +/* buffer.h -- buffer type and functions */ #ifndef _BUFFER_H_ -#define _BUFFER_H_ +# define _BUFFER_H_ #include "line.h" +#include "names.h" -typedef char fname_t[ 256] ; /* file name type */ -typedef char bname_t[ 16] ; /* buffer name type */ +/* Text is kept in buffers. A buffer header, described below, exists for + every buffer in the system. The buffers are kept in a big list, so that + commands that search for a buffer by name can find the buffer header. + There is a safe store for the dot and mark in the header, but this is + only valid if the buffer is not being displayed (that is, if "b_nwnd" is + 0). The text for the buffer is kept in a circularly linked list of + lines, with a pointer to the header line in "b_linep". -/* - * Text is kept in buffers. A buffer header, described below, exists for every - * buffer in the system. The buffers are kept in a big list, so that commands - * that search for a buffer by name can find the buffer header. There is a - * safe store for the dot and mark in the header, but this is only valid if - * the buffer is not being displayed (that is, if "b_nwnd" is 0). The text for - * the buffer is kept in a circularly linked list of lines, with a pointer to - * the header line in "b_linep". - * Buffers may be "Inactive" which means the files associated with them - * have not been read in yet. These get read in at "use buffer" time. + Buffers may be "Inactive" which means the files associated with them + have not been read in yet. These get read in at "use buffer" time. */ + +typedef char fname_t[ 256] ; /* file name type */ +typedef char bname_t[ 16] ; /* buffer name type */ + typedef struct buffer { - struct buffer *b_bufp; /* Link to next struct buffer */ - line_p b_dotp ; /* Link to "." struct line structure */ - line_p b_markp ; /* The same as the above two, */ - line_p b_linep ; /* Link to the header struct line */ - int b_doto; /* Offset of "." in above struct line */ - int b_marko; /* but for the "mark" */ - int b_mode; /* editor mode of this buffer */ - char b_active; /* window activated flag */ - char b_nwnd; /* Count of windows on buffer */ - char b_flag; /* Flags */ - fname_t b_fname ; /* File name */ - bname_t b_bname ; /* Buffer name */ + struct buffer *b_bufp ; /* Link to next struct buffer */ + line_p b_dotp ; /* Link to "." struct line structure */ + line_p b_markp ; /* The same as the above two, */ + line_p b_linep ; /* Link to the header struct line */ + int b_doto ; /* Offset of "." in above struct line */ + int b_marko ; /* but for the "mark" */ + int b_mode ; /* editor mode of this buffer */ + char b_active ; /* window activated flag */ + char b_nwnd ; /* Count of windows on buffer */ + char b_flag ; /* Flags */ + fname_t b_fname ; /* File name */ + bname_t b_bname ; /* Buffer name */ } *buffer_p ; -extern buffer_p curbp ; /* Current buffer */ -extern buffer_p bheadp ; /* Head of list of buffers */ -extern buffer_p blistp ; /* Buffer for C-X C-B */ +extern buffer_p curbp ; /* Current buffer */ +extern buffer_p bheadp ; /* Head of list of buffers */ +extern buffer_p blistp ; /* Buffer for C-X C-B */ -#define BFINVS 0x01 /* Internal invisable buffer */ -#define BFCHG 0x02 /* Changed since last write */ -#define BFTRUNC 0x04 /* buffer was truncated when read */ +#define BFINVS 0x01 /* Internal invisable buffer */ +#define BFCHG 0x02 /* Changed since last write */ +#define BFTRUNC 0x04 /* buffer was truncated when read */ -/* mode flags */ -#define NUMMODES 9 /* # of defined modes */ +/* mode flags */ +#define NUMMODES 9 /* # of defined modes */ -#define MDWRAP 0x0001 /* word wrap */ -#define MDCMOD 0x0002 /* C indentation and fence match */ -#define MDEXACT 0x0004 /* Exact matching for searches */ -#define MDVIEW 0x0008 /* read-only buffer */ -#define MDOVER 0x0010 /* overwrite mode */ -#define MDMAGIC 0x0020 /* regular expresions in search */ -#define MDASAVE 0x0040 /* auto-save mode */ -#define MDUTF8 0x0080 /* utf8 mode */ -#define MDDOS 0x0100 /* CRLF eol mode */ +#define MDWRAP 0x0001 /* word wrap */ +#define MDCMOD 0x0002 /* C indentation and fence match */ +#define MDEXACT 0x0004 /* Exact matching for searches */ +#define MDVIEW 0x0008 /* read-only buffer */ +#define MDOVER 0x0010 /* overwrite mode */ +#define MDMAGIC 0x0020 /* regular expresions in search */ +#define MDASAVE 0x0040 /* auto-save mode */ +#define MDUTF8 0x0080 /* utf8 mode */ +#define MDDOS 0x0100 /* CRLF eol mode */ + +extern const char *modename[ NUMMODES] ; /* text names of modes */ +extern int gmode ; /* global editor mode */ -extern const char *modename[] ; /* text names of modes */ -extern int gmode ; /* global editor mode */ +/* Bindable functions */ +BINDABLE( killbuffer) ; +BINDABLE( listbuffers) ; +BINDABLE( namebuffer) ; +BINDABLE( nextbuffer) ; +BINDABLE( unmark) ; +BINDABLE( usebuffer) ; +boolean anycb( void) ; /* Any changed buffer? */ +int bclear( buffer_p bp) ; /* empty buffer */ +int swbuffer( buffer_p bp) ; /* switch to buffer, make it current */ +int zotbuf( buffer_p bp) ; /* remove buffer */ -int usebuffer( int f, int n) ; -int nextbuffer( int f, int n) ; -int swbuffer( buffer_p bp) ; -int killbuffer( int f, int n) ; -int zotbuf( buffer_p bp) ; -int namebuffer( int f, int n) ; -int listbuffers( int f, int n) ; -int anycb( void) ; -int bclear( buffer_p bp) ; -int unmark( int f, int n) ; -/* Lookup a buffer by name. */ -buffer_p bfind( const char *bname, int cflag, int bflag) ; +/* Lookup a buffer by name. If not found and create_f is TRUE then create + it with flags set. +*/ +buffer_p bfind( const char *bname, boolean create_f, int flags) ; #endif +/* end of buffer.h */ diff --git a/display.c b/display.c index 35a3559..c5226c9 100644 --- a/display.c +++ b/display.c @@ -1,17 +1,14 @@ /* display.c -- implements display.h */ - #include "display.h" #define REVSTA 1 /* Status line appears in reverse video */ -/* display.c - * - * The functions in this file handle redisplay. There are two halves, the - * ones that update the virtual display screen, and the ones that make the - * physical display screen the same as the virtual display screen. These - * functions use hints that are left in the windows by the commands. - * - * Modified by Petri Kutvonen +/* The functions in this file handle redisplay. There are two halves, the + ones that update the virtual display screen, and the ones that make the + physical display screen the same as the virtual display screen. These + functions use hints that are left in the windows by the commands. + + Modified by Petri Kutvonen */ #include @@ -251,40 +248,37 @@ static void vteeol( void) { vcp[ vtcol++] = ' ' ; } -/* - * upscreen: +/* upscreen: * user routine to force a screen update * always finishes complete update */ -int upscreen(int f, int n) -{ - update(TRUE); - return TRUE; +BINDABLE( upscreen) { + update( TRUE) ; + return TRUE ; } #if SCROLLCODE static int scrflags; #endif -/* - * Make sure that the display is right. This is a three part process. First, - * scan through all of the windows looking for dirty ones. Check the framing, - * and refresh the screen. Second, make sure that "currow" and "curcol" are - * correct for the current window. Third, make the virtual and physical - * screens the same. - * - * int force; force update past type ahead? + +/* Make sure that the display is right. This is a three part process. + First, scan through all of the windows looking for dirty ones. Check + the framing, and refresh the screen. Second, make sure that "currow" + and "curcol" are correct for the current window. Third, make the + virtual and physical screens the same. + + boolean force_f ; force update past type ahead? */ -int update(int force) -{ +int update( boolean force_f) { struct window *wp; #if TYPEAH && ! PKCODE - if (force == FALSE && typahead()) + if( force_f == FALSE && typahead()) return TRUE; #endif #if VISMAC == 0 - if (force == FALSE && kbdmode == PLAY) + if( force_f == FALSE && kbdmode == PLAY) return TRUE; #endif @@ -359,7 +353,7 @@ int update(int force) updgar(); /* update the virtual screen to the physical screen */ - updupd(force); + updupd( force_f) ; /* update the cursor and flush the buffers */ movecursor(currow, curcol - lbound); diff --git a/display.h b/display.h index b244df1..1e0f041 100644 --- a/display.h +++ b/display.h @@ -1,10 +1,12 @@ +/* display.h -- display functionality */ #ifndef _DISPLAY_H_ -#define _DISPLAY_H_ +# define _DISPLAY_H_ -#include +# include -#include "estruct.h" -#include "utf8.h" +# include "estruct.h" +# include "names.h" /* BINDABLE() */ +# include "utf8.h" /* unicode_t */ extern int mpresf ; /* Stuff in message line */ extern int scrollcount ; /* number of lines to scroll */ @@ -13,11 +15,13 @@ extern int disinp ; /* display input characters (echo) */ extern int gfcolor ; /* global forgrnd color (white) */ extern int gbcolor ; /* global backgrnd color (black) */ +/* Bindable functions */ +BINDABLE( upscreen) ; + void vtinit( void) ; void vtfree( void) ; void vttidy( void) ; -int upscreen( int f, int n) ; -int update( int force) ; +int update( boolean force_f) ; void updpos( void) ; void upddex( void) ; void updgar( void) ; @@ -33,13 +37,13 @@ void echos( const char *s) ; void rubout( void) ; void getscreensize( int *widthp, int *heightp) ; -#if UNIX -#include -#ifdef SIGWINCH +# if UNIX +# include +# ifdef SIGWINCH extern int chg_width, chg_height ; void sizesignal( int signr) ; +# endif +# endif #endif -#endif - -#endif +/* end of display.h */ diff --git a/eval.c b/eval.c index 689a74d..c4f427f 100644 --- a/eval.c +++ b/eval.c @@ -1,9 +1,7 @@ /* eval.c -- implements eval.h */ #include "eval.h" -/* eval.c - * - * Expression evaluation functions +/* Expression evaluation functions * * written 1986 by Daniel Lawrence * modified by Petri Kutvonen @@ -210,7 +208,7 @@ static struct { { "and", UFAND | DYNAMIC }, /* logical and */ { "asc", UFASCII | MONAMIC }, /* char to integer conversion */ { "ban", UFBAND | DYNAMIC }, /* bitwise and 9-10-87 jwm */ - { "bin", UFBIND | MONAMIC }, /* loopup what function name is bound to a key */ + { "bin", UFBIND | MONAMIC }, /* look up function name bound to key */ { "bno", UFBNOT | MONAMIC }, /* bitwise not */ { "bor", UFBOR | DYNAMIC }, /* bitwise or 9-10-87 jwm */ { "bxo", UFBXOR | DYNAMIC }, /* bitwise xor 9-10-87 jwm */ @@ -797,14 +795,13 @@ static char *gtenv( char *vname) { return errorm ; } -/* - * set a variable + +/* set a variable * * int f; default flag * int n; numeric arg (can overide prompted value) */ -int setvar(int f, int n) -{ +BINDABLE( setvar) { int status; /* status return */ struct variable_description vd; /* variable num/type */ char var[NVSIZE + 2]; /* name of variable to fetch %1234567890\0 */ @@ -1466,28 +1463,25 @@ static void mlforce( char *s) { discmd = oldcmd; /* and restore the original setting */ } -/* - * This function simply clears the message line, - * mainly for macro usage + +/* This function simply clears the message line, mainly for macro usage * * int f, n; arguments ignored */ -int clrmes( int f, int n) { +TBINDABLE( clrmes) { mlforce( "") ; return TRUE ; } -/* - * This function writes a string on the message line - * mainly for macro usage - * - * int f, n; arguments ignored - */ -int writemsg( int f, int n) { - int status ; - char *buf ; /* buffer to receive message into */ - status = newmlarg( &buf, "Message to write: ", 0) ; +/* This function writes a string on the message line mainly for macro usage + * + * int f, n; arguments ignored + */ +BINDABLE( writemsg) { + char *buf ; /* buffer to receive message into */ + + int status = newmlarg( &buf, "write-message: ", 0) ; if( status == TRUE) { /* write the message out */ mlforce( buf) ; diff --git a/eval.h b/eval.h index e6fac0d..8c178ee 100644 --- a/eval.h +++ b/eval.h @@ -1,29 +1,33 @@ +/* eval.h -- variables and operands evaluation */ #ifndef _EVAL_H_ -#define _EVAL_H_ +# define _EVAL_H_ +#include "names.h" -#define DEBUGM 1 /* $debug triggers macro debugging */ +#define DEBUGM 1 /* $debug triggers macro debugging */ -#if DEBUGM +# if DEBUGM int mdbugout( char *fmt, ...) ; -#endif +# endif - -extern int macbug ; /* macro debuging flag */ -extern int cmdstatus ; /* last command status */ -extern int rval ; /* return value of a subprocess */ -extern long envram ; /* # of bytes current in use by malloc */ +extern int macbug ; /* macro debuging flag */ +extern int cmdstatus ; /* last command status */ +extern int rval ; /* return value of a subprocess */ +extern long envram ; /* # of bytes current in use by malloc */ int readfirst_f( void) ; int is_it_cmd( char *token) ; void varinit( void) ; -int setvar( int f, int n) ; const char *getval( char *token) ; int stol( char *val) ; char *mklower( char *str) ; -int clrmes( int f, int n) ; -int writemsg( int f, int n) ; +/* Bindable functions */ +TBINDABLE( clrmes) ; +BINDABLE( setvar) ; +BINDABLE( writemsg) ; #endif + +/* end of eval.h */ diff --git a/exec.c b/exec.c index 0f2f3a1..e85134d 100644 --- a/exec.c +++ b/exec.c @@ -1,41 +1,34 @@ /* exec.c -- implements exec.h */ #include "exec.h" -/* exec.c - * - * This file is for functions dealing with execution of - * commands, command lines, buffers, files and startup files. - * - * written 1986 by Daniel Lawrence - * modified by Petri Kutvonen +/* This file is for bindable functions dealing with execution of commands, + command lines, buffers, files and startup files. + + written 1986 by Daniel Lawrence + modified by Petri Kutvonen */ -#include #include #include #include "buffer.h" #include "bind.h" -#include "display.h" -#include "estruct.h" #include "eval.h" #include "file.h" #include "flook.h" #include "input.h" #include "line.h" +#include "mlout.h" #include "random.h" #include "util.h" #include "window.h" - static char *execstr = NULL ; /* pointer to string to execute */ -boolean clexec = FALSE ; /* command line execution flag */ - - +boolean clexec = FALSE ; /* command line execution flag */ /* Directive definitions */ -#define DIF 0 +#define DIF 0 #define DELSE 1 #define DENDIF 2 #define DGOTO 3 @@ -46,82 +39,73 @@ boolean clexec = FALSE ; /* command line execution flag */ #define DBREAK 8 #define DFORCE 9 -#define NUMDIRS 10 /* The !WHILE directive in the execution language needs to * stack references to pending whiles. These are stored linked * to each currently open procedure via a linked list of * the following structure. */ -struct while_block { - struct line *w_begin; /* ptr to !while statement */ - struct line *w_end; /* ptr to the !endwhile statement */ - int w_type; /* block type */ - struct while_block *w_next; /* next while */ -}; +typedef struct while_block { + line_p w_begin ; /* ptr to !while statement */ + line_p w_end ; /* ptr to the !endwhile statement */ + int w_type ; /* block type */ + struct while_block *w_next ; /* next while */ +} *while_p ; #define BTWHILE 1 #define BTBREAK 2 -/* directive name table: - This holds the names of all the directives.... */ +/* directive name table: holds the names of all the directives.... */ static const char *dname[] = { - "if", "else", "endif", - "goto", "return", "endm", - "while", "endwhile", "break", - "force" -}; + "if", "else", "endif", "goto", "return", + "endm", "while", "endwhile", "break", "force" +} ; -static char golabel[ NSTRING] = "" ; /* current line to go to */ -static int execlevel = 0 ; /* execution IF level */ -static struct buffer *bstore = NULL ; /* buffer to store macro text to */ -static int mstore = FALSE ; /* storing text to macro flag */ +#define NUMDIRS ARRAY_SIZE( dname) -static int dobuf( struct buffer *bp) ; -static void freewhile( struct while_block *wp) ; +static char golabel[ NSTRING] = "" ; /* current line to go to */ +static int execlevel = 0 ; /* execution IF level */ +static buffer_p bstore = NULL ; /* buffer to store macro text to */ +static int mstore = FALSE ; /* storing text to macro flag */ + +static int dobuf( buffer_p bp) ; static int macarg( char *tok, int toksz) ; -/* - * Execute a named command even if it is not bound. - */ -int namedcmd( int f, int n) { - /* prompt the user to type a named command */ - mlwrite("execute-named-cmd: "); +/* Execute a named command even if it is not bound. */ +BINDABLE( namedcmd) { +/* prompt the user to type a named command */ + mloutstr( "execute-named-cmd: "); - /* and now get the function name to execute */ +/* and now get the function name to execute */ nbind_p nbp = getname() ; if( nbp == NULL) /* abort */ - return FALSE ; + return ABORT ; fnp_t kfunc = nbp->n_func ; - if (kfunc == NULL) { - mlwrite("(No such function)"); - return FALSE; - } + if( kfunc == NULL) + return mloutfail( "(No such function)") ; if( (bind_tag( nbp) & 1) && (curbp->b_mode & MDVIEW)) return rdonly() ; /* and then execute the command */ - return kfunc(f, n) ; + return kfunc( f, n) ; } static int docmd( char *cline) ; -/* - * execcmd: +/* execcmd: * Execute a command line command to be typed in * by the user * * int f, n; default Flag and Numeric argument */ -int execcmd( int f, int n) { - int status ; /* status return */ +BINDABLE( execcmd) { char *cmdstr ; /* string holding command to execute */ - /* get the line wanted */ - status = newmlarg( &cmdstr, "execute-command-line: ", 0) ; +/* get the line wanted */ + int status = newmlarg( &cmdstr, "execute-command-line: ", 0) ; if( status != TRUE) return status ; @@ -133,8 +117,7 @@ int execcmd( int f, int n) { return status ; } -/* - * docmd: +/* docmd: * take a passed string as a command line and translate * it to be executed as a command. This function will be * used by execute-command-line and by all source and @@ -147,27 +130,24 @@ int execcmd( int f, int n) { * char *cline; command line to execute */ static int docmd( char *cline) { - int f; /* default argument flag */ - int n; /* numeric repeat value */ - int status; /* return status of function */ - boolean oldcle ; /* old contents of clexec flag */ - char *oldestr; /* original exec string */ - char tkn[NSTRING]; /* next token off of command line */ + boolean oldcle ; /* old contents of clexec flag */ + char *oldestr ; /* original exec string */ + char tkn[NSTRING] ; /* next token off of command line */ /* if we are scanning and not executing..go back here */ if (execlevel) - return TRUE; + return TRUE ; - oldestr = execstr; /* save last ptr to string to execute */ - execstr = cline; /* and set this one as current */ + oldestr = execstr ; /* save last ptr to string to execute */ + execstr = cline ; /* and set this one as current */ /* first set up the default command values */ - f = FALSE; - n = 1; - lastflag = thisflag; - thisflag = 0; + int f = FALSE ; + int n = 1 ; + lastflag = thisflag ; + thisflag = 0 ; - status = macarg( tkn, sizeof tkn) ; + int status = macarg( tkn, sizeof tkn) ; if( status != TRUE) { /* and grab the first token */ execstr = oldestr; return status; @@ -193,9 +173,8 @@ static int docmd( char *cline) { nbind_p nbp = fncmatch( tkn) ; fnp_t fnc = nbp->n_func ; if( fnc == NULL) { - mlwrite("(No such Function)"); - execstr = oldestr; - return FALSE; + execstr = oldestr ; + return mloutfail( "(No such Function)") ; } if( (bind_tag( nbp) & 1) && (curbp->b_mode & MDVIEW)) @@ -222,25 +201,21 @@ static int docmd( char *cline) { * char **tokref out, destination of newly allocated token string */ static char *newtoken( char *src, char **tokref) { - boolean quotef ; /* is the current string quoted? */ - char *tok ; /* allocated string */ - int size ; /* allocated size */ - int idx = 0 ; /* insertion point into token string */ - - tok = malloc( NSTRING) ; - size = (tok == NULL) ? 0 : NSTRING ; + char *tok = malloc( NSTRING) ; + int size = (tok == NULL) ? 0 : NSTRING ; + int idx = 0 ; /* insertion point into token string */ /* first scan past any whitespace in the source string */ - while (*src == ' ' || *src == '\t') - ++src; + while( *src == ' ' || *src == '\t') + ++src ; /* scan through the source string */ - quotef = FALSE; - while (*src) { + boolean quotef = FALSE ; /* is the current string quoted? */ + while( *src) { char c ; /* temporary character */ /* process special characters */ - if (*src == '~') { + if( *src == '~') { ++src; if (*src == 0) break; @@ -395,40 +370,31 @@ static int macarg( char *tok, int toksz) { static char macbufname[] = "*Macro xx*" ; #define MACDIGITPOS 7 -int storemac(int f, int n) -{ - struct buffer *bp; /* pointer to macro buffer */ +BINDABLE( storemac) { +/* must have a numeric argument to this function */ + if( f == FALSE) + return mloutfail( "No macro number specified"); - /* must have a numeric argument to this function */ - if (f == FALSE) { - mlwrite("No macro specified"); - return FALSE; - } +/* range check the macro number */ + if( n < 1 || n > 40) + return mloutfail( "Macro number out of range") ; - /* range check the macro number */ - if (n < 1 || n > 40) { - mlwrite("Macro number out of range"); - return FALSE; - } - - /* construct the macro buffer name */ +/* construct the macro buffer name */ macbufname[ MACDIGITPOS] = '0' + (n / 10) ; macbufname[ MACDIGITPOS + 1] = '0' + (n % 10) ; - /* set up the new macro buffer */ - bp = bfind( macbufname, TRUE, BFINVS) ; - if( bp == NULL) { - mlwrite("Can not create macro"); - return FALSE; - } +/* set up the new macro buffer */ + buffer_p bp = bfind( macbufname, TRUE, BFINVS) ; + if( bp == NULL) + return mloutfail( "Can not create macro") ; - /* and make sure it is empty */ - bclear(bp); +/* and make sure it is empty */ + bclear( bp) ; - /* and set the macro store pointers to it */ - mstore = TRUE; - bstore = bp; - return TRUE; +/* and set the macro store pointers to it */ + mstore = TRUE ; + bstore = bp ; + return TRUE ; } /* @@ -436,14 +402,12 @@ int storemac(int f, int n) ** common to execute buffer, procedure and macro */ static int exec( int n, char *bufname, char *errstr) { - struct buffer *bp ; /* ptr to buffer to execute */ - /* find the pointer to that buffer */ - bp = bfind( bufname, FALSE, 0) ; + buffer_p bp = bfind( bufname, FALSE, 0) ; if( bp == NULL) { - mlwrite( "No such %s", errstr) ; - return FALSE ; - } + mloutfmt( "No such %s", errstr) ; + return FALSE ; + } /* and now execute it as asked */ int status = TRUE ; @@ -453,7 +417,6 @@ static int exec( int n, char *bufname, char *errstr) { return status ; } -#if PROC /* * storeproc: * Set up a procedure buffer and flag to store all @@ -462,9 +425,7 @@ static int exec( int n, char *bufname, char *errstr) { * int f; default flag * int n; macro number to use */ -int storeproc( int f, int n) { - struct buffer *bp ; /* pointer to macro buffer */ - int status ; /* return status */ +BINDABLE( storeproc) { bname_t bname ; /* name of buffer to use */ char *name ; @@ -473,7 +434,7 @@ int storeproc( int f, int n) { return storemac( f, n) ; /* get the name of the procedure */ - status = newmlarg( &name, "Procedure name: ", sizeof bname - 2) ; + int status = newmlarg( &name, "Procedure name: ", sizeof bname - 2) ; if( status != TRUE) return status ; @@ -484,11 +445,9 @@ int storeproc( int f, int n) { free( name) ; /* set up the new macro buffer */ - bp = bfind( bname, TRUE, BFINVS) ; - if( bp == NULL) { - mlwrite( "Can not create macro") ; - return FALSE ; - } + buffer_p bp = bfind( bname, TRUE, BFINVS) ; + if( bp == NULL) + return mloutfail( "Can not create macro") ; /* and make sure it is empty */ bclear( bp) ; @@ -505,13 +464,12 @@ int storeproc( int f, int n) { * * int f, n; default flag and numeric arg */ -int execproc( int f, int n) { - int status ; /* status return */ - bname_t bufn ; /* name of buffer to execute */ +BINDABLE( execproc) { + bname_t bufn ; /* name of buffer to execute */ char *name ; /* find out what buffer the user wants to execute */ - status = newmlarg( &name, "execute-procedure: ", sizeof bufn - 2) ; + int status = newmlarg( &name, "execute-procedure: ", sizeof bufn - 2) ; if( status != TRUE) return status ; @@ -523,20 +481,18 @@ int execproc( int f, int n) { return exec( n, bufn, "procedure") ; } -#endif -/* - * execbuf: + +/* execbuf: * Execute the contents of a buffer of commands * * int f, n; default flag and numeric arg */ -int execbuf( int f, int n) { - int status ; /* status return */ +BINDABLE( execbuf) { char *bufn ; /* name of buffer to execute */ - /* find out what buffer the user wants to execute */ - status = newmlarg( &bufn, "Execute buffer: ", sizeof( bname_t)) ; +/* find out what buffer the user wants to execute */ + int status = newmlarg( &bufn, "Execute buffer: ", sizeof( bname_t)) ; if( status != TRUE) return status ; @@ -545,8 +501,21 @@ int execbuf( int f, int n) { return status ; } -/* - * dobuf: + +/* free a list of while block pointers + * + * while_p wp; head of structure to free + */ +static void freewhile( while_p wp) { + while( wp != NULL) { + while_p next = wp->w_next ; + free( wp) ; + wp = next ; + } +} + + +/* dobuf: * execute the contents of the buffer pointed to * by the passed BP * @@ -566,75 +535,68 @@ int execbuf( int f, int n) { * * *LBL01 * - * struct buffer *bp; buffer to execute + * buffer_p bp; buffer to execute */ -static int dobuf(struct buffer *bp) -{ - int status; /* status return */ - struct line *lp; /* pointer to line to execute */ - struct line *hlp; /* pointer to line header */ - struct line *glp; /* line to goto */ - struct line *mp; /* Macro line storage temp */ - int dirnum; /* directive index */ - int linlen; /* length of line to execute */ - int i; /* index */ - int force; /* force TRUE result? */ - struct window *wp; /* ptr to windows to scan */ - struct while_block *whlist; /* ptr to !WHILE list */ - struct while_block *scanner; /* ptr during scan */ - struct while_block *whtemp; /* temporary ptr to a struct while_block */ - char *einit; /* initial value of eline */ - char *eline; /* text of line to execute */ - char tkn[NSTRING]; /* buffer to evaluate an expresion in */ +static int dobuf( buffer_p bp) { + int linlen ; /* length of line to execute */ + int i ; /* index */ + while_p whtemp ; /* temporary ptr to a struct while_block */ + char *eline ; /* text of line to execute */ + char tkn[ NSTRING] ; /* buffer to evaluate an expression in */ - /* clear IF level flags/while ptr */ - execlevel = 0; - whlist = NULL; - scanner = NULL; +/* clear IF level flags/while ptr */ + execlevel = 0 ; + while_p whlist = NULL ; /* ptr to !WHILE list */ + while_p scanner = NULL ; /* ptr during scan */ - /* scan the buffer to execute, building WHILE header blocks */ - hlp = bp->b_linep; - lp = hlp->l_fp; - while (lp != hlp) { - /* scan the current line */ - eline = lp->l_text; - i = lp->l_used; +/* scan the buffer to execute, building WHILE header blocks */ + line_p hlp = bp->b_linep ; /* pointer to line header */ + for( line_p lp = hlp->l_fp ; lp != hlp ; lp = lp->l_fp) { + /* scan the current line */ + eline = lp->l_text ; + i = lp->l_used ; - /* trim leading whitespace */ + /* trim leading whitespace */ while (i-- > 0 && (*eline == ' ' || *eline == '\t')) ++eline; - /* if theres nothing here, don't bother */ - if (i <= 0) - goto nxtscan; + /* if theres nothing here, don't bother */ + if( i <= 0) + continue ; - /* if is a while directive, make a block... */ - if (eline[0] == '!' && eline[1] == 'w' && eline[2] == 'h') { - whtemp = (struct while_block *)malloc(sizeof(struct while_block)); - if (whtemp == NULL) { - noram:mlwrite - ("%%Out of memory during while scan"); - failexit:freewhile - (scanner); - freewhile(whlist); - return FALSE; + /* if it is a while directive, make a block... */ + if( eline[0] == '!' + && eline[1] == 'w' + && eline[2] == 'h') { + whtemp = malloc( sizeof *whtemp) ; + if( whtemp == NULL) { + noram: + mloutstr( "%%Out of memory during while scan") ; + failexit: + freewhile( scanner) ; + freewhile( whlist) ; + return FALSE ; } - whtemp->w_begin = lp; - whtemp->w_type = BTWHILE; - whtemp->w_next = scanner; - scanner = whtemp; + + whtemp->w_begin = lp ; + whtemp->w_type = BTWHILE ; + whtemp->w_next = scanner ; + scanner = whtemp ; } /* if is a BREAK directive, make a block... */ - if (eline[0] == '!' && eline[1] == 'b' && eline[2] == 'r') { + if( eline[0] == '!' + && eline[1] == 'b' + && eline[2] == 'r') { if (scanner == NULL) { - mlwrite - ("%%!BREAK outside of any !WHILE loop"); - goto failexit; + mloutstr( "%%!BREAK outside of any !WHILE loop") ; + goto failexit ; } - whtemp = (struct while_block *)malloc(sizeof(struct while_block)); - if (whtemp == NULL) - goto noram; + + whtemp = malloc( sizeof *whtemp) ; + if( whtemp == NULL) + goto noram ; + whtemp->w_begin = lp; whtemp->w_type = BTBREAK; whtemp->w_next = scanner; @@ -642,60 +604,62 @@ static int dobuf(struct buffer *bp) } /* if it is an endwhile directive, record the spot... */ - if (eline[0] == '!' && strncmp(&eline[1], "endw", 4) == 0) { + if( eline[0] == '!' + && strncmp( &eline[1], "endw", 4) == 0) { if (scanner == NULL) { - mlwrite - ("%%!ENDWHILE with no preceding !WHILE in '%s'", - bp->b_bname); - goto failexit; + mloutfmt( "%%!ENDWHILE with no preceding !WHILE in '%s'", + bp->b_bname) ; + goto failexit ; } + /* move top records from the scanner list to the whlist until we have moved all BREAK records and one WHILE record */ do { - scanner->w_end = lp; - whtemp = whlist; - whlist = scanner; - scanner = scanner->w_next; - whlist->w_next = whtemp; - } while (whlist->w_type == BTBREAK); + scanner->w_end = lp ; + whtemp = whlist ; + whlist = scanner ; + scanner = scanner->w_next ; + whlist->w_next = whtemp ; + } while( whlist->w_type == BTBREAK) ; } - - nxtscan: /* on to the next line */ - lp = lp->l_fp; } /* while and endwhile should match! */ - if (scanner != NULL) { - mlwrite("%%!WHILE with no matching !ENDWHILE in '%s'", - bp->b_bname); - goto failexit; + if( scanner != NULL) { + mloutfmt( "%%!WHILE with no matching !ENDWHILE in '%s'", bp->b_bname) ; + goto failexit ; } /* let the first command inherit the flags from the last one.. */ thisflag = lastflag; /* starting at the beginning of the buffer */ - hlp = bp->b_linep; - lp = hlp->l_fp; - while (lp != hlp) { - /* allocate eline and copy macro line to it */ - linlen = lp->l_used; - if ((einit = eline = malloc(linlen + 1)) == NULL) { - mlwrite("%%Out of Memory during macro execution"); - freewhile(whlist); - return FALSE; + hlp = bp->b_linep ; + char *einit = NULL ; /* initial value of eline */ + int status = TRUE ; /* status of command execution */ + boolean done = FALSE ; + for( line_p lp = hlp->l_fp ; !done && (lp != hlp) ; lp = lp->l_fp) { + if( einit) + free( einit) ; + + /* allocate eline and copy macro line to it */ + linlen = lp->l_used ; + einit = eline = malloc( linlen + 1) ; + if( eline == NULL) { + status = mloutfail( "%%Out of Memory during macro execution") ; + break ; } mystrscpy( eline, lp->l_text, linlen + 1) ; - /* trim leading whitespace */ - while (*eline == ' ' || *eline == '\t') - ++eline; + /* trim leading whitespace */ + while( *eline == ' ' || *eline == '\t') + ++eline ; - /* dump comments and blank lines */ - if (*eline == ';' || *eline == '#' || *eline == 0) - goto onward; + /* dump comments and blank lines */ + if( *eline == ';' || *eline == '#' || *eline == 0) + continue ; #if DEBUGM /* if $debug == TRUE, every line to execute @@ -708,269 +672,236 @@ static int dobuf(struct buffer *bp) /* debug macro name, if levels and lastly the line */ c = mdbugout( "<<<%s:%d:%s>>>", bp->b_bname, execlevel, eline) ; if( c == abortc) { - freewhile( whlist) ; - free( einit) ; - return FALSE ; + status = FALSE ; + break ; } else if( c == metac) { macbug = FALSE ; } } #endif - /* Parse directives here.... */ - dirnum = -1; - if (*eline == '!') { - /* Find out which directive this is */ - ++eline; - for (dirnum = 0; dirnum < NUMDIRS; dirnum++) - if (strncmp(eline, dname[dirnum], - strlen(dname[dirnum])) == 0) - break; + /* Parse directives here.... */ + unsigned dirnum = NUMDIRS ; /* directive index */ + if( *eline == '!') { + /* Find out which directive this is */ + ++eline ; + for( dirnum = 0 ; dirnum < NUMDIRS ; dirnum++) + if( !strncmp( eline, dname[ dirnum], strlen( dname[ dirnum]))) + break ; - /* and bitch if it's illegal */ - if (dirnum == NUMDIRS) { - mlwrite("%%Unknown Directive"); - freewhile(whlist); - free( einit) ; - return FALSE; + /* and bitch if it's illegal */ + if( dirnum == NUMDIRS) { + status = mloutfail( "%%Unknown Directive") ; + break ; } /* service only the !ENDM macro here */ - if (dirnum == DENDM) { - mstore = FALSE; - bstore = NULL; - goto onward; + if( dirnum == DENDM) { + mstore = FALSE ; + bstore = NULL ; + continue ; } /* restore the original eline.... */ - --eline; + --eline ; } /* if macro store is on, just salt this away */ - if (mstore) { - /* allocate the space for the line */ - linlen = strlen(eline); - if ((mp = lalloc(linlen)) == NULL) { - free( einit) ; - mlwrite( "Out of memory while storing macro") ; - return FALSE ; + if( mstore) { + /* allocate the space for the line */ + linlen = strlen( eline) ; + line_p mp = lalloc( linlen) ; + if( mp == NULL) { + status = mloutfail( "Out of memory while storing macro") ; + break ; } /* copy the text into the new line */ - for (i = 0; i < linlen; ++i) - lputc(mp, i, eline[i]); + for( i = 0 ; i < linlen ; ++i) + lputc( mp, i, eline[ i]) ; /* attach the line to the end of the buffer */ - bstore->b_linep->l_bp->l_fp = mp; - mp->l_bp = bstore->b_linep->l_bp; - bstore->b_linep->l_bp = mp; - mp->l_fp = bstore->b_linep; - goto onward; + bstore->b_linep->l_bp->l_fp = mp ; + mp->l_bp = bstore->b_linep->l_bp ; + bstore->b_linep->l_bp = mp ; + mp->l_fp = bstore->b_linep ; + continue ; } - - force = FALSE; + int force = FALSE ; /* force TRUE result? */ /* dump comments */ - if (*eline == '*') - goto onward; + if( *eline == '*') + continue ; /* now, execute directives */ - if (dirnum != -1) { + if( dirnum != NUMDIRS) { /* skip past the directive */ - while (*eline && *eline != ' ' && *eline != '\t') - ++eline; + while( *eline && *eline != ' ' && *eline != '\t') + ++eline ; + execstr = eline; - - switch (dirnum) { - case DIF: /* IF directive */ - /* grab the value of the logical exp */ - if (execlevel == 0) { + switch( dirnum) { + case DIF: /* IF directive */ + /* grab the value of the logical exp */ + if( execlevel == 0) { if( macarg( tkn, sizeof tkn) != TRUE) - goto eexec; - if (stol(tkn) == FALSE) - ++execlevel; + done = TRUE ; + else if( stol( tkn) == FALSE) + ++execlevel ; } else - ++execlevel; - goto onward; + ++execlevel ; - case DWHILE: /* WHILE directive */ - /* grab the value of the logical exp */ - if (execlevel == 0) { - if( macarg( tkn, sizeof tkn) != TRUE) - goto eexec; - if (stol(tkn) == TRUE) - goto onward; + continue ; + + case DWHILE: /* WHILE directive */ + /* grab the value of the logical exp */ + if( execlevel == 0) { + if( macarg( tkn, sizeof tkn) != TRUE) { + done = TRUE ; + continue ; + } else if( stol( tkn) == TRUE) + continue ; } - /* drop down and act just like !BREAK */ + /* drop down and act just like !BREAK */ /* fallthrough */ case DBREAK: /* BREAK directive */ - if (dirnum == DBREAK && execlevel) - goto onward; + if( dirnum == DBREAK && execlevel) + continue ; /* jump down to the endwhile */ /* find the right while loop */ - whtemp = whlist; - while (whtemp) { - if (whtemp->w_begin == lp) - break; - whtemp = whtemp->w_next; - } - - if (whtemp == NULL) { - mlwrite - ("%%Internal While loop error"); - freewhile(whlist); - return FALSE; - } + for( whtemp = whlist ; whtemp ; whtemp = whtemp->w_next) + if( whtemp->w_begin == lp) + break ; + if( whtemp == NULL) { + status = mloutfail( "%%Internal While loop error") ; + done = TRUE ; + } else /* reset the line pointer back.. */ - lp = whtemp->w_end; - goto onward; + lp = whtemp->w_end ; - case DELSE: /* ELSE directive */ - if (execlevel == 1) - --execlevel; - else if (execlevel == 0) - ++execlevel; - goto onward; + continue ; - case DENDIF: /* ENDIF directive */ + case DELSE: /* ELSE directive */ + if( execlevel == 1) + --execlevel ; + else if( execlevel == 0) + ++execlevel ; + + continue ; + + case DENDIF: /* ENDIF directive */ if (execlevel) --execlevel; - goto onward; - case DGOTO: /* GOTO directive */ - /* .....only if we are currently executing */ - if (execlevel == 0) { - /* grab label to jump to */ + continue ; + + case DGOTO: /* GOTO directive */ + /* .....only if we are currently executing */ + if( execlevel == 0) { + line_p glp ; /* line to goto */ + + /* grab label to jump to */ eline = token( eline, golabel, sizeof golabel) ; - linlen = strlen(golabel); - glp = hlp->l_fp; - while (glp != hlp) { - if (*glp->l_text == '*' && - (strncmp - (&glp->l_text[1], - golabel, - linlen) == 0)) { - lp = glp; - goto onward; + linlen = strlen( golabel) ; + for( glp = hlp->l_fp ; glp != hlp ; glp = glp->l_fp) { + if( *glp->l_text == '*' + && !strncmp( &glp->l_text[ 1], golabel, linlen)) { + break ; } - glp = glp->l_fp; } - mlwrite("%%No such label"); - freewhile(whlist); - return FALSE; + + if( glp == hlp) { + status = mloutfail( "%%No such label") ; + done = TRUE ; + } else + lp = glp ; } - goto onward; + + continue ; case DRETURN: /* RETURN directive */ - if (execlevel == 0) - goto eexec; - goto onward; + if( execlevel == 0) + done = TRUE ; + + continue ; case DENDWHILE: /* ENDWHILE directive */ - if (execlevel) { - --execlevel; - goto onward; + if( execlevel) { + --execlevel ; + continue ; } else { - /* find the right while loop */ - whtemp = whlist; - while (whtemp) { - if (whtemp->w_type == - BTWHILE - && whtemp->w_end == lp) - break; - whtemp = whtemp->w_next; - } + /* find the right while loop */ + for( whtemp = whlist ; whtemp ; whtemp = whtemp->w_next) + if( whtemp->w_type == BTWHILE + && whtemp->w_end == lp) + break ; - if (whtemp == NULL) { - mlwrite - ("%%Internal While loop error"); - freewhile(whlist); - return FALSE; - } + if( whtemp == NULL) { + status = mloutfail( "%%Internal While loop error") ; + done = TRUE ; + } else + /* reset the line pointer back.. */ + lp = whtemp->w_begin->l_bp ; - /* reset the line pointer back.. */ - lp = whtemp->w_begin->l_bp; - goto onward; + continue ; } case DFORCE: /* FORCE directive */ - force = TRUE; + force = TRUE ; } } - /* execute the statement */ - status = docmd(eline); - if (force) /* force the status */ - status = TRUE; + /* execute the statement */ + status = docmd( eline) ; + if( force) /* force the status */ + status = TRUE ; - /* check for a command error */ - if (status != TRUE) { - /* look if buffer is showing */ - wp = wheadp; - while (wp != NULL) { - if (wp->w_bufp == bp) { - /* and point it */ - wp->w_dotp = lp; - wp->w_doto = 0; - wp->w_flag |= WFHARD; + /* check for a command error */ + if( status != TRUE) { + /* look if buffer is showing */ + for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp) { + if( wp->w_bufp == bp) { + /* and point it */ + wp->w_dotp = lp ; + wp->w_doto = 0 ; + wp->w_flag |= WFHARD ; } - wp = wp->w_wndp; } - /* in any case set the buffer . */ - bp->b_dotp = lp; - bp->b_doto = 0; - free(einit); - execlevel = 0; - freewhile(whlist); - return status; - } - onward: /* on to the next line */ - free(einit); - lp = lp->l_fp; + /* in any case set the buffer . */ + bp->b_dotp = lp ; + bp->b_doto = 0 ; + break ; + } } - eexec: /* exit the current function */ - execlevel = 0; - freewhile(whlist); - return TRUE; + execlevel = 0 ; + freewhile( whlist) ; + if( einit) + free( einit) ; + + return status ; } -/* - * free a list of while block pointers - * - * struct while_block *wp; head of structure to free - */ -static void freewhile(struct while_block *wp) -{ - if (wp == NULL) - return; - if (wp->w_next) - freewhile(wp->w_next); - free(wp); -} -/* - * execute a series of commands in a file +/* execute a series of commands in a file * * int f, n; default flag and numeric arg to pass on to file */ -int execfile( int f, int n) { - int status ; /* return status of name query */ +BINDABLE( execfile) { char *fname ; /* name of file to execute */ - char *fspec ; /* full file spec */ - status = newmlarg( &fname, "Execute file: ", 0) ; + int status = newmlarg( &fname, "Execute file: ", 0) ; if( status != TRUE) return status ; /* look up the path for the file */ - fspec = flook( fname, FALSE) ; /* used to be TRUE, P.K. */ + char *fspec = flook( fname, FALSE) ; /* used to be TRUE, P.K. */ free( fname) ; /* if it isn't around */ @@ -984,46 +915,54 @@ int execfile( int f, int n) { return status ; } -/* - * dofile: +/* dofile: * yank a file into a buffer and execute it * if there are no errors, delete the buffer on exit * * char *fname; file name to execute */ int dofile( const char *fname) { - struct buffer *bp; /* buffer to place file to exeute */ - struct buffer *cb; /* temp to hold current buf while we read */ - int status; /* results of various calls */ - bname_t bname ; /* name of buffer */ + bname_t bname ; /* name of buffer */ - makename(bname, fname); /* derive the name of the buffer */ - unqname(bname); /* make sure we don't stomp things */ - if ((bp = bfind(bname, TRUE, 0)) == NULL) /* get the needed buffer */ - return FALSE; + makename( bname, fname) ; /* derive the name of the buffer */ + unqname( bname) ; /* make sure we don't stomp things */ + buffer_p bp = bfind( bname, TRUE, 0) ; /* get the needed buffer */ + if( bp == NULL) + return FALSE ; - bp->b_mode = MDVIEW; /* mark the buffer as read only */ - cb = curbp; /* save the old buffer */ - curbp = bp; /* make this one current */ - /* and try to read in the file to execute */ - if ((status = readin(fname, FALSE)) != TRUE) { - curbp = cb; /* restore the current buffer */ - return status; - } + bp->b_mode = MDVIEW ; /* mark the buffer as read only */ + buffer_p cb = curbp ; /* save the old buffer */ + curbp = bp ; /* make this one current */ +/* and try to read in the file to execute */ + int status = readin( fname, FALSE) ; + curbp = cb ; /* restore the current buffer */ + if( status == TRUE) { + /* go execute it! */ + status = dobuf( bp) ; + if( status == TRUE && bp->b_nwnd == 0) + /* if not displayed, remove the now unneeded buffer and exit */ + zotbuf( bp) ; + } - /* go execute it! */ - curbp = cb; /* restore the current buffer */ - if ((status = dobuf(bp)) != TRUE) - return status; + return status ; +#if 0 + if( status != TRUE) + return status ; + +/* go execute it! */ + status = dobuf( bp) ; + if( status != TRUE) + return status ; /* if not displayed, remove the now unneeded buffer and exit */ - if (bp->b_nwnd == 0) - zotbuf(bp); - return TRUE; + if( bp->b_nwnd == 0) + zotbuf( bp) ; + + return TRUE ; +#endif } -/* - * cbuf: +/* cbuf: * Execute the contents of a numbered buffer * * int f, n; default flag and numeric arg @@ -1039,7 +978,7 @@ static int cbuf( int f, int n, int bufnum) { /* execute buffer of numbered macro [1..40] */ #define cbufnn( nn) \ -int cbuf##nn( int f, int n) { \ +BINDABLE( cbuf##nn) { \ return cbuf( f, n, nn) ; \ } diff --git a/exec.h b/exec.h index 849ac0c..638064f 100644 --- a/exec.h +++ b/exec.h @@ -1,69 +1,65 @@ +/* exec.h -- bindable functions to execute functions, macros and procedures */ #ifndef _EXEC_H_ -#define _EXEC_H_ +# define _EXEC_H_ -#include "retcode.h" +#include "names.h" +extern boolean clexec ; /* command line execution flag */ -#define PROC 1 /* named procedures */ - -#if PROC -int storeproc( int f, int n) ; -int execproc( int f, int n) ; -#endif - - -extern boolean clexec ; /* command line execution flag */ - - -int namedcmd( int f, int n) ; -int execcmd( int f, int n) ; +int dofile( const char *fname) ; void gettoken( char *tok, int maxtoksize) ; boolean gettokval( char *tok, int maxtoksize) ; char *getnewtokval( void) ; -int storemac( int f, int n) ; -int execbuf( int f, int n) ; -int execfile( int f, int n) ; -int dofile( const char *fname) ; -int cbuf1( int f, int n) ; -int cbuf2( int f, int n) ; -int cbuf3( int f, int n) ; -int cbuf4( int f, int n) ; -int cbuf5( int f, int n) ; -int cbuf6( int f, int n) ; -int cbuf7( int f, int n) ; -int cbuf8( int f, int n) ; -int cbuf9( int f, int n) ; -int cbuf10( int f, int n) ; -int cbuf11( int f, int n) ; -int cbuf12( int f, int n) ; -int cbuf13( int f, int n) ; -int cbuf14( int f, int n) ; -int cbuf15( int f, int n) ; -int cbuf16( int f, int n) ; -int cbuf17( int f, int n) ; -int cbuf18( int f, int n) ; -int cbuf19( int f, int n) ; -int cbuf20( int f, int n) ; -int cbuf21( int f, int n) ; -int cbuf22( int f, int n) ; -int cbuf23( int f, int n) ; -int cbuf24( int f, int n) ; -int cbuf25( int f, int n) ; -int cbuf26( int f, int n) ; -int cbuf27( int f, int n) ; -int cbuf28( int f, int n) ; -int cbuf29( int f, int n) ; -int cbuf30( int f, int n) ; -int cbuf31( int f, int n) ; -int cbuf32( int f, int n) ; -int cbuf33( int f, int n) ; -int cbuf34( int f, int n) ; -int cbuf35( int f, int n) ; -int cbuf36( int f, int n) ; -int cbuf37( int f, int n) ; -int cbuf38( int f, int n) ; -int cbuf39( int f, int n) ; -int cbuf40( int f, int n) ; +/* Bindable functions */ +BINDABLE( execbuf) ; +BINDABLE( execcmd) ; +BINDABLE( execfile) ; +BINDABLE( execproc) ; +BINDABLE( namedcmd) ; +BINDABLE( storemac) ; +BINDABLE( storeproc) ; +BINDABLE( cbuf1) ; +BINDABLE( cbuf2) ; +BINDABLE( cbuf3) ; +BINDABLE( cbuf4) ; +BINDABLE( cbuf5) ; +BINDABLE( cbuf6) ; +BINDABLE( cbuf7) ; +BINDABLE( cbuf8) ; +BINDABLE( cbuf9) ; +BINDABLE( cbuf10) ; +BINDABLE( cbuf11) ; +BINDABLE( cbuf12) ; +BINDABLE( cbuf13) ; +BINDABLE( cbuf14) ; +BINDABLE( cbuf15) ; +BINDABLE( cbuf16) ; +BINDABLE( cbuf17) ; +BINDABLE( cbuf18) ; +BINDABLE( cbuf19) ; +BINDABLE( cbuf20) ; +BINDABLE( cbuf21) ; +BINDABLE( cbuf22) ; +BINDABLE( cbuf23) ; +BINDABLE( cbuf24) ; +BINDABLE( cbuf25) ; +BINDABLE( cbuf26) ; +BINDABLE( cbuf27) ; +BINDABLE( cbuf28) ; +BINDABLE( cbuf29) ; +BINDABLE( cbuf30) ; +BINDABLE( cbuf31) ; +BINDABLE( cbuf32) ; +BINDABLE( cbuf33) ; +BINDABLE( cbuf34) ; +BINDABLE( cbuf35) ; +BINDABLE( cbuf36) ; +BINDABLE( cbuf37) ; +BINDABLE( cbuf38) ; +BINDABLE( cbuf39) ; +BINDABLE( cbuf40) ; #endif + +/* end of exec.h */ diff --git a/file.c b/file.c index d372541..68e21e5 100644 --- a/file.c +++ b/file.c @@ -1,13 +1,11 @@ /* file.c -- implements file.h */ #include "file.h" -/* file.c - * - * The routines in this file handle the reading, writing - * and lookup of disk files. All of details about the - * reading and writing of the disk are in "fileio.c". - * - * modified by Petri Kutvonen +/* The routines in this file handle the reading, writing and lookup of disk + files. All of details about the reading and writing of the disk are in + "fileio.c". + + modified by Petri Kutvonen */ #include @@ -233,8 +231,8 @@ int getfile( const char *fname, boolean lockfl) { return s; } -/* - * Read file "fname" into the current buffer, blowing away any text + +/* Read file "fname" into the current buffer, blowing away any text * found there. Called by both the read and find commands. Return * the final status of the read. Also called by the mainline, to * read in a file specified on the command line as an argument. @@ -284,10 +282,10 @@ int readin(const char *fname, boolean lockfl) /* read the file in */ mloutstr( "(Reading file)") ; - while ((s = ffgetline()) == FIOSUC) { + while( (s = ffgetline()) == FIOSUC) { line_p lp ; - if( nline >= 10000000 /* MAXNLINE Maximum # of lines from one file */ + if( nline >= 10000000 /* Maximum # of lines from one file */ || (lp = lalloc( fpayload)) == NULL) { s = FIOMEM ; /* Keep message on the */ break ; /* display. */ @@ -327,12 +325,11 @@ int readin(const char *fname, boolean lockfl) if( fcode == FCODE_UTF_8) curbp->b_mode |= MDUTF8 ; - if( s == FIOERR) { - errmsg = "I/O ERROR, " ; - curbp->b_flag |= BFTRUNC ; - } else if( s == FIOMEM) { - errmsg = "OUT OF MEMORY, " ; + if( s == FIOERR + || s == FIOMEM) { + errmsg = (s == FIOERR) ? "I/O ERROR, " : "OUT OF MEMORY, " ; curbp->b_flag |= BFTRUNC ; + curbp->b_mode |= MDVIEW ; /* force view mode as lost data */ } else errmsg = "" ; @@ -464,10 +461,8 @@ BINDABLE( filesave) { /* complain about truncated files */ if( (curbp->b_flag & BFTRUNC) != 0 - && mlyesno("Truncated file ... write it out") == FALSE) { - mloutfmt( "%B(Aborted)") ; - return FALSE ; - } + && mlyesno( "Truncated file ... write it out") == FALSE) + return mloutfail( "(Aborted)") ; return writeout( curbp->b_fname) ; } diff --git a/input.c b/input.c index 5ce7d44..2367b80 100644 --- a/input.c +++ b/input.c @@ -15,9 +15,10 @@ #include "bind.h" #include "estruct.h" #include "bindable.h" -#include "display.h" +#include "display.h" /* rubout(), echos(), echoc(), update() */ #include "exec.h" #include "isa.h" +#include "mlout.h" #include "names.h" #include "terminal.h" #include "utf8.h" @@ -38,12 +39,12 @@ kbdstate kbdmode = STOP ; /* current keyboard macro mode */ int lastkey = 0 ; /* last keystoke */ int kbdrep = 0 ; /* number of repetitions */ -int metac = CTRL | '[' ; /* current meta character */ -int ctlxc = CTRL | 'X' ; /* current control X prefix char */ -int reptc = CTRL | 'U' ; /* current universal repeat char */ -int abortc = CTRL | 'G' ; /* current abort command char */ +int metac = CTL_ | '[' ; /* current meta character */ +int ctlxc = CTL_ | 'X' ; /* current control X prefix char */ +int reptc = CTL_ | 'U' ; /* current universal repeat char */ +int abortc = CTL_ | 'G' ; /* current abort command char */ -const int nlc = CTRL | 'J' ; /* end of input char */ +const int nlc = CTL_ | 'J' ; /* end of input char */ void ue_system( const char *cmd) { @@ -66,7 +67,7 @@ int mlyesno( const char *prompt) for (;;) { /* prompt the user */ - mlwrite( "%s (y/n)? ", prompt) ; + mloutfmt( "%s (y/n)? ", prompt) ; /* get the response */ c = get1key() ; @@ -142,11 +143,11 @@ int newmlargt( char **outbufref, const char *prompt, int size) { /* * ectoc: * expanded character to character - * collapse the CTRL and SPEC flags back into an ascii code + * collapse the CTL_ and SPEC flags back into an ascii code */ int ectoc( int c) { - if( c & CTRL) - c ^= CTRL | 0x40 ; + if( c & CTL_) + c ^= CTL_ | 0x40 ; if( c & SPEC) c &= 255 ; @@ -189,10 +190,10 @@ nbind_p getname( void) { /* and match it off */ return fncmatch( buf) ; - } else if (c == ectoc(abortc)) { /* Bell, abort */ - ctrlg(FALSE, 0); - TTflush(); - return NULL; + } else if( c == ectoc(abortc)) { /* Bell, abort */ + ctrlg( FALSE, 1) ; + TTflush() ; + return NULL ; } else if (c == 0x7F || c == 0x08) { /* rubout/erase */ if (cpos != 0) { @@ -322,7 +323,7 @@ int tgetc(void) } /* GET1KEY: Get one keystroke. The only prefixes legal here are the SPEC - and CTRL prefixes. */ + and CTL_ prefixes. */ static int get1unicode( int *up) { /* Accept UTF-8 sequence */ int bytes ; @@ -343,7 +344,7 @@ static int get1unicode( int *up) { bytes = utf8_to_unicode( utf, 0, sizeof utf, (unicode_t *) up) ; } else { if( (c >= 0x00 && c <= 0x1F) || c == 0x7F) /* C0 control -> C- */ - c ^= CTRL | 0x40 ; + c ^= CTL_ | 0x40 ; *up = c ; bytes = 1 ; @@ -383,7 +384,7 @@ int getcmd( void) { c = *(kptr++) = get1key() ; if( c == 0x9B) goto foundCSI ; - else if( c == (CTRL | '[')) { + else if( c == (CTL_ | '[')) { /* fetch terminal sequence */ c = *(kptr++) = get1key() ; if( c == 'O') { /* F1 .. F4 */ @@ -407,7 +408,7 @@ int getcmd( void) { mask = META ; if( (v - 1) & 4) - mask |= CTRL ; + mask |= CTL_ ; v = v1 ; } @@ -513,7 +514,7 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) quote_f = FALSE; /* prompt the user for the input string */ - mlwrite( "%s", prompt); + mloutstr( prompt); for (;;) { #if COMPLC @@ -538,23 +539,22 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) } /* If it is a , change it to a */ - if( c == (CTRL | 'M')) - c = CTRL | 0x40 | '\n' ; + if( c == (CTL_ | 'M')) + c = CTL_ | 0x40 | '\n' ; if( c == eolchar) { /* if they hit the line terminator, wrap it up */ buf[ cpos] = 0 ; /* clear the message line */ - mlwrite(""); + mloutstr( "") ; /* if we default the buffer, return FALSE */ retval = cpos != 0 ; break ; } else if( c == abortc) { /* Abort the input? */ - ctrlg( FALSE, 0) ; - retval = ABORT ; + retval = ctrlg( FALSE, 1) ; break ; } @@ -574,7 +574,7 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) } } else if( c == 0x15) { /* C-U, kill */ - mlwrite( "%s", prompt) ; + mloutstr( prompt) ; cpos = 0 ; #if COMPLC } else if( (c == 0x09 || c == ' ') && file_f) { @@ -588,7 +588,7 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar) didtry = 1; ocpos = cpos; - mlwrite( "%s", prompt) ; + mloutstr( prompt) ; while( cpos != 0) { c = buf[ --cpos] ; if( c == '*' || c == '?') { diff --git a/isearch.c b/isearch.c index 615ce1c..fb6eddd 100644 --- a/isearch.c +++ b/isearch.c @@ -1,8 +1,7 @@ +/* isearch.c -- implements isearch.h */ #include "isearch.h" -/* isearch.c - * - * The functions in this file implement commands that perform incremental +/* The functions in this file implement commands that perform incremental * searches in the forward and backward directions. This "ISearch" command * is intended to emulate the same command from the original EMACS * implementation (ITS). Contains references to routines internal to @@ -24,7 +23,6 @@ * Modified by Petri Kutvonen */ -#include #include #include "basic.h" @@ -42,8 +40,6 @@ /* * Incremental search defines. */ -#if ISRCH - #define CMDBUFLEN 256 /* Length of our command buffer */ #define IS_ABORT 0x07 /* Abort the isearch */ @@ -60,10 +56,7 @@ /* IS_QUIT is no longer used, the variable metac is used instead */ -#endif - - -static int isearch( int f, int n) ; +static BINDABLE( isearch) ; /* internal use, not to be bound */ static int checknext( char chr, char *patrn, int dir) ; static int scanmore( char *patrn, int dir) ; static int match_pat( char *patrn) ; @@ -73,8 +66,6 @@ static int uneat( void) ; static void reeat( int c) ; -#if ISRCH - static int echo_char(int c, int col); /* A couple of "own" variables for re-eat */ @@ -89,12 +80,10 @@ static int cmd_offset; /* Current offset into command buff */ static int cmd_reexecute = -1; /* > 0 if re-executing command */ -/* - * Subroutine to do incremental reverse search. It actually uses the +/* Subroutine to do incremental reverse search. It actually uses the * same code as the normal incremental search, as both can go both ways. */ -int risearch(int f, int n) -{ +BINDABLE( risearch) { struct line *curline; /* Current line on entry */ int curoff; /* Current offset on entry */ @@ -124,11 +113,10 @@ int risearch(int f, int n) return TRUE; } -/* - * Again, but for the forward direction + +/* Again, but for the forward direction */ -int fisearch(int f, int n) -{ +BINDABLE( fisearch) { struct line *curline; /* Current line on entry */ int curoff; /* Current offset on entry */ @@ -156,8 +144,8 @@ int fisearch(int f, int n) return TRUE; } -/* - * Subroutine to do an incremental search. In general, this works similarly + +/* Subroutine to do an incremental search. In general, this works similarly * to the older micro-emacs search function, except that the search happens * as each character is typed, with the screen and cursor updated with each * new search character. @@ -182,8 +170,7 @@ int fisearch(int f, int n) * exists (or until the search is aborted). */ -static int isearch(int f, int n) -{ +static BINDABLE( isearch) { int status; /* Search status */ int col; /* prompt column */ unsigned cpos ; /* character number in search string */ @@ -541,8 +528,6 @@ static void reeat(int c) saved_get_char = term.t_getchar; /* Save the char get routine */ term.t_getchar = uneat; /* Replace it with ours */ } -#else -int isearch(int f, int n) -{ -} -#endif + + +/* end of isearch.c */ diff --git a/isearch.h b/isearch.h index 871ae9c..e687524 100644 --- a/isearch.h +++ b/isearch.h @@ -1,11 +1,11 @@ +/* isearch.h -- incremental search */ #ifndef __ISEARCH_H__ #define __ISEARCH_H__ -#define ISRCH 1 /* Incremental searches like ITS EMACS */ +# include "names.h" /* BINDABLE */ -#if ISRCH -int risearch( int f, int n) ; -int fisearch( int f, int n) ; -#endif +BINDABLE( risearch) ; +BINDABLE( fisearch) ; #endif +/* end of isearch */ diff --git a/line.c b/line.c index f093e1d..d3d90a5 100644 --- a/line.c +++ b/line.c @@ -249,12 +249,11 @@ void lchange(int flag) * * int f, n; default flag and numeric argument */ -int insspace(int f, int n) -{ +BINDABLE( insspace) { assert( !(curbp->b_mode & MDVIEW)) ; - linsert(n, ' '); - backchar(f, n); - return TRUE; + linsert( n, ' ') ; + backchar( f, n) ; + return TRUE ; } /* @@ -821,8 +820,7 @@ BINDABLE( yank) { * VIEW (read-only) mode */ boolean rdonly( void) { - mloutfmt( "%B(Key illegal in VIEW mode)") ; - return FALSE ; + return mloutfail( "(Key illegal in VIEW mode)") ; } /* end of line.c */ diff --git a/mlout.c b/mlout.c index 3c7ebd0..7ac7713 100644 --- a/mlout.c +++ b/mlout.c @@ -12,4 +12,9 @@ void mloutstr( const char *str) { mloutfmt( (*str) ? "%s" : "", str) ; } +boolean mloutfail( const char *msg) { + mloutfmt( "%B%s", msg) ; + return FALSE ; +} + /* end of mlout.c */ diff --git a/mlout.h b/mlout.h index c201baa..4bb6c22 100644 --- a/mlout.h +++ b/mlout.h @@ -3,9 +3,12 @@ #ifndef __MLOUT_H__ #define __MLOUT_H__ +#include "retcode.h" + extern void (*mloutfmt)( const char *, ...) ; void mloutstr( const char *str) ; +boolean mloutfail( const char *msg) ; /* output with BELL and return FALSE */ #endif /* __MLOUT_H__ */ diff --git a/names.c b/names.c index ae7c45a..378d260 100644 --- a/names.c +++ b/names.c @@ -33,51 +33,51 @@ const name_bind names[] = { - {" abort-command", ctrlg, CTRL | 'G'} , + {" abort-command", ctrlg, CTL_ | 'G'} , {" add-global-mode", setgmode, META | 'M'} , {" add-mode", setemode, CTLX | 'M'} , {" apropos", apro, META | 'A'} , - {" backward-character", (fnp_t) backchar, CTRL | 'B'} , - {" begin-macro", ctlxlp, CTLX | '('} , + {" backward-character", (fnp_t) backchar, CTL_ | 'B'} , + {" begin-macro", (fnp_t) ctlxlp, CTLX | '('} , {" beginning-of-file", (fnp_t) gotobob, META | '<'} , - {" beginning-of-line", (fnp_t) gotobol, CTRL | 'A'} , + {" beginning-of-line", (fnp_t) gotobol, CTL_ | 'A'} , {" bind-to-key", bindtokey, META | 'K'} , {" buffer-position", showcpos, CTLX | '='} , - {"!case-region-lower", lowerregion, CTLX | CTRL | 'L'} , - {"!case-region-upper", upperregion, CTLX | CTRL | 'U'} , + {"!case-region-lower", lowerregion, CTLX | CTL_ | 'L'} , + {"!case-region-upper", upperregion, CTLX | CTL_ | 'U'} , {"!case-word-capitalize", capword, META | 'C'} , {"!case-word-lower", lowerword, META | 'L'} , {"!case-word-upper", upperword, META | 'U'} , {" change-file-name", filename, CTLX | 'N'} , - {" change-screen-size", newsize, META | CTRL | 'D'} , /* M^S */ - {" change-screen-width", newwidth, META | CTRL | 'T'} , - {" clear-and-redraw", redraw, CTRL | 'L'} , - {" clear-message-line", clrmes, 0} , + {" change-screen-size", newsize, META | CTL_ | 'D'} , /* M^S */ + {" change-screen-width", newwidth, META | CTL_ | 'T'} , + {" clear-and-redraw", (fnp_t) redraw, CTL_ | 'L'} , + {" clear-message-line", (fnp_t) clrmes, 0} , {" copy-region", copyregion, META | 'W'} , - {" count-words", wordcount, META | CTRL | 'C'} , - {" ctlx-prefix", cex, CTRL | 'X'} , - {"!delete-blank-lines", deblank, CTLX | CTRL | 'O'} , + {" count-words", wordcount, META | CTL_ | 'C'} , + {" ctlx-prefix", (fnp_t) cex, CTL_ | 'X'} , + {"!delete-blank-lines", deblank, CTLX | CTL_ | 'O'} , {" delete-buffer", killbuffer, CTLX | 'K'} , - {" delete-global-mode", delgmode, META | CTRL | 'M'} , - {" delete-mode", delmode, CTLX | CTRL | 'M'} , - {"!delete-next-character", forwdel, CTRL | 'D'} , + {" delete-global-mode", delgmode, META | CTL_ | 'M'} , + {" delete-mode", delmode, CTLX | CTL_ | 'M'} , + {"!delete-next-character", forwdel, CTL_ | 'D'} , {"!delete-next-word", delfword, META | 'D'} , {" delete-other-windows", onlywind, CTLX | '1'} , - {"!delete-previous-character", backdel, CTRL | 'H'} , /* ^? */ - {"!delete-previous-word", delbword, META | CTRL | 'H'} , /* M^? */ + {"!delete-previous-character", backdel, CTL_ | 'H'} , /* ^? */ + {"!delete-previous-word", delbword, META | CTL_ | 'H'} , /* M^? */ {" delete-window", delwind, CTLX | '0'} , {" describe-bindings", desbind, 0} , {" describe-key", deskey, CTLX | '?'} , - {"!detab-line", detab, CTLX | CTRL | 'D'} , /* X^A */ - {" end-macro", ctlxrp, CTLX | ')'} , + {"!detab-line", detab, CTLX | CTL_ | 'D'} , /* X^A */ + {" end-macro", (fnp_t) ctlxrp, CTLX | ')'} , {" end-of-file", (fnp_t) gotoeob, META | '>'} , - {" end-of-line", (fnp_t) gotoeol, CTRL | 'E'} , - {"!entab-line", entab, CTLX | CTRL | 'E'} , - {" exchange-point-and-mark", (fnp_t) swapmark, CTLX | CTRL | 'X'} , + {" end-of-line", (fnp_t) gotoeol, CTL_ | 'E'} , + {"!entab-line", entab, CTLX | CTL_ | 'E'} , + {" exchange-point-and-mark", (fnp_t) swapmark, CTLX | CTL_ | 'X'} , {" execute-buffer", execbuf, 0} , {" execute-command-line", execcmd, 0} , {" execute-file", execfile, 0} , - {" execute-macro", ctlxe, CTLX | 'E'} , + {" execute-macro", (fnp_t) ctlxe, CTLX | 'E'} , {" execute-macro-1", cbuf1, 0} , {" execute-macro-10", cbuf10, 0} , {" execute-macro-11", cbuf11, 0} , @@ -119,116 +119,106 @@ const name_bind names[] = { {" execute-macro-8", cbuf8, 0} , {" execute-macro-9", cbuf9, 0} , {" execute-named-command", namedcmd, META | 'X'} , -#if PROC - {" execute-procedure", execproc, META | CTRL | 'E'} , -#endif + {" execute-procedure", execproc, META | CTL_ | 'E'} , {" execute-program", execprg, CTLX | '$'} , - {" exit-emacs", quit, CTLX | CTRL | 'C'} , + {" exit-emacs", quit, CTLX | CTL_ | 'C'} , {"!fill-paragraph", fillpara, META | 'Q'} , {"!filter-buffer", filter_buffer, CTLX | '#'} , - {" find-file", filefind, CTLX | CTRL | 'F'} , - {" forward-character", (fnp_t) forwchar, CTRL | 'F'} , + {" find-file", filefind, CTLX | CTL_ | 'F'} , + {" forward-character", (fnp_t) forwchar, CTL_ | 'F'} , {" goto-line", gotoline, META | 'G'} , #if CFENCE - {" goto-matching-fence", getfence, META | CTRL | 'F'} , + {" goto-matching-fence", getfence, META | CTL_ | 'F'} , #endif {" grow-window", enlargewind, CTLX | 'Z'} , /* X^ */ - {"!handle-tab", insert_tab, CTRL | 'I'} , + {"!handle-tab", insert_tab, CTL_ | 'I'} , {" help", help, META | '?'} , {" hunt-backward", backhunt, 0} , {" hunt-forward", forwhunt, META | 'S'} , {" i-shell", spawncli, CTLX | 'C'} , -#if ISRCH {" incremental-search", fisearch, CTLX | 'S'} , -#endif - {"!insert-file", insfile, CTLX | CTRL | 'I'} , - {"!insert-space", insspace, CTRL | 'C'} , + {"!insert-file", insfile, CTLX | CTL_ | 'I'} , + {"!insert-space", insspace, CTL_ | 'C'} , {"!insert-string", istring, 0} , #if PKCODE {"!justify-paragraph", justpara, META | 'J'} , #endif - {"!kill-paragraph", killpara, META | CTRL | 'W'} , - {"!kill-region", killregion, CTRL | 'W'} , - {"!kill-to-end-of-line", killtext, CTRL | 'K'} , - {" list-buffers", listbuffers, CTLX | CTRL | 'B'} , - {" meta-prefix", metafn, CTRL | '['} , - {" move-window-down", mvdnwind, CTLX | CTRL | 'N'} , - {" move-window-up", mvupwind, CTLX | CTRL | 'P'} , - {" name-buffer", namebuffer, META | CTRL | 'N'} , - {"!newline", insert_newline, CTRL | 'M'} , - {"!newline-and-indent", indent, CTRL | 'J'} , + {"!kill-paragraph", killpara, META | CTL_ | 'W'} , + {"!kill-region", killregion, CTL_ | 'W'} , + {"!kill-to-end-of-line", killtext, CTL_ | 'K'} , + {" list-buffers", listbuffers, CTLX | CTL_ | 'B'} , + {" meta-prefix", (fnp_t) metafn, CTL_ | '['} , + {" move-window-down", mvdnwind, CTLX | CTL_ | 'N'} , + {" move-window-up", mvupwind, CTLX | CTL_ | 'P'} , + {" name-buffer", namebuffer, META | CTL_ | 'N'} , + {"!newline", insert_newline, CTL_ | 'M'} , + {"!newline-and-indent", indent, CTL_ | 'J'} , {" next-buffer", nextbuffer, CTLX | 'X'} , - {" next-line", (fnp_t) forwline, CTRL | 'N'} , - {" next-page", (fnp_t) forwpage, CTRL | 'V'} , + {" next-line", (fnp_t) forwline, CTL_ | 'N'} , + {" next-page", (fnp_t) forwpage, CTL_ | 'V'} , {" next-paragraph", gotoeop, META | 'N'} , {" next-window", nextwind, CTLX | 'O'} , {" next-word", forwword, META | 'F'} , - {" nop", nullproc, SPEC | META | 'C'}, /* hook */ - {"!open-line", openline, CTRL | 'O'} , + {" nop", (fnp_t) nullproc, META | SPEC | 'C'}, /* hook */ + {"!open-line", openline, CTL_ | 'O'} , {"!overwrite-string", ovstring, 0} , {" pipe-command", pipecmd, CTLX | '@'} , - {" previous-line", (fnp_t) backline, CTRL | 'P'} , - {" previous-page", (fnp_t) backpage, CTRL | 'Z'} , /* MV */ + {" previous-line", (fnp_t) backline, CTL_ | 'P'} , + {" previous-page", (fnp_t) backpage, CTL_ | 'Z'} , /* MV */ {" previous-paragraph", gotobop, META | 'P'} , {" previous-window", prevwind, CTLX | 'P'} , {" previous-word", backword, META | 'B'} , - {"!query-replace-string", qreplace, META | CTRL | 'R'} , + {"!query-replace-string", qreplace, META | CTL_ | 'R'} , {" quick-exit", quickexit, META | 'Z'} , - {"!quote-character", quote, CTRL | 'Q'} , /* also XQ */ - {"!read-file", fileread, CTLX | CTRL | 'R'} , - {" redraw-display", reposition, META | CTRL | 'L'} , /* M! */ + {"!quote-character", quote, CTL_ | 'Q'} , + {"!read-file", fileread, CTLX | CTL_ | 'R'} , + {" redraw-display", (fnp_t) reposition, META | CTL_ | 'L'} , {"!replace-string", sreplace, META | 'R'} , {" resize-window", resize, CTLX | 'W'} , {" restore-window", restwnd, 0} , -#if ISRCH {" reverse-incremental-search", risearch, CTLX | 'R'} , -#endif -#if PROC - {" run", execproc, 0} , // alias of execute-procedure -#endif - {"!save-file", filesave, CTLX | CTRL | 'S'} , /* also X^D */ + {" run", execproc, 0} , /* alias of execute-procedure */ + {"!save-file", filesave, CTLX | CTL_ | 'S'} , /* also X^D */ {" save-window", savewnd, 0} , - {" scroll-next-down", scrnextdw, META | CTRL | 'V'} , - {" scroll-next-up", scrnextup, META | CTRL | 'Z'} , - {" search-forward", forwsearch, CTRL | 'S'} , - {" search-reverse", backsearch, CTRL | 'R'} , + {" scroll-next-down", scrnextdw, META | CTL_ | 'V'} , + {" scroll-next-up", scrnextup, META | CTL_ | 'Z'} , + {" search-forward", forwsearch, CTL_ | 'S'} , + {" search-reverse", backsearch, CTL_ | 'R'} , {" select-buffer", usebuffer, CTLX | 'B'} , {" set", setvar, CTLX | 'A'} , {" set-fill-column", setfillcol, CTLX | 'F'} , {" set-mark", (fnp_t) setmark, META | ' '} , /* M. */ {" shell-command", spawn, CTLX | '!'} , - {" shrink-window", shrinkwind, CTLX | CTRL | 'Z'} , + {" shrink-window", shrinkwind, CTLX | CTL_ | 'Z'} , {" split-current-window", splitwind, CTLX | '2'} , {" store-macro", storemac, 0} , -#if PROC {" store-procedure", storeproc, 0} , -#endif #if BSD | SVR4 {" suspend-emacs", bktoshell, CTLX | 'D'} , /* BSD MS */ #endif - {"!transpose-characters", (fnp_t) twiddle, CTRL | 'T'} , - {"!trim-line", trim, CTLX | CTRL | 'T'} , - {" unbind-key", unbindkey, META | CTRL | 'K'} , - {" universal-argument", unarg, CTRL | 'U'} , + {"!transpose-characters", (fnp_t) twiddle, CTL_ | 'T'} , + {"!trim-line", trim, CTLX | CTL_ | 'T'} , + {" unbind-key", unbindkey, META | CTL_ | 'K'} , + {" universal-argument", (fnp_t) unarg, CTL_ | 'U'} , {" unmark-buffer", unmark, META | '~'} , {" update-screen", upscreen, 0} , - {" view-file", viewfile, CTLX | CTRL | 'V'} , - {"!wrap-word", wrapword, SPEC | META | 'W'} , /* hook */ - {" write-file", filewrite, CTLX | CTRL | 'W'} , + {" view-file", viewfile, CTLX | CTL_ | 'V'} , + {"!wrap-word", wrapword, META | SPEC | 'W'} , /* hook */ + {" write-file", filewrite, CTLX | CTL_ | 'W'} , {" write-message", writemsg, 0} , - {"!yank", yank, CTRL | 'Y'} , + {"!yank", yank, CTL_ | 'Y'} , {" ", NULL, 0}, /* extra key mapping */ -// { NULL, newsize, META | CTRL | 'S'}, - { NULL, backdel, CTRL | '?'}, - { NULL, delbword, META | CTRL | '?'}, - { NULL, detab, CTLX | CTRL | 'A'}, +// { NULL, newsize, META | CTL_ | 'S'}, + { NULL, backdel, CTL_ | '?'}, + { NULL, delbword, META | CTL_ | '?'}, + { NULL, detab, CTLX | CTL_ | 'A'}, { NULL, enlargewind, CTLX | '^'}, { NULL, (fnp_t) backpage, META | 'V'}, { NULL, quote, CTLX | 'Q'}, - { NULL, reposition, META | '!'}, -//detab { NULL, filesave, CTLX | CTRL | 'D'}, + { NULL, (fnp_t) reposition, META | '!'}, +//detab { NULL, filesave, CTLX | CTL_ | 'D'}, { NULL, (fnp_t) setmark, META | '.'}, // { NULL, bktoshell, META | 'S'}, @@ -246,14 +236,14 @@ const name_bind names[] = { { NULL, help, SPEC | 'P'}, /* F1 */ /* hooks */ - { NULL, nullproc, SPEC | META | 'R'}, /* hook */ - { NULL, nullproc, SPEC | META | 'X'}, /* hook */ + { NULL, (fnp_t) nullproc, META | SPEC | 'R'}, /* hook */ + { NULL, (fnp_t) nullproc, META | SPEC | 'X'}, /* hook */ { NULL, NULL, 0} } ; -static int lastnmidx = 0 ; /* index of last name entry */ +static int lastnmidx = 0 ; /* index of last name entry */ kbind_p keytab ; static int ktsize = 140 ; /* last check: need at least 133 + 1 */ @@ -282,6 +272,9 @@ boolean init_bindings( void) { /* Add key definition */ if( nbp->n_keycode) { kbind_p ktp = setkeybinding( nbp->n_keycode, nbp) ; + if( ktp->k_code == 0) /* Table full, no memory left */ + return FALSE ; + /* check it was indeed an insertion at end of table not a * key code re-definition */ assert( (++ktp)->k_code == 0) ; @@ -293,14 +286,20 @@ boolean init_bindings( void) { /* Process extra key bindings if any */ for( nbp++ ; nbp->n_func != NULL ; nbp++) { - /* Check entry */ + /* Check entry: a keycode and no name */ assert( nbp->n_keycode && (nbp->n_name == NULL)) ; /* Look for corresponding function and add extra key binding */ nbind_p fnbp ; for( fnbp = names ; fnbp->n_func != NULL ; fnbp++) if( fnbp->n_func == nbp->n_func) { - setkeybinding( nbp->n_keycode, fnbp) ; + kbind_p ktp = setkeybinding( nbp->n_keycode, fnbp) ; + if( ktp->k_code == 0) /* Table full, no memory left */ + return FALSE ; + + /* check it was indeed an insertion at end of table not a + * key code re-definition */ + assert( (++ktp)->k_code == 0) ; break ; } @@ -370,8 +369,8 @@ boolean delkeybinding( unsigned key) { return FALSE ; } -/* - * This function looks a key binding up in the binding table + +/* This function looks a key binding up in the binding table * * int c; key to find what is bound to it */ @@ -416,23 +415,26 @@ nbind_p fncmatch( char *name) { } -/* user function that does NOTHING */ -BINDABLE( nullproc) { +/* user function that does NOTHING (bound to hooks) */ +TBINDABLE( nullproc) { return TRUE ; } + /* dummy function for binding to meta prefix */ -BINDABLE( metafn) { +TBINDABLE( metafn) { return TRUE ; } + /* dummy function for binding to control-x prefix */ -BINDABLE( cex) { +TBINDABLE( cex) { return TRUE ; } + /* dummy function for binding to universal-argument */ -BINDABLE( unarg) { +TBINDABLE( unarg) { return TRUE ; } diff --git a/names.h b/names.h index 47d5277..6be2190 100644 --- a/names.h +++ b/names.h @@ -1,12 +1,10 @@ /* names.h -- mapping of functions to names and keys */ - #ifndef _NAMES_H_ -#define _NAMES_H_ +# define _NAMES_H_ #include "retcode.h" - -#define CTRL 0x01000000 /* Control flag, or'ed in */ +#define CTL_ 0x01000000 /* Control flag, or'ed in */ #define META 0x02000000 /* Meta flag, or'ed in */ #define CTLX 0x04000000 /* ^X flag, or'ed in */ #define SPEC 0x08000000 /* special key (function keys) */ @@ -14,7 +12,9 @@ /* Bindable uEMACS function pointer type and definition template */ -#define BINDABLE( fname) int fname( int f, int n) +#define BINDABLE( fname) int fname( boolean f, int n) +#define BBINDABLE( fname) boolean fname( boolean f, int n) +#define TBINDABLE BBINDABLE typedef BINDABLE( (*fnp_t)) ; @@ -53,11 +53,10 @@ nbind_p fncmatch( char *name) ; /* look up by name */ /* bindable functions mapped to prefix keys and hooks */ -BINDABLE( nullproc) ; -BINDABLE( metafn) ; -BINDABLE( cex) ; -BINDABLE( unarg) ; +TBINDABLE( nullproc) ; +TBINDABLE( metafn) ; +TBINDABLE( cex) ; +TBINDABLE( unarg) ; #endif - /* end of names.h */ diff --git a/random.c b/random.c index 98441d3..6569ed5 100644 --- a/random.c +++ b/random.c @@ -144,15 +144,13 @@ int getcline(void) return numlines + 1; } -/* - * Return current column. Stop at first non-blank given TRUE argument. +/* Return current column. Stop at first non-blank given TRUE argument. */ -int getccol(int bflg) -{ - int i, col; +int getccol( int bflg) { + int i, col ; line_p dlp = curwp->w_dotp ; - int byte_offset = curwp->w_doto; - int len = llength(dlp); + int byte_offset = curwp->w_doto ; + int len = llength( dlp) ; col = i = 0; while (i < byte_offset) { @@ -160,17 +158,20 @@ int getccol(int bflg) i += utf8_to_unicode(dlp->l_text, i, len, &c); if( bflg && c != ' ' && c != '\t') /* Request Stop at first non-blank */ - break; + break ; if (c == '\t') col += tabwidth - col % tabwidth ; else if (c < 0x20 || c == 0x7F) /* displayed as ^c */ col += 2 ; else if (c >= 0x80 && c <= 0xa0) /* displayed as \xx */ col += 3 ; - else - col += 1 ; + else { + int w = utf8_width( c) ; /* incomplete wc_width */ + col += (w < 0) ? 2 : w ; + } } - return col; + + return col ; } /* @@ -714,60 +715,40 @@ BINDABLE( killtext) { return ldelete(chunk, TRUE); } -/* - * prompt and set an editor mode + +/* prompt and set an editor mode * * int f, n; default and argument */ -int setemode(int f, int n) -{ -#if PKCODE - return adjustmode(TRUE, FALSE); -#else - adjustmode(TRUE, FALSE); -#endif +BINDABLE( setemode) { + return adjustmode( TRUE, FALSE) ; } -/* - * prompt and delete an editor mode + +/* prompt and delete an editor mode * * int f, n; default and argument */ -int delmode(int f, int n) -{ -#if PKCODE - return adjustmode(FALSE, FALSE); -#else - adjustmode(FALSE, FALSE); -#endif +BINDABLE( delmode) { + return adjustmode( FALSE, FALSE) ; } -/* - * prompt and set a global editor mode + +/* prompt and set a global editor mode * * int f, n; default and argument */ -int setgmode(int f, int n) -{ -#if PKCODE - return adjustmode(TRUE, TRUE); -#else - adjustmode(TRUE, TRUE); -#endif +BINDABLE( setgmode) { + return adjustmode( TRUE, TRUE) ; } -/* - * prompt and delete a global editor mode + +/* prompt and delete a global editor mode * * int f, n; default and argument */ -int delgmode(int f, int n) -{ -#if PKCODE - return adjustmode(FALSE, TRUE); -#else - adjustmode(FALSE, TRUE); -#endif +BINDABLE( delgmode) { + return adjustmode( FALSE, TRUE) ; } /* @@ -858,11 +839,10 @@ static int adjustmode( int kind, int global) { static int iovstring( int f, int n, const char *prompt, int (*fun)( char *)) { - int status ; /* status return code */ char *tstring ; /* string to add */ - /* ask for string to insert */ - status = newmlargt( &tstring, prompt, 0) ; /* grab as big a token as screen allow */ +/* ask for string to insert */ + int status = newmlargt( &tstring, prompt, 0) ; if( tstring == NULL) return status ; diff --git a/region.c b/region.c index 7504416..c31a431 100644 --- a/region.c +++ b/region.c @@ -9,7 +9,7 @@ */ #include -#include +#include #include "buffer.h" #include "estruct.h" @@ -139,10 +139,9 @@ int getregion( region_p rp) { line_p flp, blp ; long fsize, bsize ; - if (curwp->w_markp == NULL) { - mloutstr( "No mark set in this window") ; - return FALSE; - } + if (curwp->w_markp == NULL) + return mloutfail( "No mark set in this window") ; + if (curwp->w_dotp == curwp->w_markp) { rp->r_linep = curwp->w_dotp; if (curwp->w_doto < curwp->w_marko) { @@ -182,8 +181,8 @@ int getregion( region_p rp) { } } } - mloutstr( "Bug: lost mark") ; - return FALSE; + + return mloutfail( "Bug: lost mark") ; } /* end of region.c */ diff --git a/search.c b/search.c index 6e0dace..7f6d4fd 100644 --- a/search.c +++ b/search.c @@ -1,9 +1,7 @@ /* search.c -- implements search.h */ #include "search.h" -/* search.c - * - * The functions in this file implement commands that search in the forward +/* The functions in this file implement commands that search in the forward * and backward directions. There are no special characters in the search * strings. Probably should have a regular expression search, or something * like that. @@ -177,15 +175,14 @@ static int biteq(int bc, char *cclmap); static char *clearbits(void); static void setbit(int bc, char *cclmap); -/* - * forwsearch -- Search forward. Get a search string from the user, and + +/* forwsearch -- Search forward. Get a search string from the user, and * search for the string. If found, reset the "." to be just after * the match string, and (perhaps) repaint the display. * * int f, n; default flag / numeric argument */ -int forwsearch(int f, int n) -{ +BINDABLE( forwsearch) { int status = TRUE; /* If n is negative, search backwards. @@ -223,15 +220,14 @@ int forwsearch(int f, int n) return status; } -/* - * forwhunt -- Search forward for a previously acquired search string. + +/* forwhunt -- Search forward for a previously acquired search string. * If found, reset the "." to be just after the match string, * and (perhaps) repaint the display. * * int f, n; default flag / numeric argument */ -int forwhunt(int f, int n) -{ +BINDABLE( forwhunt) { int status = TRUE; if (n < 0) /* search backwards */ @@ -276,16 +272,15 @@ int forwhunt(int f, int n) return status; } -/* - * backsearch -- Reverse search. Get a search string from the user, and + +/* backsearch -- Reverse search. Get a search string from the user, and * search, starting at "." and proceeding toward the front of the buffer. * If found "." is left pointing at the first character of the pattern * (the last character that was matched). * * int f, n; default flag / numeric argument */ -int backsearch(int f, int n) -{ +BINDABLE( backsearch) { int status = TRUE; /* If n is negative, search forwards. @@ -324,16 +319,15 @@ int backsearch(int f, int n) return status; } -/* - * backhunt -- Reverse search for a previously acquired search string, + +/* backhunt -- Reverse search for a previously acquired search string, * starting at "." and proceeding toward the front of the buffer. * If found "." is left pointing at the first character of the pattern * (the last character that was matched). * * int f, n; default flag / numeric argument */ -int backhunt(int f, int n) -{ +BINDABLE( backhunt) { int status = TRUE; if (n < 0) @@ -788,26 +782,24 @@ void rvstrcpy(char *rvstr, char *str) *rvstr = '\0'; } -/* - * sreplace -- Search and replace. + +/* sreplace -- Search and replace. * * int f; default flag * int n; # of repetitions wanted */ -int sreplace(int f, int n) -{ - return replaces(FALSE, f, n); +BINDABLE( sreplace) { + return replaces( FALSE, f, n) ; } -/* - * qreplace -- search and replace with query. + +/* qreplace -- search and replace with query. * * int f; default flag * int n; # of repetitions wanted */ -int qreplace(int f, int n) -{ - return replaces(TRUE, f, n); +BINDABLE( qreplace) { + return replaces( TRUE, f, n) ; } /* diff --git a/spawn.c b/spawn.c index ff6aa18..f48e74a 100644 --- a/spawn.c +++ b/spawn.c @@ -1,9 +1,7 @@ /* spawn.c -- implements spawn.h */ #include "spawn.h" -/* spawn.c - * - * Various operating system access commands. +/* Various operating system access commands. * * Modified by Petri Kutvonen */ @@ -34,13 +32,11 @@ #endif -/* - * Create a subjob with a copy of the command intrepreter in it. When the +/* Create a subjob with a copy of the command intrepreter in it. When the * command interpreter exits, mark the screen as garbage so that you do a full * repaint. Bound to "^X C". */ -int spawncli(int f, int n) -{ +BINDABLE( spawncli) { #if USG | BSD char *cp; #endif @@ -81,9 +77,8 @@ int spawncli(int f, int n) } #if BSD | SVR4 - -int bktoshell(int f, int n) -{ /* suspend MicroEMACS and wait to wake up */ +/* suspend MicroEMACS and wait to wake up */ +BINDABLE( bktoshell) { vttidy(); /****************************** int pid; @@ -103,12 +98,12 @@ void rtfrmshell(void) } #endif -/* - * Run a one-liner in a subjob. When the command returns, wait for a single + +/* Run a one-liner in a subjob. When the command returns, wait for a single * character to be typed, then mark the screen as garbage so a full repaint is * done. Bound to "C-X !". */ -int spawn( int f, int n) { +BINDABLE( spawn) { int s ; char *line ; @@ -141,13 +136,13 @@ int spawn( int f, int n) { #endif } -/* - * Run an external program with arguments. When it returns, wait for a single + +/* Run an external program with arguments. When it returns, wait for a single * character to be typed, then mark the screen as garbage so a full repaint is * done. Bound to "C-X $". */ -int execprg( int f, int n) { +BINDABLE( execprg) { int s ; char *line ; @@ -176,11 +171,11 @@ int execprg( int f, int n) { #endif } -/* - * Pipe a one line command into a window + +/* Pipe a one line command into a window * Bound to ^X @ */ -int pipecmd( int f, int n) { +BINDABLE( pipecmd) { int s ; /* return status from CLI */ struct window *wp ; /* pointer to new window */ buffer_p bp ; /* pointer to buffer to zot */ @@ -271,11 +266,11 @@ int pipecmd( int f, int n) { return TRUE; } -/* - * filter a buffer through an external DOS program + +/* filter a buffer through an external DOS program * Bound to ^X # */ -int filter_buffer( int f, int n) { +BINDABLE( filter_buffer) { int s ; /* return status from CLI */ buffer_p bp ; /* pointer to buffer to zot */ char *mlarg ; diff --git a/util.c b/util.c index 9028863..13e44a0 100644 --- a/util.c +++ b/util.c @@ -1,17 +1,20 @@ +/* util.c -- implements util.h */ #include "util.h" /* Safe zeroing, no complaining about overlap */ -void mystrscpy(char *dst, const char *src, int size) -{ - if (!size) - return; - while (--size) { - char c = *src++; - if (!c) - break; - *dst++ = c; +void mystrscpy( char *dst, const char *src, int size) { + if( size <= 0) + return ; + + while( --size) { + char c = *src++ ; + if( !c) + break ; + + *dst++ = c ; } - *dst = 0; + + *dst = 0 ; } - +/* end of util.c */ diff --git a/util.h b/util.h index 73440b1..d599559 100644 --- a/util.h +++ b/util.h @@ -1,7 +1,9 @@ +/* util.h -- utility functions */ #ifndef UTIL_H_ -#define UTIL_H_ +# define UTIL_H_ #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -void mystrscpy(char *dst, const char *src, int size); +void mystrscpy( char *dst, const char *src, int size) ; #endif /* UTIL_H_ */ +/* end of util.h */ diff --git a/window.c b/window.c index eef9669..d5448fa 100644 --- a/window.c +++ b/window.c @@ -1,77 +1,65 @@ /* window.c -- inplements window.h */ - #include "window.h" -/* window.c - * - * Window management. Some of the functions are internal, and some are - * attached to keys that the user actually types. - * +/* Window management. Some of the functions are internal, and some are + attached to keys that the user actually types. */ #include -#include #include "basic.h" #include "buffer.h" -#include "display.h" +#include "display.h" /* upmode() */ #include "estruct.h" #include "execute.h" #include "line.h" +#include "mlout.h" #include "terminal.h" #include "wrapper.h" +window_p curwp ; /* Current window */ +window_p wheadp ; /* Head of list of windows */ -struct window *curwp ; /* Current window */ -struct window *wheadp ; /* Head of list of windows */ - -static struct window *swindow = NULL ; /* saved window pointer */ +static window_p savwindow = NULL ; /* saved window pointer */ -/* - * Reposition dot in the current window to line "n". If the argument is - * positive, it is that line. If it is negative it is that line from the - * bottom. If it is 0 the window is centered (this is what the standard - * redisplay code does). With no argument it defaults to 0. Bound to M-!. +/* Reposition dot in the current window to line "n". If the argument is + positive, it is that line. If it is negative it is that line from the + bottom. If it is 0 the window is centered (this is what the standard + redisplay code does). With no argument it defaults to 0. Bound to M-!. */ -int reposition(int f, int n) -{ - if (f == FALSE) /* default to 0 to center screen */ - n = 0; - curwp->w_force = n; - curwp->w_flag |= WFFORCE; - return TRUE; +TBINDABLE( reposition) { + curwp->w_force = (f == FALSE) ? 0 : n ; /* default to 0 to center screen */ + curwp->w_flag |= WFFORCE ; + return TRUE ; } -/* - * Refresh the screen. With no argument, it just does the refresh. With an - * argument it recenters "." in the current window. Bound to "C-L". + +/* Refresh the screen. With no argument, it just does the refresh. With + an argument it recenters "." in the current window. Bound to "C-L". */ -int redraw(int f, int n) -{ - if (f == FALSE) - sgarbf = TRUE; +TBINDABLE( redraw) { + if( f == FALSE) + sgarbf = TRUE ; else { - curwp->w_force = 0; /* Center dot. */ - curwp->w_flag |= WFFORCE; + curwp->w_force = 0 ; /* Center dot. */ + curwp->w_flag |= WFFORCE ; } - return TRUE; + return TRUE ; } -/* - * The command make the next window (next => down the screen) the current - * window. There are no real errors, although the command does nothing if - * there is only 1 window on the screen. Bound to "C-X C-N". - * - * with an argument this command finds the th window from the top - * - * int f, n; default flag and numeric argument - * + +/* The command make the next window (next => down the screen) the current + window. There are no real errors, although the command does nothing if + there is only 1 window on the screen. Bound to "C-X C-N". + + with an argument this command finds the th window from the top + + int f, n; default flag and numeric argument */ -int nextwind(int f, int n) -{ - struct window *wp; +BINDABLE( nextwind) { + window_p wp; int nwindows; /* total number of windows */ if (f) { @@ -94,10 +82,8 @@ int nextwind(int f, int n) wp = wheadp; while (--n) wp = wp->w_wndp; - } else { - mlwrite("Window number out of range"); - return FALSE; - } + } else + return mloutfail( "Window number out of range") ; } else if ((wp = curwp->w_wndp) == NULL) wp = wheadp; curwp = wp; @@ -107,15 +93,14 @@ int nextwind(int f, int n) return TRUE; } -/* - * This command makes the previous window (previous => up the screen) the - * current window. There arn't any errors, although the command does not do a - * lot if there is 1 window. + +/* This command makes the previous window (previous => up the screen) the + current window. There arn't any errors, although the command does not + do a lot if there is 1 window. */ -int prevwind(int f, int n) -{ - struct window *wp1; - struct window *wp2; +BINDABLE( prevwind) { + window_p wp1; + window_p wp2; /* if we have an argument, we mean the nth window from the bottom */ if (f) @@ -137,28 +122,26 @@ int prevwind(int f, int n) return TRUE; } -/* - * This command moves the current window down by "arg" lines. Recompute the + +/* This command moves the current window down by "arg" lines. Recompute the * top line in the window. The move up and move down code is almost completely * the same; most of the work has to do with reframing the window, and picking * a new dot. We share the code by having "move down" just be an interface to * "move up". Magic. Bound to "C-X C-N". */ -int mvdnwind(int f, int n) -{ - return mvupwind(f, -n); +BINDABLE( mvdnwind) { + return mvupwind( f, -n) ; } -/* - * Move the current window up by "arg" lines. Recompute the new top line of + +/* Move the current window up by "arg" lines. Recompute the new top line of * the window. Look to see if "." is still on the screen. If it is, you win. * If it isn't, then move "." to center it in the new framing of the window * (this command does not really move "."; it moves the frame). Bound to * "C-X C-P". */ -int mvupwind(int f, int n) -{ - struct line *lp; +BINDABLE( mvupwind) { + line_p lp; int i; lp = curwp->w_linep; @@ -193,17 +176,16 @@ int mvupwind(int f, int n) return TRUE; } -/* - * This command makes the current window the only window on the screen. Bound + +/* This command makes the current window the only window on the screen. Bound * to "C-X 1". Try to set the framing so that "." does not have to move on the * display. Some care has to be taken to keep the values of dot and mark in * the buffer structures right if the distruction of a window makes a buffer * become undisplayed. */ -int onlywind(int f, int n) -{ - struct window *wp; - struct line *lp; +BINDABLE( onlywind) { + window_p wp; + line_p lp; int i; while (wheadp != curwp) { @@ -241,23 +223,20 @@ int onlywind(int f, int n) return TRUE; } -/* - * Delete the current window, placing its space in the window above, + +/* Delete the current window, placing its space in the window above, * or, if it is the top window, the window below. Bound to C-X 0. * * int f, n; arguments are ignored for this command */ -int delwind(int f, int n) -{ - struct window *wp; /* window to recieve deleted space */ - struct window *lwp; /* ptr window before curwp */ +BINDABLE( delwind) { + window_p wp; /* window to recieve deleted space */ + window_p lwp; /* ptr window before curwp */ int target; /* target line to search for */ /* if there is only one window, don't delete it */ - if (wheadp->w_wndp == NULL) { - mlwrite("Can not delete this window"); - return FALSE; - } + if( wheadp->w_wndp == NULL) + return mloutfail( "Can not delete this window") ; /* find window before curwp in linked list */ wp = wheadp; @@ -316,8 +295,8 @@ int delwind(int f, int n) return TRUE; } -/* - * Split the current window. A window smaller than 3 lines cannot be + +/* Split the current window. A window smaller than 3 lines cannot be * split. An argument of 1 forces the cursor into the upper window, an * argument of two forces the cursor to the lower window. The only * other error that is possible is a "malloc" failure allocating the @@ -325,21 +304,21 @@ int delwind(int f, int n) * * int f, n; default flag and numeric argument */ -int splitwind(int f, int n) -{ - struct window *wp; - struct line *lp; +BINDABLE( splitwind) { + window_p wp; + line_p lp; int ntru; int ntrl; int ntrd; - struct window *wp1; - struct window *wp2; + window_p wp1; + window_p wp2; - if (curwp->w_ntrows < 3) { - mlwrite("Cannot split a %d line window", curwp->w_ntrows); - return FALSE; + if( curwp->w_ntrows < 3) { + mloutfmt( "Cannot split a %d line window", curwp->w_ntrows) ; + return FALSE ; } - wp = xmalloc(sizeof(struct window)); + + wp = xmalloc( sizeof *wp) ; ++curbp->b_nwnd; /* Displayed twice. */ wp->w_bufp = curbp; wp->w_dotp = curwp->w_dotp; @@ -398,33 +377,30 @@ int splitwind(int f, int n) return TRUE; } -/* - * Enlarge the current window. Find the window that loses space. Make sure it + +/* Enlarge the current window. Find the window that loses space. Make sure it * is big enough. If so, hack the window descriptions, and ask redisplay to do * all the hard work. You don't just set "force reframe" because dot would * move. Bound to "C-X Z". */ -int enlargewind(int f, int n) -{ - struct window *adjwp; - struct line *lp; +BINDABLE( enlargewind) { + window_p adjwp; + line_p lp; int i; if (n < 0) return shrinkwind(f, -n); - if (wheadp->w_wndp == NULL) { - mlwrite("Only one window"); - return FALSE; - } + if( wheadp->w_wndp == NULL) + return mloutfail( "Only one window") ; + if ((adjwp = curwp->w_wndp) == NULL) { adjwp = wheadp; while (adjwp->w_wndp != curwp) adjwp = adjwp->w_wndp; } - if (adjwp->w_ntrows <= n) { - mlwrite("Impossible change"); - return FALSE; - } + if( adjwp->w_ntrows <= n) + return mloutfail( "Impossible change") ; + if (curwp->w_wndp == adjwp) { /* Shrink below. */ lp = adjwp->w_linep; for (i = 0; i < n && lp != adjwp->w_bufp->b_linep; ++i) @@ -450,32 +426,29 @@ int enlargewind(int f, int n) return TRUE; } -/* - * Shrink the current window. Find the window that gains space. Hack at the + +/* Shrink the current window. Find the window that gains space. Hack at the * window descriptions. Ask the redisplay to do all the hard work. Bound to * "C-X C-Z". */ -int shrinkwind(int f, int n) -{ - struct window *adjwp; - struct line *lp; +BINDABLE( shrinkwind) { + window_p adjwp; + line_p lp; int i; if (n < 0) return enlargewind(f, -n); - if (wheadp->w_wndp == NULL) { - mlwrite("Only one window"); - return FALSE; - } + if( wheadp->w_wndp == NULL) + return mloutfail( "Only one window") ; + if ((adjwp = curwp->w_wndp) == NULL) { adjwp = wheadp; while (adjwp->w_wndp != curwp) adjwp = adjwp->w_wndp; } - if (curwp->w_ntrows <= n) { - mlwrite("Impossible change"); - return FALSE; - } + if( curwp->w_ntrows <= n) + return mloutfail( "Impossible change") ; + if (curwp->w_wndp == adjwp) { /* Grow below. */ lp = adjwp->w_linep; for (i = 0; i < n && lback(lp) != adjwp->w_bufp->b_linep; @@ -502,13 +475,12 @@ int shrinkwind(int f, int n) return TRUE; } -/* - * Resize the current window to the requested size + +/* Resize the current window to the requested size * * int f, n; default flag and numeric argument */ -int resize(int f, int n) -{ +BINDABLE( resize) { int clines; /* current # of lines in window */ /* must have a non-default argument, else ignore call */ @@ -525,14 +497,14 @@ int resize(int f, int n) return enlargewind(TRUE, n - clines); } -/* - * Pick a window for a pop-up. Split the screen if there is only one window. + +/* Pick a window for a pop-up. Split the screen if there is only one window. * Pick the uppermost window that isn't the current window. An LRU algorithm * might be better. Return a pointer, or NULL on error. */ -struct window *wpopup(void) +window_p wpopup(void) { - struct window *wp; + window_p wp; if (wheadp->w_wndp == NULL /* Only 1 window */ && splitwind(FALSE, 0) == FALSE) /* and it won't split */ @@ -543,59 +515,57 @@ struct window *wpopup(void) return wp; } -int scrnextup(int f, int n) -{ /* scroll the next window up (back) a page */ + +/* scroll the next window up (back) a page */ +BINDABLE( scrnextup) { nextwind(FALSE, 1); backpage(f, n); prevwind(FALSE, 1); return TRUE; } -int scrnextdw(int f, int n) -{ /* scroll the next window down (forward) a page */ + +/* scroll the next window down (forward) a page */ +BINDABLE( scrnextdw) { nextwind(FALSE, 1); forwpage(f, n); prevwind(FALSE, 1); return TRUE; } -int savewnd(int f, int n) -{ /* save ptr to current window */ - swindow = curwp; - return TRUE; + +/* save ptr to current window */ +BINDABLE( savewnd) { + savwindow = curwp ; + return TRUE ; } -int restwnd(int f, int n) -{ /* restore the saved screen */ - struct window *wp; - /* find the window */ - wp = wheadp; - while (wp != NULL) { - if (wp == swindow) { - curwp = wp; - curbp = wp->w_bufp; - upmode(); - return TRUE; +/* restore the saved screen */ +BINDABLE( restwnd) { +/* check the saved window still exists */ + for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp) { + if( wp == savwindow) { + curwp = wp ; + curbp = wp->w_bufp ; + upmode() ; + return TRUE ; } - wp = wp->w_wndp; } - mlwrite("(No such window exists)"); - return FALSE; + return mloutfail( "(No such window exists)") ; } -/* - * resize the screen, re-writing the screen + +/* resize the screen, re-writing the screen * * int f; default flag * int n; numeric argument */ -int newsize(int f, int n) -{ - struct window *wp; /* current window being examined */ - struct window *nextwp; /* next window to scan */ - struct window *lastwp; /* last window scanned */ +BINDABLE( newsize) { + window_p wp; /* current window being examined */ + window_p nextwp; /* next window to scan */ + window_p lastwp; /* last window scanned */ int lastline; /* screen line of last line of current window */ /* if the command defaults, assume the largest */ @@ -603,10 +573,8 @@ int newsize(int f, int n) n = term.t_mrow ; /* make sure it's in range */ - if (n < 3 || n > term.t_mrow) { - mlwrite("%%Screen size out of range"); - return FALSE; - } + if( n < 3 || n > term.t_mrow) + return mloutfail( "%%Screen size out of range") ; if (term.t_nrow == n - 1) return TRUE; @@ -674,25 +642,22 @@ int newsize(int f, int n) return TRUE; } -/* - * resize the screen, re-writing the screen + +/* resize the screen, re-writing the screen * * int f; default flag * int n; numeric argument */ -int newwidth(int f, int n) -{ - struct window *wp; +BINDABLE( newwidth) { + window_p wp; /* if the command defaults, assume the largest */ if (f == FALSE) n = term.t_mcol; /* make sure it's in range */ - if (n < 10 || n > term.t_mcol) { - mlwrite("%%Screen width out of range"); - return FALSE; - } + if (n < 10 || n > term.t_mcol) + return mloutfail( "%%Screen width out of range") ; /* otherwise, just re-width it (no big deal) */ term.t_ncol = n; @@ -713,7 +678,7 @@ int newwidth(int f, int n) int getwpos(void) { /* get screen offset of current line in current window */ int sline; /* screen line from top of window */ - struct line *lp; /* scannile line pointer */ + line_p lp; /* scannile line pointer */ /* search down the line we want */ lp = curwp->w_linep; @@ -731,3 +696,5 @@ void cknewwindow(void) { execute(META | SPEC | 'X', FALSE, 1); } + +/* end of window.c */ diff --git a/window.h b/window.h index 8a7f781..e555ed3 100644 --- a/window.h +++ b/window.h @@ -1,34 +1,35 @@ +/* window.h -- window functionality */ #ifndef _WINDOW_H_ #define _WINDOW_H_ #include "defines.h" /* COLOR, SCROLLCODE */ -#include "buffer.h" /* buffer, line */ +#include "buffer.h" /* buffer_p, line_p */ +#include "names.h" /* BINDABLE() */ -/* - * There is a window structure allocated for every active display window. The - * windows are kept in a big list, in top to bottom screen order, with the - * listhead at "wheadp". Each window contains its own values of dot and mark. - * The flag field contains some bits that are set by commands to guide - * redisplay. Although this is a bit of a compromise in terms of decoupling, - * the full blown redisplay is just too expensive to run for every input - * character. +/* There is a window structure allocated for every active display window. + The windows are kept in a big list, in top to bottom screen order, with + the listhead at "wheadp". Each window contains its own values of dot + and mark. The flag field contains some bits that are set by commands to + guide redisplay. Although this is a bit of a compromise in terms of + decoupling, the full blown redisplay is just too expensive to run for + every input character. */ typedef struct window { struct window *w_wndp; /* Next window */ - struct buffer *w_bufp; /* Buffer displayed in window */ + buffer_p w_bufp ; /* Buffer displayed in window */ line_p w_linep ; /* Top line in the window */ line_p w_dotp ; /* Line containing "." */ line_p w_markp ; /* Line containing "mark" */ int w_doto ; /* Byte offset for "." */ - int w_marko; /* Byte offset for "mark" */ - int w_toprow ; /* Origin 0 top row of window */ - int w_ntrows ; /* # of rows of text in window */ - char w_force; /* If NZ, forcing row. */ - char w_flag; /* Flags. */ -#if COLOR - char w_fcolor; /* current forground color */ - char w_bcolor; /* current background color */ -#endif + int w_marko ; /* Byte offset for "mark" */ + int w_toprow ; /* Origin 0 top row of window */ + int w_ntrows ; /* # of rows of text in window */ + char w_force ; /* If NZ, forcing row. */ + char w_flag ; /* Flags. */ +# if COLOR + char w_fcolor ; /* current forground color */ + char w_bcolor ; /* current background color */ +# endif } *window_p ; extern window_p curwp ; /* Current window */ @@ -44,31 +45,34 @@ extern window_p wheadp ; /* Head of list of windows */ #define WFMODE 0x10 /* Update mode line. */ #define WFCOLR 0x20 /* Needs a color change */ -#if SCROLLCODE -#define WFKILLS 0x40 /* something was deleted */ -#define WFINS 0x80 /* something was inserted */ -#endif +# if SCROLLCODE +# define WFKILLS 0x40 /* something was deleted */ +# define WFINS 0x80 /* something was inserted */ +# endif + +/* Bindable functions */ + BINDABLE( delwind) ; + BINDABLE( enlargewind) ; + BINDABLE( mvdnwind) ; + BINDABLE( mvupwind) ; + BINDABLE( newsize) ; + BINDABLE( newwidth) ; + BINDABLE( nextwind) ; + BINDABLE( onlywind) ; + BINDABLE( prevwind) ; +TBINDABLE( redraw) ; +TBINDABLE( reposition) ; + BINDABLE( resize) ; + BINDABLE( restwnd) ; + BINDABLE( savewnd) ; + BINDABLE( scrnextdw) ; + BINDABLE( scrnextup) ; + BINDABLE( shrinkwind) ; + BINDABLE( splitwind) ; -int reposition( int f, int n); -int redraw( int f, int n) ; -int nextwind( int f, int n) ; -int prevwind( int f, int n) ; -int mvdnwind( int f, int n) ; -int mvupwind( int f, int n) ; -int onlywind( int f, int n) ; -int delwind( int f, int n) ; -int splitwind( int f, int n) ; -int enlargewind( int f, int n) ; -int shrinkwind( int f, int n) ; -int resize( int f, int n) ; -int scrnextup( int f, int n) ; -int scrnextdw( int f, int n) ; -int savewnd( int f, int n) ; -int restwnd( int f, int n) ; -int newsize( int f, int n) ; -int newwidth( int f, int n) ; int getwpos( void) ; void cknewwindow( void) ; window_p wpopup( void) ; /* Pop up window creation. */ #endif +/* end of window.h */ diff --git a/word.c b/word.c index cc7ce03..7b859f8 100644 --- a/word.c +++ b/word.c @@ -1,13 +1,11 @@ /* word.c -- implements word.h */ #include "word.h" -/* word.c - * - * The routines in this file implement commands that work word or a - * paragraph at a time. There are all sorts of word mode commands. If I - * do any sentence mode commands, they are likely to be put in this file. - * - * Modified by Petri Kutvonen +/* The routines in this file implement commands that work word or a + paragraph at a time. There are all sorts of word mode commands. If I + do any sentence mode commands, they are likely to be put in this file. + + Modified by Petri Kutvonen */ #include @@ -31,18 +29,18 @@ static int justflag = FALSE ; /* justify, don't fill */ static int inword( void) ; -/* Word wrap on n-spaces. Back-over whatever precedes the point on the current - * line and stop on the first word-break or the beginning of the line. If we - * reach the beginning of the line, jump back to the end of the word and start - * a new line. Otherwise, break the line at the word-break, eat it, and jump - * back to the end of the word. - * Returns TRUE on success, FALSE on errors. - * - * @f: default flag. - * @n: numeric argument. +/* Word wrap on n-spaces. Back-over whatever precedes the point on the + current line and stop on the first word-break or the beginning of the + line. If we reach the beginning of the line, jump back to the end of + the word and start a new line. Otherwise, break the line at the + word-break, eat it, and jump back to the end of the word. + + Returns TRUE on success, FALSE on errors. + + @f: default flag. + @n: numeric argument. */ -int wrapword(int f, int n) -{ +BINDABLE( wrapword) { int cnt; /* size of word wrapped to next line */ int c; /* charector temporary */ @@ -81,11 +79,12 @@ int wrapword(int f, int n) return TRUE; } -/* Move the cursor backward by "n" words. All of the details of motion are - * performed by the "backchar" and "forwchar" routines. Error if you try to - * move beyond the buffers. + +/* Move the cursor backward by "n" words. All of the details of motion are + performed by the "backchar" and "forwchar" routines. Error if you try + to move beyond the buffers. */ -int backword( int f, int n) { +BINDABLE( backword) { if( n < 0) return forwword( f, -n) ; @@ -106,10 +105,12 @@ int backword( int f, int n) { return forwchar( FALSE, 1) ; } -/* Move the cursor forward by the specified number of words. All of the motion - * is done by "forwchar". Error if you try and move beyond the buffer's end. + +/* Move the cursor forward by the specified number of words. All of the + motion is done by "forwchar". Error if you try and move beyond the + buffer's end. */ -int forwword( int f, int n) { +BINDABLE( forwword) { if( n < 0) return backword( f, -n) ; @@ -172,40 +173,41 @@ static boolean capcapword( int n, boolean first_f, boolean rest_f) { return TRUE ; } -/* Move the cursor forward by the specified number of words. As you move, - * convert any characters to upper case. Error if you try and move beyond the - * end of the buffer. Bound to "M-U". + +/* Move the cursor forward by the specified number of words. As you move, + convert any characters to upper case. Error if you try and move beyond + the end of the buffer. Bound to "M-U". */ -int upperword( int f, int n) { +BINDABLE( upperword) { return capcapword( n, TRUE, TRUE) ; } -/* Move the cursor forward by the specified number of words. As you move - * convert characters to lower case. Error if you try and move over the end of - * the buffer. Bound to "M-L". +/* Move the cursor forward by the specified number of words. As you move + convert characters to lower case. Error if you try and move over the + end of the buffer. Bound to "M-L". */ -int lowerword( int f, int n) { +BINDABLE( lowerword) { return capcapword( n, FALSE, FALSE) ; } -/* Move the cursor forward by the specified number of words. As you move - * convert the first character of the word to upper case, and subsequent - * characters to lower case. Error if you try and move past the end of the - * buffer. Bound to "M-C". + +/* Move the cursor forward by the specified number of words. As you move + convert the first character of the word to upper case, and subsequent + characters to lower case. Error if you try and move past the end of the + buffer. Bound to "M-C". */ -int capword( int f, int n) { +BINDABLE( capword) { return capcapword( n, TRUE, FALSE) ; } -/* - * Kill forward by "n" words. Remember the location of dot. Move forward by - * the right number of words. Put dot back where it was and issue the kill - * command for the right number of characters. With a zero argument, just - * kill one word and no whitespace. Bound to "M-D". + +/* Kill forward by "n" words. Remember the location of dot. Move forward + by the right number of words. Put dot back where it was and issue the + kill command for the right number of characters. With a zero argument, + just kill one word and no whitespace. Bound to "M-D". */ -int delfword(int f, int n) -{ +BINDABLE( delfword) { line_p dotp; /* original cursor line */ int doto; /* and row */ int c; /* temp char */ @@ -285,13 +287,13 @@ int delfword(int f, int n) return ldelete(size, TRUE); } -/* - * Kill backwards by "n" words. Move backwards by the desired number of words, - * counting the characters. When dot is finally moved to its resting place, - * fire off the kill command. Bound to "M-Rubout" and to "M-Backspace". + +/* Kill backwards by "n" words. Move backwards by the desired number of + words, counting the characters. When dot is finally moved to its + resting place, fire off the kill command. Bound to "M-Rubout" and to + "M-Backspace". */ -int delbword(int f, int n) -{ +BINDABLE( delbword) { assert( !(curbp->b_mode & MDVIEW)) ; /* ignore the command if there is a nonpositive argument */ @@ -352,17 +354,13 @@ static int parafillnjustify( int f, int n, int justify_f) { assert( !(curbp->b_mode & MDVIEW)) ; - if (fillcol == 0) { /* no fill column set */ - mloutstr( "No fill column set") ; - return FALSE; - } + if( fillcol == 0) /* no fill column set */ + return mloutfail( "No fill column set") ; if( justify_f) { leftmarg = getccol( FALSE) ; - if (leftmarg + 10 > fillcol) { - mloutstr( "Column too narrow") ; - return FALSE; - } + if( leftmarg + 10 > fillcol) + return mloutfail( "Column too narrow") ; justflag = justify_f ; } @@ -472,33 +470,33 @@ static int parafillnjustify( int f, int n, int justify_f) { return TRUE; } -/* - * Fill the current paragraph according to the current + +/* Fill the current paragraph according to the current * fill column * * f and n - deFault flag and Numeric argument */ -int fillpara( int f, int n) { +BINDABLE( fillpara) { return parafillnjustify( f, n, FALSE) ; } + /* Fill the current paragraph according to the current * fill column and cursor position * * int f, n; deFault flag and Numeric argument */ -int justpara( int f, int n) { +BINDABLE( justpara) { return parafillnjustify( f, n, TRUE) ; } -/* - * delete n paragraphs starting with the current one + +/* delete n paragraphs starting with the current one * * int f default flag * int n # of paras to delete */ -int killpara(int f, int n) -{ +BINDABLE( killpara) { while (n--) { /* for each paragraph to delete */ /* mark out the end and beginning of the para to delete */ @@ -525,15 +523,13 @@ int killpara(int f, int n) } -/* - * wordcount: count the # of words in the marked region, +/* wordcount: count the # of words in the marked region, * along with average word sizes, # of chars, etc, * and report on them. * * int f, n; ignored numeric arguments */ -int wordcount(int f, int n) -{ +BINDABLE( wordcount) { line_p lp; /* current line to scan */ int offset; /* current char to scan */ long size; /* size of region left to count */ @@ -591,15 +587,14 @@ int wordcount(int f, int n) return TRUE; } -/* - * go back to the beginning of the current paragraph + +/* go back to the beginning of the current paragraph * here we look for a or or * combination to delimit the beginning of a paragraph * * int f, n; default Flag & Numeric argument */ -int gotobop(int f, int n) -{ +BINDABLE( gotobop) { if (n < 0) /* the other way... */ return gotoeop(f, -n); @@ -629,15 +624,14 @@ int gotobop(int f, int n) return TRUE; } -/* - * Go forward to the end of the current paragraph - * here we look for a or or - * combination to delimit the beginning of a paragraph - * - * int f, n; default Flag & Numeric argument + +/* Go forward to the end of the current paragraph here we look for a + or or combination to delimit the + beginning of a paragraph + + int f, n; default Flag & Numeric argument */ -int gotoeop(int f, int n) -{ +BINDABLE( gotoeop) { if (n < 0) /* the other way... */ return gotobop(f, -n); From b4d69118f5242e3fa5b166098363845c820968c4 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Fri, 13 Aug 2021 11:06:58 +0800 Subject: [PATCH 33/37] Revise header files formatting. Finish tagging BBINDABLE functions. Modify forward-character to handle combined Unicode. Bump up version number and set default program name as 'ue'. --- basic.h | 4 +- bind.h | 4 +- bindable.h | 4 +- buffer.h | 6 +- defines.h | 3 - display.h | 35 +- estruct.h | 155 ++++---- eval.h | 14 +- exec.h | 3 +- execute.h | 17 +- file.h | 10 +- fileio.h | 20 +- flook.h | 11 +- input.h | 30 +- isa.h | 43 +- isearch.h | 2 +- line.c | 1106 +++++++++++++++++++++++++-------------------------- line.h | 41 +- lock.h | 7 +- mlout.h | 6 +- names.h | 16 +- pklock.h | 4 - random.c | 1116 ++++++++++++++++++++++++++-------------------------- random.h | 56 ++- region.h | 12 +- retcode.h | 21 +- search.h | 34 +- spawn.h | 5 +- terminal.h | 112 +++--- termio.h | 10 +- utf8.h | 6 +- util.h | 7 +- version.h | 12 +- window.h | 60 +-- word.h | 1 - wrapper.h | 6 +- 36 files changed, 1493 insertions(+), 1506 deletions(-) diff --git a/basic.h b/basic.h index 4c58267..1f4d264 100644 --- a/basic.h +++ b/basic.h @@ -1,8 +1,8 @@ /* basic.h -- basic commands for cursor movement in active window */ #ifndef _BASIC_H_ -# define _BASIC_H_ +#define _BASIC_H_ -# include "names.h" +#include "names.h" /* BINDABLE() */ /* $overlap is the size of the line overlap when kbd calls page forw/back if 0, page will move by 2/3 of the window size (1/3 page overlap) diff --git a/bind.h b/bind.h index e58fbc9..77046ff 100644 --- a/bind.h +++ b/bind.h @@ -1,9 +1,8 @@ /* bind.h -- bindable functions dealing with name and key bindings */ - #ifndef _BIND_H_ #define _BIND_H_ -#include "names.h" +#include "names.h" /* BINDABLE() */ /* Bindable uEMACS functions */ BINDABLE( apro) ; @@ -19,5 +18,4 @@ int startup( const char *fname) ; const char *transbind( char *skey) ; /* by string representation of key */ #endif - /* end of bind.h */ diff --git a/bindable.h b/bindable.h index 21a709f..5959496 100644 --- a/bindable.h +++ b/bindable.h @@ -2,13 +2,13 @@ #ifndef _BINDABLE_H_ #define _BINDABLE_H_ -#include "names.h" +#include "names.h" /* BINDABLE() */ /* functions that can be bound to keys or procedure names */ BBINDABLE( ctlxe) ; BBINDABLE( ctlxlp) ; BBINDABLE( ctlxrp) ; - BINDABLE( ctrlg) ; /* ABORT */ + BINDABLE( ctrlg) ; /* ABORT */ BINDABLE( quickexit) ; BINDABLE( quit) ; diff --git a/buffer.h b/buffer.h index 50f3b75..50b4329 100644 --- a/buffer.h +++ b/buffer.h @@ -1,9 +1,9 @@ /* buffer.h -- buffer type and functions */ #ifndef _BUFFER_H_ -# define _BUFFER_H_ +#define _BUFFER_H_ -#include "line.h" -#include "names.h" +#include "line.h" /* line_p */ +#include "names.h" /* BINDABLE() */ /* Text is kept in buffers. A buffer header, described below, exists for every buffer in the system. The buffers are kept in a big list, so that diff --git a/defines.h b/defines.h index d62ac3a..c5959f2 100644 --- a/defines.h +++ b/defines.h @@ -1,9 +1,7 @@ /* defines.h -- */ - #ifndef __DEFINES_H__ #define __DEFINES_H__ - /* Must define one of USG | BSD */ @@ -16,5 +14,4 @@ #define NSTRING 128 /* # of bytes, string buffers */ #endif - /* end of defines.h */ diff --git a/display.h b/display.h index 1e0f041..cd5564c 100644 --- a/display.h +++ b/display.h @@ -1,19 +1,19 @@ /* display.h -- display functionality */ #ifndef _DISPLAY_H_ -# define _DISPLAY_H_ +#define _DISPLAY_H_ -# include +#include -# include "estruct.h" -# include "names.h" /* BINDABLE() */ -# include "utf8.h" /* unicode_t */ +#include "estruct.h" +#include "names.h" /* BINDABLE() */ +#include "utf8.h" /* unicode_t */ -extern int mpresf ; /* Stuff in message line */ -extern int scrollcount ; /* number of lines to scroll */ -extern int discmd ; /* display command flag */ -extern int disinp ; /* display input characters (echo) */ -extern int gfcolor ; /* global forgrnd color (white) */ -extern int gbcolor ; /* global backgrnd color (black) */ +extern int mpresf ; /* Stuff in message line */ +extern int scrollcount ; /* number of lines to scroll */ +extern int discmd ; /* display command flag */ +extern int disinp ; /* display input characters (echo) */ +extern int gfcolor ; /* global forgrnd color (white) */ +extern int gbcolor ; /* global backgrnd color (black) */ /* Bindable functions */ BINDABLE( upscreen) ; @@ -37,13 +37,14 @@ void echos( const char *s) ; void rubout( void) ; void getscreensize( int *widthp, int *heightp) ; -# if UNIX -# include -# ifdef SIGWINCH -extern int chg_width, chg_height ; +#if UNIX +# include +# ifdef SIGWINCH + extern int chg_width, chg_height ; -void sizesignal( int signr) ; -# endif + void sizesignal( int signr) ; # endif +#endif + #endif /* end of display.h */ diff --git a/estruct.h b/estruct.h index a5c2ae5..1de6331 100644 --- a/estruct.h +++ b/estruct.h @@ -1,19 +1,17 @@ /* estruct.h -- */ - #ifndef _ESTRUCT_H_ #define _ESTRUCT_H_ /* Structure and preprocessor defines * - * written by Dave G. Conroy - * modified by Steve Wilhite, George Jones + * written by Dave G. Conroy + * modified by Steve Wilhite, George Jones * substantially modified by Daniel Lawrence - * modified by Petri Kutvonen + * modified by Petri Kutvonen */ - -#ifdef MSDOS -#undef MSDOS +#ifdef MSDOS +# undef MSDOS #endif /* Machine/OS definitions. */ @@ -22,121 +20,116 @@ /* Make an intelligent guess about the target system. */ -#if defined(BSD) || defined(sun) || defined(ultrix) || defined(__osf__) -#ifndef BSD -#define BSD 1 /* Berkeley UNIX */ -#endif -#else -#define BSD 0 -#endif +# if defined(BSD) || defined(sun) || defined(ultrix) || defined(__osf__) +# ifndef BSD +# define BSD 1 /* Berkeley UNIX */ +# endif +# else +# define BSD 0 +# endif -#if defined(SVR4) || defined(__linux__) /* ex. SunOS 5.3 */ -#define SVR4 1 -#define SYSV 1 -#undef BSD -#endif +# if defined(SVR4) || defined(__linux__) /* ex. SunOS 5.3 */ +# define SVR4 1 +# define SYSV 1 +# undef BSD +# endif -#if defined(SYSV) || defined(u3b2) || defined(_AIX) || (defined(i386) && defined(unix)) || defined( __unix__) -#define USG 1 /* System V UNIX */ -#else -#define USG 0 -#endif +# if defined(SYSV) || defined(u3b2) || defined(_AIX) || (defined(i386) && defined(unix)) || defined( __unix__) +# define USG 1 /* System V UNIX */ +# else +# define USG 0 +# endif #else -# define BSD 0 /* UNIX BSD 4.2 and ULTRIX */ -# define USG 1 /* UNIX system V */ -#endif /*autoconf */ +# define BSD 0 /* UNIX BSD 4.2 and ULTRIX */ +# define USG 1 /* UNIX system V */ +#endif /*autoconf || BSD || SYSV */ -#define MSDOS 0 /* MS-DOS */ +#define MSDOS 0 /* MS-DOS */ -/* Compiler definitions */ -#ifndef AUTOCONF -# define UNIX 1 /* a random UNIX compiler */ +/* Compiler definitions */ +#ifndef AUTOCONF +# define UNIX 1 /* a random UNIX compiler */ #else -# define UNIX (BSD | USG) -#endif /*autoconf */ +# define UNIX (BSD | USG) +#endif /*autoconf */ -/* Debugging options */ +/* Debugging options */ -#define RAMSIZE 0 /* dynamic RAM memory usage tracking */ +#define RAMSIZE 0 /* dynamic RAM memory usage tracking */ #if RAMSIZE -#define RAMSHOW 1 /* auto dynamic RAM reporting */ +# define RAMSHOW 1 /* auto dynamic RAM reporting */ #endif -#ifndef AUTOCONF -/* Terminal Output definitions */ -# define TERMCAP 0 /* Use TERMCAP */ -# define IBMPC 1 /* IBM-PC CGA/MONO/EGA driver */ +#ifndef AUTOCONF +/* Terminal Output definitions */ +# define TERMCAP 0 /* Use TERMCAP */ +# define IBMPC 1 /* IBM-PC CGA/MONO/EGA driver */ #else -# define TERMCAP UNIX -# define IBMPC MSDOS +# define TERMCAP UNIX +# define IBMPC MSDOS #endif /* Autoconf. */ -/* Configuration options */ +/* Configuration options */ -#define VISMAC 0 /* update display during keyboard macros */ - -#ifndef AUTOCONF - -#define COLOR 1 /* color commands and windows */ -#define FILOCK 0 /* file locking under unix BSD 4.2 */ +#define VISMAC 0 /* update display during keyboard macros */ +#ifndef AUTOCONF +# define COLOR 1 /* color commands and windows */ +# define FILOCK 0 /* file locking under unix BSD 4.2 */ #else - -#define COLOR MSDOS -#ifdef SVR4 -#define FILOCK 1 -#else -#define FILOCK BSD -#endif - +# define COLOR MSDOS +# ifdef SVR4 +# define FILOCK 1 +# else +# define FILOCK BSD +# endif #endif /* Autoconf. */ -#define CLEAN 0 /* de-alloc memory on exit */ +#define CLEAN 0 /* de-alloc memory on exit */ -#ifndef AUTOCONF -# define XONXOFF 0 /* don't disable XON-XOFF flow control P.K. */ +#ifndef AUTOCONF +# define XONXOFF 0 /* don't disable XON-XOFF flow control P.K. */ #else -# define XONXOFF UNIX +# define XONXOFF UNIX #endif /* Autoconf. */ -#define PKCODE 1 /* include my extensions P.K., define always */ +#define PKCODE 1 /* include my extensions P.K., define always */ #define SCROLLCODE 1 /* scrolling code P.K. */ /* Define some ability flags. */ -#if IBMPC -#define MEMMAP 1 +#if IBMPC +# define MEMMAP 1 #else -#define MEMMAP 0 +# define MEMMAP 0 #endif -#if USG | BSD -# define ENVFUNC 1 +#if USG | BSD +# define ENVFUNC 1 #else -# define ENVFUNC 0 +# define ENVFUNC 0 #endif -/* Dynamic RAM tracking and reporting redefinitions */ +/* Dynamic RAM tracking and reporting redefinitions */ -#if RAMSIZE -#include -void *allocate( size_t size) ; -void release( void *ptr) ; -#define malloc allocate -#define free release +#if RAMSIZE +# include + void *allocate( size_t size) ; + void release( void *ptr) ; +# define malloc allocate +# define free release #endif -/* De-allocate memory always on exit (if the operating system or - main program can not +/* De-allocate memory always on exit (if the operating system or + main program can not */ -#if CLEAN -#define exit(a) cexit(a) +#if CLEAN +# define exit(a) cexit(a) -void cexit( int status) ; + void cexit( int status) ; #endif #endif - /* end of estruct.h */ diff --git a/eval.h b/eval.h index 8c178ee..af9e838 100644 --- a/eval.h +++ b/eval.h @@ -1,14 +1,13 @@ /* eval.h -- variables and operands evaluation */ #ifndef _EVAL_H_ -# define _EVAL_H_ +#define _EVAL_H_ #include "names.h" #define DEBUGM 1 /* $debug triggers macro debugging */ - -# if DEBUGM -int mdbugout( char *fmt, ...) ; -# endif +#if DEBUGM + int mdbugout( char *fmt, ...) ; +#endif extern int macbug ; /* macro debuging flag */ extern int cmdstatus ; /* last command status */ @@ -25,9 +24,8 @@ char *mklower( char *str) ; /* Bindable functions */ TBINDABLE( clrmes) ; -BINDABLE( setvar) ; -BINDABLE( writemsg) ; + BINDABLE( setvar) ; + BINDABLE( writemsg) ; #endif - /* end of eval.h */ diff --git a/exec.h b/exec.h index 638064f..1bb6999 100644 --- a/exec.h +++ b/exec.h @@ -1,6 +1,6 @@ /* exec.h -- bindable functions to execute functions, macros and procedures */ #ifndef _EXEC_H_ -# define _EXEC_H_ +#define _EXEC_H_ #include "names.h" @@ -61,5 +61,4 @@ BINDABLE( cbuf39) ; BINDABLE( cbuf40) ; #endif - /* end of exec.h */ diff --git a/execute.h b/execute.h index 9949149..d514b15 100644 --- a/execute.h +++ b/execute.h @@ -1,20 +1,19 @@ /* execute.h -- */ +#ifndef _EXECUTE_H_ +#define _EXECUTE_H_ -#define CFENCE 1 /* fence matching in CMODE */ - - -#include "names.h" /* key code */ - - -extern int gasave ; /* global ASAVE size */ -extern int gacount ; /* count until next ASAVE */ +#include "names.h" /* BINDABLE() */ +extern int gasave ; /* global ASAVE size */ +extern int gacount ; /* count until next ASAVE */ int execute( unsigned keycode, int f, int n) ; void kbd_loop( void) ; +#define CFENCE 1 /* fence matching in CMODE */ #if CFENCE -BINDABLE( getfence) ; + BINDABLE( getfence) ; #endif +#endif /* end of execute.h */ diff --git a/file.h b/file.h index b168a4a..6186c6b 100644 --- a/file.h +++ b/file.h @@ -1,13 +1,12 @@ /* file.h -- file centric commands */ - #ifndef _FILE_H_ #define _FILE_H_ -#include "buffer.h" -#include "names.h" +#include "buffer.h" /* bname_t */ +#include "names.h" /* BINDABLE() */ -extern boolean restflag ; /* restricted use? */ -boolean resterr( void) ; /* restricted error message */ +extern boolean restflag ; /* restricted use? */ +boolean resterr( void) ; /* restricted error message */ /* Bindable functions */ BINDABLE( filefind) ; @@ -25,5 +24,4 @@ void unqname( char *name) ; int writeout( const char *fn) ; #endif - /* end of file.h */ diff --git a/fileio.h b/fileio.h index 7f14184..df0219a 100644 --- a/fileio.h +++ b/fileio.h @@ -1,12 +1,13 @@ +/* fileio.h -- file primitives */ #ifndef _FILEIO_H_ #define _FILEIO_H_ typedef enum { - FIOSUC, /* File I/O, success. */ - FIOFNF, /* File I/O, file not found. */ - FIOEOF, /* File I/O, end of file. */ - FIOERR, /* File I/O, error. */ - FIOMEM /* File I/O, out of memory */ + FIOSUC, /* File I/O, success. */ + FIOFNF, /* File I/O, file not found. */ + FIOEOF, /* File I/O, end of file. */ + FIOERR, /* File I/O, error. */ + FIOMEM /* File I/O, out of memory */ } fio_code ; #define FTYPE_NONE 0 @@ -21,10 +22,10 @@ typedef enum { #define FCODE_EXTND 0x82 #define FCODE_MIXED 0x83 -extern char *fline ; /* dynamic return line */ -extern int ftype ; -extern int fcode ; /* encoding type */ -extern int fpayload ; /* actual length of fline content */ +extern char *fline ; /* dynamic return line */ +extern int ftype ; +extern int fcode ; /* encoding type */ +extern int fpayload ; /* actual length of fline content */ fio_code ffclose( void) ; fio_code ffgetline( void) ; @@ -33,3 +34,4 @@ fio_code ffropen( const char *fn) ; fio_code ffwopen( const char *fn) ; #endif +/* end of fileio.h */ diff --git a/flook.h b/flook.h index e56358c..869084f 100644 --- a/flook.h +++ b/flook.h @@ -1,8 +1,11 @@ -#include "retcode.h" +/* flook.h -- */ +#ifndef _FLOOK_H_ +#define _FLOOK_H_ +#include "retcode.h" /* boolean */ -#define rcfname pathname[ 0] -#define hlpfname pathname[ 1] +#define rcfname pathname[ 0] +#define hlpfname pathname[ 1] extern const char *pathname[] ; @@ -10,3 +13,5 @@ extern const char *pathname[] ; boolean fexist( const char *fname) ; char *flook( const char *fname, boolean hflag) ; +#endif +/* end of flook.h */ diff --git a/input.h b/input.h index b9bd06d..405f1a1 100644 --- a/input.h +++ b/input.h @@ -1,25 +1,26 @@ +/* input.h -- */ #ifndef _INPUT_H_ #define _INPUT_H_ -#include "names.h" +#include "names.h" /* nbind_p */ typedef enum { - STOP, PLAY, RECORD + STOP, PLAY, RECORD } kbdstate ; -extern kbdstate kbdmode ; /* current keyboard macro mode */ -extern int lastkey ; /* last keystoke */ -extern int kbdrep ; /* number of repetitions */ -extern int kbdm[] ; /* Holds kayboard macro data */ -extern int *kbdptr ; /* current position in keyboard buf */ -extern int *kbdend ; /* ptr to end of the keyboard */ +extern kbdstate kbdmode ; /* current keyboard macro mode */ +extern int lastkey ; /* last keystoke */ +extern int kbdrep ; /* number of repetitions */ +extern int kbdm[] ; /* Holds kayboard macro data */ +extern int *kbdptr ; /* current position in keyboard buf */ +extern int *kbdend ; /* ptr to end of the keyboard */ -extern int metac ; /* current meta character */ -extern int ctlxc ; /* current control X prefix char */ -extern int reptc ; /* current universal repeat char */ -extern int abortc ; /* current abort command char */ -extern const int nlc ; /* end of input char */ +extern int metac ; /* current meta character */ +extern int ctlxc ; /* current control X prefix char */ +extern int reptc ; /* current universal repeat char */ +extern int abortc ; /* current abort command char */ +extern const int nlc ; /* end of input char */ void ue_system( const char *cmd) ; @@ -28,7 +29,7 @@ int newmlarg( char **outbufref, const char *prompt, int size) ; int newmlargt( char **outbufref, const char *prompt, int size) ; int ectoc( int c) ; -/* Get a command name from the command line or interactively */ +/* Get a command binding from the command line or interactively */ nbind_p getname( void) ; int tgetc( void) ; @@ -37,3 +38,4 @@ int getcmd( void) ; int getstring( const char *prompt, char *buf, int nbuf, int eolchar) ; #endif +/* end of input.h */ diff --git a/isa.h b/isa.h index 3f1ca06..044c9c6 100644 --- a/isa.h +++ b/isa.h @@ -1,34 +1,32 @@ /* isa.h -- isletter, islower, isupper, flipcase */ - #ifndef __ISA_H__ #define __ISA_H__ -#define NATIONL 0 /* if 1, interpret [,],\,{,},| as characters P.K. */ - - -#ifdef islower -#undef islower +#ifdef islower +# undef islower #endif -#ifdef isupper -#undef isupper +#ifdef isupper +# undef isupper #endif -#if NATIONL -#define LASTUL ']' -#define LASTLL '}' +#define NATIONL 0 /* if 1, interpret [,],\,{,},| as characters P.K. */ +#if NATIONL +# define LASTUL ']' +# define LASTLL '}' #else -#define LASTUL 'Z' -#define LASTLL 'z' +# define LASTUL 'Z' +# define LASTLL 'z' #endif -#define isletter(c) __isxletter((0xFF & (c))) -#define islower(c) isxlower((0xFF & (c))) -#define isupper(c) isxupper((0xFF & (c))) +#define isletter(c) __isxletter((0xFF & (c))) +#define islower(c) isxlower((0xFF & (c))) +#define isupper(c) isxupper((0xFF & (c))) -#define __isxletter(c) (('a' <= c && LASTLL >= c) || ('A' <= c && LASTUL >= c) || (192<=c /* && c<=255 */)) -#define isxlower(c) (('a' <= c && LASTLL >= c) || (224 <= c && 252 >= c)) -#define isxupper(c) (('A' <= c && LASTUL >= c) || (192 <= c && 220 >= c)) +#define __isxletter(c) (('a' <= c && LASTLL >= c) || \ + ('A' <= c && LASTUL >= c) || (192<=c /* && c<=255 */)) +#define isxlower(c) (('a' <= c && LASTLL >= c) || (224 <= c && 252 >= c)) +#define isxupper(c) (('A' <= c && LASTUL >= c) || (192 <= c && 220 >= c)) /* DIFCASE represents the integer difference between upper and lower case letters. It is an xor-able value, which is fortunate, since the @@ -36,9 +34,8 @@ ascii in ebcdic. */ -#define DIFCASE 0x20 /* ASCII 'a' - 'A' */ -#define flipcase( c) ((c) ^ DIFCASE) /* Toggle the case of a letter. */ - -#endif /* __ISA_H__ */ +#define DIFCASE 0x20 /* ASCII 'a' - 'A' */ +#define flipcase( c) ((c) ^ DIFCASE) /* Toggle the case of a letter. */ +#endif /* end of isa.h */ diff --git a/isearch.h b/isearch.h index e687524..38d513f 100644 --- a/isearch.h +++ b/isearch.h @@ -2,7 +2,7 @@ #ifndef __ISEARCH_H__ #define __ISEARCH_H__ -# include "names.h" /* BINDABLE */ +#include "names.h" /* BINDABLE */ BINDABLE( risearch) ; BINDABLE( fisearch) ; diff --git a/line.c b/line.c index d3d90a5..c5939e4 100644 --- a/line.c +++ b/line.c @@ -15,7 +15,7 @@ */ #include -#include /* NULL, offsetof() */ +#include /* NULL, offsetof() */ #include /* malloc(), free() */ #include @@ -26,7 +26,7 @@ #include "window.h" -int tabwidth = 8 ; /* column span of a tab */ +int tabwidth = 8 ; /* column span of a tab */ static int ldelnewline( void) ; @@ -37,117 +37,121 @@ static int ldelnewline( void) ; * was taken up by the keycode structure). */ -#define KBLOCK 250 /* sizeof kill buffer chunks */ +#define KBLOCK 250 /* sizeof kill buffer chunks */ typedef struct kill { - struct kill *d_next; /* Link to next chunk, NULL if last. */ - char d_chunk[KBLOCK]; /* Deleted text. */ + struct kill *d_next; /* Link to next chunk, NULL if last. */ + char d_chunk[KBLOCK]; /* Deleted text. */ } *kill_p ; -static kill_p kbufp = NULL ; /* current kill buffer chunk pointer */ -static kill_p kbufh = NULL ; /* kill buffer header pointer */ -static int kused = KBLOCK ; /* # of bytes used in kill buffer */ -static int klen ; /* length of kill buffer content */ -static char *value = NULL ; /* temp buffer for value */ +static kill_p kbufp = NULL ; /* current kill buffer chunk pointer */ +static kill_p kbufh = NULL ; /* kill buffer header pointer */ +static int kused = KBLOCK ; /* # of bytes used in kill buffer */ +static int klen ; /* length of kill buffer content */ +static char *value = NULL ; /* temp buffer for value */ /* * return some of the contents of the kill buffer */ char *getkill( void) { - kill_p kp ; - char *cp ; + kill_p kp ; + char *cp ; - if (kbufh == NULL) - /* no kill buffer....just a null string */ - return "" ; + if (kbufh == NULL) + /* no kill buffer....just a null string */ + return "" ; - if( value != NULL) - free( value) ; + if( value != NULL) + free( value) ; - value = (char *) malloc( klen + 1) ; - cp = value ; - for( kp = kbufh ; kp != NULL ; kp = kp->d_next) { - int size ; - - if( kp->d_next != NULL) - size = KBLOCK ; - else - size = kused ; - - memcpy( cp, kp->d_chunk, size) ; - cp += size ; - } - - *cp = 0 ; + value = (char *) malloc( klen + 1) ; + cp = value ; + for( kp = kbufh ; kp != NULL ; kp = kp->d_next) { + int size ; - /* and return the constructed value */ - return value; + if( kp->d_next != NULL) + size = KBLOCK ; + else + size = kused ; + + memcpy( cp, kp->d_chunk, size) ; + cp += size ; + } + + *cp = 0 ; + + /* and return the constructed value */ + return value; } -/* - * Move the cursor backwards by "n" characters. If "n" is less than zero call - * "forwchar" to actually do the move. Otherwise compute the new cursor - * location. Error if you try and move out of the buffer. Set the flag if the - * line pointer for dot changes. + +/* Move the cursor backwards by "n" characters. If "n" is less than zero + call "forwchar" to actually do the move. Otherwise compute the new + cursor location. Error if you try and move out of the buffer. Set the + flag if the line pointer for dot changes. */ -boolean backchar( int f, int n) { - assert( f == TRUE || (f == FALSE && n == 1)) ; - if( n < 0) - return forwchar( f, -n) ; +BBINDABLE( backchar) { + assert( f == TRUE || n == 1) ; + if( n < 0) + return forwchar( f, -n) ; - while( n--) { - if( curwp->w_doto == 0) { /* at beginning of line */ - line_p lp ; + while( n--) { + if( curwp->w_doto == 0) { /* at beginning of line */ + line_p lp = lback( curwp->w_dotp) ; + if( lp == curbp->b_linep) /* at beginning of buffer */ + return FALSE ; - lp = lback( curwp->w_dotp) ; - if( lp == curbp->b_linep) /* at beginning of buffer */ - return FALSE ; + curwp->w_dotp = lp ; + curwp->w_doto = llength( lp) ; + curwp->w_flag |= WFMOVE ; + } else { + unsigned pos = curwp->w_doto -= 1 ; + if( pos > 0) + curwp->w_doto -= utf8_revdelta( (unsigned char *) &( (curwp->w_dotp)->l_text[ pos]), pos) ; + } + } - curwp->w_dotp = lp ; - curwp->w_doto = llength( lp) ; - curwp->w_flag |= WFMOVE ; - } else { - unsigned pos ; - - pos = curwp->w_doto -= 1 ; - if( pos > 0) - curwp->w_doto -= utf8_revdelta( (unsigned char *) &( (curwp->w_dotp)->l_text[ pos]), pos) ; - } - } - - return TRUE ; + return TRUE ; } -/* - * Move the cursor forwards by "n" characters. If "n" is less than zero call - * "backchar" to actually do the move. Otherwise compute the new cursor - * location, and move ".". Error if you try and move off the end of the - * buffer. Set the flag if the line pointer for dot changes. + +/* Move the cursor forwards by "n" characters. If "n" is less than zero + call "backchar" to actually do the move. Otherwise compute the new + cursor location, and move ".". Error if you try and move off the end of + the buffer. Set the flag if the line pointer for dot changes. */ -boolean forwchar( int f, int n) { - assert( f == TRUE || (f == FALSE && n == 1)) ; - if( n < 0) - return backchar( f, -n) ; +BBINDABLE( forwchar) { + assert( f == TRUE || n == 1) ; + if( n < 0) + return backchar( f, -n) ; - while( n--) { - int len = llength( curwp->w_dotp) ; - if( curwp->w_doto == len) { /* at end of line */ - if( curwp->w_dotp == curbp->b_linep) /* at end of buffer */ - return FALSE ; + while( n--) { + int len = llength( curwp->w_dotp) ; + if( curwp->w_doto == len) { /* at end of line */ + if( curwp->w_dotp == curbp->b_linep) /* at end of buffer */ + return FALSE ; - curwp->w_dotp = lforw( curwp->w_dotp) ; - curwp->w_doto = 0 ; - curwp->w_flag |= WFMOVE ; - } else { - unicode_t unc ; - unsigned bytes ; - - bytes = utf8_to_unicode( curwp->w_dotp->l_text, curwp->w_doto, len, &unc) ; - curwp->w_doto += bytes ; - } - } + curwp->w_dotp = lforw( curwp->w_dotp) ; + curwp->w_doto = 0 ; + curwp->w_flag |= WFMOVE ; + } else { + unicode_t unc ; - return TRUE ; + curwp->w_doto += utf8_to_unicode( curwp->w_dotp->l_text, + curwp->w_doto, len, &unc) ; + /* check if next char is null width unicode */ + while( curwp->w_doto != len) { + unsigned bytes = utf8_to_unicode( curwp->w_dotp->l_text, + curwp->w_doto, len, &unc) ; + if( utf8_width( unc) == 0) + curwp->w_doto += bytes ; + else + break ; + } + } + } + + return TRUE ; } /* @@ -157,23 +161,23 @@ boolean forwchar( int f, int n) { * message in the message line if no space. */ line_p lalloc( int used) { -#define BLOCK_SIZE 16 /* Line block chunk size. */ +#define BLOCK_SIZE 16 /* Line block chunk size. */ /* rounding down use masking instead or modulo when BLOCK_SIZE is power of 2 */ #if (BLOCK_SIZE & -BLOCK_SIZE) == BLOCK_SIZE - int size = (used + BLOCK_SIZE) & ~(BLOCK_SIZE - 1) ; + int size = (used + BLOCK_SIZE) & ~(BLOCK_SIZE - 1) ; #else - int size = used + BLOCK_SIZE - used % BLOCK_SIZE ; + int size = used + BLOCK_SIZE - used % BLOCK_SIZE ; #endif - line_p lp = (line_p) malloc( offsetof( struct line, l_text) + size) ; - if( lp == NULL) - mloutstr( "(OUT OF MEMORY)") ; - else { - lp->l_size = size ; - lp->l_used = used ; - } + line_p lp = (line_p) malloc( offsetof( struct line, l_text) + size) ; + if( lp == NULL) + mloutstr( "(OUT OF MEMORY)") ; + else { + lp->l_size = size ; + lp->l_used = used ; + } - return lp ; + return lp ; } /* @@ -183,40 +187,40 @@ line_p lalloc( int used) { * conditions described in the above comments don't hold here. */ void lfree( line_p lp) { - buffer_p bp; - struct window *wp; + buffer_p bp; + struct window *wp; - wp = wheadp; - while (wp != NULL) { - if (wp->w_linep == lp) - wp->w_linep = lp->l_fp; - if (wp->w_dotp == lp) { - wp->w_dotp = lp->l_fp; - wp->w_doto = 0; - } - if (wp->w_markp == lp) { - wp->w_markp = lp->l_fp; - wp->w_marko = 0; - } - wp = wp->w_wndp; - } - bp = bheadp; - while (bp != NULL) { - if (bp->b_nwnd == 0) { - if (bp->b_dotp == lp) { - bp->b_dotp = lp->l_fp; - bp->b_doto = 0; - } - if (bp->b_markp == lp) { - bp->b_markp = lp->l_fp; - bp->b_marko = 0; - } - } - bp = bp->b_bufp; - } - lp->l_bp->l_fp = lp->l_fp; - lp->l_fp->l_bp = lp->l_bp; - free((char *) lp); + wp = wheadp; + while (wp != NULL) { + if (wp->w_linep == lp) + wp->w_linep = lp->l_fp; + if (wp->w_dotp == lp) { + wp->w_dotp = lp->l_fp; + wp->w_doto = 0; + } + if (wp->w_markp == lp) { + wp->w_markp = lp->l_fp; + wp->w_marko = 0; + } + wp = wp->w_wndp; + } + bp = bheadp; + while (bp != NULL) { + if (bp->b_nwnd == 0) { + if (bp->b_dotp == lp) { + bp->b_dotp = lp->l_fp; + bp->b_doto = 0; + } + if (bp->b_markp == lp) { + bp->b_markp = lp->l_fp; + bp->b_marko = 0; + } + } + bp = bp->b_bufp; + } + lp->l_bp->l_fp = lp->l_fp; + lp->l_fp->l_bp = lp->l_bp; + free((char *) lp); } /* @@ -228,32 +232,32 @@ void lfree( line_p lp) { */ void lchange(int flag) { - struct window *wp; + struct window *wp; - if (curbp->b_nwnd != 1) /* Ensure hard. */ - flag = WFHARD; - if ((curbp->b_flag & BFCHG) == 0) { /* First change, so */ - flag |= WFMODE; /* update mode lines. */ - curbp->b_flag |= BFCHG; - } - wp = wheadp; - while (wp != NULL) { - if (wp->w_bufp == curbp) - wp->w_flag |= flag; - wp = wp->w_wndp; - } + if (curbp->b_nwnd != 1) /* Ensure hard. */ + flag = WFHARD; + if ((curbp->b_flag & BFCHG) == 0) { /* First change, so */ + flag |= WFMODE; /* update mode lines. */ + curbp->b_flag |= BFCHG; + } + wp = wheadp; + while (wp != NULL) { + if (wp->w_bufp == curbp) + wp->w_flag |= flag; + wp = wp->w_wndp; + } } /* * insert spaces forward into text * - * int f, n; default flag and numeric argument + * int f, n; default flag and numeric argument */ BINDABLE( insspace) { - assert( !(curbp->b_mode & MDVIEW)) ; - linsert( n, ' ') ; - backchar( f, n) ; - return TRUE ; + assert( !(curbp->b_mode & MDVIEW)) ; + linsert( n, ' ') ; + backchar( f, n) ; + return TRUE ; } /* @@ -261,24 +265,24 @@ BINDABLE( insspace) { */ int linstr( char *instr) { - int status = TRUE ; + int status = TRUE ; - if( instr != NULL) { - unicode_t tmpc ; + if( instr != NULL) { + unicode_t tmpc ; - while( (tmpc = *instr++ & 0xFF)) { - status = - (tmpc == '\n' ? lnewline() : (int) linsert_byte( 1, tmpc)) ; + while( (tmpc = *instr++ & 0xFF)) { + status = + (tmpc == '\n' ? lnewline() : (int) linsert_byte( 1, tmpc)) ; - /* Insertion error? */ - if( status != TRUE) { - mloutstr( "%Out of memory while inserting") ; - return status ; - } - } - } + /* Insertion error? */ + if( status != TRUE) { + mloutstr( "%Out of memory while inserting") ; + return status ; + } + } + } - return status ; + return status ; } /* @@ -292,142 +296,142 @@ int linstr( char *instr) { */ boolean linsert_byte( int n, int c) { - char *cp1; - char *cp2; - line_p lp1, lp2, lp3 ; - int doto; - int i; - struct window *wp; + char *cp1; + char *cp2; + line_p lp1, lp2, lp3 ; + int doto; + int i; + struct window *wp; - assert( (curbp->b_mode & MDVIEW) == 0) ; + assert( (curbp->b_mode & MDVIEW) == 0) ; - lchange(WFEDIT); - lp1 = curwp->w_dotp; /* Current line */ - if (lp1 == curbp->b_linep) { /* At the end: special */ - if (curwp->w_doto != 0) { - mloutstr( "bug: linsert") ; - return FALSE; - } + lchange(WFEDIT); + lp1 = curwp->w_dotp; /* Current line */ + if (lp1 == curbp->b_linep) { /* At the end: special */ + if (curwp->w_doto != 0) { + mloutstr( "bug: linsert") ; + return FALSE; + } - lp2 = lalloc( n) ; /* Allocate new line */ - if( lp2 == NULL) - return FALSE ; + lp2 = lalloc( n) ; /* Allocate new line */ + if( lp2 == NULL) + return FALSE ; - lp3 = lp1->l_bp; /* Previous line */ - lp3->l_fp = lp2; /* Link in */ - lp2->l_fp = lp1; - lp1->l_bp = lp2; - lp2->l_bp = lp3; - for (i = 0; i < n; ++i) - lp2->l_text[i] = c; - curwp->w_dotp = lp2; - curwp->w_doto = n; - return TRUE; - } - doto = curwp->w_doto; /* Save for later. */ - if (lp1->l_used + n > lp1->l_size) { /* Hard: reallocate */ - lp2 = lalloc( lp1->l_used + n) ; - if( lp2 == NULL) - return FALSE ; + lp3 = lp1->l_bp; /* Previous line */ + lp3->l_fp = lp2; /* Link in */ + lp2->l_fp = lp1; + lp1->l_bp = lp2; + lp2->l_bp = lp3; + for (i = 0; i < n; ++i) + lp2->l_text[i] = c; + curwp->w_dotp = lp2; + curwp->w_doto = n; + return TRUE; + } + doto = curwp->w_doto; /* Save for later. */ + if (lp1->l_used + n > lp1->l_size) { /* Hard: reallocate */ + lp2 = lalloc( lp1->l_used + n) ; + if( lp2 == NULL) + return FALSE ; - cp1 = &lp1->l_text[0]; - cp2 = &lp2->l_text[0]; - while (cp1 != &lp1->l_text[doto]) - *cp2++ = *cp1++; - cp2 += n; - while (cp1 != &lp1->l_text[lp1->l_used]) - *cp2++ = *cp1++; - lp1->l_bp->l_fp = lp2; - lp2->l_fp = lp1->l_fp; - lp1->l_fp->l_bp = lp2; - lp2->l_bp = lp1->l_bp; - free((char *) lp1); - } else { /* Easy: in place */ - lp2 = lp1; /* Pretend new line */ - lp2->l_used += n; - cp2 = &lp1->l_text[lp1->l_used]; - cp1 = cp2 - n; - while (cp1 != &lp1->l_text[doto]) - *--cp2 = *--cp1; - } - for (i = 0; i < n; ++i) /* Add the characters */ - lp2->l_text[doto + i] = c; - wp = wheadp; /* Update windows */ - while (wp != NULL) { - if (wp->w_linep == lp1) - wp->w_linep = lp2; - if (wp->w_dotp == lp1) { - wp->w_dotp = lp2; - if (wp == curwp || wp->w_doto > doto) - wp->w_doto += n; - } - if (wp->w_markp == lp1) { - wp->w_markp = lp2; - if (wp->w_marko > doto) - wp->w_marko += n; - } - wp = wp->w_wndp; - } - return TRUE; + cp1 = &lp1->l_text[0]; + cp2 = &lp2->l_text[0]; + while (cp1 != &lp1->l_text[doto]) + *cp2++ = *cp1++; + cp2 += n; + while (cp1 != &lp1->l_text[lp1->l_used]) + *cp2++ = *cp1++; + lp1->l_bp->l_fp = lp2; + lp2->l_fp = lp1->l_fp; + lp1->l_fp->l_bp = lp2; + lp2->l_bp = lp1->l_bp; + free((char *) lp1); + } else { /* Easy: in place */ + lp2 = lp1; /* Pretend new line */ + lp2->l_used += n; + cp2 = &lp1->l_text[lp1->l_used]; + cp1 = cp2 - n; + while (cp1 != &lp1->l_text[doto]) + *--cp2 = *--cp1; + } + for (i = 0; i < n; ++i) /* Add the characters */ + lp2->l_text[doto + i] = c; + wp = wheadp; /* Update windows */ + while (wp != NULL) { + if (wp->w_linep == lp1) + wp->w_linep = lp2; + if (wp->w_dotp == lp1) { + wp->w_dotp = lp2; + if (wp == curwp || wp->w_doto > doto) + wp->w_doto += n; + } + if (wp->w_markp == lp1) { + wp->w_markp = lp2; + if (wp->w_marko > doto) + wp->w_marko += n; + } + wp = wp->w_wndp; + } + return TRUE; } int linsert( int n, unicode_t c) { - assert( n >= 0) ; - assert( !(curbp->b_mode & MDVIEW)) ; + assert( n >= 0) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if( n > 0) { - char utf8[ 4] ; - int bytes ; + if( n > 0) { + char utf8[ 4] ; + int bytes ; - bytes = unicode_to_utf8(c, utf8) ; - if (bytes == 1) - return linsert_byte(n, (unsigned char) utf8[0]); + bytes = unicode_to_utf8(c, utf8) ; + if (bytes == 1) + return linsert_byte(n, (unsigned char) utf8[0]); - do { - int j ; + do { + int j ; - for( j = 0 ; j < bytes ; j += 1) - if( !linsert_byte( 1, (unsigned char) utf8[ j])) - return FALSE ; - } while( --n > 0) ; - } + for( j = 0 ; j < bytes ; j += 1) + if( !linsert_byte( 1, (unsigned char) utf8[ j])) + return FALSE ; + } while( --n > 0) ; + } - return TRUE; + return TRUE; } /* Overwrite a character into the current line at the current position * - * int c ; character to overwrite on current position + * int c ; character to overwrite on current position */ static int lowrite( int c) { - if( curwp->w_doto < curwp->w_dotp->l_used - && ( lgetc( curwp->w_dotp, curwp->w_doto) != '\t' - || (curwp->w_doto % tabwidth) == (tabwidth - 1) - )) - ldelchar( 1, FALSE) ; + if( curwp->w_doto < curwp->w_dotp->l_used + && ( lgetc( curwp->w_dotp, curwp->w_doto) != '\t' + || (curwp->w_doto % tabwidth) == (tabwidth - 1) + )) + ldelchar( 1, FALSE) ; - return linsert( 1, c) ; + return linsert( 1, c) ; } /* * lover -- Overwrite a string at the current point */ int lover( char *ostr) { - int status = TRUE ; + int status = TRUE ; - if( ostr != NULL) { - char tmpc ; + if( ostr != NULL) { + char tmpc ; - while( (tmpc = *ostr++)) { - status = (tmpc == '\n' ? lnewline() : lowrite( tmpc)) ; - if( status != TRUE) { /* Insertion error? */ - mloutstr( "%Out of memory while overwriting") ; - return status ; - } - } - } - - return status ; + while( (tmpc = *ostr++)) { + status = (tmpc == '\n' ? lnewline() : lowrite( tmpc)) ; + if( status != TRUE) { /* Insertion error? */ + mloutstr( "%Out of memory while overwriting") ; + return status ; + } + } + } + + return status ; } /* @@ -439,65 +443,65 @@ int lover( char *ostr) { * split forces more updating. */ int lnewline( void) { - char *cp1; - char *cp2; - line_p lp1, lp2 ; - int doto; - struct window *wp; + char *cp1; + char *cp2; + line_p lp1, lp2 ; + int doto; + struct window *wp; - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; #if SCROLLCODE - lchange(WFHARD | WFINS); + lchange(WFHARD | WFINS); #else - lchange(WFHARD); + lchange(WFHARD); #endif - lp1 = curwp->w_dotp; /* Get the address and */ - doto = curwp->w_doto; /* offset of "." */ - lp2 = lalloc( doto) ; /* New first half line */ - if( lp2 == NULL) - return FALSE ; + lp1 = curwp->w_dotp; /* Get the address and */ + doto = curwp->w_doto; /* offset of "." */ + lp2 = lalloc( doto) ; /* New first half line */ + if( lp2 == NULL) + return FALSE ; - cp1 = &lp1->l_text[0]; /* Shuffle text around */ - cp2 = &lp2->l_text[0]; - while (cp1 != &lp1->l_text[doto]) - *cp2++ = *cp1++; - cp2 = &lp1->l_text[0]; - while (cp1 != &lp1->l_text[lp1->l_used]) - *cp2++ = *cp1++; - lp1->l_used -= doto; - lp2->l_bp = lp1->l_bp; - lp1->l_bp = lp2; - lp2->l_bp->l_fp = lp2; - lp2->l_fp = lp1; - wp = wheadp; /* Windows */ - while (wp != NULL) { - if (wp->w_linep == lp1) - wp->w_linep = lp2; - if (wp->w_dotp == lp1) { - if (wp->w_doto < doto) - wp->w_dotp = lp2; - else - wp->w_doto -= doto; - } - if (wp->w_markp == lp1) { - if (wp->w_marko < doto) - wp->w_markp = lp2; - else - wp->w_marko -= doto; - } - wp = wp->w_wndp; - } - return TRUE; + cp1 = &lp1->l_text[0]; /* Shuffle text around */ + cp2 = &lp2->l_text[0]; + while (cp1 != &lp1->l_text[doto]) + *cp2++ = *cp1++; + cp2 = &lp1->l_text[0]; + while (cp1 != &lp1->l_text[lp1->l_used]) + *cp2++ = *cp1++; + lp1->l_used -= doto; + lp2->l_bp = lp1->l_bp; + lp1->l_bp = lp2; + lp2->l_bp->l_fp = lp2; + lp2->l_fp = lp1; + wp = wheadp; /* Windows */ + while (wp != NULL) { + if (wp->w_linep == lp1) + wp->w_linep = lp2; + if (wp->w_dotp == lp1) { + if (wp->w_doto < doto) + wp->w_dotp = lp2; + else + wp->w_doto -= doto; + } + if (wp->w_markp == lp1) { + if (wp->w_marko < doto) + wp->w_markp = lp2; + else + wp->w_marko -= doto; + } + wp = wp->w_wndp; + } + return TRUE; } int lgetchar( unicode_t *c) { - if( curwp->w_dotp->l_used == curwp->w_doto) { - *c = (curbp->b_mode & MDDOS) ? '\r' : '\n' ; - return 1 ; - } else - return utf8_to_unicode( curwp->w_dotp->l_text, curwp->w_doto, - llength( curwp->w_dotp), c) ; + if( curwp->w_dotp->l_used == curwp->w_doto) { + *c = (curbp->b_mode & MDDOS) ? '\r' : '\n' ; + return 1 ; + } else + return utf8_to_unicode( curwp->w_dotp->l_text, curwp->w_doto, + llength( curwp->w_dotp), c) ; } /* @@ -509,14 +513,14 @@ int lgetchar( unicode_t *c) { */ boolean ldelchar( long n, boolean kflag) { /* testing for read only mode is done by ldelete() */ - while( n-- > 0) { - unicode_t c; + while( n-- > 0) { + unicode_t c; - if( !ldelete( lgetchar( &c), kflag)) - return FALSE ; - } + if( !ldelete( lgetchar( &c), kflag)) + return FALSE ; + } - return TRUE ; + return TRUE ; } /* @@ -525,101 +529,101 @@ boolean ldelchar( long n, boolean kflag) { * deleted, and FALSE if they were not (because dot ran into the end of the * buffer. The "kflag" is TRUE if the text should be put in the kill buffer. * - * long n; # of chars to delete - * int kflag; put killed text in kill buffer flag + * long n; # of chars to delete + * int kflag; put killed text in kill buffer flag */ boolean ldelete( long n, boolean kflag) { - char *cp1; - char *cp2; - line_p dotp; - int doto; - int chunk; - struct window *wp; + char *cp1; + char *cp2; + line_p dotp; + int doto; + int chunk; + struct window *wp; - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - while( n > 0) { - dotp = curwp->w_dotp; - doto = curwp->w_doto; - if (dotp == curbp->b_linep) /* Hit end of buffer. */ - return FALSE; - chunk = dotp->l_used - doto; /* Size of chunk. */ - if (chunk > n) - chunk = n; - if (chunk == 0) { /* End of line, merge. */ + while( n > 0) { + dotp = curwp->w_dotp; + doto = curwp->w_doto; + if (dotp == curbp->b_linep) /* Hit end of buffer. */ + return FALSE; + chunk = dotp->l_used - doto; /* Size of chunk. */ + if (chunk > n) + chunk = n; + if (chunk == 0) { /* End of line, merge. */ #if SCROLLCODE - lchange(WFHARD | WFKILLS); + lchange(WFHARD | WFKILLS); #else - lchange(WFHARD); + lchange(WFHARD); #endif - if (ldelnewline() == FALSE - || (kflag != FALSE && kinsert('\n') == FALSE)) - return FALSE; - --n; - continue; - } - lchange(WFEDIT); - cp1 = &dotp->l_text[doto]; /* Scrunch text. */ - cp2 = cp1 + chunk; - if (kflag != FALSE) { /* Kill? */ - while (cp1 != cp2) { - if (kinsert(*cp1) == FALSE) - return FALSE; - ++cp1; - } - cp1 = &dotp->l_text[doto]; - } - while (cp2 != &dotp->l_text[dotp->l_used]) - *cp1++ = *cp2++; - dotp->l_used -= chunk; - wp = wheadp; /* Fix windows */ - while (wp != NULL) { - if (wp->w_dotp == dotp && wp->w_doto >= doto) { - wp->w_doto -= chunk; - if (wp->w_doto < doto) - wp->w_doto = doto; - } - if (wp->w_markp == dotp && wp->w_marko >= doto) { - wp->w_marko -= chunk; - if (wp->w_marko < doto) - wp->w_marko = doto; - } - wp = wp->w_wndp; - } - n -= chunk; - } - return TRUE; + if (ldelnewline() == FALSE + || (kflag != FALSE && kinsert('\n') == FALSE)) + return FALSE; + --n; + continue; + } + lchange(WFEDIT); + cp1 = &dotp->l_text[doto]; /* Scrunch text. */ + cp2 = cp1 + chunk; + if (kflag != FALSE) { /* Kill? */ + while (cp1 != cp2) { + if (kinsert(*cp1) == FALSE) + return FALSE; + ++cp1; + } + cp1 = &dotp->l_text[doto]; + } + while (cp2 != &dotp->l_text[dotp->l_used]) + *cp1++ = *cp2++; + dotp->l_used -= chunk; + wp = wheadp; /* Fix windows */ + while (wp != NULL) { + if (wp->w_dotp == dotp && wp->w_doto >= doto) { + wp->w_doto -= chunk; + if (wp->w_doto < doto) + wp->w_doto = doto; + } + if (wp->w_markp == dotp && wp->w_marko >= doto) { + wp->w_marko -= chunk; + if (wp->w_marko < doto) + wp->w_marko = doto; + } + wp = wp->w_wndp; + } + n -= chunk; + } + return TRUE; } /* - * getctext: grab and return a string with the text of - * the current line + * getctext: grab and return a string with the text of + * the current line */ char *getctext( void) { - line_p lp ; /* line to copy */ - int size; /* length of line to return */ - static int rsize = 0 ; - static char *rline ; /* line to return */ + line_p lp ; /* line to copy */ + int size; /* length of line to return */ + static int rsize = 0 ; + static char *rline ; /* line to return */ - /* find the contents of the current line and its length */ - lp = curwp->w_dotp; - size = lp->l_used; - if( size >= rsize) { - if( rsize) - free( rline) ; + /* find the contents of the current line and its length */ + lp = curwp->w_dotp; + size = lp->l_used; + if( size >= rsize) { + if( rsize) + free( rline) ; - rsize = size + 1 ; - rline = malloc( rsize) ; - if( rline == NULL) { - rsize = 0 ; - return "" ; - } - } + rsize = size + 1 ; + rline = malloc( rsize) ; + if( rline == NULL) { + rsize = 0 ; + return "" ; + } + } - /* copy it across */ - memcpy( rline, lp->l_text, size) ; - rline[ size] = 0 ; - return rline ; + /* copy it across */ + memcpy( rline, lp->l_text, size) ; + rline[ size] = 0 ; + return rline ; } /* @@ -632,82 +636,82 @@ char *getctext( void) { * "ldelete" only. */ static int ldelnewline( void) { - char *cp1; - char *cp2; - line_p lp1, lp2, lp3 ; - struct window *wp; + char *cp1; + char *cp2; + line_p lp1, lp2, lp3 ; + struct window *wp; - assert( (curbp->b_mode & MDVIEW) == 0) ; + assert( (curbp->b_mode & MDVIEW) == 0) ; - lp1 = curwp->w_dotp; - lp2 = lp1->l_fp; - if (lp2 == curbp->b_linep) { /* At the buffer end. */ - if (lp1->l_used == 0) /* Blank line. */ - lfree(lp1); - return TRUE; - } - if (lp2->l_used <= lp1->l_size - lp1->l_used) { - cp1 = &lp1->l_text[lp1->l_used]; - cp2 = &lp2->l_text[0]; - while (cp2 != &lp2->l_text[lp2->l_used]) - *cp1++ = *cp2++; - wp = wheadp; - while (wp != NULL) { - if (wp->w_linep == lp2) - wp->w_linep = lp1; - if (wp->w_dotp == lp2) { - wp->w_dotp = lp1; - wp->w_doto += lp1->l_used; - } - if (wp->w_markp == lp2) { - wp->w_markp = lp1; - wp->w_marko += lp1->l_used; - } - wp = wp->w_wndp; - } - lp1->l_used += lp2->l_used; - lp1->l_fp = lp2->l_fp; - lp2->l_fp->l_bp = lp1; - free((char *) lp2); - return TRUE; - } + lp1 = curwp->w_dotp; + lp2 = lp1->l_fp; + if (lp2 == curbp->b_linep) { /* At the buffer end. */ + if (lp1->l_used == 0) /* Blank line. */ + lfree(lp1); + return TRUE; + } + if (lp2->l_used <= lp1->l_size - lp1->l_used) { + cp1 = &lp1->l_text[lp1->l_used]; + cp2 = &lp2->l_text[0]; + while (cp2 != &lp2->l_text[lp2->l_used]) + *cp1++ = *cp2++; + wp = wheadp; + while (wp != NULL) { + if (wp->w_linep == lp2) + wp->w_linep = lp1; + if (wp->w_dotp == lp2) { + wp->w_dotp = lp1; + wp->w_doto += lp1->l_used; + } + if (wp->w_markp == lp2) { + wp->w_markp = lp1; + wp->w_marko += lp1->l_used; + } + wp = wp->w_wndp; + } + lp1->l_used += lp2->l_used; + lp1->l_fp = lp2->l_fp; + lp2->l_fp->l_bp = lp1; + free((char *) lp2); + return TRUE; + } - lp3 = lalloc( lp1->l_used + lp2->l_used) ; - if( lp3 == NULL) - return FALSE ; + lp3 = lalloc( lp1->l_used + lp2->l_used) ; + if( lp3 == NULL) + return FALSE ; - cp1 = &lp1->l_text[0]; - cp2 = &lp3->l_text[0]; - while (cp1 != &lp1->l_text[lp1->l_used]) - *cp2++ = *cp1++; - cp1 = &lp2->l_text[0]; - while (cp1 != &lp2->l_text[lp2->l_used]) - *cp2++ = *cp1++; - lp1->l_bp->l_fp = lp3; - lp3->l_fp = lp2->l_fp; - lp2->l_fp->l_bp = lp3; - lp3->l_bp = lp1->l_bp; - wp = wheadp; - while (wp != NULL) { - if (wp->w_linep == lp1 || wp->w_linep == lp2) - wp->w_linep = lp3; - if (wp->w_dotp == lp1) - wp->w_dotp = lp3; - else if (wp->w_dotp == lp2) { - wp->w_dotp = lp3; - wp->w_doto += lp1->l_used; - } - if (wp->w_markp == lp1) - wp->w_markp = lp3; - else if (wp->w_markp == lp2) { - wp->w_markp = lp3; - wp->w_marko += lp1->l_used; - } - wp = wp->w_wndp; - } - free((char *) lp1); - free((char *) lp2); - return TRUE; + cp1 = &lp1->l_text[0]; + cp2 = &lp3->l_text[0]; + while (cp1 != &lp1->l_text[lp1->l_used]) + *cp2++ = *cp1++; + cp1 = &lp2->l_text[0]; + while (cp1 != &lp2->l_text[lp2->l_used]) + *cp2++ = *cp1++; + lp1->l_bp->l_fp = lp3; + lp3->l_fp = lp2->l_fp; + lp2->l_fp->l_bp = lp3; + lp3->l_bp = lp1->l_bp; + wp = wheadp; + while (wp != NULL) { + if (wp->w_linep == lp1 || wp->w_linep == lp2) + wp->w_linep = lp3; + if (wp->w_dotp == lp1) + wp->w_dotp = lp3; + else if (wp->w_dotp == lp2) { + wp->w_dotp = lp3; + wp->w_doto += lp1->l_used; + } + if (wp->w_markp == lp1) + wp->w_markp = lp3; + else if (wp->w_markp == lp2) { + wp->w_markp = lp3; + wp->w_marko += lp1->l_used; + } + wp = wp->w_wndp; + } + free((char *) lp1); + free((char *) lp2); + return TRUE; } /* @@ -717,59 +721,59 @@ static int ldelnewline( void) { */ void kdelete(void) { - kill_p kp; /* ptr to scan kill buffer chunk list */ + kill_p kp; /* ptr to scan kill buffer chunk list */ - if (kbufh != NULL) { + if (kbufh != NULL) { - /* first, delete all the chunks */ - kbufp = kbufh; - while (kbufp != NULL) { - kp = kbufp->d_next; - free(kbufp); - kbufp = kp; - } + /* first, delete all the chunks */ + kbufp = kbufh; + while (kbufp != NULL) { + kp = kbufp->d_next; + free(kbufp); + kbufp = kp; + } - /* and reset all the kill buffer pointers */ - kbufh = kbufp = NULL; - kused = KBLOCK; - klen = 0 ; - if( value != NULL) { - free( value) ; - value = NULL ; - } - } + /* and reset all the kill buffer pointers */ + kbufh = kbufp = NULL; + kused = KBLOCK; + klen = 0 ; + if( value != NULL) { + free( value) ; + value = NULL ; + } + } } /* * Insert a character to the kill buffer, allocating new chunks as needed. * Return TRUE if all is well, and FALSE on errors. * - * int c; character to insert in the kill buffer + * int c; character to insert in the kill buffer */ int kinsert( int c) { - /* check to see if we need a new chunk */ - if( kused >= KBLOCK) { - kill_p nchunk = malloc( sizeof *nchunk) ; - if( nchunk == NULL) - return FALSE ; + /* check to see if we need a new chunk */ + if( kused >= KBLOCK) { + kill_p nchunk = malloc( sizeof *nchunk) ; + if( nchunk == NULL) + return FALSE ; - if( kbufh == NULL) { /* set head ptr if first time */ - kbufh = nchunk ; - klen = 0 ; - } + if( kbufh == NULL) { /* set head ptr if first time */ + kbufh = nchunk ; + klen = 0 ; + } - if( kbufp != NULL) /* point the current to this new one */ - kbufp->d_next = nchunk ; + if( kbufp != NULL) /* point the current to this new one */ + kbufp->d_next = nchunk ; - kbufp = nchunk ; - kbufp->d_next = NULL ; - kused = 0 ; - } + kbufp = nchunk ; + kbufp->d_next = NULL ; + kused = 0 ; + } - /* and now insert the character */ - kbufp->d_chunk[ kused++] = c ; - klen += 1 ; - return TRUE ; + /* and now insert the character */ + kbufp->d_chunk[ kused++] = c ; + klen += 1 ; + return TRUE ; } /* @@ -778,41 +782,41 @@ int kinsert( int c) { * check for errors. Bound to "C-Y". */ BINDABLE( yank) { - int c; - int i; - char *sp; /* pointer into string to insert */ - kill_p kp; /* pointer into kill buffer */ + int c; + int i; + char *sp; /* pointer into string to insert */ + kill_p kp; /* pointer into kill buffer */ - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if (n < 0) - return FALSE; - /* make sure there is something to yank */ - if (kbufh == NULL) - return TRUE; /* not an error, just nothing */ + if (n < 0) + return FALSE; + /* make sure there is something to yank */ + if (kbufh == NULL) + return TRUE; /* not an error, just nothing */ - /* for each time.... */ - while (n--) { - kp = kbufh; - while (kp != NULL) { - if (kp->d_next == NULL) - i = kused; - else - i = KBLOCK; - sp = kp->d_chunk; - while (i--) { - if ((c = *sp++) == '\n') { - if (lnewline() == FALSE) - return FALSE; - } else { - if (linsert_byte(1, c) == FALSE) - return FALSE; - } - } - kp = kp->d_next; - } - } - return TRUE; + /* for each time.... */ + while (n--) { + kp = kbufh; + while (kp != NULL) { + if (kp->d_next == NULL) + i = kused; + else + i = KBLOCK; + sp = kp->d_chunk; + while (i--) { + if ((c = *sp++) == '\n') { + if (lnewline() == FALSE) + return FALSE; + } else { + if (linsert_byte(1, c) == FALSE) + return FALSE; + } + } + kp = kp->d_next; + } + } + return TRUE; } /* @@ -820,7 +824,7 @@ BINDABLE( yank) { * VIEW (read-only) mode */ boolean rdonly( void) { - return mloutfail( "(Key illegal in VIEW mode)") ; + return mloutfail( "(Key illegal in VIEW mode)") ; } /* end of line.c */ diff --git a/line.h b/line.h index 656a83b..0dc78e3 100644 --- a/line.h +++ b/line.h @@ -1,26 +1,24 @@ /* line.h -- line centric interface */ - #ifndef _LINE_H_ #define _LINE_H_ #include "names.h" #include "utf8.h" -/* - * All text is kept in circularly linked lists of "struct line" structures. - * These begin at the header line (which is the blank line beyond the end - * of the buffer). This line is pointed to by the "struct buffer". Each - * line contains a number of bytes in the line (the "used" size), the size - * of the text array, and the text. The end of line is not stored as a - * byte; it's implied. Future additions will include update hints, and a - * list of marks into the line. +/* All text is kept in circularly linked lists of "struct line" structures. + These begin at the header line (which is the blank line beyond the end + of the buffer). This line is pointed to by the "struct buffer". Each + line contains a number of bytes in the line (the "used" size), the size + of the text array, and the text. The end of line is not stored as a + byte; it's implied. Future additions will include update hints, and a + list of marks into the line. */ typedef struct line { - struct line *l_fp ; /* Forward link to the next line */ - struct line *l_bp ; /* Backward link to the previous line */ - int l_size ; /* Allocated size */ - int l_used ; /* Used size */ - char l_text[ 1] ; /* A bunch of characters */ + struct line *l_fp ; /* Forward link to the next line */ + struct line *l_bp ; /* Backward link to the previous line */ + int l_size ; /* Allocated size */ + int l_used ; /* Used size */ + char l_text[ 1] ; /* A bunch of characters */ } *line_p ; #define lforw(lp) ((lp)->l_fp) @@ -29,15 +27,15 @@ typedef struct line { #define lputc(lp, n, c) ((lp)->l_text[(n)]=(c)) #define llength(lp) ((lp)->l_used) -extern int tabwidth ; /* Map to $tab, default to 8, can be set to [1, .. */ +extern int tabwidth ; /* Map to $tab, default to 8, can be set to [1, .. */ char *getkill( void) ; /* Bindable functions */ -boolean backchar( int f, int n) ; -boolean forwchar( int f, int n) ; -BINDABLE( insspace) ; -BINDABLE( yank) ; +BBINDABLE( backchar) ; +BBINDABLE( forwchar) ; + BINDABLE( insspace) ; + BINDABLE( yank) ; void lfree( line_p lp) ; void lchange( int flag) ; @@ -54,8 +52,7 @@ void kdelete( void) ; int kinsert( int c) ; line_p lalloc( int minsize) ; /* Allocate a line of at least minsize chars. */ -boolean rdonly( void) ; /* Read Only error message */ - -#endif /* _LINE_H_ */ +boolean rdonly( void) ; /* Read Only error message */ +#endif /* end of line.h */ diff --git a/lock.h b/lock.h index f7907c0..9b82e91 100644 --- a/lock.h +++ b/lock.h @@ -1,18 +1,15 @@ /* lock.h -- */ - #ifndef _LOCK_H_ #define _LOCK_H_ #include "estruct.h" -#if BSD | SVR4 - +#if BSD | SVR4 int lockchk( const char *fname) ; int lockrel( void) ; int lock( const char *fname) ; int unlock( const char *fname) ; - -#endif #endif +#endif /* end of lock.h */ diff --git a/mlout.h b/mlout.h index 4bb6c22..444cfde 100644 --- a/mlout.h +++ b/mlout.h @@ -1,5 +1,4 @@ /* mlout.h -- message line output interface */ - #ifndef __MLOUT_H__ #define __MLOUT_H__ @@ -8,8 +7,7 @@ extern void (*mloutfmt)( const char *, ...) ; void mloutstr( const char *str) ; -boolean mloutfail( const char *msg) ; /* output with BELL and return FALSE */ - -#endif /* __MLOUT_H__ */ +boolean mloutfail( const char *msg) ; /* output with BELL and return FALSE */ +#endif /* end of mlout.h */ diff --git a/names.h b/names.h index 6be2190..a4cdc1f 100644 --- a/names.h +++ b/names.h @@ -1,20 +1,20 @@ /* names.h -- mapping of functions to names and keys */ #ifndef _NAMES_H_ -# define _NAMES_H_ +#define _NAMES_H_ #include "retcode.h" -#define CTL_ 0x01000000 /* Control flag, or'ed in */ -#define META 0x02000000 /* Meta flag, or'ed in */ -#define CTLX 0x04000000 /* ^X flag, or'ed in */ -#define SPEC 0x08000000 /* special key (function keys) */ -#define PRFXMASK 0x0F000000 /* prefix mask */ +#define CTL_ 0x01000000 /* Control flag, or'ed in */ +#define META 0x02000000 /* Meta flag, or'ed in */ +#define CTLX 0x04000000 /* ^X flag, or'ed in */ +#define SPEC 0x08000000 /* special key (function keys) */ +#define PRFXMASK 0x0F000000 /* prefix mask */ /* Bindable uEMACS function pointer type and definition template */ #define BINDABLE( fname) int fname( boolean f, int n) #define BBINDABLE( fname) boolean fname( boolean f, int n) -#define TBINDABLE BBINDABLE +#define TBINDABLE BBINDABLE typedef BINDABLE( (*fnp_t)) ; @@ -46,7 +46,7 @@ extern kbind_p keytab ; /* key bind to functions table */ boolean init_bindings( void) ; kbind_p setkeybinding( unsigned key, nbind_p nbp) ; boolean delkeybinding( unsigned key) ; -kbind_p getkeybinding( unsigned key) ; /* look up by key code */ +kbind_p getkeybinding( unsigned key) ; /* look up by key code */ /* find a name to function association in the name to function mapping table */ nbind_p fncmatch( char *name) ; /* look up by name */ diff --git a/pklock.h b/pklock.h index 81955ee..745aec5 100644 --- a/pklock.h +++ b/pklock.h @@ -1,17 +1,13 @@ /* pklock.h -- */ - #ifndef _PKLOCK_H_ #define _PKLOCK_H_ #include "estruct.h" #if (FILOCK && BSD) || SVR4 - char *dolock( const char *fname) ; char *undolock( const char *fname) ; - #endif #endif - /* end of pklock.h */ diff --git a/random.c b/random.c index 6569ed5..aa7295b 100644 --- a/random.c +++ b/random.c @@ -1,12 +1,10 @@ /* random.c -- implements random.h */ #include "random.h" -/* random.c - * - * This file contains the command processing functions for a number of - * random commands. There is no functional grouping here, for sure. - * - * Modified by Petri Kutvonen +/* This file contains the command processing functions for a number of + random commands. There is no functional grouping here, for sure. + + Modified by Petri Kutvonen */ #include @@ -26,26 +24,26 @@ #include "window.h" -static const char *cname[] = { /* names of colors */ - "BLACK", "RED", "GREEN", "YELLOW", "BLUE", - "MAGENTA", "CYAN", "WHITE" -#if PKCODE & IBMPC - , "HIGH" +static const char *cname[] = { /* names of colors */ + "BLACK", "RED", "GREEN", "YELLOW", "BLUE", + "MAGENTA", "CYAN", "WHITE" +#if PKCODE & IBMPC + , "HIGH" #endif } ; -#define NCOLORS (sizeof cname / sizeof( *cname)) /* # of supported colors */ +#define NCOLORS (sizeof cname / sizeof( *cname)) /* # of supported colors */ -int gfcolor = NCOLORS - 1 ; /* global forgrnd color (white) */ -int gbcolor = 0 ; /* global backgrnd color (black) */ +int gfcolor = NCOLORS - 1 ; /* global forgrnd color (white) */ +int gbcolor = 0 ; /* global backgrnd color (black) */ -boolean hardtab = TRUE ; /* use hard tab instead of soft tab */ -int fillcol = 72 ; /* Current fill column */ +boolean hardtab = TRUE ; /* use hard tab instead of soft tab */ +int fillcol = 72 ; /* Current fill column */ /* uninitialized global definitions */ -int thisflag ; /* Flags, this command */ -int lastflag ; /* Flags, last command */ +int thisflag ; /* Flags, this command */ +int lastflag ; /* Flags, last command */ static int adjustmode( int kind, int global) ; @@ -54,9 +52,9 @@ static int cinsert( void) ; /* Set fill column to n. Bound to C-X F set-fill-column. */ BINDABLE( setfillcol) { - fillcol = n ; - mlwrite( "(Fill column is %d)", n) ; - return TRUE ; + fillcol = n ; + mlwrite( "(Fill column is %d)", n) ; + return TRUE ; } /* Display the current position of the cursor, in origin 1 X-Y coordinates, @@ -66,187 +64,188 @@ BINDABLE( setfillcol) { Normally this is bound to C-X = buffer-position. */ BINDABLE( showcpos) { - line_p lp ; /* current line */ - long numchars; /* # of chars in file */ - int numlines; /* # of lines in file */ - long predchars; /* # chars preceding point */ - int predlines; /* # lines preceding point */ - unicode_t curchar ; /* character under cursor */ - unsigned bytes ; /* length of unicode sequence */ - int ratio; - int col; - int savepos; /* temp save for current offset */ - int ecol; /* column pos/end of current line */ + line_p lp ; /* current line */ + long numchars; /* # of chars in file */ + int numlines; /* # of lines in file */ + long predchars; /* # chars preceding point */ + int predlines; /* # lines preceding point */ + unicode_t curchar ; /* character under cursor */ + unsigned bytes ; /* length of unicode sequence */ + int ratio; + int col; + int savepos; /* temp save for current offset */ + int ecol; /* column pos/end of current line */ - /* start counting chars and lines */ - numchars = 0; - numlines = 0; - predchars = 0; - predlines = 0; - bytes = lgetchar( &curchar) ; - for( lp = lforw( curbp->b_linep) ; lp != curbp->b_linep ; lp = lforw( lp)) { - /* if we are on the current line, record it */ - if (lp == curwp->w_dotp) { - predlines = numlines; - predchars = numchars + curwp->w_doto; - } - /* on to the next line */ - ++numlines; - numchars += llength( lp) + ((curbp->b_mode & MDDOS) ? 2 : 1) ; - } + /* start counting chars and lines */ + numchars = 0; + numlines = 0; + predchars = 0; + predlines = 0; + bytes = lgetchar( &curchar) ; + for( lp = lforw( curbp->b_linep) ; lp != curbp->b_linep ; lp = lforw( lp)) { + /* if we are on the current line, record it */ + if (lp == curwp->w_dotp) { + predlines = numlines; + predchars = numchars + curwp->w_doto; + } + /* on to the next line */ + ++numlines; + numchars += llength( lp) + ((curbp->b_mode & MDDOS) ? 2 : 1) ; + } - /* if at end of file, record it */ - if (curwp->w_dotp == curbp->b_linep) { - predlines = numlines; - predchars = numchars; - bytes = 0 ; - } + /* if at end of file, record it */ + if (curwp->w_dotp == curbp->b_linep) { + predlines = numlines; + predchars = numchars; + bytes = 0 ; + } - /* Get real column and end-of-line column. */ - col = getccol(FALSE); - savepos = curwp->w_doto; - curwp->w_doto = llength(curwp->w_dotp); - ecol = getccol(FALSE); - curwp->w_doto = savepos; + /* Get real column and end-of-line column. */ + col = getccol(FALSE); + savepos = curwp->w_doto; + curwp->w_doto = llength(curwp->w_dotp); + ecol = getccol(FALSE); + curwp->w_doto = savepos; - /* Ratio before dot. */ - ratio = (numchars == 0) ? 100 : (100L * predchars) / numchars ; + /* Ratio before dot. */ + ratio = (numchars == 0) ? 100 : (100L * predchars) / numchars ; - /* summarize and report the info */ - char fmtbuf[] = "Line %d/%d Col %d/%d Char %D/%D (%d%%) char = %s%x" ; - if( bytes == 0) - strcpy( &fmtbuf[ 39], "EOF") ; + /* summarize and report the info */ + char fmtbuf[] = "Line %d/%d Col %d/%d Char %D/%D (%d%%) char = %s%x" ; + if( bytes == 0) + strcpy( &fmtbuf[ 39], "EOF") ; - mlwrite( fmtbuf, predlines + 1, numlines + 1, col, ecol, predchars, - numchars, ratio, (bytes > 1) ? "\\u" : "0x", curchar) ; - return TRUE; + mlwrite( fmtbuf, predlines + 1, numlines + 1, col, ecol, predchars, + numchars, ratio, (bytes > 1) ? "\\u" : "0x", curchar) ; + return TRUE; } int getcline(void) -{ /* get the current line number */ - line_p lp ; /* current line */ - int numlines; /* # of lines before point */ +{ /* get the current line number */ + line_p lp ; /* current line */ + int numlines; /* # of lines before point */ - /* starting at the beginning of the buffer */ - lp = lforw(curbp->b_linep); + /* starting at the beginning of the buffer */ + lp = lforw(curbp->b_linep); - /* start counting lines */ - numlines = 0; - while (lp != curbp->b_linep) { - /* if we are on the current line, record it */ - if (lp == curwp->w_dotp) - break; - ++numlines; - lp = lforw(lp); - } + /* start counting lines */ + numlines = 0; + while (lp != curbp->b_linep) { + /* if we are on the current line, record it */ + if (lp == curwp->w_dotp) + break; + ++numlines; + lp = lforw(lp); + } - /* and return the resulting count */ - return numlines + 1; + /* and return the resulting count */ + return numlines + 1; } /* Return current column. Stop at first non-blank given TRUE argument. */ int getccol( int bflg) { - int i, col ; - line_p dlp = curwp->w_dotp ; - int byte_offset = curwp->w_doto ; - int len = llength( dlp) ; + int i, col ; + line_p dlp = curwp->w_dotp ; + int byte_offset = curwp->w_doto ; + int len = llength( dlp) ; - col = i = 0; - while (i < byte_offset) { - unicode_t c; + col = i = 0; + while (i < byte_offset) { + unicode_t c; - i += utf8_to_unicode(dlp->l_text, i, len, &c); - if( bflg && c != ' ' && c != '\t') /* Request Stop at first non-blank */ - break ; - if (c == '\t') - col += tabwidth - col % tabwidth ; - else if (c < 0x20 || c == 0x7F) /* displayed as ^c */ - col += 2 ; - else if (c >= 0x80 && c <= 0xa0) /* displayed as \xx */ - col += 3 ; - else { - int w = utf8_width( c) ; /* incomplete wc_width */ - col += (w < 0) ? 2 : w ; - } - } + i += utf8_to_unicode(dlp->l_text, i, len, &c); + if( bflg && c != ' ' && c != '\t') /* Request Stop at first non-blank */ + break ; + if (c == '\t') + col += tabwidth - col % tabwidth ; + else if (c < 0x20 || c == 0x7F) /* displayed as ^c */ + col += 2 ; + else if (c >= 0x80 && c <= 0xa0) /* displayed as \xx */ + col += 3 ; + else { + int w = utf8_width( c) ; /* incomplete wc_width */ + col += (w < 0) ? 2 : w ; + } + } - return col ; + return col ; } /* * Set current column. * - * int pos; position to set cursor + * int pos; position to set cursor */ boolean setccol( int pos) { - int i; /* index into current line */ - int col; /* current cursor column */ - int llen; /* length of line in bytes */ - char *text ; + int i; /* index into current line */ + int col; /* current cursor column */ + int llen; /* length of line in bytes */ + char *text ; - col = 0; - llen = llength(curwp->w_dotp); - text = curwp->w_dotp->l_text ; + col = 0; + llen = llength(curwp->w_dotp); + text = curwp->w_dotp->l_text ; - /* scan the line until we are at or past the target column */ - for( i = 0 ; i < llen && col < pos ; ) { - unicode_t c ; /* character being scanned */ + /* scan the line until we are at or past the target column */ + for( i = 0 ; i < llen && col < pos ; ) { + unicode_t c ; /* character being scanned */ - /* advance one character */ - i += utf8_to_unicode( text, i, llen, &c) ; - if (c == '\t') - col += tabwidth - col % tabwidth ; - else if (c < 0x20 || c == 0x7F) /* displayed as ^C */ - col += 2 ; - else if (c >= 0x80 && c <= 0xa0) /* displayed as \xx */ - col += 3 ; - else - col += 1 ; - } + /* advance one character */ + i += utf8_to_unicode( text, i, llen, &c) ; + if (c == '\t') + col += tabwidth - col % tabwidth ; + else if (c < 0x20 || c == 0x7F) /* displayed as ^C */ + col += 2 ; + else if (c >= 0x80 && c <= 0xa0) /* displayed as \xx */ + col += 3 ; + else + col += 1 ; + } - /* set us at the new position */ - curwp->w_doto = i; + /* set us at the new position */ + curwp->w_doto = i; - /* and tell whether we made it */ - return col >= pos; + /* and tell whether we made it */ + return col >= pos; } -/* - * 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 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. */ -boolean twiddle( int f, int n) { - unicode_t c ; - boolean eof_f = FALSE ; +BBINDABLE( twiddle) { + unicode_t c ; + boolean eof_f = FALSE ; - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - int len = llength( curwp->w_dotp) ; - if( len < 2 || curwp->w_doto == 0) /* at least 2 chars & not bol */ - return FALSE ; + int len = llength( curwp->w_dotp) ; + if( len < 2 || curwp->w_doto == 0) /* at least 2 chars & not bol */ + return FALSE ; - if( curwp->w_doto == len) { /* at end of line */ - backchar( FALSE, 1) ; - eof_f = TRUE ; - } + if( curwp->w_doto == len) { /* at end of line */ + backchar( FALSE, 1) ; + eof_f = TRUE ; + } - len = lgetchar( &c) ; /* len => unicode or extended ASCII */ - ldelchar( 1, FALSE) ; - backchar( FALSE, 1) ; - if( len == 1) - linsert_byte( 1, c) ; - else - linsert( 1, c) ; + len = lgetchar( &c) ; /* len => unicode or extended ASCII */ + ldelchar( 1, FALSE) ; + backchar( FALSE, 1) ; + if( len == 1) + linsert_byte( 1, c) ; + else + linsert( 1, c) ; - if( eof_f == TRUE) - forwchar( FALSE, 1) ; + if( eof_f == TRUE) + forwchar( FALSE, 1) ; - lchange( WFEDIT) ; - return TRUE ; + lchange( WFEDIT) ; + return TRUE ; } /* Quote the next character, and insert it into the buffer. All the @@ -256,194 +255,194 @@ boolean twiddle( int f, int n) { quote-character. */ BINDABLE( quote) { - int ret ; + int ret ; - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - int c = ectoc( get1key()) ; - if( n < 0) - ret = FALSE ; - else if( n == 0) - ret = TRUE ; - else if( c == '\n') - do - ret = lnewline() ; - while( ret == TRUE && --n) ; - else - ret = linsert( n, c) ; + int c = ectoc( get1key()) ; + if( n < 0) + ret = FALSE ; + else if( n == 0) + ret = TRUE ; + else if( c == '\n') + do + ret = lnewline() ; + while( ret == TRUE && --n) ; + else + ret = linsert( n, c) ; - return ret ; + return ret ; } /* Insert tab/blank/space up to nth next tabulation according to hard/soft tab current state and tab width. Bound to C-I handle-tab. */ BINDABLE( insert_tab) { - int status ; + int status ; - if( n < 0) - status = FALSE ; - else if( n == 0) - status = TRUE ; - else if( hardtab == TRUE) - status = linsert( n, '\t') ; - else /* softtab */ - do { - status = linsert( tabwidth - getccol( FALSE) % tabwidth, ' ') ; - } while( status != FALSE && --n) ; + if( n < 0) + status = FALSE ; + else if( n == 0) + status = TRUE ; + else if( hardtab == TRUE) + status = linsert( n, '\t') ; + else /* softtab */ + do { + status = linsert( tabwidth - getccol( FALSE) % tabwidth, ' ') ; + } while( status != FALSE && --n) ; - return status ; + return status ; } /* * change tabs to spaces * - * int f, n; default flag and numeric repeat count + * int f, n; default flag and numeric repeat count */ BINDABLE( detab) { - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if( f == FALSE) - n = 1 ; + if( f == FALSE) + n = 1 ; - /* loop thru detabbing n lines */ - int inc = (n > 0) ? 1 : -1 ; /* increment to next line [sgn(n)] */ - for( ; n ; n -= inc) { - curwp->w_doto = 0; /* start at the beginning */ + /* loop thru detabbing n lines */ + int inc = (n > 0) ? 1 : -1 ; /* increment to next line [sgn(n)] */ + for( ; n ; n -= inc) { + curwp->w_doto = 0; /* start at the beginning */ - /* detab the entire current line */ - while (curwp->w_doto < llength(curwp->w_dotp)) { - /* if we have a tab */ - if( curwbyte() == '\t') { - int size ; + /* detab the entire current line */ + while (curwp->w_doto < llength(curwp->w_dotp)) { + /* if we have a tab */ + if( curwbyte() == '\t') { + int size ; - ldelchar( 1, FALSE) ; - size = tabwidth - curwp->w_doto % tabwidth ; - insspace( TRUE, size) ; - forwchar( TRUE, size) ; - } else - forwchar( FALSE, 1) ; - } + ldelchar( 1, FALSE) ; + size = tabwidth - curwp->w_doto % tabwidth ; + insspace( TRUE, size) ; + forwchar( TRUE, size) ; + } else + forwchar( FALSE, 1) ; + } - /* advance/or back to the next line */ - if( forwline( TRUE, inc) == FALSE) - break ; - } + /* advance/or back to the next line */ + if( forwline( TRUE, inc) == FALSE) + break ; + } - curwp->w_doto = 0; /* to the begining of the line */ - thisflag &= ~CFCPCN; /* flag that this resets the goal column */ - lchange(WFEDIT); /* yes, we have made at least an edit */ - return (n == 0) ? TRUE : FALSE ; + curwp->w_doto = 0; /* to the begining of the line */ + thisflag &= ~CFCPCN; /* flag that this resets the goal column */ + lchange(WFEDIT); /* yes, we have made at least an edit */ + return (n == 0) ? TRUE : FALSE ; } /* * change spaces to tabs where possible * - * int f, n; default flag and numeric repeat count + * int f, n; default flag and numeric repeat count */ BINDABLE( entab) { -#define nextab(a) (a + tabwidth - a % tabwidth) +#define nextab(a) (a + tabwidth - a % tabwidth) - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if( f == FALSE) - n = 1 ; + if( f == FALSE) + n = 1 ; - /* loop thru entabbing n lines */ - int inc = (n > 0) ? 1 : -1 ; /* increment to next line [sgn(n)] */ - for( ; n ; n -= inc) { - int fspace ; /* pointer to first space if in a run */ - int ccol ; /* current cursor column */ + /* loop thru entabbing n lines */ + int inc = (n > 0) ? 1 : -1 ; /* increment to next line [sgn(n)] */ + for( ; n ; n -= inc) { + int fspace ; /* pointer to first space if in a run */ + int ccol ; /* current cursor column */ - curwp->w_doto = 0; /* start at the beginning */ + curwp->w_doto = 0; /* start at the beginning */ - /* entab the entire current line */ - fspace = -1; - ccol = 0; - while (curwp->w_doto < llength(curwp->w_dotp)) { - /* see if it is time to compress */ - if ((fspace >= 0) && (nextab(fspace) <= ccol)) { - if (ccol - fspace < 2) - fspace = -1; - else { - /* there is a bug here dealing with mixed space/tabed - lines.......it will get fixed */ - backchar(TRUE, ccol - fspace); - ldelete( (long) (ccol - fspace), FALSE) ; - linsert(1, '\t'); - fspace = -1; - } - } + /* entab the entire current line */ + fspace = -1; + ccol = 0; + while (curwp->w_doto < llength(curwp->w_dotp)) { + /* see if it is time to compress */ + if ((fspace >= 0) && (nextab(fspace) <= ccol)) { + if (ccol - fspace < 2) + fspace = -1; + else { + /* there is a bug here dealing with mixed space/tabed + lines.......it will get fixed */ + backchar(TRUE, ccol - fspace); + ldelete( (long) (ccol - fspace), FALSE) ; + linsert(1, '\t'); + fspace = -1; + } + } - /* get the current character */ - switch( curwbyte()) { - case '\t': /* a tab...count em up */ - ccol = nextab(ccol); - break; + /* get the current character */ + switch( curwbyte()) { + case '\t': /* a tab...count em up */ + ccol = nextab(ccol); + break; - case ' ': /* a space...compress? */ - if (fspace == -1) - fspace = ccol; - ccol++; - break; + case ' ': /* a space...compress? */ + if (fspace == -1) + fspace = ccol; + ccol++; + break; - default: /* any other char...just count */ - ccol++; - fspace = -1; - } + default: /* any other char...just count */ + ccol++; + fspace = -1; + } - forwchar(FALSE, 1); - } + forwchar(FALSE, 1); + } - /* advance/or back to the next line */ - if( forwline( TRUE, inc) == FALSE) - break ; - } + /* advance/or back to the next line */ + if( forwline( TRUE, inc) == FALSE) + break ; + } - curwp->w_doto = 0; /* to the begining of the line */ - thisflag &= ~CFCPCN; /* flag that this resets the goal column */ - lchange(WFEDIT); /* yes, we have made at least an edit */ - return (n == 0) ? TRUE : FALSE ; + curwp->w_doto = 0; /* to the begining of the line */ + thisflag &= ~CFCPCN; /* flag that this resets the goal column */ + lchange(WFEDIT); /* yes, we have made at least an edit */ + return (n == 0) ? TRUE : FALSE ; } /* * trim trailing whitespace from the point to eol * - * int f, n; default flag and numeric repeat count + * int f, n; default flag and numeric repeat count */ BINDABLE( trim) { - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if( f == FALSE) - n = 1 ; + if( f == FALSE) + n = 1 ; - /* loop thru trimming n lines */ - int inc = (n > 0) ? 1 : -1 ; /* increment to next line [sgn(n)] */ - for( ; n ; n -= inc) { - line_p lp ; /* current line pointer */ - int offset ; /* original line offset position */ - int length ; /* current length */ + /* loop thru trimming n lines */ + int inc = (n > 0) ? 1 : -1 ; /* increment to next line [sgn(n)] */ + for( ; n ; n -= inc) { + line_p lp ; /* current line pointer */ + int offset ; /* original line offset position */ + int length ; /* current length */ - lp = curwp->w_dotp; /* find current line text */ - offset = curwp->w_doto; /* save original offset */ + lp = curwp->w_dotp; /* find current line text */ + offset = curwp->w_doto; /* save original offset */ - /* trim the current line */ - for( length = lp->l_used ; length > offset ; length--) { - char c = lgetc( lp, length - 1) ; - if( c != ' ' && c != '\t') - break ; - } + /* trim the current line */ + for( length = lp->l_used ; length > offset ; length--) { + char c = lgetc( lp, length - 1) ; + if( c != ' ' && c != '\t') + break ; + } - lp->l_used = length; + lp->l_used = length; - /* advance/or back to the next line */ - if( forwline( TRUE, inc) == FALSE) - break ; - } + /* advance/or back to the next line */ + if( forwline( TRUE, inc) == FALSE) + break ; + } - lchange(WFEDIT); - thisflag &= ~CFCPCN; /* flag that this resets the goal column */ - return (n == 0) ? TRUE : FALSE ; + lchange(WFEDIT); + thisflag &= ~CFCPCN; /* flag that this resets the goal column */ + return (n == 0) ? TRUE : FALSE ; } /* @@ -452,16 +451,16 @@ BINDABLE( trim) { * procerssors. They even handle the looping. Normally this is bound to "C-O". */ BINDABLE( openline) { - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - int ret = (n < 0) ? FALSE : TRUE ; - for( int i = n ; ret == TRUE && i ; i--) /* Insert newlines. */ - ret = lnewline() ; + int ret = (n < 0) ? FALSE : TRUE ; + for( int i = n ; ret == TRUE && i ; i--) /* Insert newlines. */ + ret = lnewline() ; - if( ret == TRUE) /* Then back up overtop */ - ret = backchar( f, n) ; /* of them all. */ + if( ret == TRUE) /* Then back up overtop */ + ret = backchar( f, n) ; /* of them all. */ - return ret ; + return ret ; } /* @@ -469,95 +468,95 @@ BINDABLE( openline) { * indentation as specified. */ BINDABLE( insert_newline) { - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if( n < 0) - return FALSE ; + if( n < 0) + return FALSE ; - /* if we are in C mode and this is a default */ - if (n == 1 && (curbp->b_mode & MDCMOD) && - curwp->w_dotp != curbp->b_linep) - return cinsert(); + /* if we are in C mode and this is a default */ + if (n == 1 && (curbp->b_mode & MDCMOD) && + curwp->w_dotp != curbp->b_linep) + return cinsert(); - /* - * If a newline 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 ((curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 && - getccol(FALSE) > fillcol && - (curwp->w_bufp->b_mode & MDVIEW) == FALSE) - execute(META | SPEC | 'W', FALSE, 1); + /* + * If a newline 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 ((curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 && + getccol(FALSE) > fillcol && + (curwp->w_bufp->b_mode & MDVIEW) == FALSE) + execute(META | SPEC | 'W', FALSE, 1); - /* insert some lines */ - while (n--) { - int s ; + /* insert some lines */ + while (n--) { + int s ; - if ((s = lnewline()) != TRUE) - return s; + if ((s = lnewline()) != TRUE) + return s; #if SCROLLCODE - curwp->w_flag |= WFINS; + curwp->w_flag |= WFINS; #endif - } - return TRUE; + } + return TRUE; } static int cinsert(void) -{ /* insert a newline and indentation for C */ - char *cptr; /* string pointer into text to copy */ - int tptr; /* index to scan into line */ - int bracef; /* was there a brace at the end of line? */ - int i, nicol ; +{ /* insert a newline and indentation for C */ + char *cptr; /* string pointer into text to copy */ + int tptr; /* index to scan into line */ + int bracef; /* was there a brace at the end of line? */ + int i, nicol ; - /* grab a pointer to text to copy indentation from */ - cptr = &curwp->w_dotp->l_text[0]; + /* grab a pointer to text to copy indentation from */ + cptr = &curwp->w_dotp->l_text[0]; - /* check for a brace */ - tptr = curwp->w_doto ; - bracef = (tptr > 0) && (cptr[ tptr - 1] == '{') ; + /* check for a brace */ + tptr = curwp->w_doto ; + bracef = (tptr > 0) && (cptr[ tptr - 1] == '{') ; - /* save the indent of the previous line */ - nicol = 0 ; - for( i = 0 ; i < tptr ; i += 1) { - int ch ; + /* save the indent of the previous line */ + nicol = 0 ; + for( i = 0 ; i < tptr ; i += 1) { + int ch ; - ch = cptr[ i] ; - if( ch == ' ') - nicol += 1 ; - else if( ch == '\t') - nicol += tabwidth - nicol % tabwidth ; - else - break ; - } + ch = cptr[ i] ; + if( ch == ' ') + nicol += 1 ; + else if( ch == '\t') + nicol += tabwidth - nicol % tabwidth ; + else + break ; + } - if( i == tptr) { /* all line is blank */ - curwp->w_doto = 0 ; /* gotobol */ - lnewline() ; - curwp->w_doto = tptr ; /* gotoeol */ - } else { - /* put in the newline */ - if (lnewline() == FALSE) - return FALSE; + if( i == tptr) { /* all line is blank */ + curwp->w_doto = 0 ; /* gotobol */ + lnewline() ; + curwp->w_doto = tptr ; /* gotoeol */ + } else { + /* put in the newline */ + if (lnewline() == FALSE) + return FALSE; - /* and the saved indentation */ - i = nicol % tabwidth ; /* spaces */ - nicol /= tabwidth ; /* tabs */ - if( bracef) { - /* and one more tab for a brace */ - nicol += 1 ; - i = 0 ; - } + /* and the saved indentation */ + i = nicol % tabwidth ; /* spaces */ + nicol /= tabwidth ; /* tabs */ + if( bracef) { + /* and one more tab for a brace */ + nicol += 1 ; + i = 0 ; + } - if( nicol > 0) - insert_tab( FALSE, nicol) ; + if( nicol > 0) + insert_tab( FALSE, nicol) ; - if( i > 0) - linsert( i, ' ') ; - } + if( i > 0) + linsert( i, ' ') ; + } #if SCROLLCODE - curwp->w_flag |= WFINS; + curwp->w_flag |= WFINS; #endif - return TRUE; + return TRUE; } @@ -566,26 +565,26 @@ static int cinsert(void) * 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 - * delete-blank-lines. Any argument is ignored. + * delete-blank-lines. Any argument is ignored. */ BINDABLE( deblank) { - line_p lp1, lp2 ; - long nld ; + line_p lp1, lp2 ; + long nld ; - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - 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, FALSE); + 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, FALSE); } /* Insert a newline, then enough tabs and spaces to duplicate the @@ -597,36 +596,36 @@ BINDABLE( deblank) { * newline-and-indent. */ BINDABLE( indent) { - int i ; + int i ; - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if( n < 0) - return FALSE ; + if( n < 0) + return FALSE ; /* number of columns to indent */ - int nicol = 0 ; - for( i = 0 ; i < llength( curwp->w_dotp) ; i += 1) { - int c ; + int nicol = 0 ; + for( i = 0 ; i < llength( curwp->w_dotp) ; i += 1) { + int c ; - c = lgetc( curwp->w_dotp, i) ; - if( c == '\t') - nicol += tabwidth - nicol % tabwidth ; - else if( c == ' ') - nicol += 1 ; - else - break ; - } + c = lgetc( curwp->w_dotp, i) ; + if( c == '\t') + nicol += tabwidth - nicol % tabwidth ; + else if( c == ' ') + nicol += 1 ; + else + break ; + } - i = nicol / tabwidth ; /* # of tab to insert */ - nicol %= tabwidth ; /* # of space to insert */ - while( n--) - if( lnewline() == FALSE - || ( i != 0 && insert_tab( FALSE, i) == FALSE) - || ( nicol != 0 && linsert( nicol, ' ') == FALSE)) - return FALSE ; + i = nicol / tabwidth ; /* # of tab to insert */ + nicol %= tabwidth ; /* # of space to insert */ + while( n--) + if( lnewline() == FALSE + || ( i != 0 && insert_tab( FALSE, i) == FALSE) + || ( nicol != 0 && linsert( nicol, ' ') == FALSE)) + return FALSE ; - return TRUE ; + return TRUE ; } /* @@ -636,20 +635,20 @@ BINDABLE( indent) { * of text if typed with a big argument. Normally bound to "C-D". */ BINDABLE( forwdel) { - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if( n == 0) - return TRUE ; - else if( n < 0) - return backdel( f, -n) ; + if( n == 0) + return TRUE ; + else if( n < 0) + return backdel( f, -n) ; - if (f != FALSE) { /* Really a kill. */ - if ((lastflag & CFKILL) == 0) - kdelete(); - thisflag |= CFKILL; - } + if (f != FALSE) { /* Really a kill. */ + if ((lastflag & CFKILL) == 0) + kdelete(); + thisflag |= CFKILL; + } - return ldelchar( n, f != FALSE) ; + return ldelchar( n, f != FALSE) ; } /* @@ -659,20 +658,20 @@ BINDABLE( forwdel) { * both "RUBOUT" and "C-H". */ BINDABLE( backdel) { - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if( n == 0) - return TRUE ; - else if( n < 0) - return forwdel( f, -n) ; + if( n == 0) + return TRUE ; + else if( n < 0) + return forwdel( f, -n) ; - if (f != FALSE) { /* Really a kill. */ - if ((lastflag & CFKILL) == 0) - kdelete(); - thisflag |= CFKILL; - } + if (f != FALSE) { /* Really a kill. */ + if ((lastflag & CFKILL) == 0) + kdelete(); + thisflag |= CFKILL; + } - return backchar( f, n) && ldelchar( n, f != FALSE) ; + return backchar( f, n) && ldelchar( n, f != FALSE) ; } /* @@ -684,199 +683,198 @@ BINDABLE( backdel) { * that number of newlines. Normally bound to "C-K". */ BINDABLE( killtext) { - line_p nextp ; - long chunk; + line_p nextp ; + long chunk; - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if ((lastflag & CFKILL) == 0) /* Clear kill buffer if */ - kdelete(); /* last wasn't a kill. */ - thisflag |= CFKILL; - 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); + if ((lastflag & CFKILL) == 0) /* Clear kill buffer if */ + kdelete(); /* last wasn't a kill. */ + thisflag |= CFKILL; + 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); } /* prompt and set an editor mode * - * int f, n; default and argument + * int f, n; default and argument */ BINDABLE( setemode) { - return adjustmode( TRUE, FALSE) ; + return adjustmode( TRUE, FALSE) ; } /* prompt and delete an editor mode * - * int f, n; default and argument + * int f, n; default and argument */ BINDABLE( delmode) { - return adjustmode( FALSE, FALSE) ; + return adjustmode( FALSE, FALSE) ; } /* prompt and set a global editor mode * - * int f, n; default and argument + * int f, n; default and argument */ BINDABLE( setgmode) { - return adjustmode( TRUE, TRUE) ; + return adjustmode( TRUE, TRUE) ; } /* prompt and delete a global editor mode * - * int f, n; default and argument + * int f, n; default and argument */ BINDABLE( delgmode) { - return adjustmode( FALSE, TRUE) ; + return adjustmode( FALSE, TRUE) ; } -/* - * change the editor mode status + +/* change the editor mode status * - * int kind; true = set, false = delete - * int global; true = global flag, false = current buffer flag + * int kind; true = set, false = delete + * int global; true = global flag, false = current buffer flag */ static int adjustmode( int kind, int global) { - unsigned i ; /* loop index */ - int status; /* error return on input */ - char prompt[50]; /* string to prompt user with */ - char *cbuf ; /* buffer to recieve mode name into */ + unsigned i ; /* loop index */ + int status; /* error return on input */ + char prompt[50]; /* string to prompt user with */ + char *cbuf ; /* buffer to recieve mode name into */ - /* build the proper prompt string */ - if (global) - strcpy(prompt, "Global mode to "); - else - strcpy(prompt, "Mode to "); + /* build the proper prompt string */ + if (global) + strcpy(prompt, "Global mode to "); + else + strcpy(prompt, "Mode to "); - if (kind == TRUE) - strcat(prompt, "add: "); - else - strcat(prompt, "delete: "); + if (kind == TRUE) + strcat(prompt, "add: "); + else + strcat(prompt, "delete: "); - /* prompt the user and get an answer */ + /* prompt the user and get an answer */ - status = newmlarg( &cbuf, prompt, 0) ; - if (status != TRUE) - return status; + status = newmlarg( &cbuf, prompt, 0) ; + if (status != TRUE) + return status; - /* test it first against the colors we know */ - for (i = 0; i < NCOLORS; i++) { - if( strcasecmp( cbuf, cname[ i]) == 0) { - /* finding the match, we set the color */ -#if COLOR - if( *cbuf >= 'A' && *cbuf <= 'Z') { - if (global) - gfcolor = i; -#if PKCODE == 0 - else + /* test it first against the colors we know */ + for (i = 0; i < NCOLORS; i++) { + if( strcasecmp( cbuf, cname[ i]) == 0) { + /* finding the match, we set the color */ +#if COLOR + if( *cbuf >= 'A' && *cbuf <= 'Z') { + if (global) + gfcolor = i; +#if PKCODE == 0 + else #endif - curwp->w_fcolor = i; - } else { - if (global) - gbcolor = i; -#if PKCODE == 0 - else + curwp->w_fcolor = i; + } else { + if (global) + gbcolor = i; +#if PKCODE == 0 + else #endif - curwp->w_bcolor = i; - } + curwp->w_bcolor = i; + } - curwp->w_flag |= WFCOLR; + curwp->w_flag |= WFCOLR; #endif - mlerase(); - free( cbuf) ; - return TRUE; - } - } + mlerase(); + free( cbuf) ; + return TRUE; + } + } - /* test it against the modes we know */ + /* test it against the modes we know */ - for (i = 0; i < NUMMODES; i++) { - if( strcasecmp( cbuf, modename[ i]) == 0) { - /* finding a match, we process it */ - if (kind == TRUE) - if (global) - gmode |= (1 << i); - else - curbp->b_mode |= (1 << i); - else if (global) - gmode &= ~(1 << i); - else - curbp->b_mode &= ~(1 << i); - /* display new mode line */ - if (global == 0) - upmode(); - mlerase(); /* erase the junk */ - free( cbuf) ; - return TRUE; - } - } + for (i = 0; i < NUMMODES; i++) { + if( strcasecmp( cbuf, modename[ i]) == 0) { + /* finding a match, we process it */ + if (kind == TRUE) + if (global) + gmode |= (1 << i); + else + curbp->b_mode |= (1 << i); + else if (global) + gmode &= ~(1 << i); + else + curbp->b_mode &= ~(1 << i); + /* display new mode line */ + if (global == 0) + upmode(); + mlerase(); /* erase the junk */ + free( cbuf) ; + return TRUE; + } + } - mlwrite("No such mode!"); - free( cbuf) ; - return FALSE; + mlwrite("No such mode!"); + free( cbuf) ; + return FALSE; } static int iovstring( int f, int n, const char *prompt, int (*fun)( char *)) { - char *tstring ; /* string to add */ + char *tstring ; /* string to add */ /* ask for string to insert */ - int status = newmlargt( &tstring, prompt, 0) ; - if( tstring == NULL) - return status ; + int status = newmlargt( &tstring, prompt, 0) ; + if( tstring == NULL) + return status ; - if( f == FALSE) - n = 1 ; - else if( n < 0) - n = -n ; + if( f == FALSE) + n = 1 ; + else if( n < 0) + n = -n ; - /* insert it */ - while( n-- && status == TRUE) - status = fun( tstring) ; + /* insert it */ + while( n-- && status == TRUE) + status = fun( tstring) ; - free( tstring) ; - return status ; + free( tstring) ; + return status ; } -/* - * ask for and insert a string into the current - * buffer at the current point - * - * int f, n; ignored arguments + +/* ask for and insert a string into the current buffer at the current point + + int f, n; ignored arguments */ BINDABLE( istring) { - return iovstring( f, n, "insert-string: ", linstr) ; + return iovstring( f, n, "insert-string: ", linstr) ; } -/* - * ask for and overwite a string into the current - * buffer at the current point - * - * int f, n; ignored arguments + +/* ask for and overwite a string into the current buffer at the current + point + + int f, n; ignored arguments */ BINDABLE( ovstring) { - return iovstring( f, n, "overwrite-string: ", lover) ; + return iovstring( f, n, "overwrite-string: ", lover) ; } /* end of random.c */ diff --git a/random.h b/random.h index 6f7e657..1668f1f 100644 --- a/random.h +++ b/random.h @@ -1,19 +1,18 @@ /* random.h -- various commands */ - #ifndef _RANDOM_H_ #define _RANDOM_H_ #include "names.h" /* Command flags */ -#define CFCPCN 0x0001 /* Flag that last command was C-P, C-N */ -#define CFKILL 0x0002 /* Flag that last command was a kill */ +#define CFCPCN 0x0001 /* Flag that last command was C-P, C-N */ +#define CFKILL 0x0002 /* Flag that last command was a kill */ -extern int thisflag ; /* Flags, this command */ -extern int lastflag ; /* Flags, last command */ +extern int thisflag ; /* Flags, this command */ +extern int lastflag ; /* Flags, last command */ -extern int fillcol ; /* Fill column */ -extern boolean hardtab ; /* Use hard tab instead of soft tab */ +extern int fillcol ; /* Fill column */ +extern boolean hardtab ; /* Use hard tab instead of soft tab */ int getcline( void) ; @@ -21,28 +20,27 @@ int getccol( int bflg) ; boolean setccol( int pos) ; /* Bindable functions */ -BINDABLE( setfillcol) ; -BINDABLE( showcpos) ; -boolean twiddle( int f, int n) ; -BINDABLE( quote) ; -BINDABLE( insert_tab) ; -BINDABLE( detab) ; -BINDABLE( entab) ; -BINDABLE( trim) ; -BINDABLE( openline) ; -BINDABLE( insert_newline) ; -BINDABLE( deblank) ; -BINDABLE( indent) ; -BINDABLE( forwdel) ; -BINDABLE( backdel) ; -BINDABLE( killtext) ; -BINDABLE( setemode) ; -BINDABLE( delmode) ; -BINDABLE( setgmode) ; -BINDABLE( delgmode) ; -BINDABLE( istring) ; -BINDABLE( ovstring) ; + BINDABLE( setfillcol) ; + BINDABLE( showcpos) ; +BBINDABLE( twiddle) ; + BINDABLE( quote) ; + BINDABLE( insert_tab) ; + BINDABLE( detab) ; + BINDABLE( entab) ; + BINDABLE( trim) ; + BINDABLE( openline) ; + BINDABLE( insert_newline) ; + BINDABLE( deblank) ; + BINDABLE( indent) ; + BINDABLE( forwdel) ; + BINDABLE( backdel) ; + BINDABLE( killtext) ; + BINDABLE( setemode) ; + BINDABLE( delmode) ; + BINDABLE( setgmode) ; + BINDABLE( delgmode) ; + BINDABLE( istring) ; + BINDABLE( ovstring) ; #endif - /* end of random.h */ diff --git a/region.h b/region.h index 991d1a0..ee69545 100644 --- a/region.h +++ b/region.h @@ -4,14 +4,13 @@ #include "line.h" -/* - * The starting position of a region, and the size of the region in - * characters, is kept in a region structure. Used by the region commands. +/* The starting position of a region, and the size of the region in + characters, is kept in a region structure. Used by the region commands. */ typedef struct { - line_p r_linep ; /* Origin struct line address. */ - int r_offset ; /* Origin struct line offset. */ - long r_size ; /* Length in characters. */ + line_p r_linep ; /* Origin struct line address. */ + int r_offset ; /* Origin struct line offset. */ + long r_size ; /* Length in characters. */ } region_t ; typedef region_t *region_p ; @@ -25,5 +24,4 @@ BINDABLE( upperregion) ; int getregion( region_p rp) ; #endif - /* end of region.h */ diff --git a/retcode.h b/retcode.h index 8a50cd6..00fe09f 100644 --- a/retcode.h +++ b/retcode.h @@ -1,20 +1,23 @@ +/* retcode.h -- */ #ifndef __RETCODE_H__ #define __RETCODE_H__ -#ifdef FALSE -#error "FALSE shouldn't be defined" -#undef FALSE +#ifdef FALSE +# error "FALSE shouldn't be defined" +# undef FALSE #endif -#ifdef TRUE -#error "TRUE shouldn't be defined" -#undef TRUE + +#ifdef TRUE +# error "TRUE shouldn't be defined" +# undef TRUE #endif typedef enum { - FALSE, /* 0, false, no, bad, etc. */ - TRUE /* 1, true, yes, good, etc. */ + FALSE, /* 0, false, no, bad, etc. */ + TRUE /* 1, true, yes, good, etc. */ } boolean ; -#define ABORT 2 /* 2, death, ^G, abort, etc. */ +#define ABORT 2 /* 2, death, ^G, abort, etc. */ #endif +/* end of retcode.h */ diff --git a/search.h b/search.h index 9b3d596..a2423ce 100644 --- a/search.h +++ b/search.h @@ -1,40 +1,38 @@ /* search.h -- */ - #ifndef _SEARCH_H_ #define _SEARCH_H_ -#define MAGIC 1 /* include regular expression matching? */ +#include "line.h" /* line_p */ +#include "names.h" /* BINDABLE() */ -#include "line.h" - -typedef char spat_t[ 128] ; /* search pattern type */ -#define NPAT sizeof( spat_t) /* # of bytes, pattern */ +typedef char spat_t[ 128] ; /* search pattern type */ +#define NPAT sizeof( spat_t) /* # of bytes, pattern */ extern unsigned int matchlen ; extern char *patmatch ; -extern spat_t pat ; /* Search pattern */ -extern spat_t tap ; /* Reversed pattern array. */ -extern spat_t rpat ; /* replacement pattern */ +extern spat_t pat ; /* Search pattern */ +extern spat_t tap ; /* Reversed pattern array. */ +extern spat_t rpat ; /* replacement pattern */ /* * PTBEG, PTEND, FORWARD, and REVERSE are all toggle-able values for * the scan routines. */ -#define PTBEG 0 /* Leave the point at the beginning on search */ -#define PTEND 1 /* Leave the point at the end on search */ -#define FORWARD 0 /* forward direction */ -#define REVERSE 1 /* backwards direction */ +#define PTBEG 0 /* Leave the point at the beginning on search */ +#define PTEND 1 /* Leave the point at the end on search */ +#define FORWARD 0 /* forward direction */ +#define REVERSE 1 /* backwards direction */ int scanner( const char *patrn, int direct, int beg_or_end) ; /* Bindable functions */ -BINDABLE( forwsearch) ; -BINDABLE( forwhunt) ; -BINDABLE( backsearch) ; BINDABLE( backhunt) ; -BINDABLE( sreplace) ; +BINDABLE( backsearch) ; +BINDABLE( forwhunt) ; +BINDABLE( forwsearch) ; BINDABLE( qreplace) ; +BINDABLE( sreplace) ; int eq( unsigned char bc, unsigned char pc) ; void savematch( void) ; @@ -45,11 +43,11 @@ int boundary( line_p curline, int curoff, int dir) ; void setprompt( char *tpat, unsigned tpat_size, char *prompt, char *apat) ; +#define MAGIC 1 /* include regular expression matching? */ #if MAGIC void mcclear( void) ; void rmcclear( void) ; #endif #endif - /* end of search.h */ diff --git a/spawn.h b/spawn.h index cc219f9..a311a71 100644 --- a/spawn.h +++ b/spawn.h @@ -1,6 +1,8 @@ /* spawn.h -- various operating system access commands */ +#ifndef _SPAWN_H_ +#define _SPAWN_H_ -#include "names.h" +#include "names.h" /* BINDABLE() */ /* Bindable functions */ BINDABLE( spawncli) ; @@ -12,4 +14,5 @@ BINDABLE( filter_buffer) ; void rtfrmshell( void) ; +#endif /* end of spawn.h */ diff --git a/terminal.h b/terminal.h index a25218d..3f7d375 100644 --- a/terminal.h +++ b/terminal.h @@ -1,14 +1,12 @@ +/* terminal.h -- */ #ifndef __TERMINAL_H__ #define __TERMINAL_H__ - -#include "defines.h" /* COLOR, SCROLLCODE */ +#include "defines.h" /* COLOR, SCROLLCODE */ #include "retcode.h" #include "utf8.h" - -/* - * The editor communicates with the display using a high level interface. A +/* The editor communicates with the display using a high level interface. A * "TERM" structure holds useful variables, and indirect pointers to routines * that do useful operations. The low level get and put routines are here too. * This lets a terminal, in addition to having non standard commands, have @@ -17,69 +15,71 @@ * one terminal type. */ struct terminal { - const short t_maxrow ; /* max number of rows allowable */ - const short t_maxcol ; /* max number of columns allowable */ - short t_mrow ; /* max number of rows displayable */ - short t_nrow ; /* current number of rows displayed */ - short t_mcol ; /* max number of rows displayable */ - short t_ncol ; /* current number of columns displayed */ - short t_margin; /* min margin for extended lines */ - short t_scrsiz; /* size of scroll region " */ - int t_pause; /* # times thru update to pause */ - void (*t_open)(void); /* Open terminal at the start. */ - void (*t_close)(void); /* Close terminal at end. */ - void (*t_kopen)(void); /* Open keyboard */ - void (*t_kclose)(void); /* close keyboard */ - int (*t_getchar)(void); /* Get character from keyboard. */ - int (*t_putchar)( unicode_t) ; /* Put character to display. */ - void (*t_flush) (void); /* Flush output buffers. */ - void (*t_move)(int, int);/* Move the cursor, origin 0. */ - void (*t_eeol)(void); /* Erase to end of line. */ - void (*t_eeop)(void); /* Erase to end of page. */ - void (*t_beep)(void); /* Beep. */ - void (*t_rev)(int); /* set reverse video state */ - int (*t_rez)(char *); /* change screen resolution */ -#if COLOR - int (*t_setfor) (); /* set forground color */ - int (*t_setback) (); /* set background color */ + const short t_maxrow ; /* max number of rows allowable */ + const short t_maxcol ; /* max number of columns allowable */ + short t_mrow ; /* max number of rows displayable */ + short t_nrow ; /* current number of rows displayed */ + short t_mcol ; /* max number of rows displayable */ + short t_ncol ; /* current number of columns displayed */ + short t_margin; /* min margin for extended lines */ + short t_scrsiz; /* size of scroll region " */ + int t_pause; /* # times thru update to pause */ + void (*t_open)(void); /* Open terminal at the start. */ + void (*t_close)(void); /* Close terminal at end. */ + void (*t_kopen)(void); /* Open keyboard */ + void (*t_kclose)(void); /* close keyboard */ + int (*t_getchar)(void); /* Get character from keyboard. */ + int (*t_putchar)( unicode_t) ; /* Put character to display. */ + void (*t_flush) (void); /* Flush output buffers. */ + void (*t_move)(int, int);/* Move the cursor, origin 0. */ + void (*t_eeol)(void); /* Erase to end of line. */ + void (*t_eeop)(void); /* Erase to end of page. */ + void (*t_beep)(void); /* Beep. */ + void (*t_rev)(int); /* set reverse video state */ + int (*t_rez)(char *); /* change screen resolution */ +#if COLOR + int (*t_setfor) (); /* set forground color */ + int (*t_setback) (); /* set background color */ #endif #if SCROLLCODE - void (*t_scroll)(int, int,int); /* scroll a region of the screen */ + void (*t_scroll)(int, int,int); /* scroll a region of the screen */ #endif }; -/* TEMPORARY macros for terminal I/O (to be placed in a machine - dependant place later) */ +/* TEMPORARY macros for terminal I/O (to be placed in a machine dependant + place later) + */ -#define TTopen (*term.t_open) -#define TTclose (*term.t_close) -#define TTkopen (*term.t_kopen) -#define TTkclose (*term.t_kclose) -#define TTgetc (*term.t_getchar) -#define TTputc (*term.t_putchar) -#define TTflush (*term.t_flush) -#define TTmove (*term.t_move) -#define TTeeol (*term.t_eeol) -#define TTeeop (*term.t_eeop) -#define TTbeep (*term.t_beep) -#define TTrev (*term.t_rev) -#define TTrez (*term.t_rez) -#if COLOR -#define TTforg (*term.t_setfor) -#define TTbacg (*term.t_setback) +#define TTopen (*term.t_open) +#define TTclose (*term.t_close) +#define TTkopen (*term.t_kopen) +#define TTkclose (*term.t_kclose) +#define TTgetc (*term.t_getchar) +#define TTputc (*term.t_putchar) +#define TTflush (*term.t_flush) +#define TTmove (*term.t_move) +#define TTeeol (*term.t_eeol) +#define TTeeop (*term.t_eeop) +#define TTbeep (*term.t_beep) +#define TTrev (*term.t_rev) +#define TTrez (*term.t_rez) +#if COLOR +#define TTforg (*term.t_setfor) +#define TTbacg (*term.t_setback) #endif /* Terminal table defined only in term.c */ extern struct terminal term ; -extern int ttrow ; /* Row location of HW cursor */ -extern int ttcol ; /* Column location of HW cursor */ +extern int ttrow ; /* Row location of HW cursor */ +extern int ttcol ; /* Column location of HW cursor */ -extern boolean eolexist ; /* does clear to EOL exist? */ -extern boolean revexist ; /* does reverse video exist? */ -extern boolean sgarbf ; /* State of screen unknown */ +extern boolean eolexist ; /* does clear to EOL exist? */ +extern boolean revexist ; /* does reverse video exist? */ +extern boolean sgarbf ; /* State of screen unknown */ -extern char sres[] ; /* Current screen resolution. */ - /* NORMAL, CGA, EGA, VGA */ +extern char sres[] ; /* Current screen resolution. */ + /* NORMAL, CGA, EGA, VGA */ #endif +/* end of terminal.h */ diff --git a/termio.h b/termio.h index 5f96f25..b2603c2 100644 --- a/termio.h +++ b/termio.h @@ -1,14 +1,15 @@ +/* termio.h -- */ #ifndef _TERMIO_H_ #define _TERMIO_H_ #include "utf8.h" -#define TYPEAH 1 /* type ahead causes update to be skipped */ +#define TYPEAH 1 /* type ahead causes update to be skipped */ -#define HUGE 1000 /* Huge number (for row/col) */ +#define HUGE 1000 /* Huge number (for row/col) */ -extern int ttrow ; /* Row location of HW cursor */ -extern int ttcol ; /* Column location of HW cursor */ +extern int ttrow ; /* Row location of HW cursor */ +extern int ttcol ; /* Column location of HW cursor */ void ttopen( void) ; void ttclose( void) ; @@ -18,3 +19,4 @@ int ttgetc( void) ; int typahead( void) ; #endif +/* end of termio.h */ diff --git a/utf8.h b/utf8.h index b3b9505..93a95fc 100644 --- a/utf8.h +++ b/utf8.h @@ -1,5 +1,6 @@ -#ifndef UTF8_H -#define UTF8_H +/* utf8.h -- */ +#ifndef _UTF8_H_ +#define _UTF8_H_ typedef unsigned int unicode_t ; @@ -10,3 +11,4 @@ unsigned utf8_revdelta( unsigned char *buf, unsigned pos) ; unsigned unicode_to_utf8( unicode_t c, char *utf8) ; #endif +/* end of utf8.h */ diff --git a/util.h b/util.h index d599559..e7190f1 100644 --- a/util.h +++ b/util.h @@ -1,9 +1,10 @@ /* util.h -- utility functions */ -#ifndef UTIL_H_ -# define UTIL_H_ +#ifndef _UTIL_H_ +#define _UTIL_H_ #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + void mystrscpy( char *dst, const char *src, int size) ; -#endif /* UTIL_H_ */ +#endif /* end of util.h */ diff --git a/version.h b/version.h index b2ab58d..174bbd2 100644 --- a/version.h +++ b/version.h @@ -1,16 +1,18 @@ -#ifndef VERSION_H_ -#define VERSION_H_ +/* version.h -- name and version strings */ +#ifndef _VERSION_H_ +#define _VERSION_H_ #ifdef PROGRAM # define _QUOTE( s) #s # define QUOTE( s) _QUOTE( s) # define PROGRAM_NAME QUOTE(PROGRAM) #else -# define PROGRAM_NAME "em" +# define PROGRAM_NAME "ue" #endif #define PROGRAM_NAME_UTF8 "µEMACS" -#define VERSION "4.2.4" +#define VERSION "4.2.5" -#endif /* VERSION_H_ */ +#endif +/* end of version.h */ diff --git a/window.h b/window.h index e555ed3..a139562 100644 --- a/window.h +++ b/window.h @@ -2,9 +2,9 @@ #ifndef _WINDOW_H_ #define _WINDOW_H_ -#include "defines.h" /* COLOR, SCROLLCODE */ -#include "buffer.h" /* buffer_p, line_p */ -#include "names.h" /* BINDABLE() */ +#include "defines.h" /* COLOR, SCROLLCODE */ +#include "buffer.h" /* buffer_p, line_p */ +#include "names.h" /* BINDABLE() */ /* There is a window structure allocated for every active display window. The windows are kept in a big list, in top to bottom screen order, with @@ -15,40 +15,40 @@ every input character. */ typedef struct window { - struct window *w_wndp; /* Next window */ - buffer_p w_bufp ; /* Buffer displayed in window */ - line_p w_linep ; /* Top line in the window */ - line_p w_dotp ; /* Line containing "." */ - line_p w_markp ; /* Line containing "mark" */ - int w_doto ; /* Byte offset for "." */ - int w_marko ; /* Byte offset for "mark" */ - int w_toprow ; /* Origin 0 top row of window */ - int w_ntrows ; /* # of rows of text in window */ - char w_force ; /* If NZ, forcing row. */ - char w_flag ; /* Flags. */ -# if COLOR - char w_fcolor ; /* current forground color */ - char w_bcolor ; /* current background color */ -# endif + struct window *w_wndp; /* Next window */ + buffer_p w_bufp ; /* Buffer displayed in window */ + line_p w_linep ; /* Top line in the window */ + line_p w_dotp ; /* Line containing "." */ + line_p w_markp ; /* Line containing "mark" */ + int w_doto ; /* Byte offset for "." */ + int w_marko ; /* Byte offset for "mark" */ + int w_toprow ; /* Origin 0 top row of window */ + int w_ntrows ; /* # of rows of text in window */ + char w_force ; /* If NZ, forcing row. */ + char w_flag ; /* Flags. */ +#if COLOR + char w_fcolor ; /* current forground color */ + char w_bcolor ; /* current background color */ +#endif } *window_p ; -extern window_p curwp ; /* Current window */ -extern window_p wheadp ; /* Head of list of windows */ +extern window_p curwp ; /* Current window */ +extern window_p wheadp ; /* Head of list of windows */ /* curwbyte return the byte after the dot in current window */ #define curwbyte() lgetc( curwp->w_dotp, curwp->w_doto) -#define WFFORCE 0x01 /* Window needs forced reframe */ -#define WFMOVE 0x02 /* Movement from line to line */ -#define WFEDIT 0x04 /* Editing within a line */ -#define WFHARD 0x08 /* Better to a full display */ -#define WFMODE 0x10 /* Update mode line. */ -#define WFCOLR 0x20 /* Needs a color change */ +#define WFFORCE 0x01 /* Window needs forced reframe */ +#define WFMOVE 0x02 /* Movement from line to line */ +#define WFEDIT 0x04 /* Editing within a line */ +#define WFHARD 0x08 /* Better to a full display */ +#define WFMODE 0x10 /* Update mode line. */ +#define WFCOLR 0x20 /* Needs a color change */ -# if SCROLLCODE -# define WFKILLS 0x40 /* something was deleted */ -# define WFINS 0x80 /* something was inserted */ -# endif +#if SCROLLCODE +# define WFKILLS 0x40 /* something was deleted */ +# define WFINS 0x80 /* something was inserted */ +#endif /* Bindable functions */ BINDABLE( delwind) ; diff --git a/word.h b/word.h index abdb507..e4dde1f 100644 --- a/word.h +++ b/word.h @@ -21,5 +21,4 @@ BINDABLE( killpara) ; BINDABLE( wordcount) ; #endif - /* end of word.h */ diff --git a/wrapper.h b/wrapper.h index 5c609e5..0f556b2 100644 --- a/wrapper.h +++ b/wrapper.h @@ -1,10 +1,12 @@ +/* wrapper.h -- */ #ifndef WRAPPER_H_ #define WRAPPER_H_ -#include +#include /* size_t */ void xmkstemp( char *fname_template) ; void *xmalloc( size_t size) ; -#endif /* WRAPPER_H_ */ +#endif +/* end of wrapper.h */ From 3bce7a4751bc20dd267a7dcbd7210bc40d0f9c08 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 14 Aug 2021 11:45:41 +0800 Subject: [PATCH 34/37] Rework file primitives. --- file.c | 829 +++++++++++++++++++++++++++---------------------------- fileio.c | 214 +++++++------- fileio.h | 7 +- flook.c | 214 +++++++------- 4 files changed, 610 insertions(+), 654 deletions(-) diff --git a/file.c b/file.c index 68e21e5..40ad584 100644 --- a/file.c +++ b/file.c @@ -29,488 +29,476 @@ #include "window.h" typedef enum { - EOL_NONE, - EOL_UNIX, - EOL_DOS, - EOL_MAC, - EOL_MIXED + EOL_NONE, + EOL_UNIX, + EOL_DOS, + EOL_MAC, + EOL_MIXED } eoltype ; static const char *eolname[] = { - "NONE", - "UNIX", - "DOS", - "MAC", - "MIXED" + "NONE", + "UNIX", + "DOS", + "MAC", + "MIXED" } ; static const char *codename[] = { - "ASCII", - "UTF-8", - "EXTENDED", - "MIXED" + "ASCII", + "UTF-8", + "EXTENDED", + "MIXED" } ; -boolean restflag = FALSE ; /* restricted use? */ +boolean restflag = FALSE ; /* restricted use? */ static int ifile( const char *fname) ; boolean resterr( void) { - mloutfmt( "%B(That command is RESTRICTED)") ; - return FALSE ; + mloutfmt( "%B(That command is RESTRICTED)") ; + return FALSE ; } + /* Read a file into the current buffer. This is really easy; all you do is * find the name of the file, and call the standard "read a file into the * current buffer" code. Bound to C-X C-R read-file. */ BINDABLE( fileread) { - char *fname ; + char *fname ; - if( restflag) /* don't allow this command if restricted */ - return resterr() ; + if( restflag) /* don't allow this command if restricted */ + return resterr() ; - int status = newmlarg( &fname, "read-file: ", sizeof( fname_t)) ; - if( status == TRUE) { - status = readin( fname, TRUE) ; - free( fname) ; - } + int status = newmlarg( &fname, "read-file: ", sizeof( fname_t)) ; + if( status == TRUE) { + status = readin( fname, TRUE) ; + free( fname) ; + } - return status ; + return status ; } + /* Insert a file into the current buffer. This is really easy; all you do * is find the name of the file, and call the standard "insert a file into * the current buffer" code. Bound to C-X C-I insert-file. */ BINDABLE( insfile) { - char *fname ; + char *fname ; - if( restflag) /* don't allow this command if restricted */ - return resterr() ; + if( restflag) /* don't allow this command if restricted */ + return resterr() ; - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - int status = newmlarg( &fname, "insert-file: ", sizeof( fname_t)) ; - if( status == TRUE) { - status = ifile( fname) ; - free( fname) ; - } + int status = newmlarg( &fname, "insert-file: ", sizeof( fname_t)) ; + if( status == TRUE) { + status = ifile( fname) ; + free( fname) ; + } - if( status != TRUE) - return status ; + if( status != TRUE) + return status ; - return reposition( TRUE, -1) ; /* Redraw with dot at bottom of window */ + return reposition( TRUE, -1) ; /* Redraw with dot at bottom of window */ } + /* Select a file for editing. Look around to see if you can find the file * in another buffer; if you can find it just switch to the buffer. If you * cannot find the file, create a new buffer, read in the text, and switch * to the new buffer. Bound to C-X C-F find-file. */ BINDABLE( filefind) { - char *fname ; /* file user wishes to find */ + char *fname ; /* file user wishes to find */ - if( restflag) /* don't allow this command if restricted */ - return resterr() ; + if( restflag) /* don't allow this command if restricted */ + return resterr() ; - int status = newmlarg( &fname, "find-file: ", sizeof( fname_t)) ; - if( status == TRUE) { - status = getfile( fname, TRUE) ; - free( fname) ; - } + int status = newmlarg( &fname, "find-file: ", sizeof( fname_t)) ; + if( status == TRUE) { + status = getfile( fname, TRUE) ; + free( fname) ; + } - return status ; + return status ; } + +/* Update mode lines of all windows displaying current buffer */ static void upd_mode( void) { - struct window *wp ; - -/* Update mode lines */ - for( wp = wheadp ; wp != NULL ; wp = wp->w_wndp) - if( wp->w_bufp == curbp) - wp->w_flag |= WFMODE ; + for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp) + if( wp->w_bufp == curbp) + wp->w_flag |= WFMODE ; } -BINDABLE( viewfile) { /* visit a file in VIEW mode */ - char *fname ; /* file user wishes to find */ - if( restflag) /* don't allow this command if restricted */ - return resterr() ; +BINDABLE( viewfile) { /* visit a file in VIEW mode */ + char *fname ; /* file user wishes to find */ - int status = newmlarg( &fname, "view-file: ", sizeof( fname_t)) ; - if( status == TRUE) { - status = getfile( fname, FALSE) ; - free( fname) ; + if( restflag) /* don't allow this command if restricted */ + return resterr() ; - if( status == TRUE) { /* if we succeed, put it in view mode */ - curwp->w_bufp->b_mode |= MDVIEW ; - upd_mode() ; - } - } + int status = newmlarg( &fname, "view-file: ", sizeof( fname_t)) ; + if( status == TRUE) { + status = getfile( fname, FALSE) ; + free( fname) ; - return status ; + if( status == TRUE) { /* if we succeed, put it in view mode */ + curwp->w_bufp->b_mode |= MDVIEW ; + upd_mode() ; + } + } + + return status ; } -/* - * getfile() + +/* getfile() * - * char fname[]; file name to find - * boolean lockfl; check the file for locks? + * char fname[]; file name to find + * boolean lockfl; check the file for locks? */ int getfile( const char *fname, boolean lockfl) { - buffer_p bp; - int s; - bname_t bname ; /* buffer name to put file */ + buffer_p bp; + int s; + bname_t bname ; /* buffer name to put file */ /* Is the file in current buffer? */ - if( strcmp( curbp->b_fname, fname) == 0) { - mloutstr( "(Current buffer)") ; - return TRUE ; - } + if( strcmp( curbp->b_fname, fname) == 0) { + mloutstr( "(Current buffer)") ; + return TRUE ; + } /* Is the file in any buffer? */ - for( bp = bheadp; bp != NULL; bp = bp->b_bufp) { - if( (bp->b_flag & BFINVS) == 0 - && strcmp( bp->b_fname, fname) == 0) { - line_p lp ; - int i ; + for( bp = bheadp; bp != NULL; bp = bp->b_bufp) { + if( (bp->b_flag & BFINVS) == 0 + && strcmp( bp->b_fname, fname) == 0) { + line_p lp ; + int i ; - swbuffer( bp) ; + swbuffer( bp) ; - /* Center dotted line in window */ - i = curwp->w_ntrows / 2 ; - for( lp = curwp->w_dotp ; lback( lp) != curbp->b_linep ; lp = lback( lp)) - if( i-- == 0) - break ; + /* Center dotted line in window */ + i = curwp->w_ntrows / 2 ; + for( lp = curwp->w_dotp ; lback( lp) != curbp->b_linep ; lp = lback( lp)) + if( i-- == 0) + break ; - curwp->w_linep = lp ; + curwp->w_linep = lp ; - /* Refresh window */ - curwp->w_flag |= WFMODE | WFHARD ; - cknewwindow() ; - mloutstr( "(Old buffer)") ; - return TRUE ; - } - } + /* Refresh window */ + curwp->w_flag |= WFMODE | WFHARD ; + cknewwindow() ; + mloutstr( "(Old buffer)") ; + return TRUE ; + } + } - makename(bname, fname); /* New buffer name. */ - while ((bp = bfind(bname, FALSE, 0)) != NULL) { - char *new_bname ; + makename(bname, fname); /* New buffer name. */ + while ((bp = bfind(bname, FALSE, 0)) != NULL) { + char *new_bname ; - /* old buffer name conflict code */ - s = newmlarg( &new_bname, "Buffer name: ", sizeof( bname_t)) ; - if( s == ABORT) /* ^G to just quit */ - return s ; - else if (s == FALSE) { /* CR to clobber it */ - makename( bname, fname) ; - break ; - } else { /* TRUE */ - mystrscpy( bname, new_bname, sizeof bname) ; - free( new_bname) ; - } - } + /* old buffer name conflict code */ + s = newmlarg( &new_bname, "Buffer name: ", sizeof( bname_t)) ; + if( s == ABORT) /* ^G to just quit */ + return s ; + else if (s == FALSE) { /* CR to clobber it */ + makename( bname, fname) ; + break ; + } else { /* TRUE */ + mystrscpy( bname, new_bname, sizeof bname) ; + free( new_bname) ; + } + } - if (bp == NULL && (bp = bfind(bname, TRUE, 0)) == NULL) { - mloutstr( "Cannot create buffer") ; - return FALSE; - } - if (--curbp->b_nwnd == 0) { /* Undisplay. */ - curbp->b_dotp = curwp->w_dotp; - curbp->b_doto = curwp->w_doto; - curbp->b_markp = curwp->w_markp; - curbp->b_marko = curwp->w_marko; - } - curbp = bp; /* Switch to it. */ - curwp->w_bufp = bp; - curbp->b_nwnd++; - s = readin(fname, lockfl); /* Read it in. */ - cknewwindow(); - return s; + if (bp == NULL && (bp = bfind(bname, TRUE, 0)) == NULL) { + mloutstr( "Cannot create buffer") ; + return FALSE; + } + if (--curbp->b_nwnd == 0) { /* Undisplay. */ + curbp->b_dotp = curwp->w_dotp; + curbp->b_doto = curwp->w_doto; + curbp->b_markp = curwp->w_markp; + curbp->b_marko = curwp->w_marko; + } + curbp = bp; /* Switch to it. */ + curwp->w_bufp = bp; + curbp->b_nwnd++; + s = readin(fname, lockfl); /* Read it in. */ + cknewwindow(); + return s; } -/* Read file "fname" into the current buffer, blowing away any text - * found there. Called by both the read and find commands. Return - * the final status of the read. Also called by the mainline, to - * read in a file specified on the command line as an argument. - * The command bound to M-FNR is called after the buffer is set up - * and before it is read. - * - * char fname[]; name of file to read - * boolean lockfl; check for file locks? - */ -int readin(const char *fname, boolean lockfl) -{ - struct window *wp; - buffer_p bp; - int status ; - fio_code s ; +/* Read file "fname" into the current buffer, blowing away any text found + there. Called by both the read and find commands. Return the final + status of the read. Also called by the mainline, to read in a file + specified on the command line as an argument. The command bound to + M-FNR is called after the buffer is set up and before it is read. - bp = curbp; /* Cheap. */ + char fname[]; name of file to read + boolean lockfl; check for file locks? + */ +int readin( const char *fname, boolean lockfl) { + fio_code s ; + + buffer_p bp = curbp ; /* Cheap. */ #if (FILOCK && BSD) || SVR4 - if( lockfl && lockchk( fname) == ABORT) { -#if PKCODE - s = FIOFNF; - strcpy(bp->b_fname, ""); - mloutstr( "(File in use)") ; -#else - return ABORT; -#endif - } else + if( lockfl && lockchk( fname) == ABORT) { +# if PKCODE + s = FIOFNF; + strcpy(bp->b_fname, ""); + mloutstr( "(File in use)") ; +# else + return ABORT; +# endif + } else #endif { - if( (status = bclear( bp)) != TRUE) /* Might be old. */ - return status ; + int status = bclear( bp) ; + if( status != TRUE) /* Might be old. */ + return status ; - bp->b_flag &= ~(BFINVS | BFCHG); - if( fname != bp->b_fname) /* Copy if source differs from destination */ - mystrscpy( bp->b_fname, fname, sizeof( fname_t)) ; + bp->b_flag &= ~(BFINVS | BFCHG); + if( fname != bp->b_fname) /* Copy if source differs from destination */ + mystrscpy( bp->b_fname, fname, sizeof( fname_t)) ; - /* let a user macro get hold of things...if he wants */ - execute(META | SPEC | 'R', FALSE, 1); + /* let a user macro get hold of things...if he wants */ + execute(META | SPEC | 'R', FALSE, 1); - s = ffropen( bp->b_fname) ; /* Always use the name associated to buffer */ - if( s == FIOFNF) /* File not found. */ - mloutstr( "(New file)") ; - else if( s == FIOSUC) { - char *errmsg ; - eoltype found_eol ; - int nline = 0 ; + s = ffropen( bp->b_fname) ; /* Always use the name associated to buffer */ + if( s == FIOFNF) /* File not found. */ + mloutstr( "(New file)") ; + else if( s == FIOSUC) { + char *errmsg ; + eoltype found_eol ; + int nline = 0 ; - /* read the file in */ - mloutstr( "(Reading file)") ; - while( (s = ffgetline()) == FIOSUC) { - line_p lp ; + /* read the file in */ + mloutstr( "(Reading file)") ; + while( (s = ffgetline()) == FIOSUC) { + line_p lp ; - if( nline >= 10000000 /* Maximum # of lines from one file */ - || (lp = lalloc( fpayload)) == NULL) { - s = FIOMEM ; /* Keep message on the */ - break ; /* display. */ - } + if( nline >= 10000000 /* Maximum # of lines from one file */ + || (lp = lalloc( fpayload)) == NULL) { + s = FIOMEM ; /* Keep message on the */ + break ; /* display. */ + } - memcpy( lp->l_text, fline, fpayload) ; - lp->l_fp = curbp->b_linep ; /* insert before end of buffer */ - lp->l_bp = lp->l_fp->l_bp ; - lp->l_fp->l_bp = lp ; - lp->l_bp->l_fp = lp ; - nline += 1 ; - } + memcpy( lp->l_text, fline, fpayload) ; + lp->l_fp = curbp->b_linep ; /* insert before end of buffer */ + lp->l_bp = lp->l_fp->l_bp ; + lp->l_fp->l_bp = lp ; + lp->l_bp->l_fp = lp ; + nline += 1 ; + } - if( s == FIOERR) - mloutstr( "File read error") ; + if( s == FIOERR) + mloutstr( "File read error") ; - switch( ftype) { - case FTYPE_DOS: - found_eol = EOL_DOS ; - curbp->b_mode |= MDDOS ; - break ; - case FTYPE_UNIX: - found_eol = EOL_UNIX ; - break ; - case FTYPE_MAC: - found_eol = EOL_MAC ; - break ; - case FTYPE_NONE: - found_eol = EOL_NONE ; - break ; - default: - found_eol = EOL_MIXED ; - curbp->b_mode |= MDVIEW ; /* force view mode as we have lost - ** EOL information */ - } + switch( ftype) { + case FTYPE_DOS: + found_eol = EOL_DOS ; + curbp->b_mode |= MDDOS ; + break ; + case FTYPE_UNIX: + found_eol = EOL_UNIX ; + break ; + case FTYPE_MAC: + found_eol = EOL_MAC ; + break ; + case FTYPE_NONE: + found_eol = EOL_NONE ; + break ; + default: + found_eol = EOL_MIXED ; + curbp->b_mode |= MDVIEW ; /* force view mode as we have lost + ** EOL information */ + } - if( fcode == FCODE_UTF_8) - curbp->b_mode |= MDUTF8 ; + if( fcode == FCODE_UTF_8) + curbp->b_mode |= MDUTF8 ; - if( s == FIOERR - || s == FIOMEM) { - errmsg = (s == FIOERR) ? "I/O ERROR, " : "OUT OF MEMORY, " ; - curbp->b_flag |= BFTRUNC ; - curbp->b_mode |= MDVIEW ; /* force view mode as lost data */ - } else - errmsg = "" ; + if( s == FIOERR + || s == FIOMEM) { + errmsg = (s == FIOERR) ? "I/O ERROR, " : "OUT OF MEMORY, " ; + curbp->b_flag |= BFTRUNC ; + curbp->b_mode |= MDVIEW ; /* force view mode as lost data */ + } else + errmsg = "" ; - mloutfmt( "(%sRead %d line%s, code/eol: %s/%s)", - errmsg, - nline, - &"s"[ nline == 1], - codename[ fcode & (FCODE_MASK - 1)], - eolname[ found_eol]) ; - ffclose() ; /* Ignore errors. */ - } + mloutfmt( "(%sRead %d line%s, code/eol: %s/%s)", + errmsg, + nline, + &"s"[ nline == 1], + codename[ fcode], + eolname[ found_eol]) ; + ffclose() ; /* Ignore errors. */ + } } - for (wp = wheadp; wp != NULL; wp = wp->w_wndp) { - if (wp->w_bufp == curbp) { - wp->w_linep = lforw(curbp->b_linep); - wp->w_dotp = lforw(curbp->b_linep); - wp->w_doto = 0; - wp->w_markp = NULL; - wp->w_marko = 0; - wp->w_flag |= WFMODE | WFHARD; - } - } + for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp) { + if( wp->w_bufp == curbp) { + wp->w_linep = lforw( curbp->b_linep) ; + wp->w_dotp = lforw(curbp->b_linep) ; + wp->w_doto = 0 ; + wp->w_markp = NULL ; + wp->w_marko = 0 ; + wp->w_flag |= WFMODE | WFHARD ; + } + } - return (s == FIOERR || s == FIOFNF) ? FALSE : TRUE ; + return (s == FIOERR || s == FIOFNF) ? FALSE : TRUE ; } -/* - * Take a file name, and from it - * fabricate a buffer name. This routine knows - * about the syntax of file names on the target system. - * I suppose that this information could be put in - * a better place than a line of code. + +/* Take a file name, and from it fabricate a buffer name. This routine + knows about the syntax of file names on the target system. I suppose + that this information could be put in a better place than a line of + code. */ -void makename( bname_t bname, const char *fname) -{ - const char *cp1; - char *cp2; +void makename( bname_t bname, const char *fname) { + const char *cp1 = fname ; + for( const char *cp = cp1 ; *cp ; cp++) + if( *cp == '/') + cp1 = cp + 1 ; - cp1 = &fname[0]; - while (*cp1 != 0) - ++cp1; + char *cp2 = bname ; + while( *cp1 != 0 && *cp1 != ';') { + unicode_t c ; + int n = utf8_to_unicode( cp1, 0, 4, &c) ; + if( cp2 + n <= &bname[ sizeof( bname_t) - 2]) /* 1 digit buffer name conflict [0..9] + EOS */ + while( n--) + *cp2++ = *cp1++ ; + else + break ; + } - while (cp1 != &fname[0] && cp1[-1] != '/') - --cp1; - - cp2 = &bname[0]; - while( *cp1 != 0 && *cp1 != ';') { - unicode_t c ; - int n ; - - n = utf8_to_unicode( cp1, 0, 4, &c) ; - if( cp2 + n <= &bname[ sizeof( bname_t) - 2]) /* 1 digit buffer name conflict [0..9] + EOS */ - while( n--) - *cp2++ = *cp1++ ; - else - break ; - } - - *cp2 = 0; + *cp2 = 0 ; } -/* - * make sure a buffer name is unique + +/* make sure a buffer name is unique * - * char *name; name to check on + * char *name; name to check on */ -void unqname(char *name) -{ - char *sp; +void unqname( char *name) { +/* check to see if it is in the buffer list */ + while( bfind( name, 0, FALSE) != NULL) { + /* go to the end of the name */ + char *sp = name ; + while( *sp) + ++sp ; - /* check to see if it is in the buffer list */ - while (bfind(name, 0, FALSE) != NULL) { - - /* go to the end of the name */ - sp = name; - while (*sp) - ++sp; - if (sp == name || (*(sp - 1) < '0' || *(sp - 1) > '8')) { - *sp++ = '0'; - *sp = 0; - } else - *(--sp) += 1; - } + if( sp == name || (*(sp - 1) < '0' || *(sp - 1) > '8')) { + *sp++ = '0' ; + *sp = 0 ; + } else + *(--sp) += 1 ; + } } + /* Ask for a file name, and write the content of the current buffer to that * file. Update the remembered file name and clear the buffer changed * flag. Bound to C-X C-W write-file. */ BINDABLE( filewrite) { - char *fname ; + char *fname ; - if( restflag) /* don't allow this command if restricted */ - return resterr() ; + if( restflag) /* don't allow this command if restricted */ + return resterr() ; - int status = newmlarg( &fname, "write-file: ", sizeof( fname_t)) ; - if( status == TRUE) { - if( strlen( fname) > sizeof( fname_t) - 1) - status = FALSE ; - else { - status = writeout( fname) ; - if( status == TRUE) - strcpy( curbp->b_fname, fname) ; - } + int status = newmlarg( &fname, "write-file: ", sizeof( fname_t)) ; + if( status == TRUE) { + if( strlen( fname) > sizeof( fname_t) - 1) + status = FALSE ; + else { + status = writeout( fname) ; + if( status == TRUE) + strcpy( curbp->b_fname, fname) ; + } - free( fname) ; - } + free( fname) ; + } - return status ; + return status ; } + /* Save the content of the current buffer in its associated file. Do - * nothing if nothing has changed (this may be a bug, not a feature). - * Error if there is no remembered file name for the buffer. Bound to "C-X - * C-S save-file". May get called by "M-Z quick-exit". + nothing if nothing has changed (this may be a bug, not a feature). + Error if there is no remembered file name for the buffer. Bound to "C-X + C-S save-file". May get called by "M-Z quick-exit". */ BINDABLE( filesave) { - assert( !(curbp->b_mode & MDVIEW)) ; + assert( !(curbp->b_mode & MDVIEW)) ; - if( (curbp->b_flag & BFCHG) == 0) /* Return, no changes. */ - return TRUE ; + if( (curbp->b_flag & BFCHG) == 0) /* Return, no changes. */ + return TRUE ; - if( curbp->b_fname[0] == 0) { /* Must have a name. */ - mloutfmt( "%BNo file name") ; - return FALSE ; - } + if( curbp->b_fname[0] == 0) { /* Must have a name. */ + mloutfmt( "%BNo file name") ; + return FALSE ; + } - /* complain about truncated files */ - if( (curbp->b_flag & BFTRUNC) != 0 - && mlyesno( "Truncated file ... write it out") == FALSE) - return mloutfail( "(Aborted)") ; + /* complain about truncated files */ + if( (curbp->b_flag & BFTRUNC) != 0 + && mlyesno( "Truncated file ... write it out") == FALSE) + return mloutfail( "(Aborted)") ; - return writeout( curbp->b_fname) ; + return writeout( curbp->b_fname) ; } -/* - * This function performs the details of file - * writing. Uses the file management routines in the - * "fileio.c" package. The number of lines written is - * displayed. Sadly, it looks inside a struct line; provide - * a macro for this. Most of the grief is error - * checking of some sort. + +/* This function performs the details of file writing. Uses the file + management routines in the "fileio.c" package. The number of lines + written is displayed. Sadly, it looks inside a struct line; provide a + macro for this. Most of the grief is error checking of some sort. */ int writeout( const char *fn) { - fio_code s ; + fio_code s ; - s = ffwopen( fn) ; /* Open writes message. */ - if( s != FIOSUC) - mloutstr( "Cannot open file for writing") ; - else { - line_p lp ; - fio_code s2 ; - int nline = 0 ; + s = ffwopen( fn) ; /* Open writes message. */ + if( s != FIOSUC) + mloutstr( "Cannot open file for writing") ; + else { + line_p lp ; + fio_code s2 ; + int nline = 0 ; - mloutstr( "(Writing...)") ; /* tell us we are writing */ - for( lp = lforw( curbp->b_linep) ; lp != curbp->b_linep ; lp = lforw( lp)) { - s = ffputline( lp->l_text, llength( lp), curbp->b_mode & MDDOS) ; - if( s != FIOSUC) - break ; + mloutstr( "(Writing...)") ; /* tell us we are writing */ + for( lp = lforw( curbp->b_linep) ; lp != curbp->b_linep ; lp = lforw( lp)) { + s = ffputline( lp->l_text, llength( lp), curbp->b_mode & MDDOS) ; + if( s != FIOSUC) + break ; - nline += 1 ; - } + nline += 1 ; + } - s2 = ffclose() ; - if( s != FIOSUC) - mloutstr( "Write I/O error") ; - else if( s2 != FIOSUC) - mloutstr( "Error closing file") ; - else { /* Successfull write and close. */ - mloutfmt( "(Wrote %d line%s)", nline, &"s"[ nline == 1]) ; - curbp->b_flag &= ~BFCHG ; - upd_mode() ; - return TRUE ; - } - } + s2 = ffclose() ; + if( s != FIOSUC) + mloutstr( "Write I/O error") ; + else if( s2 != FIOSUC) + mloutstr( "Error closing file") ; + else { /* Successfull write and close. */ + mloutfmt( "(Wrote %d line%s)", nline, &"s"[ nline == 1]) ; + curbp->b_flag &= ~BFCHG ; + upd_mode() ; + return TRUE ; + } + } - return FALSE ; + return FALSE ; } + /* The command allows the user to modify the file name associated with the current buffer. It is like the "f" command in UNIX "ed". The operation is simple; just zap the name in the buffer structure, and mark the @@ -518,103 +506,102 @@ int writeout( const char *fn) { if you wish. Bound to C-X N change-file-name. */ BINDABLE( filename) { - char *fname ; + char *fname ; - if( restflag) /* don't allow this command if restricted */ - return resterr() ; + if( restflag) /* don't allow this command if restricted */ + return resterr() ; - int status = newmlarg( &fname, "Name: ", sizeof( fname_t)) ; - if( status == ABORT) - return status ; - else if( status == FALSE) - curbp->b_fname[ 0] = '\0' ; - else { /* TRUE */ - mystrscpy( curbp->b_fname, fname, sizeof( fname_t)) ; - free( fname) ; - } + int status = newmlarg( &fname, "Name: ", sizeof( fname_t)) ; + if( status == ABORT) + return status ; + else if( status == FALSE) + curbp->b_fname[ 0] = '\0' ; + else { /* TRUE */ + mystrscpy( curbp->b_fname, fname, sizeof( fname_t)) ; + free( fname) ; + } - curbp->b_mode &= ~MDVIEW ; /* no longer read only mode */ - upd_mode() ; - return TRUE ; + curbp->b_mode &= ~MDVIEW ; /* no longer read only mode */ + upd_mode() ; + return TRUE ; } -/* - * Insert file "fname" into the current - * buffer, Called by insert file command. Return the final - * status of the read. + +/* Insert file "fname" into the current buffer, Called by insert file + command. Return the final status of the read. */ static int ifile( const char *fname) { - fio_code s ; + fio_code s ; - curbp->b_flag |= BFCHG ; /* we have changed */ - curbp->b_flag &= ~BFINVS ; /* and are not temporary */ - s = ffropen( fname) ; - if( s == FIOFNF) { /* File not found. */ - mloutstr( "(No such file)") ; - return FALSE ; - } + curbp->b_flag |= BFCHG ; /* we have changed */ + curbp->b_flag &= ~BFINVS ; /* and are not temporary */ + s = ffropen( fname) ; + if( s == FIOFNF) { /* File not found. */ + mloutstr( "(No such file)") ; + return FALSE ; + } - if( s == FIOSUC) { /* Hard file open. */ - int nline = 0 ; /* number of line read */ - char *errmsg ; + if( s == FIOSUC) { /* Hard file open. */ + int nline = 0 ; /* number of line read */ + char *errmsg ; - mloutstr( "(Inserting file)") ; + mloutstr( "(Inserting file)") ; - /* back up a line and save the mark here */ - curwp->w_dotp = lback(curwp->w_dotp); - curwp->w_doto = 0; - curwp->w_markp = curwp->w_dotp; - curwp->w_marko = 0; + /* back up a line and save the mark here */ + curwp->w_dotp = lback(curwp->w_dotp); + curwp->w_doto = 0; + curwp->w_markp = curwp->w_dotp; + curwp->w_marko = 0; - while( (s = ffgetline()) == FIOSUC) { - line_p lpp, lp, lpn ; + while( (s = ffgetline()) == FIOSUC) { + line_p lpp, lp, lpn ; - if( (lp = lalloc( fpayload)) == NULL) { - s = FIOMEM ; /* Keep message on the */ - break ; /* display. */ - } + if( (lp = lalloc( fpayload)) == NULL) { + s = FIOMEM ; /* Keep message on the */ + break ; /* display. */ + } - memcpy( lp->l_text, fline, fpayload) ; - lp->l_bp = lpp = curwp->w_dotp ; /* insert after dot line */ - lp->l_fp = lpn = lpp->l_fp ; /* line after insert */ + memcpy( lp->l_text, fline, fpayload) ; + lp->l_bp = lpp = curwp->w_dotp ; /* insert after dot line */ + lp->l_fp = lpn = lpp->l_fp ; /* line after insert */ - /* re-link new line between lpp and lpn */ - lpn->l_bp = lp ; - lpp->l_fp = lp ; + /* re-link new line between lpp and lpn */ + lpn->l_bp = lp ; + lpp->l_fp = lp ; - /* and advance and write out the current line */ - curwp->w_dotp = lp ; - nline += 1 ; - } + /* and advance and write out the current line */ + curwp->w_dotp = lp ; + nline += 1 ; + } - ffclose() ; /* Ignore errors. */ - curwp->w_markp = lforw(curwp->w_markp); - if( s == FIOERR) { - errmsg = "I/O ERROR, " ; - curbp->b_flag |= BFTRUNC ; - } else if( s == FIOMEM) { - errmsg = "OUT OF MEMORY, " ; - curbp->b_flag |= BFTRUNC; - } else - errmsg = "" ; + ffclose() ; /* Ignore errors. */ + curwp->w_markp = lforw(curwp->w_markp); + if( s == FIOERR) { + errmsg = "I/O ERROR, " ; + curbp->b_flag |= BFTRUNC ; + } else if( s == FIOMEM) { + errmsg = "OUT OF MEMORY, " ; + curbp->b_flag |= BFTRUNC; + } else + errmsg = "" ; - mloutfmt( "(%sInserted %d line%s)", - errmsg, - nline, - &"s"[ nline == 1]) ; - } + mloutfmt( "(%sInserted %d line%s)", + errmsg, + nline, + &"s"[ nline == 1]) ; + } /* advance to the next line and mark the window for changes */ - curwp->w_dotp = lforw(curwp->w_dotp); - curwp->w_flag |= WFHARD | WFMODE; + curwp->w_dotp = lforw(curwp->w_dotp); + curwp->w_flag |= WFHARD | WFMODE; /* copy window parameters back to the buffer structure */ - curbp->b_dotp = curwp->w_dotp; - curbp->b_doto = curwp->w_doto; - curbp->b_markp = curwp->w_markp; - curbp->b_marko = curwp->w_marko; + curbp->b_dotp = curwp->w_dotp; + curbp->b_doto = curwp->w_doto; + curbp->b_markp = curwp->w_markp; + curbp->b_marko = curwp->w_marko; - return (s == FIOERR) ? FALSE : TRUE ; + return (s == FIOERR) ? FALSE : TRUE ; } /* end of file.c */ diff --git a/fileio.c b/fileio.c index 9c46487..c255d7f 100644 --- a/fileio.c +++ b/fileio.c @@ -1,13 +1,10 @@ /* fileio.c -- implements fileio.h */ - #include "fileio.h" -/* FILEIO.C - * - * The routines in this file read and write ASCII files from the disk. All of - * the knowledge about files are here. - * - * modified by Petri Kutvonen +/* The routines in this file read and write ASCII files from the disk. All + of the knowledge about files are here. + + modified by Petri Kutvonen */ #include @@ -18,66 +15,62 @@ #include "retcode.h" #include "utf8.h" -char *fline = NULL ; /* dynamic return line */ -static int flen = 0 ; /* current allocated length of fline */ -int ftype ; -int fcode ; /* encoding type FCODE_xxxxx */ -int fpayload ; /* actual length of fline content */ +char *fline = NULL ; /* dynamic return line */ +static int flen = 0 ; /* current allocated length of fline */ +int ftype ; +int fcode ; /* encoding type FCODE_xxxxx */ +int fpayload ; /* actual length of fline content */ -static FILE *ffp ; /* File pointer, all functions. */ -static boolean eofflag ; /* end-of-file flag */ +static FILE *ffp ; /* File pointer, all functions. */ +static boolean eofflag ; /* end-of-file flag */ -/* - * Open a file for reading. - */ -fio_code ffropen( const char *fn) -{ - if ((ffp = fopen(fn, "r")) == NULL) - return FIOFNF; - eofflag = FALSE; +/* Open a file for reading. */ +fio_code ffropen( const char *fn) { + eofflag = FALSE ; ftype = FTYPE_NONE ; fcode = FCODE_ASCII ; - return FIOSUC; + ffp = fopen( fn, "r") ; + return (ffp == NULL) ? FIOFNF : FIOSUC ; } -/* - * Open a file for writing. Return TRUE if all is well, and FALSE on error - * (cannot create). + +/* Open a file for writing. Return TRUE if all is well, and FALSE on error + (cannot create). */ fio_code ffwopen( const char *fn) { ffp = fopen( fn, "w") ; - return (ffp == NULL) ? FIOERR : FIOSUC ; + return (ffp == NULL) ? FIOERR : FIOSUC ; } -/* - * Close a file. Should look at the status in all systems. + +/* Close a file. Should look at the status in all systems. */ -fio_code ffclose(void) -{ +fio_code ffclose( void) { /* free this since we do not need it anymore */ - if (fline) { - free(fline); - fline = NULL; + if( fline) { + free( fline) ; + fline = NULL ; } - eofflag = FALSE; + + eofflag = FALSE ; ftype = FTYPE_NONE ; fcode = FCODE_ASCII ; - return (fclose( ffp) != FALSE) ? FIOERR : FIOSUC ; + return (fclose( ffp) != FALSE) ? FIOERR : FIOSUC ; } -/* - * Write a line to the already opened file. The "buf" points to the buffer, - * and the "nbuf" is its length, less the free newline. Return the status. - * Check only at the newline. + +/* Write a line to the already opened file. The "buf" points to the + buffer, and the "nbuf" is its length, less the free newline. Return the + status. Check only at the newline. */ fio_code ffputline( char *buf, int nbuf, int dosflag) { - fwrite( buf, 1, nbuf, ffp) ; - - if( dosflag) - fputc( '\r', ffp) ; + fwrite( buf, 1, nbuf, ffp) ; + + if( dosflag) + fputc( '\r', ffp) ; fputc( '\n', ffp) ; @@ -87,92 +80,91 @@ fio_code ffputline( char *buf, int nbuf, int dosflag) { return FIOSUC ; } -/* - * Read a line from a file, and store the bytes in the supplied buffer. The - * "nbuf" is the length of the buffer. Complain about long lines and lines - * at the end of the file that don't have a newline present. Check for I/O - * errors too. Return status. +/* Read a line from a file, and store the bytes in the supplied buffer. + The "nbuf" is the length of the buffer. Complain about long lines and + lines at the end of the file that don't have a newline present. Check + for I/O errors too. Return status. */ fio_code ffgetline( void) { - int c ; /* current character read */ - int i ; /* current index into fline */ - int lcode = FCODE_ASCII ; /* line encoding, defaults to ASCII */ + int c ; /* current character read */ + int lcode = FCODE_ASCII ; /* line encoding, defaults to ASCII */ /* if we are at the end...return it */ - if( eofflag) - return FIOEOF ; + if( eofflag) + return FIOEOF ; /* dump fline if it ended up too big */ - if( flen > NSTRING) { - free( fline) ; - fline = NULL ; - } + if( flen > NSTRING) { + free( fline) ; + fline = NULL ; + } /* if we don't have an fline, allocate one */ - if( fline == NULL) - if( (fline = malloc( flen = NSTRING)) == NULL) - return FIOMEM ; + if( fline == NULL) + if( (fline = malloc( flen = NSTRING)) == NULL) + return FIOMEM ; /* read the line in */ - i = 0 ; - while( (c = fgetc( ffp)) != EOF && c != '\r' && c != '\n') { - /* if line is full, get more room */ - if( i >= flen) { - char *tmpline ; /* temp storage for expanding line */ + int i = 0 ; /* current index into fline */ + while( (c = fgetc( ffp)) != EOF && c != '\r' && c != '\n') { + /* if line is full, get more room */ + if( i >= flen) { + char *tmpline ; /* temp storage for expanding line */ - tmpline = malloc( flen + NSTRING) ; - if( tmpline == NULL) - return FIOMEM ; + tmpline = malloc( flen + NSTRING) ; + if( tmpline == NULL) + return FIOMEM ; - memcpy( tmpline, fline, flen) ; - flen += NSTRING ; - free( fline) ; - fline = tmpline ; - } + memcpy( tmpline, fline, flen) ; + flen += NSTRING ; + free( fline) ; + fline = tmpline ; + } - fline[ i++] = c ; - lcode |= c ; - } + fline[ i++] = c ; + lcode |= c ; + } - fpayload = i ; - lcode &= FCODE_MASK ; - if( lcode && (fcode != FCODE_MIXED)) { /* line contains extended chars */ - /* Check if consistent UTF-8 encoding */ - int pos = 0 ; + fpayload = i ; + if( lcode & 0x80 /* line contains extended chars */ + && (fcode != FCODE_MIXED)) { + /* Check if consistent UTF-8 encoding */ + lcode = FCODE_ASCII ; + int pos = 0 ; + while( (pos < i) && (lcode != FCODE_MIXED)) { + unicode_t uc ; - while( (pos < i) && (lcode != FCODE_MIXED)) { - unicode_t uc ; - int bytes ; + int bytes = utf8_to_unicode( fline, pos, i, &uc) ; + pos += bytes ; + if( bytes > 1) /* Multi byte UTF-8 sequence */ + lcode |= FCODE_UTF_8 ; + else if( uc > 127) /* Extended ASCII */ + lcode |= FCODE_EXTND ; + } - bytes = utf8_to_unicode( fline, pos, i, &uc) ; - pos += bytes ; - if( bytes > 1) /* Multi byte UTF-8 sequence */ - lcode |= FCODE_UTF_8 ; - else if( uc > 127) /* Extended ASCII */ - lcode |= FCODE_EXTND ; - } - - fcode |= lcode ; - } + fcode |= lcode ; + } /* test for any errors that may have occured */ - if( c == EOF) { - if( ferror( ffp)) - return FIOERR ; + if( c == EOF) { + if( ferror( ffp)) + return FIOERR ; - if( i != 0) - eofflag = TRUE ; - else - return FIOEOF ; - } else if( c == '\r') { - c = fgetc( ffp) ; - if( c != '\n') { - ftype |= FTYPE_MAC ; - ungetc( c, ffp) ; - } else - ftype |= FTYPE_DOS ; - } else /* c == '\n' */ - ftype |= FTYPE_UNIX ; + if( i != 0) + eofflag = TRUE ; + else + return FIOEOF ; + } else if( c == '\r') { + c = fgetc( ffp) ; + if( c != '\n') { + ftype |= FTYPE_MAC ; + ungetc( c, ffp) ; + } else + ftype |= FTYPE_DOS ; + } else /* c == '\n' */ + ftype |= FTYPE_UNIX ; return FIOSUC ; } + +/* end of fileio.c */ diff --git a/fileio.h b/fileio.h index df0219a..c8caf57 100644 --- a/fileio.h +++ b/fileio.h @@ -17,10 +17,9 @@ typedef enum { /* FTYPE_MIXED [ 3, 5, 6, 7] */ #define FCODE_ASCII 0 -#define FCODE_MASK 0x80 -#define FCODE_UTF_8 0x81 -#define FCODE_EXTND 0x82 -#define FCODE_MIXED 0x83 +#define FCODE_UTF_8 1 +#define FCODE_EXTND 2 +#define FCODE_MIXED 3 extern char *fline ; /* dynamic return line */ extern int ftype ; diff --git a/flook.c b/flook.c index 055aa44..899a2ce 100644 --- a/flook.c +++ b/flook.c @@ -1,153 +1,131 @@ /* flook.c -- implements flook.h */ #include "flook.h" - #include #include #include - #include "defines.h" -#include "fileio.h" -/* possible names and paths of help files under different OSs */ +/* possible names and paths of help files under different OSes */ const char *pathname[] = { -#if BSD | USG - ".emacsrc", - "emacs.hlp", -#if PKCODE - "/usr/global/lib/", "/usr/local/bin/", "/usr/local/lib/", -#endif - "/usr/local/", "/usr/lib/", "" +#if BSD | USG + ".emacsrc", + "emacs.hlp", +# if PKCODE + "/usr/global/lib/", "/usr/local/bin/", "/usr/local/lib/", +# endif + "/usr/local/", "/usr/lib/", "" #endif } ; -#define PATHNAME_SIZE (sizeof pathname / sizeof pathname[ 0]) +#define PATHTABLE_SIZE (sizeof pathname / sizeof pathname[ 0]) -/* - * does exist on disk? +/* does exist on disk? * * char *fname; file to check for existance */ -boolean fexist( const char *fname) -{ - FILE *fp; +boolean fexist( const char *fname) { + FILE *fp = fopen( fname, "r") ; + if( fp == NULL) + return FALSE ; + else + fclose( fp) ; - /* try to open the file for reading */ - fp = fopen(fname, "r"); - - /* if it fails, just return false! */ - if (fp == NULL) - return FALSE; - - /* otherwise, close it and report true */ - fclose(fp); - return TRUE; + return TRUE ; } -/* - * Look up the existance of a file along the normal or PATH - * environment variable. Look first in the HOME directory if - * asked and possible - * - * char *fname; base file name to search for - * boolean hflag; Look in the HOME environment variable first? + +/* Look up the existence of a file along the normal or PATH environment + variable. Look first in the HOME directory if asked and possible. + + char *fname; base file name to search for + boolean hflag; look in the HOME environment variable first? */ -char *flook( const char *fname, boolean hflag) -{ - unsigned i ; /* index */ - int len ; - static char fspec[NSTRING]; /* full path spec to search */ +char *flook( const char *fname, boolean hflag) { + static char fspec[ NSTRING] ; /* full path spec to search */ -#if ENVFUNC - char *path; /* environmental PATH variable */ + int len = sizeof fspec - strlen( fname) - 1 ; + if( len < 0) + return NULL ; + +#if ENVFUNC + if( hflag) { + char *home = getenv( "HOME") ; /* path to home directory */ + if( home != NULL) { + if( len > (int) strlen( home) + 1) { + /* build home dir file spec */ + strcpy( fspec, home) ; + strcat( fspec, "/") ; + strcat( fspec, fname) ; + + /* and try it out */ + if( fexist( fspec)) + return fspec ; + } + } + } #endif - len = sizeof fspec - strlen( fname) - 1 ; - if( len < 0) - return NULL ; + /* always try the current directory first */ + if( len >= 0) { + strcpy( fspec, fname) ; + if( fexist( fspec)) + return fspec ; + } -#if ENVFUNC - if (hflag) { - char *home; /* path to home directory */ +#if ENVFUNC +# if USG | BSD +# define PATHCHR ':' +# else +# define PATHCHR ';' +# endif - home = getenv("HOME"); - if (home != NULL) { - if( len > (int) strlen( home) + 1) { - /* build home dir file spec */ - strcpy( fspec, home) ; - strcat(fspec, "/"); - strcat(fspec, fname); +/* get the PATH variable */ + char *path = getenv( "PATH") ; /* environmental PATH variable */ + if( path != NULL) + while( *path) { + int cnt = len ; + /* build next possible file spec */ + char *sp = fspec ; /* pointer into path spec */ + while( *path && (*path != PATHCHR)) { + if( cnt-- > 0) + *sp++ = *path ; - /* and try it out */ - if( fexist( fspec)) - return fspec ; - } - } - } + path += 1 ; + } + + if( cnt >= 0) { + /* add a terminating dir separator if we need it */ + if (sp != fspec) + *sp++ = '/'; + *sp = 0; + strcat(fspec, fname); + + /* and try it out */ + if( fexist( fspec)) + return fspec ; + } + + if( *path == PATHCHR) + ++path ; + } #endif - /* always try the current directory first */ - if( len >= 0) { - strcpy( fspec, fname) ; - if( fexist( fspec)) - return fspec ; - } + /* look it up via the old table method */ + for( unsigned i = 2 ; i < PATHTABLE_SIZE ; i++) + if( len >= (int) strlen( pathname[ i])) { + strcpy( fspec, pathname[ i]) ; + strcat( fspec, fname); -#if ENVFUNC -#if USG | BSD -#define PATHCHR ':' -#else -#define PATHCHR ';' -#endif + /* and try it out */ + if( fexist( fspec)) + return fspec ; + } - /* get the PATH variable */ - path = getenv("PATH"); - if (path != NULL) - while (*path) { - char *sp; /* pointer into path spec */ - int cnt ; - - cnt = len ; - /* build next possible file spec */ - sp = fspec; - while( *path && (*path != PATHCHR)) { - if( cnt-- > 0) - *sp++ = *path ; - - path += 1 ; - } - - if( cnt >= 0) { - /* add a terminating dir separator if we need it */ - if (sp != fspec) - *sp++ = '/'; - *sp = 0; - strcat(fspec, fname); - - /* and try it out */ - if( fexist( fspec)) - return fspec ; - } - - if (*path == PATHCHR) - ++path; - } -#endif - - /* look it up via the old table method */ - for( i = 2; i < PATHNAME_SIZE ; i++) - if( len >= (int) strlen( pathname[ i])) { - strcpy( fspec, pathname[ i]) ; - strcat( fspec, fname); - - /* and try it out */ - if( fexist( fspec)) - return fspec ; - } - - return NULL; /* no such luck */ + return NULL ; /* no such luck */ } +/* end of flook.c */ From 109e330861b1d0e0306e16f2fd5076cb5ab271c4 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sun, 15 Aug 2021 09:41:35 +0800 Subject: [PATCH 35/37] Consistent interface to deal with workaround on Cygwin when checking width of unicode character > 0xFFFF. --- basic.c | 6 +- display.c | 348 ++++++++++++++++++++++++------------------------------ random.c | 3 +- utf8.c | 21 ++-- utf8.h | 3 +- 5 files changed, 174 insertions(+), 207 deletions(-) diff --git a/basic.c b/basic.c index f6e3306..791d021 100644 --- a/basic.c +++ b/basic.c @@ -44,10 +44,8 @@ static unsigned getgoal( line_p dlp) { col += 2 ; else if( c >= 0x80 && c <= 0xA0) /* \xx */ col += 3 ; - else { - int w = utf8_width( c) ; /* work around */ - col += (w < 0) ? 2 : w ; /* unknown unicode width as \u */ - } + else + col += utf8_width( c) ; if( col > curgoal) break ; diff --git a/display.c b/display.c index c5226c9..11bb8e8 100644 --- a/display.c +++ b/display.c @@ -12,7 +12,6 @@ */ #include -#include #include #include #include @@ -28,16 +27,16 @@ #include "utf8.h" #include "window.h" -struct video { - int v_flag; /* Flags */ +typedef struct { + int v_flag ; /* Flags */ #if COLOR - int v_fcolor; /* current forground color */ - int v_bcolor; /* current background color */ - int v_rfcolor; /* requested forground color */ - int v_rbcolor; /* requested background color */ + int v_fcolor ; /* current foreground color */ + int v_bcolor ; /* current background color */ + int v_rfcolor ; /* requested foreground color */ + int v_rbcolor ; /* requested background color */ #endif unicode_t v_text[] ; /* Screen data. */ -}; +} *video_p ; #define VFCHG 0x0001 /* Changed flag */ #define VFEXT 0x0002 /* extended (beyond column 80) */ @@ -45,19 +44,20 @@ struct video { #define VFREQ 0x0008 /* reverse video request */ #define VFCOL 0x0010 /* color change requested */ -static struct video **vscreen; /* Virtual screen. */ +static video_p *vscreen ; /* Virtual screen. */ #if MEMMAP == 0 || SCROLLCODE -static struct video **pscreen; /* Physical screen. */ +static video_p *pscreen ; /* Physical screen. */ #endif static int displaying = TRUE; #if UNIX -#include +# include #endif + #ifdef SIGWINCH -#include +# include /* for window size changes */ -int chg_width, chg_height; + int chg_width, chg_height ; #endif static int currow ; /* Cursor row */ @@ -73,16 +73,16 @@ int discmd = TRUE ; /* display command flag */ int disinp = TRUE ; /* display input characters (echo) */ -static int reframe(struct window *wp); -static void updone(struct window *wp); -static void updall(struct window *wp); +static int reframe( window_p wp) ; +static void updone( window_p wp) ; +static void updall( window_p wp) ; static int scrolls(int inserts); static void scrscroll(int from, int to, int count); static int texttest(int vrow, int prow); static int endofline(unicode_t *s, int n); static void updext(void); -static int updateline(int row, struct video *vp1, struct video *vp2); -static void modeline(struct window *wp); +static int updateline(int row, video_p vp1, video_p vp2); +static void modeline( window_p wp) ; static void mlputi(int i, int r); static void mlputli(long l, int r); static void mlputf(int s); @@ -91,100 +91,90 @@ static void mlputs( const char *s) ; static int newscreensize(int h, int w); #endif -/* - * Initialize the data structures used by the display code. The edge vectors - * used to access the screens are set up. The operating system's terminal I/O - * channel is set up. All the other things get initialized at compile time. - * The original window has "WFCHG" set, so that it will get completely - * redrawn on the first call to "update". + +/* Initialize the data structures used by the display code. The edge + vectors used to access the screens are set up. The operating system's + terminal I/O channel is set up. All the other things get initialized at + compile time. The original window has "WFCHG" set, so that it will get + completely redrawn on the first call to "update". */ -void vtinit(void) -{ - int i; - struct video *vp; - - TTopen(); /* open the screen */ - TTkopen(); /* open the keyboard */ - TTrev(FALSE); - vscreen = xmalloc( term.t_maxrow * sizeof( struct video *)) ; +void vtinit( void) { + TTopen() ; /* open the screen */ + TTkopen() ; /* open the keyboard */ + TTrev(FALSE) ; + vscreen = xmalloc( term.t_maxrow * sizeof( video_p )) ; #if MEMMAP == 0 || SCROLLCODE - pscreen = xmalloc( term.t_maxrow * sizeof( struct video *)) ; + pscreen = xmalloc( term.t_maxrow * sizeof( video_p )) ; #endif - for( i = 0 ; i < term.t_maxrow ; ++i) { - vp = xmalloc( sizeof( struct video) + term.t_maxcol * sizeof( unicode_t)) ; - vp->v_flag = 0; + for( int i = 0 ; i < term.t_maxrow ; ++i) { + video_p vp = xmalloc( sizeof *vp + term.t_maxcol * sizeof( unicode_t)) ; + vp->v_flag = 0 ; #if COLOR - vp->v_rfcolor = 7; - vp->v_rbcolor = 0; + vp->v_rfcolor = 7 ; + vp->v_rbcolor = 0 ; #endif - vscreen[i] = vp; + vscreen[ i] = vp ; #if MEMMAP == 0 || SCROLLCODE - vp = xmalloc( sizeof( struct video) + term.t_maxcol * sizeof( unicode_t)) ; - vp->v_flag = 0; - pscreen[i] = vp; + vp = xmalloc( sizeof *vp + term.t_maxcol * sizeof( unicode_t)) ; + vp->v_flag = 0 ; + pscreen[ i] = vp ; #endif } } #if CLEAN /* free up all the dynamically allocated video structures */ - -void vtfree(void) -{ - int i; - for( i = 0 ; i < term.t_maxrow; ++i ) { - free(vscreen[i]); +void vtfree( void) { + for( int i = 0 ; i < term.t_maxrow; ++i ) { + free( vscreen[ i]) ; #if MEMMAP == 0 || SCROLLCODE - free(pscreen[i]); + free( pscreen[ i]) ; #endif } - free(vscreen); + + free( vscreen) ; #if MEMMAP == 0 || SCROLLCODE - free(pscreen); + free( pscreen) ; #endif } #endif -/* - * Clean up the virtual terminal system, in anticipation for a return to the - * operating system. Move down to the last line and clear it out (the next - * system prompt will be written in the line). Shut down the channel to the - * terminal. + +/* Clean up the virtual terminal system, in anticipation for a return to + the operating system. Move down to the last line and clear it out (the + next system prompt will be written in the line). Shut down the channel + to the terminal. */ -void vttidy(void) -{ +void vttidy( void) { mlerase() ; /* ends with movecursor( term.t_nrow, 0) and TTflush() */ - TTclose(); - TTkclose(); + TTclose() ; + TTkclose() ; #ifdef PKCODE - { - int ret ; - ret = write( 1, "\r", 1) ; - if( ret != 1) { + int ret = write( 1, "\r", 1) ; + if( ret != 1) { /* some error handling here */ - } } #endif } -/* - * Set the virtual cursor to the specified row and column on the virtual - * screen. There is no checking for nonsense values; this might be a good - * idea during the early stages. + +/* Set the virtual cursor to the specified row and column on the virtual + screen. There is no checking for nonsense values; this might be a good + idea during the early stages. */ static void vtmove( int row, int col) { - vtrow = row; - vtcol = col; + vtrow = row ; + vtcol = col ; } -/* - * Write a character to the virtual screen. The virtual row and - * column are updated. If we are not yet on left edge, don't print - * it yet. If the line is too long put a "$" in the last column. - * - * This routine only puts printing characters into the virtual - * terminal buffers. Only column overflow is checked. + +/* Write a character to the virtual screen. The virtual row and column are + updated. If we are not yet on left edge, don't print it yet. If the + line is too long put a "$" in the last column. + + This routine only puts printing characters into the virtual terminal + buffers. Only column overflow is checked. */ static void sane_vtputc( unicode_t c) { @@ -216,7 +206,7 @@ static void vtputc( unicode_t c) { sane_vtputc( '\\') ; sane_vtputc( hex[ c >> 4]) ; sane_vtputc( hex[ c & 15]) ; - } else if( utf8_width( c) < 0) { + } else if( _utf8_width( c) < 0) { sane_vtputc( '\\') ; /* show as non printable */ sane_vtputc( 'u') ; } else @@ -231,7 +221,7 @@ static int vtputs( const char *s) { s += utf8_to_unicode( s, 0, 4, &c) ; vtputc( c) ; - n += utf8_width( c) ; /* To Do: only works if all printable */ + n += utf8_width( c) ; } return n ; @@ -271,8 +261,6 @@ static int scrflags; boolean force_f ; force update past type ahead? */ int update( boolean force_f) { - struct window *wp; - #if TYPEAH && ! PKCODE if( force_f == FALSE && typahead()) return TRUE; @@ -286,59 +274,45 @@ int update( boolean force_f) { #if SCROLLCODE - /* first, propagate mode line changes to all instances of - a buffer displayed in more than one window */ - wp = wheadp; - while (wp != NULL) { - if (wp->w_flag & WFMODE) { - if (wp->w_bufp->b_nwnd > 1) { - /* make sure all previous windows have this */ - struct window *owp; - owp = wheadp; - while (owp != NULL) { - if (owp->w_bufp == wp->w_bufp) - owp->w_flag |= WFMODE; - owp = owp->w_wndp; - } - } - } - wp = wp->w_wndp; - } - +/* first, propagate mode line changes to all instances of a buffer displayed + * in more than one window */ + window_p wp ; + for( wp = wheadp ; wp != NULL ; wp = wp->w_wndp) + if( wp->w_flag & WFMODE + && wp->w_bufp->b_nwnd > 1) + /* make sure all previous windows have this */ + for( window_p owp = wheadp ; owp != NULL ; owp = owp->w_wndp) + if( owp->w_bufp == wp->w_bufp) + owp->w_flag |= WFMODE ; #endif - /* update any windows that need refreshing */ - wp = wheadp; - while (wp != NULL) { - if (wp->w_flag) { - /* if the window has changed, service it */ - reframe(wp); /* check the framing */ +/* update any windows that need refreshing */ + for( wp = wheadp ; wp != NULL ; wp = wp->w_wndp) + if( wp->w_flag) { + /* if the window has changed, service it */ + reframe( wp) ; /* check the framing */ #if SCROLLCODE - if (wp->w_flag & (WFKILLS | WFINS)) { - scrflags |= - (wp->w_flag & (WFINS | WFKILLS)); - wp->w_flag &= ~(WFKILLS | WFINS); + if( wp->w_flag & (WFKILLS | WFINS)) { + scrflags |= wp->w_flag & (WFINS | WFKILLS) ; + wp->w_flag &= ~(WFKILLS | WFINS) ; } #endif - if ((wp->w_flag & ~WFMODE) == WFEDIT) - updone(wp); /* update EDITed line */ - else if (wp->w_flag & ~WFMOVE) - updall(wp); /* update all lines */ + if( (wp->w_flag & ~WFMODE) == WFEDIT) + updone( wp) ; /* update EDITed line */ + else if( wp->w_flag & ~WFMOVE) + updall( wp) ; /* update all lines */ #if SCROLLCODE - if (scrflags || (wp->w_flag & WFMODE)) + if( scrflags || (wp->w_flag & WFMODE)) #else - if (wp->w_flag & WFMODE) + if( wp->w_flag & WFMODE) #endif - modeline(wp); /* update modeline */ - wp->w_flag = 0; - wp->w_force = 0; + modeline( wp) ; /* update modeline */ + wp->w_flag = 0 ; + wp->w_force = 0 ; } - /* on to the next window */ - wp = wp->w_wndp; - } /* recalc the current hardware cursor location */ - updpos(); + updpos() ; #if MEMMAP && ! SCROLLCODE /* update the cursor and flush the buffers */ @@ -371,9 +345,8 @@ int update( boolean force_f) { * check to see if the cursor is on in the window * and re-frame it if needed or wanted */ -static int reframe(struct window *wp) -{ - struct line *lp, *lp0; +static int reframe( window_p wp) { + line_p lp, lp0 ; int i = 0; /* if not a requested reframe, check for a needed one */ @@ -455,7 +428,7 @@ static int reframe(struct window *wp) return TRUE; } -static void show_line(struct line *lp) +static void show_line( line_p lp) { int i = 0, len = llength(lp); @@ -470,11 +443,11 @@ static void show_line(struct line *lp) * updone: * update the current line to the virtual screen * - * struct window *wp; window to update current line in + * window_p wp; window to update current line in */ -static void updone(struct window *wp) +static void updone(window_p wp) { - struct line *lp; /* line to update */ + line_p lp; /* line to update */ int sline; /* physical screen line to update */ /* search down the line we want */ @@ -488,7 +461,7 @@ static void updone(struct window *wp) /* and update the virtual line */ vscreen[sline]->v_flag |= VFCHG; vscreen[sline]->v_flag &= ~VFREQ; - vtmove(sline, 0); + vtmove( sline, 0) ; show_line(lp); #if COLOR vscreen[sline]->v_rfcolor = wp->w_fcolor; @@ -501,11 +474,11 @@ static void updone(struct window *wp) * updall: * update all the lines in a window on the virtual screen * - * struct window *wp; window to update lines in + * window_p wp; window to update lines in */ -static void updall(struct window *wp) +static void updall(window_p wp) { - struct line *lp; /* line to update */ + line_p lp; /* line to update */ int sline; /* physical screen line to update */ /* search down the lines, updating them */ @@ -516,7 +489,7 @@ static void updall(struct window *wp) /* and update the virtual line */ vscreen[sline]->v_flag |= VFCHG; vscreen[sline]->v_flag &= ~VFREQ; - vtmove(sline, 0); + vtmove( sline, 0) ; if (lp != wp->w_bufp->b_linep) { /* if we are not at the end */ show_line(lp); @@ -534,49 +507,43 @@ static void updall(struct window *wp) } -/* - * updpos: - * update the position of the hardware cursor and handle extended - * lines. This is the only update for simple moves. - */ -void updpos(void) -{ - struct line *lp; - int i; - /* find the current row */ - lp = curwp->w_linep; - currow = curwp->w_toprow; - while (lp != curwp->w_dotp) { - ++currow; - lp = lforw(lp); +/* updpos: + update the position of the hardware cursor and handle extended lines. + This is the only update for simple moves. + */ +void updpos(void) { +/* find the current row */ + line_p lp = curwp->w_linep ; + currow = curwp->w_toprow ; + while( lp != curwp->w_dotp) { + ++currow ; + lp = lforw( lp) ; } - /* find the current column */ +/* find the current column */ curcol = 0; - i = 0; - while (i < curwp->w_doto) { - unicode_t c; + int i = 0 ; + while( i < curwp->w_doto) { + unicode_t c ; i += utf8_to_unicode( lp->l_text, i, curwp->w_doto, &c) ; if( c == '\t') curcol += tabwidth - curcol % tabwidth ; else if( c < 0x20 || c == 0x7F) - curcol += 2 ; /* displayed as ^c */ + curcol += 2 ; /* displayed as ^c */ else if( c >= 0x80 && c <= 0xA0) - curcol += 3 ; /* displayed as \xx */ - else { - int width = utf8_width( c) ; - curcol += (width < 0) ? 2 : width ; /* non printable are displayed as \u */ - } + curcol += 3 ; /* displayed as \xx */ + else + curcol += utf8_width( c) ; /* non printable are displayed as \u */ } - /* if extended, flag so and update the virtual line image */ - if (curcol >= term.t_ncol - 1) { - vscreen[currow]->v_flag |= (VFEXT | VFCHG); - updext(); +/* if extended, flag so and update the virtual line image */ + if( curcol >= term.t_ncol - 1) { + vscreen[ currow]->v_flag |= (VFEXT | VFCHG) ; + updext() ; } else - lbound = 0; + lbound = 0 ; } /* @@ -585,8 +552,8 @@ void updpos(void) */ void upddex(void) { - struct window *wp; - struct line *lp; + window_p wp; + line_p lp; int i; wp = wheadp; @@ -599,7 +566,7 @@ void upddex(void) if (vscreen[i]->v_flag & VFEXT) { if ((wp != curwp) || (lp != wp->w_dotp) || (curcol < term.t_ncol - 1)) { - vtmove(i, 0); + vtmove( i, 0) ; show_line(lp); vteeol(); @@ -659,7 +626,7 @@ void updgar(void) */ int updupd(int force) { - struct video *vp1; + video_p vp1; int i; #if SCROLLCODE @@ -697,8 +664,8 @@ int updupd(int force) */ static int scrolls(int inserts) { /* returns true if it does something */ - struct video *vpv; /* virtual screen image */ - struct video *vpp; /* physical screen image */ + video_p vpv; /* virtual screen image */ + video_p vpp; /* physical screen image */ int i, j, k; int rows, cols; int first, match, count, target, end; @@ -834,8 +801,8 @@ static void scrscroll(int from, int to, int count) */ static int texttest(int vrow, int prow) { - struct video *vpv = vscreen[vrow]; /* virtual screen image */ - struct video *vpp = pscreen[prow]; /* physical screen image */ + video_p vpv = vscreen[vrow]; /* virtual screen image */ + video_p vpp = pscreen[prow]; /* physical screen image */ return !memcmp(vpv->v_text, vpp->v_text, 4*term.t_ncol); } @@ -864,7 +831,7 @@ static int endofline(unicode_t *s, int n) static void updext(void) { int rcursor; /* real cursor location */ - struct line *lp; /* pointer to current line */ + line_p lp; /* pointer to current line */ /* calculate what column the real cursor will end up in */ rcursor = ((curcol - term.t_ncol) % term.t_scrsiz) + term.t_margin; @@ -872,7 +839,7 @@ static void updext(void) /* scan through the line outputing characters to the virtual screen */ /* once we reach the left edge */ - vtmove(currow, -lbound); /* start scanning offscreen */ + vtmove( currow, -lbound) ; /* start scanning offscreen */ lp = curwp->w_dotp; /* line to output */ show_line(lp); @@ -892,7 +859,7 @@ static void updext(void) #if MEMMAP /* UPDATELINE specific code for the IBM-PC and other compatables */ -static int updateline(int row, struct video *vp1, struct video *vp2) +static int updateline(int row, video_p vp1, video_p vp2) { #if SCROLLCODE unicode_t *cp1; @@ -925,15 +892,13 @@ static int updateline(int row, struct video *vp1, struct video *vp2) #else -/* - * updateline() +/* updateline() * - * int row; row of screen to update - * struct video *vp1; virtual screen image - * struct video *vp2; physical screen image + * int row ; row of screen to update + * video_p vp1 ; virtual screen image + * video_p vp2 ; physical screen image */ -static int updateline(int row, struct video *vp1, struct video *vp2) -{ +static int updateline(int row, video_p vp1, video_p vp2) { /* UPDATELINE code for all other versions */ unicode_t *cp1; @@ -1069,10 +1034,9 @@ static int updateline(int row, struct video *vp1, struct video *vp2) * change the modeline format by hacking at this routine. Called by "update" * any time there is a dirty window. */ -static void modeline(struct window *wp) -{ +static void modeline( window_p wp) { int n; /* cursor position count */ - struct buffer *bp; + buffer_p bp ; int i; /* loop index */ int lchar; /* character to draw line in buffer with */ int firstm; /* is this the first mode? */ @@ -1083,7 +1047,7 @@ static void modeline(struct window *wp) vscreen[n]->v_rfcolor = 0; /* black on */ vscreen[n]->v_rbcolor = 7; /* white..... */ #endif - vtmove(n, 0); /* Seek to right line. */ + vtmove( n, 0) ; /* Seek to right line. */ if (wp == curwp) /* mark the current buffer */ #if PKCODE && REVSTA lchar = '-'; @@ -1148,7 +1112,7 @@ static void modeline(struct window *wp) } { /* determine if top line, bottom line, or both are visible */ - struct line *lp = wp->w_linep; + line_p lp = wp->w_linep; int rows = wp->w_ntrows; char *msg = NULL; char tline[ 6] ; /* buffer for part of mode line */ @@ -1172,7 +1136,7 @@ static void modeline(struct window *wp) } } if (!msg) { - struct line *lp; + line_p lp; int numlines, predlines ; lp = lforw(bp->b_linep); @@ -1215,7 +1179,7 @@ static void modeline(struct window *wp) void upmode(void) { /* update all the mode lines */ - struct window *wp; + window_p wp; wp = wheadp; while (wp != NULL) { diff --git a/random.c b/random.c index aa7295b..4f2f3be 100644 --- a/random.c +++ b/random.c @@ -164,8 +164,7 @@ int getccol( int bflg) { else if (c >= 0x80 && c <= 0xa0) /* displayed as \xx */ col += 3 ; else { - int w = utf8_width( c) ; /* incomplete wc_width */ - col += (w < 0) ? 2 : w ; + col += utf8_width( c) ; } } diff --git a/utf8.c b/utf8.c index 81a8513..10cf20c 100644 --- a/utf8.c +++ b/utf8.c @@ -7,10 +7,9 @@ #include #include -/* - * Display width of UTF-8 character - */ -int utf8_width( unicode_t c) { + +/* Display width of UTF-8 character */ +int _utf8_width( unicode_t c) { #if CYGWIN assert( sizeof( wchar_t) == 2) ; /* wcwidth only supports UTF-16 */ return (c < 0x10000) ? wcwidth( (wchar_t) c) : -1 ; @@ -19,8 +18,14 @@ int utf8_width( unicode_t c) { #endif } -/* - * utf8_to_unicode() + +int utf8_width( unicode_t c) { + int w = _utf8_width( c) ; + return (w < 0) ? 2 : w ; /* display \u if can't figure out width */ +} + + +/* utf8_to_unicode() * * Convert a UTF-8 sequence to its unicode value, and return the length of * the sequence in bytes. @@ -84,8 +89,8 @@ unsigned utf8_to_unicode( const char *line, unsigned index, unsigned len, return bytes; } -/* - * unicode_to_utf8() + +/* unicode_to_utf8() * * Convert a unicode value to its canonical utf-8 sequence. * diff --git a/utf8.h b/utf8.h index 93a95fc..4939459 100644 --- a/utf8.h +++ b/utf8.h @@ -4,7 +4,8 @@ typedef unsigned int unicode_t ; -int utf8_width( unicode_t c) ; +int _utf8_width( unicode_t c) ; /* straight width */ +int utf8_width( unicode_t c) ; /* workaround width */ unsigned utf8_to_unicode( const char *line, unsigned index, unsigned len, unicode_t *res) ; unsigned utf8_revdelta( unsigned char *buf, unsigned pos) ; From 79c3dfa4d9cccbf19df4cb37a39ca21c21f512f0 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sun, 15 Aug 2021 11:22:32 +0800 Subject: [PATCH 36/37] Refactoring display primitives. --- display.c | 1051 ++++++++++++++++++++++++++--------------------------- display.h | 6 +- 2 files changed, 507 insertions(+), 550 deletions(-) diff --git a/display.c b/display.c index 11bb8e8..fcf0126 100644 --- a/display.c +++ b/display.c @@ -49,7 +49,7 @@ static video_p *vscreen ; /* Virtual screen. */ static video_p *pscreen ; /* Physical screen. */ #endif -static int displaying = TRUE; +static int displaying = TRUE ; #if UNIX # include #endif @@ -76,19 +76,23 @@ int disinp = TRUE ; /* display input characters (echo) */ static int reframe( window_p wp) ; static void updone( window_p wp) ; static void updall( window_p wp) ; -static int scrolls(int inserts); -static void scrscroll(int from, int to, int count); -static int texttest(int vrow, int prow); -static int endofline(unicode_t *s, int n); -static void updext(void); -static int updateline(int row, video_p vp1, video_p vp2); +static int scrolls( int inserts) ; +static void scrscroll( int from, int to, int count) ; +static int texttest( int vrow, int prow) ; +static int endofline( unicode_t *s, int n) ; +static void upddex( void) ; +static void updext( void) ; +static void updgar( void) ; +static void updpos( void) ; +static int updateline( int row, video_p vp1, video_p vp2) ; +static boolean updupd( boolean force_f) ; static void modeline( window_p wp) ; -static void mlputi(int i, int r); -static void mlputli(long l, int r); -static void mlputf(int s); +static void mlputi( int i, int r) ; +static void mlputli( long l, int r) ; +static void mlputf( int s) ; static void mlputs( const char *s) ; #if SIGWINCH -static int newscreensize(int h, int w); +static int newscreensize( int h, int w) ; #endif @@ -101,7 +105,7 @@ static int newscreensize(int h, int w); void vtinit( void) { TTopen() ; /* open the screen */ TTkopen() ; /* open the keyboard */ - TTrev(FALSE) ; + TTrev( FALSE) ; vscreen = xmalloc( term.t_maxrow * sizeof( video_p )) ; #if MEMMAP == 0 || SCROLLCODE @@ -126,7 +130,7 @@ void vtinit( void) { #if CLEAN /* free up all the dynamically allocated video structures */ void vtfree( void) { - for( int i = 0 ; i < term.t_maxrow; ++i ) { + for( int i = 0 ; i < term.t_maxrow ; ++i ) { free( vscreen[ i]) ; #if MEMMAP == 0 || SCROLLCODE free( pscreen[ i]) ; @@ -202,7 +206,7 @@ static void vtputc( unicode_t c) { sane_vtputc( '^') ; sane_vtputc( c ^ 0x40) ; } else if( c >= 0x80 && c <= 0xA0) { - static const char hex[] = "0123456789abcdef"; + static const char hex[] = "0123456789abcdef" ; sane_vtputc( '\\') ; sane_vtputc( hex[ c >> 4]) ; sane_vtputc( hex[ c & 15]) ; @@ -227,8 +231,8 @@ static int vtputs( const char *s) { return n ; } -/* - * Erase from the end of the software cursor to the end of the line on which + +/* Erase from the end of the software cursor to the end of the line on which * the software cursor is located. */ static void vteeol( void) { @@ -248,7 +252,7 @@ BINDABLE( upscreen) { } #if SCROLLCODE -static int scrflags; +static int scrflags ; #endif @@ -260,17 +264,17 @@ static int scrflags; boolean force_f ; force update past type ahead? */ -int update( boolean force_f) { +boolean update( boolean force_f) { #if TYPEAH && ! PKCODE if( force_f == FALSE && typahead()) - return TRUE; + return TRUE ; #endif #if VISMAC == 0 if( force_f == FALSE && kbdmode == PLAY) - return TRUE; + return TRUE ; #endif - displaying = TRUE; + displaying = TRUE ; #if SCROLLCODE @@ -316,195 +320,182 @@ int update( boolean force_f) { #if MEMMAP && ! SCROLLCODE /* update the cursor and flush the buffers */ - movecursor(currow, curcol - lbound); + movecursor( currow, curcol - lbound) ; #endif /* check for lines to de-extend */ - upddex(); + upddex() ; /* if screen is garbage, re-plot it */ - if (sgarbf != FALSE) - updgar(); + if( sgarbf != FALSE) + updgar() ; /* update the virtual screen to the physical screen */ updupd( force_f) ; /* update the cursor and flush the buffers */ - movecursor(currow, curcol - lbound); - TTflush(); - displaying = FALSE; + movecursor( currow, curcol - lbound) ; + TTflush() ; + displaying = FALSE ; #if SIGWINCH - while (chg_width || chg_height) - newscreensize(chg_height, chg_width); + while( chg_width || chg_height) + newscreensize( chg_height, chg_width) ; #endif - return TRUE; + return TRUE ; } -/* - * reframe: + +/* reframe: * check to see if the cursor is on in the window * and re-frame it if needed or wanted */ static int reframe( window_p wp) { line_p lp, lp0 ; - int i = 0; + int i = 0 ; /* if not a requested reframe, check for a needed one */ - if ((wp->w_flag & WFFORCE) == 0) { + if( (wp->w_flag & WFFORCE) == 0) { #if SCROLLCODE /* loop from one line above the window to one line after */ - lp = wp->w_linep; - lp0 = lback(lp); - if (lp0 == wp->w_bufp->b_linep) - i = 0; + lp = wp->w_linep ; + lp0 = lback( lp) ; + if( lp0 == wp->w_bufp->b_linep) + i = 0 ; else { - i = -1; - lp = lp0; + i = -1 ; + lp = lp0 ; } - for (; i <= (int) (wp->w_ntrows); i++) + for( ; i <= (int) (wp->w_ntrows) ; i++) #else - lp = wp->w_linep; - for (i = 0; i < wp->w_ntrows; i++) + lp = wp->w_linep ; + for( i = 0 ; i < wp->w_ntrows ; i++) #endif { /* if the line is in the window, no reframe */ - if (lp == wp->w_dotp) { + if( lp == wp->w_dotp) { #if SCROLLCODE /* if not _quite_ in, we'll reframe gently */ - if (i < 0 || i == wp->w_ntrows) { + if( i < 0 || i == wp->w_ntrows) { /* if the terminal can't help, then we're simply outside */ - if (term.t_scroll == NULL) - i = wp->w_force; - break; + if( term.t_scroll == NULL) + i = wp->w_force ; + break ; } #endif - return TRUE; + return TRUE ; } /* if we are at the end of the file, reframe */ - if (lp == wp->w_bufp->b_linep) - break; + if( lp == wp->w_bufp->b_linep) + break ; /* on to the next line */ - lp = lforw(lp); + lp = lforw( lp) ; } } #if SCROLLCODE - if (i == -1) { /* we're just above the window */ - i = scrollcount; /* put dot at first line */ - scrflags |= WFINS; - } else if (i == wp->w_ntrows) { /* we're just below the window */ - i = -scrollcount; /* put dot at last line */ - scrflags |= WFKILLS; + if( i == -1) { /* we're just above the window */ + i = scrollcount ; /* put dot at first line */ + scrflags |= WFINS ; + } else if( i == wp->w_ntrows) { /* we're just below the window */ + i = -scrollcount ; /* put dot at last line */ + scrflags |= WFKILLS ; } else /* put dot where requested */ #endif - i = wp->w_force; /* (is 0, unless reposition() was called) */ + i = wp->w_force ; /* (is 0, unless reposition() was called) */ - wp->w_flag |= WFMODE; + wp->w_flag |= WFMODE ; /* how far back to reframe? */ - if (i > 0) { /* only one screen worth of lines max */ - if (--i >= wp->w_ntrows) - i = wp->w_ntrows - 1; - } else if (i < 0) { /* negative update???? */ - i += wp->w_ntrows; - if (i < 0) - i = 0; + if( i > 0) { /* only one screen worth of lines max */ + if( --i >= wp->w_ntrows) + i = wp->w_ntrows - 1 ; + } else if( i < 0) { /* negative update???? */ + i += wp->w_ntrows ; + if( i < 0) + i = 0 ; } else - i = wp->w_ntrows / 2; + i = wp->w_ntrows / 2 ; /* backup to new line at top of window */ - lp = wp->w_dotp; - while (i != 0 && lback(lp) != wp->w_bufp->b_linep) { - --i; - lp = lback(lp); + lp = wp->w_dotp ; + while( i != 0 && lback( lp) != wp->w_bufp->b_linep) { + --i ; + lp = lback( lp) ; } /* and reset the current line at top of window */ - wp->w_linep = lp; - wp->w_flag |= WFHARD; - wp->w_flag &= ~WFFORCE; - return TRUE; + wp->w_linep = lp ; + wp->w_flag |= WFHARD ; + wp->w_flag &= ~WFFORCE ; + return TRUE ; } -static void show_line( line_p lp) -{ - int i = 0, len = llength(lp); +static void show_line( line_p lp) { + int i = 0, len = llength( lp) ; - while (i < len) { - unicode_t c; - i += utf8_to_unicode(lp->l_text, i, len, &c); - vtputc(c); + while( i < len) { + unicode_t c ; + i += utf8_to_unicode( lp->l_text, i, len, &c) ; + vtputc( c) ; } } -/* - * updone: + +/* updone: * update the current line to the virtual screen * * window_p wp; window to update current line in */ -static void updone(window_p wp) -{ - line_p lp; /* line to update */ - int sline; /* physical screen line to update */ +static void updone( window_p wp) { +/* search down the line we want */ + int sline = wp->w_toprow ; /* physical screen line to update */ + line_p lp ; + for( lp = wp->w_linep ; lp != wp->w_dotp ; lp = lforw( lp)) + ++sline ; - /* search down the line we want */ - lp = wp->w_linep; - sline = wp->w_toprow; - while (lp != wp->w_dotp) { - ++sline; - lp = lforw(lp); - } - - /* and update the virtual line */ - vscreen[sline]->v_flag |= VFCHG; - vscreen[sline]->v_flag &= ~VFREQ; +/* and update the virtual line */ + vscreen[ sline]->v_flag |= VFCHG ; + vscreen[ sline]->v_flag &= ~VFREQ ; vtmove( sline, 0) ; - show_line(lp); + show_line( lp) ; #if COLOR - vscreen[sline]->v_rfcolor = wp->w_fcolor; - vscreen[sline]->v_rbcolor = wp->w_bcolor; + vscreen[ sline]->v_rfcolor = wp->w_fcolor ; + vscreen[ sline]->v_rbcolor = wp->w_bcolor ; #endif - vteeol(); + vteeol() ; } -/* - * updall: + +/* updall: * update all the lines in a window on the virtual screen * * window_p wp; window to update lines in */ -static void updall(window_p wp) -{ - line_p lp; /* line to update */ - int sline; /* physical screen line to update */ - - /* search down the lines, updating them */ - lp = wp->w_linep; - sline = wp->w_toprow; - while (sline < wp->w_toprow + wp->w_ntrows) { - - /* and update the virtual line */ - vscreen[sline]->v_flag |= VFCHG; - vscreen[sline]->v_flag &= ~VFREQ; +static void updall( window_p wp) { +/* search down the lines, updating them */ + line_p lp = wp->w_linep ; /* line to update */ + int sline = wp->w_toprow ; /* physical screen line to update */ + while( sline < wp->w_toprow + wp->w_ntrows) { + /* and update the virtual line */ + vscreen[ sline]->v_flag |= VFCHG ; + vscreen[ sline]->v_flag &= ~VFREQ ; vtmove( sline, 0) ; - if (lp != wp->w_bufp->b_linep) { - /* if we are not at the end */ - show_line(lp); - lp = lforw(lp); + if( lp != wp->w_bufp->b_linep) { + /* if we are not at the end */ + show_line( lp) ; + lp = lforw( lp) ; } - /* on to the next one */ + /* on to the next one */ #if COLOR - vscreen[sline]->v_rfcolor = wp->w_fcolor; - vscreen[sline]->v_rbcolor = wp->w_bcolor; + vscreen[ sline]->v_rfcolor = wp->w_fcolor ; + vscreen[ sline]->v_rbcolor = wp->w_bcolor ; #endif - vteeol(); - ++sline; + vteeol() ; + ++sline ; } - } @@ -512,7 +503,7 @@ static void updall(window_p wp) update the position of the hardware cursor and handle extended lines. This is the only update for simple moves. */ -void updpos(void) { +static void updpos( void) { /* find the current row */ line_p lp = curwp->w_linep ; currow = curwp->w_toprow ; @@ -522,7 +513,7 @@ void updpos(void) { } /* find the current column */ - curcol = 0; + curcol = 0 ; int i = 0 ; while( i < curwp->w_doto) { unicode_t c ; @@ -546,348 +537,322 @@ void updpos(void) { lbound = 0 ; } -/* - * upddex: + +/* upddex: * de-extend any line that deserves it */ -void upddex(void) -{ - window_p wp; - line_p lp; - int i; - - wp = wheadp; - - while (wp != NULL) { - lp = wp->w_linep; - i = wp->w_toprow; - - while (i < wp->w_toprow + wp->w_ntrows) { - if (vscreen[i]->v_flag & VFEXT) { - if ((wp != curwp) || (lp != wp->w_dotp) || - (curcol < term.t_ncol - 1)) { +static void upddex( void) { + for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp) { + line_p lp = wp->w_linep ; + for( int i = wp->w_toprow ; i < wp->w_toprow + wp->w_ntrows ; i++) { + if( vscreen[ i]->v_flag & VFEXT) { + if( (wp != curwp) + || (lp != wp->w_dotp) + || (curcol < term.t_ncol - 1)) { vtmove( i, 0) ; - show_line(lp); - vteeol(); + show_line( lp) ; + vteeol() ; - /* this line no longer is extended */ - vscreen[i]->v_flag &= ~VFEXT; - vscreen[i]->v_flag |= VFCHG; + /* this line no longer is extended */ + vscreen[ i]->v_flag &= ~VFEXT ; + vscreen[ i]->v_flag |= VFCHG ; } } - lp = lforw(lp); - ++i; + + lp = lforw( lp) ; } - /* and onward to the next window */ - wp = wp->w_wndp; } } -/* - * updgar: + +/* updgar: * if the screen is garbage, clear the physical screen and * the virtual screen and force a full update */ -void updgar(void) -{ - unicode_t *txt; - int i, j; - - for (i = 0; i < term.t_nrow; ++i) { - vscreen[i]->v_flag |= VFCHG; +static void updgar( void) { + for( int i = 0 ; i < term.t_nrow ; ++i) { + vscreen[ i]->v_flag |= VFCHG ; #if REVSTA - vscreen[i]->v_flag &= ~VFREV; + vscreen[ i]->v_flag &= ~VFREV ; #endif #if COLOR - vscreen[i]->v_fcolor = gfcolor; - vscreen[i]->v_bcolor = gbcolor; + vscreen[ i]->v_fcolor = gfcolor ; + vscreen[ i]->v_bcolor = gbcolor ; #endif #if MEMMAP == 0 || SCROLLCODE - txt = pscreen[i]->v_text; - for (j = 0; j < term.t_ncol; ++j) - txt[j] = ' '; + unicode_t *txt = pscreen[ i]->v_text ; + for( int j = 0 ; j < term.t_ncol ; ++j) + txt[ j] = ' ' ; #endif } - movecursor(0, 0); /* Erase the screen. */ - (*term.t_eeop) (); - sgarbf = FALSE; /* Erase-page clears */ - mpresf = FALSE; /* the message area. */ + movecursor( 0, 0) ; /* Erase the screen. */ + term.t_eeop() ; + sgarbf = FALSE ; /* Erase-page clears */ + mpresf = FALSE ; /* the message area. */ #if COLOR - mlerase(); /* needs to be cleared if colored */ + mlerase() ; /* needs to be cleared if colored */ #endif } -/* - * updupd: + +/* updupd: * update the physical screen from the virtual screen * * int force; forced update flag */ -int updupd(int force) -{ - video_p vp1; - int i; - +static boolean updupd( boolean force_f) { #if SCROLLCODE - if (scrflags & WFKILLS) - scrolls(FALSE); - if (scrflags & WFINS) - scrolls(TRUE); - scrflags = 0; + if( scrflags & WFKILLS) + scrolls( FALSE) ; + + if( scrflags & WFINS) + scrolls( TRUE) ; + + scrflags = 0 ; #endif - for (i = 0; i < term.t_nrow; ++i) { - vp1 = vscreen[i]; + for( int i = 0 ; i < term.t_nrow ; ++i) { + video_p vp1 = vscreen[ i] ; - /* for each line that needs to be updated */ - if ((vp1->v_flag & VFCHG) != 0) { + /* for each line that needs to be updated */ + if( (vp1->v_flag & VFCHG) != 0) { #if TYPEAH && ! PKCODE - if (force == FALSE && typahead()) - return TRUE; + if( force_f == FALSE && typahead()) + return TRUE ; #endif #if MEMMAP && ! SCROLLCODE - updateline(i, vp1); + updateline( i, vp1) ; #else - updateline(i, vp1, pscreen[i]); + updateline( i, vp1, pscreen[ i]) ; #endif } } - return TRUE; + + return TRUE ; } #if SCROLLCODE -/* - * optimize out scrolls (line breaks, and newlines) +/* optimize out scrolls (line breaks, and newlines) * arg. chooses between looking for inserts or deletes + + * returns true if it does something */ -static int scrolls(int inserts) -{ /* returns true if it does something */ - video_p vpv; /* virtual screen image */ - video_p vpp; /* physical screen image */ - int i, j, k; - int rows, cols; - int first, match, count, target, end; - int longmatch, longcount; - int from, to; +static int scrolls( int inserts) { + int i, j, k ; + int count, target, end ; + int to ; - if (!term.t_scroll) /* no way to scroll */ - return FALSE; + if( !term.t_scroll) /* no way to scroll */ + return FALSE ; - rows = term.t_nrow; - cols = term.t_ncol; + int rows = term.t_nrow ; + int cols = term.t_ncol ; - first = -1; - for (i = 0; i < rows; i++) { /* find first wrong line */ - if (!texttest(i, i)) { - first = i; - break; - } - } + for( i = 0 ; i < rows ; i++) /* find first wrong line */ + if( !texttest( i, i)) + break ; - if (first < 0) - return FALSE; /* no text changes */ + if( i == rows) /* no text changes */ + return FALSE ; - vpv = vscreen[first]; - vpp = pscreen[first]; + int first = i ; - if (inserts) { - /* determine types of potential scrolls */ - end = endofline(vpv->v_text, cols); - if (end == 0) - target = first; /* newlines */ - else if (memcmp(vpp->v_text, vpv->v_text, 4*end) == 0) - target = first + 1; /* broken line newlines */ + video_p vpv = vscreen[ first] ; + video_p vpp = pscreen[ first] ; + + if( inserts) { + /* determine types of potential scrolls */ + end = endofline( vpv->v_text, cols) ; + if( end == 0) + target = first ; /* newlines */ + else if( memcmp( vpp->v_text, vpv->v_text, 4*end) == 0) + target = first + 1 ; /* broken line newlines */ else - target = first; + target = first ; } else { - target = first + 1; + target = first + 1 ; } /* find the matching shifted area */ - match = -1; - longmatch = -1; - longcount = 0; - from = target; - for (i = from + 1; i < rows - longcount /* P.K. */ ; i++) { - if (inserts ? texttest(i, from) : texttest(from, i)) { - match = i; - count = 1; - for (j = match + 1, k = from + 1; - j < rows && k < rows; j++, k++) { - if (inserts ? texttest(j, k) : - texttest(k, j)) - count++; + int match = -1 ; + int longmatch = -1 ; + int longcount = 0 ; + int from = target ; + for( i = from + 1 ; i < rows - longcount /* P.K. */ ; i++) { + if( inserts ? texttest( i, from) : texttest( from, i)) { + match = i ; + count = 1 ; + for( j = match + 1, k = from + 1 ; + j < rows && k < rows ; j++, k++) { + if( inserts ? texttest( j, k) : + texttest( k, j)) + count++ ; else - break; + break ; } - if (longcount < count) { - longcount = count; - longmatch = match; + if( longcount < count) { + longcount = count ; + longmatch = match ; } } } - match = longmatch; - count = longcount; - if (!inserts) { + match = longmatch ; + count = longcount ; + + if( !inserts) { /* full kill case? */ - if (match > 0 && texttest(first, match - 1)) { - target--; - match--; - count++; + if( match > 0 && texttest( first, match - 1)) { + target-- ; + match-- ; + count++ ; } } /* do the scroll */ - if (match > 0 && count > 2) { /* got a scroll */ + if( match > 0 && count > 2) { /* got a scroll */ /* move the count lines starting at target to match */ - if (inserts) { - from = target; - to = match; + if( inserts) { + from = target ; + to = match ; } else { - from = match; - to = target; + from = match ; + to = target ; } - if (2 * count < abs(from - to)) - return FALSE; - scrscroll(from, to, count); - for (i = 0; i < count; i++) { - vpp = pscreen[to + i]; - vpv = vscreen[to + i]; - memcpy(vpp->v_text, vpv->v_text, 4*cols); - vpp->v_flag = vpv->v_flag; /* XXX */ - if (vpp->v_flag & VFREV) { - vpp->v_flag &= ~VFREV; - vpp->v_flag |= ~VFREQ; + if( 2 * count < abs( from - to)) + return FALSE ; + scrscroll( from, to, count) ; + for( i = 0 ; i < count ; i++) { + vpp = pscreen[ to + i] ; + vpv = vscreen[ to + i] ; + memcpy( vpp->v_text, vpv->v_text, 4*cols) ; + vpp->v_flag = vpv->v_flag ; /* XXX */ + if( vpp->v_flag & VFREV) { + vpp->v_flag &= ~VFREV ; + vpp->v_flag |= ~VFREQ ; } #if MEMMAP - vscreen[to + i]->v_flag &= ~VFCHG; + vscreen[ to + i]->v_flag &= ~VFCHG ; #endif } - if (inserts) { - from = target; - to = match; + if( inserts) { + from = target ; + to = match ; } else { - from = target + count; - to = match + count; + from = target + count ; + to = match + count ; } #if MEMMAP == 0 - for (i = from; i < to; i++) { - unicode_t *txt; - txt = pscreen[i]->v_text; - for (j = 0; j < term.t_ncol; ++j) - txt[j] = ' '; - vscreen[i]->v_flag |= VFCHG; + for( i = from ; i < to ; i++) { + unicode_t *txt ; + txt = pscreen[ i]->v_text ; + for( j = 0 ; j < term.t_ncol ; ++j) + txt[ j] = ' ' ; + vscreen[ i]->v_flag |= VFCHG ; } #endif - return TRUE; + return TRUE ; } - return FALSE; + return FALSE ; } + /* move the "count" lines starting at "from" to "to" */ -static void scrscroll(int from, int to, int count) -{ - ttrow = ttcol = -1; - (*term.t_scroll) (from, to, count); +static void scrscroll( int from, int to, int count) { + ttrow = ttcol = -1 ; + term.t_scroll( from, to, count) ; } -/* - * return TRUE on text match + +/* return TRUE on text match * * int vrow, prow; virtual, physical rows */ -static int texttest(int vrow, int prow) -{ - video_p vpv = vscreen[vrow]; /* virtual screen image */ - video_p vpp = pscreen[prow]; /* physical screen image */ +static int texttest( int vrow, int prow) { + video_p vpv = vscreen[ vrow] ; /* virtual screen image */ + video_p vpp = pscreen[ prow] ; /* physical screen image */ - return !memcmp(vpv->v_text, vpp->v_text, 4*term.t_ncol); + return !memcmp( vpv->v_text, vpp->v_text, 4*term.t_ncol) ; } -/* - * return the index of the first blank of trailing whitespace - */ -static int endofline(unicode_t *s, int n) -{ - int i; - for (i = n - 1; i >= 0; i--) - if (s[i] != ' ') - return i + 1; - return 0; + +/* return the index of the first blank of trailing whitespace */ +static int endofline( unicode_t *s, int n) { + int i ; + for( i = n - 1 ; i >= 0 ; i--) + if( s[ i] != ' ') + return i + 1 ; + return 0 ; } #endif /* SCROLLCODE */ -/* - * updext: + +/* updext: * update the extended line which the cursor is currently * on at a column greater than the terminal width. The line * will be scrolled right or left to let the user see where * the cursor is */ -static void updext(void) -{ - int rcursor; /* real cursor location */ - line_p lp; /* pointer to current line */ +static void updext( void) { + int rcursor ; /* real cursor location */ + line_p lp ; /* pointer to current line */ /* calculate what column the real cursor will end up in */ - rcursor = ((curcol - term.t_ncol) % term.t_scrsiz) + term.t_margin; - taboff = lbound = curcol - rcursor + 1; + rcursor = ((curcol - term.t_ncol) % term.t_scrsiz) + term.t_margin ; + taboff = lbound = curcol - rcursor + 1 ; /* scan through the line outputing characters to the virtual screen */ /* once we reach the left edge */ vtmove( currow, -lbound) ; /* start scanning offscreen */ - lp = curwp->w_dotp; /* line to output */ - show_line(lp); + lp = curwp->w_dotp ; /* line to output */ + show_line( lp) ; /* truncate the virtual line, restore tab offset */ - vteeol(); - taboff = 0; + vteeol() ; + taboff = 0 ; /* and put a '$' in column 1 */ - vscreen[currow]->v_text[0] = '$'; + vscreen[ currow]->v_text[ 0] = '$' ; } -/* - * Update a single line. This does not know how to use insert or delete + +/* Update a single line. This does not know how to use insert or delete * character sequences; we are using VT52 functionality. Update the physical * row and column variables. It does try an exploit erase to end of line. */ #if MEMMAP /* UPDATELINE specific code for the IBM-PC and other compatables */ -static int updateline(int row, video_p vp1, video_p vp2) -{ +static int updateline( int row, video_p vp1, video_p vp2) { #if SCROLLCODE - unicode_t *cp1; - unicode_t *cp2; - int nch; + unicode_t *cp1 ; + unicode_t *cp2 ; + int nch ; - cp1 = &vp1->v_text[0]; - cp2 = &vp2->v_text[0]; - nch = term.t_ncol; + cp1 = &vp1->v_text[ 0] ; + cp2 = &vp2->v_text[ 0] ; + nch = term.t_ncol ; do { - *cp2 = *cp1; - ++cp2; - ++cp1; + *cp2 = *cp1 ; + ++cp2 ; + ++cp1 ; } - while (--nch); + while( --nch) ; #endif #if COLOR - scwrite(row, vp1->v_text, vp1->v_rfcolor, vp1->v_rbcolor); - vp1->v_fcolor = vp1->v_rfcolor; - vp1->v_bcolor = vp1->v_rbcolor; + scwrite( row, vp1->v_text, vp1->v_rfcolor, vp1->v_rbcolor) ; + vp1->v_fcolor = vp1->v_rfcolor ; + vp1->v_bcolor = vp1->v_rbcolor ; #else - if (vp1->v_flag & VFREQ) - scwrite(row, vp1->v_text, 0, 7); + if( vp1->v_flag & VFREQ) + scwrite( row, vp1->v_text, 0, 7) ; else - scwrite(row, vp1->v_text, 7, 0); + scwrite( row, vp1->v_text, 7, 0) ; #endif - vp1->v_flag &= ~(VFCHG | VFCOL); /* flag this line as changed */ - + vp1->v_flag &= ~(VFCHG | VFCOL) ; /* flag this line as changed */ } #else @@ -898,42 +863,42 @@ static int updateline(int row, video_p vp1, video_p vp2) * video_p vp1 ; virtual screen image * video_p vp2 ; physical screen image */ -static int updateline(int row, video_p vp1, video_p vp2) { +static int updateline( int row, video_p vp1, video_p vp2) { /* UPDATELINE code for all other versions */ - unicode_t *cp1; - unicode_t *cp2; - unicode_t *cp3; - unicode_t *cp4; - unicode_t *cp5; - int nbflag; /* non-blanks to the right flag? */ + unicode_t *cp1 ; + unicode_t *cp2 ; + unicode_t *cp3 ; + unicode_t *cp4 ; + unicode_t *cp5 ; + int nbflag ; /* non-blanks to the right flag? */ #if REVSTA - int rev; /* reverse video flag */ + int rev ; /* reverse video flag */ #endif int req = FALSE ; /* reverse video request flag */ /* set up pointers to virtual and physical lines */ - cp1 = &vp1->v_text[0]; - cp2 = &vp2->v_text[0]; + cp1 = &vp1->v_text[ 0] ; + cp2 = &vp2->v_text[ 0] ; #if COLOR - TTforg(vp1->v_rfcolor); - TTbacg(vp1->v_rbcolor); + TTforg( vp1->v_rfcolor) ; + TTbacg( vp1->v_rbcolor) ; #endif #if REVSTA | COLOR /* do a re-write of the entire line if it is reverse or there ** is a request to change the reverse status */ - rev = (vp1->v_flag & VFREV) == VFREV; - req = (vp1->v_flag & VFREQ) == VFREQ; + rev = (vp1->v_flag & VFREV) == VFREV ; + req = (vp1->v_flag & VFREQ) == VFREQ ; if( req || (req != rev) #if COLOR || (vp1->v_fcolor != vp1->v_rfcolor) || (vp1->v_bcolor != vp1->v_rbcolor) #endif ) { - movecursor(row, 0); /* Go to start of line. */ + movecursor( row, 0) ; /* Go to start of line. */ TTrev( req) ; /* set needed rev video state */ /* scan through the line and dump it to the screen and @@ -949,23 +914,23 @@ static int updateline(int row, video_p vp1, video_p vp2) { TTrev( FALSE) ; /* turn rev video off */ /* update the needed flags */ - vp1->v_flag &= ~VFCHG; - if (req) - vp1->v_flag |= VFREV; + vp1->v_flag &= ~VFCHG ; + if( req) + vp1->v_flag |= VFREV ; else - vp1->v_flag &= ~VFREV; + vp1->v_flag &= ~VFREV ; #if COLOR - vp1->v_fcolor = vp1->v_rfcolor; - vp1->v_bcolor = vp1->v_rbcolor; + vp1->v_fcolor = vp1->v_rfcolor ; + vp1->v_bcolor = vp1->v_rbcolor ; #endif - return TRUE; + return TRUE ; } #endif /* advance past any common chars at the left */ - while (cp1 != &vp1->v_text[term.t_ncol] && cp1[0] == cp2[0]) { - ++cp1; - ++cp2; + while( cp1 != &vp1->v_text[ term.t_ncol] && cp1[ 0] == cp2[ 0]) { + ++cp1 ; + ++cp2 ; } /* This can still happen, even though we only call this routine on changed @@ -975,94 +940,94 @@ static int updateline(int row, video_p vp1, video_p vp2) { * be hard operations that do a lot of update, so I don't really care. */ /* if both lines are the same, no update needs to be done */ - if (cp1 == &vp1->v_text[term.t_ncol]) { - vp1->v_flag &= ~VFCHG; /* flag this line is changed */ - return TRUE; + if( cp1 == &vp1->v_text[ term.t_ncol]) { + vp1->v_flag &= ~VFCHG ; /* flag this line is changed */ + return TRUE ; } /* find out if there is a match on the right */ - nbflag = FALSE; - cp3 = &vp1->v_text[term.t_ncol]; - cp4 = &vp2->v_text[term.t_ncol]; + nbflag = FALSE ; + cp3 = &vp1->v_text[ term.t_ncol] ; + cp4 = &vp2->v_text[ term.t_ncol] ; - while (cp3[-1] == cp4[-1]) { - --cp3; - --cp4; - if (cp3[0] != ' ') /* Note if any nonblank */ - nbflag = TRUE; /* in right match. */ + while( cp3[ -1] == cp4[ -1]) { + --cp3 ; + --cp4 ; + if( cp3[ 0] != ' ') /* Note if any nonblank */ + nbflag = TRUE ; /* in right match. */ } - cp5 = cp3; + cp5 = cp3 ; /* Erase to EOL ? */ - if (nbflag == FALSE && eolexist == TRUE && (req != TRUE)) { - while (cp5 != cp1 && cp5[-1] == ' ') - --cp5; + if( nbflag == FALSE && eolexist == TRUE && (req != TRUE)) { + while( cp5 != cp1 && cp5[ -1] == ' ') + --cp5 ; - if (cp3 - cp5 <= 3) /* Use only if erase is */ - cp5 = cp3; /* fewer characters. */ + if( cp3 - cp5 <= 3) /* Use only if erase is */ + cp5 = cp3 ; /* fewer characters. */ } - movecursor(row, cp1 - &vp1->v_text[0]); /* Go to start of line. */ + movecursor( row, cp1 - &vp1->v_text[ 0]) ; /* Go to start of line. */ #if REVSTA - TTrev(rev); + TTrev( rev) ; #endif - while (cp1 != cp5) { /* Ordinary. */ + while( cp1 != cp5) { /* Ordinary. */ unicode_t c = *cp1++ ; TTputc( c) ; ttcol += utf8_width( c) ; *cp2++ = c ; } - if (cp5 != cp3) { /* Erase. */ - TTeeol(); - while (cp1 != cp3) - *cp2++ = *cp1++; + if( cp5 != cp3) { /* Erase. */ + TTeeol() ; + while( cp1 != cp3) + *cp2++ = *cp1++ ; } #if REVSTA - TTrev(FALSE); + TTrev( FALSE) ; #endif - vp1->v_flag &= ~VFCHG; /* flag this line as updated */ - return TRUE; + vp1->v_flag &= ~VFCHG ; /* flag this line as updated */ + return TRUE ; } #endif -/* - * Redisplay the mode line for the window pointed to by the "wp". This is the + +/* Redisplay the mode line for the window pointed to by the "wp". This is the * only routine that has any idea of how the modeline is formatted. You can * change the modeline format by hacking at this routine. Called by "update" * any time there is a dirty window. */ static void modeline( window_p wp) { - int n; /* cursor position count */ + int n ; /* cursor position count */ buffer_p bp ; - int i; /* loop index */ - int lchar; /* character to draw line in buffer with */ - int firstm; /* is this the first mode? */ + int i ; /* loop index */ + int lchar ; /* character to draw line in buffer with */ + int firstm ; /* is this the first mode? */ - n = wp->w_toprow + wp->w_ntrows; /* Location. */ - vscreen[n]->v_flag |= VFCHG | VFREQ | VFCOL; /* Redraw next time. */ + n = wp->w_toprow + wp->w_ntrows ; /* Location. */ + vscreen[ n]->v_flag |= VFCHG | VFREQ | VFCOL ; /* Redraw next time. */ #if COLOR - vscreen[n]->v_rfcolor = 0; /* black on */ - vscreen[n]->v_rbcolor = 7; /* white..... */ + vscreen[ n]->v_rfcolor = 0 ; /* black on */ + vscreen[ n]->v_rbcolor = 7 ; /* white..... */ #endif vtmove( n, 0) ; /* Seek to right line. */ - if (wp == curwp) /* mark the current buffer */ + if( wp == curwp) /* mark the current buffer */ #if PKCODE && REVSTA - lchar = '-'; + lchar = '-' ; #else - lchar = '='; + lchar = '=' ; #endif else #if REVSTA - if (revexist) - lchar = ' '; + if( revexist) + lchar = ' ' ; else #endif - lchar = '-'; + lchar = '-' ; - bp = wp->w_bufp; + bp = wp->w_bufp ; vtputc( ((bp->b_flag & BFTRUNC) != 0) ? '#' : lchar) ; /* truncated? */ vtputc( ((bp->b_flag & BFCHG) != 0) ? '*' : lchar) ; /* changed? */ vtputc( ' ') ; @@ -1077,15 +1042,15 @@ static void modeline( window_p wp) { /* display the modes */ - if ((bp->b_flag & BFTRUNC) != 0) { - firstm = FALSE; + if( (bp->b_flag & BFTRUNC) != 0) { + firstm = FALSE ; n += vtputs( "Truncated") ; } else firstm = TRUE ; - for (i = 0; i < NUMMODES; i++) /* add in the mode flags */ - if (wp->w_bufp->b_mode & (1 << i)) { - if (firstm != TRUE) + for( i = 0 ; i < NUMMODES ; i++) /* add in the mode flags */ + if( wp->w_bufp->b_mode & (1 << i)) { + if( firstm != TRUE) n += vtputs( " ") ; else firstm = FALSE ; @@ -1096,69 +1061,68 @@ static void modeline( window_p wp) { n += vtputs( ") ") ; #if PKCODE - if (bp->b_fname[0] != 0 && strcmp(bp->b_bname, bp->b_fname) != 0) { + if( bp->b_fname[ 0] != 0 && strcmp( bp->b_bname, bp->b_fname) != 0) { #else - if (bp->b_fname[0] != 0) { /* File name. */ + if( bp->b_fname[ 0] != 0) { /* File name. */ n += vtputs( "File: ") ; #endif n += vtputs( bp->b_fname) ; - vtputc(' '); - ++n; + vtputc(' ') ; + ++n ; } - while (n < term.t_ncol) { /* Pad to full width. */ - vtputc(lchar); - ++n; + while( n < term.t_ncol) { /* Pad to full width. */ + vtputc( lchar) ; + ++n ; } { /* determine if top line, bottom line, or both are visible */ - line_p lp = wp->w_linep; - int rows = wp->w_ntrows; - char *msg = NULL; + line_p lp = wp->w_linep ; + int rows = wp->w_ntrows ; + char *msg = NULL ; char tline[ 6] ; /* buffer for part of mode line */ vtcol -= 7 ; /* strlen(" top ") plus a couple */ - while (rows--) { - lp = lforw(lp); - if (lp == wp->w_bufp->b_linep) { - msg = " Bot "; - break; + while( rows--) { + lp = lforw( lp) ; + if( lp == wp->w_bufp->b_linep) { + msg = " Bot " ; + break ; } } - if (lback(wp->w_linep) == wp->w_bufp->b_linep) { - if (msg) { - if (wp->w_linep == wp->w_bufp->b_linep) - msg = " Emp "; + if( lback( wp->w_linep) == wp->w_bufp->b_linep) { + if( msg) { + if( wp->w_linep == wp->w_bufp->b_linep) + msg = " Emp " ; else - msg = " All "; + msg = " All " ; } else { - msg = " Top "; + msg = " Top " ; } } - if (!msg) { - line_p lp; + if( !msg) { + line_p lp ; int numlines, predlines ; - lp = lforw(bp->b_linep); - numlines = 0; - predlines = 0; - while (lp != bp->b_linep) { - if (lp == wp->w_linep) { - predlines = numlines; + lp = lforw( bp->b_linep) ; + numlines = 0 ; + predlines = 0 ; + while( lp != bp->b_linep) { + if( lp == wp->w_linep) { + predlines = numlines ; } - ++numlines; - lp = lforw(lp); + ++numlines ; + lp = lforw( lp) ; } - if (wp->w_dotp == bp->b_linep) { - msg = " Bot "; + if( wp->w_dotp == bp->b_linep) { + msg = " Bot " ; } else { int ratio = 0 ; - if (numlines != 0) - ratio = - (100L * predlines) / numlines; - if (ratio > 99) - ratio = 99; + if( numlines != 0) + ratio = (100L * predlines) / numlines ; + if( ratio > 99) + ratio = 99 ; tline[ 0] = ' ' ; tline[ 1] = ratio / 10 + '0' ; @@ -1169,7 +1133,7 @@ static void modeline( window_p wp) { if( tline[ 1] == '0') tline[ 1] = ' ' ; - msg = tline; + msg = tline ; } } @@ -1177,33 +1141,31 @@ static void modeline( window_p wp) { } } -void upmode(void) -{ /* update all the mode lines */ - window_p wp; +void upmode( void) { /* update all the mode lines */ + window_p wp ; - wp = wheadp; - while (wp != NULL) { - wp->w_flag |= WFMODE; - wp = wp->w_wndp; + wp = wheadp ; + while( wp != NULL) { + wp->w_flag |= WFMODE ; + wp = wp->w_wndp ; } } -/* - * Send a command to the terminal to move the hardware cursor to row "row" + +/* Send a command to the terminal to move the hardware cursor to row "row" * and column "col". The row and column arguments are origin 0. Optimize out * random calls. Update "ttrow" and "ttcol". */ -void movecursor(int row, int col) -{ - if (row != ttrow || col != ttcol) { - ttrow = row; - ttcol = col; - TTmove(row, col); +void movecursor( int row, int col) { + if( row != ttrow || col != ttcol) { + ttrow = row ; + ttcol = col ; + TTmove( row, col) ; } } -/* - * Erase the message line. This is a special routine because the message line + +/* Erase the message line. This is a special routine because the message line * is not considered to be part of the virtual screen. It always works * immediately; the terminal buffer is flushed via a call to the flusher. */ @@ -1236,8 +1198,8 @@ static void mlputc( unicode_t c) { } } -/* - * output a string of output characters + +/* output a string of output characters * * char *s; string to output */ @@ -1247,8 +1209,8 @@ void ostring( const char *s) { } -/* - * Write a message into the message line. Keep track of the physical cursor + +/* Write a message into the message line. Keep track of the physical cursor * position. A small class of printf like format items is handled. Assumes the * stack grows down; this assumption is made by the "++" in the argument scan * loop. Set the "message line" flag TRUE. @@ -1258,14 +1220,14 @@ void ostring( const char *s) { */ void vmlwrite( const char *fmt, va_list ap) { /* if we are not currently echoing on the command line, abort this */ - if (discmd == FALSE) { - movecursor(term.t_nrow, 0); - return; + if( discmd == FALSE) { + movecursor( term.t_nrow, 0) ; + return ; } #if COLOR /* set up the proper colors for the command line */ - TTforg(7); - TTbacg(0); + TTforg( 7) ; + TTbacg( 0) ; #endif /* if we can not erase to end-of-line, do it manually */ @@ -1288,28 +1250,28 @@ void vmlwrite( const char *fmt, va_list ap) { fmt += utf8_to_unicode( fmt, 0, 4, &c) ; switch( c) { case 'd': - mlputi(va_arg(ap, int), 10); - break; + mlputi( va_arg( ap, int), 10) ; + break ; case 'o': - mlputi(va_arg(ap, int), 8); - break; + mlputi( va_arg( ap, int), 8) ; + break ; case 'x': - mlputi(va_arg(ap, int), 16); - break; + mlputi( va_arg( ap, int), 16) ; + break ; case 'D': - mlputli(va_arg(ap, long), 10); - break; + mlputli( va_arg( ap, long), 10) ; + break ; case 's': mlputs( (char *) va_arg( ap, char *)) ; - break; + break ; case 'f': - mlputf(va_arg(ap, int)); - break; + mlputf( va_arg( ap, int)) ; + break ; case 'B': /* ring a bell */ TTbeep() ; @@ -1328,7 +1290,7 @@ void vmlwrite( const char *fmt, va_list ap) { if( eolexist == TRUE && ttcol < term.t_ncol) TTeeol() ; - TTflush(); + TTflush() ; } void mlwrite( const char *fmt, ...) { @@ -1339,8 +1301,8 @@ void mlwrite( const char *fmt, ...) { va_end( ap) ; } -/* - * Write out a string. Update the physical cursor position. This assumes that + +/* Write out a string. Update the physical cursor position. This assumes that * the characters in the string all have width "1"; if this is not the case * things will get screwed up a little. */ @@ -1357,8 +1319,8 @@ static void mlputs( const char *s) { } } -/* - * Write out an integer, in the specified radix. Update the physical cursor + +/* Write out an integer, in the specified radix. Update the physical cursor * position. */ static void mlputi( int i, int r) { @@ -1381,9 +1343,8 @@ static void mlputi( int i, int r) { mlputc( hexdigits[ u % r]) ; } -/* - * do the same except as a long integer. - */ + +/* do the same except as a long integer. */ static void mlputli( long l, int r) { long q ; @@ -1400,8 +1361,8 @@ static void mlputli( long l, int r) { mlputc( (int) (l % r) + '0') ; } -/* - * write out a scaled integer with two decimal places + +/* write out a scaled integer with two decimal places * * int s; scaled integer to output */ @@ -1415,9 +1376,9 @@ static void mlputf( int s) { /* send out the integer portion */ mlputi( i, 10) ; - mlputc('.') ; - mlputc((f / 10) + '0') ; - mlputc((f % 10) + '0') ; + mlputc( '.') ; + mlputc( (f / 10) + '0') ; + mlputc( (f % 10) + '0') ; } @@ -1425,29 +1386,27 @@ static void mlputf( int s) { Store number of lines into *heightp and width into *widthp. If zero or a negative number is stored, the value is not valid. */ -void getscreensize(int *widthp, int *heightp) -{ +void getscreensize( int *widthp, int *heightp) { #ifdef TIOCGWINSZ - struct winsize size; - *widthp = 0; - *heightp = 0; - if (ioctl(0, TIOCGWINSZ, &size) < 0) - return; - *widthp = size.ws_col; - *heightp = size.ws_row; + struct winsize size ; + *widthp = 0 ; + *heightp = 0 ; + if( ioctl( 0, TIOCGWINSZ, &size) < 0) + return ; + *widthp = size.ws_col ; + *heightp = size.ws_row ; #else - *widthp = 0; - *heightp = 0; + *widthp = 0 ; + *heightp = 0 ; #endif } #ifdef SIGWINCH -void sizesignal(int signr) -{ - int w, h; - int old_errno = errno; +void sizesignal( int signr) { + int w, h ; + int old_errno = errno ; - getscreensize(&w, &h); + getscreensize( &w, &h) ; if( h > 0 && w > 0) { term.t_mrow = h = h < term.t_maxrow ? h : term.t_maxrow ; @@ -1456,32 +1415,33 @@ void sizesignal(int signr) newscreensize( h, w) ; } - signal(SIGWINCH, sizesignal); - errno = old_errno; + signal( SIGWINCH, sizesignal) ; + errno = old_errno ; } -static int newscreensize(int h, int w) -{ - /* do the change later */ - if (displaying) { - chg_width = w; - chg_height = h; - return FALSE; +static int newscreensize( int h, int w) { +/* do the change later */ + if( displaying) { + chg_width = w ; + chg_height = h ; + return FALSE ; } - chg_width = chg_height = 0; - if( h <= term.t_mrow) - newsize(TRUE, h); - if( w <= term.t_mcol) - newwidth(TRUE, w); - update(TRUE); - return TRUE; + chg_width = chg_height = 0 ; + if( h <= term.t_mrow) + newsize( TRUE, h) ; + + if( w <= term.t_mcol) + newwidth( TRUE, w) ; + + update( TRUE) ; + return TRUE ; } #endif -/* - * output a character when echo is enabled + +/* output a character when echo is enabled * * char c ; character to output */ @@ -1492,8 +1452,8 @@ void echoc( unicode_t c) { } } -/* - * output a string of characters when display input is enabled + +/* output a string of characters when display input is enabled * * char *s; string to output */ @@ -1507,6 +1467,7 @@ void echos( const char *s) { } } + void rubout( void) { if( disinp) { TTputc( '\b') ; diff --git a/display.h b/display.h index cd5564c..89371fd 100644 --- a/display.h +++ b/display.h @@ -21,11 +21,7 @@ BINDABLE( upscreen) ; void vtinit( void) ; void vtfree( void) ; void vttidy( void) ; -int update( boolean force_f) ; -void updpos( void) ; -void upddex( void) ; -void updgar( void) ; -int updupd( int force) ; +boolean update( boolean force_f) ; void upmode( void) ; void movecursor( int row, int col) ; void mlerase( void) ; From 731ea754bbc438db432a161b24aa85ac01eed695 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sun, 15 Aug 2021 16:05:31 +0800 Subject: [PATCH 37/37] Formatting and typos. --- main.c | 9 +++-- tcap.c | 108 ++++++++++++++++++++++++++++----------------------------- 2 files changed, 58 insertions(+), 59 deletions(-) diff --git a/main.c b/main.c index 253fa7a..71a35f4 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,6 @@ /* main.c -- */ -/* - * main.c - * - * µEMACS 4.2 +/* µEMACS 4.2 * * Based on: * @@ -438,7 +435,7 @@ static void dspram( void) #endif #endif -/* On some primitave operation systems, and when emacs is used as +/* On some primitive operation systems, and when emacs is used as a subprogram to a larger project, emacs needs to de-alloc its own used memory */ @@ -483,3 +480,5 @@ void cexit( int status) { exit(status); } #endif + +/* end of main.c */ diff --git a/tcap.c b/tcap.c index 7fff1b2..5ae6cee 100644 --- a/tcap.c +++ b/tcap.c @@ -1,9 +1,7 @@ /* tcap.c -- implements terminal.h */ #include "terminal.h" -/* tcap.c - * - * Unix V7 SysV and BS4 Termcap video driver +/* Unix V7 SysV and BS4 Termcap video driver * * modified by Petri Kutvonen */ @@ -35,9 +33,9 @@ boolean sgarbf = TRUE ; /* TRUE if screen is garbage */ char sres[ 16] ; /* current screen resolution */ /* NORMAL, CGA, EGA, VGA */ -#if UNIX -#include -#endif +# if UNIX +# include +# endif #define MARGIN 8 #define SCRSIZ 64 @@ -57,33 +55,33 @@ static void tcapscrollregion(int top, int bot); static void putpad(char *str); static void tcapopen(void); -#if PKCODE -static void tcapclose(void); -#endif +# if PKCODE + static void tcapclose(void); +# endif -#if COLOR -static void tcapfcol(void); -static void tcapbcol(void); -#endif -#if SCROLLCODE -static void tcapscroll_reg(int from, int to, int linestoscroll); -static void tcapscroll_delins(int from, int to, int linestoscroll); -#endif +# if COLOR + static void tcapfcol(void); + static void tcapbcol(void); +# endif +# if SCROLLCODE + static void tcapscroll_reg(int from, int to, int linestoscroll); + static void tcapscroll_delins(int from, int to, int linestoscroll); +# endif #define TCAPSLEN 315 static char tcapbuf[TCAPSLEN]; static char *_UP, _PC, *CM, *CE, *CL, *SO, *SE; -#if PKCODE -static char *TI, *TE; -#if USE_BROKEN_OPTIMIZATION -static int term_init_ok = 0; -#endif -#endif +# if PKCODE + static char *TI, *TE; +# if USE_BROKEN_OPTIMIZATION + static int term_init_ok = 0; +# endif +# endif -#if SCROLLCODE -static char *CS, *DL, *AL, *SF, *SR; -#endif +# if SCROLLCODE + static char *CS, *DL, *AL, *SF, *SR; +# endif struct terminal term = { 480, /* actual 479 on 2560x1440 landscape terminal window */ @@ -96,11 +94,11 @@ struct terminal term = { SCRSIZ, NPAUSE, tcapopen, -#if PKCODE +# if PKCODE tcapclose, -#else +# else ttclose, -#endif +# endif tcapkopen, tcapkclose, ttgetc, @@ -112,14 +110,14 @@ struct terminal term = { tcapbeep, tcaprev, tcapcres -#if COLOR - , tcapfcol, +# if COLOR + , tcapfcol, tcapbcol -#endif -#if SCROLLCODE - , NULL /* set dynamically at open time */ -#endif -}; +# endif +# if SCROLLCODE + , NULL /* set dynamically at open time */ +# endif +} ; static void tcapopen(void) { @@ -128,9 +126,9 @@ static void tcapopen(void) char *tv_stype; int int_col, int_row; -#if PKCODE && USE_BROKEN_OPTIMIZATION +# if PKCODE && USE_BROKEN_OPTIMIZATION if (!term_init_ok) { -#endif +# endif if ((tv_stype = getenv("TERM")) == NULL) { fputs( "Environment variable TERM not defined!\n", stderr) ; exit( EXIT_FAILURE) ; @@ -177,7 +175,7 @@ static void tcapopen(void) SO = tgetstr("so", &p); if (SO != NULL) revexist = TRUE; -#if PKCODE +# if PKCODE if (tgetnum("sg") > 0) { /* can reverse be used? P.K. */ revexist = FALSE; SE = NULL; @@ -185,7 +183,7 @@ static void tcapopen(void) } TI = tgetstr("ti", &p); /* terminal init and exit */ TE = tgetstr("te", &p); -#endif +# endif if (CL == NULL || CM == NULL || _UP == NULL) { fputs( "Incomplete termcap entry\n", stderr) ; @@ -194,7 +192,7 @@ static void tcapopen(void) if (CE == NULL) /* will we be able to use clear to EOL? */ eolexist = FALSE; -#if SCROLLCODE +# if SCROLLCODE CS = tgetstr("cs", &p); SF = tgetstr("sf", &p); SR = tgetstr("sr", &p); @@ -210,20 +208,20 @@ static void tcapopen(void) } else { term.t_scroll = NULL; } -#endif +# endif if (p >= &tcapbuf[TCAPSLEN]) { fputs( "Terminal description too big!\n", stderr) ; exit( EXIT_FAILURE) ; } -#if PKCODE && USE_BROKEN_OPTIMIZATION +# if PKCODE && USE_BROKEN_OPTIMIZATION term_init_ok = 1; } -#endif +# endif ttopen(); } -#if PKCODE +# if PKCODE static void tcapclose(void) { putpad(tgoto(CM, 0, term.t_nrow)); @@ -231,26 +229,26 @@ static void tcapclose(void) ttflush(); ttclose(); } -#endif +# endif static void tcapkopen(void) { -#if PKCODE +# if PKCODE putpad(TI); ttflush(); ttrow = 999; ttcol = 999; sgarbf = TRUE; -#endif +# endif strcpy(sres, "NORMAL"); } static void tcapkclose(void) { -#if PKCODE +# if PKCODE putpad(TE); ttflush(); -#endif +# endif } static void tcapmove(int row, int col) @@ -288,7 +286,7 @@ static int tcapcres(char *res) return TRUE; } -#if SCROLLCODE +# if SCROLLCODE /* move howmanylines lines starting at from to to */ static void tcapscroll_reg(int from, int to, int howmanylines) @@ -340,9 +338,9 @@ static void tcapscrollregion(int top, int bot) putpad(tgoto(CS, bot, top)); } -#endif +# endif -#if COLOR +# if COLOR /* No colors here, ignore this. */ static void tcapfcol(void) { @@ -351,7 +349,7 @@ static void tcapfcol(void) static void tcapbcol(void) { } -#endif +# endif static void tcapbeep(void) { @@ -363,3 +361,5 @@ static void putpad(char *str) tputs( str, 1, (int (*)( int)) ttputc) ; } #endif /* TERMCAP */ + +/* end of tcap.c */