1

I am trying to figure out how to fix the indentation in tuareg mode so that it does not insert a massive amount of indentation in anonymous functions and structures. I have used it in the past and it has not done this, but now it is. I would like to know how to configure it so that this problem goes away.

For example. This code is indented like this by tuareg mode:

let m = List.map (fun (va,vb) ->
                  (va,vb)
                 ) m
in

I would like it to be indented like this:

let m = List.map (fun (va,vb) ->
  (va,vb)
) m
in

Similarly, tuareg indents this code like this:

module SMap = Map.Make(struct
                        type t = string
                        let compare = compare
                      end)

I would prefer it to be indented like this:

module SMap = Map.Make(struct
  type t = string
  let compare = compare
end)

I am using tuareg mode 2.0.7 that was released on November 12, 2013.

Update: I can confirm that rolling back to 2.0.6 resolves this problem for me. However, I'm still looking for the configuration option to fix this.

Arlen Cox
  • 384
  • 1
  • 2
  • 10
  • OCaml mode indents it originally the way you want (at least it does so for me when I try to use your examples). Not sure what's the difference between using `ocaml-mode` and `tuareg-mode`. The former seems to be an extension of the later. –  Nov 19 '13 at 00:41
  • In my experience, `caml-mode` is insufficient for the more modern features of the language. For what it's worth, `tuareg-mode` used to behave correctly, or at least it has for me in the past on different systems. I'm not interested in moving to an old version or I'll be stuck there forever, despite new language features. I'm interested in how to configure `tuareg-mode` to a more sane indentation. If there is a mode called `ocaml-mode`, please link to it. I'm only familiar with `caml-mode`. – Arlen Cox Nov 19 '13 at 07:00
  • I think I've confused something. I'm using `tuareg-mode`. I think once I had indeed installed something that was called `ocaml-mode`, maybe this: http://pauillac.inria.fr/~remy/cours/appsem/emacs/index.html but I haven't really worked on it. And my `tuareg-mode` must be older, which would explain why the indentation works for me. However, I took a peek into the settings of my older version and there are lots of them related to indentation. Could you look into `tuareg-sig-struct-align`? It must be `t` to produce the result you want. –  Nov 19 '13 at 10:44
  • Yes, I have `tuareg-sig-struct-align` set to `t`. At least it is at the beginning of `tuareg.el`. – Arlen Cox Nov 19 '13 at 15:46
  • Then it would be probably a regression bug in the newer version. My best understanding of the very reason this variable exists is to govern how indentation is performed in the case with structs inside module definitions. If it doesn't do what it says it should, then you probably need to tell the maintainer that they need to fix it. –  Nov 19 '13 at 15:56
  • I have this problem as well, but in Tuareg 2.0.6! – arvidj Jun 16 '14 at 07:43
  • I've moved to ocp-indent: http://www.typerex.org/ocp-indent.html It works much better and more reliably than tuareg. – Arlen Cox Jun 16 '14 at 12:18

2 Answers2

0

With my ~/.emacs

(custom-set-variables
 '(custom-enabled-themes (quote (tango-dark)))
 '(tool-bar-mode nil)
 '(tuareg-begin-indent 4)
 '(tuareg-class-indent 4)
 '(tuareg-default-indent 4)
 '(tuareg-do-indent 4)
 '(tuareg-for-while-indent 4)
 '(tuareg-fun-indent 4)
 '(tuareg-if-then-else-indent 4)
 '(tuareg-let-indent 4)
 '(tuareg-match-indent 4)
 '(tuareg-method-indent 4)
 '(tuareg-pipe-extra-unindent 4)
 '(tuareg-sig-struct-indent 4)
 '(tuareg-try-indent 4)
 '(tuareg-val-indent 4))
(custom-set-faces
 '(default ((t (:family "DejaVu Sans Mono" :foundry "unknown" :slant normal :weight normal :height 98 :width normal)))))

;disable backup
(setq backup-inhibited t)

;disable auto save
(setq auto-save-default nil)

;disable line wrapping
(setq-default truncate-lines t)

;space only indentation
(setq-default indent-tabs-mode nil)

your examples are indented as

tuareg-mode

Feel free to adjust indents to 2 spaces.


Update: It seems that version 2.0.7 indeed uses new indentation algorithm. However, commenting these two lines in tuareg-el:

1213 ;;(when (require 'smie nil 'noerror)
1214 ;;  (setq tuareg-use-smie t))

reverts to 'old' indentation mode.

barti_ddu
  • 10,179
  • 1
  • 45
  • 53
  • I tried this configuration and it indeed changed my indents to four, but otherwise the configuration remains the same. Maybe I got a broken version of `tuareg-mode`? Are you running version 2.0.7? – Arlen Cox Nov 19 '13 at 15:47
  • @ArlenCox: 2.0.6 (if that matters, however, i doubt that it does); can you move your `.emacs` temporarily out and try with fresh one (copy-paste the _whole_ example)? – barti_ddu Nov 19 '13 at 16:19
  • I updated the original post. Moving to 2.0.6 solves the problem, so it is probably some kind of regression in tuareg-mode. I'm going to file a bug. – Arlen Cox Nov 19 '13 at 20:13
0

An alternative solution is to use ocp-indent. Install it using opam

opam install ocp-indent

Then somewhere in your .emacs file use the following elisp code to load and configure ocp-indent.

;; Add opam emacs directory to the load-path
(setq opam-share (substring (shell-command-to-string "opam config var share 2> /dev/null") 0 -1))
(add-to-list 'load-path (concat opam-share "/emacs/site-lisp"))

;; Setup environment variables using OPAM
(dolist (var (car (read-from-string (shell-command-to-string "opam config env --sexp"))))
  (setenv (car var) (cadr var)))

;; One of the `opam config env` variables is PATH. Update `exec-path` to that.
(setq exec-path (split-string (getenv "PATH") path-separator))

(require 'ocp-indent)

(add-hook 'tuareg-mode-hook (lambda ()
  ;;  Your other tuareg specific settings here.
  (setq indent-line-function 'ocp-indent-line)))
Mads Hartmann
  • 690
  • 7
  • 12