[PATCH] possible new feature: execute a command without capturing the output

Benno Schulenberg <[email protected]> Thu, 4 Sep 2025 11:23:48 +0200
Newsgroups gmane.editors.nano.devel
Message-ID <[email protected]>
The feature is activated by preceding the command with two pipes (||).

This lets the command's output go to the terminal, enabling the user
to implement the OSC52 copy-to-clipboard command with a string bind:

  bind M-* "{execute}|| printf "\033]52;c;%s\007" "$(base64 | tr -d '\n')" {enter}{undo}" main

Now this keystroke copies the buffer (or the selected region) to the
local system clipboard, also when `nano` is running on a remote host
-- if and when the used terminal implements the OSC52 command.

Inspired-by: Alessandro Carminati <[email protected]>
---
 src/files.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/files.c b/src/files.c
index 957a95e1..ca8f76c9 100644
--- a/src/files.c
+++ b/src/files.c
@@ -1022,10 +1022,15 @@ void execute_command(const char *command)
 		/* Original and temporary handlers for SIGINT. */
 	ssize_t was_lineno = (openfile->mark ? 0 : openfile->current->lineno);
 	int command_status, sender_status;
+	bool capture_output = TRUE;
 	FILE *stream;
 
 	should_pipe = (command[0] == '|');
 
+	/* Two leading pipe symbols mean: let the output go to the terminal. */
+	if (should_pipe && command[1] == '|')
+		capture_output = FALSE;
+
 	/* Create a pipe to read the command's output from, and, if needed,
 	 * a pipe to feed the command's input through. */
 	if (pipe(from_fd) == -1 || (should_pipe && pipe(to_fd) == -1)) {
@@ -1043,11 +1048,13 @@ void execute_command(const char *command)
 		/* Child: close the unused read end of the output pipe. */
 		close(from_fd[0]);
 
-		/* Connect the write end of the output pipe to the process' output streams. */
-		if (dup2(from_fd[1], STDOUT_FILENO) < 0)
-			exit(3);
-		if (dup2(from_fd[1], STDERR_FILENO) < 0)
-			exit(4);
+		if (capture_output) {
+			/* Connect the write end of the output pipe to the process' output streams. */
+			if (dup2(from_fd[1], STDOUT_FILENO) < 0)
+				exit(3);
+			if (dup2(from_fd[1], STDERR_FILENO) < 0)
+				exit(4);
+		}
 
 		/* If the parent sends text, connect the read end of the
 		 * feeding pipe to the child's input stream. */
@@ -1059,7 +1066,8 @@ void execute_command(const char *command)
 		}
 
 		/* Run the given command inside the preferred shell. */
-		execl(theshell, tail(theshell), "-c", should_pipe ? &command[1] : command, NULL);
+		execl(theshell, tail(theshell), "-c", should_pipe ? capture_output ?
+										&command[1] : &command[2] : command, NULL);
 
 		/* If the exec call returns, there was an error. */
 		exit(6);
-- 
2.51.0