Commit cbfe666b authored by Richard M. Stallman's avatar Richard M. Stallman
Browse files

(setenv): Rewrite. Provide a way to unset interactively.

parent dad146f9
...@@ -31,30 +31,38 @@ ...@@ -31,30 +31,38 @@
;;; Code: ;;; Code:
;;;###autoload ;;;###autoload
(defun setenv (variable &optional value) (defun setenv (variable &optional value unset)
"Set the value of the environment variable named VARIABLE to VALUE. "Set the value of the environment variable named VARIABLE to VALUE.
VARIABLE should be a string. VALUE is optional; if not provided or is VARIABLE should be a string. VALUE is optional; if not provided or is
`nil', the environment variable VARIABLE will be removed. `nil', the environment variable VARIABLE will be removed.
Interactively, a prefix argument means to unset the variable.
This function works by modifying `process-environment'." This function works by modifying `process-environment'."
(interactive "sSet environment variable: \nsSet %s to value: ") (interactive
(if current-prefix-arg
(list (read-string "Clear environment variable: ") nil t)
(let ((var (read-string "Set environment variable: ")))
(list var (read-string (format "Set %s to value: " var))))))
(if unset (setq value nil))
(if (string-match "=" variable) (if (string-match "=" variable)
(error "Environment variable name `%s' contains `='" variable) (error "Environment variable name `%s' contains `='" variable)
(let ((pattern (concat "\\`" (regexp-quote (concat variable "=")))) (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
(case-fold-search nil) (case-fold-search nil)
(scan process-environment)) (scan process-environment)
(if scan found)
(while scan (while scan
(cond (cond ((string-match pattern (car scan))
((string-match pattern (car scan)) (setq found t)
(if (eq nil value) (if (eq nil value)
(setq process-environment (delq (car scan) process-environment)) (setq process-environment (delq (car scan) process-environment))
(setcar scan (concat variable "=" value))) (setcar scan (concat variable "=" value)))
(setq scan nil)) (setq scan nil)))
((null (setq scan (cdr scan))) (setq scan (cdr scan)))
(or found
(if value
(setq process-environment (setq process-environment
(cons (concat variable "=" value) process-environment))))) (cons (concat variable "=" value)
(setq process-environment process-environment)))))))
(cons (concat variable "=" value) process-environment))))))
(provide 'env) (provide 'env)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment