Perl extension to massage selections from Vim.
[profile.git] / .urxvt / vimselection
diff --git a/.urxvt/vimselection b/.urxvt/vimselection
new file mode 100644 (file)
index 0000000..4a68fab
--- /dev/null
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+# Hide Vim stuff like marks and numbers from the selection.
+sub on_sel_grab {
+  my ($self) = @_;
+
+  # Assume that the terminal will have been resized.
+  return unless $self->{term}->ncol > 80;
+
+  my $selection = $self->{term}->selection;
+  my @lines = split /\n/, $selection;
+  my @new;
+  my $n = 0;
+
+  foreach my $line (@lines) {
+    # If this is the first line only do the replacement at the beginning.
+    # Subsequent lines are necessarily captured from the first column.
+    unless ($n++) {
+      if (($self->{term}->selection_beg)[1] > 1) {
+        push @new, $line;
+        next;
+      }
+    }
+
+    # Strip signs, numbers (five or eight columns) and non-line squiggles.
+    if ($line =~ /^(. )?(~|[ \d]{5}|[ \d]{8})?/) { $line =~ s/// }
+
+    push @new, $line;
+  }
+
+  # Save the new selection.
+  $selection = join "\n", @new;
+  $self->{term}->selection($selection);
+}