Friday, May 2, 2008

Switching Browsers in Emacs

Recently, I have met one dilemma in Emacs. Since I'm using Emacs for my blog writing, Firefox is used for previewing. Also as an Common Lisp user, I use emacs-w3m to read HyperSpec. Here comes the problem: I can only set one browser for browse-url-browser-function. How can I suit my requirements simultaneously?

Fortunately, Emacs is highly extensible. I just write one simple interactive function toggle-browser, which can toggle between Firefox and emacs-w3m when pressing M-x toggle-browser. Following is the code:

(defun toggle-browser ()
  "Toggle browser between Firefox and emacs-w3m."
  (setq browse-url-browser-function 
        (if (eql browse-url-browser-function 'browse-url-firefox)
  (message "%s" browse-url-browser-function))

Update: please look at the 1st comment from pedro for a better solution.


  1. Or you can do something like:

    (setq browse-url-browser-function '(("hyperspec" . w3m-browse-url)
    ("weitz" . w3m-browse-url)
    ("." . browse-url-firefox)))

  2. @pedro: your solution is better, thanks!