Function: tuareg-mode

tuareg-mode is an interactive and natively compiled function defined in tuareg.el.

Signature

(tuareg-mode)

Documentation

Major mode for editing OCaml code.

Provides automatic indentation and compilation interface. Performs font/color highlighting using Font-Lock. It is designed for OCaml but handles Caml Light as well.

The Font-Lock minor-mode is used according to your customization options.

You have better byte-compile tuareg.el.

For customization purposes, you should use tuareg-mode-hook
(run for every file) or tuareg-load-hook (run once) and not patch
the mode itself. You should add to your configuration file something like:
  (add-hook 'tuareg-mode-hook
            (lambda ()
               ... ; your customization code
            ))
For example you can change the indentation of some keywords, the electric flags, Font-Lock colors... Every customizable variable is documented, use C-h-v or look at the mode's source code.

dot-emacs.el is a sample customization file for standard changes. You can append it to your .emacs or use it as a tutorial.

M-x ocamldebug FILE starts the OCaml debugger ocamldebug on the executable FILE, with input and output in an Emacs buffer named *ocamldebug-FILE*.

A Tuareg Interactive Mode to evaluate expressions in a REPL (aka toplevel) is included. Type M-x tuareg-run-ocaml or simply M-x run-ocaml or see special-keys below.

Short cuts for the Tuareg mode:
C-M-x tuareg-eval-phrase
C-c . b tuareg-insert-begin-form
C-c . c tuareg-insert-class-form
C-c . f tuareg-insert-for-form
C-c . i tuareg-insert-if-form
C-c . l tuareg-insert-let-form
C-c . m tuareg-insert-match-form
C-c . t tuareg-insert-try-form
C-c . w tuareg-insert-while-form
C-c C-; tuareg-comment-dwim
C-c C-a tuareg-find-alternate-file
C-c C-b tuareg-eval-buffer
C-c C-c compile
C-c C-e tuareg-eval-phrase
C-c C-k tuareg-kill-ocaml
C-c C-q tuareg-indent-phrase
C-c C-r tuareg-eval-region
C-c C-s tuareg-run-ocaml
C-c C-w tuareg-opam-update-env
C-c C-z tuareg-switch-to-repl
C-c TAB tuareg-interrupt-ocaml
C-c ` tuareg-interactive-next-error-source
C-x C-e tuareg-eval-phrase
M-q tuareg-indent-phrase

Short cuts for interactions with the REPL:
<delete> delete-forward-char
<kp-delete> delete-forward-char
<kp-enter> tuareg-interactive-send-input-end-of-phrase
<mouse-2> comint-insert-input
C-<down> comint-next-input
C-<return> tuareg-interactive-send-input-end-of-phrase
C-<up> comint-previous-input
C-M-l comint-show-output
C-c . comint-insert-previous-argument
C-c C-\ comint-quit-subjob
C-c C-a comint-bol-or-process-mark
C-c C-c comint-interrupt-subjob
C-c C-d comint-send-eof
C-c C-e comint-show-maximum-output
C-c C-k tuareg-kill-ocaml
C-c C-l comint-dynamic-list-input-ring
C-c C-n comint-next-prompt
C-c C-o comint-delete-output
C-c C-p comint-previous-prompt
C-c C-r comint-show-output
C-c C-s comint-write-output
C-c C-u comint-kill-input
C-c C-w backward-kill-word
C-c C-x comint-get-next-from-history
C-c C-z tuareg-switch-to-recent-buffer
C-c M-o comint-clear-buffer
C-c M-r comint-previous-matching-input-from-input
C-c M-s comint-next-matching-input-from-input
C-c RET comint-copy-old-input
C-c SPC comint-accumulate
C-c TAB tuareg-interrupt-ocaml
C-c ` tuareg-interactive-next-error-repl
C-d comint-delchar-or-maybe-eof
C-x <up> comint-complete-input-ring
M-n comint-next-input
M-p comint-previous-input
M-r comint-history-isearch-backward-regexp
RET tuareg-interactive-send-input
S-<return> tuareg-interactive-send-input-end-of-phrase

Key Bindings

This command is not in any keymaps.

Source Code

;; Defined in /nix/store/6mbh851dhnag8f92qfrlh9ckldzwf6c6-emacs-packages-deps/share/emacs/site-lisp/elpa/tuareg-20260626.936/tuareg.el
;;;###autoload
(define-derived-mode tuareg-mode prog-mode "Tuareg"
  "Major mode for editing OCaml code.

Provides automatic indentation and compilation interface.  Performs font/color
highlighting using Font-Lock.  It is designed for OCaml but handles
Caml Light as well.

The Font-Lock minor-mode is used according to your customization
options.

You have better byte-compile tuareg.el.

For customization purposes, you should use `tuareg-mode-hook'
\(run for every file) or `tuareg-load-hook' (run once) and not patch
the mode itself.  You should add to your configuration file something like:
  (add-hook \\='tuareg-mode-hook
            (lambda ()
               ... ; your customization code
            ))
For example you can change the indentation of some keywords, the
`electric' flags, Font-Lock colors... Every customizable variable is
documented, use `C-h-v' or look at the mode's source code.

`dot-emacs.el' is a sample customization file for standard changes.
You can append it to your `.emacs' or use it as a tutorial.

`M-x ocamldebug' FILE starts the OCaml debugger ocamldebug on the executable
FILE, with input and output in an Emacs buffer named *ocamldebug-FILE*.

A Tuareg Interactive Mode to evaluate expressions in a REPL (aka toplevel) is
included.  Type `M-x tuareg-run-ocaml' or simply `M-x run-ocaml' or see
special-keys below.

Short cuts for the Tuareg mode:
\\{tuareg-mode-map}

Short cuts for interactions with the REPL:
\\{tuareg-interactive-mode-map}"

  (setq mode-name
        '(tuareg--other-file
          ;; FIXME: Clicking on the "+mli" should probably jump to the
          ;; .mli file.
          ("" tuareg-mode-name "[+" tuareg--other-file "]")
          tuareg-mode-name))
  ;; FIXME: Update `tuareg--other-file' every once in a while, e.g. when we
  ;; save the `.ml' or `.mli' file.
  (setq tuareg--other-file
        (if tuareg-mode-line-other-file (tuareg--other-file buffer-file-name)))
  (unless (tuareg--switch-outside-build)
    ;; Initialize the Tuareg menu
    (tuareg-build-menu)

    (setq-local paragraph-start (concat "^[ \t]*$\\|\\*)$\\|" page-delimiter))
    (setq-local paragraph-separate paragraph-start)
    (setq-local require-final-newline mode-require-final-newline)
    (setq-local comment-start "(* ")
    (setq-local comment-end " *)")
    (setq-local comment-start-skip "(\\*+[ \t]*")
    ;; `ocamlc' counts columns from 0, contrary to other tools which start at 1.
    (setq-local compilation-first-column 0)
    (setq-local compilation-error-screen-columns nil)
    ;; TABs should NOT be used in OCaml files:
    (setq indent-tabs-mode nil)
    (setq ff-search-directories '(".")
          ff-other-file-alist tuareg-other-file-alist)
    (add-hook 'ff-file-created-hook #'tuareg--ff-file-created-hook nil t)
    (tuareg--common-mode-setup)
    (tuareg--install-font-lock)
    (setq-local beginning-of-defun-function #'tuareg-beginning-of-defun)
    (setq-local end-of-defun-function #'tuareg-end-of-defun)
    (setq imenu-create-index-function #'tuareg-imenu-create-index)
    (run-mode-hooks 'tuareg-load-hook)))