-
Notifications
You must be signed in to change notification settings - Fork 2
/
emacs-file-local-variable-prop-line.pl
executable file
·72 lines (62 loc) · 1.62 KB
/
emacs-file-local-variable-prop-line.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env perl
use strict;
use warnings qw/FATAL all NONFATAL misc/;
use File::Basename;
sub usage {
die <<END;
Usage: @{[basename($0)]} FILES...
or @{[basename($0)]} --KEY FILES...
or @{[basename($0)]} --KEY=TEST_STRING FILES...
This command extracts "prop-line" from given FILES.
"prop-line" is meta information, embeded in textfiles,
with surrounding "-*-".
If optional "--KEY" is given, this command extract value part of the key
if it exists.
Also if "--KEY=TEST_STRING" is given and the file's prop-line has
same value, this command prints the filename.
For more information about prop-line, see:
https://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html
END
}
{
usage() unless @ARGV;
my (%looking, $test_mode);
while (@ARGV and $ARGV[0] =~ /^--(?:(?<key>[-\w]+)(?:=(?<value>.*))?)\z/) {
shift @ARGV;
last if not defined $+{key};
$test_mode++ if defined $+{value};
$looking{$+{key}} = [$+{value}];
}
my ($nfound);
while (<>) {
chomp;
if (1..2) {
# prop-line only lives in first or second line.
if (my ($match) = m{-\*- ([^\n]*)? -\*-}) {
if (%looking) {
foreach my $item (split /\s*;\s*/, $match) {
next unless $item =~ /:/;
my ($key, $value) = split /: /, $item, 2;
if (my $found = $looking{$key}) {
if (not defined (my $test = $found->[0])) {
print $value, "\n";
} elsif ($test eq $value) {
print $ARGV, "\n";
$nfound++;
last;
}
}
}
} else {
print $match, "\n";
}
close ARGV;
}
} else {
close ARGV;
}
}
if ($test_mode) {
exit($nfound ? 0 : 1);
}
}