2014-06-08 13:52:20 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
# Generate ZSH completion
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
my $mpv = $ARGV[0] || 'mpv';
|
|
|
|
|
2015-03-30 03:15:59 +00:00
|
|
|
my @opts = parse_main_opts('--list-options', '^ (\-\-[^\s\*]*)\*?\s*(.*)');
|
2014-06-08 13:52:20 +00:00
|
|
|
|
2015-03-30 03:15:59 +00:00
|
|
|
my @ao = parse_opts('--ao=help', '^ ([^\s\:]*)\s*: (.*)');
|
|
|
|
my @vo = parse_opts('--vo=help', '^ ([^\s\:]*)\s*: (.*)');
|
2014-06-08 13:52:20 +00:00
|
|
|
|
2015-03-30 03:15:59 +00:00
|
|
|
my @af = parse_opts('--af=help', '^ ([^\s\:]*)\s*: (.*)');
|
|
|
|
my @vf = parse_opts('--vf=help', '^ ([^\s\:]*)\s*: (.*)');
|
2014-06-08 13:52:20 +00:00
|
|
|
|
2015-03-30 03:15:59 +00:00
|
|
|
my @protos = parse_opts('--list-protocols', '^ ([^\s]*)');
|
2014-07-01 00:42:00 +00:00
|
|
|
|
|
|
|
my ($opts_str, $ao_str, $vo_str, $af_str, $vf_str, $protos_str);
|
2014-06-08 13:52:20 +00:00
|
|
|
|
2014-06-29 05:54:48 +00:00
|
|
|
$opts_str .= qq{ '$_' \\\n} foreach (@opts);
|
2014-06-08 13:52:20 +00:00
|
|
|
chomp $opts_str;
|
|
|
|
|
|
|
|
$ao_str .= qq{ '$_' \\\n} foreach (@ao);
|
|
|
|
chomp $ao_str;
|
|
|
|
|
|
|
|
$vo_str .= qq{ '$_' \\\n} foreach (@vo);
|
|
|
|
chomp $vo_str;
|
|
|
|
|
|
|
|
$af_str .= qq{ '$_' \\\n} foreach (@af);
|
|
|
|
chomp $af_str;
|
|
|
|
|
|
|
|
$vf_str .= qq{ '$_' \\\n} foreach (@vf);
|
|
|
|
chomp $vf_str;
|
|
|
|
|
2015-03-30 03:30:10 +00:00
|
|
|
$protos_str = join(' ', @protos);
|
2014-07-01 00:42:00 +00:00
|
|
|
|
2016-04-15 20:51:00 +00:00
|
|
|
my $runtime_completions = <<'EOS';
|
|
|
|
profile|show-profile)
|
2015-03-30 03:30:10 +00:00
|
|
|
local -a profiles
|
|
|
|
local current
|
|
|
|
for current in "${(@f)$($words[1] --profile=help)}"; do
|
|
|
|
current=${current//\*/\\\*}
|
|
|
|
current=${current//\:/\\\:}
|
|
|
|
current=${current//\[/\\\[}
|
|
|
|
current=${current//\]/\\\]}
|
|
|
|
if [[ $current =~ $'\t'([^$'\t']*)$'\t'(.*) ]]; then
|
|
|
|
if [[ -n $match[2] ]]; then
|
|
|
|
current="$match[1][$match[2]]"
|
|
|
|
else
|
|
|
|
current="$match[1]"
|
2014-08-15 19:06:35 +00:00
|
|
|
fi
|
2015-03-30 03:30:10 +00:00
|
|
|
profiles=($profiles $current)
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [[ $state == show-profile ]]; then
|
|
|
|
# For --show-profile, only one allowed
|
|
|
|
if (( ${#profiles} > 0 )); then
|
|
|
|
_values 'profile' $profiles && rc=0
|
2014-08-15 19:06:35 +00:00
|
|
|
fi
|
2015-03-30 03:30:10 +00:00
|
|
|
else
|
|
|
|
# For --profile, multiple allowed
|
|
|
|
profiles=($profiles 'help[list profiles]')
|
|
|
|
_values -s , 'profile(s)' $profiles && rc=0
|
|
|
|
fi
|
2016-04-15 20:51:00 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
audio-device)
|
|
|
|
local -a audio_devices
|
|
|
|
local current
|
|
|
|
for current in "${(@f)$($words[1] --audio-device=help)}"; do
|
|
|
|
current=${current//\*/\\\*}
|
|
|
|
current=${current//\:/\\\:}
|
|
|
|
current=${current//\[/\\\[}
|
|
|
|
current=${current//\]/\\\]}
|
|
|
|
if [[ $current =~ ' '\'([^\']*)\'' \('(.*)'\)' ]]; then
|
|
|
|
audio_devices=($audio_devices "$match[1][$match[2]]")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
audio_devices=($audio_devices 'help[list audio devices]')
|
|
|
|
_values 'audio device' $audio_devices && rc=0
|
|
|
|
;;
|
2014-08-15 19:06:35 +00:00
|
|
|
EOS
|
2016-04-15 20:51:00 +00:00
|
|
|
chomp $runtime_completions;
|
2014-08-15 19:06:35 +00:00
|
|
|
|
2014-06-08 13:52:20 +00:00
|
|
|
my $tmpl = <<"EOS";
|
|
|
|
#compdef mpv
|
|
|
|
|
2016-04-08 03:36:27 +00:00
|
|
|
# For customization, see:
|
|
|
|
# https://github.com/mpv-player/mpv/wiki/Zsh-completion-customization
|
2014-06-08 13:52:20 +00:00
|
|
|
|
2014-08-15 19:22:44 +00:00
|
|
|
local curcontext="\$curcontext" state state_descr line
|
|
|
|
typeset -A opt_args
|
|
|
|
|
2016-04-08 03:36:27 +00:00
|
|
|
# By default, don't complete URLs unless no files match
|
|
|
|
local -a tag_order
|
2016-04-08 16:11:58 +00:00
|
|
|
zstyle -a ":completion:*:*:\$service:*" tag-order tag_order || \
|
|
|
|
zstyle ":completion:*:*:\$service:*" tag-order '!urls'
|
2016-04-08 03:36:27 +00:00
|
|
|
|
2014-08-13 21:42:37 +00:00
|
|
|
local rc=1
|
|
|
|
|
2014-06-29 19:08:40 +00:00
|
|
|
_arguments -C -S \\
|
2014-06-08 13:52:20 +00:00
|
|
|
$opts_str
|
2014-08-13 21:42:37 +00:00
|
|
|
'*:files:->mfiles' && rc=0
|
2014-06-08 13:52:20 +00:00
|
|
|
|
|
|
|
case \$state in
|
|
|
|
ao)
|
2015-03-30 04:10:38 +00:00
|
|
|
_values -s , 'audio outputs' \\
|
2014-06-08 13:52:20 +00:00
|
|
|
$ao_str
|
2015-03-30 04:10:38 +00:00
|
|
|
&& rc=0
|
2014-06-08 13:52:20 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
vo)
|
2015-03-30 04:10:38 +00:00
|
|
|
_values -s , 'video outputs' \\
|
2014-06-08 13:52:20 +00:00
|
|
|
$vo_str
|
2015-03-30 04:10:38 +00:00
|
|
|
&& rc=0
|
2014-06-08 13:52:20 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
af)
|
2015-03-30 04:10:38 +00:00
|
|
|
_values -s , 'audio filters' \\
|
2014-06-08 13:52:20 +00:00
|
|
|
$af_str
|
2015-03-30 04:10:38 +00:00
|
|
|
&& rc=0
|
2014-06-08 13:52:20 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
vf)
|
2015-03-30 04:10:38 +00:00
|
|
|
_values -s , 'video filters' \\
|
2014-06-08 13:52:20 +00:00
|
|
|
$vf_str
|
2015-03-30 04:10:38 +00:00
|
|
|
&& rc=0
|
2014-06-08 13:52:20 +00:00
|
|
|
;;
|
|
|
|
|
2016-04-15 20:51:00 +00:00
|
|
|
$runtime_completions
|
2014-08-15 19:06:35 +00:00
|
|
|
|
2014-08-14 10:07:36 +00:00
|
|
|
files)
|
|
|
|
compset -P '*,'
|
|
|
|
compset -S ',*'
|
|
|
|
_files -r ',/ \\t\\n\\-' && rc=0
|
|
|
|
;;
|
|
|
|
|
2014-06-08 13:52:20 +00:00
|
|
|
mfiles)
|
2014-08-15 19:22:44 +00:00
|
|
|
local expl
|
2014-06-08 13:52:20 +00:00
|
|
|
_tags files urls
|
|
|
|
while _tags; do
|
|
|
|
_requested files expl 'media file' _files -g \\
|
2016-08-26 18:43:55 +00:00
|
|
|
"*.(#i)(asf|asx|avi|f4v|flac|flv|m1v|m2p|m2v|m4a|m4v|mjpg|mka|mkv|mov|mp3|mp4|mpe|mpeg|mpg|ogg|ogm|ogv|opus|qt|rm|ts|vob|wav|webm|wma|wmv|wv)(-.)" && rc=0
|
2014-06-08 13:52:20 +00:00
|
|
|
if _requested urls; then
|
|
|
|
while _next_label urls expl URL; do
|
2014-08-15 19:08:14 +00:00
|
|
|
_urls "\$expl[@]" && rc=0
|
2014-08-13 21:42:37 +00:00
|
|
|
compadd -S '' "\$expl[@]" $protos_str && rc=0
|
2014-06-08 13:52:20 +00:00
|
|
|
done
|
|
|
|
fi
|
2014-08-13 21:42:37 +00:00
|
|
|
(( rc )) || return 0
|
2014-06-08 13:52:20 +00:00
|
|
|
done
|
|
|
|
;;
|
|
|
|
esac
|
2014-08-13 21:42:37 +00:00
|
|
|
|
|
|
|
return rc
|
2014-06-08 13:52:20 +00:00
|
|
|
EOS
|
|
|
|
|
|
|
|
print $tmpl;
|
|
|
|
|
2015-01-06 04:20:06 +00:00
|
|
|
sub parse_main_opts {
|
|
|
|
my ($cmd, $regex) = @_;
|
|
|
|
|
|
|
|
my @list;
|
2015-07-03 22:01:36 +00:00
|
|
|
my @lines = call_mpv($cmd);
|
2015-01-06 04:20:06 +00:00
|
|
|
|
|
|
|
foreach my $line (@lines) {
|
|
|
|
my ($name, $desc) = ($line =~ /^$regex/) or next;
|
|
|
|
|
|
|
|
next if ($desc eq 'removed' || $desc eq 'alias');
|
|
|
|
|
|
|
|
if ($desc =~ /^Flag/) {
|
|
|
|
|
|
|
|
push @list, $name;
|
|
|
|
|
|
|
|
$name =~ /^--(.*)/;
|
|
|
|
if ($1 !~ /^(\{|\}|v|list-options|really-quiet|no-.*)$/) {
|
|
|
|
push @list, "--no-$1";
|
|
|
|
}
|
|
|
|
|
|
|
|
} elsif ($desc =~ /^Print/) {
|
|
|
|
|
|
|
|
push @list, $name;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
# Option takes argument
|
|
|
|
|
|
|
|
my $entry = $name;
|
|
|
|
|
|
|
|
$desc =~ s/\:/\\:/g;
|
|
|
|
$entry .= "=-:$desc:";
|
|
|
|
|
|
|
|
if ($desc =~ /^Choices\\: ([^(]*)/) {
|
|
|
|
my $choices = $1;
|
|
|
|
$choices =~ s/ +$//; # strip trailing space
|
|
|
|
$entry .= "($choices)";
|
|
|
|
|
|
|
|
# If "no" is one of the choices, it can also be
|
|
|
|
# negated like a flag (--no-whatever).
|
|
|
|
if ($choices =~ /\bno\b/) {
|
|
|
|
$name =~ s/^--/--no-/;
|
|
|
|
push @list, $name;
|
|
|
|
}
|
|
|
|
} elsif ($line =~ /\[file\]/) {
|
|
|
|
$entry .= '->files';
|
2016-04-15 20:51:00 +00:00
|
|
|
} elsif ($name =~ /^--(ao|vo|af|vf|profile|show-profile|audio-device)$/) {
|
2015-01-06 04:20:06 +00:00
|
|
|
$entry .= "->$1";
|
|
|
|
}
|
|
|
|
push @list, $entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Sort longest first, because zsh won't complete an option listed
|
|
|
|
# after one that's a prefix of it.
|
|
|
|
@list = sort {
|
|
|
|
$a =~ /([^=]*)/; my $ma = $1;
|
|
|
|
$b =~ /([^=]*)/; my $mb = $1;
|
|
|
|
|
|
|
|
length($mb) <=> length($ma)
|
|
|
|
} @list;
|
|
|
|
|
|
|
|
return @list;
|
|
|
|
}
|
|
|
|
|
2014-06-08 13:52:20 +00:00
|
|
|
sub parse_opts {
|
2015-01-06 04:20:06 +00:00
|
|
|
my ($cmd, $regex) = @_;
|
2014-06-08 13:52:20 +00:00
|
|
|
|
2014-06-29 21:24:27 +00:00
|
|
|
my @list;
|
2015-07-03 22:01:36 +00:00
|
|
|
my @lines = call_mpv($cmd);
|
2014-06-08 13:52:20 +00:00
|
|
|
|
2014-06-29 21:24:27 +00:00
|
|
|
foreach my $line (@lines) {
|
|
|
|
if ($line !~ /^$regex/) {
|
|
|
|
next;
|
|
|
|
}
|
2014-06-08 13:52:20 +00:00
|
|
|
|
2014-06-29 21:24:27 +00:00
|
|
|
my $entry = $1;
|
2014-06-29 05:54:48 +00:00
|
|
|
|
2014-06-29 21:24:27 +00:00
|
|
|
if (defined $2) {
|
|
|
|
my $desc = $2;
|
|
|
|
$desc =~ s/\:/\\:/g;
|
2015-03-30 04:10:38 +00:00
|
|
|
$entry .= "[$desc]";
|
2014-06-29 21:24:27 +00:00
|
|
|
}
|
2014-06-08 13:52:20 +00:00
|
|
|
|
2014-06-29 21:24:27 +00:00
|
|
|
push @list, $entry
|
|
|
|
}
|
2014-06-08 13:52:20 +00:00
|
|
|
|
2014-06-29 21:24:27 +00:00
|
|
|
return @list;
|
2014-06-08 13:52:20 +00:00
|
|
|
}
|
2015-07-03 22:01:36 +00:00
|
|
|
|
|
|
|
sub call_mpv {
|
|
|
|
my ($cmd) = @_;
|
|
|
|
my $output = `"$mpv" --no-config $cmd`;
|
|
|
|
if ($? == -1) {
|
|
|
|
die "Could not run mpv: $!";
|
2015-09-19 23:26:22 +00:00
|
|
|
} elsif ((my $exit_code = $? >> 8) != 0) {
|
|
|
|
die "mpv returned $exit_code with output:\n$output";
|
2015-07-03 22:01:36 +00:00
|
|
|
}
|
|
|
|
return split /\n/, $output;
|
|
|
|
}
|