[f80ada8] | 1 | use strict; |
---|
| 2 | use warnings; |
---|
| 3 | |
---|
| 4 | # Completers for client state |
---|
| 5 | |
---|
| 6 | package BarnOwl::Complete::Client; |
---|
| 7 | |
---|
[dc8f6e0] | 8 | use BarnOwl::Completion::Util qw(complete_flags complete_file); |
---|
[a3a9eb7] | 9 | use BarnOwl::Complete::Filter qw(complete_filter_name complete_filter_expr); |
---|
[f80ada8] | 10 | |
---|
[d4ecc78] | 11 | my @all_colors = qw(default |
---|
| 12 | black |
---|
| 13 | blue |
---|
| 14 | cyan |
---|
| 15 | green |
---|
| 16 | magenta |
---|
| 17 | red |
---|
| 18 | white |
---|
| 19 | yellow); |
---|
| 20 | |
---|
[1610e5b] | 21 | my %show = ( |
---|
| 22 | information => undef, |
---|
| 23 | colors => undef, |
---|
| 24 | commands => undef, |
---|
| 25 | command => \&complete_command, |
---|
[02a72bf] | 26 | errors => undef, |
---|
[1610e5b] | 27 | filters => undef, |
---|
| 28 | filter => \&complete_filter_name, |
---|
| 29 | license => undef, |
---|
[02a72bf] | 30 | keymaps => undef, |
---|
| 31 | keymap => \&complete_keymap, |
---|
[1610e5b] | 32 | quickstart => undef, |
---|
| 33 | startup => undef, |
---|
| 34 | status => undef, |
---|
| 35 | styles => undef, |
---|
| 36 | subscriptions => undef, |
---|
| 37 | subs => undef, |
---|
| 38 | terminal => undef, |
---|
[c6adf17] | 39 | timers => undef, |
---|
[1610e5b] | 40 | variables => undef, |
---|
| 41 | variable => \&complete_variable, |
---|
| 42 | version => undef, |
---|
| 43 | view => undef, |
---|
| 44 | zpunts => undef, |
---|
| 45 | ); |
---|
| 46 | |
---|
[f80ada8] | 47 | sub complete_command { return sort @BarnOwl::all_commands; } |
---|
[d4ecc78] | 48 | sub complete_color { return @all_colors; } |
---|
[3ff3d86] | 49 | sub complete_variable { return @{BarnOwl::all_variables()}; } |
---|
| 50 | sub complete_style { return @{BarnOwl::all_styles()}; } |
---|
[02a72bf] | 51 | sub complete_keymap { return @{BarnOwl::all_keymaps()}; } |
---|
[f80ada8] | 52 | |
---|
| 53 | sub complete_help { |
---|
| 54 | my $ctx = shift; |
---|
[7940ac2] | 55 | if($ctx->word == 1) { |
---|
| 56 | return complete_command(); |
---|
| 57 | } |
---|
[f80ada8] | 58 | } |
---|
| 59 | |
---|
[1610e5b] | 60 | sub 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 | |
---|
[d4ecc78] | 72 | sub complete_filter { |
---|
| 73 | my $ctx = shift; |
---|
[76e0e4a] | 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 |
---|
[d4ecc78] | 81 | return complete_flags($ctx, |
---|
| 82 | [qw()], |
---|
| 83 | { |
---|
| 84 | "-c" => \&complete_color, |
---|
| 85 | "-b" => \&complete_color, |
---|
| 86 | }, |
---|
[76e0e4a] | 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; |
---|
[6035008] | 94 | $ctx = $ctx->shift_words($idx); |
---|
| 95 | return complete_filter_expr($ctx); |
---|
[76e0e4a] | 96 | }, |
---|
| 97 | stop_at_nonflag => 1 |
---|
[d4ecc78] | 98 | ); |
---|
| 99 | } |
---|
| 100 | |
---|
[dab89e28] | 101 | sub 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 | |
---|
| 113 | sub 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 | |
---|
[bd25f30] | 124 | sub complete_view { |
---|
| 125 | my $ctx = shift; |
---|
| 126 | if ($ctx->word == 1) { |
---|
[6bc35b4] | 127 | return ("--home", "-d", "-r", "-s", complete_filter_name()); |
---|
[bd25f30] | 128 | } |
---|
| 129 | if ($ctx->words->[1] eq "--home") { |
---|
| 130 | return; |
---|
| 131 | } |
---|
| 132 | if ($ctx->words->[1] eq "-d") { |
---|
[6035008] | 133 | $ctx = $ctx->shift_words(2); |
---|
| 134 | return complete_filter_expr($ctx); |
---|
[bd25f30] | 135 | } |
---|
[6bc35b4] | 136 | if ($ctx->words->[1] eq "-s") { |
---|
| 137 | return complete_style(); |
---|
| 138 | } |
---|
[bd25f30] | 139 | return; |
---|
| 140 | } |
---|
| 141 | |
---|
[28cf94b] | 142 | sub complete_getvar { |
---|
| 143 | my $ctx = shift; |
---|
| 144 | return unless ($ctx->word == 1); |
---|
| 145 | return complete_variable(); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | sub complete_set { |
---|
| 149 | my $ctx = shift; |
---|
| 150 | return complete_flags($ctx, |
---|
| 151 | [qw(-q)], |
---|
| 152 | { |
---|
| 153 | }, |
---|
| 154 | \&complete_set_args |
---|
| 155 | ); |
---|
| 156 | } |
---|
| 157 | sub complete_set_args { |
---|
| 158 | my $ctx = shift; |
---|
| 159 | my $arg = shift; |
---|
| 160 | return if $arg; |
---|
| 161 | return complete_variable(); |
---|
| 162 | } |
---|
| 163 | |
---|
[b06a888] | 164 | sub complete_startup { |
---|
| 165 | my $ctx = shift; |
---|
| 166 | my $new_ctx = $ctx->shift_words(1); |
---|
| 167 | return BarnOwl::Completion::get_completions($new_ctx); |
---|
| 168 | } |
---|
| 169 | |
---|
[1dc839b] | 170 | sub 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 | |
---|
[4d26776] | 186 | sub complete_print { |
---|
| 187 | my $ctx = shift; |
---|
| 188 | return unless $ctx->word == 1; |
---|
| 189 | return complete_variable(); |
---|
| 190 | } |
---|
| 191 | |
---|
[dc8f6e0] | 192 | sub complete_one_file_arg { |
---|
| 193 | my $ctx = shift; |
---|
| 194 | return unless $ctx->word == 1; |
---|
| 195 | return complete_file($ctx->words->[1]); |
---|
| 196 | } |
---|
| 197 | |
---|
[d4ecc78] | 198 | BarnOwl::Completion::register_completer(help => \&complete_help); |
---|
| 199 | BarnOwl::Completion::register_completer(filter => \&complete_filter); |
---|
[dab89e28] | 200 | BarnOwl::Completion::register_completer(filteror => \&complete_filter_no_flags); |
---|
| 201 | BarnOwl::Completion::register_completer(filterand => \&complete_filter_no_flags); |
---|
| 202 | BarnOwl::Completion::register_completer(filterappend => \&complete_filter_append); |
---|
[bd25f30] | 203 | BarnOwl::Completion::register_completer(view => \&complete_view); |
---|
[1610e5b] | 204 | BarnOwl::Completion::register_completer(show => \&complete_show); |
---|
[28cf94b] | 205 | BarnOwl::Completion::register_completer(getvar => \&complete_getvar); |
---|
| 206 | BarnOwl::Completion::register_completer(set => \&complete_set); |
---|
[d5ccf4e8] | 207 | BarnOwl::Completion::register_completer(unset => \&complete_set); |
---|
[b06a888] | 208 | BarnOwl::Completion::register_completer(startup => \&complete_startup); |
---|
[1dc839b] | 209 | BarnOwl::Completion::register_completer(bindkey => \&complete_bindkey); |
---|
[4d26776] | 210 | BarnOwl::Completion::register_completer(print => \&complete_print); |
---|
[f80ada8] | 211 | |
---|
[dc8f6e0] | 212 | BarnOwl::Completion::register_completer(source => \&complete_one_file_arg); |
---|
| 213 | BarnOwl::Completion::register_completer('load-subs' => \&complete_one_file_arg); |
---|
| 214 | BarnOwl::Completion::register_completer(loadsubs => \&complete_one_file_arg); |
---|
| 215 | BarnOwl::Completion::register_completer(loadloginsubs => \&complete_one_file_arg); |
---|
| 216 | BarnOwl::Completion::register_completer(dump => \&complete_one_file_arg); |
---|
| 217 | |
---|
[f80ada8] | 218 | 1; |
---|