Mercurial > core / emacs/lib/exec-path-from-shell.el
changeset 62: |
e310b68526b9 |
author: |
ellis <ellis@rwest.io> |
date: |
Wed, 29 Nov 2023 18:59:15 -0500 |
permissions: |
-rw-r--r-- |
description: |
elisp and lisp
needed to add exec-path-from-shell.el for SSH env variables - also needed to run ssh-agent.service and use systemd --user import-environment SSH_* |
1 ;;; exec-path-from-shell.el --- Get environment variables such as $PATH from the shell -*- lexical-binding: t -*- 3 ;; Copyright (C) 2012-2014 Steve Purcell 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")) 11 ;; This file is not part of GNU Emacs. 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. 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. 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/>. 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 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. 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. 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. 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'. 51 ;; ELPA packages are available on Marmalade and MELPA. Alternatively, 52 ;; place this file on a directory in your `load-path', and explicitly 57 ;; (require 'exec-path-from-shell) ;; if not using the ELPA package 58 ;; (exec-path-from-shell-initialize) 60 ;; Customize `exec-path-from-shell-variables' to modify the list of 61 ;; variables imported. 63 ;; If you use your Emacs config on other platforms, you can instead 64 ;; make initialization conditional as follows: 66 ;; (when (memq window-system '(mac ns)) 67 ;; (exec-path-from-shell-initialize)) 69 ;; Alternatively, you can use `exec-path-from-shell-copy-envs' or 70 ;; `exec-path-from-shell-copy-env' directly, e.g. 72 ;; (exec-path-from-shell-copy-env "PYTHONPATH") 76 ;; Satisfy the byte compiler 77 (eval-when-compile (require 'eshell)) 80 (defgroup exec-path-from-shell nil 81 "Make Emacs use shell-defined values for $PATH etc." 82 :prefix "exec-path-from-shell-" 85 (defcustom exec-path-from-shell-variables 87 "List of environment variables which are copied from the shell." 88 :type '(repeat (string :tag "Environment variable")) 89 :group 'exec-path-from-shell) 91 (defcustom exec-path-from-shell-warn-duration-millis 500 92 "Print a warning if shell execution takes longer than this many milliseconds." 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." 100 (file :tag "Shell executable") 101 (const :tag "Use `shell-file-name' or $SHELL" nil)) 102 :group 'exec-path-from-shell) 104 (defvar exec-path-from-shell-debug nil 105 "Display debug info when non-nil.") 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) "\"")) 111 (defun exec-path-from-shell--shell () 112 "Return the shell to use. 113 See documentation for `exec-path-from-shell-shell-name'." 115 exec-path-from-shell-shell-name 118 (error "SHELL environment variable is unset"))) 120 (defcustom exec-path-from-shell-arguments 121 (let ((shell (exec-path-from-shell--shell))) 122 (if (string-match-p "t?csh$" shell) 124 (if (string-match-p "fish" shell) 127 "Additional arguments to pass to the shell. 129 The default value denotes an interactive login shell." 130 :type '(repeat (string :tag "Shell argument")) 131 :group 'exec-path-from-shell) 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))) 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))) 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))) 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))))))) 155 (defun exec-path-from-shell-printf (str &optional args) 156 "Return the result of printing STR in the user's shell. 158 Executes the shell as interactive login shell. 160 STR is inserted literally in a single-quoted argument to printf, 161 and may therefore contain backslashed escape sequences understood 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")) 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 175 (if (exec-path-from-shell--standard-shell-p shell) 177 (concat "sh -c " (shell-quote-argument printf-command))))))) 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) 189 (error "Expected printf output from shell, but got: %S" (buffer-string)))))) 191 (defun exec-path-from-shell-getenvs (names) 192 "Get the environment variables with NAMES from the user's shell. 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"))) 206 (let ((value (car values))) 207 (push (cons (car names) 208 (unless (string-equal random-default value) 211 (setq values (cdr values) 215 (defun exec-path-from-shell-getenv (name) 216 "Get the environment variable NAME from the user's shell. 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))))) 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'." 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 231 (setq-default eshell-path-env value))) 234 (defun exec-path-from-shell-copy-envs (names) 235 "Set the environment variables with NAMES from the user's shell. 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))) 243 (exec-path-from-shell-setenv (car pair) (cdr pair))) 247 (defun exec-path-from-shell-copy-env (name) 248 "Set the environment variable $NAME from the user's shell. 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)))) 257 (defun exec-path-from-shell-initialize () 258 "Initialize environment from the user's shell. 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." 264 (exec-path-from-shell-copy-envs exec-path-from-shell-variables)) 267 (provide 'exec-path-from-shell) 271 ;; indent-tabs-mode: nil 272 ;; require-final-newline: t 273 ;; checkdoc-minor-mode: t 276 ;;; exec-path-from-shell.el ends here