[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Nano-devel] [PATCH 2/2] tools: add the filter command to the tools menu
From: |
Benno Schulenberg |
Subject: |
[Nano-devel] [PATCH 2/2] tools: add the filter command to the tools menu |
Date: |
Thu, 9 May 2019 12:05:16 +0200 |
---
src/files.c | 18 ++++++++++++++++--
src/global.c | 9 ++++++++-
src/prompt.c | 3 ++-
src/proto.h | 1 +
4 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/src/files.c b/src/files.c
index b9d894f3..c0a09068 100644
--- a/src/files.c
+++ b/src/files.c
@@ -32,6 +32,9 @@
#define LOCKBUFSIZE 8192
+#define JUSTREAD FALSE
+#define PIPECONTENT TRUE
+
/* Verify that the containing directory of the given filename exists. */
bool has_valid_path(const char *filename)
{
@@ -1074,7 +1077,7 @@ char *get_next_filename(const char *name, const char
*suffix)
/* Insert a file into the current buffer, or into a new buffer when
* the MULTIBUFFER flag is set. */
-void do_insertfile(void)
+void do_insertfile(bool piping)
{
int response;
const char *msg;
@@ -1083,6 +1086,11 @@ void do_insertfile(void)
#ifndef NANO_TINY
file_format original_fmt = openfile->fmt;
bool execute = FALSE;
+
+ if (piping) {
+ execute = TRUE;
+ given = mallocstrcpy(given, "|");
+ }
#endif
/* Display newlines in filenames as ^J. */
@@ -1269,7 +1277,13 @@ void do_insertfile_void(void)
if (ISSET(RESTRICTED))
show_restricted_warning();
else
- do_insertfile();
+ do_insertfile(JUSTREAD);
+}
+
+/* Pipe the current buffer or region through an external command. */
+void pipe_content(void)
+{
+ do_insertfile(PIPECONTENT);
}
/* When passed "[relative path]" or "[relative path][filename]" in
diff --git a/src/global.c b/src/global.c
index 216e6b7e..616230f3 100644
--- a/src/global.c
+++ b/src/global.c
@@ -602,6 +602,8 @@ void shortcut_init(void)
const char *fulljustify_gist = N_("Justify the entire file");
#endif
#ifndef NANO_TINY
+ const char *filter_gist =
+ N_("Filter current buffer or marked region through a command");
const char *wordcount_gist =
N_("Count the number of words, lines, and characters");
#endif
@@ -931,10 +933,14 @@ void shortcut_init(void)
#ifdef ENABLE_JUSTIFY
add_to_funcs(do_full_justify, MMAIN|MTOOLS,
- N_("FullJstify"), WITHORSANS(fulljustify_gist), TOGETHER,
NOVIEW);
+ N_("FullJstify"), WITHORSANS(fulljustify_gist), BLANKAFTER,
NOVIEW);
#endif
#ifndef NANO_TINY
+ if (!ISSET(RESTRICTED))
+ add_to_funcs(pipe_content, MTOOLS,
+ N_("Filter"), WITHORSANS(filter_gist),
BLANKAFTER, NOVIEW);
+
add_to_funcs(do_wordlinechar_count, MMAIN,
N_("Word Count"), WITHORSANS(wordcount_gist), TOGETHER, VIEW);
#endif
@@ -1348,6 +1354,7 @@ void shortcut_init(void)
add_to_sclist(MTOOLS, "^L", 0, do_linter, 0);
add_to_sclist(MTOOLS, "^K", 0, cut_till_eof, 0);
add_to_sclist(MTOOLS, "^J", 0, do_full_justify, 0);
+ add_to_sclist(MTOOLS, "^F", 0, pipe_content, 0);
add_to_sclist(MMOST & ~MFINDINHELP, "F1", 0, do_help_void, 0);
add_to_sclist(MMAIN|MHELP|MBROWSER, "F2", 0, do_exit, 0);
add_to_sclist(MMAIN, "F3", 0, do_writeout_void, 0);
diff --git a/src/prompt.c b/src/prompt.c
index f11320d2..bb08f640 100644
--- a/src/prompt.c
+++ b/src/prompt.c
@@ -792,7 +792,8 @@ void offer_tools(void)
if (input == KEY_WINCH)
continue;
- if (func == do_spell || func == do_linter || func ==
do_full_justify) {
+ if (func == do_spell || func == do_linter ||
+ func == do_full_justify || func
== pipe_content) {
func();
return;
} else if (func == cut_till_eof) {
diff --git a/src/proto.h b/src/proto.h
index 6762f012..0e12a5d1 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -285,6 +285,7 @@ void read_file(FILE *f, int fd, const char *filename, bool
undoable);
int open_file(const char *filename, bool newfie, bool quiet, FILE **f);
char *get_next_filename(const char *name, const char *suffix);
void do_insertfile_void(void);
+void pipe_content(void);
char *get_full_path(const char *origpath);
char *safe_tempfile(FILE **f);
#ifdef ENABLE_OPERATINGDIR
--
2.20.1