Permalink
Browse files

Generate fish completion file

  • Loading branch information...
simon04 committed Feb 10, 2019
1 parent 9a36c0a commit 916c87ce8d013eed91333d22f17aa3f68f28d26c
Showing with 74 additions and 18 deletions.
  1. +1 −0 .gitignore
  2. +25 −0 configure.ac
  3. +14 −2 scripts/Makefile.am
  4. +34 −16 scripts/zsh.pl
@@ -53,6 +53,7 @@ mkinstalldirs
tags
test-driver
scripts/_curl
scripts/curl.fish
curl_fuzzer
curl_fuzzer_seed_corpus.zip
libstandaloneengine.a
@@ -3408,6 +3408,31 @@ case "$OPT_ZSH_FPATH" in
;;
esac

dnl **********************************************************************
dnl Check for fish completion path
dnl **********************************************************************

OPT_FISH_FPATH=default
AC_ARG_WITH(fish-functions-dir,
AC_HELP_STRING([--with-fish-functions-dir=PATH],[Install fish completions to PATH])
AC_HELP_STRING([--without-fish-functions-dir],[Do not install fish completions]),
[OPT_FISH_FPATH=$withval])
case "$OPT_FISH_FPATH" in
no)
dnl --without-fish-functions-dir option used
;;
default|yes)
dnl --with-fish-functions-dir option used without path
FISH_FUNCTIONS_DIR="$datarootdir/fish/completions"
AC_SUBST(FISH_FUNCTIONS_DIR)
;;
*)
dnl --with-fish-functions-dir option used with path
FISH_FUNCTIONS_DIR="$withval"
AC_SUBST(FISH_FUNCTIONS_DIR)
;;
esac

dnl **********************************************************************
dnl Back to "normal" configuring
dnl **********************************************************************
@@ -20,13 +20,15 @@
#
###########################################################################
ZSH_FUNCTIONS_DIR = @ZSH_FUNCTIONS_DIR@
FISH_FUNCTIONS_DIR = @FISH_FUNCTIONS_DIR@
PERL = @PERL@

ZSH_COMPLETION_FUNCTION_FILENAME = _curl
FISH_COMPLETION_FUNCTION_FILENAME = curl.fish

CLEANFILES = $(ZSH_COMPLETION_FUNCTION_FILENAME)
CLEANFILES = $(ZSH_COMPLETION_FUNCTION_FILENAME) $(FISH_COMPLETION_FUNCTION_FILENAME)

all-local: $(ZSH_COMPLETION_FUNCTION_FILENAME)
all-local: $(ZSH_COMPLETION_FUNCTION_FILENAME) $(FISH_COMPLETION_FUNCTION_FILENAME)

$(ZSH_COMPLETION_FUNCTION_FILENAME): zsh.pl
if CROSSCOMPILING
@@ -36,10 +38,20 @@ else # if not cross-compiling:
$(PERL) $(srcdir)/zsh.pl $(top_builddir)/src/curl$(EXEEXT) > $@
endif

$(FISH_COMPLETION_FUNCTION_FILENAME): zsh.pl
if CROSSCOMPILING
@echo "NOTICE: we can't generate fish completion when cross-compiling!"
else # if not cross-compiling:
@if ! test -x "$(PERL)"; then echo "No perl: can't install zsh.pl"; exit 0; fi
$(PERL) $(srcdir)/zsh.pl $(top_builddir)/src/curl$(EXEEXT) fish > $@
endif

install-data-local:
if CROSSCOMPILING
@echo "NOTICE: we can't install zsh completion when cross-compiling!"
else # if not cross-compiling:
$(MKDIR_P) $(DESTDIR)$(ZSH_FUNCTIONS_DIR)
$(MKDIR_P) $(DESTDIR)$(FISH_FUNCTIONS_DIR)
$(INSTALL_DATA) $(ZSH_COMPLETION_FUNCTION_FILENAME) $(DESTDIR)$(ZSH_FUNCTIONS_DIR)/$(ZSH_COMPLETION_FUNCTION_FILENAME)
$(INSTALL_DATA) $(FISH_COMPLETION_FUNCTION_FILENAME) $(DESTDIR)$(FISH_FUNCTIONS_DIR)/$(FISH_COMPLETION_FUNCTION_FILENAME)
endif
@@ -1,19 +1,25 @@
#!/usr/bin/env perl

# Generate ZSH completion
# Generate ZSH or FISH completion
# Usage: zsh.pl [path to curl] [fish|zsh]

use strict;
use warnings;

my $curl = $ARGV[0] || 'curl';
my $shell = $ARGV[1] || 'zsh'; # zsh or fish

my $regex = '\s+(?:(-[^\s]+),\s)?(--[^\s]+)\s*(\<.+?\>)?\s+(.*)';
my @opts = parse_main_opts('--help', $regex);

my $opts_str;
if ($shell eq 'fish') {
print "# curl fish completion\n\n";
print qq{$_ \n} foreach (@opts);
} elsif ($shell eq 'zsh') {
my $opts_str;

$opts_str .= qq{ $_ \\\n} foreach (@opts);
chomp $opts_str;
$opts_str .= qq{ $_ \\\n} foreach (@opts);
chomp $opts_str;

my $tmpl = <<"EOS";
#compdef curl
@@ -32,7 +38,8 @@
return rc
EOS

print $tmpl;
print $tmpl;
}

sub parse_main_opts {
my ($cmd, $regex) = @_;
@@ -52,16 +59,26 @@ sub parse_main_opts {
$desc =~ s/\]/\\\]/g if defined $desc;
$desc =~ s/\:/\\\:/g if defined $desc;

$option .= '{' . trim($short) . ',' if defined $short;
$option .= trim($long) if defined $long;
$option .= '}' if defined $short;
$option .= '\'[' . trim($desc) . ']\'' if defined $desc;

$option .= ":'$arg'" if defined $arg;

$option .= ':_files'
if defined $arg and ($arg eq '<file>' || $arg eq '<filename>'
|| $arg eq '<dir>');
if ($shell eq 'fish') {
$option .= "complete --command curl";
$option .= " --short-option '" . strip_dash(trim($short)) . "'"
if defined $short;
$option .= " --long-option '" . strip_dash(trim($long)) . "'"
if defined $long;
$option .= " --description '" . strip_dash(trim($desc)) . "'"
if defined $desc;
} elsif ($shell eq 'zsh') {
$option .= '{' . trim($short) . ',' if defined $short;
$option .= trim($long) if defined $long;
$option .= '}' if defined $short;
$option .= '\'[' . trim($desc) . ']\'' if defined $desc;

$option .= ":'$arg'" if defined $arg;

$option .= ':_files'
if defined $arg and ($arg eq '<file>' || $arg eq '<filename>'
|| $arg eq '<dir>');
}

push @list, $option;
}
@@ -73,12 +90,13 @@ sub parse_main_opts {
$b =~ /([^=]*)/; my $mb = $1;

length($mb) <=> length($ma)
} @list;
} @list if $shell eq 'zsh';

return @list;
}

sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
sub strip_dash { my $s = shift; $s =~ s/^-+//g; return $s };

sub call_curl {
my ($cmd) = @_;

0 comments on commit 916c87c

Please sign in to comment.