Skip to content

Commit 8fdd605

Browse files
committed
New command require-final-newline <nil | T | ask>
Disabled by default. Fixes #25 Signed-off-by: Joachim Wiberg <[email protected]>
1 parent 2241fc5 commit 8fdd605

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

doc/mg.1

+9-1
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,14 @@ is negative, it is that line from the bottom.
885885
.It Ic redraw-display
886886
Refresh the display.
887887
Recomputes all window sizes in case something has changed.
888+
.It Ic require-final-newline <nil | T | ask>
889+
Require files to have a final newline added when saving. By default
890+
this is disabled (nil). To always add a missing newline set to
891+
.Ic T ,
892+
to restore the original
893+
.Nm
894+
behavior, use
895+
.Ic ask .
888896
.It Ic revert-buffer
889897
Revert the current buffer to the latest file on disk.
890898
.It Ic save-buffer
@@ -966,7 +974,7 @@ Sets the mark in the current window to the current dot location.
966974
Sets the prefix string to be used by the
967975
.Ic prefix-region
968976
command.
969-
.It set-tab-width
977+
.It Ic set-tab-width
970978
Set the tab width for the current buffer, or the default for new buffers
971979
if called with a prefix argument or from the startup file.
972980
.It Ic shell-command

src/def.h

+1
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ int filewrite(int, int);
474474
int filesave(int, int);
475475
int buffsave(struct buffer *);
476476
int makebkfile(int, int);
477+
int reqnewline(int, int);
477478
int writeout(FILE **, struct buffer *, char *);
478479
void upmodes(struct buffer *);
479480
size_t xbasename(char *, const char *, size_t);

src/file.c

+26-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include "def.h"
1919

20+
static int reqnl = FALSE; /* Don't enforce final newline by default. */
21+
2022
size_t xdirname(char *, const char *, size_t);
2123

2224
/*
@@ -663,6 +665,26 @@ makebkfile(int f, int n)
663665
return (TRUE);
664666
}
665667

668+
int
669+
reqnewline(int f, int n)
670+
{
671+
char buf[5], *arg;
672+
673+
arg = eread("Require newline at EOF (nil, t, ask): ", buf, sizeof(buf), EFNEW);
674+
if (arg == NULL)
675+
return (ABORT);
676+
677+
if (!strcasecmp(arg, "nil"))
678+
reqnl = FALSE;
679+
else if (!strcasecmp(arg, "t"))
680+
reqnl = TRUE;
681+
else if (!strcasecmp(arg, "ask"))
682+
reqnl = 2;
683+
else
684+
return (FALSE);
685+
return (TRUE);
686+
}
687+
666688
/*
667689
* NB: bp is passed to both ffwopen and ffclose because some
668690
* attribute information may need to be updated at open time
@@ -703,7 +725,10 @@ writeout(FILE ** ffp, struct buffer *bp, char *fn)
703725
lpend = bp->b_headp;
704726
eobnl = 0;
705727
if (llength(lback(lpend)) != 0) {
706-
eobnl = eyorn("No newline at end of file, add one");
728+
if (reqnl == 2)
729+
eobnl = eyorn("No newline at end of file, add one");
730+
else
731+
eobnl = reqnl;
707732
if (eobnl != TRUE && eobnl != FALSE)
708733
return (eobnl); /* abort */
709734
}

src/funmap.c

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ static struct funmap functnames[] = {
201201
{re_repl, "replace-regexp", 2, NULL},
202202
{replstr, "replace-string", 2, NULL},
203203
#endif /* REGEX */
204+
{reqnewline, "require-final-newline", 1, NULL},
204205
{revertbuffer, "revert-buffer", 0, NULL},
205206
{filesave, "save-buffer", 1, NULL},
206207
{quit, "save-buffers-kill-emacs", 0, NULL},

0 commit comments

Comments
 (0)