Review buffer list related functions.

This commit is contained in:
Renaud 2016-03-24 21:53:36 +08:00
parent 9669fea1ce
commit 3e2d73394a
3 changed files with 51 additions and 76 deletions

114
buffer.c
View File

@ -36,9 +36,6 @@ const char *modename[] = { /* name of modes */
int gmode = 0 ; /* global editor mode */ int gmode = 0 ; /* global editor mode */
static const char modecode[] = "WCEVOMAUD" ; /* letters to represent modes */
static int makelist( int iflag) ; static int makelist( int iflag) ;
static int addline( char *text) ; static int addline( char *text) ;
static void l_to_a( char *buf, int width, long num) ; static void l_to_a( char *buf, int width, long num) ;
@ -322,90 +319,77 @@ int listbuffers(int f, int n)
*/ */
#define FNAMSTART (3 + 1 + NUMMODES + 10 + 1 + (sizeof( bname_t) - 1) + 1) #define FNAMSTART (3 + 1 + NUMMODES + 10 + 1 + (sizeof( bname_t) - 1) + 1)
static void do_layout( char *line, int mode) {
int i ;
/* 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] = '.' ;
}
static int makelist( int iflag) static int makelist( int iflag)
{ {
char *cp1;
char *cp2;
int c;
struct buffer *bp; struct buffer *bp;
struct line *lp;
int s; int s;
int i;
long nbytes; /* # of bytes in current buffer */
long nlines ; /* # of lines in current buffer */
char line[ FNAMSTART + sizeof( fname_t)] ; char line[ FNAMSTART + sizeof( fname_t)] ;
blistp->b_flag &= ~BFCHG; /* Don't complain! */ blistp->b_flag &= ~BFCHG; /* Don't complain! Mute bclear() */
if ((s = bclear(blistp)) != TRUE) /* Blow old text away */ if ((s = bclear(blistp)) != TRUE) /* Blow old text away */
return s; return s;
blistp->b_fname[ 0] = 0 ; blistp->b_fname[ 0] = 0 ; /* in case of user override */
if( addline("ACT MODES Size Buffer File") == FALSE if( addline("ACT MODES Size Buffer File") == FALSE
|| addline("‾‾‾ ‾‾‾‾‾ ‾‾‾‾ ‾‾‾‾‾‾ ‾‾‾‾") == FALSE) || addline("‾‾‾ ‾‾‾‾‾ ‾‾‾‾ ‾‾‾‾‾‾ ‾‾‾‾") == FALSE)
return FALSE; return FALSE;
/* build line to report global mode settings */ /* report global mode settings */
strcpy( line, " ") ; do_layout( line, gmode) ;
cp1 = &line[ 4] ; if( addline( line) == FALSE)
return FALSE ;
/* output the mode codes */ /* output the list of buffers */
for (i = 0; i < NUMMODES; i++)
if (gmode & (1 << i))
*cp1++ = modecode[i];
else
*cp1++ = '.';
strcpy(cp1, " Global Modes");
if (addline(line) == FALSE)
return FALSE;
/* output the list of buffers */
for( bp = bheadp ; bp != NULL ; bp = bp->b_bufp) { /* For all buffers */ for( bp = bheadp ; bp != NULL ; bp = bp->b_bufp) { /* For all buffers */
/* skip invisible buffers if iflag is false */ char *cp1, *cp2 ;
int c ;
struct line *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)) if (((bp->b_flag & BFINVS) != 0) && (iflag != TRUE))
continue; continue;
cp1 = &line[0]; /* 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) */ /* output status of ACTIVE flag ('@' when the file has been read in) */
*cp1++ = (bp->b_active == TRUE) ? '@' : ' ' ; *cp1++ = (bp->b_active == TRUE) ? '@' : ' ' ;
/* report if the file is truncated */ /* report if the file is truncated */
*cp1++ = ((bp->b_flag & BFTRUNC) != 0) ? '#' : ' ' ; *cp1++ = ((bp->b_flag & BFTRUNC) != 0) ? '#' : ' ' ;
/* output status of changed flag ('*' when the buffer is changed) */ /* output status of changed flag ('*' when the buffer is changed) */
*cp1++ = ((bp->b_flag & BFCHG) != 0) ? '*' : ' ' ; *cp1 = ((bp->b_flag & BFCHG) != 0) ? '*' : ' ' ;
*cp1++ = ' '; /* space */
/* output the mode codes */
for (i = 0; i < NUMMODES; i++) {
if (bp->b_mode & (1 << i))
*cp1++ = modecode[i];
else
*cp1++ = '.';
}
/* No gap as buffer size is left-padded with space */
/* Buffer size */ /* Buffer size */
nbytes = 0L; /* Count bytes in buf. */ nbytes = 0L; /* Count bytes in buf. */
nlines = 0 ; nlines = 0 ;
lp = lforw(bp->b_linep); for( lp = lforw( bp->b_linep) ; lp != bp->b_linep ; lp = lforw( lp)) {
while (lp != bp->b_linep) {
nbytes += (long) llength(lp) + 1L; nbytes += (long) llength(lp) + 1L;
nlines += 1 ; nlines += 1 ;
lp = lforw(lp);
} }
if( bp->b_mode & MDDOS) if( bp->b_mode & MDDOS)
nbytes += nlines ; nbytes += nlines ;
l_to_a( cp1, 10 + 1, nbytes) ; /* "%10d" formatted numbers */ l_to_a( &line[ 13], 10 + 1, nbytes) ; /* "%10d" formatted numbers */
cp1 += 10 ; cp1 = &line[ 23] ;
*cp1++ = ' ' ;
*cp1++ = ' '; /* Gap. */
cp2 = &bp->b_bname[0]; /* Buffer name */ cp2 = &bp->b_bname[0]; /* Buffer name */
while ((c = *cp2++) != 0) while ((c = *cp2++) != 0)
*cp1++ = c; *cp1++ = c;
@ -499,15 +483,12 @@ int anycb(void)
struct buffer *bfind( const char *bname, int cflag, int bflag) struct buffer *bfind( const char *bname, int cflag, int bflag)
{ {
struct buffer *bp; struct buffer *bp;
struct buffer *sb; /* buffer to insert after */
struct line *lp; struct line *lp;
bp = bheadp; for( bp = bheadp ; bp != NULL ; bp = bp->b_bufp)
while (bp != NULL) { if( strcmp( bname, bp->b_bname) == 0)
if (strcmp(bname, bp->b_bname) == 0) return bp ;
return bp;
bp = bp->b_bufp;
}
if (cflag != FALSE) { if (cflag != FALSE) {
if ((bp = (struct buffer *)malloc(sizeof(struct buffer))) == NULL) if ((bp = (struct buffer *)malloc(sizeof(struct buffer))) == NULL)
return NULL; return NULL;
@ -521,16 +502,15 @@ struct buffer *bfind( const char *bname, int cflag, int bflag)
bp->b_bufp = bheadp; bp->b_bufp = bheadp;
bheadp = bp; bheadp = bp;
} else { } else {
sb = bheadp; struct buffer *sb; /* buffer to insert after */
while (sb->b_bufp != NULL) {
if (strcmp(sb->b_bufp->b_bname, bname) > 0) for( sb = bheadp ; sb->b_bufp != NULL ; sb = sb->b_bufp)
break; if( strcmp( sb->b_bufp->b_bname, bname) > 0)
sb = sb->b_bufp; break ;
}
/* and insert it */ /* and insert it */
bp->b_bufp = sb->b_bufp; bp->b_bufp = sb->b_bufp ;
sb->b_bufp = bp; sb->b_bufp = bp ;
} }
/* and set up the other buffer fields */ /* and set up the other buffer fields */

7
main.c
View File

@ -347,10 +347,9 @@ static void edinit(char *bname)
struct buffer *bp; struct buffer *bp;
struct window *wp; struct window *wp;
bp = bfind(bname, TRUE, 0); /* First buffer */ if( NULL == (bp = bfind( bname, TRUE, 0)) /* First buffer */
blistp = bfind("*List*", TRUE, BFINVS); /* Buffer list buffer */ || NULL == (blistp = bfind( "*List*", TRUE, BFINVS)) /* Buffer list buffer */
wp = (struct window *)malloc(sizeof(struct window)); /* First window */ || NULL == (wp = (struct window *) malloc( sizeof( struct window)))) { /* First window */
if (bp == NULL || wp == NULL || blistp == NULL) {
fputs( "First initialisation failed!\n", stderr) ; fputs( "First initialisation failed!\n", stderr) ;
exit( EXIT_FAILURE) ; exit( EXIT_FAILURE) ;
} }

View File

@ -81,9 +81,6 @@ int showcpos(int f, int n)
int savepos; /* temp save for current offset */ int savepos; /* temp save for current offset */
int ecol; /* column pos/end of current line */ int ecol; /* column pos/end of current line */
/* starting at the beginning of the buffer */
lp = lforw(curbp->b_linep);
/* start counting chars and lines */ /* start counting chars and lines */
numchars = 0; numchars = 0;
numlines = 0; numlines = 0;
@ -91,7 +88,7 @@ int showcpos(int f, int n)
predlines = 0; predlines = 0;
curchar = 0; curchar = 0;
bytes = 1 ; bytes = 1 ;
while (lp != curbp->b_linep) { for( lp = lforw( curbp->b_linep) ; lp != curbp->b_linep ; lp = lforw( lp)) {
/* if we are on the current line, record it */ /* if we are on the current line, record it */
if (lp == curwp->w_dotp) { if (lp == curwp->w_dotp) {
int len ; int len ;
@ -107,7 +104,6 @@ int showcpos(int f, int n)
/* on to the next line */ /* on to the next line */
++numlines; ++numlines;
numchars += llength( lp) + ((curbp->b_mode & MDDOS) ? 2 : 1) ; numchars += llength( lp) + ((curbp->b_mode & MDDOS) ? 2 : 1) ;
lp = lforw(lp);
} }
/* if at end of file, record it */ /* if at end of file, record it */