Toggle http proxy in emacs-w3m

  |   Source

emacs-w3m is a browser embedded in Emacs. It can handle all my basic web surfing needs.

For some reason, I need switch my http proxy frequently when accessing the internet in China. So I need figure out a easy way doing this.

It turns out all I need to do is to set/unset the environment variable "http_proxy" in elisp code. Restarting the w3m session after change the environment variable is not needed at all.

Here is my code:


(defun toggle-env-http-proxy ()
  "set/unset the environment variable http_proxy which w3m uses"
  (interactive)
  (let ((proxy "http://127.0.0.1:8000"))
    (if (string= (getenv "http_proxy") proxy)
        ;; clear the proxy
        (progn
          (setenv "http_proxy" "")
          (message "env http_proxy is empty now")
          )
      ;; set the proxy
      (setenv "http_proxy" proxy)
      (message "env http_proxy is %s now" proxy)
        )
    ))

Comments powered by Disqus