From 767f310dcd887066c7263cfe6093f7529ffe631c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 23 Mar 2021 11:02:18 +0600 Subject: lei mark: add support for (bash) completion Only lightly tested, this seems to suffer from the same problem as external completions for network URLs with colons in them. In any case, its usable enough for me. The core LEI module now supports completions for lazy-loaded commands, too, so we'll be able to do completions for other commands more easily. --- lib/PublicInbox/LeiMark.pm | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'lib/PublicInbox/LeiMark.pm') diff --git a/lib/PublicInbox/LeiMark.pm b/lib/PublicInbox/LeiMark.pm index aa52ad5a..7b50aa51 100644 --- a/lib/PublicInbox/LeiMark.pm +++ b/lib/PublicInbox/LeiMark.pm @@ -174,4 +174,47 @@ sub ipc_atfork_child { PublicInbox::OnDestroy->new($$, \¬e_missing, $self); } +# Workaround bash word-splitting s to ['kw', ':', 'keyword' ...] +# Maybe there's a better way to go about this in +# contrib/completion/lei-completion.bash +sub _complete_mark_common ($) { + my ($argv) = @_; + # 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[+\-](?:kw|L)\z/ && + $x[1] ne ':') { + shift @x; + } + if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :) + $re = join('', @x); + } else { # just return everything and hope for the best + $re = join('', @$argv); + } + $re = quotemeta($re); + } + ($cur, $re); +} + +# FIXME: same problems as _complete_forget_external and similar +sub _complete_mark { + my ($self, @argv) = @_; + my @all = map { ("+kw:$_", "-kw:$_") } @KW; + return @all if !@argv; + my ($cur, $re) = _complete_mark_common(\@argv); + map { + # only return the part specified on the CLI + # don't duplicate if already 100% completed + /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : (); + } grep(/$re\Q$cur/, @all); +} + 1; -- cgit v1.2.3-24-ge0c7