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

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