[8eac1a5] | 1 | use warnings; |
---|
| 2 | use strict; |
---|
| 3 | |
---|
| 4 | =head1 NAME |
---|
| 5 | |
---|
| 6 | BarnOwl::Completion |
---|
| 7 | |
---|
| 8 | =head1 DESCRIPTION |
---|
| 9 | |
---|
| 10 | Hooks for tab-completion support in BarnOwl. |
---|
| 11 | |
---|
| 12 | =cut |
---|
| 13 | |
---|
| 14 | package BarnOwl::Completion; |
---|
| 15 | |
---|
| 16 | use BarnOwl::Completion::Context; |
---|
[6e48560] | 17 | use BarnOwl::Editwin qw(text_before_point text_after_point); |
---|
[8eac1a5] | 18 | |
---|
[6e48560] | 19 | use List::Util qw(max first); |
---|
[8eac1a5] | 20 | |
---|
[6e48560] | 21 | sub do_complete { |
---|
| 22 | my $cmd = shift; |
---|
| 23 | my $before = text_before_point(); |
---|
| 24 | my $after = text_after_point(); |
---|
| 25 | BarnOwl::debug("Completing: $before-|-$after"); |
---|
| 26 | my $ctx = BarnOwl::Completion::Context->new($before, $after); |
---|
| 27 | |
---|
| 28 | my @words = get_completions($ctx); |
---|
| 29 | return unless @words; |
---|
| 30 | my $prefix = common_prefix(@words); |
---|
| 31 | |
---|
| 32 | my $word = $ctx->words->[$ctx->word]; |
---|
| 33 | |
---|
| 34 | if($prefix && $prefix ne $word) { |
---|
| 35 | if(scalar @words == 1) { |
---|
| 36 | $prefix .= ' '; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | BarnOwl::Editwin::insert_text(substr($prefix, length($word))); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | if(scalar @words > 1) { |
---|
| 43 | show_completions(@words); |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | sub get_completions { |
---|
| 48 | my $ctx = shift; |
---|
| 49 | if($ctx->word == 0) { |
---|
| 50 | return complete_command($ctx->words->[0]); |
---|
| 51 | } else { |
---|
| 52 | return; |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | sub complete_command { |
---|
| 57 | my $cmd = shift; |
---|
| 58 | return grep {$_ =~ m{^\Q$cmd\E}} @BarnOwl::all_commands; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | sub show_completions { |
---|
| 62 | my @words = @_; |
---|
| 63 | my $all = join(" ", map {BarnOwl::quote($_)} @words); |
---|
| 64 | my $width = BarnOwl::getnumcols(); |
---|
| 65 | if (length($all) > $width-1) { |
---|
| 66 | $all = substr($all, 0, $width-4) . "..."; |
---|
| 67 | } |
---|
| 68 | BarnOwl::message($all); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | sub common_prefix { |
---|
| 72 | my @words = @_; |
---|
| 73 | my $len = max(map {length($_)} @words); |
---|
| 74 | my $pfx = ''; |
---|
| 75 | for my $i (1..$len) { |
---|
| 76 | $pfx = substr($words[0], 0, $i); |
---|
| 77 | if(first {substr($_, 0, $i) ne $pfx} @words) { |
---|
| 78 | $pfx = substr($pfx, 0, $i-1); |
---|
| 79 | last; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | return $pfx; |
---|
| 84 | } |
---|
[8eac1a5] | 85 | |
---|
| 86 | 1; |
---|