Emacs 怎么使用代理

make-network-process 并不使用 process-environment,而是使用 url-proxy-services

Emacs 发起的网络请求基于 make-network-process 没错,但如果设置了 url-proxy-services,那么 make-network-process 则是创建与代理服务器的连接,通过代理服务器转发请求。

以下代码分别经由代理访问 google 和不经代理访问 baidu:

(let ((adfn (lambda (&rest args) (message "==> make-network-process:\n%S" args))))
  (unwind-protect
      (with-proxy
        (advice-add 'make-network-process :before adfn)
        (url-retrieve-synchronously "https://www.google.com"))
    (advice-remove 'make-network-process adfn)))

;; ==> make-network-process:
;; (:name "www.google.com" :buffer #<buffer *url-http-temp*> :host "127.0.0.1"
;; :service 7890 :nowait (:nowait t) :tls-parameters nil :coding nil)

(let ((adfn (lambda (&rest args) (message "==> make-network-process:\n%S" args))))
  (unwind-protect
      (progn
        (advice-add 'make-network-process :before adfn)
        (url-retrieve-synchronously "https://www.baidu.com"))
    (advice-remove 'make-network-process adfn)))

;; ==> make-network-process:
;; (:name "www.baidu.com" :buffer #<buffer *url-http-temp*> :host
;; "www.baidu.com" :service 443 :nowait nil :tls-parameters nil :coding nil)

可以了解一下《 with-proxy: 在局部范围内使用代理 》。