Indent Finder

The end of your indentation's nightmare


by Philippe Fremy

Introduction

Indentation of external program sources is a common problem. Some people use 2 spaces, some 4 spaces, some tabulations, some (horror!) mix tab and spaces. Text editors have usually options helping to deal with the way you indent your files. Pressing tab will insert spaces of tabultations depending on your settings. However, it only works properly with your own files, which have been indented in your own way.

As soon as you start editing external sources, you are likely to face a different indentation. Then your careful editor setting will simply fuck up the file you edit unless the guy did use the same indentation as yours. And you may not notice it. For example if I indent with tab but sets them to be displayed as four columns and if I edit a file indented with 4 spaces, all the lines I create will be indented with tab. They will render fine on my editor, but probably not on someone else's editor.

I haven't found (yet) an editor that deals properly with this problem. The solution however is simple: the text editor must find the indentation used within a file and tune its settings according to this. This is what this program does.

Program

Indent Finder is a python script that reads a file and tells you what indentation is used inside the file. It is not tied to any particular language. It was tested successfully with C, C++, python and Java code.

The current versions provides helps for the integration with vim. I hope other editors will pick either the script or the idea, and that auto-detecting indentation will become common amongst text editors. I am releasing the code under the BSD License to encourage this.

The script is written in python because it was quick and easy way to write it. The algorithm is pretty simple, and could be rewritten in C to avoid the small (almost unnoticeable) delay due to the python interpreter starting.

How it works

Indent Finder scans each line of the entry file for a space character (white space or tab) repeated until a non space character is found. Such a line is considered to be a properly indented line of code. Blank lines and mixed indentation lines are safely ignored. Lines coming after a line ending in '\\' have higher chance of being not properly indented, and are thus ignored too.

An array stores the number of lines that have a specific indentation: tab, number of spaces between 2 and 8. For space indentation, a line is considered indented with a base of x if the number of spaces modulo x yields zero. Thus, an indentaiton of 4 spaces increases the 2-spaces and the 4-spaces indentation line count.

At the end of the scan phase, the indentation that was used with the highest number of lines is taken. For spaces, to avoid the problemes of multiples like 2 and 4, the highest indentation number is preferred. A lower number is chosen if it reports at least 10% more lines with this indentation.

If IndentFinder ever reports wrong indentation, send me immediately a mail, if possible with the offending file.

The algorithm could be improved by scanning actually not the indentation used in a line but the change in the indentation. But it works already very well so I don't plan to do it.

Download & Install

The program was tested successfully on windows with python 2.2 and vim 6.1, and on a gentoo linux with python 2.2 and kvim.

Date Version Download Change description
22/11/2002 1.0 Unix Version (20 kb)

Windows Version (44 kb)

Initial and stable release


Usage

In command line mode, it produces simple output like: space 4 or tab 8. This output can by used in other programs or shell scripts.

To use indent-finder with vim, install the python script (python setup.py install) and drop the file indent_finder.vim inside your vim plugin directory (see :help plugins for more information on plugings). All new opened buffer will be scanned by Indent Finder, which will set the vim settings appropriately. The settings modified are shiftwidth, tabstop, softtabstop and expandtab. By default, all file types are scanned but you may restrict this by editing the indent_finder.vim script.

Feedback

Bugs, suggestions, patch, I am open to feedback: Mail me.





Last modification : $Date: 2002/11/25 22:39:58 $ - $Author: philippe $