Skip to content

Commit

Permalink
Implement a new function "set-newline-at-eof-mode" which controls how…
Browse files Browse the repository at this point in the history
… mg handles a missing newline at the end of a file during save. It has 3 possible values: prompt, always and never. prompt is the default, current behavior. always will always add a newline if it is missing. never will never add a newline.
  • Loading branch information
rain-1 committed Feb 27, 2022
1 parent da1036f commit 4570d41
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
9 changes: 9 additions & 0 deletions def.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ struct varentry {
};
SLIST_HEAD(vhead, varentry);

/*
* Newline at EOF modes.
*/
#define NL_EOF_PROMPT 0
#define NL_EOF_ALWAYS 1
#define NL_EOF_NEVER 2

/*
* Previously from ttydef.h
*/
Expand Down Expand Up @@ -689,6 +696,7 @@ int notabmode(int, int);
#endif /* NOTAB */
int overwrite_mode(int, int);
int set_default_mode(int,int);
int set_newline_at_eof_mode(int,int);

#ifdef REGEX
/* re_search.c X */
Expand Down Expand Up @@ -786,6 +794,7 @@ extern int tceeol;
extern int tcinsl;
extern int tcdell;
extern int rptcount; /* successive invocation count */
extern int nlateofmode;

/* https://github.com/hboetes/mg/issues/7#issuecomment-475869095 */
#if defined(__APPLE__) || defined(__NetBSD__)
Expand Down
10 changes: 9 additions & 1 deletion file.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,15 @@ 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 (nlateofmode == NL_EOF_PROMPT) {
eobnl = eyorn("No newline at end of file, add one");
}
else if (nlateofmode == NL_EOF_ALWAYS) {
eobnl = TRUE;
}
else if (nlateofmode == NL_EOF_NEVER) {
eobnl = FALSE;
}
if (eobnl != TRUE && eobnl != FALSE)
return (eobnl); /* abort */
}
Expand Down
1 change: 1 addition & 0 deletions funmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ static struct funmap functnames[] = {
#endif /* REGEX */
{setcasereplace, "set-case-replace", 0},
{set_default_mode, "set-default-mode", 1},
{set_newline_at_eof_mode, "set-newline-at-eof-mode", 1},
{setfillcol, "set-fill-column", 1},
{setmark, "set-mark-command", 0},
{setprefix, "set-prefix-string", 1},
Expand Down
33 changes: 33 additions & 0 deletions modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ int defb_nmodes = 0;
struct maps_s *defb_modes[PBMODES] = { &fundamental_mode };
int defb_flag = 0;

int nlateofmode = NL_EOF_PROMPT;

int
changemode(int f, int n, char *newmode)
{
Expand Down Expand Up @@ -172,3 +174,34 @@ set_default_mode(int f, int n)
#endif /* NOTAB */
return (TRUE);
}

// Assumes buf has size at least 8
//
// Check if the input is "prompt", "always", or "never".
// defaults to prompt.
int
name_newline_at_eof_mode(char *buf)
{
if (!strncmp("always", buf, 8))
return NL_EOF_ALWAYS;
else if (!strncmp("never", buf, 8))
return NL_EOF_NEVER;
else
return NL_EOF_PROMPT;
}

int
set_newline_at_eof_mode(int f, int n)
{
char modebuf[32], *bufp;

if ((bufp = eread("Set Newline At EOF Mode: ", modebuf, sizeof(modebuf),
EFNEW)) == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);

nlateofmode = name_newline_at_eof_mode(modebuf);

return (TRUE);
}

0 comments on commit 4570d41

Please sign in to comment.