Add emacs-server-import-environment.el

This commit is contained in:
2026-04-08 14:16:36 -07:00
parent 0215834891
commit aef3ccc5c6
2 changed files with 43 additions and 12 deletions

View File

@@ -0,0 +1,43 @@
#!/usr/bin/env -S emacs -Q --script
;;; -*- lexical-binding: t -*-
(require 'cl-lib)
(require 'server)
(defun append-to-path (value)
"Split VALUE and append to PATH."
(let ((to-add (split-string value ":" t nil)))
(server-eval-at "server"
`(dolist (file ',to-add)
(add-to-list 'exec-path file)))))
(dolist (var argv)
(let ((sep (cl-position ?= var :test #'eql)))
(cond
((string-prefix-p "-" var)
;; unset
(when (equal var "-PATH")
(message "Refusing to unset PATH")
(kill-emacs 1))
(server-eval-at "server"
`(setenv ,(substring var 1) nil)))
(sep
(let ((name (substring var 0 sep))
(value (substring var (1+ sep))))
(if (equal name "PATH")
(append-to-path value)
;; append to PATH, not set
;; set to passed value
(server-eval-at "server"
`(setenv ,name ,value)))))
(t
(if (equal var "PATH")
;; copy form and append to path
(append-to-path (getenv var))
;; copy from our evnironment
(server-eval-at "server"
`(setenv ,var ,(getenv var))))))))
;; Local Variables:
;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
;; End: