summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-05-30 06:24:41 -0500
committerJesse Luehrs <doy@tozt.net>2013-05-30 06:24:41 -0500
commit159bc18de6d8c1bd512dabdf5ad497258ddebe1d (patch)
tree938c8c92999ea345fc4f592bc9cdf1087ab1e60d
parentc3675ce37cf430b8763d078e3eacf820b5d8c6b3 (diff)
downloadreply-159bc18de6d8c1bd512dabdf5ad497258ddebe1d.tar.gz
reply-159bc18de6d8c1bd512dabdf5ad497258ddebe1d.zip
editor command
-rw-r--r--lib/Reply/Plugin/Editor.pm54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/Reply/Plugin/Editor.pm b/lib/Reply/Plugin/Editor.pm
new file mode 100644
index 0000000..76184d1
--- /dev/null
+++ b/lib/Reply/Plugin/Editor.pm
@@ -0,0 +1,54 @@
+package Reply::Plugin::Editor;
+use strict;
+use warnings;
+
+use base 'Reply::Plugin';
+
+use File::HomeDir;
+use File::Spec;
+use Proc::InvokeEditor;
+
+sub new {
+ my $class = shift;
+
+ my $self = $class->SUPER::new(@_);
+ $self->{editor} = Proc::InvokeEditor->new;
+ $self->{current_text} = '';
+
+ return $self;
+}
+
+sub command_e {
+ my $self = shift;
+ my ($line) = @_;
+
+ my $text;
+ if (length $line) {
+ if ($line =~ s+^~/++) {
+ $line = File::Spec->catfile(File::HomeDir->my_home, $line);
+ }
+ elsif ($line =~ s+^~([^/]*)/++) {
+ $line = File::Spec->catfile(File::HomeDir->users_home($1), $line);
+ }
+
+ my $current_text = do {
+ local $/;
+ if (open my $fh, '<', $line) {
+ <$fh>;
+ }
+ else {
+ warn "Couldn't open $line: $!";
+ return '';
+ }
+ };
+ $text = $self->{editor}->edit($current_text, '.pl');
+ }
+ else {
+ $text = $self->{editor}->edit($self->{current_text}, '.pl');
+ $self->{current_text} = $text;
+ }
+
+ return $text;
+}
+
+1;