[f80ada8] | 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); |
---|
| 9 | |
---|
[d4ecc78] | 10 | my @all_colors = qw(default |
---|
| 11 | black |
---|
| 12 | blue |
---|
| 13 | cyan |
---|
| 14 | green |
---|
| 15 | magenta |
---|
| 16 | red |
---|
| 17 | white |
---|
| 18 | yellow); |
---|
| 19 | |
---|
[1610e5b] | 20 | my %show = ( |
---|
| 21 | information => undef, |
---|
| 22 | colors => undef, |
---|
| 23 | commands => undef, |
---|
| 24 | command => \&complete_command, |
---|
| 25 | filters => undef, |
---|
| 26 | filter => \&complete_filter_name, |
---|
| 27 | license => undef, |
---|
| 28 | quickstart => undef, |
---|
| 29 | startup => undef, |
---|
| 30 | status => undef, |
---|
| 31 | styles => undef, |
---|
| 32 | subscriptions => undef, |
---|
| 33 | subs => undef, |
---|
| 34 | terminal => undef, |
---|
| 35 | variables => undef, |
---|
| 36 | variable => \&complete_variable, |
---|
| 37 | version => undef, |
---|
| 38 | view => undef, |
---|
| 39 | zpunts => undef, |
---|
| 40 | ); |
---|
| 41 | |
---|
[f80ada8] | 42 | sub complete_command { return sort @BarnOwl::all_commands; } |
---|
[d4ecc78] | 43 | sub complete_color { return @all_colors; } |
---|
[1610e5b] | 44 | sub complete_filter_name { return @{BarnOwl::all_filters()}; } |
---|
[3ff3d86] | 45 | sub complete_variable { return @{BarnOwl::all_variables()}; } |
---|
| 46 | sub complete_style { return @{BarnOwl::all_styles()}; } |
---|
[f80ada8] | 47 | |
---|
[bc5e856] | 48 | my %filter_cmds = ( |
---|
| 49 | sender => \&BarnOwl::Complete::Zephyr::complete_user, |
---|
| 50 | recipient => \&BarnOwl::Complete::Zephyr::complete_user, |
---|
| 51 | class => \&BarnOwl::Complete::Zephyr::complete_class, |
---|
| 52 | instance => \&BarnOwl::Complete::Zephyr::complete_instance, |
---|
| 53 | opcode => \&BarnOwl::Complete::Zephyr::complete_opcode, |
---|
| 54 | realm => \&BarnOwl::Complete::Zephyr::complete_realm, |
---|
| 55 | body => undef, |
---|
| 56 | hostname => undef, |
---|
| 57 | type => sub { qw(zephyr aim admin); }, |
---|
| 58 | direction => sub { qw(in out none); }, |
---|
| 59 | login => sub { qw(login logout none); }, |
---|
| 60 | filter => \&complete_filter_name, |
---|
| 61 | perl => undef, |
---|
| 62 | ); |
---|
| 63 | |
---|
| 64 | # Returns: |
---|
| 65 | # - where to look next after pulling out an expression |
---|
| 66 | # - $INCOMPLETE if this cannot form a complete expression (or w/e) |
---|
| 67 | # - pushes to completion list as it finds valid completions |
---|
| 68 | |
---|
| 69 | my $INCOMPLETE = -1; |
---|
| 70 | sub _complete_filter_expr { |
---|
| 71 | # Takes as arguments context and the index into $ctx->words where the |
---|
| 72 | # filter expression starts |
---|
| 73 | my $ctx = shift; |
---|
| 74 | my $start = shift; |
---|
| 75 | my $o_comp = shift; |
---|
| 76 | my $end = $ctx->word; |
---|
| 77 | |
---|
| 78 | # Grab an expression; we don't allow empty |
---|
| 79 | my $i = $start; |
---|
| 80 | $i = _complete_filter_primitive_expr($ctx, $start, $o_comp); |
---|
| 81 | return $INCOMPLETE if $start == $INCOMPLETE; |
---|
| 82 | |
---|
| 83 | while ($i <= $end) { |
---|
| 84 | if ($i == $end) { |
---|
| 85 | # We could and/or another clause |
---|
| 86 | push @$o_comp, qw(and or); |
---|
| 87 | return $end; # Or we could let the parent do his thing |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if ($ctx->words->[$i] ne 'and' && $ctx->words->[$i] ne 'or') { |
---|
| 91 | return $i; # We're done. Let the parent do his thing |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | # Eat the and/or |
---|
| 95 | $i++; |
---|
| 96 | |
---|
| 97 | # Grab an expression |
---|
| 98 | $i = _complete_filter_primitive_expr($ctx, $i, $o_comp); |
---|
| 99 | return $INCOMPLETE if $i == $INCOMPLETE; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | return $i; # Well, it looks like we're happy |
---|
| 103 | # (Actually, I'm pretty sure this never happens...) |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | sub _complete_filter_primitive_expr { |
---|
| 107 | my $ctx = shift; |
---|
| 108 | my $start = shift; |
---|
| 109 | my $o_comp = shift; |
---|
| 110 | my $end = $ctx->word; |
---|
| 111 | |
---|
| 112 | if ($start >= $end) { |
---|
| 113 | push @$o_comp, "("; |
---|
| 114 | push @$o_comp, qw(true false not); |
---|
| 115 | push @$o_comp, keys %filter_cmds; |
---|
| 116 | return $INCOMPLETE; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | my $word = $ctx->words->[$start]; |
---|
| 120 | if ($word eq "(") { |
---|
| 121 | $start = _complete_filter_expr($ctx, $start+1, $o_comp); |
---|
| 122 | return $INCOMPLETE if $start == $INCOMPLETE; |
---|
| 123 | |
---|
| 124 | # Now, we expect a ")" |
---|
| 125 | if ($start >= $end) { |
---|
| 126 | push @$o_comp, ")"; |
---|
| 127 | return $INCOMPLETE; |
---|
| 128 | } |
---|
| 129 | if ($ctx->words->[$start] ne ')') { |
---|
| 130 | # User is being confusing. Give up. |
---|
| 131 | return $INCOMPLETE; |
---|
| 132 | } |
---|
| 133 | return $start+1; # Eat the ) |
---|
| 134 | } elsif ($word eq "not") { |
---|
| 135 | # We just want another primitive expression |
---|
| 136 | return _complete_filter_primitive_expr($ctx, $start+1, $o_comp); |
---|
| 137 | } elsif ($word eq "true" || $word eq "false") { |
---|
| 138 | # No arguments |
---|
| 139 | return $start+1; # Eat the boolean. Mmmm, tasty. |
---|
| 140 | } else { |
---|
| 141 | # It's of the form 'CMD ARG' |
---|
| 142 | return $start+2 if ($start+1 < $end); # The user supplied the argument |
---|
| 143 | |
---|
| 144 | # complete the argument |
---|
| 145 | my $arg_func = $filter_cmds{$word}; |
---|
| 146 | push @$o_comp, ($arg_func ? ($arg_func->()) : ()); |
---|
| 147 | return $INCOMPLETE; |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | sub complete_filter_expr { |
---|
| 152 | my $ctx = shift; |
---|
| 153 | my $start = shift; |
---|
| 154 | my @completions = (); |
---|
| 155 | _complete_filter_expr($ctx, $start, \@completions); |
---|
| 156 | # Get rid of duplicates and sort |
---|
| 157 | my %hash = (); |
---|
| 158 | @hash{@completions} = (); |
---|
| 159 | @completions = sort keys %hash; |
---|
| 160 | return @completions; |
---|
| 161 | } |
---|
| 162 | |
---|
[4fde21c] | 163 | sub complete_filter_args { |
---|
| 164 | my $ctx = shift; |
---|
| 165 | my $arg = shift; |
---|
[bc5e856] | 166 | return complete_filter_name() unless $arg; |
---|
| 167 | my $idx = 2; # skip the filter name |
---|
| 168 | while ($idx < $ctx->word) { |
---|
| 169 | last unless ($ctx->words->[$idx] =~ m{^-}); |
---|
| 170 | $idx += 2; # skip the flag and the argument |
---|
| 171 | } |
---|
| 172 | return complete_filter_expr($ctx, $idx); |
---|
[4fde21c] | 173 | } |
---|
| 174 | |
---|
[f80ada8] | 175 | sub complete_help { |
---|
| 176 | my $ctx = shift; |
---|
[7940ac2] | 177 | if($ctx->word == 1) { |
---|
| 178 | return complete_command(); |
---|
| 179 | } |
---|
[f80ada8] | 180 | } |
---|
| 181 | |
---|
[1610e5b] | 182 | sub complete_show { |
---|
| 183 | my $ctx = shift; |
---|
| 184 | if($ctx->word == 1) { |
---|
| 185 | return keys %show; |
---|
| 186 | } elsif ($ctx->word == 2) { |
---|
| 187 | my $cmd = $show{$ctx->words->[1]}; |
---|
| 188 | if($cmd) { |
---|
| 189 | return $cmd->($ctx); |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | |
---|
[d4ecc78] | 194 | sub complete_filter { |
---|
| 195 | my $ctx = shift; |
---|
| 196 | return complete_flags($ctx, |
---|
| 197 | [qw()], |
---|
| 198 | { |
---|
| 199 | "-c" => \&complete_color, |
---|
| 200 | "-b" => \&complete_color, |
---|
| 201 | }, |
---|
[4fde21c] | 202 | \&complete_filter_args |
---|
[d4ecc78] | 203 | ); |
---|
| 204 | } |
---|
| 205 | |
---|
[bd25f30] | 206 | sub complete_view { |
---|
| 207 | my $ctx = shift; |
---|
| 208 | if ($ctx->word == 1) { |
---|
[6bc35b4] | 209 | return ("--home", "-d", "-r", "-s", complete_filter_name()); |
---|
[bd25f30] | 210 | } |
---|
| 211 | if ($ctx->words->[1] eq "--home") { |
---|
| 212 | return; |
---|
| 213 | } |
---|
| 214 | if ($ctx->words->[1] eq "-d") { |
---|
| 215 | return complete_filter_expr($ctx, 2); |
---|
| 216 | } |
---|
[6bc35b4] | 217 | if ($ctx->words->[1] eq "-s") { |
---|
| 218 | return complete_style(); |
---|
| 219 | } |
---|
[bd25f30] | 220 | return; |
---|
| 221 | } |
---|
| 222 | |
---|
[28cf94b] | 223 | sub complete_getvar { |
---|
| 224 | my $ctx = shift; |
---|
| 225 | return unless ($ctx->word == 1); |
---|
| 226 | return complete_variable(); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | sub complete_set { |
---|
| 230 | my $ctx = shift; |
---|
| 231 | return complete_flags($ctx, |
---|
| 232 | [qw(-q)], |
---|
| 233 | { |
---|
| 234 | }, |
---|
| 235 | \&complete_set_args |
---|
| 236 | ); |
---|
| 237 | } |
---|
| 238 | sub complete_set_args { |
---|
| 239 | my $ctx = shift; |
---|
| 240 | my $arg = shift; |
---|
| 241 | return if $arg; |
---|
| 242 | return complete_variable(); |
---|
| 243 | } |
---|
| 244 | |
---|
[d4ecc78] | 245 | BarnOwl::Completion::register_completer(help => \&complete_help); |
---|
| 246 | BarnOwl::Completion::register_completer(filter => \&complete_filter); |
---|
[bd25f30] | 247 | BarnOwl::Completion::register_completer(view => \&complete_view); |
---|
[1610e5b] | 248 | BarnOwl::Completion::register_completer(show => \&complete_show); |
---|
[28cf94b] | 249 | BarnOwl::Completion::register_completer(getvar => \&complete_getvar); |
---|
| 250 | BarnOwl::Completion::register_completer(set => \&complete_set); |
---|
[d5ccf4e8] | 251 | BarnOwl::Completion::register_completer(unset => \&complete_set); |
---|
[f80ada8] | 252 | |
---|
| 253 | 1; |
---|