bsb has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to run a snippet of perl when I load a file in vim, that will take a good guess at the tabsize. My code's usually with ts=4 but other stuff is mixed.
I can't find any existing solutions so began writing one. Unfortunately, my 3 week holiday commences in 5 minutes so I'll just leave the very preliminary code I have in the hope that someone else will find the problem interesting enough to solve. Cheeky, I know.
Brad
#!/usr/bin/perl -w @ARGV or @ARGV = (qw( /usr/share/perl/5.6.1/Shell.pm )); no warnings 'uninitialized'; for my $f (@ARGV) { open(F,$f) or die "Open $f:$!"; my $tabs = 0; my $ltabs = 0; while($_ = <F>) { $tabs += tr/\t/\t/; $ltabs += () = m/^(\t)/g; my ($ls, $first) = m/^(\s+)(\S+)/; my ($last) = m/(\S+)\s+$/; $ls =~ tr/ /_/; my ($four, $eight) = ($ls,$ls); $four =~ s/\t/====/g; print "$four$first|$last\n" if ($ls); $eight =~ s/\t/========/g; print " "x40,"$eight$first|$last\n" if ($ls); } print "$f: $tabs $ltabs\n"; close F; } __END__ '#' should line up leading spaces count tabs are multiple of leading spaces ( can have anomalies ) if for eval { go out >> ____eval ____eval ====sub ========sub ====____if ========____if ========\$Shell::capture_stderr ================\$Shell::captu +re_stderr ====____} ========____} } goes in << ========____\$ret; ================____\$ret; ========} ================} ====____} ========____} ====} ========}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Guessing tab size
by jmcnamara (Monsignor) on Oct 15, 2003 at 10:17 UTC | |
|
Re: Guessing tab size
by edan (Curate) on Oct 15, 2003 at 10:44 UTC |