| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | |
|---|
| 4 | # Completers for client state |
|---|
| 5 | |
|---|
| 6 | package BarnOwl::Complete::Client; |
|---|
| 7 | |
|---|
| 8 | use BarnOwl::Completion::Util qw(complete_flags complete_file); |
|---|
| 9 | use BarnOwl::Complete::Filter qw(complete_filter_name complete_filter_expr); |
|---|
| 10 | |
|---|
| 11 | my @all_colors = qw(default |
|---|
| 12 | black |
|---|
| 13 | blue |
|---|
| 14 | cyan |
|---|
| 15 | green |
|---|
| 16 | magenta |
|---|
| 17 | red |
|---|
| 18 | white |
|---|
| 19 | yellow); |
|---|
| 20 | |
|---|
| 21 | my %show = ( |
|---|
| 22 | colors => undef, |
|---|
| 23 | commands => undef, |
|---|
| 24 | command => \&complete_command, |
|---|
| 25 | errors => undef, |
|---|
| 26 | filters => undef, |
|---|
| 27 | filter => \&complete_filter_name, |
|---|
| 28 | license => undef, |
|---|
| 29 | keymaps => undef, |
|---|
| 30 | keymap => \&complete_keymap, |
|---|
| 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 | |
|---|
| 45 | sub complete_command { return sort @BarnOwl::all_commands; } |
|---|
| 46 | sub complete_color { return @all_colors; } |
|---|
| 47 | sub complete_variable { return @{BarnOwl::all_variables()}; } |
|---|
| 48 | sub complete_style { return @{BarnOwl::all_styles()}; } |
|---|
| 49 | sub complete_keymap { return @{BarnOwl::all_keymaps()}; } |
|---|
| 50 | |
|---|
| 51 | sub complete_help { |
|---|
| 52 | my $ctx = shift; |
|---|
| 53 | if($ctx->word == 1) { |
|---|
| 54 | return complete_command(); |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | sub 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 | |
|---|
| 70 | sub complete_filter { |
|---|
| 71 | my $ctx = shift; |
|---|
| 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 |
|---|
| 79 | return complete_flags($ctx, |
|---|
| 80 | [qw()], |
|---|
| 81 | { |
|---|
| 82 | "-c" => \&complete_color, |
|---|
| 83 | "-b" => \&complete_color, |
|---|
| 84 | }, |
|---|
| 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; |
|---|
| 92 | $ctx = $ctx->shift_words($idx); |
|---|
| 93 | return complete_filter_expr($ctx); |
|---|
| 94 | }, |
|---|
| 95 | stop_at_nonflag => 1 |
|---|
| 96 | ); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | sub 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 | |
|---|
| 111 | sub 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 | |
|---|
| 122 | sub complete_view { |
|---|
| 123 | my $ctx = shift; |
|---|
| 124 | if ($ctx->word == 1) { |
|---|
| 125 | return ("--home", "-d", "-r", "-s", complete_filter_name()); |
|---|
| 126 | } |
|---|
| 127 | if ($ctx->words->[1] eq "--home") { |
|---|
| 128 | return; |
|---|
| 129 | } |
|---|
| 130 | if ($ctx->words->[1] eq "-d") { |
|---|
| 131 | $ctx = $ctx->shift_words(2); |
|---|
| 132 | return complete_filter_expr($ctx); |
|---|
| 133 | } |
|---|
| 134 | if ($ctx->words->[1] eq "-s") { |
|---|
| 135 | return complete_style(); |
|---|
| 136 | } |
|---|
| 137 | return; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | sub complete_getvar { |
|---|
| 141 | my $ctx = shift; |
|---|
| 142 | return unless ($ctx->word == 1); |
|---|
| 143 | return complete_variable(); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | sub complete_set { |
|---|
| 147 | my $ctx = shift; |
|---|
| 148 | my $is_unset = ($ctx->words->[0] eq "unset"); |
|---|
| 149 | |
|---|
| 150 | # Shift away the -q if seen. |
|---|
| 151 | my $seen_q = 0; |
|---|
| 152 | if ($ctx->word > 1 && $ctx->words->[1] eq "-q") { |
|---|
| 153 | $seen_q = 1; |
|---|
| 154 | $ctx = $ctx->shift_words(1); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | # First argument is the variable. |
|---|
| 158 | if ($ctx->word == 1) { |
|---|
| 159 | my @vars = complete_variable(); |
|---|
| 160 | if ($is_unset) { |
|---|
| 161 | # Only complete the variables which are unsettable. |
|---|
| 162 | @vars = grep { BarnOwl::get_variable_info($_)->{takes_on_off} } @vars; |
|---|
| 163 | } |
|---|
| 164 | unshift(@vars, "-q") unless $seen_q; |
|---|
| 165 | return @vars; |
|---|
| 166 | } |
|---|
| 167 | # For set, second argument is the value. |
|---|
| 168 | if (!$is_unset && $ctx->word == 2) { |
|---|
| 169 | # Parse what we can out of validsettings. |
|---|
| 170 | my $info; |
|---|
| 171 | eval { $info = BarnOwl::get_variable_info($ctx->words->[1]) }; |
|---|
| 172 | return if $@; |
|---|
| 173 | if ($info->{validsettings} eq "<path>") { |
|---|
| 174 | return complete_file($ctx->words->[2]); |
|---|
| 175 | } elsif ($info->{validsettings} !~ /^<.*>$/) { |
|---|
| 176 | # Assume it's a comma-separated list of values; |
|---|
| 177 | return split(",", $info->{validsettings}); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | return; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | sub complete_startup { |
|---|
| 184 | my $ctx = shift; |
|---|
| 185 | my $new_ctx = $ctx->shift_words(1); |
|---|
| 186 | return BarnOwl::Completion::get_completions($new_ctx); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | sub complete_bindkey { |
|---|
| 190 | my $ctx = shift; |
|---|
| 191 | # bindkey KEYMAP KEYSEQ command COMMAND |
|---|
| 192 | # 0 1 2 3 4 |
|---|
| 193 | if ($ctx->word == 1) { |
|---|
| 194 | return complete_keymap(); |
|---|
| 195 | } elsif ($ctx->word == 2) { |
|---|
| 196 | return; |
|---|
| 197 | } elsif ($ctx->word == 3) { |
|---|
| 198 | return ('command'); |
|---|
| 199 | } else { |
|---|
| 200 | my $new_ctx = $ctx->shift_words(4); |
|---|
| 201 | return BarnOwl::Completion::get_completions($new_ctx); |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | sub complete_print { |
|---|
| 206 | my $ctx = shift; |
|---|
| 207 | return unless $ctx->word == 1; |
|---|
| 208 | return complete_variable(); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | sub complete_one_file_arg { |
|---|
| 212 | my $ctx = shift; |
|---|
| 213 | return unless $ctx->word == 1; |
|---|
| 214 | return complete_file($ctx->words->[1]); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | BarnOwl::Completion::register_completer(help => \&complete_help); |
|---|
| 218 | BarnOwl::Completion::register_completer(filter => \&complete_filter); |
|---|
| 219 | BarnOwl::Completion::register_completer(filteror => \&complete_filter_no_flags); |
|---|
| 220 | BarnOwl::Completion::register_completer(filterand => \&complete_filter_no_flags); |
|---|
| 221 | BarnOwl::Completion::register_completer(filterappend => \&complete_filter_append); |
|---|
| 222 | BarnOwl::Completion::register_completer(view => \&complete_view); |
|---|
| 223 | BarnOwl::Completion::register_completer(show => \&complete_show); |
|---|
| 224 | BarnOwl::Completion::register_completer(getvar => \&complete_getvar); |
|---|
| 225 | BarnOwl::Completion::register_completer(set => \&complete_set); |
|---|
| 226 | BarnOwl::Completion::register_completer(unset => \&complete_set); |
|---|
| 227 | BarnOwl::Completion::register_completer(startup => \&complete_startup); |
|---|
| 228 | BarnOwl::Completion::register_completer(unstartup => \&complete_startup); |
|---|
| 229 | BarnOwl::Completion::register_completer(bindkey => \&complete_bindkey); |
|---|
| 230 | BarnOwl::Completion::register_completer(print => \&complete_print); |
|---|
| 231 | |
|---|
| 232 | BarnOwl::Completion::register_completer(source => \&complete_one_file_arg); |
|---|
| 233 | BarnOwl::Completion::register_completer('load-subs' => \&complete_one_file_arg); |
|---|
| 234 | BarnOwl::Completion::register_completer(loadsubs => \&complete_one_file_arg); |
|---|
| 235 | BarnOwl::Completion::register_completer(loadloginsubs => \&complete_one_file_arg); |
|---|
| 236 | BarnOwl::Completion::register_completer(dump => \&complete_one_file_arg); |
|---|
| 237 | |
|---|
| 238 | 1; |
|---|