2013-05-20 01:16:08 -04:00
|
|
|
/* buffer.c -- implements buffer.h */
|
|
|
|
#include "buffer.h"
|
|
|
|
|
2010-02-15 21:09:42 -05:00
|
|
|
/* buffer.c
|
2005-05-31 11:50:56 -04:00
|
|
|
*
|
|
|
|
* 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
|
2010-02-15 21:09:42 -05:00
|
|
|
*
|
2005-05-31 11:50:56 -04:00
|
|
|
* modified by Petri Kutvonen
|
|
|
|
*/
|
|
|
|
|
2013-10-08 21:43:15 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-11-14 21:10:03 -05:00
|
|
|
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "defines.h"
|
2013-10-09 01:43:32 -04:00
|
|
|
#include "estruct.h"
|
2013-05-25 00:28:17 -04:00
|
|
|
#include "file.h"
|
2013-05-28 02:46:44 -04:00
|
|
|
#include "input.h"
|
2015-02-13 02:33:21 -05:00
|
|
|
#include "mlout.h"
|
2013-05-28 00:47:48 -04:00
|
|
|
#include "window.h"
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2013-06-13 07:16:46 -04:00
|
|
|
|
2013-10-08 05:16:27 -04:00
|
|
|
struct buffer *curbp ; /* Current buffer */
|
|
|
|
struct buffer *bheadp ; /* Head of list of buffers */
|
|
|
|
struct buffer *blistp ; /* Buffer for C-X C-B */
|
|
|
|
|
2013-09-30 11:05:29 -04:00
|
|
|
const char *modename[] = { /* name of modes */
|
2016-03-23 08:25:16 -04:00
|
|
|
"Wrap", "Cmode", "Exact", "View", "Over",
|
2013-09-30 11:05:29 -04:00
|
|
|
"Magic",
|
|
|
|
"Asave", "Utf-8", "Dos"
|
|
|
|
} ;
|
|
|
|
|
2013-10-08 05:16:27 -04:00
|
|
|
int gmode = 0 ; /* global editor mode */
|
|
|
|
|
2013-06-13 07:16:46 -04:00
|
|
|
static int makelist( int iflag) ;
|
|
|
|
static int addline( char *text) ;
|
2014-06-18 02:59:40 -04:00
|
|
|
static void l_to_a( char *buf, int width, long num) ;
|
2013-06-13 07:16:46 -04:00
|
|
|
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-09-30 21:11:54 -04:00
|
|
|
int usebuffer( int f, int n) {
|
|
|
|
struct buffer *bp ;
|
|
|
|
int status ;
|
|
|
|
char *bufn ;
|
|
|
|
|
|
|
|
/* Get buffer name */
|
|
|
|
status = newmlarg( &bufn, "Use 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 ;
|
|
|
|
|
|
|
|
/* Switch to buffer */
|
|
|
|
return swbuffer( bp) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-09-30 21:06:17 -04:00
|
|
|
/*
|
|
|
|
* switch to the next buffer in the buffer list
|
|
|
|
*
|
|
|
|
* int f, n; default flag, numeric argument
|
|
|
|
*/
|
|
|
|
int nextbuffer(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-11-28 15:57:12 -05:00
|
|
|
struct buffer *bp = NULL; /* eligable buffer to switch to */
|
|
|
|
struct buffer *bbp; /* eligable buffer to switch to */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* make sure the arg is legit */
|
|
|
|
if (f == FALSE)
|
|
|
|
n = 1;
|
|
|
|
if (n < 1)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
bbp = curbp;
|
|
|
|
while (n-- > 0) {
|
|
|
|
/* advance to the next buffer */
|
|
|
|
bp = bbp->b_bufp;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* don't get caught in an infinite loop! */
|
|
|
|
if (bp == bbp)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
bbp = bp;
|
|
|
|
}
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return swbuffer(bp);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-09-30 21:06:17 -04:00
|
|
|
/*
|
|
|
|
* make buffer BP current
|
|
|
|
*/
|
2010-02-15 21:09:42 -05:00
|
|
|
int swbuffer(struct buffer *bp)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-08-01 17:37:28 -04:00
|
|
|
struct window *wp;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
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 */
|
2005-05-31 11:50:56 -04:00
|
|
|
/* 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;
|
2005-09-30 18:26:09 -04:00
|
|
|
curbp->b_mode |= gmode; /* P.K. */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
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;
|
2005-05-31 11:50:56 -04:00
|
|
|
curwp->w_marko = bp->b_marko;
|
|
|
|
cknewwindow();
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
cknewwindow();
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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".
|
|
|
|
*/
|
2015-09-30 21:11:54 -04:00
|
|
|
int killbuffer( int f, int n) {
|
|
|
|
struct buffer *bp ;
|
|
|
|
int status ;
|
|
|
|
char *bufn ;
|
|
|
|
|
|
|
|
/* Get buffer name */
|
|
|
|
status = newmlarg( &bufn, "Kill 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 ;
|
|
|
|
|
|
|
|
if( bp->b_flag & BFINVS) /* Deal with special buffers */
|
|
|
|
return TRUE ; /* by doing nothing. */
|
|
|
|
|
|
|
|
return zotbuf( bp) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-09-30 21:06:17 -04:00
|
|
|
/*
|
|
|
|
* kill the buffer pointed to by bp
|
|
|
|
*/
|
2010-02-15 21:09:42 -05:00
|
|
|
int zotbuf(struct buffer *bp)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
struct buffer *bp1;
|
|
|
|
struct buffer *bp2;
|
|
|
|
int s;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (bp->b_nwnd != 0) { /* Error if on screen. */
|
2015-02-13 02:33:21 -05:00
|
|
|
mloutstr("Buffer is being displayed");
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
if ((s = bclear(bp)) != TRUE) /* Blow text away. */
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2005-09-30 18:26:09 -04:00
|
|
|
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 */
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 21:06:17 -04:00
|
|
|
/*
|
|
|
|
* Rename the current buffer
|
|
|
|
*
|
|
|
|
* int f, n; default Flag & Numeric arg
|
|
|
|
*/
|
2015-09-30 21:11:54 -04:00
|
|
|
int namebuffer( int f, int n) {
|
|
|
|
struct buffer *bp ; /* pointer to scan through all buffers */
|
|
|
|
int status ;
|
|
|
|
char *bufn ; /* buffer to hold buffer name */
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2015-09-30 21:11:54 -04:00
|
|
|
|
|
|
|
bp = bp->b_bufp ; /* onward */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 05:44:05 -04:00
|
|
|
/* copy buffer name to structure */
|
|
|
|
strncpy( curbp->b_bname, bufn, sizeof( bname_t) - 1) ;
|
|
|
|
curbp->b_bname[ sizeof( bname_t) - 1] = '\0' ;
|
2015-09-30 21:11:54 -04:00
|
|
|
free( bufn) ;
|
|
|
|
|
|
|
|
curwp->w_flag |= WFMODE ; /* make mode line replot */
|
2015-02-13 02:33:21 -05:00
|
|
|
mloutstr( "") ; /* erase message line */
|
2015-09-30 21:11:54 -04:00
|
|
|
return TRUE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-30 21:06:17 -04:00
|
|
|
* 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)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-08-01 17:37:28 -04:00
|
|
|
struct window *wp;
|
2010-02-27 05:38:22 -05:00
|
|
|
int s;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
if ((s = makelist(f)) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2005-09-30 18:26:09 -04:00
|
|
|
if (blistp->b_nwnd == 0) { /* Not on screen yet. */
|
2016-05-18 23:26:07 -04:00
|
|
|
struct buffer *bp ;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((wp = wpopup()) == NULL)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-09-30 18:26:09 -04:00
|
|
|
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;
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2005-09-30 21:06:17 -04:00
|
|
|
* indicates wether to list hidden buffers.
|
|
|
|
*
|
|
|
|
* int iflag; list hidden buffer flag
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2016-03-24 03:25:48 -04:00
|
|
|
/* Layout: "ACT MODES Size Buffer File"
|
|
|
|
AAA MMMMMMMMMSSSSSSSSSS BBBBBBBBBBBBBBB FFF...
|
|
|
|
FNAMSTART ---------------------------------------^
|
|
|
|
*/
|
|
|
|
#define FNAMSTART (3 + 1 + NUMMODES + 10 + 1 + (sizeof( bname_t) - 1) + 1)
|
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
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] = '.' ;
|
|
|
|
}
|
|
|
|
|
2013-06-13 07:16:46 -04:00
|
|
|
static int makelist( int iflag)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
struct buffer *bp;
|
|
|
|
int s;
|
2016-03-24 03:25:48 -04:00
|
|
|
char line[ FNAMSTART + sizeof( fname_t)] ;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
blistp->b_flag &= ~BFCHG; /* Don't complain! Mute bclear() */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((s = bclear(blistp)) != TRUE) /* Blow old text away */
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2016-03-24 03:25:48 -04:00
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
blistp->b_fname[ 0] = 0 ; /* in case of user override */
|
2016-03-24 03:25:48 -04:00
|
|
|
|
2014-05-30 22:02:39 -04:00
|
|
|
if( addline("ACT MODES Size Buffer File") == FALSE
|
2015-05-05 07:34:12 -04:00
|
|
|
|| addline("‾‾‾ ‾‾‾‾‾ ‾‾‾‾ ‾‾‾‾‾‾ ‾‾‾‾") == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
/* report global mode settings */
|
|
|
|
do_layout( line, gmode) ;
|
|
|
|
if( addline( line) == FALSE)
|
|
|
|
return FALSE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
/* output the list of buffers */
|
2016-03-24 03:25:48 -04:00
|
|
|
for( bp = bheadp ; bp != NULL ; bp = bp->b_bufp) { /* For all buffers */
|
2016-03-24 09:53:36 -04:00
|
|
|
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 */
|
2016-03-24 03:25:48 -04:00
|
|
|
if (((bp->b_flag & BFINVS) != 0) && (iflag != TRUE))
|
2005-09-30 18:26:09 -04:00
|
|
|
continue;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
do_layout( line, bp->b_mode) ;
|
|
|
|
cp1 = line ; /* Start at left edge */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
/* output status of ACTIVE flag ('@' when the file has been read in) */
|
2016-03-24 03:25:48 -04:00
|
|
|
*cp1++ = (bp->b_active == TRUE) ? '@' : ' ' ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
/* report if the file is truncated */
|
2016-03-24 03:25:48 -04:00
|
|
|
*cp1++ = ((bp->b_flag & BFTRUNC) != 0) ? '#' : ' ' ;
|
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
/* output status of changed flag ('*' when the buffer is changed) */
|
|
|
|
*cp1 = ((bp->b_flag & BFCHG) != 0) ? '*' : ' ' ;
|
2016-03-24 03:25:48 -04:00
|
|
|
|
2014-05-31 00:09:33 -04:00
|
|
|
/* Buffer size */
|
2005-09-30 18:26:09 -04:00
|
|
|
nbytes = 0L; /* Count bytes in buf. */
|
2015-05-05 07:34:12 -04:00
|
|
|
nlines = 0 ;
|
2016-03-24 09:53:36 -04:00
|
|
|
for( lp = lforw( bp->b_linep) ; lp != bp->b_linep ; lp = lforw( lp)) {
|
2005-09-30 18:26:09 -04:00
|
|
|
nbytes += (long) llength(lp) + 1L;
|
2015-05-05 07:34:12 -04:00
|
|
|
nlines += 1 ;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2015-05-05 07:34:12 -04:00
|
|
|
|
2015-05-05 21:11:23 -04:00
|
|
|
if( bp->b_mode & MDDOS)
|
2015-05-05 07:34:12 -04:00
|
|
|
nbytes += nlines ;
|
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
l_to_a( &line[ 13], 10 + 1, nbytes) ; /* "%10d" formatted numbers */
|
|
|
|
cp1 = &line[ 23] ;
|
|
|
|
*cp1++ = ' ' ;
|
2005-09-30 18:26:09 -04:00
|
|
|
cp2 = &bp->b_bname[0]; /* Buffer name */
|
|
|
|
while ((c = *cp2++) != 0)
|
|
|
|
*cp1++ = c;
|
2016-03-24 03:25:48 -04:00
|
|
|
|
|
|
|
if( bp->b_fname[ 0] != 0) {
|
|
|
|
while( cp1 < &line[ FNAMSTART])
|
|
|
|
*cp1++ = ' ' ;
|
|
|
|
|
|
|
|
strncpy( cp1, bp->b_fname, &line[ sizeof line - 1] - cp1) ;
|
|
|
|
line[ sizeof line - 1] = 0 ;
|
|
|
|
} else
|
|
|
|
*cp1 = 0 ; /* Terminate string */
|
|
|
|
|
|
|
|
if( addline( line) == FALSE) /* Add to the buffer. */
|
|
|
|
return FALSE ;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2016-03-24 03:25:48 -04:00
|
|
|
|
|
|
|
return TRUE ; /* All done */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2014-06-18 02:59:40 -04:00
|
|
|
static void l_to_a(char *buf, int width, long num)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2014-05-31 00:09:33 -04:00
|
|
|
buf[ --width] = 0 ; /* End of string. */
|
2005-09-30 18:26:09 -04:00
|
|
|
while (num >= 10) { /* Conditional digits. */
|
|
|
|
buf[--width] = (int) (num % 10L) + '0';
|
|
|
|
num /= 10L;
|
|
|
|
}
|
|
|
|
buf[--width] = (int) num + '0'; /* Always 1 digit. */
|
2014-05-31 00:09:33 -04:00
|
|
|
while( width > 0) /* Pad with blanks. */
|
2005-09-30 18:26:09 -04:00
|
|
|
buf[--width] = ' ';
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-06-13 07:16:46 -04:00
|
|
|
static int addline( char *text)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *lp;
|
2010-02-27 05:38:22 -05:00
|
|
|
int i;
|
|
|
|
int ntext;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
ntext = strlen(text);
|
|
|
|
if ((lp = lalloc(ntext)) == NULL)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-09-30 18:26:09 -04:00
|
|
|
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 */
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-09-30 21:06:17 -04:00
|
|
|
int anycb(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
struct buffer *bp;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
bp = bheadp;
|
|
|
|
while (bp != NULL) {
|
|
|
|
if ((bp->b_flag & BFINVS) == 0
|
|
|
|
&& (bp->b_flag & BFCHG) != 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-09-30 18:26:09 -04:00
|
|
|
bp = bp->b_bufp;
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a buffer, by name. Return a pointer
|
2010-02-15 21:09:42 -05:00
|
|
|
* to the buffer structure associated with it.
|
2005-05-31 11:50:56 -04:00
|
|
|
* If the buffer is not found
|
|
|
|
* and the "cflag" is TRUE, create it. The "bflag" is
|
|
|
|
* the settings for the flags in in buffer.
|
|
|
|
*/
|
2013-06-12 02:27:09 -04:00
|
|
|
struct buffer *bfind( const char *bname, int cflag, int bflag)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
struct buffer *bp;
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *lp;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2016-03-24 09:53:36 -04:00
|
|
|
for( bp = bheadp ; bp != NULL ; bp = bp->b_bufp)
|
|
|
|
if( strcmp( bname, bp->b_bname) == 0)
|
|
|
|
return bp ;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (cflag != FALSE) {
|
2010-02-15 21:09:42 -05:00
|
|
|
if ((bp = (struct buffer *)malloc(sizeof(struct buffer))) == NULL)
|
2010-08-29 06:03:55 -04:00
|
|
|
return NULL;
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((lp = lalloc(0)) == NULL) {
|
|
|
|
free((char *) bp);
|
2010-08-29 06:03:55 -04:00
|
|
|
return NULL;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
/* find the place in the list to insert this buffer */
|
|
|
|
if (bheadp == NULL || strcmp(bheadp->b_bname, bname) > 0) {
|
|
|
|
/* insert at the beginning */
|
2005-09-30 18:26:09 -04:00
|
|
|
bp->b_bufp = bheadp;
|
|
|
|
bheadp = bp;
|
|
|
|
} else {
|
2016-03-24 09:53:36 -04:00
|
|
|
struct buffer *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 ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and insert it */
|
2016-03-24 09:53:36 -04:00
|
|
|
bp->b_bufp = sb->b_bufp ;
|
|
|
|
sb->b_bufp = bp ;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and set up the other buffer fields */
|
|
|
|
bp->b_active = TRUE;
|
2005-09-30 18:26:09 -04:00
|
|
|
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;
|
2015-03-16 01:24:08 -04:00
|
|
|
bp->b_fname[ 0] = '\0' ;
|
|
|
|
strncpy( bp->b_bname, bname, sizeof( bname_t) - 1) ;
|
|
|
|
bp->b_bname[ sizeof( bname_t) - 1] = '\0' ;
|
2005-09-30 18:26:09 -04:00
|
|
|
lp->l_fp = lp;
|
|
|
|
lp->l_bp = lp;
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return bp;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2010-02-15 21:09:42 -05:00
|
|
|
int bclear(struct buffer *bp)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *lp;
|
2010-02-27 05:38:22 -05:00
|
|
|
int s;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
if ((bp->b_flag & BFINVS) == 0 /* Not scratch buffer. */
|
|
|
|
&& (bp->b_flag & BFCHG) != 0 /* Something changed */
|
|
|
|
&& (s = mlyesno("Discard changes")) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2005-09-30 18:26:09 -04:00
|
|
|
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;
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-09-30 21:06:17 -04:00
|
|
|
/*
|
|
|
|
* unmark the current buffers change flag
|
|
|
|
*
|
|
|
|
* int f, n; unused command arguments
|
|
|
|
*/
|
|
|
|
int unmark(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
curbp->b_flag &= ~BFCHG;
|
|
|
|
curwp->w_flag |= WFMODE;
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|