From 159bc18de6d8c1bd512dabdf5ad497258ddebe1d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 30 May 2013 06:24:41 -0500 Subject: editor command --- lib/Reply/Plugin/Editor.pm | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 lib/Reply/Plugin/Editor.pm 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; -- cgit v1.2.3-54-g00ecf