Skip to content

Commit

Permalink
New command require-final-newline <nil | T | ask>
Browse files Browse the repository at this point in the history
Disabled by default.  Fixes #25

Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Mar 22, 2024
1 parent 2241fc5 commit 8fdd605
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
10 changes: 9 additions & 1 deletion doc/mg.1
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nil | T | ask>
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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 26 additions & 1 deletion src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */
}
Expand Down
1 change: 1 addition & 0 deletions src/funmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 8fdd605

Please sign in to comment.