OCaml Editing: Difference between revisions

From Xen
Jump to navigationJump to search
(Migrated)
 
m (Removed superfluous <nowiki></nowiki> tag pairs.)
Line 19: Line 19:
By default, Tuareg mode inserts extra indentation for code sections after let...in blocks. For example:
By default, Tuareg mode inserts extra indentation for code sections after let...in blocks. For example:


<pre><nowiki>
<pre>
let foo () =
let foo () =
let a = "please" in
let a = "please" in
Line 44: Line 44:


print_endline f
print_endline f
</nowiki></pre>
</pre>


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




<pre><nowiki>
<pre>
(setq tuareg-in-indent 0)
(setq tuareg-in-indent 0)
</nowiki></pre>
</pre>




<pre><nowiki>
<pre>
let foo () =
let foo () =
let a = "please" in
let a = "please" in
Line 70: Line 70:
let f = "monitor" in
let f = "monitor" in
print_endline f
print_endline f
</nowiki></pre>
</pre>




Line 82: Line 82:




<pre><nowiki>
<pre>
(setq tuareg-default-indent n)
(setq tuareg-default-indent n)
(setq default-tab-width n)
(setq default-tab-width n)
</nowiki></pre>
</pre>




Line 94: Line 94:
'''Shorter tabs'''
'''Shorter tabs'''


<pre><nowiki>
<pre>
(setq tuareg-default-indent 4)
(setq tuareg-default-indent 4)
(setq default-tab-width 4)
(setq default-tab-width 4)
</nowiki></pre>
</pre>


'''Wider tabs'''
'''Wider tabs'''


<pre><nowiki>
<pre>
(setq tuareg-default-indent 8)
(setq tuareg-default-indent 8)
(setq default-tab-width 8)
(setq default-tab-width 8)
</nowiki></pre>
</pre>




Line 121: Line 121:




<pre><nowiki>
<pre>
:help matchit-install
:help matchit-install
</nowiki></pre>
</pre>


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




<pre><nowiki>
<pre>
let b:mw='\<let\>:\<and\>:\(\<in\>\|;;\),'
let b:mw='\<let\>:\<and\>:\(\<in\>\|;;\),'
let b:mw=b:mw . '\<if\>:\<then\>:\<else\>,'
let b:mw=b:mw . '\<if\>:\<then\>:\<else\>,'
Line 135: Line 135:
let b:mw=b:mw . '\<\(match\|try\)\>:\<with\>'
let b:mw=b:mw . '\<\(match\|try\)\>:\<with\>'
let b:match_words=b:mw
let b:match_words=b:mw
</nowiki></pre>
</pre>




Line 141: Line 141:
Add the following line to your .vimrc file:
Add the following line to your .vimrc file:


<pre><nowiki>
<pre>
filetype plugin indent on
filetype plugin indent on
</nowiki></pre>
</pre>


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


<pre><nowiki>
<pre>
set shiftwidth=2
set shiftwidth=2
</nowiki></pre>
</pre>


To view tabs explicitly, use:
To view tabs explicitly, use:


<pre><nowiki>
<pre>
set list
set list
</nowiki></pre>
</pre>


Alternatively, use the following to colour tabs red:
Alternatively, use the following to colour tabs red:


<pre><nowiki>
<pre>
syn match TAB_CHAR "\t"
syn match TAB_CHAR "\t"
hi link TAB_CHAR Error
hi link TAB_CHAR Error
</nowiki></pre>
</pre>


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


<pre><nowiki>
<pre>
set tabstop=8
set tabstop=8
</nowiki></pre>
</pre>


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:
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:


<pre><nowiki>
<pre>
set noexpandtab
set noexpandtab
set tabstop=8
set tabstop=8
set shiftwidth=8
set shiftwidth=8
</nowiki></pre>
</pre>


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:
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:


<pre><nowiki>
<pre>
execute "set tabstop=".&shiftwidth
execute "set tabstop=".&shiftwidth
</nowiki></pre>
</pre>


[[Category:OCaml]]
[[Category:OCaml]]

Revision as of 21:49, 28 November 2011


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