changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / emacs/lib/exec-path-from-shell.el

changeset 698: 96958d3eb5b0
parent: e310b68526b9
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 ;;; exec-path-from-shell.el --- Get environment variables such as $PATH from the shell -*- lexical-binding: t -*-
2 
3 ;; Copyright (C) 2012-2014 Steve Purcell
4 
5 ;; Author: Steve Purcell <steve@sanityinc.com>
6 ;; Keywords: unix, environment
7 ;; URL: https://github.com/purcell/exec-path-from-shell
8 ;; Package-Version: 2.1
9 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.6"))
10 
11 ;; This file is not part of GNU Emacs.
12 
13 ;; This file is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17 
18 ;; This file is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22 
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this file. If not, see <http://www.gnu.org/licenses/>.
25 
26 ;;; Commentary:
27 
28 ;; On OS X (and perhaps elsewhere) the $PATH environment variable and
29 ;; `exec-path' used by a windowed Emacs instance will usually be the
30 ;; system-wide default path, rather than that seen in a terminal
31 ;; window.
32 
33 ;; This library allows the user to set Emacs' `exec-path' and $PATH
34 ;; from the shell path, so that `shell-command', `compile' and the
35 ;; like work as expected.
36 
37 ;; It also allows other environment variables to be retrieved from the
38 ;; shell, so that Emacs will see the same values you get in a terminal.
39 
40 ;; If you use a non-POSIX-standard shell like "tcsh" or "fish", your
41 ;; shell will be asked to execute "sh" as a subshell in order to print
42 ;; out the variables in a format which can be reliably parsed. "sh"
43 ;; must be a POSIX-compliant shell in this case.
44 
45 ;; Note that shell variables which have not been exported as
46 ;; environment variables (e.g. using the "export" keyword) may not be
47 ;; visible to `exec-path-from-shell'.
48 
49 ;; Installation:
50 
51 ;; ELPA packages are available on Marmalade and MELPA. Alternatively,
52 ;; place this file on a directory in your `load-path', and explicitly
53 ;; require it.
54 
55 ;; Usage:
56 ;;
57 ;; (require 'exec-path-from-shell) ;; if not using the ELPA package
58 ;; (exec-path-from-shell-initialize)
59 ;;
60 ;; Customize `exec-path-from-shell-variables' to modify the list of
61 ;; variables imported.
62 ;;
63 ;; If you use your Emacs config on other platforms, you can instead
64 ;; make initialization conditional as follows:
65 ;;
66 ;; (when (memq window-system '(mac ns))
67 ;; (exec-path-from-shell-initialize))
68 ;;
69 ;; Alternatively, you can use `exec-path-from-shell-copy-envs' or
70 ;; `exec-path-from-shell-copy-env' directly, e.g.
71 ;;
72 ;; (exec-path-from-shell-copy-env "PYTHONPATH")
73 
74 ;;; Code:
75 
76 ;; Satisfy the byte compiler
77 (eval-when-compile (require 'eshell))
78 (require 'cl-lib)
79 
80 (defgroup exec-path-from-shell nil
81  "Make Emacs use shell-defined values for $PATH etc."
82  :prefix "exec-path-from-shell-"
83  :group 'environment)
84 
85 (defcustom exec-path-from-shell-variables
86  '("PATH" "MANPATH")
87  "List of environment variables which are copied from the shell."
88  :type '(repeat (string :tag "Environment variable"))
89  :group 'exec-path-from-shell)
90 
91 (defcustom exec-path-from-shell-warn-duration-millis 500
92  "Print a warning if shell execution takes longer than this many milliseconds."
93  :type 'integer)
94 
95 (defcustom exec-path-from-shell-shell-name nil
96  "If non-nil, use this shell executable.
97 Otherwise, use either `shell-file-name' (if set), or the value of
98 the SHELL environment variable."
99  :type '(choice
100  (file :tag "Shell executable")
101  (const :tag "Use `shell-file-name' or $SHELL" nil))
102  :group 'exec-path-from-shell)
103 
104 (defvar exec-path-from-shell-debug nil
105  "Display debug info when non-nil.")
106 
107 (defun exec-path-from-shell--double-quote (s)
108  "Double-quote S, escaping any double-quotes already contained in it."
109  (concat "\"" (replace-regexp-in-string "\"" "\\\\\"" s) "\""))
110 
111 (defun exec-path-from-shell--shell ()
112  "Return the shell to use.
113 See documentation for `exec-path-from-shell-shell-name'."
114  (or
115  exec-path-from-shell-shell-name
116  shell-file-name
117  (getenv "SHELL")
118  (error "SHELL environment variable is unset")))
119 
120 (defcustom exec-path-from-shell-arguments
121  (let ((shell (exec-path-from-shell--shell)))
122  (if (string-match-p "t?csh$" shell)
123  (list "-d")
124  (if (string-match-p "fish" shell)
125  (list "-l")
126  (list "-l" "-i"))))
127  "Additional arguments to pass to the shell.
128 
129 The default value denotes an interactive login shell."
130  :type '(repeat (string :tag "Shell argument"))
131  :group 'exec-path-from-shell)
132 
133 (defun exec-path-from-shell--debug (msg &rest args)
134  "Print MSG and ARGS like `message', but only if debug output is enabled."
135  (when exec-path-from-shell-debug
136  (apply 'message msg args)))
137 
138 (defun exec-path-from-shell--standard-shell-p (shell)
139  "Return non-nil iff SHELL supports the standard ${VAR-default} syntax."
140  (not (string-match "\\(fish\\|nu\\|t?csh\\)$" shell)))
141 
142 (defmacro exec-path-from-shell--warn-duration (&rest body)
143  "Evaluate BODY and warn if execution duration exceeds a time limit.
144 The limit is given by `exec-path-from-shell-warn-duration-millis'."
145  (let ((start-time (cl-gensym))
146  (duration-millis (cl-gensym)))
147  `(let ((,start-time (current-time)))
148  (prog1
149  (progn ,@body)
150  (let ((,duration-millis (* 1000.0 (float-time (time-subtract (current-time) ,start-time)))))
151  (if (> ,duration-millis exec-path-from-shell-warn-duration-millis)
152  (message "Warning: exec-path-from-shell execution took %dms. See the README for tips on reducing this." ,duration-millis)
153  (exec-path-from-shell--debug "Shell execution took %dms" ,duration-millis)))))))
154 
155 (defun exec-path-from-shell-printf (str &optional args)
156  "Return the result of printing STR in the user's shell.
157 
158 Executes the shell as interactive login shell.
159 
160 STR is inserted literally in a single-quoted argument to printf,
161 and may therefore contain backslashed escape sequences understood
162 by printf.
163 
164 ARGS is an optional list of args which will be inserted by printf
165 in place of any % placeholders in STR. ARGS are not automatically
166 shell-escaped, so they may contain $ etc."
167  (let* ((printf-bin (or (executable-find "printf") "printf"))
168  (printf-command
169  (concat printf-bin
170  " '__RESULT\\000" str "\\000__RESULT' "
171  (mapconcat #'exec-path-from-shell--double-quote args " ")))
172  (shell (exec-path-from-shell--shell))
173  (shell-args (append exec-path-from-shell-arguments
174  (list "-c"
175  (if (exec-path-from-shell--standard-shell-p shell)
176  printf-command
177  (concat "sh -c " (shell-quote-argument printf-command)))))))
178  (with-temp-buffer
179  (exec-path-from-shell--debug "Invoking shell %s with args %S" shell shell-args)
180  (let ((exit-code (exec-path-from-shell--warn-duration
181  (apply #'call-process shell nil t nil shell-args))))
182  (exec-path-from-shell--debug "Shell printed: %S" (buffer-string))
183  (unless (zerop exit-code)
184  (error "Non-zero exit code from shell %s invoked with args %S. Output was:\n%S"
185  shell shell-args (buffer-string))))
186  (goto-char (point-min))
187  (if (re-search-forward "__RESULT\0\\(.*\\)\0__RESULT" nil t)
188  (match-string 1)
189  (error "Expected printf output from shell, but got: %S" (buffer-string))))))
190 
191 (defun exec-path-from-shell-getenvs (names)
192  "Get the environment variables with NAMES from the user's shell.
193 
194 Execute the shell according to `exec-path-from-shell-arguments'.
195 The result is a list of (NAME . VALUE) pairs."
196  (when (file-remote-p default-directory)
197  (error "You cannot run exec-path-from-shell from a remote buffer (Tramp, etc.)"))
198  (let* ((random-default (md5 (format "%s%s%s" (emacs-pid) (random) (current-time))))
199  (dollar-names (mapcar (lambda (n) (format "${%s-%s}" n random-default)) names))
200  (values (split-string (exec-path-from-shell-printf
201  (mapconcat #'identity (make-list (length names) "%s") "\\000")
202  dollar-names) "\0")))
203  (let (result)
204  (while names
205  (prog1
206  (let ((value (car values)))
207  (push (cons (car names)
208  (unless (string-equal random-default value)
209  value))
210  result))
211  (setq values (cdr values)
212  names (cdr names))))
213  result)))
214 
215 (defun exec-path-from-shell-getenv (name)
216  "Get the environment variable NAME from the user's shell.
217 
218 Execute the shell as interactive login shell, have it output the
219 variable of NAME and return this output as string."
220  (cdr (assoc name (exec-path-from-shell-getenvs (list name)))))
221 
222 (defun exec-path-from-shell-setenv (name value)
223  "Set the value of environment var NAME to VALUE.
224 Additionally, if NAME is \"PATH\" then also update the
225 variables `exec-path' and `eshell-path-env'."
226  (setenv name value)
227  (when (string-equal "PATH" name)
228  (setq exec-path (append (parse-colon-path value) (list exec-directory)))
229  ;; `eshell-path-env' is a buffer local variable, so change its default
230  ;; value.
231  (setq-default eshell-path-env value)))
232 
233 ;;;###autoload
234 (defun exec-path-from-shell-copy-envs (names)
235  "Set the environment variables with NAMES from the user's shell.
236 
237 As a special case, if the variable is $PATH, then the variables
238 `exec-path' and `eshell-path-env' are also set appropriately.
239 The result is an alist, as described by
240 `exec-path-from-shell-getenvs'."
241  (let ((pairs (exec-path-from-shell-getenvs names)))
242  (mapc (lambda (pair)
243  (exec-path-from-shell-setenv (car pair) (cdr pair)))
244  pairs)))
245 
246 ;;;###autoload
247 (defun exec-path-from-shell-copy-env (name)
248  "Set the environment variable $NAME from the user's shell.
249 
250 As a special case, if the variable is $PATH, then the variables
251 `exec-path' and `eshell-path-env' are also set appropriately.
252 Return the value of the environment variable."
253  (interactive "sCopy value of which environment variable from shell? ")
254  (cdar (exec-path-from-shell-copy-envs (list name))))
255 
256 ;;;###autoload
257 (defun exec-path-from-shell-initialize ()
258  "Initialize environment from the user's shell.
259 
260 The values of all the environment variables named in
261 `exec-path-from-shell-variables' are set from the corresponding
262 values used in the user's shell."
263  (interactive)
264  (exec-path-from-shell-copy-envs exec-path-from-shell-variables))
265 
266 
267 (provide 'exec-path-from-shell)
268 
269 ;; Local Variables:
270 ;; coding: utf-8
271 ;; indent-tabs-mode: nil
272 ;; require-final-newline: t
273 ;; checkdoc-minor-mode: t
274 ;; End:
275 
276 ;;; exec-path-from-shell.el ends here