1Text::FindIndent(3) User Contributed Perl Documentation Text::FindIndent(3)
2
3
4
6 Text::FindIndent - Heuristically determine the indent style
7
9 use Text::FindIndent;
10 my $indentation_type = Text::FindIndent->parse($text, skip_pod => 1);
11 if ($indentation_type =~ /^s(\d+)/) {
12 print "Indentation with $1 spaces\n";
13 }
14 elsif ($indentation_type =~ /^t(\d+)/) {
15 print "Indentation with tabs, a tab should indent by $1 characters\n";
16 }
17 elsif ($indentation_type =~ /^m(\d+)/) {
18 print "Indentation with $1 characters in tab/space mixed mode\n";
19 }
20 else {
21 print "Indentation style unknown\n";
22 }
23
25 This is a module that attempts to intuit the underlying indent "policy"
26 for a text file (most likely a source code file).
27
29 parse
30 The class method "parse" tries to determine the indentation style of
31 the given piece of text (which must start at a new line and can be
32 passed in either as a string or as a reference to a scalar containing
33 the string).
34
35 Returns a letter followed by a number. If the letter is "s", then the
36 text is most likely indented with spaces. The number indicates the
37 number of spaces used for indentation. A "t" indicates tabs. The number
38 after the "t" indicates the number characters each level of indentation
39 corresponds to. A "u" indicates that the indenation style could not be
40 determined. Finally, an "m" followed by a number means that this many
41 characters are used for each indentation level, but the indentation is
42 an arbitrary number of tabs followed by 0-7 spaces. This can happen if
43 your editor is stupid enough to do smart indentation/whitespace
44 compression. (I.e. replaces all indentations many tabs as possible but
45 leaves the rest as spaces.)
46
47 The function supports parsing of "vim" modelines. Those settings
48 override the heuristics. The modeline's options that are recognized are
49 "sts"/"softtabstob", "et"/"noet"/"expandtabs"/"noexpandtabs", and
50 "ts"/"tabstop".
51
52 Similarly, parsing of "emacs" Local Variables is somewhat supported.
53 "parse" use explicit settings to override the heuristics but uses style
54 settings only as a fallback. The following options are recognized:
55 "tab-width", "indent-tabs-mode", "c-basic-offset", and "style".
56
57 There is one named option that you can pass to "parse()": "skip_pod".
58 When set to true, any section of POD (see perlpod) will be ignored for
59 indentation finding. This is because verbatim paragraphs and examples
60 embedded in POD or quite often indented differently from normal Perl
61 code around the POD section. Defaults to false. Example:
62
63 my $mode = Text::FindIndent->parse(\$text, skip_pod => 1);
64
65 to_vim_commands
66 A class method that converts the output of "parse(\$text)" into a
67 series of vi(m) commands that will configure vim to use the detected
68 indentation setting. Returns zero (failure) or more lines of text that
69 are suitable for passing to "VIM::DoCommand()" one by one.
70
71 As a convenience, if the argument to "to_vim_commands" doesn't look
72 like the output of "parse", it is redirected to "parse" first.
73
74 To use this, you can put the following line in your .vimrc if your vim
75 has Perl support. Suggestions on how to do this in a more elegant way
76 are welcome. The code should be on one line but is broken up for
77 displaying:
78
79 map <F5> <Esc> :perl use Text::FindIndent;VIM::DoCommand($_) for
80 Text::FindIndent->to_vim_commands(join "\n", $curbuf->Get(1..$curbuf->Count()));<CR>
81
82 (Patches to implement the equivalent for emacs would be welcome as
83 well.)
84
86 Bugs should be reported via the CPAN bug tracker at
87
88 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-FindIndent>
89
90 For other issues, contact the author.
91
93 Steffen Mueller <smueller@cpan.org>
94
95 Adam Kennedy <adamk@cpan.org>
96
98 Copyright 2008 - 2010 Steffen Mueller.
99
100 Copyright 2008 - 2010 Adam Kennedy,
101
102 This program is free software; you can redistribute it and/or modify it
103 under the same terms as Perl itself.
104
105 The full text of the license can be found in the LICENSE file included
106 with this module.
107
108
109
110perl v5.30.1 2020-01-30 Text::FindIndent(3)