Skip to content

Commit 401c43b

Browse files
committed
feat(core): better implementation of proxies
1 parent 87e6c5f commit 401c43b

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

core/me-lib.el

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,18 +1777,44 @@ it forget them only when we are sure they don't exist."
17771777
;;; Proxy
17781778
;;; =====
17791779

1780-
(defun minemacs-enable-proxy ()
1781-
"Set *_proxy Linux environment variables from `minemacs-proxies'."
1782-
(interactive)
1783-
(dolist (prox minemacs-proxies)
1784-
(let ((var (format "%s_proxy" (car prox))))
1785-
(+log! "Set %S to %S" var (cdr prox))
1786-
(setenv var (cdr prox)))))
1780+
(defun minemacs-get-enabled-proxies ()
1781+
"Get a list of enabled proxies."
1782+
(cl-loop
1783+
for prox in '("no" "ftp" "http" "https")
1784+
append (cl-loop for fn in '(downcase upcase)
1785+
collect (cons (funcall fn prox) (getenv (funcall fn (format "%s_proxy" prox)))))))
1786+
1787+
(defun minemacs-set-enabled-proxies (proxies)
1788+
"Set PROXIES."
1789+
(cl-loop
1790+
for prox in proxies
1791+
do (cl-loop
1792+
for fn in '(upcase downcase)
1793+
do (cons (funcall fn (car prox)) (setenv (funcall fn (format "%s_proxy" (car prox))) (cdr prox))))))
1794+
1795+
(defun minemacs-enable-proxy (proxies)
1796+
"Set *_proxy Linux environment variables from PROXIES."
1797+
(interactive (list minemacs-proxies))
1798+
(minemacs-set-enabled-proxies proxies))
17871799

17881800
(defun minemacs-disable-proxy ()
17891801
"Unset *_proxy Linux environment variables."
17901802
(interactive)
1791-
(mapc #'setenv (mapcar (apply-partially #'format "%s_proxy") (mapcar #'car minemacs-proxies))))
1803+
(minemacs-set-enabled-proxies (mapcar (lambda (a) (list (car a))) minemacs-proxies)))
1804+
1805+
(defmacro +with-proxies (&rest body)
1806+
"Execute BODY with proxies enabled from `minemacs-proxies'."
1807+
`(progn
1808+
(minemacs-enable-proxy minemacs-proxies)
1809+
,@body
1810+
(minemacs-disable-proxy)))
1811+
1812+
(defmacro +with-no-proxies (&rest body)
1813+
"Execute BODY with proxies disabled."
1814+
`(let ((old-proxies (minemacs-get-enabled-proxies)))
1815+
(minemacs-enable-proxy minemacs-proxies)
1816+
,@body
1817+
(minemacs-disable-proxy)))
17921818

17931819

17941820

init.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167

168168
;; When `minemacs-proxies' is set in "early-init.el" or in "init-tweaks.el",
169169
;; `minemacs-enable-proxy' will set the environment variables accordingly.
170-
(minemacs-enable-proxy)
170+
(minemacs-enable-proxy minemacs-proxies)
171171

172172
;; HACK: Load the environment variables saved from shell using `+env-save' to
173173
;; `+env-file'. `+env-save' saves all environment variables except these matched

0 commit comments

Comments
 (0)