summaryrefslogtreecommitdiffstats
path: root/TOOLS/zsh.pl
blob: d6f771417443f62baf339399f3753b5721595c92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/perl

# Generate ZSH completion

use strict;
use warnings;

my $mpv = $ARGV[0] || 'mpv';

my @opts = parse_main_opts('--list-options', '^ (\-\-[^\s\*]*)\*?\s*(.*)');

my @ao = parse_opts('--ao=help', '^  ([^\s\:]*)\s*: (.*)');
my @vo = parse_opts('--vo=help', '^  ([^\s\:]*)\s*: (.*)');

my @af = parse_opts('--af=help', '^  ([^\s\:]*)\s*: (.*)');
my @vf = parse_opts('--vf=help', '^  ([^\s\:]*)\s*: (.*)');

my @protos = parse_opts('--list-protocols', '^ ([^\s]*)');

my ($opts_str, $ao_str, $vo_str, $af_str, $vf_str, $protos_str);

$opts_str .= qq{  '$_' \\\n} foreach (@opts);
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;

$protos_str = join(' ', @protos);

my $profile_comp = <<'EOS';
    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]"
        fi
        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
      fi
    else
      # For --profile, multiple allowed
      profiles=($profiles 'help[list profiles]')
      _values -s , 'profile(s)' $profiles && rc=0
    fi
EOS
chomp $profile_comp;

my $tmpl = <<"EOS";
#compdef mpv

# For customization, see:
#  https://github.com/mpv-player/mpv/wiki/Zsh-completion-customization

local curcontext="\$curcontext" state state_descr line
typeset -A opt_args

# By default, don't complete URLs unless no files match
local -a tag_order
zstyle -a ":completion:*:*:\$service:*" tag-order tag_order || \
  zstyle  ":completion:*:*:\$service:*" tag-order '!urls'

local rc=1

_arguments -C -S \\
$opts_str
  '*:files:->mfiles' && rc=0

case \$state in
  ao)
    _values -s , 'audio outputs' \\
$ao_str
    && rc=0
  ;;

  vo)
    _values -s , 'video outputs' \\
$vo_str
    && rc=0
  ;;

  af)
    _values -s , 'audio filters' \\
$af_str
    && rc=0
  ;;

  vf)
    _values -s , 'video filters' \\
$vf_str
    && rc=0
  ;;

  profile|show-profile)
$profile_comp
  ;;

  files)
    compset -P '*,'
    compset -S ',*'
    _files -r ',/ \\t\\n\\-' && rc=0
  ;;

  mfiles)
    local expl
    _tags files urls
    while _tags; do
      _requested files expl 'media file' _files -g \\
        "*.(#i)(asf|asx|avi|flac|flv|m1v|m2p|m2v|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
      if _requested urls; then
        while _next_label urls expl URL; do
          _urls "\$expl[@]" && rc=0
          compadd -S '' "\$expl[@]" $protos_str && rc=0
        done
      fi
      (( rc )) || return 0
    done
  ;;
esac

return rc
EOS

print $tmpl;

sub parse_main_opts {
    my ($cmd, $regex) = @_;

    my @list;
    my @lines = call_mpv($cmd);

    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';
            } elsif ($name =~ /^--(ao|vo|af|vf|profile|show-profile)$/) {
                $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;
}

sub parse_opts {
    my ($cmd, $regex) = @_;

    my @list;
    my @lines = call_mpv($cmd);

    foreach my $line (@lines) {
        if ($line !~ /^$regex/) {
            next;
        }

        my $entry = $1;

        if (defined $2) {
            my $desc = $2;
            $desc =~ s/\:/\\:/g;
            $entry .= "[$desc]";
        }

        push @list, $entry
    }

    return @list;
}

sub call_mpv {
    my ($cmd) = @_;
    my $output = `"$mpv" --no-config $cmd`;
    if ($? == -1) {
        die "Could not run mpv: $!";
    } elsif ((my $exit_code = $? >> 8) != 0) {
        die "mpv returned $exit_code with output:\n$output";
    }
    return split /\n/, $output;
}