Install Necessary Packages with opam

Usually, we have a venv per project. We first use opam to install necessary packages:

1
opam install merlin ocp-indent ocamlformat dune ocaml-lsp

where merlin is LSP, ocp-indent handles auto indentation, ocamlformat for formatting, dune for project management.

Setting Up for OCaml

Let’s put OCaml related configs in lisp/ocaml.el and load it in init.el with (require 'ocaml). Of course, you should first setup lsp-mode.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(use-package caml
:ensure t
:mode (("\\.ml\\'" . caml-mode)
("\\.mli\\'" . caml-mode)
("\\.mll\\'" . caml-mode)
("\\.mly\\'" . caml-mode))
:hook (caml-mode . lsp))

(use-package dune
:ensure t
:mode (("dune\\'" . dune-mode)
("dune-project\\'" . dune-mode)))

(use-package merlin
:ensure t
:hook (caml-mode . merlin-mode)
:custom
(merlin-error-after-save nil))

(use-package merlin-eldoc
:ensure t
:hook (caml-mode . merlin-eldoc-setup))

(use-package ocp-indent
:ensure t
:commands ocp-indent-buffer-local
:hook
(caml-mode . ocp-setup-indent)
)

(use-package ocamlformat
:ensure t)


(provide 'ocaml)

where we simply setup each components.