rename perldoc helper to perl, as it is now a generic perl completion

handler, and use it for perl completion as well
This commit is contained in:
Guillaume Rousse 2010-11-07 01:13:14 +01:00
parent 2d58e69ef9
commit fd8ade0077
3 changed files with 93 additions and 60 deletions

View File

@ -1,3 +1,3 @@
helpers_SCRIPTS = perldoc
helpers_SCRIPTS = perl
EXTRA_DIST = $(helpers_SCRIPTS)

View File

@ -1,6 +1,6 @@
#!/usr/bin/env perl
use strict;
use File::Spec::Functions qw( rel2abs catdir catfile no_upwards );
use File::Spec::Functions qw( rel2abs catdir catfile no_upwards splitpath );
sub uniq { my %seen; grep { not $seen{$_}++ } @_ }
@ -27,7 +27,7 @@ sub suggestions_from_path {
}
sub get_package_suggestions {
my ( $pkg ) = @_;
my ( $pkg, $prefix ) = @_;
my @segment = split /::|:\z/, $pkg, -1;
my $file_rx = qr/\A(${\quotemeta pop @segment}\w*)(?:\.pm|\.pod)?\z/;
@ -54,9 +54,49 @@ sub get_package_suggestions {
@suggestion = map { ":$_" } @suggestion;
}
# only add eventual prefix on first segment
if ($prefix && !@segment) {
@suggestion = map { $prefix . $_ } @suggestion;
}
return @suggestion;
}
sub get_file_suggestions {
my ($path) = @_;
my $dir;
if ($path) {
(undef, $dir, undef) = splitpath($path);
$dir = '.' if !$dir;
} else {
$dir = '.';
}
my $dh;
return unless opendir ($dh, $dir);
my @files = readdir($dh);
closedir $dh;
@files = map { catfile $dir, $_ } @files if $dir ne '.';
return filter($path, @files);
}
sub get_directory_suggestions {
my ($path, $prefix) = @_;
my @suggestions =
grep { -d $_}
get_file_suggestions($path);
if ($prefix) {
@suggestions = map { $prefix . $_ } @suggestions;
}
return @suggestions;
}
sub get_functions {
my $perlfunc;
@ -89,30 +129,69 @@ sub filter {
return grep { /$pattern/ } @list;
}
sub get_suggestions {
sub get_perldoc_suggestions {
my (@args) = @_;
my $word = pop @args;
my $cur = pop @args;
my $prev = pop @args;
if (@args) {
if ($args[-1] eq '-f') {
if ($prev) {
if ($prev eq '-f') {
return filter(
$word,
$cur,
get_functions
);
}
}
if ($word =~ /^-/) {
if ($cur =~ /^-/) {
return filter(
$word,
$cur,
qw/-h -D -t -u -m -l -F -i -v -V -T -r -d -o -M -w -n -X -L/
);
} else {
return get_package_suggestions($word);
return get_package_suggestions($cur);
}
}
sub get_perl_suggestions {
my (@args) = @_;
my $cur = pop @args;
my $prev = pop @args;
my $prefix;
if ($cur =~ /^(-\S)(\S*)/) {
$prev = $1;
$cur = $2;
$prefix = $prev;
}
if ($prev) {
if ($prev eq '-I' || $prev eq '-x') {
return get_directory_suggestions($cur, $prefix);
}
if ($prev eq '-m' || $prev eq '-M') {
return get_package_suggestions($cur, $prefix);
}
}
if ($cur =~ /^-/) {
return filter(
$cur,
qw/
-C -s -T -u -U -W -X -h -v -V -c -w -d -D
-p -n -a -F -l -0 -I -m -M -P -S -x -i -e
/
);
} else {
return get_file_suggestions($cur);
}
}
my ($cmd, @args) = get_command_line();
print "$_\n" for get_suggestions(@args);
print "$_\n" for
$cmd eq 'perl' ? get_perl_suggestions(@args) :
$cmd eq 'perldoc' ? get_perldoc_suggestions(@args) :
() ;

View File

@ -2,55 +2,9 @@
have perl &&
{
_perlmodules()
{
COMPREPLY=( $( compgen -P "$prefix" -W "$( perl -e 'sub mods { my ($base,$dir)=@_; return if $base !~ /^\Q$ENV{cur}/; chdir($dir) or return; for (glob(q[*.pm])) {s/\.pm$//; print qq[$base$_\n]}; mods(/^(?:[.\d]+|$Config{archname}-$Config{osname}|auto)$/ ? undef : qq[${base}${_}::],qq[$dir/$_]) for grep {-d} glob(q[*]); } mods(undef,$_) for @INC;' )" -- "$cur" ) )
__ltrim_colon_completions "$1"
}
complete -C ${BASH_SOURCE[0]%/*}/helpers/perl -o nospace -o default perl
_perl()
{
local cur prev prefix temp
local optPrefix optSuffix
COMPREPLY=()
_get_comp_words_by_ref -n : cur prev
prefix=""
# If option not followed by whitespace, reassign prev and cur
if [[ "$cur" == -?* ]]; then
temp=$cur
prev=${temp:0:2}
cur=${temp:2}
optPrefix=-P$prev
optSuffix=-S/
prefix=$prev
fi
# only handle module completion for now
case $prev in
-I|-x)
local IFS=$'\n'
_compopt_o_filenames
COMPREPLY=( $( compgen -d $optPrefix $optSuffix -- "$cur" ) )
return 0
;;
-m|-M)
_perlmodules "$cur"
return 0
;;
esac
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-C -s -T -u -U -W -X -h -v -V -c -w -d \
-D -p -n -a -F -l -0 -I -m -M -P -S -x -i -e ' -- "$cur" ) )
else
_filedir
fi
}
complete -F _perl -o nospace perl
complete -C ${BASH_SOURCE[0]%/*}/helpers/perldoc -o nospace -o default perldoc
complete -C ${BASH_SOURCE[0]%/*}/helpers/perl -o nospace -o default perldoc
}
# Local variables: