Bill Clementson's Blog

Bits and pieces (mostly Lisp-related) that I collect from the ether.

February 2007
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28
Jan  Mar

SLIME Tips and Techniques - Part 5 (Alternative Browsers in Emacs)

Thursday, February 8, 2007

One of the things that I really like about Emacs is the flexibility it gives you to do things exactly as you want them done. For example, in my earlier post about accessing library documentation in SLIME, I stated that "I use w3m for browsing documentation in SLIME. I generally use Firefox for normal Internet browsing but prefer to use an Emacs-based browser when working with Lisp code in SLIME as it's easier to copy/paste things and I don't have to leave Emacs to view the docs.". I subsequently received an email asking me about this. I should have mentioned that the browse-url-browser-function variable (that lets one specify which browser will be used) has a couple of different ways it can be used. So, although you can choose a single, global default browser to be used (in this example, I'm using w3m as the default browser):

(setq browse-url-browser-function 'w3m-browse-url)
you can also specify different browsers to be used by storing a list of pairs (REGEXP . FUNCTION) in the browse-url-browser-function variable. In this case the function called will be the one associated with the first REGEXP which matches the current URL. So, you can have:
(setq browse-url-browser-function '(("hyperspec" . w3m-browse-url)
				    ("weitz" . w3m-browse-url)
				    ("." . browse-url-default-macosx-browser)))
in which case any URL's that contain either the word "hyperspec" or the word "weitz" will be opened with w3m and everything else will be opened with the default browser on my Mac (which, in my case, happens to be Firefox). So, if I'm using SLIME, I can ensure that documentation pages are opened in w3m in Emacs rather than in an external browser window while any other URL is opened in Firefox.

However, the fact that you're specifying a function rather than an external executable means that you have the flexibility to do a lot more than just call an external browser. For example, in an email to the SLIME developer's list, Håkon Alstadheim provided this example to illustrate how one could change the default behaviour of w3m so that it opened in a new window:
(require 'w3m)
(defun w3m-browse-url-other-window (url &optional newwin) (interactive (browse-url-interactive-arg "w3m URL: ")) (let ((pop-up-frames nil)) (switch-to-buffer-other-window (w3m-get-buffer-create "*w3m*")) (w3m-browse-url url)))
(setq browse-url-browser-function (list (cons "^ftp:/.*" (lambda (url &optional nf) (call-interactively #'find-file-at-point url))) (cons "." #'w3m-browse-url-other-window)))
Notice also how the above example specifies that the find-file-at-point function should be used when any FTP link is accessed. The BrowseURL page on the Emacs Wiki has some more examples of custom browse-url-browser-function functions if you're interested.

Ah, the joys of Emacs! :-)

emacs Copyright © 2007 by Bill Clementson