diff options
-rw-r--r-- | usr.bin/mg/buffer.c | 21 | ||||
-rw-r--r-- | usr.bin/mg/def.h | 6 | ||||
-rw-r--r-- | usr.bin/mg/display.c | 11 | ||||
-rw-r--r-- | usr.bin/mg/file.c | 10 | ||||
-rw-r--r-- | usr.bin/mg/line.c | 58 |
5 files changed, 76 insertions, 30 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c index b4023b9e792..c33abfedddd 100644 --- a/usr.bin/mg/buffer.c +++ b/usr.bin/mg/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.26 2002/03/11 13:02:56 vincent Exp $ */ +/* $OpenBSD: buffer.c,v 1.27 2002/03/16 04:17:36 vincent Exp $ */ /* * Buffer handling. @@ -10,6 +10,22 @@ static BUFFER *makelist(void); +int +togglereadonly(void) +{ + if (!(curbp->b_flag & BFREADONLY)) { + curbp->b_flag |= BFREADONLY; + ewprintf("Now readonly"); + } else { + curbp->b_flag &=~ BFREADONLY; + if (curbp->b_flag & BFCHG) + ewprintf("Warning: Buffer was modified"); + } + curwp->w_flag |= WFMODE; + + return(1); +} + /* * 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 @@ -161,7 +177,6 @@ killbuffer(int f, int n) free_undo_record(rec); rec = next; } - free((char *)bp->b_bname); /* Release name block */ free(bp); /* Release buffer block */ return TRUE; @@ -273,7 +288,7 @@ makelist(void) if (addlinef(blp, "%c%c%c %-*.*s%c%-6d %-*s", (bp == curbp) ? '.' : ' ', /* current buffer ? */ ((bp->b_flag & BFCHG) != 0) ? '*' : ' ', /* changed ? */ - ' ', /* no readonly buffers yet */ + ((bp->b_flag & BFREADONLY) != 0) ? ' ' : '*', w - 5, /* four chars already written */ w - 5, /* four chars already written */ bp->b_bname, /* buffer name */ diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index 2d585d2df43..603a3f77255 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.36 2002/03/11 13:02:56 vincent Exp $ */ +/* $OpenBSD: def.h,v 1.37 2002/03/16 04:17:36 vincent Exp $ */ #include <sys/queue.h> @@ -234,6 +234,7 @@ typedef struct BUFFER { struct fileinfo b_fi; /* File attributes */ LIST_HEAD(, undo_rec) b_undo; /* Undo actions list */ } BUFFER; + #define b_bufp b_list.l_p.x_bp #define b_bname b_list.l_name @@ -243,6 +244,8 @@ typedef struct BUFFER { #define BFNOTAB 0x04 /* no tab mode */ #endif #define BFOVERWRITE 0x08 /* overwrite mode */ +#define BFREADONLY 0x10 /* read only mode */ + /* * This structure holds the starting position @@ -363,6 +366,7 @@ int delwind(int, int); MGWIN *wpopup(void); /* buffer.c */ +int togglereadonly(void); BUFFER *bfind(const char *, int); int poptobuffer(int, int); int killbuffer(int, int); diff --git a/usr.bin/mg/display.c b/usr.bin/mg/display.c index d970c34a90f..a03bd6dccf5 100644 --- a/usr.bin/mg/display.c +++ b/usr.bin/mg/display.c @@ -1,4 +1,4 @@ -/* $OpenBSD: display.c,v 1.11 2002/03/11 13:02:56 vincent Exp $ */ +/* $OpenBSD: display.c,v 1.12 2002/03/16 04:17:36 vincent Exp $ */ /* * The functions in this file handle redisplay. The @@ -755,7 +755,7 @@ modeline(MGWIN *wp) { int n; BUFFER *bp; - int mode; + int mode; n = wp->w_toprow + wp->w_ntrows; /* Location. */ vscreen[n]->v_color = CMODE; /* Mode line color. */ @@ -764,10 +764,13 @@ modeline(MGWIN *wp) bp = wp->w_bufp; vtputc('-'); vtputc('-'); - if ((bp->b_flag & BFCHG) != 0) { /* "*" if changed. */ + if ((bp->b_flag & BFREADONLY) != 0) { + vtputc('%'); + vtputc('%'); + } else if ((bp->b_flag & BFCHG) != 0) { /* "*" if changed. */ vtputc('*'); vtputc('*'); - } else { + } else { vtputc('-'); vtputc('-'); } diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c index 18c91da6a9d..c86a3d0254e 100644 --- a/usr.bin/mg/file.c +++ b/usr.bin/mg/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.15 2002/03/11 13:02:56 vincent Exp $ */ +/* $OpenBSD: file.c,v 1.16 2002/03/16 04:17:36 vincent Exp $ */ /* * File commands. @@ -134,6 +134,14 @@ readin(fname) wp->w_marko = 0; } } + + /* We need to set the READONLY flag after we insert the file */ + if (access(fname, W_OK) && errno != ENOENT) + curbp->b_flag |= BFREADONLY; + else + curbp->b_flag &=~ BFREADONLY; + + return status; } diff --git a/usr.bin/mg/line.c b/usr.bin/mg/line.c index 02fa4c4b5b8..1207b01eb90 100644 --- a/usr.bin/mg/line.c +++ b/usr.bin/mg/line.c @@ -1,4 +1,4 @@ -/* $OpenBSD: line.c,v 1.14 2002/02/21 04:21:05 vincent Exp $ */ +/* $OpenBSD: line.c,v 1.15 2002/03/16 04:17:36 vincent Exp $ */ /* * Text line handling. @@ -83,8 +83,7 @@ lrealloc(LINE *lp, int newsize) * magic conditions described in the above comments don't hold here. */ void -lfree(lp) - LINE *lp; +lfree(LINE *lp) { BUFFER *bp; MGWIN *wp; @@ -128,8 +127,7 @@ lfree(lp) * mode line needs to be updated (the "*" has to be set). */ void -lchange(flag) - int flag; +lchange(int flag) { MGWIN *wp; @@ -157,19 +155,23 @@ lchange(flag) * if all is well, and FALSE on errors. */ int -linsert(n, c) - int n, c; +linsert(int n, int c) { LINE *lp1; MGWIN *wp; RSIZE i; int doto; + if (curbp->b_flag & BFREADONLY) { + ewprintf("Buffer is read only"); + return FALSE; + } + lchange(WFEDIT); /* current line */ lp1 = curwp->w_dotp; - + /* special case for the end */ if (lp1 == curbp->b_linep) { LINE *lp2, *lp3; @@ -240,12 +242,17 @@ linsert(n, c) * current window. The funny ass-backwards way is no longer used. */ int -lnewline() +lnewline(void) { LINE *lp1, *lp2; int doto, nlen; MGWIN *wp; + if (curbp->b_flag & BFREADONLY) { + ewprintf("Buffer is read only"); + return FALSE; + } + lchange(WFHARD); if (!undoaction) { @@ -254,7 +261,6 @@ lnewline() strdup("\n"), 1); } - /* Get the address and offset of "." */ lp1 = curwp->w_dotp; doto = curwp->w_doto; @@ -309,9 +315,7 @@ lnewline() * of insertion into the kill buffer. */ int -ldelete(n, kflag) - RSIZE n; - int kflag; +ldelete(RSIZE n, int kflag) { LINE *dotp; RSIZE chunk; @@ -319,10 +323,15 @@ ldelete(n, kflag) int doto; char *cp1, *cp2; + if (curbp->b_flag & BFREADONLY) { + ewprintf("Buffer is read only"); + return FALSE; + } + if (!undoaction) { undo_add_delete(curwp->w_dotp, curwp->w_doto, n); } - + /* * HACK - doesn't matter, and fixes back-over-nl bug for empty * kill buffers. @@ -402,11 +411,16 @@ ldelete(n, kflag) * TRUE if all looks ok. */ int -ldelnewline() +ldelnewline(void) { LINE *lp1, *lp2, *lp3; MGWIN *wp; + if (curbp->b_flag & BFREADONLY) { + ewprintf("Buffer is read only"); + return FALSE; + } + lp1 = curwp->w_dotp; lp2 = lp1->l_fp; /* at the end of the buffer */ @@ -469,16 +483,18 @@ ldelnewline() * was there). */ int -lreplace(plen, st, f) - RSIZE plen; /* length to remove */ - char *st; /* replacement string */ - int f; /* case hack disable */ +lreplace(RSIZE plen, char *st, int f) { RSIZE rlen; /* replacement length */ int rtype; /* capitalization */ int c; /* used for random characters */ int doto; /* offset into line */ - + + if (curbp->b_flag & BFREADONLY) { + ewprintf("Buffer is read only"); + return FALSE; + } + /* * Find the capitalization of the word that was found. f says use * exact case of replacement string (same thing that happens with @@ -497,7 +513,7 @@ lreplace(plen, st, f) } } } - + /* * make the string lengths match (either pad the line * so that it will fit, or scrunch out the excess). |