1package Reply::Plugin::Editor;
2our $AUTHORITY = 'cpan:DOY';
3$Reply::Plugin::Editor::VERSION = '0.42';
4use strict;
5use warnings;
6# ABSTRACT: command to edit the current line in a text editor
7
8use base 'Reply::Plugin';
9
10use File::HomeDir;
11use File::Spec;
12use Proc::InvokeEditor;
13
14
15sub 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
30sub 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
641;
65
66__END__
67
68=pod
69
70=encoding UTF-8
71
72=head1 NAME
73
74Reply::Plugin::Editor - command to edit the current line in a text editor
75
76=head1 VERSION
77
78version 0.42
79
80=head1 SYNOPSIS
81
82  ; .replyrc
83  [Editor]
84  editor = emacs
85
86=head1 DESCRIPTION
87
88This plugin provides the C<#e> command. It will launch your editor, and allow
89you to edit bits of code in your editor, which will then be evaluated all at
90once. The text you entered will be saved, and restored the next time you enter
91the command. Alternatively, you can pass a filename to the C<#e> command, and
92the contents of that file will be preloaded instead.
93
94The C<editor> option can be specified to provide a different editor to use,
95otherwise it will use the value of C<$ENV{VISUAL}> or C<$ENV{EDITOR}>.
96
97=for Pod::Coverage command_e
98
99=head1 AUTHOR
100
101Jesse Luehrs <doy@tozt.net>
102
103=head1 COPYRIGHT AND LICENSE
104
105This software is Copyright (c) 2016 by Jesse Luehrs.
106
107This is free software, licensed under:
108
109  The MIT (X11) License
110
111=cut
112