diff --git a/doc/mg.1 b/doc/mg.1 index d2c88da..940f73e 100644 --- a/doc/mg.1 +++ b/doc/mg.1 @@ -885,6 +885,14 @@ is negative, it is that line from the bottom. .It Ic redraw-display Refresh the display. Recomputes all window sizes in case something has changed. +.It Ic require-final-newline +Require files to have a final newline added when saving. By default +this is disabled (nil). To always add a missing newline set to +.Ic T , +to restore the original +.Nm +behavior, use +.Ic ask . .It Ic revert-buffer Revert the current buffer to the latest file on disk. .It Ic save-buffer @@ -966,7 +974,7 @@ Sets the mark in the current window to the current dot location. Sets the prefix string to be used by the .Ic prefix-region command. -.It set-tab-width +.It Ic set-tab-width Set the tab width for the current buffer, or the default for new buffers if called with a prefix argument or from the startup file. .It Ic shell-command diff --git a/src/def.h b/src/def.h index 049e78a..1f86f18 100644 --- a/src/def.h +++ b/src/def.h @@ -474,6 +474,7 @@ int filewrite(int, int); int filesave(int, int); int buffsave(struct buffer *); int makebkfile(int, int); +int reqnewline(int, int); int writeout(FILE **, struct buffer *, char *); void upmodes(struct buffer *); size_t xbasename(char *, const char *, size_t); diff --git a/src/file.c b/src/file.c index 432eff3..9771a15 100644 --- a/src/file.c +++ b/src/file.c @@ -17,6 +17,8 @@ #include "def.h" +static int reqnl = FALSE; /* Don't enforce final newline by default. */ + size_t xdirname(char *, const char *, size_t); /* @@ -663,6 +665,26 @@ makebkfile(int f, int n) return (TRUE); } +int +reqnewline(int f, int n) +{ + char buf[5], *arg; + + arg = eread("Require newline at EOF (nil, t, ask): ", buf, sizeof(buf), EFNEW); + if (arg == NULL) + return (ABORT); + + if (!strcasecmp(arg, "nil")) + reqnl = FALSE; + else if (!strcasecmp(arg, "t")) + reqnl = TRUE; + else if (!strcasecmp(arg, "ask")) + reqnl = 2; + else + return (FALSE); + return (TRUE); +} + /* * NB: bp is passed to both ffwopen and ffclose because some * attribute information may need to be updated at open time @@ -703,7 +725,10 @@ writeout(FILE ** ffp, struct buffer *bp, char *fn) lpend = bp->b_headp; eobnl = 0; if (llength(lback(lpend)) != 0) { - eobnl = eyorn("No newline at end of file, add one"); + if (reqnl == 2) + eobnl = eyorn("No newline at end of file, add one"); + else + eobnl = reqnl; if (eobnl != TRUE && eobnl != FALSE) return (eobnl); /* abort */ } diff --git a/src/funmap.c b/src/funmap.c index 27c3653..015b904 100644 --- a/src/funmap.c +++ b/src/funmap.c @@ -201,6 +201,7 @@ static struct funmap functnames[] = { {re_repl, "replace-regexp", 2, NULL}, {replstr, "replace-string", 2, NULL}, #endif /* REGEX */ + {reqnewline, "require-final-newline", 1, NULL}, {revertbuffer, "revert-buffer", 0, NULL}, {filesave, "save-buffer", 1, NULL}, {quit, "save-buffers-kill-emacs", 0, NULL},