Emacs and W3M: Toggling between work and the Web

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.

  • http://blog.nethazard.net Gabriel Saldana

    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.

  • http://niels.kicks-ass.org Niels Giesen

    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?

  • http://sachachua.com Sacha Chua

    <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… =)

  • ky

    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.

  • Ning

    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!

    • http://sachachua.com Sacha Chua

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