Skip to content

Commit

Permalink
Allow custom commands for running ocamlformat (#2577)
Browse files Browse the repository at this point in the history
Currently it's possible to customize the path or name of the
ocamlformat executable that gets run by the emacs ocamlformat
package. However sometimes it's desirable to run ocamlformat by
running a multi-word command, such as `opam exec ocamlformat --`. This
change adds support running coamlformat with custom multi-word
commands by allowing the `ocamlformat-command` variable to be set to a
list of strings.

Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
  • Loading branch information
gridbugs authored Sep 24, 2024
1 parent 8cf0db5 commit f8e16b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ profile. This started with version 0.26.0.
- Support OCaml 5.2 syntax (#2519, @Julow)
This includes:
+ Local open in types.
- Allow a custom command to be used to run ocamlformat in the emacs plugin (#2577, @gridbugs)

### Changed

Expand Down
18 changes: 14 additions & 4 deletions emacs/ocamlformat.el
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@

(defcustom ocamlformat-command "ocamlformat"
"The `ocamlformat' command."
:type 'string
:type '(choice
(string :tag "The name of the ocamlformat executable")
(repeat :tag "The prefix of the command to run to run ocamlformat" string))
:group 'ocamlformat)

(defcustom ocamlformat-enable 'enable
Expand Down Expand Up @@ -266,15 +268,23 @@ is nil."
((eq ocamlformat-file-kind 'implementation)
(list "--impl"))
((eq ocamlformat-file-kind 'interface)
(list "--intf")))))
(list "--intf"))))
(ocamlformat-exe
(if (listp ocamlformat-command)
(car ocamlformat-command)
ocamlformat-command))
(ocamlformat-prefix-args
(if (listp ocamlformat-command)
(cdr ocamlformat-command)
'())))
(unwind-protect
(save-restriction
(widen)
(write-region nil nil bufferfile)
(if (zerop
(apply #'call-process
ocamlformat-command nil (list :file errorfile) nil
(append margin-args enable-args extension-args
ocamlformat-exe nil (list :file errorfile) nil
(append ocamlformat-prefix-args margin-args enable-args extension-args
(list
"--name" buffer-file-name
"--output" outputfile bufferfile))))
Expand Down

0 comments on commit f8e16b3

Please sign in to comment.