Text::FindIndent(3pm)

πŸ“¦
version 0.12 Β· 6 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

✨ Text::FindIndent β€” heuristically determine the indent style ✨

okay so bestie, imagine you're reading a source file and you're like "how many spaces is this even using" β€” that's literally this module. It's Text::FindIndent (the Perl one, Text::FindIndent(3pm)), and it does the detective work for you. πŸ•΅οΈβ™€οΈπŸ’–


🌸 SYNOPSIS 🌸

use Text::FindIndent;
my $indentation_type = Text::FindIndent->parse($text, skip_pod => 1);
if ($indentation_type =~ /^s(\d+)/) {
  print "Indentation with $1 spaces\n";
}
elsif ($indentation_type =~ /^t(\d+)/) {
  print "Indentation with tabs, a tab should indent by $1 characters\n";
}
elsif ($indentation_type =~ /^m(\d+)/) {
  print "Indentation with $1 characters in tab/space mixed mode\n";
}
else {
  print "Indentation style unknown\n";
}

πŸ’… DESCRIPTION πŸ’…

So like. This is a module that tries to intuit the underlying indent "policy" of a text file β€” and yeah, most likely that's a source code file, because who else is out here caring about indentation. πŸ‘‘


πŸŽ€ METHODS πŸŽ€

parse

The class method parse is the main character here β€” it tries to figure out the indentation style of whatever text you hand it. Important vibe check: the text must start at a new line, and you can pass it in either as a string or as a reference to a scalar containing the string. 🫢

It returns a letter followed by a number, and here's the whole decoder ring:

  • if the letter is s, the text is most likely indented with spaces, and the number tells you how many spaces are used for indentation.
  • a t means tabs, and the number after the t tells you how many characters each level of indentation corresponds to.
  • a u means the indentation style could not be determined. it's giving mystery. πŸ’€
  • finally, an m followed by a number means that this many characters are used for each indentation level, but the indentation is an arbitrary number of tabs followed by 0-7 spaces. This can happen if your editor is stupid enough to do smart indentation/whitespace compression β€” i.e. it replaces all indentation with as many tabs as possible but leaves the rest as spaces. πŸ™ˆ

The function also supports parsing of vim modelines, and those settings override the heuristics. The modeline options that get recognized are:

  • sts / softtabstob
  • et / noet / expandtabs / noexpandtabs
  • ts / tabstop

Similarly, parsing of emacs Local Variables is somewhat supported. parse uses explicit settings to override the heuristics, but uses style settings only as a fallback. The options it recognizes are:

  • tab-width
  • indent-tabs-mode
  • c-basic-offset
  • style

There's one named option you can pass to parse(): skip_pod. When it's set to true, any section of POD (see perlpod) gets ignored for indentation finding. Why? Because verbatim paragraphs and examples embedded in POD are quite often indented differently from the normal Perl code around the POD section. It defaults to false. Example:

my $mode = Text::FindIndent->parse(\$text, skip_pod => 1);

to_vim_commands

A class method that converts the output of parse(\$text) into a series of vi(m) commands that will configure vim to use the detected indentation setting. Returns zero (failure) or more lines of text that are suitable for passing to VIM::DoCommand() one by one. ✨

As a convenience, if the argument to to_vim_commands doesn't look like the output of parse, it gets redirected to parse first. no extra steps, we got you.

To use this, you can put the following line in your .vimrc if your vim has Perl support. Suggestions on how to do this in a more elegant way are welcome. The code should be on one line but is broken up for displaying:

map <F5> <Esc> :perl use Text::FindIndent;VIM::DoCommand($_) for
Text::FindIndent->to_vim_commands(join "\n", $curbuf->Get(1..$curbuf->Count()));<CR>

(Patches to implement the equivalent for emacs would be welcome as well.) 🧚


πŸ¦‹ SUPPORT πŸ¦‹

Bugs should be reported via the CPAN bug tracker at

<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-FindIndent>

For other issues, contact the author.


πŸ‘‘ AUTHOR πŸ‘‘

  • Steffen Mueller <smueller@cpan.org>
  • Adam Kennedy <adamk@cpan.org>

🌈 REPOSITORY 🌈

<https://github.com/tsee/p5-Text-FindIndent>


Copyright 2008 - 2010 Steffen Mueller.

Copyright 2008 - 2010 Adam Kennedy,

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.


okay bestie that's the whole page πŸ’… and just so you have the receipts: this was perl v5.40.1, dated 2025-05-21, for Text::FindIndent(3pm). go forth and indent correctly, no cap. πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from