I like the w3m
browsing experience well enough, and reading
technical pages in Emacs is a good workflow. Except that I need to
hide lots of useless text for various web pages.
Not sure if I want to use the w3m
or the 24.4-specific eww
, so
now I bounce between them. To use w3m, make sure it is installed:
brew install w3m
For v24.4, I’m intrigued with EWW, but w3m works really well. The one I use is the one I’ve loaded …
(use-package eww
:commands eww eww-follow-link
:init
(setq browse-url-browser-function 'eww-browse-url)
(setq eww-search-prefix "http://www.google.com/search?q=")
(defun eww-wiki (text)
"Function used to search wikipedia for the given text."
(interactive (list (read-string "Wiki for: ")))
(eww (format "https://en.m.wikipedia.org/wiki/Special:Search?search=%s"
(url-encode-url text))))
:config ;; clean up the rendered display:
(add-hook 'eww-after-render-hook 'ha/eww-rerender-pages)
(add-hook 'eww-mode 'ace-link-mode)
:bind (("C-c w w" . eww)
("C-c w i" . eww-wiki)
("C-c w l" . eww-follow-link)))
The following set up functions from emacs-w3m:
(use-package w3m
:commands w3m-goto-url w3m-search
:init
(setq browse-url-browser-function 'w3m-browse-url)
(setq w3m-use-cookies t)
;; clean up the w3m buffers:
(add-hook 'w3m-display-functions 'w3m-hide-stuff)
(add-hook 'w3m-mode 'ace-link-mode)
(global-set-key (kbd "C-c w w") 'w3m-goto-url)
(global-set-key (kbd "C-c w l") 'browse-url-at-point)
(global-set-key (kbd "C-c w g") 'w3m-search)
:config
(define-key w3m-mode-map (kbd "&") 'w3m-view-url-with-external-browser))
While browsing, remember the following:
- TAB to jump from link to link.
- RETURN to follow a link
- SPACE to move down the page
- b to move up the page
- B to move back in the history
- M to open the URL in Firefox
- I to open the image if it didn’t show up correctly
- c to copy the URL of the current page in the kill ring.
- u to copy the URL of the link in the kill ring.
- a to bookmark this page
- v to look at the bookmarks
- s to look through the page history for this session.
Need to be able to switch and have a link in an org-mode
file show
up in the default, graphical browser:
(defun ha-switch-default-browser ()
"Switches the default browser between the internal and external web browser."
(interactive)
;; | Variable | Function
(if (equal browse-url-browser-function 'browse-url-default-browser)
(if (fboundp 'w3m)
(setq browse-url-browser-function 'w3m-browse-url)
(setq browse-url-browser-function 'eww-browse-url))
(setq browse-url-browser-function 'browse-url-default-browser))
;; Now we need to display the current setting. The variables are
;; pretty typical and have the goodies, but I just need to get rid
;; of the word "url" or "browser", and the results are pretty close:
(cl-flet ((remove-bad-parts (l)
(-filter (lambda (s) (pcase s
("url" nil)
("browse" nil)
("browser" nil)
(_ t))) l)))
(message "Browser set to: %s"
(-> (symbol-name browse-url-browser-function)
(split-string "-")
remove-bad-parts
car))))
(global-set-key (kbd "C-c w d") 'ha-switch-default-browser)
Navigating some sites in a text browser is a bit painful, but we could have some functions that either move the cursor passed the header to the start of the content, or actually remove some of the content.
Don’t need to actually remove stuff when search in Google, as I really just need to jump ahead and skip the header:
(defun w3m-skip-in-google ()
"For a Google Search, skip to the first result."
(beginning-of-buffer)
(search-forward-regexp "[0-9, ]+ results")
(forward-line 2)
(recenter-top-bottom 0))
Without a clear enough label, searching for the start of content will always be fragile. We’ll look for the start of the first column.
(defun w3m-skip-in-stackoverflow ()
(beginning-of-buffer)
(search-forward-regexp "^ ")
(forward-line -2)
(recenter-top-bottom 0))
Stack Overflow has a lot of text that isn’t helpful to someone in a text-based browser, so I would like to remove the voting and other parts. Not sure how to do it, since the columns are of variable width.
The clojuredocs.org website has a big header, but doesn’t include a link to jump to the content, so let’s try to figure that out for most function definitions:
(defun w3m-skip-in-clojuredocs()
"When viewing the Clojuredocs, we can skip to the meat of the
function description by looking for the label, ‘Available since’,
and finding the function name just before that."
(beginning-of-buffer)
(search-forward-regexp "Available since")
(forward-line -4)
(recenter-top-bottom 0))
Add a hook to the w3m-display-functions
to match the URL to see
which function we need to call:
(defun w3m-hide-stuff (url)
"Call screen cleaning functions for the W3M based on the URL."
(interactive)
(cond ((string-match "google\.com/search" url) (w3m-skip-in-google))
((string-match "clojuredocs.org" url) (w3m-skip-in-clojuredocs))
((string-match "stackoverflow.com" url) (w3m-skip-in-stackoverflow))
))
Ace-Link already supports EWW.
(use-package ace-link
:ensure t
:config
(ace-link-setup-default))
Make sure that we can simply require
this library.
(provide 'init-browser)
Before you can build this on a new system, make sure that you put
the cursor over any of these properties, and hit: C-c C-c