summaryrefslogtreecommitdiff
path: root/usr.bin/mg
diff options
context:
space:
mode:
authorVincent Labrecque <vincent@cvs.openbsd.org>2004-07-09 13:50:41 +0000
committerVincent Labrecque <vincent@cvs.openbsd.org>2004-07-09 13:50:41 +0000
commit7b1f7fd51d2a0a0dadbeab57769196b6c5767596 (patch)
treeaf74f8680b3b20e70e34b5ec1e3b6a6ac20ed970 /usr.bin/mg
parenta36bfad67e1ea4d02bf6f413f09c0b0a5941fe65 (diff)
when saving a file, check whether it is newline terminated and if it is not,
prompt the user and add the newline if he agrees. tested by a lot of helpful tech@ guys, approved by henning
Diffstat (limited to 'usr.bin/mg')
-rw-r--r--usr.bin/mg/def.h3
-rw-r--r--usr.bin/mg/fileio.c11
-rw-r--r--usr.bin/mg/line.c43
3 files changed, 33 insertions, 24 deletions
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h
index 4969cc0219a..1022b45c2ab 100644
--- a/usr.bin/mg/def.h
+++ b/usr.bin/mg/def.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: def.h,v 1.54 2004/01/27 23:43:37 vincent Exp $ */
+/* $OpenBSD: def.h,v 1.55 2004/07/09 13:50:39 vincent Exp $ */
#include <sys/queue.h>
@@ -352,6 +352,7 @@ void lfree(LINE *);
void lchange(int);
int linsert_str(const char *, int);
int linsert(int, int);
+int lnewline_at(LINE *, int);
int lnewline(void);
int ldelete(RSIZE, int);
int ldelnewline(void);
diff --git a/usr.bin/mg/fileio.c b/usr.bin/mg/fileio.c
index 8176cad59b2..7b846d7d683 100644
--- a/usr.bin/mg/fileio.c
+++ b/usr.bin/mg/fileio.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fileio.c,v 1.41 2004/07/08 20:48:50 vincent Exp $ */
+/* $OpenBSD: fileio.c,v 1.42 2004/07/09 13:50:40 vincent Exp $ */
/*
* POSIX fileio.c
@@ -107,6 +107,15 @@ ffputbuf(BUFFER *bp)
if (lforw(lp) != lpend) /* no implied \n on last line */
putc('\n', ffp);
}
+ /*
+ * XXX should be variable controlled (once we have variables)
+ */
+ if (llength(lback(lpend)) != 0) {
+ if (eyorn("No newline at end of file, add one") == TRUE) {
+ lnewline_at(lback(lpend), llength(lback(lpend)));
+ putc('\n', ffp);
+ }
+ }
return (FIOSUC);
}
diff --git a/usr.bin/mg/line.c b/usr.bin/mg/line.c
index bf5768212ba..b9129d91540 100644
--- a/usr.bin/mg/line.c
+++ b/usr.bin/mg/line.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: line.c,v 1.20 2004/01/27 23:43:37 vincent Exp $ */
+/* $OpenBSD: line.c,v 1.21 2004/07/09 13:50:40 vincent Exp $ */
/*
* Text line handling.
@@ -330,32 +330,19 @@ linsert(int n, int c)
return TRUE;
}
-/*
- * Insert a newline into the buffer at the current location of dot in the
- * current window. The funny ass-backwards way is no longer used.
- */
int
-lnewline(void)
+lnewline_at(LINE *lp1, int doto)
{
- LINE *lp1, *lp2;
- int doto, nlen;
+ LINE *lp2;
+ int nlen;
MGWIN *wp;
- if (curbp->b_flag & BFREADONLY) {
- ewprintf("Buffer is read only");
- return FALSE;
- }
-
lchange(WFHARD);
undo_add_boundary();
- undo_add_insert(curwp->w_dotp, llength(curwp->w_dotp), 1);
+ undo_add_insert(lp1, llength(lp1), 1);
undo_add_boundary();
- /* Get the address and offset of "." */
- lp1 = curwp->w_dotp;
- doto = curwp->w_doto;
-
/* avoid unnecessary copying */
if (doto == 0) {
/* new first part */
@@ -399,6 +386,20 @@ lnewline(void)
}
/*
+ * Insert a newline into the buffer at the current location of dot in the
+ * current window.
+ */
+int
+lnewline(void)
+{
+ if (curbp->b_flag & BFREADONLY) {
+ ewprintf("Buffer is read only");
+ return FALSE;
+ }
+ return lnewline_at(curwp->w_dotp, curwp->w_doto);
+}
+
+/*
* This function deletes "n" bytes, starting at dot. It understands how to
* deal with end of lines, etc. It returns TRUE if all of the characters
* were deleted, and FALSE if they were not (because dot ran into the end
@@ -694,8 +695,7 @@ kinsert(int c, int dir)
* we are trying to get space at the beginning of the kill buffer.
*/
static int
-kgrow(back)
- int back;
+kgrow(int back)
{
int nstart;
char *nbufp;
@@ -726,8 +726,7 @@ kgrow(back)
* scan along until it gets a "-1" back.
*/
int
-kremove(n)
- int n;
+kremove(int n)
{
if (n < 0 || n + kstart >= kused)
return -1;