Emacs w3m: Open pages in external browsers

| emacs, wickedcoolemacs

Sometimes w3m is not enough. To make it easier to open the current page in a browser such as Mozilla Firefox, add the following to your ~/.emacs:

(defun wicked/w3m-open-current-page-in-firefox ()
  "Open the current URL in Mozilla Firefox."
  (interactive)
  (browse-url-firefox w3m-current-url)) ;; (1)

(defun wicked/w3m-open-link-or-image-in-firefox ()
  "Open the current link or image in Firefox."
  (interactive)
  (browse-url-firefox (or (w3m-anchor) ;; (2)
                          (w3m-image)))) ;; (3)

This defines a function that uses the current URL being browsed(1) and another function that takes the URL of the link at point(2). If no link is found, it takes the URL of the image at point(3).

You can use other browse-url functions instead of browse-url-firefox. For example, replacing browse-url-firefox with browse-url-kde will open the page, link, or image in Konqueror, KDE’s web browser.

I like binding f to the function that opens the current URL in Mozilla Firefox and F to the function that opens the current link or image in Mozilla Firefox. To do the same, add the following to your ~/.emacs:

(eval-after-load 'w3m
  (progn 
    (define-key w3m-mode-map "f" 'wicked/w3m-open-current-page-in-firefox)
    (define-key w3m-mode-map "F" 'wicked/w3m-open-link-or-image-in-firefox)))


This is part of the book that I’m writing about Emacs, which will be published by No Starch Press if I manage to get it together in time.

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

5 comments

This might be the place to ask this question.
I want to open a javascript file (or similar) that resides on the Internet, from within Emacs.
How do I do that? I don't want to view a page (like a browser), I want to view the source.
Yes, I can cut and paste and edit html lines and use the browser to "view source", but I want Emacs to do ALL the work. Thanks.

Yet one more way to do it is to attach external browser to S-mouse-2

(eval-after-load "w3m"
(define-key w3m-link-map [S-mouse-2]
(lambda (event)
(interactive "e")
(mouse-set-point event)
(w3m-external-view-this-url))))

Oh, nifty! Thanks for showing me even better ways to do things. =)

I had to modify the above with a little patch:

-(define-key w3m-link-map [S-mouse-2]
+'(define-key w3m-link-map [S-mouse-2]

I'm getting an error in evaluating the "(eval-after-load 'w3m " ... "define-key" code:

Debugger entered--Lisp error: (void-variable w3m-mode-map)
(define-key w3m-mode-map "f" (quote my-w3m-open-current-page-in-firefox))
(progn (define-key w3m-mode-map "f" (quote my-w3m-open-current-page-in-firefox)) (define-key w3m-mode-map "F" (quote my-w3m-open-link-or-image-in-firefox)))
(eval-after-load (quote w3m) (progn (define-key w3m-mode-map "f" ...) (define-key w3m-mode-map "F" ...)))
eval((eval-after-load (quote w3m) (progn (define-key w3m-mode-map "f" ...) (define-key w3m-mode-map "F" ...))))
eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp nil nil)

Though the two functions to open the current page and link in a new page work fine if i run them manual via M-x