+sub canonicalize_excludes {
+ my ($lei, $excludes) = @_;
+ my %x;
+ for my $loc (@$excludes) {
+ my @l = get_externals($lei, $loc, 1);
+ $x{$_} = 1 for @l;
+ }
+ \%x;
+}
+
+# returns an anonymous sub which returns an array of potential results
+sub complete_url_prepare {
+ my $argv = $_[-1]; # $_[0] may be $lei
+ # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
+ # Maybe there's a better way to go about this in
+ # contrib/completion/lei-completion.bash
+ my $re = '';
+ my $cur = pop(@$argv) // '';
+ if (@$argv) {
+ my @x = @$argv;
+ if ($cur eq ':' && @x) {
+ push @x, $cur;
+ $cur = '';
+ }
+ while (@x > 2 && $x[0] !~ /\A(?:http|nntp|imap)s?\z/i &&
+ $x[1] ne ':') {
+ shift @x;
+ }
+ if (@x >= 2) { # qw(https : hostname : 443) or qw(http :)
+ $re = join('', @x);
+ } else { # just filter out the flags and hope for the best
+ $re = join('', grep(!/^-/, @$argv));
+ }
+ $re = quotemeta($re);
+ }
+ my $match_cb = sub {
+ # the "//;" here (for AUTH=ANONYMOUS) interacts badly with
+ # bash tab completion, strip it out for now since our commands
+ # work w/o it. Not sure if there's a better solution...
+ $_[0] =~ s!//;AUTH=ANONYMOUS\@!//!i;
+ $_[0] =~ s!;!\\;!g;
+ # only return the part specified on the CLI
+ # don't duplicate if already 100% completed
+ $_[0] =~ /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ()
+ };
+ wantarray ? ($re, $cur, $match_cb) : $match_cb;