From 975b49123592e20c71860e7d44d5788c59928f7d Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Tue, 10 Jul 2018 16:24:22 +0200 Subject: [PATCH] dropping a feature: remove the ability to use the 'formatter' command Since the last version, the user can filter an entire buffer through an external command. This external command can also be a formatting program, so there is no longer any need for this specific and special formatter command. --- doc/nano.texi | 10 +---- doc/nanorc.5 | 9 +--- src/global.c | 23 ++-------- src/nano.h | 2 - src/proto.h | 5 +-- src/rcfile.c | 9 +--- src/text.c | 113 -------------------------------------------------- src/winio.c | 7 ++-- 8 files changed, 14 insertions(+), 164 deletions(-) diff --git a/doc/nano.texi b/doc/nano.texi index 7a36ffbb..296de73d 100644 --- a/doc/nano.texi +++ b/doc/nano.texi @@ -986,11 +986,6 @@ on the system and will be silently ignored otherwise.) Use the given @var{program} to do a syntax check on the current buffer. (This overrides the speller function.) -@item formatter @var{program} [@var{arg} @dots{}] -Use the given @var{program} to automatically reformat the text in -the current buffer --- useful for a programming language like Go. -(This overrides the speller and linter functions.) - @item comment "@var{string}" Use the given string for commenting and uncommenting lines. If the string contains a vertical bar or pipe character (@t{|}), @@ -1034,8 +1029,7 @@ to @code{icolor}. @item extendsyntax @var{name} @var{command} [@var{arg} @dots{}] Extend the syntax previously defined as "@var{name}" with another @var{command}. This allows you to add a new @code{color}, @code{icolor}, @code{header}, -@code{magic}, @code{comment}, @code{linter}, or @code{formatter} command -to an already +@code{magic}, @code{comment}, or @code{linter} command to an already defined syntax --- useful when you want to slightly improve a syntax defined in one of the system-installed files (which normally are not writable). @@ -1164,7 +1158,7 @@ Shows the current cursor position: the line, column, and character positions. Counts the number of words, lines and characters in the current buffer. @item speller -Invokes a spell-checking program (or linting program, or formatter program, +Invokes a spell-checking program (or linting program, if the active syntax defines such a thing). @item justify diff --git a/doc/nanorc.5 b/doc/nanorc.5 index 14d784e2..44719cc9 100644 --- a/doc/nanorc.5 +++ b/doc/nanorc.5 @@ -341,11 +341,6 @@ system and will be silently ignored otherwise.) Use the given \fIprogram\fR to run a syntax check on the current buffer. (This overrides the speller function.) .TP -.BI formatter " program " \fR[ "arg " \fR...] -Use the given \fIprogram\fR to automatically reformat the text in -the current buffer -- useful in a programming language like Go. -(This overrides the speller and linter functions.) -.TP .BI "comment """ string """ Use the given \fIstring\fR for commenting and uncommenting lines. If the string contains a vertical bar or pipe character (\fB|\fR), @@ -391,7 +386,7 @@ to \fBicolor\fP. .BI extendsyntax " name command " \fR[ "arg " \fR...] Extend the syntax previously defined as \fIname\fR with another \fIcommand\fR. This allows adding a new \fBcolor\fP, \fBicolor\fP, -\fBheader\fP, \fBmagic\fP, \fBcomment\fP, \fBlinter\fP, or \fBformatter\fP +\fBheader\fP, \fBmagic\fP, \fBcomment\fP, or \fBlinter\fP command to an already defined syntax -- useful when you want to slightly improve a syntax defined in one of the system-installed files (which normally are not writable). @@ -514,7 +509,7 @@ Shows the current cursor position: the line, column, and character positions. Counts the number of words, lines and characters in the current buffer. .TP .B speller -Invokes a spell-checking program (or linting program, or formatter program, +Invokes a spell-checking program (or linting program, if the active syntax defines such a thing). .TP .B linter diff --git a/src/global.c b/src/global.c index 6df22d9c..6e81e3ea 100644 --- a/src/global.c +++ b/src/global.c @@ -655,9 +655,6 @@ void shortcut_init(void) const char *lint_gist = N_("Invoke the linter, if available"); const char *prevlint_gist = N_("Go to previous linter msg"); const char *nextlint_gist = N_("Go to next linter msg"); -#ifdef ENABLE_SPELLER - const char *formatter_gist = N_("Invoke formatter, if available"); -#endif #endif #endif /* ENABLE_HELP */ @@ -754,10 +751,6 @@ void shortcut_init(void) #ifdef ENABLE_COLOR add_to_funcs(do_linter, MMAIN, N_("To Linter"), WITHORSANS(lint_gist), TOGETHER, NOVIEW); -#ifdef ENABLE_SPELLER - add_to_funcs(do_formatter, MMAIN, - N_("Formatter"), WITHORSANS(formatter_gist), BLANKAFTER, NOVIEW); -#endif #endif } @@ -1349,21 +1342,14 @@ void replace_scs_for(void (*oldfunc)(void), void (*newfunc)(void)) s->func = newfunc; } -void set_lint_or_format_shortcuts(void) +void set_linter_shortcut(void) { - if (openfile->syntax->formatter) { - replace_scs_for(do_spell, do_formatter); - replace_scs_for(do_linter, do_formatter); - } else { - replace_scs_for(do_spell, do_linter); - replace_scs_for(do_formatter, do_linter); - } + replace_scs_for(do_spell, do_linter); } -void set_spell_shortcuts(void) +void set_speller_shortcut(void) { - replace_scs_for(do_formatter, do_spell); - replace_scs_for(do_linter, do_spell); + replace_scs_for(do_linter, do_spell); } #endif /* ENABLE_COLOR && ENABLE_SPELLER */ @@ -1795,7 +1781,6 @@ void thanks_for_all_the_fish(void) free(sint->name); free(sint->linter); - free(sint->formatter); while (sint->extensions != NULL) { regexlisttype *item = sint->extensions; diff --git a/src/nano.h b/src/nano.h index a5f70223..c0779801 100644 --- a/src/nano.h +++ b/src/nano.h @@ -229,8 +229,6 @@ typedef struct syntaxtype { /* The list of libmagic results that this syntax applies to. */ char *linter; /* The command with which to lint this type of file. */ - char *formatter; - /* The formatting command (for programming languages mainly). */ #ifdef ENABLE_COMMENT char *comment; /* The line comment prefix (and postfix) for this type of file. */ diff --git a/src/proto.h b/src/proto.h index d30afa34..46794fff 100644 --- a/src/proto.h +++ b/src/proto.h @@ -323,8 +323,8 @@ void assign_keyinfo(sc *s, const char *keystring, const int keycode); void print_sclist(void); void shortcut_init(void); #ifdef ENABLE_COLOR -void set_lint_or_format_shortcuts(void); -void set_spell_shortcuts(void); +void set_linter_shortcut(void); +void set_speller_shortcut(void); #endif const subnfunc *sctofunc(const sc *s); const char *flagtostr(int flag); @@ -555,7 +555,6 @@ void do_spell(void); #endif #ifdef ENABLE_COLOR void do_linter(void); -void do_formatter(void); #endif #ifndef NANO_TINY void do_wordlinechar_count(void); diff --git a/src/rcfile.c b/src/rcfile.c index 87832f06..67584787 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -300,7 +300,6 @@ void parse_syntax(char *ptr) live_syntax->headers = NULL; live_syntax->magics = NULL; live_syntax->linter = NULL; - live_syntax->formatter = NULL; #ifdef ENABLE_COMMENT live_syntax->comment = mallocstrcpy(NULL, GENERAL_COMMENT_CHARACTER); #endif @@ -834,7 +833,7 @@ void grab_and_store(const char *kind, char *ptr, regexlisttype **storage) } } -/* Gather and store the string after a comment/linter/formatter command. */ +/* Gather and store the string after a comment/linter command. */ void pick_up_name(const char *kind, char *ptr, char **storage) { if (!opensyntax) { @@ -985,12 +984,6 @@ void parse_rcfile(FILE *rcstream, bool syntax_only) parse_colors(ptr, NANO_REG_EXTENDED | REG_ICASE); else if (strcasecmp(keyword, "linter") == 0) pick_up_name("linter", ptr, &live_syntax->linter); - else if (strcasecmp(keyword, "formatter") == 0) -#ifdef ENABLE_SPELLER - pick_up_name("formatter", ptr, &live_syntax->formatter); -#else - ; -#endif else if (syntax_only && (strcasecmp(keyword, "set") == 0 || strcasecmp(keyword, "unset") == 0 || strcasecmp(keyword, "bind") == 0 || diff --git a/src/text.c b/src/text.c index 307cbe5f..18a62f0a 100644 --- a/src/text.c +++ b/src/text.c @@ -3303,119 +3303,6 @@ void do_linter(void) free(tmplint); } } - -#ifdef ENABLE_SPELLER -/* Run a formatter for the current syntax. This expects the formatter - * to be non-interactive and operate on a file in-place, which we'll - * pass it on the command line. */ -void do_formatter(void) -{ - bool status; - FILE *temp_file; - int format_status; - ssize_t lineno_save = openfile->current->lineno; - size_t current_x_save = openfile->current_x; - size_t pww_save = openfile->placewewant; - bool was_at_eol = (openfile->current->data[openfile->current_x] == '\0'); - pid_t pid_format; - static char **formatargs = NULL; - char *temp, *finalstatus = NULL; - - if (openfile->totsize == 0) { - statusbar(_("Finished")); - return; - } - - temp = safe_tempfile(&temp_file); - - if (temp == NULL) { - statusline(ALERT, _("Error writing temp file: %s"), strerror(errno)); - return; - } - -#ifndef NANO_TINY - /* We're not supporting partial formatting, oi vey. */ - openfile->mark = NULL; -#endif - status = write_file(temp, temp_file, TRUE, OVERWRITE, TRUE); - - if (!status) { - statusline(ALERT, _("Error writing temp file: %s"), strerror(errno)); - free(temp); - return; - } - - blank_bottombars(); - statusbar(_("Invoking formatter, please wait")); - - construct_argument_list(&formatargs, openfile->syntax->formatter, temp); - - /* Start a new process for the formatter. */ - if ((pid_format = fork()) == 0) { - /* Start the formatting program; we are using $PATH. */ - execvp(formatargs[0], formatargs); - - /* Should not be reached, if the formatter is found! */ - exit(1); - } - - /* If we couldn't fork, get out. */ - if (pid_format < 0) { - statusbar(_("Could not fork")); - unlink(temp); - free(temp); - return; - } - -#ifndef NANO_TINY - /* Block SIGWINCHes so the formatter doesn't get any. */ - allow_sigwinch(FALSE); -#endif - - /* Wait for the formatter to finish. */ - wait(&format_status); - - if (!WIFEXITED(format_status) || WEXITSTATUS(format_status) != 0) - finalstatus = invocation_error(openfile->syntax->formatter); - else { - /* Replace the text of the current buffer with the formatted text. */ - replace_buffer(temp); - - /* Restore the cursor position. */ - goto_line_posx(lineno_save, current_x_save); - if (was_at_eol || openfile->current_x > strlen(openfile->current->data)) - openfile->current_x = strlen(openfile->current->data); - openfile->placewewant = pww_save; - adjust_viewport(STATIONARY); - - set_modified(); - -#ifndef NANO_TINY - /* Flush the undo stack, to avoid a mess or crash when - * the user tries to undo things in reformatted lines. */ - discard_until(NULL, openfile, FALSE); -#endif - finalstatus = _("Finished formatting"); - } - - unlink(temp); - free(temp); - -#ifndef NANO_TINY - /* Unblock SIGWINCHes again. */ - allow_sigwinch(TRUE); -#endif - - statusbar(finalstatus); - - /* If there were error messages, allow the user some time to read them. */ - if (WIFEXITED(format_status) && WEXITSTATUS(format_status) == 2) - sleep(4); - - /* If there were any messages, clear them off. */ - total_refresh(); -} -#endif /* ENABLE_SPELLER */ #endif /* ENABLE_COLOR */ #ifndef NANO_TINY diff --git a/src/winio.c b/src/winio.c index 4ae6d83b..e32e7d55 100644 --- a/src/winio.c +++ b/src/winio.c @@ -3334,11 +3334,10 @@ void total_refresh(void) void display_main_list(void) { #if defined(ENABLE_COLOR) && defined(ENABLE_SPELLER) - if (openfile->syntax && - (openfile->syntax->formatter || openfile->syntax->linter)) - set_lint_or_format_shortcuts(); + if (openfile->syntax && openfile->syntax->linter) + set_linter_shortcut(); else - set_spell_shortcuts(); + set_speller_shortcut(); #endif bottombars(MMAIN);