[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/opam-switch-mode 3a1c181d04 01/31: first version, can set
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/opam-switch-mode 3a1c181d04 01/31: first version, can set but not reset |
Date: |
Mon, 14 Nov 2022 08:59:59 -0500 (EST) |
branch: elpa/opam-switch-mode
commit 3a1c181d046b3857ec2508afd33768d9af8a64fe
Author: Hendrik Tews <Hendrik.Tews@kernkonzept.com>
Commit: Hendrik Tews <Hendrik.Tews@kernkonzept.com>
first version, can set but not reset
---
opam-mode.el | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 155 insertions(+)
diff --git a/opam-mode.el b/opam-mode.el
new file mode 100644
index 0000000000..c8d1e8ba6b
--- /dev/null
+++ b/opam-mode.el
@@ -0,0 +1,155 @@
+
+(require 'seq)
+
+(defcustom opam-program-name "opam"
+ "XXX")
+
+(defcustom opam-common-options ()
+ "XXX")
+
+(defcustom opam-common-environment
+ '("OPAMUTF8=never"
+ "OPAMCOLOR=never"
+ "LC_ALL=C")
+ "XXX")
+
+(defun opam-run-command-without-stderr (sub-cmd
+ &optional switch sexp
+ &rest args)
+ "XXX"
+ (let ((process-environment
+ (append opam-common-environment process-environment))
+ (options (append args opam-common-options)))
+ (when switch
+ (push (format "--switch=%s" switch) options))
+ (when sexp
+ (push "--sexp" options))
+ ;; (message "run %s %s %s" opam-program-name sub-cmd options)
+ (apply 'process-file opam-program-name
+ nil '(t nil) nil sub-cmd options)))
+
+(defun opam-command-as-string (sub-cmd &optional switch sexp &rest args)
+ "XXX"
+ (with-temp-buffer
+ (apply 'opam-run-command-without-stderr sub-cmd switch sexp args)
+ (buffer-string)))
+
+(defun opam-run-command-with-stderr (err-buf sub-cmd &optional switch sexp
&rest args)
+ "XXX"
+ (let ((process-environment
+ (append opam-common-environment process-environment))
+ (options (append args opam-common-options))
+ (temp-file (make-nearby-temp-file "emacs-opam-")))
+ (when switch
+ (push (format "--switch=%s" switch) options))
+ (when sexp
+ (push "--sexp" options))
+ (apply 'process-file opam-program-name
+ nil (list t temp-file) nil sub-cmd options)
+ (with-current-buffer err-buf
+ (insert-file-contents temp-file))
+ (ignore-errors (delete-file temp-file))))
+
+;; (defun test (sub-cmd switch sexp)
+;; (let (err-buf stdout-string)
+;; (with-temp-buffer
+;; (setq err-buf (current-buffer))
+;; (with-temp-buffer
+;; (opam-run-command-with-stderr err-buf sub-cmd switch sexp)
+;; (setq stdout-string (buffer-string)))
+;; (cons stdout-string (buffer-string)))))
+
+
+
+(defun opam-get-root ()
+ (let ((root (opam-command-as-string "var" nil nil "root")))
+ (when (eq (aref root (1- (length root))) ?\n)
+ (setq root (substring root 0 -1)))
+ root))
+
+(defconst opam-root (opam-get-root)
+ "XXX")
+
+;; OPAMUTF8=never OPAMCOLOR=never LC_ALL=C opam switch
+;; # switch compiler description
+;; -> 4112-coq-812 ocaml-variants.4.11.2+flambda 4112-coq-812
+;; 44 ocaml-base-compiler.4.04.0
+;; 450-coq-8.9 ocaml-base-compiler.4.05.0 450-coq-8.9
+;; 471-no-coq ocaml-base-compiler.4.07.1 471-no-coq
+;; system ocaml-system.4.01.0
+;;
+;; # switch compiler description
+;; -> default ocaml.4.13.1 default
+;;
+;; [WARNING] The environment is not in sync with the current switch.
+;; You should run: eval $(opam env)
+
+(defun opam-get-switches ()
+ ""
+ (let (opam-switches)
+ (with-temp-buffer
+ (opam-run-command-without-stderr "switch")
+ (goto-char (point-min))
+ (forward-line)
+ (while (re-search-forward "^\\(..\\) *\\([^ ]*\\).*$" nil t)
+ (push (match-string 2) opam-switches)
+ (when (equal (match-string 1) "->")
+ (setq default-switch (match-string 2))))
+ opam-switches)))
+
+(defvar opam-switch-history nil
+ "XXX")
+
+(defvar opam-saved-env nil
+ "XXX")
+
+(defvar opam-saved-exec-path nil
+ "XXX")
+
+
+(defun opam-save-current-env (opam-env)
+ "XXX"
+ (setq opam-saved-env
+ (mapcar (lambda (x) (list (car x) (getenv (car x)))) opam-env))
+ (setq opam-saved-exec-path exec-path))
+
+
+(defun opam-set-env (opam-env)
+ "XXX"
+ (let ((new-bin-dir
+ (seq-find
+ (lambda (dir) (string-prefix-p opam-root dir))
+ (parse-colon-path (cadr (assoc "PATH" opam-env))))))
+ (unless new-bin-dir
+ (error "No opam-root directory in PATH"))
+ (mapc (lambda (x) (setenv (car x) (cadr x))) opam-env)
+ (setq exec-path
+ (seq-remove (lambda (dir) (string-prefix-p opam-root dir))
exec-path))
+ (push new-bin-dir exec-path)))
+
+
+(defun opam-set-switch (switch-name)
+ "XXX"
+ (interactive
+ (let* ((switches (opam-get-switches))
+ (default (car switches))
+ (current-switch (getenv "OPAM_SWITCH_PREFIX")))
+ (if current-switch
+ (setq current-switch (file-name-nondirectory current-switch))
+ (setq current-switch "<none>"))
+ (list
+ (completing-read
+ (format "current switch %s; switch to (empty to reset): "
current-switch)
+ switches nil t "" 'opam-switch-history nil))))
+ (when (and (equal switch-name "") (not opam-saved-env))
+ (error "No saved opam environment, cannot reset."))
+ (if (equal switch-name "")
+ (opam-reset-env)
+ (let ((opam-env
+ (car (read-from-string
+ (opam-command-as-string "env" switch-name t)))))
+ (unless opam-saved-env
+ (opam-save-current-env opam-env))
+ (opam-set-env opam-env))))
+
+(provide 'opam-mode)
- [nongnu] elpa/opam-switch-mode e2ed274519 05/31: add emacs code documentation, also delete default switch parsing, (continued)
- [nongnu] elpa/opam-switch-mode e2ed274519 05/31: add emacs code documentation, also delete default switch parsing, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode 7de138a0e9 07/31: add minor mode with menu bar menu, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode e681cbdcb7 29/31: chore: Update header and maintainer mailing list, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode a83de84719 19/31: refactor: Replace `opsw--` with `opam-switch--`, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode 8a021ff128 12/31: rename to opam-switch-mode; use opsw as prefix for internals, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode df290bd0e3 21/31: Merge pull request #6 from ProofGeneral/prepare-melpa, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode eaf04b959d 25/31: fix: Improve error handling further (if opam can't be found), ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode 9d8aa6d5dc 08/31: display current switch in menu, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode 2c5ccd61f8 24/31: fix: Address review comments, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode d7ccecbf51 11/31: reset proof shell in Proof General via a hook function, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode 3a1c181d04 01/31: first version, can set but not reset,
ELPA Syncer <=
- [nongnu] elpa/opam-switch-mode da49a0980b 03/31: license and minimal documentation, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode 0c2ecc2834 02/31: add reset, cleanup, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode 4f05665f5c 22/31: fix: Add missing autoloads, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode 4334933613 28/31: docs(README.md): Add MELPA badge, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode 7c2118fb9e 20/31: docs(README.md): Add direct hyperlink to PG online doc, ELPA Syncer, 2022/11/14
- [nongnu] elpa/opam-switch-mode c8ca450330 06/31: hint on other shells; update README, ELPA Syncer, 2022/11/14