Introduction

Preference of indentation style for source code differs widely. People use various column indent sizes, some use tabs only, some use spaces only, some mix tabs and spaces.

If someone edits your code and uses a different indentation style to your own, you will probably want to re-indent that portion of the code to match your own style. Some people have even been known to refuse patches to their code if the indentation style is not to their taste !

Instead of spending time re-indenting code or shouting at people for not adhering to your personal style, how about simply reducing the workload and the politics by using modelines ?

About modelines

Being nice is possible, through the use of ‘modelines’. A modeline is a piece of text at the beginning or end of a file which gives hints to an editor opening that file as to how it should go about indenting code.

Most people working on the KDE project use either Vim or EMACS. Both these editors support modelines. Not surprisingly, they are incompatible, but there’s no law against providing modelines for each.

How you can save the world

All you need to do is to create a modeline which describes your own indentation preference and to remember to add it to all files you create. Even better, why not try to write modelines for as many editors as is practical and append them to your files ?

Tips

To avoid the hassle of remembering or finding your modeline, you can get your editor to automatically insert it when you type a certain command.

I use Vim, which has a neat feature where text is automatically replaced on insertion by a different string. When I type MODELINE, my personal modelines for Vim and EMACS are inserted.

I would guess it is possible to do this in other editors. Certainly it will be possible with EMACS, I just don’t know how. Please tell me !

Examples

Something like the (imaginary) standard KDE style

// vim:ts=4:sw=4:tw=78:et
// -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-

Code sample

void
MyClass::myMethod()
{
    for (int i = 0; i < 1000; i++)
    {
        someMethod();

        while (true)
        {
            someOtherMethod();

            do
            {
                yetAnotherMethod();

            } while (false);
        }
    }
}

Something like the Qt style.

// vim:sw=4:sts=4:ts=8:noet:sta:tw=78

Code sample (tab is '>-------')

    void
MyClass::myMethod()
{                
    for (int i = 0; i < 1000; i++)
    {   
>-------someMethod();

>-------while (true)
>-------{
>-------    someOtherMethod();

>-------    do
>-------    {
>------->-------yetAnotherMethod();

>-------    } while (false);
>-------}   
    }
}

TODO

Code formatting in general

Wanted

Extra information. Help me out here !