Erlang code indentation

erlang-mode default indentation

This is the most commonly used indentation standard for Erlang code. See http://www.erlang.org/doc/man/erlang_mode.html

The problem with this standard is that it uses tabs and spaces both for indentation level and the alignment. This mixing prevents developers from changing the 8 characters tab size, because such change would break the indentation and the alignment.

In this sense, the unique benefit of using tabs is to save storage. But using tabs introduces several problems http://www.emacswiki.org/emacs/TabsAreEvil

Furthermore, mixing tabs and spaces is even worse http://www.emacswiki.org/pics/static/TabsSpacesBoth.png

Emacs

When you install Erlang/OTP, it includes erlang-mode with the standard rules.

When editing an .erl file, press the Tab key to indent that line automatically.

Add those lines to $HOME/.emacs to allow indentation using Shift+F10, and clean trailing spaces with F10:

(global-set-key [f10] 'delete-trailing-whitespace)
(global-set-key [s-f10] 'erlang-indent-current-buffer)

Vim

Add this line to your file $HOME/.vimrc

set ts=8 sw=4 sts=4 noet

erlang-mode space-only indentation

The space-only indentation is based in the standard one, but uses only space characters both for indentation and alignment, and tabs are considered to be 4 spaces.

Of course, this prevents developers from changing the indentation level, as already happens in the standard indentation.

Emacs

Add to your $HOME/.emacs the lines:

(setq erlang-mode-hook
    (function (lambda ()
                (setq indent-tabs-mode nil))))

Vim

Add to your $HOME/.vimrc the lines:

set tabstop=8
set shiftwidth=4
set softtabstop=4
set expandtab

To highlight TAB characters, add in $HOME/.vim/syntax/erlang.vim the lines

syntax match Tab /\t/"
hi Tab gui=underline guifg=blue ctermbg=blue

To visually display TAB and tailing characters, add this in $HOME/.vimrc

set listchars=tab:>-,trail:_

this makes code looks like this

start(Host, Opts) ->
    Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
    ChildSpec = {Proc,_____
>------->------- {?MODULE, start_link, [Host, Opts]},
>------->------- transient, 1000, worker, [?MODULE]},
    supervisor:start_child(ejabberd_sup, ChildSpec).

indenter.sh script

Download the indenter.sh script to ejabberd/src directory and execute it.

Syndicate content