-
Notifications
You must be signed in to change notification settings - Fork 1
/
opam-user-setup.el
122 lines (102 loc) · 4.57 KB
/
opam-user-setup.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
;; ## added by OPAM user-setup for emacs / base ## cfd3c9b7837c85cffd0c59de521990f0 ## you can edit, but keep this line
(provide 'opam-user-setup)
;; Base configuration for OPAM
(defun opam-shell-command-to-string (command)
"Similar to shell-command-to-string, but returns nil unless the process
returned 0, and ignores stderr (shell-command-to-string ignores return value)"
(let* ((return-value 0)
(return-string
(with-output-to-string
(setq return-value
(with-current-buffer standard-output
(process-file shell-file-name nil '(t nil) nil
shell-command-switch command))))))
(if (= return-value 0) return-string nil)))
(defun opam-update-env (switch)
"Update the environment to follow current OPAM switch configuration"
(interactive
(list
(let ((default
(car (split-string (opam-shell-command-to-string "opam switch show --safe")))))
(completing-read
(concat "opam switch (" default "): ")
(split-string (opam-shell-command-to-string "opam switch list -s --safe") "\n")
nil t nil nil default))))
(let* ((switch-arg (if (= 0 (length switch)) "" (concat "--switch " switch)))
(command (concat "opam config env --safe --sexp " switch-arg))
(env (opam-shell-command-to-string command)))
(when (and env (not (string= env "")))
(dolist (var (car (read-from-string env)))
(setenv (car var) (cadr var))
(when (string= (car var) "PATH")
(setq exec-path (split-string (cadr var) path-separator)))))))
(opam-update-env nil)
(defvar opam-share
(let ((reply (opam-shell-command-to-string "opam config var share --safe")))
(when reply (substring reply 0 -1))))
(add-to-list 'load-path (concat opam-share "/emacs/site-lisp"))
;; OPAM-installed tools automated detection and initialisation
(defun opam-setup-tuareg ()
(add-to-list 'load-path (concat opam-share "/tuareg") t)
(load "tuareg-site-file"))
(defun opam-setup-add-ocaml-hook (h)
(add-hook 'tuareg-mode-hook h t)
(add-hook 'caml-mode-hook h t))
(defun opam-setup-complete ()
(if (require 'company nil t)
(opam-setup-add-ocaml-hook
(lambda ()
(company-mode)
(defalias 'auto-complete 'company-complete)))
(require 'auto-complete nil t)))
(defun opam-setup-ocp-indent ()
(opam-setup-complete)
(autoload 'ocp-setup-indent "ocp-indent" "Improved indentation for Tuareg mode")
(autoload 'ocp-indent-caml-mode-setup "ocp-indent" "Improved indentation for Caml mode")
(add-hook 'tuareg-mode-hook 'ocp-setup-indent t)
(add-hook 'caml-mode-hook 'ocp-indent-caml-mode-setup t))
(defun opam-setup-ocp-index ()
(autoload 'ocp-index-mode "ocp-index" "OCaml code browsing, documentation and completion based on build artefacts")
(opam-setup-add-ocaml-hook 'ocp-index-mode))
(defun opam-setup-merlin ()
(opam-setup-complete)
(require 'merlin)
(opam-setup-add-ocaml-hook 'merlin-mode)
(defcustom ocp-index-use-auto-complete nil
"Use auto-complete with ocp-index (disabled by default by opam-user-setup because merlin is in use)"
:group 'ocp_index)
(defcustom merlin-ac-setup 'easy
"Use auto-complete with merlin (enabled by default by opam-user-setup)"
:group 'merlin-ac)
;; So you can do it on a mac, where `C-<up>` and `C-<down>` are used
;; by spaces.
(define-key merlin-mode-map
(kbd "C-c <up>") 'merlin-type-enclosing-go-up)
(define-key merlin-mode-map
(kbd "C-c <down>") 'merlin-type-enclosing-go-down)
(set-face-background 'merlin-type-face "skyblue"))
(defun opam-setup-utop ()
(autoload 'utop "utop" "Toplevel for OCaml" t)
(autoload 'utop-minor-mode "utop" "Minor mode for utop" t)
(add-hook 'tuareg-mode-hook 'utop-minor-mode))
(defvar opam-tools
'(("tuareg" . opam-setup-tuareg)
("ocp-indent" . opam-setup-ocp-indent)
("ocp-index" . opam-setup-ocp-index)
("merlin" . opam-setup-merlin)
("utop" . opam-setup-utop)))
(defun opam-detect-installed-tools ()
(let*
((command "opam list --installed --short --safe --color=never")
(names (mapcar 'car opam-tools))
(command-string (mapconcat 'identity (cons command names) " "))
(reply (opam-shell-command-to-string command-string)))
(when reply (split-string reply))))
(defvar opam-tools-installed (opam-detect-installed-tools))
(defun opam-auto-tools-setup ()
(interactive)
(dolist (tool opam-tools)
(when (member (car tool) opam-tools-installed)
(funcall (symbol-function (cdr tool))))))
(opam-auto-tools-setup)
;; ## end of OPAM user-setup addition for emacs / base ## keep this line