-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindente
executable file
·232 lines (171 loc) · 5.22 KB
/
indente
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env perl -w
#
# indente: indent and edit a file.
#
# 2006-12-11: Ported from shell function in ~.utilities.
# Written by Steven J. DeRose.
# 2007-09-04 sjd: Add -n. Clean up.
# 2007-10-02 sjd: Add -it to -norm; factor out options; warn if no $EDITOR.
# 2007-11-02 sjd: strict, add -w. change temp-file name.
# 2012-09-12, 2014-01-09: Cleanup.
#
# To do:
# Option to break before comments, pis, end-tags,...
#
use strict;
use Getopt::Long;
our $VERSION_DATE = "2014-01-09";
# Default options for normalizeXML:
my $dft_normOpts = "-i -it -btags -btext -empty F -noparam";
my @addedOptions = ();
my $all = 0;
my $geometry = "";
my $lnum = 0;
my $normOpts = $dft_normOpts;
my $norm = 0;
my $prefix = "";
my $preserve = 0;
my $quiet = 0;
my $starts = 0;
my $verbose = 0;
my $write = 0;
my $xoptions = "";
# Process options
#
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"all" => \$all,
"geometry=s" => \$geometry,
"h|help|?" => sub { system "perldoc $0"; exit; },
"lnum" => \$lnum,
"n|norm" => \$norm,
"nopts=s" => \$normOpts,
"prefix=s" => \$prefix,
"preserve!" => \$preserve,
"q|quiet!" => \$quiet,
"starts" => \$starts,
"write" => \$write,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
"xopts=s" => \@addedOptions,
);
($result) || die "Bad options.\n";
###############################################################################
# Validate and default options
#
if ($geometry) { $xoptions .= " -geometry $geometry"; }
$xoptions .= " " . join(" ",@addedOptions);
if ($lnum) { $norm = 1; }
($ARGV[0] && -e $ARGV[0]) || die "Couldn't find file.\n";
my $file = $ARGV[0];
###############################################################################
###############################################################################
# Main
#
(my $short = $file) =~ s/.*\///;
if ($prefix ne "") { $short = "$prefix" . "__" . "$short"; }
# Make new name, but keep extension so editor knows how to handle it.
my $tfile = "/tmp/_indente_" . int(rand(100000)) . "__" . $short;
my $cmd;
if ($all) {
$cmd = "splitAtMatches -e '\\<' $file >$tfile";
}
elsif ($starts) {
$cmd = "splitAtMatches $file >$tfile";
}
elsif ($norm) {
$cmd = "normalizeXML $normOpts";
if ($lnum) { $cmd .= " -lnum"; }
$cmd .= " $file >$tfile";
}
else {
$cmd = "indent $file >$tfile";
}
# Finally, indent the file as requested
#
($quiet) || print "Running: $cmd\n";
system "$cmd";
(-f $tfile) || die "Indenting failed, can't find output file '$tfile'.\n";
if (!$write) {
system "chmod -w $tfile"; # so user doesn't confuse with original file
}
my $ed = $ENV{EDITOR};
if ($ed eq "emacs") {
my $emacsOpts = "-bg mistyRose -geometry 82x70";
system "$ed $emacsOpts $xoptions -T $short $tfile &";
if ($preserve) {
warn "The indented data is in '$tfile'.\n";
}
else {
sleep 5;
system "chmod +w $tfile";
system "rm $tfile";
}
}
elsif ($ed ne "") {
system "$ed $tfile &";
if ($preserve) {
warn "The indented data is in '$tfile'.\n";
}
else {
sleep 5;
system "chmod -w $tfile";
system "rm $tfile";
}
}
else {
warn "Your \$EDITOR environment variable isn't set, so I don't know what"
. " editor to start up for you. The indented data is in '$tfile'.\n";
}
exit;
###############################################################################
###############################################################################
###############################################################################
#
=pod
=head1 Usage
indente [file]
Indent a file and then open it in your $EDITOR.
You can use the *nix C<indent> command, or C<normalizeXml>.
=head1 Options
=over
=item * B<--all>
Break before all XML start-tags, not just where C<indent> does.
=item * B<--lnum>
Number the lines (implies C<-norm>).
=item * B<--norm>
Use C<normalizeXML> instead of C<indent>.
=item * B<--nopts> I<'string'>
Send extra options to normalizeXML.
=item * B<--prefix> I<name>
Prefix 'name' to filename for edited file.
=item * B<--preserve>
Don't remove the tmp file.
=item * B<--quiet> OR B<-q>
Suppress most messages.
=item * B<--starts>
Break before XML start-tags, end-tags, comments, and PIs.
=item * B<--version>
Display version info and exit.
=item * B<--write>
Make the temp file read-write (it's normally read-only so
you don't edit it and forget it's saved only in F</tmp>).
=item * B<--xopts 'opts'>
Set X Windows options, such as:
-x '-geometry WxH+y+x -T title -fg color'
=back
=head1 Related commands
=head2 SJD
C<splitAtMatches> -- implements I<--all>.
C<normalizeXML> -- implements I<-n>.
C<stdin2Editor> -- Take input from STDIN and open it in an editor.
You can pipe any pretty-printer you like into this, to get something
like C<indente>.
=head1 Ownership
This work by Steven J. DeRose is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see L<http://creativecommons.org/licenses/by-sa/3.0/>.
For the most recent version, see L<http://www.derose.net/steve/utilities/>.
=cut