From db7cfc869417bdd4dda41f86ea847e2de8d0d651 Mon Sep 17 00:00:00 2001 From: "easyaspi314 (Devin)" Date: Sun, 10 Jun 2018 22:48:35 -0400 Subject: [PATCH 1/2] Replace \n with \r when pasting (Fixes #678) Termux will now properly send \r to the terminal instead of \n when pasting multiline strings. This fixes cat not repeating back lines and nano accidentally justifying text (because \n maps to ^J), as well as other potential issues. This matches the behavior of other terminals, such as iTerm2 which explicitly does it here: https://github.com/gnachman/iTerm2/blob/f8a5930/sources/iTermPasteHelper.m#L113 Signed-off-by: easyaspi314 (Devin) --- .../src/main/java/com/termux/terminal/TerminalEmulator.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java index 42daaea9c7..c022c38ed3 100644 --- a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java +++ b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java @@ -2338,6 +2338,9 @@ private void setTitle(String newTitle) { public void paste(String text) { // First: Always remove escape key and C1 control characters [0x80,0x9F]: text = text.replaceAll("(\u001B|[\u0080-\u009F])", ""); + // Second: Replace all newlines (\n) with carriage returns (\r). + text = text.replace('\n', '\r'); + // Then: Implement bracketed paste mode if enabled: boolean bracketed = isDecsetInternalBitSet(DECSET_BIT_BRACKETED_PASTE_MODE); if (bracketed) mSession.write("\033[200~"); From 060595f8ed605fa41d5f2c1babc9dc5c590309ac Mon Sep 17 00:00:00 2001 From: "easyaspi314 (Devin)" Date: Tue, 12 Jun 2018 23:49:51 -0400 Subject: [PATCH 2/2] Replace CRLF with CR as well. This should replace both \r\n and \n with \r now. "\r?\n" matches 0 or 1 \r and one \n, which should capture both escape sequences. --- .../src/main/java/com/termux/terminal/TerminalEmulator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java index c022c38ed3..6827f20ccd 100644 --- a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java +++ b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java @@ -2338,8 +2338,8 @@ private void setTitle(String newTitle) { public void paste(String text) { // First: Always remove escape key and C1 control characters [0x80,0x9F]: text = text.replaceAll("(\u001B|[\u0080-\u009F])", ""); - // Second: Replace all newlines (\n) with carriage returns (\r). - text = text.replace('\n', '\r'); + // Second: Replace all newlines (\n) or CRLF (\r\n) with carriage returns (\r). + text = text.replaceAll("\r?\n", "\r"); // Then: Implement bracketed paste mode if enabled: boolean bracketed = isDecsetInternalBitSet(DECSET_BIT_BRACKETED_PASTE_MODE);