From 7fb07eeb58d730c685f0c2146e714d7116ee2577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Diego=20Aur=C3=A9lio=20Mesquita?= Date: Thu, 10 May 2018 22:01:05 -0300 Subject: [PATCH 2/3] Use '|' in the first char at a command to determine if text should be piped to an external program or not. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marco Diego Aurélio Mesquita --- src/files.c | 18 ++++++++++++++++++ src/global.c | 13 ++++++++++++- src/prompt.c | 2 +- src/proto.h | 2 ++ src/text.c | 40 ++++++++++++++++++++++++++++++++-------- src/utils.c | 2 +- 6 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/files.c b/src/files.c index c32fce1b..9cdbc6bc 100644 --- a/src/files.c +++ b/src/files.c @@ -1110,6 +1110,24 @@ void do_insertfile(void) i = 0; } #endif + if (func == flip_pipe) { + if (answer && answer[0] == '|') { + fprintf(stderr, "First char is a pipe, we should remove it.\n"); + char *aux = mallocstrcpy(NULL, &answer[1]); + free(answer); + answer = aux; + } else { + char *aux = charalloc(strlen(answer) + 2); + sprintf(aux, "|%s", answer); + free(answer); + answer = aux; + } + + statusbar_x = strlenpt(answer) + 1; + given = mallocstrcpy(given, answer); + continue; + } + /* If we don't have a file yet, go back to the prompt. */ if (i != 0 && (!ISSET(MULTIBUFFER) || i != -2)) continue; diff --git a/src/global.c b/src/global.c index f5e6c0e7..996aec3b 100644 --- a/src/global.c +++ b/src/global.c @@ -311,6 +311,9 @@ void backup_file_void(void) void flip_execute(void) { } +void flip_pipe(void) +{ +} #endif #ifdef ENABLE_MULTIBUFFER void flip_newbuffer(void) @@ -512,6 +515,7 @@ void shortcut_init(void) const char *refresh_tag = N_("Refresh"); /* TRANSLATORS: Try to keep this string at most 12 characters. */ const char *whereisnext_tag = N_("WhereIs Next"); + const char *pipe_tag = N_("Pipe text"); #ifdef ENABLE_HELP /* TRANSLATORS: The next long series of strings are shortcut descriptions; @@ -524,6 +528,8 @@ void shortcut_init(void) N_("Write the current buffer (or the marked region) to disk"); const char *readfile_gist = N_("Insert another file into current buffer (or into new buffer)"); + const char *pipe_gist = + N_("Pipe current file or selction to external command"); const char *whereis_gist = N_("Search forward for a string or a regular expression"); const char *wherewas_gist = @@ -1026,6 +1032,9 @@ void shortcut_init(void) add_to_funcs(flip_execute, MEXTCMD, readfile_tag, WITHORSANS(readfile_gist), TOGETHER, NOVIEW); + + add_to_funcs(flip_pipe, MEXTCMD, + pipe_tag, WITHORSANS(pipe_gist), TOGETHER, NOVIEW); } #endif /* !NANO_TINY */ #ifdef ENABLE_MULTIBUFFER @@ -1329,8 +1338,10 @@ void shortcut_init(void) #endif #ifdef ENABLE_MULTIBUFFER /* Only when not in restricted mode, allow multiple buffers. */ - if (!ISSET(RESTRICTED)) + if (!ISSET(RESTRICTED)) { add_to_sclist(MINSERTFILE|MEXTCMD, "M-F", 0, flip_newbuffer, 0); + add_to_sclist(MEXTCMD, "^\\", 0, flip_pipe, 0); + } #endif #ifdef ENABLE_BROWSER /* Only when not in restricted mode, allow entering the file browser. */ diff --git a/src/prompt.c b/src/prompt.c index 93ab9274..757c9ba0 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -25,7 +25,7 @@ static char *prompt = NULL; /* The prompt string used for statusbar questions. */ -static size_t statusbar_x = HIGHEST_POSITIVE; +size_t statusbar_x = HIGHEST_POSITIVE; /* The cursor position in answer. */ #ifdef ENABLE_MOUSE diff --git a/src/proto.h b/src/proto.h index 8cc8d14f..1100a91a 100644 --- a/src/proto.h +++ b/src/proto.h @@ -124,6 +124,7 @@ extern char *quoteerr; extern char *word_chars; extern char *answer; +extern size_t statusbar_x; extern ssize_t tabsize; @@ -709,6 +710,7 @@ void append_void(void); void prepend_void(void); void backup_file_void(void); void flip_execute(void); +void flip_pipe(void); #endif #ifdef ENABLE_MULTIBUFFER void flip_newbuffer(void); diff --git a/src/text.c b/src/text.c index 70e8f2ef..c9f3236c 100644 --- a/src/text.c +++ b/src/text.c @@ -1105,7 +1105,8 @@ bool execute_command(const char *command) int fd[2]; int out_fd[2]; /* Pipe through which text will be sent for external command. */ - const bool has_selection = openfile->mark; + const bool has_selection = ISSET(MULTIBUFFER) ? openfile->prev->mark : openfile->mark; + const bool should_pipe = (command[0] == '|'); FILE *stream; const char *shellenv; struct sigaction oldaction, newaction; @@ -1119,7 +1120,7 @@ bool execute_command(const char *command) return FALSE; } - if (has_selection && pipe(out_fd)) { + if (should_pipe && pipe(out_fd)) { statusbar(_("Could not create outgoing pipe")); return FALSE; } @@ -1138,13 +1139,13 @@ bool execute_command(const char *command) dup2(fd[1], fileno(stdout)); dup2(fd[1], fileno(stderr)); - if (has_selection) { + if (should_pipe) { close(out_fd[1]); dup2(out_fd[0], fileno(stdin)); } /* Run the given command inside the preferred shell. */ - execl(shellenv, tail(shellenv), "-c", command, NULL); + execl(shellenv, tail(shellenv), "-c", should_pipe ? &command[1] : command, NULL); /* If the exec call returns, there was an error. */ exit(1); @@ -1160,15 +1161,38 @@ bool execute_command(const char *command) } /* If text was selected, pipe it to external command. */ - if (has_selection) { + if (should_pipe) { + if (ISSET(MULTIBUFFER)) + switch_to_prev_buffer(); + filestruct *was_cutbuffer = cutbuffer; close(out_fd[0]); cutbuffer = NULL; - do_cut_text_void(); - send_data(out_fd[1], cutbuffer); - close(out_fd[1]); + + if(has_selection) { + #ifndef NANO_TINY + add_undo(CUT); + #endif + do_cut_text(ISSET(MULTIBUFFER), !has_selection); + #ifndef NANO_TINY + update_undo(CUT); + #endif + } + + if(fork() == 0) { + send_data(out_fd[1], has_selection ? cutbuffer : openfile->fileage); + close(out_fd[1]); + exit(0); + } else { + close(out_fd[0]); + close(out_fd[1]); + } + free_filestruct(cutbuffer); cutbuffer = was_cutbuffer; + + if (ISSET(MULTIBUFFER)) + switch_to_next_buffer(); } /* Before we start reading the forked command's output, we set diff --git a/src/utils.c b/src/utils.c index 9e500bef..6db9ed7c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -476,7 +476,7 @@ size_t strlenpt(const char *text) { size_t span = 0; - while (*text != '\0') + while (text && *text != '\0') text += parse_mbchar(text, NULL, &span); return span; -- 2.11.0