1

I am coding Ocaml with Emacs, at the moment the setting of the indentation of if gives the following:

if cond1 then e1 else
  if cond2 then e2 else
    if cond3 then e3 else
      e4

I would like to realize the same format as Caml programming guidelines:

if cond1 then e1 else
if cond2 then e2 else
if cond3 then e3 else
e4

Could anyone tell me which parameter is related to that? Thank you

Edit1: here is my .emacs

SoftTimur
  • 5,630
  • 38
  • 140
  • 292

4 Answers4

2

You can now use ocp-indent which will (almost always) respect the Caml programming guidelines. The only difference is that it will indent the last expression, to avoid confusing scoping errors:

if cond1 then e1 else
if cond2 then e2 else
if cond3 then e3 else
  e4;
e5
Thomas
  • 5,047
  • 19
  • 30
1

Something seems to be wrong. Are you using the caml-mode from the OCaml distribution ? Because I do and it indents according to the programming guidelines without setting any parameter. That's what I have in my .emacs (the mode is installed in ~/.emacs.d/caml-mode):

;; Caml mode
(setq load-path (cons "~/.emacs.d/caml-mode" load-path))
(setq auto-mode-alist (cons '("\\.ml[iylp]?" . caml-mode) auto-mode-alist))
(autoload 'caml-mode "caml" "Major mode for editing Caml code." t)
(autoload 'run-caml "inf-caml" "Run an inferior Caml process." t)
(autoload 'camldebug "camldebug" "Run the Caml debugger." t)
(if window-system (require 'caml-font))

If you are using tuareg-mode I cannot help you. Note however that, contrary to popular belief, the caml-mode from the distribution is perfectly fine and is still maintained by OCaml's authors.

Daniel Bünzli
  • 5,119
  • 20
  • 21
  • Thanks for your comment, i just added my `.emacs` – SoftTimur Jul 19 '11 at 20:44
  • Well you are using tuareg-mode. You may want to try caml-mode (located in the emacs directory of the distribution). See my updated response to see what I have in my .emacs. If you are using a recent distribution you can remove all other things from your .emacs (e.g. caml-change-error-alist-for-backtraces and caml-change-error-alist-for-assert-failure) – Daniel Bünzli Jul 19 '11 at 21:57
  • Indeed, I decided to switch to `caml-mode`, but I do not like the font, do you have any suggestion to [this post](http://stackoverflow.com/questions/6763001/is-it-possible-to-use-the-font-of-tuareg-in-caml-mode-under-emacs)? – SoftTimur Jul 20 '11 at 13:49
  • No, I just use the defaults. Regarding your problem about parts not hilighted, this shouldn't happen. Maybe you have a syntax error in your ocaml file. Also try to cleanup your `.emacs`, the hooks you install for caml-mode at the end are not needed. – Daniel Bünzli Jul 20 '11 at 15:34
1

You can set the variable tuareg-if-then-else-indent to 0 which will then indent your example as

if cond1 then e1 else
if cond2 then e2 else
if cond3 then e3 else
e4

I don't know if that causes other undesirable indentation in case you don't have nested if's though. You can also M-x customize-group RET tuareg RET to see all the indentation (and other) options.

Ivan Andrus
  • 5,221
  • 24
  • 31
0

Are you not satisfied with the following?

if c1 then e1
else if c2 then e2
else if c3 then e3
else e4
  • I do not like that, as the guidelines suggest: "Lack of consistency in the treatment of all the conditions. Why a special case for the first condition?" – SoftTimur Jul 19 '11 at 20:41