| 1 | package Reply::Plugin::Editor; | ||
| 2 | our $AUTHORITY = 'cpan:DOY'; | ||
| 3 | $Reply::Plugin::Editor::VERSION = '0.42'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | # ABSTRACT: command to edit the current line in a text editor | ||
| 7 | |||
| 8 | use base 'Reply::Plugin'; | ||
| 9 | |||
| 10 | use File::HomeDir; | ||
| 11 | use File::Spec; | ||
| 12 | use Proc::InvokeEditor; | ||
| 13 | |||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift; | ||
| 17 | my %opts = @_; | ||
| 18 | |||
| 19 | my $self = $class->SUPER::new(@_); | ||
| 20 | $self->{editor} = Proc::InvokeEditor->new( | ||
| 21 | (defined $opts{editor} | ||
| 22 | ? (editors => [ $opts{editor} ]) | ||
| 23 | : ()) | ||
| 24 | ); | ||
| 25 | $self->{current_text} = ''; | ||
| 26 | |||
| 27 | return $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub command_e { | ||
| 31 | my $self = shift; | ||
| 32 | my ($line) = @_; | ||
| 33 | |||
| 34 | my $text; | ||
| 35 | if (length $line) { | ||
| 36 | if ($line =~ s+^~/++) { | ||
| 37 | $line = File::Spec->catfile(File::HomeDir->my_home, $line); | ||
| 38 | } | ||
| 39 | elsif ($line =~ s+^~([^/]*)/++) { | ||
| 40 | $line = File::Spec->catfile(File::HomeDir->users_home($1), $line); | ||
| 41 | } | ||
| 42 | |||
| 43 | my $current_text = do { | ||
| 44 | local $/; | ||
| 45 | if (open my $fh, '<', $line) { | ||
| 46 | <$fh>; | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | warn "Couldn't open $line: $!"; | ||
| 50 | return ''; | ||
| 51 | } | ||
| 52 | }; | ||
| 53 | $text = $self->{editor}->edit($current_text, '.pl'); | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | $text = $self->{editor}->edit($self->{current_text}, '.pl'); | ||
| 57 | $self->{current_text} = $text; | ||
| 58 | } | ||
| 59 | |||
| 60 | return $text; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | __END__ |