#!perl

our $DATE = '2017-07-27'; # DATE
our $VERSION = '0.003'; # VERSION

# NO_PERINCI_CMDLINE_SCRIPT
# FRAGMENT id=shcompgen-hint completer=1 for=man

use 5.010001;
use strict;
use warnings;
use Log::ger;

use Complete::File qw(complete_file);
use Complete::Util qw(complete_array_elem complete_comma_sep);
use Getopt::Long::Complete qw(GetOptionsWithCompletion);

my $opts = {
};
my $noop = sub {};
my $has_arg;

my $comp_man_or_mod = sub {
    require Complete::Man;
    require Complete::Module;
    require Complete::Util;

    my %args = @_;
    my $word = $args{word};

    # XXX for more proper
    my $has_arg = 0;
    for (0..$#{ $args{words} }) {
        if ($_ ne $args{argpos} && /^[^-]/) {
            $has_arg++;
            last;
        }
    }

    my @answers;
    if ($word =~ m!/!) {
        push @answers, Complete::Module::complete_module(word=>$word);
    } elsif ($word !~ /^-/ && !$has_arg) {
        push @answers, Complete::Man::complete_manpage(word=>$word);
    }

    Complete::Util::combine_answers(@answers);
};

my @orig_argv = @ARGV;

# this is taken from App::cpanminus::script and should be updated from time to
# time.
GetOptionsWithCompletion(
    sub {
        my %args  = @_;
        my $type      = $args{type};
        my $word      = $args{word};
        if ($type eq 'arg') {
            log_trace("[manwrap-pm] Completing arg");
            return $comp_man_or_mod->(%args);
        } elsif ($type eq 'optval') {
            my $ospec = $args{ospec};
            my $opt   = $args{opt};
            log_trace("[manwrap-pm] Completing optval (opt=$opt, ospec=$ospec)");
            if ($ospec eq 'config-file|C=s') {
                return complete_file(filter=>'f', word=>$word);
            } elsif ($ospec eq 'locale|L=s') {
                require Complete::Locale;
                return Complete::Locale::complete_locale(word => $word);
            } elsif ($ospec eq 'manpath|M=s') {
                return complete_file(filter=>'d', word=>$word);
            } elsif ($ospec eq 'sections|S|s=s') {
                require Complete::Man;
                my $ss = Complete::Man::complete_manpage_section(word=>'');
                return complete_comma_sep(word=>$word, elems=>$ss, uniq=>1);
            } elsif ($ospec eq 'pager|P=s') {
                require Complete::Program;
                return Complete::Program::complete_program(word=>$word);
            }
        }
        return [];
    },
    'config-file|C=s' => $noop,
    'debug|d' => $noop,
    'default|D' => $noop,
    'warnings=s' => $noop,
    'whatis|f' => $noop,
    'apropos|k' => $noop,
    'global-apropos|K' => $noop,
    'local-file|l' => $noop,
    'where|path|location|w' => $noop,
    'where-cat|location-cat|W' => $noop,
    'catman|c' => $noop,
    'recode|R=s' => $noop,
    'locale|L=s' => $noop,
    'systems|m=s' => $noop,
    'manpath|M=s' => $noop,
    'sections|S|s=s' => $noop,
    'extension|e=s' => $noop,
    'ignore-case|i' => $noop,
    'match-case|I' => $noop,
    'regex' => $noop,
    'wildcard' => $noop,
    'names-only' => $noop,
    'all|a' => $noop,
    'update|u' => $noop,
    'no-subpages' => $noop,
    'pager|P=s' => $noop,
    'prompt|r=s' => $noop,
    'ascii|7' => $noop,
    'encoding|E=s' => $noop,
    'no-hyphenation|nh' => $noop,
    'no-justification|nj' => $noop,
    'preprocessor|p=s' => $noop,
    'troff|t' => $noop,
    'troff-device|T:s' => $noop,
    'html|H:s' => $noop,
    'gxditview|X:s' => $noop,
    'ditroff|Z' => $noop,
    'help|?' => $noop,
    'usage' => $noop,
    'version|V' => $noop,

    '<>' => sub {
        my $arg = shift;
        # check if this is a module name in the form of Foo/Bar? if yes, we
        # convert it to Foo::Bar
        if ($arg =~ m!/!) {
            for (@orig_argv) {
                next unless $_ eq $arg;
                s!/!::!g;
            }
        }
    },
);

#use DD;
#dd @ARGV;
#dd \@orig_argv;

exec "man", @orig_argv;

# ABSTRACT: Wrapper and completer for man (extra Perl stuffs)
# PODNAME: manwrap-pm

__END__

=pod

=encoding UTF-8

=head1 NAME

manwrap-pm - Wrapper and completer for man (extra Perl stuffs)

=head1 VERSION

This document describes version 0.003 of manwrap-pm (from Perl distribution App-manwrap-pm), released on 2017-07-27.

=head1 SYNOPSIS

To install, install this module and then in your bash (and/or bash startup
file):

 % alias man='manwrap-pm'
 % complete -C manwrap-pm man

or, you can use L<shcompgen> to do that for you automatically.

Now L<man> can also accept and complete Perl module names, à la L<pmman>:

 % man tex/wr<tab>
 % man Text/Wrap _

=head1 DESCRIPTION

I made this wrapper because I like using L<pmman> for completing Perl module
names and reading Perl module documentation:

 % pmman tex/wr<tab>
 % pmman Text/Wrap _

but often, due to years of muscle memory, find myself already typing C<man> as
the command:

 % man _

Thus, this wrapper C<manwrap-pm> (to be aliased as the C<man> command):

 % alias man='manwrap-pm'

has the ability to complete Perl module names à la L<pmman> but also accepts all
the other C<man> switches and later executes C<man>. And you get the best of
both worlds.

=head1 COMPLETION

This script has shell tab completion capability with support for several
shells.

=head2 bash

To activate bash completion for this script, put:

 complete -C manwrap-pm manwrap-pm

in your bash startup (e.g. C<~/.bashrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.

It is recommended, however, that you install modules using L<cpanm-shcompgen>
which can activate shell completion for scripts immediately.

=head2 tcsh

To activate tcsh completion for this script, put:

 complete manwrap-pm 'p/*/`manwrap-pm`/'

in your tcsh startup (e.g. C<~/.tcshrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.

It is also recommended to install C<shcompgen> (see above).

=head2 other shells

For fish and zsh, install C<shcompgen> as described above.

=head1 HOMEPAGE

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

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-manwrap-pm>.

=head1 BUGS

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

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

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017, 2016 by perlancar@cpan.org.

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
