OCaml Editing

From Xen
Revision as of 19:37, 10 November 2011 by Lars.kurth (talk | contribs) (Migrated)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


OCaml Editing

Editors presented on this page are EMACS, Eclipse and VIM.

EMACS

Disabling Extraneous Indentation

By default, Tuareg mode inserts extra indentation for code sections after let...in blocks. For example:

let foo () =
    let a = "please" in

        print_endline a;

        let b = "buy" in

            print_endline b;

            let c = "me" in

                print_endline c;

                let d = "a" in

                    print_endline d;

                    let e = "widescreen" in

                        print_endline e;

                        let f = "monitor" in

                            print_endline f

Some people find this a bit hard to read, especially if there are many let...in blocks in a row.

To disable this extra indentation, add the following command to your .emacs file:


(setq tuareg-in-indent 0)  


let foo () =
    let a = "please" in
    print_endline a;
    let b = "buy" in
    print_endline b;
    let c = "me" in
    print_endline c;
    let d = "a" in
    print_endline d;
    let e = "widescreen" in
    print_endline e;
    let f = "monitor" in
    print_endline f


Indenting with tab characters instead of space characters

When Tuareg mode is installed, Emacs re-indents lines as you type them according to a set of configurable rules.

By default, Emacs will indent lines with space characters. You can however configure Emacs to indent with tab characters instead of space characters.

Add the following lines into your .emacs file, replacing n with your preferred tab display size:


(setq tuareg-default-indent n)
(setq default-tab-width n) 


As long as you specify the same value of n for both settings, Tuareg mode will indent with tab characters.

For example:

Shorter tabs

(setq tuareg-default-indent 4)
(setq default-tab-width 4) 

Wider tabs

(setq tuareg-default-indent 8)
(setq default-tab-width 8) 


Viewing Whitespace Characters

Have a look at the WhiteSpace plug-in.

ECLIPSE

Eclipse has a pretty decent OCaml plug-in. See ocaIDE

VIM

Matching

If you are a fan of % to jump between matching parenthetical characters, this functionality can be extended to cycle between related keywords, such as let...and...in, if...then...else, do...done, begin...end, sig...end, struct...end, etc. by enabling the matchit plugin, and configuring it as follows.

Install the matchit plugin by following the instructions available by typing


:help matchit-install

Insert the following in a file ~/.vim/ftplugin/ocaml.vim:


let b:mw='\<let\>:\<and\>:\(\<in\>\|;;\),'
let b:mw=b:mw . '\<if\>:\<then\>:\<else\>,'
let b:mw=b:mw . '\<\(for\|while\)\>:\<do\>:\<done\>,'
let b:mw=b:mw . '\<\(object\|sig\|struct\|begin\)\>:\<end\>,'
let b:mw=b:mw . '\<\(match\|try\)\>:\<with\>'
let b:match_words=b:mw


Identing

Add the following line to your .vimrc file:

filetype plugin indent on

To set the level of indent, add a line such as:

set shiftwidth=2

To view tabs explicitly, use:

set list

Alternatively, use the following to colour tabs red:

syn match TAB_CHAR "\t"
hi link TAB_CHAR Error

To set the displayed width of a tab to eight characters, use:

set tabstop=8

To ensure that pressing tab in insert mode causes an ASCII character 9 rather than a sequence of spaces you need to both set noexpandtab and make sure that shiftwidth and tabstop are set to the same value:

set noexpandtab
set tabstop=8
set shiftwidth=8

One possible shortcut is to set tabstop to the value of shiftwidth, due to Vim's arcane scripting lanuage that isn't as straightforward as one would expect:

execute "set tabstop=".&shiftwidth