bsb has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone have code that tries to guess tab size of an unknown file? (be it Perl or something else)

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

    Here is a program that a works on the assumption that a file with 4 space tabs will contain some lines that start with 4 spaces while a file with 8 space tabs will not. It skips any Pod in the file.
    #!/usr/bin/perl -wn if (not /^=/ .. /^=cut/) { die $tab = "4\n" if /^ \S/; } END { print "8\n" unless $tab; }

    This is far from fool-proof however. In fact I'd go as far as to say that no method is fool-proof since a file may have been edited by people with different tab/space styles.

    --
    John.

Re: Guessing tab size
by edan (Curate) on Oct 15, 2003 at 10:44 UTC

    Unfortunately, my 3 week holiday commences in 5 minutes

    I think you need to rethink your priorities in life here... :)

    --
    3dan