ocamlformat
-
library ocamlformat
-
module Ocamlformat
-
module Assoc
-
module Cmt
-
module Conf
-
module Config_option
-
module Docstring
-
module Extended_ast
-
module File_system
-
module Fmt
-
module Fmt_ast
-
module Fmt_odoc
-
module Indent
-
module Partial_ast
-
module Valid_ast
-
-
module Literal_lexer
-
module Loc_tree
-
module Migrate_ast
-
module Normalize_extended_ast
-
module Normalize_std_ast
-
module Params
-
module Exp
-
module Infix_op_arg
-
-
-
module Parse_with_comments
-
module W
-
-
module Prec
-
module Range
-
module Source
-
module Sugar
-
module Exp
-
module Let_binding
-
module Pat
-
-
module Syntax
-
module Toplevel_lexer
-
module Translation_unit
-
module Error
-
-
module Version
-
-
-
library ocamlformat.format_
-
module Format_
-
-
library ocamlformat.ocaml_common
-
module Ocaml_common
-
-
library ocamlformat.ocamlformat_stdlib
-
module Ocamlformat_stdlib
-
module Cmdliner
-
module Continue_or_stop
-
module Export
-
module List
-
module Assoc
-
module Cartesian_product
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
module Or_unequal_lengths
-
-
module String
-
module Caseless
-
module Escaping
-
module Search_pattern
-
-
module type T
-
module type T1
-
module type T2
-
module type T3
-
module Warning
-
-
-
library ocamlformat.parser_extended
-
module Parser_extended
-
module Ast_helper
-
module Attr
-
module Cf
-
module Ci
-
module Cl
-
module Const
-
module Csig
-
module Cstr
-
module Ctf
-
module Cty
-
module Exp
-
module Incl
-
module Mb
-
module Md
-
module Mod
-
module Ms
-
module Mtd
-
module Mty
-
module Of
-
module Opn
-
module Pat
-
module Rf
-
module Sig
-
module Str
-
module Te
-
module Typ
-
module Type
-
module Val
-
module Vb
-
-
module Ast_mapper
-
module Asttypes
-
module Docstrings
-
module WithMenhir
-
-
module Lexer
-
module Parse
-
module Parser
-
module Incremental
-
module MenhirInterpreter
-
-
module Parsetree
-
module Printast
-
-
-
library ocamlformat.parser_recovery
-
module Parser_recovery
-
-
library ocamlformat.parser_shims
-
module Parser_shims
-
module Clflags
-
module Int
-
module List
-
module Misc
-
module Color
-
module Error_style
-
module Int_literal_converter
-
module LongString
-
module Magic_number
-
-
-
-
library ocamlformat.parser_standard
-
module Parser_standard
-
module Ast_helper
-
module Attr
-
module Cf
-
module Ci
-
module Cl
-
module Const
-
module Csig
-
module Cstr
-
module Ctf
-
module Cty
-
module Exp
-
module Incl
-
module Mb
-
module Md
-
module Mod
-
module Ms
-
module Mtd
-
module Mty
-
module Of
-
module Opn
-
module Pat
-
module Rf
-
module Sig
-
module Str
-
module Te
-
module Typ
-
module Type
-
module Val
-
module Vb
-
-
module Ast_mapper
-
module Asttypes
-
module Docstrings
-
module WithMenhir
-
-
module Lexer
-
module Parse
-
module Parser
-
module Incremental
-
module MenhirInterpreter
-
-
module Parsetree
-
module Printast
-
-
Disable outside project
As mentioned in options
, when the option --disable-outside-detected-project
is set, .ocamlformat
files outside of the project (including the one in XDG_CONFIG_HOME
) are not read. The project root of an input file is taken to be the nearest ancestor directory that contains a .git
or .hg
or dune-project
file. If no configuration file is found, then the formatting is disabled.
This feature is often the behavior you can expect from OCamlFormat when it is directly run from your text editor, so it is advised to use this option.
Emacs setup
- add
$(opam config var share)/emacs/site-lisp
toload-path
(as done byopam user-setup install
)
- add
(require 'ocamlformat)
to.emacs
optionally add the following to
.emacs
to bindC-M-<tab>
to theocamlformat
command and install a hook to run OCamlFormat when saving:(add-hook 'tuareg-mode-hook (lambda () (define-key tuareg-mode-map (kbd "C-M-<tab>") #'ocamlformat) (add-hook 'before-save-hook #'ocamlformat-before-save)))
To pass the option --disable-outside-detected-project
(or --disable
) to OCamlFormat:
- run
emacs
- run
M-x customize-group⏎
then enterocamlformat⏎
- select the Ocamlformat Enable item
- select the OCamlformat mode in the Value Menu:
Enable
(by default),Disable
orDisable outside detected project
- save the buffer (
C-x C-s
) then enteryes⏎
and exit
Other OCamlFormat options can be set in .ocamlformat configuration files.
With use-package
A basic configuration with use-package:
(use-package ocamlformat
:custom (ocamlformat-enable 'enable-outside-detected-project)
:hook (before-save . ocamlformat-before-save)
)
Sometimes you need to have a switch for OCamlFormat (because of version conflicts or because you don't want to install it in every switch, for example). Considering your OCamlFormat switch is named ocamlformat
:
(use-package ocamlformat
:load-path
(lambda ()
(concat
;; Never use "/" or "\" since this is not portable (opam-user-setup does this though)
;; Always use file-name-as-directory since this will append the correct separator if needed
;; (or use a package that does it well like https://github.com/rejeep/f.el)
;; This is the verbose and not package depending version:
(file-name-as-directory
;; Couldn't find an option to remove the newline so a substring is needed
(substring (shell-command-to-string "opam config var share --switch=ocamlformat --safe") 0 -1))
(file-name-as-directory "emacs")
(file-name-as-directory "site-lisp")))
:custom
(ocamlformat-enable 'enable-outside-detected-project)
(ocamlformat-command
(concat
(file-name-as-directory
(substring (shell-command-to-string "opam config var bin --switch=ocamlformat --safe") 0 -1))
"ocamlformat"))
:hook (before-save . ocamlformat-before-save)
)
(Notice the :custom
to customize the OCamlFormat binary)
This could be made simpler (by defining an elisp variable corresponding to the switch prefix when loading tuareg, for example) but it allows to have a full configuration in one place only which is often less error prone.
Vim setup
- be sure the
ocamlformat
binary can be found inPATH
- install the Neoformat plugin
Optional: You can change the options passed to OCamlFormat (to use the option --disable-outside-detected-project
for example), you can customize NeoFormat with:
let g:opambin = substitute(system('opam config var bin'),'\n$','','''')
let g:neoformat_ocaml_ocamlformat = {
\ 'exe': g:opambin . '/ocamlformat',
\ 'no_append': 1,
\ 'stdin': 1,
\ 'args': ['--disable-outside-detected-project', '--name', '"%:p"', '-']
\ }
let g:neoformat_enabled_ocaml = ['ocamlformat']