Installing emacs-w3m is pretty easy: I followed the instructions here to check out the sources from CVS and added them to my load-path. This, as well as the customizations in this blog, were enough to get it set up. As a note: all of the customizations here came from this emacs-wiki page or a page it links to; you may want to see if there is anything else that you’d like on those pages. If you don’t already have the w3m browser, you’ll need to install it. On Ubuntu, you can just do ’sudo apt-get install w3m’.
(setq w3m-session-file "~/.emacs.d/.w3m-session") (setq w3m-session-autosave t) (setq w3m-session-load-last-sessions t) (setq w3m-use-cookies t) (setq browse-url-browser-function 'w3m-browse-url) (setq browse-url-new-window-flag t)nn
(require 'w3m) (require 'w3m-session) (require 'w3m-cookie)
These are the three w3m libraries that I require. w3m is just for standard browing functionality, w3m-session is for the session-saving mentioned above, and w3m-cookie allows cookies to be used when browsing. There are a lot more w3m libraries you can check out, but for the minimal usage I am planning on using it for this is quite enough.
(defun my-w3m-rename-buffer (url) "Renames the current buffer to be the current URL" (rename-buffer url t)) (add-hook 'w3m-display-hook 'my-w3m-rename-buffer) (add-hook 'w3m-display-hook (lambda (url) (let ((buffer-read-only nil)) (delete-trailing-whitespace))))
my-w3m-rename-buffer will rename the current buffer to that of it’s url. Since w3m by default uses names like ‘*w3m*’, ‘*w3m<2>*, and so on for buffer names, this is incredibly useful. Adding the function to w3m-display-hook forces all w3m buffers to have their URLs as their name. The second hook deletes trailing whitespace from w3m buffers. In a lot of cases, trailing whitespace is added, which makes it harder to navigate to where you expect when using C-e; this removes all of that.
(defun w3m-browse-current-buffer () "Look at the current buffer as rendered by w3m." (interactive) (let ((filename (concat (make-temp-file "w3m-") ".html"))) (unwind-protect (progn (write-region (point-min) (point-max) filename) (w3m-find-file filename)) (delete-file filename))))
In case I have a HTML file open in a buffer I’m editing that I want to view in w3m, I have this function. It will open up a new w3m buffer viewing the current buffer. I haven’t had occasion to use it yet, but I saw it on emacs-wiki and it seemed like something that would eventually be useful.
(unless window-system (setq browse-url-browser-function 'w3m-browse-url) (setq browse-url-new-window-flag t))
This is what sets Emacs to use w3m instead of Firefox. I only evaluate it when window-system is nil; if it is ‘x’, or anything else, I know I have a graphical display and can thus use Firefox. I change the function to use when browsing urls to w3m-browse-url, which will open the url up in w3m. The other option forces w3m to create a new buffer whenever a URL is browsed, instead of using old buffers.