Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dgl/c2c3556abbe93e2f13c527aab3c3ba36 to your computer and use it in GitHub Desktop.
Save dgl/c2c3556abbe93e2f13c527aab3c3ba36 to your computer and use it in GitHub Desktop.
From d2ed74f548fb33029a1d759c328216c5e6f5ff63 Mon Sep 17 00:00:00 2001
From: David Leadbeater <dgl@dgl.cx>
Date: Wed, 6 Dec 2023 14:17:25 +1100
Subject: [PATCH] confirm-paste: No prompt for whitespace with bracketed paste
Bracketed paste mode means the program is suggesting it can handle
pastes, so just send the data to it if it is on and enabled. The main
downside of this is there's no prompting for large pastes anymore.
There's also a minor security advantage of this -- currently if using
'p' the bracketed paste end sequence isn't filtered, but a long paste
can push it off the end of the preview line. With this change the user
gets used to a normal bracketed paste not prompting and not needing to
use 'p', so they may consider why they are being prompted.
---
src/perl/confirm-paste | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/src/perl/confirm-paste b/src/perl/confirm-paste
index 30a42c09..00da20c3 100644
--- a/src/perl/confirm-paste
+++ b/src/perl/confirm-paste
@@ -25,8 +25,13 @@ U+001F currrently) is pasted into the terminal, this extension will ask
whether it should be pasted. Strings without control characters get pasted
without prompt.
+If the program running in the terminal has enabled bracketed paste mode, then
+whitespace characters (TAB, CR, LF) will not result in a prompt, as the program
+has indicated it can safely handle those.
+
When a sanitized version is pasted (choice C<y>), then contiguous
-sequences of those control characters will be replaced by a single spaces.
+sequences of those control characters will be replaced by a single space (this
+has the effect of removing newlines).
The exact detection and sanitization algorithm is subject to change in
future versions.
@@ -46,6 +51,15 @@ sub on_tt_paste {
my $count = ($str =~ tr/\x00-\x1f//)
or return;
+ if (!($self->options & $urxvt::OPTION{disablePasteBrackets})
+ && $self->priv_modes & urxvt::PrivMode_BracketPaste) {
+ # If bracketed paste mode is on, and the only controls are TAB, CR, LF,
+ # paste without confirming. (In theory we can paste safely all control
+ # characters with bracketed paste, except the end sequence, but some
+ # programs have bugs with ^C at least, so use an explicit safe list).
+ return if $str !~ /[\x00-\x08\x0b-\x0c\x0e-\x1f]/;
+ }
+
$self->{paste} = \$str;
$self->msg ("Pasting $count control characters, continue? (y/p/n)");
--
2.40.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment