caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Leo White <lpw25@cam.ac.uk>
To: Anil Madhavapeddy <anil@recoil.org>
Cc: Gabriel Scherer <gabriel.scherer@gmail.com>,
	 caml users <caml-list@inria.fr>,
	 ivg@ieee.org,  opam-devel@lists.ocaml.org,
	 Christophe TROESTLER <Christophe.Troestler@umons.ac.be>,
	 Wojciech Meyer <wojciech.meyer@gmail.com>
Subject: Re: [opam-devel] [Caml-list] Setting up OPAM in emacs
Date: Wed, 19 Jun 2013 17:24:58 +0100	[thread overview]
Message-ID: <87k3lqyso5.fsf@kingston.cl.cam.ac.uk> (raw)
In-Reply-To: <E462B10B-15FA-4C33-BC78-233BE6A7A448@recoil.org> (Anil Madhavapeddy's message of "Sat, 15 Jun 2013 16:14:35 +0100")


The development version of OPAM now has a "--sexp" option for "opam
config env". So after the next release putting the following in your
".emacs" file should be sufficient for most users:

  ; Set OPAM environment variables
  (dolist (var (car (read-from-string (shell-command-to-string "opam config env --sexp"))))
    (setenv (car var) (cadr var)))

Regards,

Leo

Anil Madhavapeddy <anil@recoil.org> writes:

> I don't use Emacs, but OPAM has several global command-line options that
> may make your OS^H^Heditor integration easier.
>
> * --root will set the home directory of OPAM (~/.opam by default)
>
> * --switch will explicitly run the command under a compiler switch that
>   isn't the default one, but without performing a global switch.
>
> * "opam config exec" will run the sub-command with the right environment
>   variables set to the selected switch.  So:
>
>     $ opam config exec "ocamlc -version"
>
>   will get the version of the current OPAM switch ocaml, irrespective
>   of your PATH settings. (This is what Leo is doing for his interactive
>   shell below, but I'm not sure how this interacts with the rest of 
>   Tuareg).
>
> I would appreciate an Emacs user summarising best practises here and
> letting me know so that I can include a short summary in Real World OCaml.
>
> best,
> Anil
>
> On 15 Jun 2013, at 13:07, Gabriel Scherer <gabriel.scherer@gmail.com> wrote:
>
>> I don't (want to) understand the details of why opam switches don't
>> work well with Emacs right now, or of any of the proposed solutions.
>> Is there a solution integrated in OPAM by now? If not, is there a
>> consensus on what simple users like me should use to solve the
>> problem? Could you (the thread participants) decide on something, and
>> send that to the OPAM upstream, so that future versions solve this
>> issue?
>> 
>> (The immediate problem I observe is that compile-command does not use
>> the OCaml versions I would expect it to, in general defaulting to my
>> system OCaml -- that may depend on the way Emacs is launched. More
>> sophisticated things such as being able to call "opam switch" from
>> inside Emacs are probably secondary needs, and I would be fine with a
>> basic integration not supporting that, and letting people hone their
>> emacs-lisp skills for that.)
>> 
>> On Tue, Jun 4, 2013 at 4:55 PM, Leo White <lpw25@cam.ac.uk> wrote:
>>> My personal solution for Tuareg mode is to put:
>>> 
>>>  ;; Use OPAM's ocaml
>>>  (setq tuareg-interactive-program "opam config exec ocaml")
>>> 
>>> in my .emacs.
>>> 
>>> Regards,
>>> 
>>> Leo
>>> 
>>> Wojciech Meyer <wojciech.meyer@gmail.com> writes:
>>> 
>>>> Christophe's function hook is probably much better and elegant solution,
>>>> but this works for me:
>>>> 
>>>>>> 
>>>> (defun dm-opam-switch-root (ocaml-version opam-root)
>>>>  (interactive "sOCaml version: \nsOPAM root: ")
>>>>  (let* ((path (if (string-match "\\.opam" (car exec-path)) (cdr exec-path) exec-path))
>>>>         (entry
>>>>          (replace-regexp-in-string
>>>>           "\n" ""
>>>>           (shell-command-to-string
>>>>            (format "opam switch --root %s %s > /dev/null && opam config env | sed -n 's/^PATH=\\([^:]\\+\\):.*$/\\1/p'" opam-root ocaml-version))))
>>>>         (env-path (replace-regexp-in-string "\n" "" (shell-command-to-string (format "opam config env --root %s | sed -n 's/^PATH=\\(.*\\)/\\1/p'" opam-root)))))
>>>>    (setq exec-path (cons entry path))
>>>>    (setenv "PATH" env-path)))
>>>> 
>>>> (defun dm-opam-switch (ocaml-version)
>>>>  (interactive "sOCaml version: ")
>>>>  (dm-opam-switch-root ocaml-version "~/.opam"))
>>>> <<
>>>> 
>>>> I wouldn't say myself it's a perfect script, but it works. Note that I
>>>> use a lot of shell trickery instead of Emacs buffers, which is probably
>>>> not a good idea.
>>>> 
>>>> You just say M-x dm-opam-switch, and indicate which version you would
>>>> like.
>>>> 
>>>> Best,
>>>> Wojciech
>>>> 
>>>> Christophe TROESTLER <Christophe.Troestler@umons.ac.be> writes:
>>>> 
>>>>> On Tue, 28 May 2013 09:59:53 +0400, Ivan Gotovchits wrote:
>>>>>> 
>>>>>> 2. Use «eval `opam env config`» in the begining of a compile-command.
>>>>>>   This works fine when you switch compiler, but other commands, such as
>>>>>>   «C-c C-s» doesn't use new environment.
>>>>> 
>>>>> I use this for the compilation:
>>>>> 
>>>>>    ;; OPAM compilation — one must update to the current compiler
>>>>>    ;; before launching the compilation.
>>>>>    (defadvice compile (before compile-opam activate)
>>>>>      "Run opam to update environment variables"
>>>>>      (set (make-local-variable 'compilation-environment)
>>>>>        (with-temp-buffer
>>>>>          (ignore-errors (call-process "opam" nil t nil "config" "-env"))
>>>>>          (goto-char (point-min))
>>>>>          (while (re-search-forward "; *export.*$" nil t)
>>>>>            (replace-match "" nil nil))
>>>>>          (split-string (buffer-substring 1 (point-max)))
>>>>>          )))
>>>>> 
>>>>> For the toplevel, I am afraid one needs to restart it.  I guess a
>>>>> similar trick is possible to automatically select the right one.
>>>>> 
>>>>> Hope it helps,
>>>>> C.
>>> 
>>> --
>>> Caml-list mailing list.  Subscription management and archives:
>>> https://sympa.inria.fr/sympa/arc/caml-list
>>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>> _______________________________________________
>> opam-devel mailing list
>> opam-devel@lists.ocaml.org
>> http://lists.ocaml.org/listinfo/opam-devel
>> 

  reply	other threads:[~2013-06-19 16:25 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-28  5:59 Ivan Gotovchits
2013-05-28  8:05 ` Daniel Bünzli
     [not found] ` <20130528.102112.1182989008075506917.Christophe.Troestler@umons.ac.be>
2013-05-29 21:39   ` Wojciech Meyer
2013-06-04 14:55     ` Leo White
2013-06-15 12:07       ` Gabriel Scherer
2013-06-15 15:14         ` [opam-devel] " Anil Madhavapeddy
2013-06-19 16:24           ` Leo White [this message]
2013-06-19 16:05         ` Ivan Gotovchits
2013-06-19 16:06         ` [opam-devel] " Ivan Gotovchits
2013-06-19 16:49         ` Ivan Gotovchits
2013-06-19 16:54           ` Ivan Gotovchits
2013-06-19 16:06       ` Ivan Gotovchits
2013-06-20  1:10         ` Francois Berenger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k3lqyso5.fsf@kingston.cl.cam.ac.uk \
    --to=lpw25@cam.ac.uk \
    --cc=Christophe.Troestler@umons.ac.be \
    --cc=anil@recoil.org \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@gmail.com \
    --cc=ivg@ieee.org \
    --cc=opam-devel@lists.ocaml.org \
    --cc=wojciech.meyer@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).