-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshells.el
213 lines (178 loc) · 6.28 KB
/
shells.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
;; -*- lexical-binding:t -*-
;;;;
;; Nore Emacs
;; https://github.com/junjiemars/.emacs.d
;;;;
;; shells.el
;;;;
;; Commentary: shell's environment
;;;;
;;;
;; spec
;;;
(defun shells-spec->* (&optional key)
"Extract :shell from env-spec via KEY."
(cond (key (env-spec->* :shell key))
(t (env-spec->* :shell))))
(defmacro shells-spec->% (key)
"Return :shell spec of \\=`+nore-spec+\\='."
(cond ((eq key :file) (v-home% ".exec/shell-env.el"))
((eq key :SHELL) "SHELL")
((eq key :PATH) "PATH")))
(defalias '*default-shell-env*
(let ((dx nil))
(lambda (&optional op k n)
(cond ((eq :get op) (plist-get dx k))
((eq :put! op) (setq dx (plist-put dx k n)))
((eq :set! op) (setq dx k))
(t dx))))
"Default shell\\='s environment.")
;; end of spec
;;;
;; vars/paths
;;;
(defun echo-var (var &optional options)
"Return the value of $VAR via echo."
(when (stringp var)
(let ((rc (if-platform% windows-nt
(if (string-match "cmdproxy\\.exe$" shell-file-name)
(shell-command* (format "echo %%%s%%" var))
;; non cmdproxy
(shell-command* shell-file-name
(mapconcat #'identity options " ")
(format "-c 'echo $%s'" var)))
;; other platforms
(shell-command* shell-file-name
(mapconcat #'identity options " ")
(format "-c 'echo $%s'" var)))))
(and (zerop (car rc))
(string-trim> (cdr rc))))))
(defun paths->var (path &optional predicate)
"Convert a list of PATH to $PATH like var that separated by
\\=`path-separator\\='."
(string-trim>
(let ((vs))
(dolist (s path vs)
(setq vs (concat vs (if (null predicate)
(concat s path-separator)
(when (and (functionp predicate)
(funcall predicate s))
(concat s path-separator)))))))
path-separator))
(defun var->paths (var)
"Refine VAR like $PATH to list by \\=`path-separator\\='.\n
See also: \\=`parse-colon-path\\='."
(and (stringp var)
(split-string* var path-separator t "[ ]+\n")))
;; end of vars/paths
;;;
;; env
;;;
(defun setenv* (name value)
"Change or add an environment variable: NAME=VALUE.\n
See \\=`setenv\\='."
(let* ((env process-environment)
(name= (concat name "="))
(newval (concat name= value)))
(if (catch 'br
(while (car env)
(and (string= name= (car env))
(setcar env newval)
(throw 'br t))
(setq env (cdr env))))
process-environment
(setq process-environment (cons newval process-environment)))))
(defun copy-env-vars! (env vars)
(dolist (v vars)
(and (> (length v) 0)
(let ((v1 (cdr (assoc-string v env))))
(and (stringp v1) (setenv* v v1))))))
(defun spin-env-vars! (vars)
(dolist (v vars)
(and (stringp (car v)) (stringp (cdr v))
(setenv* (car v) (cdr v)))))
(defmacro copy-exec-path! (path)
(let ((p (gensym*)))
`(let ((,p ,path))
(and ,p (setq exec-path ,p)))))
;; end of env
;;;
;; windows
;;;
(when-platform% windows-nt
(defun windows-nt-env-path+ (dir &optional append)
"APPEND or push DIR to %PATH%."
(let ((env (var->paths (getenv (shells-spec->% :PATH)))))
(when (or (and (null append) (not (string= dir (car env))))
(and append (not (string= dir (last env)))))
(let ((path (let ((xs nil))
;; remove dir
(dolist (x env (nreverse xs))
(unless (string= x dir)
(setq xs (cons x xs)))))))
(setenv* (shells-spec->% :PATH)
(paths->var (if append
(append path dir)
(cons dir path)))))))))
;; end of windows
;;;
;; save/read env
;;;
(defun self-shell-save! ()
"Save \\=`*default-shell-env*\\=' to file."
(let ((vars nil)
(default-directory (emacs-home%)))
(dolist (v (shells-spec->* :copy-vars) vars)
(when (stringp v)
(let ((val (echo-var v (shells-spec->* :options))))
(when (and val (> (length val) 0))
(when (string= v (shells-spec->% :PATH))
(let ((paths nil))
(dolist (p (var->paths val))
(let ((p1 (if-platform% windows-nt
(posix-path p)
p)))
(when (and (stringp p1) (file-exists-p p1))
(when (shells-spec->* :exec-path)
(push! p1 exec-path))
(append! p1 paths t))))
(setq val (paths->var paths))))
(push! (cons v val) vars)))))
(*default-shell-env* :set! nil)
(*default-shell-env* :put! :copy-vars vars)
(when (shells-spec->* :exec-path)
(let ((paths '()))
(dolist (p exec-path)
(and (and (stringp p) (file-exists-p p))
(append! p paths t)))
(append! (v-home% ".exec/") paths t)
(*default-shell-env* :put! :exec-path paths))))
(save-sexp-to-file (*default-shell-env*) (shells-spec->% :file)))
(defun self-shell-read! ()
"Read \\=`*default-shell-env*\\=' from file."
(cond ((null (shells-spec->* :allowed))
(append! (v-home% ".exec/") exec-path))
(t
;; read from file
(*default-shell-env*
:set!
(read-sexp-from-file (shells-spec->% :file)))
;; `shell-file-name'
(let ((shell (shells-spec->* :shell-file-name)))
(when shell
(setq% explicit-shell-file-name shell shell)
(setq shell-file-name shell)
(setenv* (shells-spec->% :SHELL) shell)))
;; :copy-vars
(copy-env-vars! (*default-shell-env* :get :copy-vars)
(shells-spec->* :copy-vars))
;; :exec-path
(when (shells-spec->* :exec-path)
(copy-exec-path!
(*default-shell-env* :get :exec-path)))
;; :spin-vars
(spin-env-vars! (shells-spec->* :spin-vars))))
(append! #'self-shell-save! kill-emacs-hook))
;; save/read env
(provide 'shells)
;; end of shells.el