| 1 | package Psh::Strategy::Auto_cd; | ||
| 2 | |||
| 3 | =item * C<auto_cd> | ||
| 4 | |||
| 5 | If the input line matches the name of a directory then | ||
| 6 | it will be handled as an implicit cd. | ||
| 7 | |||
| 8 | =cut | ||
| 9 | |||
| 10 | require Psh::Strategy; | ||
| 11 | require Psh::Builtins::Cd; | ||
| 12 | |||
| 13 | @Psh::Strategy::Auto_cd::ISA=('Psh::Strategy'); | ||
| 14 | |||
| 15 | |||
| 16 | sub new { Psh::Strategy::new(@_) } | ||
| 17 | |||
| 18 | |||
| 19 | sub consumes { | ||
| 20 | return Psh::Strategy::CONSUME_TOKENS; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub applies { | ||
| 24 | my $dir= ${$_[2]}[0]; | ||
| 25 | return "auto-cd $dir" if -d $dir; | ||
| 26 | return ''; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub execute { | ||
| 30 | my $dir= ${$_[2]}[0]; | ||
| 31 | Psh::Builtins::Cd::bi_cd($dir); | ||
| 32 | return (1,undef); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub runs_before { | ||
| 36 | return qw(perlscript executable); | ||
| 37 | } | ||
| 38 | |||
| 39 | # Turn on directory completion for first words in line | ||
| 40 | $Psh::Completion::complete_first_word_dirs=1; | ||
| 41 | |||
| 42 | 1; |