home *** CD-ROM | disk | FTP | other *** search
- /*
- * File commands.
- */
- #include "jam.h"
- #include "stdlib.h"
- #include "stdio.h"
- #include "def.h"
-
- #ifndef WINDOWED
- # include "string.h"
- #endif
-
- #include <time.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- #include "keyname.h"
-
- static char rn_(*itos,(char *bufp, unsigned int num));
-
- static BOOL touchInsertedLines = FALSE;
- static BOOL justwritenewline = FALSE;
-
- BOOL clearLineChange = FALSE;
- static BOOL revert_hack = FALSE;
-
- #ifdef FAT
- BOOL no_write_cr = FALSE; /* to allow MSDOS version to not write CR */
- #endif
-
- #define VIEW 1
- #define REREAD 2
- #define REVERT 3
-
- static BOOL showFile = TRUE;
-
- static char *noWrite = "File is readonly.";
- static char *reread = "File changed on disk; reread";
- static char *rereadstr = "Reread file: ";
- static char *revertostr = "Revert to incremental of: ";
- static char *viewstr = "View file: ";
- static char *overwritestr = "File changed on disk; overwrite";
- static char *noIncFile = "Can not find/open incremental file";
- static char *insertanyway = "changed on disk; continue insert";
-
- static int rn_(forceinsert,(int f, int n, int flag));
- static void rn_(makeIname,(BUFFER *bp));
- static int rn_(_poptofile,(int f, int n, BOOL flag));
- static BOOL rn_(timechanged,(BUFFER *bp));
-
- static BOOL timechanged(bp)
- BUFFER *bp;
- {
- time_t curtime;
-
- if (bp->b_fname[0])
- getfiletime(bp->b_fname, &curtime);
-
- if (bp->b_fname[0] && bp->b_time && curtime)
- #ifdef SUN
- return(bp->b_time == curtime ? TRUE : FALSE);
- #else
- return((BOOL)(difftime(bp->b_time, curtime))); /* 0 == no secs different */
- #endif
- return(0);
- }
-
- /* get file time (last mod time) (jam)
- */
- void getfiletime(name, p)
- char *name;
- time_t *p;
- {
- struct stat statbuf;
-
- *p = 0; /* invalid time */
-
- if (!stat(name, &statbuf))
- *p = statbuf.st_mtime;
- }
-
- /* apply cmode as seems fit (JAM)
- */
- void cmodename(bp, fname)
- BUFFER *bp;
- char *fname;
- {
- BUFFER *save = curbp;
- char copiedname[NFILEN+1];
- char *ptr;
-
- strcpy(copiedname, fname);
- ptr = (char *)strchr(copiedname, '.');
-
- if (ptr++ && *ptr)
- {
- /* known extensions
- */
- if ((strcmp(ptr, "h") == 0) ||
- (strcmp(ptr, "c") == 0) ||
- (strcmp(ptr, "hpp") == 0) ||
- (strcmp(ptr, "cc") == 0) ||
- (strcmp(ptr, "cpp") == 0))
- {
- /* mode not set yet
- */
- if ((bp->b_flag & BFC) == 0)
- {
- curbp = bp;
- blinkparen(0, 1); /* set the bits... */
- bp->b_flag |= BFC;
- curbp = save;
- }
- }
- }
- }
-
- /*
- * Insert a file into the current buffer. Real easy - just call the
- * insertfile routine with the file name.
- */
- /*ARGSUSED*/
- fileinsert(f, n)
- int f, n;
- {
- register int s;
- char fname[NFILEN];
-
- epreload(dirpath());
- if ((s=eread("Insert file: ", fname, NFILEN, EFFILE|EFNEW)) != TRUE)
- return(s);
-
- touchInsertedLines = TRUE;
- s = insertfile(adjustname(fname), (char *) NULL);
- touchInsertedLines = FALSE;
- return (s);
- /* don't set buffer name */
- }
-
- /*
-
- * Select a file for editing.
- * Look around to see if you can find the
- * fine in another buffer; if you can find it
- * just switch to the buffer. If you cannot find
- * the file, create a new buffer, read in the
- * text, and switch to the new buffer.
- */
- /*ARGSUSED*/
- filevisit(f, n)
- int f, n;
- {
- register BUFFER *bp;
- int s;
- char fname[NFILEN];
- char *adjf;
-
- epreload(dirpath());
- if ((s=eread("Find file: ", fname, NFILEN, EFFILE|EFNEW)) != TRUE)
- return(s);
-
- adjf = adjustname(fname);
- if ((bp = findbuffer(adjf)) == NULL)
- return FALSE;
-
- #ifdef DOCRYPT
- setencryptstate((bp->b_flag & BFCRYPT) != 0);
- #endif
- if (showbuffer(bp, curwp, WFHARD) != TRUE)
- return FALSE;
- curbp = bp;
-
- s = FALSE;
- if ((bp->b_fname[0] == 0) || (s = timechanged(bp)))
- {
- int t = TRUE;
-
- if (s)
- t = eyesno(reread);
- if (t)
- {
- t = readin(adjf); /* Read it in. */
- if (fileisrdonly(adjf))
- bp->b_flag |= BFVIEW;
- cmodename(bp, adjf);
- getfiletime(bp->b_fname, &bp->b_time); /* reset buf time */
- return(t);
- }
- }
-
- return TRUE;
- }
- /*
- * Like 'filevisit' above, but rereads (prompt if changes)
- * (JAM)
- */
- filerevisit(f, n)
- int f, n;
- {
- int s = forceinsert(f, n, REREAD);
- return (s);
- }
- /* Revert to last saved (JAM)
- */
- int reverto(f, n)
- int f, n;
- {
- int s = forceinsert(f, n, REVERT);
- return(s);
- }
-
- /* Read file, set readonly (JAM)
- */
- int fileview(f, n)
- int f, n;
- {
- int s = forceinsert(f, n, VIEW);
- return(s);
- }
-
- /* Does work for filerevist, reverto and fileview (JAM)
- * It's ughly but works and I don't want lots of redundant
- * code. Someday filevisit will call this too....
- */
- static int forceinsert(f, n, flag)
- int f, n, flag;
- {
- register BUFFER *bp;
- int s = TRUE;
- char fname[NFILEN];
- char *adjf, *msg;
- char savename[NFILEN];
- char saveinc[NFILEN];
- RSIZE line;
-
- if (flag == VIEW)
- epreload(dirpath());
- else
- epreload(curbp->b_fname);
- if (flag == REVERT)
- msg = revertostr;
- else if (flag == VIEW)
- msg = viewstr;
- else
- msg = rereadstr;
-
- s=eread(msg, fname, NFILEN, EFFILE|EFNEW);
- if (s != TRUE)
- return s;
- adjf = adjustname(fname);
- if ((bp = findbuffer(adjf)) == NULL)
- return FALSE;
-
- if (flag == REVERT)
- {
- makeIname(bp);
- if (!bp->b_iname || !fileisok(bp->b_iname))
- {
- ewprintf(noIncFile);
- bp->b_iname[0] = '\0';
- return FALSE;
- }
- }
-
- if (showbuffer(bp, curwp, WFHARD) != TRUE)
- return FALSE;
-
- strcpy(savename, bp->b_fname); /* save real file name */
- strcpy(saveinc, bp->b_iname); /* save restored-from file */
- curbp = bp;
- if ((flag == REVERT) || (flag == REREAD))
- line = getlinenum(curbp, curwp->w_dotp);
-
- #ifdef DOCRYPT
- setencryptstate((bp->b_flag & BFCRYPT) != 0);
- #endif
- if (flag == REVERT)
- bp->b_flag &= ~BFCHG; /* don't prompt on revert */
-
- if ((bp->b_fname[0] == 0) || ((bp->b_flag & BFCHG) == 0)
- || ((bp->b_flag = BFCHG) && eyesno("Buffer modified, re-read")))
- {
- bp->b_flag &= ~(BFCHG|BFINC|BFSAVED); /* don't prompt twice */
- if (flag == REVERT)
- adjf = bp->b_iname;
- if (flag == REREAD)
- revert_hack = TRUE;
- s = readin(adjf); /* Read it in. */
- revert_hack = FALSE;
-
- if (s == TRUE)
- {
- if ((flag == VIEW) || fileisrdonly(adjf))
- bp->b_flag |= BFVIEW;
- if ((flag == REVERT) || (flag == REREAD))
- {
- strcpy(bp->b_fname, savename);
- strcpy(bp->b_iname, saveinc);
- getfiletime(bp->b_fname, &bp->b_time); /* reset buf time */
- if (flag == REVERT)
- bp->b_flag |= (BFCHG | BFINC); /* needs saving */
- gotobigline(line);
- }
- }
- }
-
- if (s)
- if ((flag != REVERT) && (flag != REREAD))
- cmodename(bp, adjf);
- return (s);
- }
-
- /*
- * Pop to a file in the other window. Same as last function, just
- * popbuf instead of showbuffer.
- */
- /*ARGSUSED*/
- poptofile(f, n)
- int f, n;
- {
- return( _poptofile(f, n, TRUE));
- }
- /* Other functions call this, not direct user action (JAM)
- */
- poptofilequiet(f, n)
- int f, n;
- {
- return( _poptofile(f, n, FALSE));
- }
- /* Other functions call this, not direct user action (JAM)
- */
- poptofilehidden(f, n)
- int f, n;
- {
- int s;
-
- showFile = FALSE;
- s = _poptofile(f, n, FALSE);
- showFile = TRUE;
- return (s);
- }
- /* Split from poptofile, and munged (JAM)
- */
- static int _poptofile(f, n, prompt)
- int f,n;
- BOOL prompt;
- {
- register BUFFER *bp;
- register EWINDOW *wp;
- int s;
- char fname[NFILEN];
- char *adjf;
-
- if (prompt)
- epreload(dirpath());
- if ((s=eread("Find file in other window: ", fname, NFILEN,
- EFFILE|EFNEW)) != TRUE)
- return(s);
- adjf = adjustname(fname);
- if ((bp = findbuffer(adjf)) == NULL)
- return FALSE;
-
- if ((wp = popbuf(bp)) == NULL)
- return FALSE;
- curbp = bp;
- curwp = wp;
-
- #ifdef DOCRYPT
- setencryptstate((bp->b_flag & BFCRYPT) != 0);
- #endif
-
- s = FALSE;
- if ((bp->b_fname[0] == 0) || (s = timechanged(bp)))
- {
- int t = TRUE;
-
- if (!prompt)
- {
- bp->b_flag &= ~BFCHG; /* no prompt means don't ask about changes */
- t = TRUE; /* make reread happen */
- }
- else if (s)
- t = eyesno(reread);
-
- if (t)
- {
- t = readin(adjf); /* Read it in. */
- if (fileisrdonly(adjf))
- bp->b_flag |= BFVIEW;
- cmodename(bp, adjf);
- getfiletime(adjf, &bp->b_time); /* reset buf time */
- return(t);
- }
- }
- return(TRUE);
- }
-
- /* Is fname already in a buffer; called
- * better have done any file name adjustments for
- * case etc because this is an obsolute check.
- */
- BUFFER *findexistingbuffer(fname)
- char *fname;
- {
- register BUFFER *bp;
-
- for (bp=bheadp; bp!=NULL; bp=bp->b_bufp)
- {
- if (fncmp(bp->b_fname, fname) == 0)
- return (bp);
- }
- return(NULL);
- }
-
- /*
- * Given a file name, either find the buffer it uses, or create a new
- * empty buffer to put it in.
- */
- BUFFER *findbuffer(fname)
- char *fname;
- {
- register BUFFER *bp;
- char bname[NBUFN], *cp;
- unsigned int count = 1;
-
- bp = findexistingbuffer(fname);
- if (bp)
- return (bp);
-
- makename(bname, fname); /* New buffer name. */
- cp = bname + strlen(bname);
-
- /* creating whole name in place vs counting hits
- * first because we don't expect many, if any, name
- * collisions
- */
- while(bfind(bname, FALSE) != NULL) {
- *cp = '<'; /* add "<count>" to then name */
- (VOID) strcpy(itos(cp, ++count)+1, ">");
- }
- return bfind(bname, TRUE);
- }
-
- /*
- * Put the decimal representation of num into a buffer. Hacked to be
- * faster, smaller, and less general.
- */
- static char *itos(bufp, num)
- char *bufp;
- unsigned int num;
- {
- if (num >= 10) {
- bufp = itos(bufp, num/10);
- num %= 10;
- }
- *++bufp = (char)('0' + num);
- return bufp;
- }
-
- /*
- * Read the file "fname" into the current buffer.
- * Make all of the text in the buffer go away, after checking
- * for unsaved changes. This is called by the "read" command, the
- * "visit" command, and the mainline (for "uemacs file").
- * ^^^^^^ this is old stuff! :)
- */
- readin(fname)
- char *fname;
- {
- register int status;
- register EWINDOW *wp;
-
- if (bclear(curbp) != TRUE) /* Might be old. */
- return TRUE;
-
- #ifdef DOCRYPT
- setencryptstate((curbp->b_flag & BFCRYPT) != 0);
- #endif
- status = insertfile(fname, fname) ;
-
- /* get the file's last time stamp
- */
- curbp->b_flag &= ~(BFINC|BFCHG|BFSAVED);
- curbp->b_time = 0;
- getfiletime(fname, &curbp->b_time);
-
- for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
- if (wp->w_bufp == curbp) {
- wp->w_dotp = wp->w_linep = lforw(curbp->b_linep);
- wp->w_doto = 0;
- wp->w_markp = NULL;
- wp->w_marko = 0;
- }
- }
-
- /* check for previous crash/death/unexplained
- * exit (JAM)
- */
- if (!revert_hack)
- {
- makeIname(curbp);
- if (curbp->b_iname && fileisok(curbp->b_iname))
- ewprintf("Incremental save file found; consider revert-to...");
- curbp->b_iname[0] = '\0'; /* tells existance of newly created file */
- }
- return status;
- }
- /*
- * Insert a file in the current buffer, after dot. Set mark
- * at the end of the text inserted, point at the beginning.
- * Return a standard status. Print a summary (lines read,
- * error message) out as well. If the
- * BACKUP conditional is set, then this routine also does the read
- * end of backup processing. The BFBAK flag, if set in a buffer,
- * says that a backup should be taken. It is set when a file is
- * read in, but not on a new file (you don't need to make a backup
- * copy of nothing).
- */
- insertfile(fname, newname)
- char fname[], newname[];
- {
- register LINE *lp1;
- register LINE *lp2;
- register EWINDOW *wp;
- int nbytes;
- LINE *olp; /* Line we started at */
- int opos; /* and offset into it */
- int s, nline;
- BUFFER *bp;
- char line[NLINE];
-
- bp = curbp; /* Cheap. */
-
- if (timechanged(bp))
- {
- char buff[512];
-
- sprintf(buff, "%s %s", bp->b_fname, insertanyway);
- if (!eyesno(buff))
- goto out;
- }
-
- #ifdef WINDOWED
- WindowSleepCursor();
- #endif
-
- if (newname != (char *) NULL)
- (VOID) strcpy(bp->b_fname, newname);
- if ((s=ffropen(fname)) == FIOERR) /* Hard file open. */
- goto out;
- if (s == FIOFNF)
- { /* File not found. */
- if (newname != NULL)
- ewprintf("(New file)");
- else
- ewprintf("(File not found)");
- goto out;
- }
- opos = curwp->w_doto;
-
- /* Open a new line, at point, and start inserting after it
- */
- (VOID) lnewline();
-
- olp = lback(curwp->w_dotp);
- if (!touchInsertedLines)
- {
- changelineflag(olp, FALSE);
- changelineflag(curwp->w_dotp, FALSE);
- }
-
- if (olp == curbp->b_linep)
- {
- /* if at end of buffer, create a line to insert before
- */
- (VOID) lnewline();
- if (!touchInsertedLines)
- changelineflag(curwp->w_dotp, FALSE);
- curwp->w_dotp = lback(curwp->w_dotp);
- if (!touchInsertedLines)
- changelineflag(curwp->w_dotp, FALSE);
- }
-
- nline = 0; /* Don't count fake line at end */
- while ((s=ffgetline(line, NLINE, &nbytes)) != FIOERR)
- {
- switch(s)
- {
- case FIOSUC:
- ++nline;
- /* FALLTHRU and continue */
- case FIOEOF: /* the last line of the file */
- if ((lp1=lalloc(nbytes)) == NULL)
- {
- s = FIOERR; /* Keep message on the */
- goto endoffile; /* display. */
- }
- if (touchInsertedLines)
- changelineflag(lp1, TRUE);
- bcopy(line, <ext(lp1)[0], (size_t)nbytes);
- lineread:
- lp2 = lback(curwp->w_dotp);
- lp2->l_fp = lp1;
- lp1->l_fp = curwp->w_dotp;
- lp1->l_bp = lp2;
- curwp->w_dotp->l_bp = lp1;
- if(s==FIOEOF)
- goto endoffile;
- break;
- case FIOLONG:
- { /* a line to long to fit in our buffer */
- char *cp;
- char *cp2;
- int i;
-
- nbytes = 0;
- for(;;)
- {
- if((cp = malloc((unsigned)(nbytes + NLINE))) == NULL)
- {
- ewprintf(Nobytes, nbytes + NLINE);
- s = FIOERR;
- if (nbytes && cp2)
- free(cp2);
- goto endoffile;
- }
- if (nbytes && cp2)
- {
- bcopy(cp2, cp, (size_t)nbytes);
- free(cp2);
- }
- bcopy(line, cp+nbytes, (size_t)NLINE);
- nbytes += NLINE;
- switch(s = ffgetline(line, NLINE, &i))
- {
- case FIOERR:
- free(cp);
- goto endoffile;
- case FIOLONG:
- cp2 = cp;
- break;
- case FIOEOF:
- case FIOSUC:
- if((lp1=lalloc(nbytes+i)) == NULL)
- {
- s = FIOERR;
- free(cp);
- goto endoffile;
- }
- if (touchInsertedLines)
- changelineflag(lp1, TRUE);
- bcopy(cp, <ext(lp1)[0], (size_t)nbytes);
- bcopy(line, <ext(lp1)[nbytes], (size_t)i);
- free(cp);
- goto lineread;
- }
- }
- }
- default:
- ewprintf("Unknown code %d reading file", s);
- s = FIOERR;
- break;
- }
- }
-
- endoffile:
- (VOID) ffclose(); /* Ignore errors. */
- if (s==FIOEOF)
- { /* Don't zap an error. */
- if (nline == 1)
- ewprintf("(Read 1 line)");
- else
- ewprintf("(Read %d lines)", nline);
- }
-
- /* Set mark at the end of the text
- */
- curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp);
- curwp->w_marko = llength(curwp->w_markp);
- (VOID) ldelnewline();
- if (!touchInsertedLines)
- changelineflag(curwp->w_dotp, FALSE);
- curwp->w_dotp = olp;
- curwp->w_doto = opos;
- if(olp == curbp->b_linep)
- curwp->w_dotp = lforw(olp);
-
- if (newname != NULL)
- bp->b_flag |= BFCHG | BFBAK | BFINC; /* Need a backup. */
- else
- bp->b_flag |= (BFCHG | BFINC);
-
- /* If the insert was at the end of buffer, set lp1 to the end of
- * buffer line, and lp2 to the beginning of the newly inserted
- * text. (Otherwise lp2 is set to NULL.) This is
- * used below to set pointers in other windows correctly if they
- * are also at the end of buffer.
- */
- lp1 = bp->b_linep;
- if (curwp->w_markp == lp1)
- {
- lp2 = curwp->w_dotp;
- }
- else
- {
- (VOID) ldelnewline(); /* delete extranious newline */
- if (!touchInsertedLines)
- changelineflag(curwp->w_dotp, FALSE);
- out:
- lp2 = NULL;
- }
- for (wp=wheadp; wp!=NULL; wp=wp->w_wndp)
- {
- if (wp->w_bufp == curbp)
- {
- wp->w_flag |= (WFMODE|WFEDIT);
- if (wp != curwp && lp2 != NULL)
- {
- if (wp->w_dotp == lp1)
- wp->w_dotp = lp2;
- if (wp->w_markp == lp1)
- wp->w_markp = lp2;
- if (wp->w_linep == lp1)
- wp->w_linep = lp2;
- }
- }
- }
- #ifdef WINDOWED
- WindowNormalCursor();
- #endif
- thisflag |= CFNEWF;
- return s != FIOERR; /* False if error. */
- }
-
- /*
- * Take a file name, and from it
- * fabricate a buffer name. This routine knows
- * about the syntax of file names on the target system.
- * BDC1 left scan delimiter.
- *
- * VMS no longer supported cause the code was all wacky (JAM)
- */
- VOID makename(bname, fname)
- char bname[];
- char fname[];
- {
- register char *cp1;
- register char *cp2;
-
- cp1 = &fname[0];
- while (*cp1 != 0)
- ++cp1;
- --cp1; /* insure at least 1 character ! */
- while (cp1!=&fname[0] && cp1[-1]!=BDC1)
- --cp1;
- cp2 = &bname[0];
- #ifdef BDC3
- while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=BDC3)
- *cp2++ = *cp1++;
- #else
- while (cp2!=&bname[NBUFN-1] && *cp1!=0)
- *cp2++ = *cp1++;
- #endif
- *cp2 = 0;
- }
-
- /*
- * Ask for a file name, and write the
- * contents of the current buffer to that file.
- * Update the remembered file name and clear the
- * buffer changed flag. This handling of file names
- * is different from the earlier versions, and
- * is more compatable with Gosling EMACS than
- * with ITS EMACS.
- */
- /*ARGSUSED*/
- filewrite(f, n)
- int f, n;
- {
- register int s;
- char fname[NFILEN];
- char *adjfname;
-
- if (curbp->b_fname[0] != '\0')
- epreload(curbp->b_fname);
- else
- epreload(dirpath());
-
- if ((s=ereply("Write file: ", fname, NFILEN)) != TRUE)
- return (s);
- adjfname = adjustname(fname);
-
- #ifdef DOCRYPT
- setencryptstate((curbp->b_flag & BFCRYPT) != 0);
- #endif
-
- clearLineChange = TRUE; /* force linechange flags cleared */
- s=writeout(curbp, adjfname);
- clearLineChange = FALSE;
-
- if (s == TRUE)
- {
- (VOID) strcpy(curbp->b_fname, adjfname);
- /* zap incremental save file and clear state
- */
- curbp->b_flag &= ~(BFBAK | BFCHG | BFINC);
- if (curbp->b_iname[0])
- {
- MakeIncSaveLog(); /* update crash file log */
- unlink(curbp->b_iname); /* delete it */
- curbp->b_flag &= ~BFSAVED;
- }
- curbp->b_iname[0] = '\0';
-
- /* save this new time stamp
- */
- getfiletime(adjfname, &curbp->b_time);
- if (showtouchedlines)
- upmodes(WFHARD); /* because LFCHANGE */
- }
-
- return s;
- }
-
- /*
- * Save the contents of the current buffer back into
- * its associated file.
- */
- #define MAKEBACKUP TRUE
- static int makebackup = MAKEBACKUP;
-
- /*ARGSUSED*/
- int filesave(f, n)
- int f, n;
- {
- return buffsave(curbp);
- }
- /*
- * Save the contents of the buffer argument into its associated file.
- * Do nothing if there have been no changes
- * (is this a bug, or a feature). Error if there is no remembered
- * file name. If this is the first write since the read or visit,
- * then a backup copy of the file is made.
- * Allow user to select whether or not to make backup files
- * by looking at the value of makebackup.
- */
- buffsave(bp)
- BUFFER *bp;
- {
- register int s;
-
- /* Somebody else touch the file? Check first...
- */
- if (timechanged(bp))
- if (eyesno(overwritestr) != TRUE)
- return(FALSE);
-
- if ((bp->b_flag & BFCHG) == 0) { /* Return, no changes. */
- ewprintf("(No changes need to be saved)");
- return TRUE;
- }
-
- if (bp->b_fname[0] == '\0') { /* Must have a name. */
- ewprintf("No file name");
- return (FALSE);
- }
-
- if (makebackup && (bp->b_flag&BFBAK))
- {
- s = fbackupfile(bp->b_fname);
- if (s == ABORT) /* Hard error. */
- return FALSE;
- if (s == FALSE /* Softer error. */
- && (s=eyesno("Backup error, save anyway")) != TRUE)
- return s;
- }
-
- #ifdef DOCRYPT
- setencryptstate((bp->b_flag & BFCRYPT) != 0);
- #endif
-
- clearLineChange = TRUE; /* force linechange flags cleared */
- s = writeout(bp, bp->b_fname);
- clearLineChange = FALSE;
-
- if (s == TRUE)
- {
- /* update mod time if writting named buffer
- */
- getfiletime(bp->b_fname, &bp->b_time);
-
- /* zap incremental save file and clear state
- */
- bp->b_flag &= ~(BFINC | BFCHG | BFBAK);
- if (bp->b_iname[0])
- {
- unlink(bp->b_iname); /* delete it */
- curbp->b_flag &= ~BFSAVED;
- MakeIncSaveLog(); /* update crash file log */
- }
- bp->b_iname[0] = '\0';
- if (showtouchedlines)
- upmodes(WFHARD); /* because LFCHANGED */
- }
- return s;
- }
-
- /* Since we don't have variables (we probably should)
- * this is a command processor for changing the value of
- * the make backup flag. If no argument is given,
- * sets makebackup to true, so backups are made. If
- * an argument is given, no backup files are made when
- * saving a new version of a file. Only used when BACKUP
- * is #defined.
- */
- /*ARGSUSED*/
- makebkfile(f, n)
- int f, n;
- {
- if(f & FFARG)
- makebackup = n > 0;
- else
- makebackup = !makebackup;
- ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
- return TRUE;
- }
-
- /*
- * This function performs the details of file
- * writing; writing the file in buffer bp to
- * file fn. Uses the file management routines
- * in the "fileio.c" package. Most of the grief
- * is checking of some sort.
- */
- writeout(bp, fn)
- register BUFFER *bp;
- char *fn;
- {
- register int s;
-
- #ifdef DOCRYPT
- setencryptstate((bp->b_flag & BFCRYPT) != 0);
- #endif
- if ((s=ffwopen(fn)) != FIOSUC) /* Open writes message. */
- return (FALSE);
-
- #ifdef WINDOWED
- WindowSleepCursor();
- #endif
-
- #ifdef FAT
- if (bp->b_flag & BFC) /* hack to let C source files not have CR */
- if (justwritenewline)
- no_write_cr = TRUE;
- #endif
-
- s = ffputbuf(bp);
-
- #ifdef FAT
- no_write_cr = FALSE;
- #endif
-
- if (s == FIOSUC) { /* No write error. */
- s = ffclose();
- if (s==FIOSUC)
- ewprintf("Wrote %s", fn);
- } else /* Ignore close error */
- (VOID) ffclose(); /* if a write error. */
- #ifdef WINDOWED
- WindowNormalCursor();
- #endif
- return s == FIOSUC;
- }
-
- /*
- * Tag all windows for bp (all windows if bp NULL) as needing their
- * mode line updated.
- */
- VOID upmodes(f)
- int f; /* additional modes, WFMODE always set */
- {
- register EWINDOW *wp;
- int flags = WFMODE;
-
- if (f)
- flags |= f;
- for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
- wp->w_flag |= flags;
- }
-
- /* Incremental save function - called from
- * timer interrupt code...(JAM)
- */
- void IncrementalSave()
- {
- register BUFFER *bp;
- int wasVis = IsCaretVis();
- BOOL msg = FALSE;
-
- SetCaretVis(FALSE);
-
- for (bp = bheadp; bp != NULL; bp = bp->b_bufp)
- if (bp->b_fname[0] && ((bp->b_flag & BFINC) != 0))
- {
- if (!msg)
- ewprintf(garbage);
- msg = TRUE;
-
- /* construct name
- */
- if (!bp->b_iname[0])
- makeIname(bp);
-
- /* make sure name is ok
- */
- if (!bp->b_iname[0])
- continue; /* internal error */
-
- /* save the file
- */
- if (writeout(bp, bp->b_iname) != TRUE)
- ewprintf("Unable to write %s", bp->b_iname);
- else
- bp->b_flag |= BFSAVED;
- bp->b_flag &= ~BFINC; /* too bad! */
- }
-
- if (msg)
- {
- MakeIncSaveLog(); /* need to update if changes saved */
- upmodes(0); /* need to force modeline redraw */
- update();
- }
-
- /* reset cursor pos... turn it back on
- */
- if (wasVis)
- SetCaretVis(TRUE);
- }
-
- /* Make incremental name (JAM)
- */
- static void makeIname(bp)
- BUFFER *bp;
- {
- #ifdef FAT
- # define EXTLEN 5
- char ext[EXTLEN];
- register char *ptr;
-
- strcpy(bp->b_iname, bp->b_fname);
- ptr = (char *)strchr(bp->b_iname, '.');
- memset(ext, '%', EXTLEN);
- ext[0] = '.';
- ext[3] = (ptr && *(ptr+1) ? *(ptr+1) : '#');
- ext[4] = '\0';
-
- if (ptr)
- strcpy(ptr, ext);
- else
- strcat(bp->b_iname, ext);
-
- #else /* unix */
- strcpy(bp->b_iname, bp->b_fname);
- strcat(bp->b_iname, "+");
- #endif
-
- if (!bp->b_iname[0] || (strcmp(bp->b_fname, bp->b_iname) == 0))
- bp->b_iname[0] = '\0'; /* failed, zap name */
- }
-
-
- static verifytime = FALSE;
- int toggleverifytime(f, n)
- int f, n;
- {
- verifytime = verifytime ? FALSE : TRUE;
- ewprintf("Timestamp checking is %s during editting.",
- verifytime ? "enabled" : "disabled");
- return (TRUE);
- }
-
- BOOL filetimechanged(bp)
- BUFFER *bp;
- {
- BOOL flag = FALSE;
-
- if (verifytime) /* enabled? */
- if (flag = timechanged(bp))
- {
- ttbeep();
- if (eyesno("File changed on disk! Edit this file") == TRUE)
- {
- getfiletime(bp->b_fname, &bp->b_time); /* reset buf time */
- ewprintf("Timestamp synced.");
- flag = FALSE; /* date synced */
- }
- }
- return (flag);
- }
-
- int togglejustnl(f,n)
- int f, n;
- {
- justwritenewline = justwritenewline ? FALSE : TRUE;
- ewprintf("Newline only on write (not CR) for C/H files is %s.",
- justwritenewline ? "enabled" : "disabled");
- return (TRUE);
- }
-