Emacs Lisp: browse-url Function to View URL in a Web Browser

Perm url with updates: http://xahlee.org/emacs/elisp_browse_url.html

Emacs Lisp: browse-url Function to View URL in a Web Browser

Xah Lee, 2011-05-03

In emacs, there's a function “browse-url”. It sends a url to a web browser. You can use it like this:

(browse-url "http://xahlee.org/")

The function “browse-url” is defined in 〔browse-url.el〕, which is bundled with emacs. It is used by for example “html-mode”. In that mode, you can press 【Ctrl+c Ctrl+v】 and it'll call “browse-url-of-buffer”, and view the current html file in a browser.

The “browse-url” function is very useful in your own lisp program. You can define a function with a key, so that pressing a button or clicking on url lets you view that url. For examples of how it is used, see: Emacs: Perl PHP Dictionary Wikipedia Google … Reference lookup.

Sending URL to Firefox Browser

Besides, “browse-url” and “browse-url-of-buffer”, there are several others:

  • browse-url-firefox
  • browse-url-default-windows-browser
  • browse-url-default-macosx-browser
  • browse-url-kde

(See 〔browse-url.el〕 for more.) These will use a specific web browser. However, on my Windows machine, by default the “browse-url-firefox” doesn't work because emacs doesn't know its path.

To make it work, you'll need to add Firefox executable path in emacs's “'exec-path” variable, like this:

(add-to-list 'exec-path "c:/Program Files (x86)/Mozilla Firefox/")

For example, i use multiple web browsers. Google Chrome for sites i have a account, especially Google sites. Firefox for all my web dev, usually with Flash and javascript off. Internet Explorer 9 for rest, and sometimes also Safari and Opera.

In emacs, sometimes i want to sent url to a specific browser. Here's a function i defined:

(defun browse-url-of-buffer-with-firefox ()
  "Same as `browse-url-of-buffer' but using Firefox."
  (interactive)
  (let ()
    (require 'browse-url)
    ;; for the function browse-url-firefox to work, you need to have the firefox path in exec-path. e.g.:
    ;; (add-to-list 'exec-path "c:/Program Files (x86)/Mozilla Firefox/")
    (browse-url-firefox (concat "file:///" buffer-file-name))
    ))

Alternative: Launching Browser by Calling Browser's Shell Command

You can also launch a browser by making a shell call. For example:

(defun browse-url-of-buffer-with-firefox ()
  "Same as `browse-url-of-buffer' but using Firefox.
You need Firefox's in the path environment variable within emacs.
e.g.
 (setenv \"PATH\" (concat \"C:/Program Files (x86)/Mozilla Firefox/\" \";\" (getenv \"PATH\") ) )"
  (interactive)
  (let ()
    (shell-command (concat "firefox file:///" buffer-file-name))
    ))

You have to make sure that the path of firefox e.g. 〔C:/Program Files (x86)/Mozilla Firefox/〕 is in emacs's “PATH” variable. Here's a example of how to add it:

; example of setting env var named “path” by appending a new path to existing path
(setenv "PATH" (concat "C:/Program Files (x86)/Mozilla Firefox/" ";" (getenv "PATH") ) )

For more detail about environment variables with emacs and Windows, see: Emacs and Microsoft Windows Tips.

Which Solution is Better?

Of these 2 solutions of launching Firefox, the one with a shell call works best for me. I have problem using the solution provided by 〔browse-url.el〕. Here's the detail.

I have my mouse setup so that when i hover the pointer over a window, that window comes to the front. (See: Windows Auto Raise & Single Click to Open File.) This is a super convenient setup. It saves you many clicks when switching windows. I highly recocommended it.

However, when using the 〔browse-url.el〕 solution, when i view a url, it also for some reason moves the mouse point, so that emacs will switch me to the browser but then some other program behind it will come to the front. Very annoying. I'm not sure why emacs does that. Looking at the code of “browse-url-firefox”, it's some 49 lines, with lots of variables controlling the behavior, such as controlling whether to open in a new window or tab. It seems overkill.

With shell call, the solution is very simple. Emacs simply makes a shell call and the shell launchs the browser.

Popular posts from this blog

Browser User Agent Strings 2012

11 Years of Writing About Emacs

does md5 creates more randomness?