[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, |
---|
| 39 | variables => undef, |
---|
| 40 | variable => \&complete_variable, |
---|
| 41 | version => undef, |
---|
| 42 | view => undef, |
---|
| 43 | zpunts => undef, |
---|
| 44 | ); |
---|
| 45 | |
---|
[f80ada8] | 46 | sub complete_command { return sort @BarnOwl::all_commands; } |
---|
[d4ecc78] | 47 | sub complete_color { return @all_colors; } |
---|
[3ff3d86] | 48 | sub complete_variable { return @{BarnOwl::all_variables()}; } |
---|
| 49 | sub complete_style { return @{BarnOwl::all_styles()}; } |
---|
[02a72bf] | 50 | sub complete_keymap { return @{BarnOwl::all_keymaps()}; } |
---|
[f80ada8] | 51 | |
---|
| 52 | sub complete_help { |
---|
| 53 | my $ctx = shift; |
---|
[7940ac2] | 54 | if($ctx->word == 1) { |
---|
| 55 | return complete_command(); |
---|
| 56 | } |
---|
[f80ada8] | 57 | } |
---|
| 58 | |
---|
[1610e5b] | 59 | sub complete_show { |
---|
| 60 | my $ctx = shift; |
---|
| 61 | if($ctx->word == 1) { |
---|
| 62 | return keys %show; |
---|
| 63 | } elsif ($ctx->word == 2) { |
---|
| 64 | my $cmd = $show{$ctx->words->[1]}; |
---|
| 65 | if($cmd) { |
---|
| 66 | return $cmd->($ctx); |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
[d4ecc78] | 71 | sub complete_filter { |
---|
| 72 | my $ctx = shift; |
---|
[76e0e4a] | 73 | # Syntax: filter FILTERNAME FLAGS EXPR |
---|
| 74 | |
---|
| 75 | # FILTERNAME |
---|
| 76 | return complete_filter_name() if $ctx->word == 1; |
---|
| 77 | |
---|
| 78 | # FLAGS |
---|
| 79 | $ctx = $ctx->shift_words(1); # complete_flags starts at the second word |
---|
[d4ecc78] | 80 | return complete_flags($ctx, |
---|
| 81 | [qw()], |
---|
| 82 | { |
---|
| 83 | "-c" => \&complete_color, |
---|
| 84 | "-b" => \&complete_color, |
---|
| 85 | }, |
---|
[76e0e4a] | 86 | # EXPR |
---|
| 87 | sub { |
---|
| 88 | my $ctx = shift; |
---|
| 89 | my $arg = shift; |
---|
| 90 | |
---|
| 91 | # We pass stop_at_nonflag, so we can rewind to the start |
---|
| 92 | my $idx = $ctx->word - $arg; |
---|
[6035008] | 93 | $ctx = $ctx->shift_words($idx); |
---|
| 94 | return complete_filter_expr($ctx); |
---|
[76e0e4a] | 95 | }, |
---|
| 96 | stop_at_nonflag => 1 |
---|
[d4ecc78] | 97 | ); |
---|
| 98 | } |
---|
| 99 | |
---|
[dab89e28] | 100 | sub complete_filter_no_flags |
---|
| 101 | { |
---|
| 102 | my $ctx = shift; |
---|
| 103 | # Syntax: filter FILTERNAME EXPR |
---|
| 104 | |
---|
| 105 | # FILTERNAME |
---|
| 106 | return complete_filter_name() if $ctx->word == 1; |
---|
| 107 | |
---|
| 108 | $ctx = $ctx->shift_words(2); |
---|
| 109 | return complete_filter_expr($ctx); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | sub complete_filter_append { |
---|
| 113 | my $ctx = shift; |
---|
| 114 | # Syntax: filterappend FILTERNAME EXPR |
---|
| 115 | |
---|
| 116 | # FILTERNAME |
---|
| 117 | return complete_filter_name() if $ctx->word == 1; |
---|
| 118 | return qw(and or) if $ctx->word == 2; |
---|
| 119 | $ctx = $ctx->shift_words(3); |
---|
| 120 | return complete_filter_expr($ctx); |
---|
| 121 | } |
---|
| 122 | |
---|
[bd25f30] | 123 | sub complete_view { |
---|
| 124 | my $ctx = shift; |
---|
| 125 | if ($ctx->word == 1) { |
---|
[6bc35b4] | 126 | return ("--home", "-d", "-r", "-s", complete_filter_name()); |
---|
[bd25f30] | 127 | } |
---|
| 128 | if ($ctx->words->[1] eq "--home") { |
---|
| 129 | return; |
---|
| 130 | } |
---|
| 131 | if ($ctx->words->[1] eq "-d") { |
---|
[6035008] | 132 | $ctx = $ctx->shift_words(2); |
---|
| 133 | return complete_filter_expr($ctx); |
---|
[bd25f30] | 134 | } |
---|
[6bc35b4] | 135 | if ($ctx->words->[1] eq "-s") { |
---|
| 136 | return complete_style(); |
---|
| 137 | } |
---|
[bd25f30] | 138 | return; |
---|
| 139 | } |
---|
| 140 | |
---|
[28cf94b] | 141 | sub complete_getvar { |
---|
| 142 | my $ctx = shift; |
---|
| 143 | return unless ($ctx->word == 1); |
---|
| 144 | return complete_variable(); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | sub complete_set { |
---|
| 148 | my $ctx = shift; |
---|
| 149 | return complete_flags($ctx, |
---|
| 150 | [qw(-q)], |
---|
| 151 | { |
---|
| 152 | }, |
---|
| 153 | \&complete_set_args |
---|
| 154 | ); |
---|
| 155 | } |
---|
| 156 | sub complete_set_args { |
---|
| 157 | my $ctx = shift; |
---|
| 158 | my $arg = shift; |
---|
| 159 | return if $arg; |
---|
| 160 | return complete_variable(); |
---|
| 161 | } |
---|
| 162 | |
---|
[b06a888] | 163 | sub 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] | 169 | sub 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] | 185 | sub complete_print { |
---|
| 186 | my $ctx = shift; |
---|
| 187 | return unless $ctx->word == 1; |
---|
| 188 | return complete_variable(); |
---|
| 189 | } |
---|
| 190 | |
---|
[dc8f6e0] | 191 | sub complete_one_file_arg { |
---|
| 192 | my $ctx = shift; |
---|
| 193 | return unless $ctx->word == 1; |
---|
| 194 | return complete_file($ctx->words->[1]); |
---|
| 195 | } |
---|
| 196 | |
---|
[d4ecc78] | 197 | BarnOwl::Completion::register_completer(help => \&complete_help); |
---|
| 198 | BarnOwl::Completion::register_completer(filter => \&complete_filter); |
---|
[dab89e28] | 199 | BarnOwl::Completion::register_completer(filteror => \&complete_filter_no_flags); |
---|
| 200 | BarnOwl::Completion::register_completer(filterand => \&complete_filter_no_flags); |
---|
| 201 | BarnOwl::Completion::register_completer(filterappend => \&complete_filter_append); |
---|
[bd25f30] | 202 | BarnOwl::Completion::register_completer(view => \&complete_view); |
---|
[1610e5b] | 203 | BarnOwl::Completion::register_completer(show => \&complete_show); |
---|
[28cf94b] | 204 | BarnOwl::Completion::register_completer(getvar => \&complete_getvar); |
---|
| 205 | BarnOwl::Completion::register_completer(set => \&complete_set); |
---|
[d5ccf4e8] | 206 | BarnOwl::Completion::register_completer(unset => \&complete_set); |
---|
[d973a73] | 207 | BarnOwl::Completion::register_completer(startup => \&complete_startup); |
---|
| 208 | BarnOwl::Completion::register_completer(unstartup => \&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; |
---|