/
word_expansion.pl
51 lines (35 loc) · 1.36 KB
/
word_expansion.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use Zoidberg::Shell;
# first get a reference to our shell object
my $shell = Zoidberg::Shell->current();
# zoiddevel(1) documents the stacks used by the parser
# here we add a subroutine to the "word_expansion" stack
$$shell{parser}->add( 'word_expansion',
sub {
my ($meta, @words) = @{ shift() }; # get a block
# simple search and replace for the pattern
# shell() is documented in Zoidberg::Shell it sets $@ on error
# on error we die without complaining and leave the pattern untouched
# so it can be checked for similar expansions,
# like the one done by Menu.pl
for (@words) {
/^=(\S+)$/ or next;
my $path = $shell->shell({die_silently => 1}, 'which', $1);
$_ = $path unless $@;
}
return [$meta, @words]; # return a block
} );
__END__
=head1 NAME
word_expansion.pl - example source script
=head1 DESCRIPTION
This script demonstrates how you can add an expansion to zoid's parser.
It can be sourced from within zoid with the C<source> builtin.
You can also put this code in your F<~/.zoidrc>.
The specific expansion implemented here replaces a word starting with
a '=' with the output of the 'which' command. For example:
zoid$ ls -l =ls
-rwxr-xr-x 1 root root 70204 2004-12-16 07:55 /bin/ls
This expansion seems to be a feature of B<zsh>, put this code in your
F<~/.zoidrc> if you like it.
=head1 AUTHOR
Jaap Karssenberg, E<lt>pardus@cpan.orgE<gt>