Function: neocaml-base-mode

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

Signature

(neocaml-base-mode)

Documentation

Base major mode for OCaml files, providing shared setup.

This mode is not intended to be used directly. Use neocaml-mode for .ml files and neocaml-interface-mode for .mli files.

In addition to any hooks its parent mode prog-mode might have run, this mode runs the hook neocaml-base-mode-hook, as the final or penultimate step during initialization.

C-M-u neocaml-backward-up-list
C-c 4 C-a ff-find-other-file-other-window
C-c C-a ff-find-other-file
C-c C-c compile
C-c C-f neocaml-format-buffer

Key Bindings

This command is not in any keymaps.

Source Code

;; Defined in /nix/store/ahssdx4rwzyxhyhavpn0bkznk13xrggi-emacs-packages-deps/share/emacs/site-lisp/elpa/neocaml-20260907.2015/neocaml.el
(define-derived-mode neocaml-base-mode prog-mode "OCaml"
  "Base major mode for OCaml files, providing shared setup.
This mode is not intended to be used directly.  Use `neocaml-mode'
for .ml files and `neocaml-interface-mode' for .mli files."
  :syntax-table neocaml-base-mode-syntax-table

  ;; Syntax propertization for char literals and quoted strings, so the
  ;; syntactic layer agrees with the parse tree (see commentary above).
  (setq-local syntax-propertize-function #'neocaml--syntax-propertize)

  ;; comment settings
  (setq-local comment-start "(* ")
  (setq-local comment-end " *)")
  (setq-local comment-start-skip "(\\*+[ \t]*")
  (setq-local comment-multi-line t)
  (setq-local comment-line-break-function #'neocaml--comment-indent-new-line)

  ;; Electric indentation on delimiters
  (setq-local electric-indent-chars
              (append "{}()" electric-indent-chars))

  ;; Fill paragraph
  (setq-local fill-paragraph-function #'neocaml--fill-paragraph)
  (setq-local adaptive-fill-mode t)

  ;; Optional format-on-save via ocamlformat
  (add-hook 'before-save-hook #'neocaml--format-before-save nil t)

  ;; Default to `dune build' for `compile'/`project-compile' in dune projects.
  (when (locate-dominating-file default-directory "dune-project")
    (setq-local compile-command "dune build"))

  ;; Make URLs and bug references in comments clickable.  Set
  ;; `bug-reference-url-format' (e.g. via .dir-locals.el) to resolve refs.
  (goto-address-prog-mode)
  (bug-reference-prog-mode)

  ;; TODO: Make this configurable?
  (setq-local treesit-font-lock-feature-list
              '((comment definition)
                (keyword string type)
                (attribute builtin constant doc-markup escape-sequence number)
                (operator bracket delimiter variable property label function)))

  (setq-local indent-line-function #'treesit-indent)

  ;; Emacs 29 has no treesit-thing-settings, so treesit-major-mode-setup
  ;; won't configure forward-sexp.  Set the hybrid function directly.
  (unless (boundp 'treesit-thing-settings)
    (setq-local forward-sexp-function #'neocaml--forward-sexp-hybrid))
  (setq-local treesit-defun-type-regexp
              (cons neocaml--defun-type-regexp
                    #'neocaml--defun-valid-p))
  (setq-local treesit-defun-name-function #'neocaml--defun-name)

  ;; which-func-mode / add-log integration
  (setq-local add-log-current-defun-function #'treesit-add-log-current-defun)

  ;; outline-minor-mode integration (Emacs 30+)
  (when (boundp 'treesit-outline-predicate)
    (setq-local treesit-outline-predicate
                (cons neocaml--defun-type-regexp
                      #'neocaml--defun-valid-p)))

  ;; ff-find-other-file setup
  (setq-local ff-other-file-alist neocaml-other-file-alist)

  ;; Setup prettify-symbols (users enable prettify-symbols-mode via hooks)
  (setq-local prettify-symbols-alist (neocaml--prettify-symbols-alist))

  ;; Register neocaml modes with eglot so it knows to start ocamllsp.
  (neocaml--register-with-eglot)

  ;; Register neocaml modes with dape's ocamlearlybird config.
  (neocaml--register-with-dape))