3 #=======================================================================
5 # File ID: 40153a86-f4a5-11e4-8530-000df06acc56
10 # ©opyleft 2015– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
17 use Digest
::SHA
qw(sha1_hex);
32 $progname =~ s/^.*\/(.*?)$/$1/;
33 our $VERSION = '0.1.1';
35 Getopt
::Long
::Configure
('bundling');
38 'help|h' => \
$Opt{'help'},
39 'quiet|q+' => \
$Opt{'quiet'},
40 'verbose|v+' => \
$Opt{'verbose'},
41 'version' => \
$Opt{'version'},
43 ) || die("$progname: Option error. Use -h for help.\n");
45 $Opt{'verbose'} -= $Opt{'quiet'};
46 $Opt{'help'} && usage
(0);
47 if ($Opt{'version'}) {
58 my $FOLD_BEGIN = "\\{\\{\\{";
59 my $FOLD_END = "\\}\\}\\}";
60 my $fold_dir = "/tmp/folds";
66 my $deleted_text = '';
68 while (my $line = <>) {
69 if ($line =~ /$FOLD_BEGIN/) {
73 chomp($fold_str = $line);
74 $fold_str =~ s/$FOLD_BEGIN//;
75 $fold_str =~ s/^\s+(.*)$/$1/;
76 $fold_str =~ s/^(.*?)\s+$/$1/;
78 if ($line =~ /$FOLD_END/) {
80 $level < 0 && msg
(0, "Extra end fold found, level = $level");
82 $deleted_text .= $line;
83 my $data_sha = sha1_hex
($deleted_text);
84 -d
$fold_dir || mkdir($fold_dir);
85 my $destfile = "$fold_dir/fold-$data_sha";
87 if (open(DestFP
, ">$destfile")) {
88 print(DestFP
$deleted_text);
89 close(DestFP
) && ($created_file = 1);
91 warn("$progname: $destfile: Could not create file: $!\n");
93 printf("[ Removed %s, %u line%s%s ]\n",
94 length($fold_str) ?
"\"$fold_str\"" : "Vim fold",
95 $count, $count == 1 ?
'' : 's',
96 $created_file ?
" stored in $destfile" : "",
105 $deleted_text .= $line;
112 msg
(0, sprintf("Missing %u end fold%s, output may be truncated",
113 $level, $level == 1 ?
'' : 's'));
121 # Print program version {{{
122 print("$progname $VERSION\n");
128 # Send the help message to stdout {{{
131 if ($Opt{'verbose'}) {
137 Usage: $progname [options] [file [files [...]]]
144 Be more quiet. Can be repeated to increase silence.
146 Increase level of verbosity. Can be repeated.
148 Print version information.
156 # Print a status message to stderr based on verbosity level {{{
157 my ($verbose_level, $Txt) = @_;
159 if ($Opt{'verbose'} >= $verbose_level) {
160 print(STDERR
"$progname: $Txt\n");
168 # This program is free software; you can redistribute it and/or modify
169 # it under the terms of the GNU General Public License as published by
170 # the Free Software Foundation; either version 2 of the License, or (at
171 # your option) any later version.
173 # This program is distributed in the hope that it will be useful, but
174 # WITHOUT ANY WARRANTY; without even the implied warranty of
175 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
176 # See the GNU General Public License for more details.
178 # You should have received a copy of the GNU General Public License
179 # along with this program.
180 # If not, see L<http://www.gnu.org/licenses/>.
182 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :