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!))