Skip to content

Commit

Permalink
Apply review suggestions by @monnier
Browse files Browse the repository at this point in the history
(tuareg-opam-update-env): call #'opam-switch-set-switch if (featurep 'opam-switch-mode)
  • Loading branch information
erikmd committed Jul 18, 2023
1 parent 50e91db commit a2f1ea4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
35 changes: 21 additions & 14 deletions tuareg-opam.el
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ opam switch at the time the command is run (provided opam is
found). You may also use `tuareg-opam-update-env', or the menus
from the ELPA package `opam-switch-mode', to set the
environment for another compiler from within emacs (without
changing the opam switch). Beware that setting it to t causes
problems if you compile under tramp."
affecting the OPAM switches outside of this Emacs session).
Beware that setting it to t causes problems if you compile under tramp."
:group 'tuareg :type 'boolean)

(defun tuareg--shell-command-to-string (command)
Expand Down Expand Up @@ -385,23 +385,30 @@ error message as a string)."
(defun tuareg-opam-update-env (switch)
"Update the environment to follow current OPAM switch configuration.
You may also be interested in the ELPA package `opam-switch-mode' that
provides a similar feature, along with a menu-bar and a mode-bar menu
`\"OPSW\"'; see https://github.com/ProofGeneral/opam-switch-mode."
Delegate the task to `opam-switch-set-switch' if the minor mode
`opam-switch-mode' (https://github.com/ProofGeneral/opam-switch-mode)
is installed. This ELPA package also provides a menu-bar and a
mode-bar menu `\"OPSW\"'."
(interactive
(let* ((compl (tuareg-opam-installed-compilers))
(current (tuareg-opam-current-compiler))
(default (if current current "current"))
(prompt (format "opam switch (default: %s): " default)))
(list (completing-read prompt compl))))
(let* ((switch (if (string= switch "") nil switch))
(env (tuareg-opam-config-env switch)))
(if env
(dolist (v env)
(setenv (car v) (cadr v))
(when (string= (car v) "PATH")
(setq exec-path (split-string (cadr v) path-separator))))
(message "Switch %s does not exist (or opam not found)" switch))))
(if (featurep 'opam-switch-mode)
(list "")
(list (completing-read prompt compl)))))
(if (featurep 'opam-switch-mode)
(if (called-interactively-p 'any)
(call-interactively #'opam-switch-set-switch)
(funcall opam-switch-set-switch switch))
(let* ((switch (if (string= switch "") nil switch))
(env (tuareg-opam-config-env switch)))
(if env
(dolist (v env)
(setenv (car v) (cadr v))
(when (string= (car v) "PATH")
(setq exec-path (split-string (cadr v) path-separator))))
(message "Switch %s does not exist (or opam not found)" switch)))))

;; OPAM compilation
(defun tuareg--compile-opam (&rest _)
Expand Down
15 changes: 6 additions & 9 deletions tuareg.el
Original file line number Diff line number Diff line change
Expand Up @@ -3937,22 +3937,19 @@ If the region is active, evaluate all phrases intersecting the region."
(comint-kill-subjob))))

(defcustom tuareg-kill-ocaml-on-opam-switch t
"If t, kill the OCaml toplevel before the opam switch changes.
If the user changes the opam switch using `opam-switch-set-switch'
or an `\"OPSW\"' menu from `opam-switch-mode', this option asks to
kill the OCaml toplevel process, so that the next eval command
starts a new process, typically with a different OCaml version
from a different opam switch.
"If t, kill Tuareg subprocesses when the OPAM switch changes.
If the user changes the OPAM switch using `opam-switch-mode',
this option asks to kill the subprocesses that are using the old
switch.
See https://github.com/ProofGeneral/opam-switch-mode
Note: `opam-switch-mode' triggers automatic changes for `exec-path'
and `process-environment', which are useful to find the `\"ocaml\"'
binary and that of its subprocesses, in the ambient opam switch.
`opam-switch-mode' 1.6+ is compatible with `tuareg-mode' whatever
is the value of `tuareg-opam-insinuate' (albeit the default value
nil is recommended as it omits the `\"opam exec --\"' wrapper)."
`opam-switch-mode' is compatible with `tuareg-mode' whatever is
the value of `tuareg-opam-insinuate'."
:type 'boolean)

(defun tuareg--kill-ocaml-on-opam-switch ()
Expand Down

0 comments on commit a2f1ea4

Please sign in to comment.