Emacs and W3M: Toggling between work and the Web

| emacs, wickedcoolemacs

Here’s a handy shortcut that toggles between the W3M web browser and other buffers you’re working on. I use it to quickly switch between code and documentation (or your favorite timewasting site, as it also makes a handy boss key).

Define the function by adding the following code to your ~/.emacs:

(defun wicked/toggle-w3m ()
  "Switch to a w3m buffer or return to the previous buffer."
  (interactive)
  (if (derived-mode-p 'w3m-mode)
      ;; Currently in a w3m buffer
      ;; Bury buffers until you reach a non-w3m one
      (while (derived-mode-p 'w3m-mode)
	(bury-buffer))
    ;; Not in w3m
    ;; Find the first w3m buffer
    (let ((list (buffer-list)))
      (while list
	(if (with-current-buffer (car list)
	      (derived-mode-p 'w3m-mode))
	    (progn
	      (switch-to-buffer (car list))
	      (setq list nil))
	  (setq list (cdr list))))
      (unless (derived-mode-p 'w3m-mode)
	(call-interactively 'w3m)))))

Then bind it to a shortcut key sequence (F7 F7 in this example) by adding the following code to your ~/.emacs:

(global-set-key (kbd " ") 'wicked/toggle-w3m)

You can then use F7 F7 to switch back and forth between your web browser and whatever else you’re working on.

You can view 6 comments or e-mail me at sacha@sachachua.com.

6 comments

Gabriel Saldana

2008-08-27T21:37:45Z

Is there a way to set w3m as the default browser on Emacs? Most times i click on a link on my mail (gnus) or visit the php manual from php-mode, firefox gets launched instead.

Niels Giesen

2008-08-28T07:25:05Z

No need to replicate built-in functionality:

w3m-close-window buries all w3m windows
w3m starts w3m or switches to last-visited w3m window

so doesn't

(defun wicked/toggle-w3m-2 ()
"Switch to a w3m buffer or return to the previous buffer."
(interactive)
(if (derived-mode-p 'w3m-mode)
(call-interactively 'w3m-close-window)
(call-interactively 'w3m)))


do the same thing?

<laugh> You can tell that I haven't read the source code for w3m-el yet. I'll do that later, then - it's nice to know all sorts of good stuff is in there!

Yes, that code snippet will work beautifully.

Gabriel: Set your browse-url-browser-function to w3m-browse-url. That should do the trick... =)

Mhh,
I do not see the use of this.
Using iswitchbuffers, I just switch between windows by C-x b.
So, I have the same behaviour in every buffer, not just
w3m-el. -> I use it more often -> I develop some kind of muscle
memory. What is the advantage of such a "specialized" keybinding
to using the usual Buffer Switch?

-ky

P.S.: 'Add comment' does not seem to work in emacs-w3m :(
Had to come back with conkeror.

A dumb question: how to install w3m?
I did a quick search on the web, it seems that there are two related packages, w3m and emacs-w3m, which one should I install?

Thanks for all the great articles about emacs. I have certainly learned a lot!

emacs-w3m is an Emacs Lisp interface for the w3m browser. Install them both. =)