Changeset b73bcbb


Ignore:
Timestamp:
Jul 27, 2009, 11:23:57 PM (15 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
82a6e8b
Parents:
0fee298
git-author:
Nelson Elhage <nelhage@mit.edu> (07/27/09 21:17:06)
git-committer:
Nelson Elhage <nelhage@mit.edu> (07/27/09 23:23:57)
Message:
Add hooks for completing command arguments.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl/Completion.pm

    r0fee298 rb73bcbb  
    1919
    2020use List::Util qw(max first);
     21
     22our %completers = ();
    2123
    2224sub do_complete {
     
    6264}
    6365
    64 sub get_completions {
    65     my $ctx = shift;
    66     if($ctx->word == 0) {
    67         return complete_command($ctx->words->[0]);
    68     } else {
    69         return;
    70     }
    71 }
    72 
    73 sub complete_command {
    74     my $cmd = shift;
    75     return grep {$_ =~ m{^\Q$cmd\E}} @BarnOwl::all_commands;
    76 }
    77 
    7866sub show_completions {
    7967    my @words = @_;
     
    10189}
    10290
     91
     92sub get_completions {
     93    my $ctx = shift;
     94    my @words = ();
     95    if($ctx->word == 0) {
     96        return complete_command($ctx->words->[0]);
     97    } else {
     98        my $cmd = $ctx->words->[0];
     99        my $word = $ctx->words->[$ctx->word];
     100        if(exists($completers{$cmd})) {
     101            return grep {$_ =~ m{^\Q$word\E}} $completers{$cmd}->($ctx);
     102        }
     103        return;
     104    }
     105}
     106
     107sub complete_command {
     108    my $cmd = shift;
     109    return grep {$_ =~ m{^\Q$cmd\E}} @BarnOwl::all_commands;
     110}
     111
     112sub register_completer {
     113    my $cmd = shift;
     114    my $completer = shift;
     115    $completers{$cmd} = $completer;
     116}
     117
     118sub load_completers {
     119    opendir(my $dh, BarnOwl::get_data_dir() . "/" . "lib/BarnOwl/Complete/") or return;
     120    while(my $name = readdir($dh)) {
     121        next if $name =~ m{^\.};
     122        next unless $name =~ m{[.]pm$};
     123        $name =~ s{[.]pm$}{};
     124        eval "use BarnOwl::Complete::$name";
     125    }
     126}
     127
     128$BarnOwl::Hooks::startup->add("BarnOwl::Completion::load_completers");
     129
    1031301;
Note: See TracChangeset for help on using the changeset viewer.