source: perl/lib/BarnOwl/Complete/Client.pm @ a95b564

Last change on this file since a95b564 was bf21c4e, checked in by Jason Gross <jasongross9@gmail.com>, 7 years ago
Correct tab-completion for set/unset They only allow -q at the front.
  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[f80ada8]1use strict;
2use warnings;
3
4# Completers for client state
5
6package BarnOwl::Complete::Client;
7
[dc8f6e0]8use BarnOwl::Completion::Util qw(complete_flags complete_file);
[a3a9eb7]9use BarnOwl::Complete::Filter qw(complete_filter_name complete_filter_expr);
[f80ada8]10
[d4ecc78]11my @all_colors = qw(default
12                    black
13                    blue
14                    cyan
15                    green
16                    magenta
17                    red
18                    white
19                    yellow);
20
[1610e5b]21my %show = (
22    colors      => undef,
23    commands    => undef,
24    command     => \&complete_command,
[02a72bf]25    errors      => undef,
[1610e5b]26    filters     => undef,
27    filter      => \&complete_filter_name,
28    license     => undef,
[02a72bf]29    keymaps     => undef,
30    keymap      => \&complete_keymap,
[1610e5b]31    quickstart  => undef,
32    startup     => undef,
33    status      => undef,
34    styles      => undef,
35    subscriptions       => undef,
36    subs        => undef,
37    terminal    => undef,
38    variables   => undef,
39    variable    => \&complete_variable,
40    version     => undef,
41    view        => undef,
42    zpunts      => undef,
43   );
44
[f80ada8]45sub complete_command { return sort @BarnOwl::all_commands; }
[d4ecc78]46sub complete_color { return @all_colors; }
[3ff3d86]47sub complete_variable    { return @{BarnOwl::all_variables()}; }
48sub complete_style       { return @{BarnOwl::all_styles()}; }
[02a72bf]49sub complete_keymap      { return @{BarnOwl::all_keymaps()}; }
[f80ada8]50
51sub complete_help {
52    my $ctx = shift;
[7940ac2]53    if($ctx->word == 1) {
54        return complete_command();
55    }
[f80ada8]56}
57
[1610e5b]58sub complete_show {
59    my $ctx = shift;
60    if($ctx->word == 1) {
61        return keys %show;
62    } elsif ($ctx->word == 2) {
63        my $cmd = $show{$ctx->words->[1]};
64        if($cmd) {
65            return $cmd->($ctx);
66        }
67    }
68}
69
[d4ecc78]70sub complete_filter {
71    my $ctx = shift;
[76e0e4a]72    # Syntax: filter FILTERNAME FLAGS EXPR
73
74    # FILTERNAME
75    return complete_filter_name() if $ctx->word == 1;
76
77    # FLAGS
78    $ctx = $ctx->shift_words(1); # complete_flags starts at the second word
[d4ecc78]79    return complete_flags($ctx,
80        [qw()],
81        {
82           "-c" => \&complete_color,
83           "-b" => \&complete_color,
84        },
[76e0e4a]85        # EXPR
86        sub {
87            my $ctx = shift;
88            my $arg = shift;
89
90            # We pass stop_at_nonflag, so we can rewind to the start
91            my $idx = $ctx->word - $arg;
[6035008]92            $ctx = $ctx->shift_words($idx);
93            return complete_filter_expr($ctx);
[76e0e4a]94        },
95        stop_at_nonflag => 1
[d4ecc78]96        );
97}
98
[dab89e28]99sub complete_filter_no_flags
100{
101    my $ctx = shift;
102    # Syntax: filter FILTERNAME EXPR
103
104    # FILTERNAME
105    return complete_filter_name() if $ctx->word == 1;
106
107    $ctx = $ctx->shift_words(2);
108    return complete_filter_expr($ctx);
109}
110
111sub complete_filter_append {
112    my $ctx = shift;
113    # Syntax: filterappend FILTERNAME EXPR
114
115    # FILTERNAME
116    return complete_filter_name() if $ctx->word == 1;
117    return qw(and or) if $ctx->word == 2;
118    $ctx = $ctx->shift_words(3);
119    return complete_filter_expr($ctx);
120}
121
[bd25f30]122sub complete_view {
123    my $ctx = shift;
124    if ($ctx->word == 1) {
[6bc35b4]125        return ("--home", "-d", "-r", "-s", complete_filter_name());
[bd25f30]126    }
127    if ($ctx->words->[1] eq "--home") {
128        return;
129    }
130    if ($ctx->words->[1] eq "-d") {
[6035008]131        $ctx = $ctx->shift_words(2);
132        return complete_filter_expr($ctx);
[bd25f30]133    }
[6bc35b4]134    if ($ctx->words->[1] eq "-s") {
135        return complete_style();
136    }
[bd25f30]137    return;
138}
139
[28cf94b]140sub complete_getvar {
141    my $ctx = shift;
142    return unless ($ctx->word == 1);
143    return complete_variable();
144}
145
146sub complete_set {
147    my $ctx = shift;
[bf21c4e]148    if ($ctx->word == 1) {
149        return ("-q", complete_variable());
150    }
151    if ($ctx->word == 2 && $ctx->words->[1] eq "-q") {
152        return complete_variable();
153    }
154    return;
[28cf94b]155}
156sub complete_set_args {
157    my $ctx = shift;
158    my $arg = shift;
159    return if $arg;
160    return complete_variable();
161}
162
[b06a888]163sub complete_startup {
164    my $ctx = shift;
165    my $new_ctx = $ctx->shift_words(1);
166    return BarnOwl::Completion::get_completions($new_ctx);
167}
168
[1dc839b]169sub complete_bindkey {
170    my $ctx = shift;
171    # bindkey KEYMAP KEYSEQ command COMMAND
172    #   0      1       2      3        4
173    if ($ctx->word == 1) {
174        return complete_keymap();
175    } elsif ($ctx->word == 2) {
176        return;
177    } elsif ($ctx->word == 3) {
178        return ('command');
179    } else {
180        my $new_ctx = $ctx->shift_words(4);
181        return BarnOwl::Completion::get_completions($new_ctx);
182    }
183}
184
[4d26776]185sub complete_print {
186    my $ctx = shift;
187    return unless $ctx->word == 1;
188    return complete_variable();
189}
190
[dc8f6e0]191sub complete_one_file_arg {
192    my $ctx = shift;
193    return unless $ctx->word == 1;
194    return complete_file($ctx->words->[1]);
195}
196
[d4ecc78]197BarnOwl::Completion::register_completer(help    => \&complete_help);
198BarnOwl::Completion::register_completer(filter  => \&complete_filter);
[dab89e28]199BarnOwl::Completion::register_completer(filteror        => \&complete_filter_no_flags);
200BarnOwl::Completion::register_completer(filterand       => \&complete_filter_no_flags);
201BarnOwl::Completion::register_completer(filterappend    => \&complete_filter_append);
[bd25f30]202BarnOwl::Completion::register_completer(view    => \&complete_view);
[1610e5b]203BarnOwl::Completion::register_completer(show    => \&complete_show);
[28cf94b]204BarnOwl::Completion::register_completer(getvar  => \&complete_getvar);
205BarnOwl::Completion::register_completer(set     => \&complete_set);
[d5ccf4e8]206BarnOwl::Completion::register_completer(unset   => \&complete_set);
[d973a73]207BarnOwl::Completion::register_completer(startup   => \&complete_startup);
208BarnOwl::Completion::register_completer(unstartup => \&complete_startup);
[1dc839b]209BarnOwl::Completion::register_completer(bindkey => \&complete_bindkey);
[4d26776]210BarnOwl::Completion::register_completer(print   => \&complete_print);
[f80ada8]211
[dc8f6e0]212BarnOwl::Completion::register_completer(source      => \&complete_one_file_arg);
213BarnOwl::Completion::register_completer('load-subs' => \&complete_one_file_arg);
214BarnOwl::Completion::register_completer(loadsubs    => \&complete_one_file_arg);
215BarnOwl::Completion::register_completer(loadloginsubs    => \&complete_one_file_arg);
216BarnOwl::Completion::register_completer(dump        => \&complete_one_file_arg);
217
[f80ada8]2181;
Note: See TracBrowser for help on using the repository browser.