refactor(ocaml): entry points into ocp-indent & opam-switch-mode

Reduces edge cases, in the case the user eagerly loads any of these
packages. Also replaces all hardcoded paths to ocp-indent and opam's
executables with variables, respecting user configuration.
This commit is contained in:
Henrik Lissner 2024-08-13 16:32:59 -04:00
parent fa153d5b91
commit 8519f6fed2
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
2 changed files with 27 additions and 23 deletions

View file

@ -98,33 +98,39 @@
(use-package! ocp-indent
;; must be careful to always defer this, it has autoloads that adds hooks
;; which we do not want if the executable can't be found
:hook (tuareg-mode-local-vars . +ocaml-init-ocp-indent-h)
:hook (tuareg-mode-local-vars . ocp-setup-indent)
:hook (caml-mode-local-vars . ocp-indent-caml-mode-setup)
:init
(defadvice! +ocaml--init-ocp-indent-maybe-h (fn &rest args)
"Run `ocp-setup-indent' only if the ocp-indent binary is found."
:around #'ocp-setup-indent
(when (executable-find ocp-indent-path)
(apply fn args)))
:config
(defun +ocaml-init-ocp-indent-h ()
"Run `ocp-setup-indent', so long as the ocp-indent binary exists."
(when (executable-find "ocp-indent")
(ocp-setup-indent))))
;; HACK: The package adds these hooks, but they're redundant (even
;; counter-productive) with the hooks I add above.
(remove-hook 'tuareg-mode-hook #'ocp-setup-indent)
(remove-hook 'caml-mode-hook #'ocp-indent-caml-mode-setup))
(use-package! opam-switch-mode
:hook (tuareg-mode-local-vars . +ocaml-init-opam-switch-h)
:init
(map! :localleader
:hook (tuareg-mode-local-vars . opam-switch-mode)
:preface
(map! :after tuareg
:localleader
:map tuareg-mode-map
"w" #'opam-switch-set-switch)
(defun +ocaml-init-opam-switch-h ()
:init
(defadvice! +ocaml--init-opam-switch-mode-maybe-h (fn &rest args)
"Activate `opam-switch-mode' if the opam executable exists."
(when (executable-find "opam")
(opam-switch-mode)))
:around #'opam-switch-mode
(when (executable-find opam-switch-program-name)
(apply fn args)))
:config
;; Use opam to set environment
(setq tuareg-opam-insinuate t)
(opam-switch-set-switch (tuareg-opam-current-compiler)))
;; Tree sitter
(eval-when! (modulep! +tree-sitter)
(add-hook! 'tuareg-mode-local-vars-hook #'tree-sitter!))
(add-hook 'tuareg-mode-local-vars-hook #'tree-sitter!))

View file

@ -2,8 +2,11 @@
;;; lang/ocaml/packages.el
(package! tuareg :pin "1d53723e39f22ab4ab76d31f2b188a2879305092")
(package! opam-switch-mode :pin "1069e56a662f23ea09d4e05611bdedeb99257012")
(package! ocp-indent :pin "f38578c25d62701847b1bcb45099a9020e2032fe")
(package! dune
:recipe (:host github :repo "ocaml/dune" :files ("editor-integration/emacs/*.el"))
:pin "aac3d84f1d5abdf276d72be3dccac23bf99b3c7c")
(unless (modulep! +lsp)
(package! merlin :pin "b6ff2d4d569c23dd8fa91639d26fb984e9411862")
@ -13,11 +16,6 @@
(not (modulep! :checkers syntax +flymake)))
(package! flycheck-ocaml :pin "77f8ddbd9bfc3a11957ac7ec7e45d5fa9179b192")))
(package! ocp-indent :pin "f38578c25d62701847b1bcb45099a9020e2032fe")
(when (modulep! :tools eval)
(package! utop :pin "384b3098c8c4a2e26b87167053952b753aa8a63a"))
(package! dune
:recipe (:host github :repo "ocaml/dune" :files ("editor-integration/emacs/*.el"))
:pin "aac3d84f1d5abdf276d72be3dccac23bf99b3c7c")