sacha chua :: living an awesome life

2156 blog subscribers
2751 on Twitter
Subscribe!
E-mail Feed reader

Emacs and w3m: Quick searches

A number of browsers allow you to define quick searches so that you can type keywords into your address bar in order to search predetermined sites. With a little bit of code, you can do this in Emacs, too. In this project, you’ll learn how to set up your own keywords to work with w3m and browse-url, so you can use your keywords for browsing both inside and outside Emacs.

First, set up keywords by adding the following lines to your ~/.emacs. We’ll use an associative list of regular expressions and substitutions, like this:

(setq wicked/quick-search-alist
      '(("^g?:? +\\(.*\\)" . ;; Google Web
         "http://www.google.com/search?q=\\1")

        ("^g!:? +\\(.*\\)" . ;; Google Lucky
         "http://www.google.com/search?btnI=I%27m+Feeling+Lucky&q=\\1")

	("^dict:? +\\(.*\\)" . ;; Dictionary
	 "http://dictionary.reference.com/search?q=\\1")))

This will turn "g keyword1 keyword2" into a Google search for keyword1 and keyword2.

Next, define advice for the functions that open URLs. Before-type advice allows you to modify arguments before the function is run, and we’ll use that to change the URLs. To modify the URL behavior of w3m, add the following to your ~/.emacs:

(require 'cl-seq)
(defadvice w3m-goto-url (before wicked activate)
  "Use the quick searches defined in `wicked/quick-search-alist'."
  (let* ((my-url (replace-regexp-in-string
		  "^ *\\| *$" ""
		  (replace-regexp-in-string "[ \t\n]+" " " (ad-get-arg 0))))
	 (match (assoc-if
		 (lambda (a) (string-match a my-url))
		 wicked/quick-search-alist)))
    (if match
	(ad-set-arg 0 (replace-regexp-in-string
		       (car match) (cdr match) my-url)))))

This sets up your quick searches for use within w3m. To set up quick searches for use with browse-url and external browsers, add the following to your ~/.emacs:

(defadvice browse-url (before wicked activate)
  "Use the quick searches defined in `wicked/quick-search-alist'."
  (let* ((my-url (replace-regexp-in-string
		  "^ *\\| *$" ""
		  (replace-regexp-in-string "[ \t\n]+" " " (ad-get-arg 0))))
	 (match (assoc-if
		 (lambda (a) (string-match a my-url))
		 wicked/quick-search-alist)))
    (if match
	(ad-set-arg 0 (replace-regexp-in-string
		       (car match) (cdr match) my-url)))))

To try out your searches, use M-x browse-url RET g emacs RET to do a Google search for all things Emacs, and use M-x w3m-goto-url (usually bound to g) inside w3m to use the quick searches.

You can define more quick searches like this:

(add-to-list 'wicked/quick-search-alist
          '("^ew:? *?\\(.*\\)" . ;; Emacs Wiki Search
            "http://www.emacswiki.org/cgi-bin/wiki?search=\\1"))
So-soHmmGoodGreatAwesome! (No Ratings Yet)
Loading ... Loading ...
Save to - del.icio.us - Digg it - reddit - StumbleUpon -

3 Responses to “Emacs and w3m: Quick searches”

  1. What’s wrong with the quick-search feature which is already in emacs-w3m? See section 4.1 of the emacs-w3m manual and the variable w3m-search-engine-alist. It even allows to launch several searches on different search engines at once.

  2. Oh, bother. <laugh> Yes, that certainly works just fine. I guess it’s just for browse-url, then! =)

  3. [...] sacha chua :: enterprise 2.0 consultant, storyteller, geek » Emacs … [...]

Discussion Area - Leave a Comment

Please comment as you, not your organization.





On This Day...

  • 2009: New presentation: The Shy Connector! — I spent today making presentations and ginger snap cookies. A day well spent! Here’s The Shy Connector, which builds on the [...]
  • 2008: Weekly review: Week ending Aug 17, 2008 — What a hectic and fun week! It was the first week of my vacation in the Philippines, and every day was [...]
  • 2006: Back in Toronto! — Will blog more when the dust settles.
  • 2005: Post your reaction! — Discuss the SpecOps issue over at http://www.pinoytechblog.com/archives/the-strange-tale-of-project-david . Go, pinoy.tech.blog! =) (I’m not part of pinoy.tech.blog, but I have friends who [...]
  • 2005: No talent in the Philippines? Yeah, right. – rant — SpecOps Labs thinks there’s not enough IT talent in the Philippines. That’s why they had to outsource their development, they [...]
  • 2005: Too chicken to try anything new — I bought four whole chickens at the recent Price Chopper sale, trying to get into once-a-month or freezer cooking. I’ve been [...]
  • 2005: No talent in the Philippines? — SpecOps Labs thinks there’s not enough IT talent in the Philippines. What a totally bogus excuse. You have no idea how [...]
  • 2002: Computer Aided Instruction (school, education) — I think I’ll go for HTML. If animation is necessary, then I’ll use Flash. I won’t be able to reach [...]