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);