Ocaml mode for emacs using treesitter.
This is beta software and in development.
It implements:
.ml
and .mli
files.C-c C-a
- Switch between .ml
and .mli
files for a module.You will need a version of Emacs with treesitter enabled.
~/.emacs
:
(setq treesit-language-source-alist
'((ocaml "https://github.com/tree-sitter/tree-sitter-ocaml" "master" "ocaml/src")))
M-x treesit-install-language-grammar RET ocaml RET
.ocaml-ts-mode.el
file. To do this I have a
directory called ~/.emacs-progmode
and then I add that path to emacs by
adding the following line to my ~/.emacs
: (add-to-list 'load-path "~/.emacs-progmode")
.ocaml-ts-mode.el
into the progmode directory. Given the step
above, that would be ~/.emacs-progmode/
.ocaml-ts-mode
to autoload:
(autoload 'ocaml-ts-mode "ocaml-ts-mode" "Major mode for editing Ocaml code" t)
ocaml-ts-mode
sets itself up for .ml
and .mli
files. However if you are using another ocaml mode, such as Tuareg or
camlmode, you will need to either manually switch to ocaml-ts-mode
by doing
M-x ocaml-ts-mode RET
or by remapping the major mode, for example by adding
the following to ~/.emacs
:
(setq major-mode-remap-alist
'((tuareg-mode . ocaml-ts-mode)))
Finally, you can configure ocaml-ts-mode
when it load via a hook. For example:
(add-hook 'ocaml-ts-mode-hook
(lambda ()
(eglot-ensure)
(company-mode)
(flyspell-prog-mode)
(local-set-key (kbd "C-<tab>") 'company-complete)
(add-hook 'before-save-hook 'ocamlformat)
(setq indent-tabs-mode nil)
(setq truncate-lines t)
(setq whitespace-line-column 100)
(setq whitespace-style '(face trailing lines-tail))
(whitespace-mode)
(yafolding-mode)
(rainbow-delimiters-mode)))
ocaml-ts-mode
is a new project and currently only supports .ml
and .mli
files. It is also much less feature rich then Tuareg mode.
Treesitter is considered the future of programming major modes in Emacs. The
power of it is treesitter understands the syntax of a language and thus the
syntax highlighting can be more precise. For example, ocaml-ts-mode
highlights function calls different than field access. It also has good support
for multiple languages in a single buffer.
ocaml-ts-mode
does not have many features and is beta.
To contribute, just open a pull request!
Some known work that needs to be done: