#!perl

use 5.010;
use strict;
use warnings;

use App::ProgUtils;
use Perinci::CmdLine::Lite;

our $DATE = '2014-07-29'; # DATE
our $VERSION = '0.10'; # VERSION

our %SPEC;
$SPEC{progedit} = {
    v => 1.1,
    summary => 'Launch editor for program',
    description => <<'_',

Basically

    % progedit PROG

is roughly the same as:

    % $EDITOR `which PROG`

except that it offers tab completion and can also search PROG in the current
directory or normal path (e.g. `progless ../bin/foo`).
_
    args => {
        program => {
            schema => 'str*',
            req => 1,
            pos => 0,
            completion => $App::ProgUtils::_complete_program,
        },
    },
};
sub progedit {
    require String::ShellQuote;

    return [412, "EDITOR is not set"] unless defined($ENV{EDITOR});

    my %args = @_;
    # array variant doesn't always work because $EDITOR can contain args, e.g.
    # "emacsclient -t"
    my $cmd = "$ENV{EDITOR} " . String::ShellQuote::shell_quote(
        App::ProgUtils::_search_program($args{program}));
    exec $cmd;
    # [200]; # unreached
}

Perinci::CmdLine::Lite->new(
    url => '/main/progedit',
)->run;

# ABSTRACT: Launch editor for program
# PODNAME: progedit

__END__

=pod

=encoding UTF-8

=head1 NAME

progedit - Launch editor for program

=head1 VERSION

This document describes version 0.10 of progedit (from Perl distribution App-ProgUtils), released on 2014-07-29.

=head1 SYNOPSIS

Basic usage:

 % progedit mpath

To activate bash completion:

 % complete -C progedit progedit; # can be put in bash startup file e.g. .bashrc
 % progedit mpa<tab>

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-ProgUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/sharyanto/perl-App-ProgUtils>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-ProgUtils>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
