August 18, 2008

Bulk view

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"))

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 just packed with things to do with friends and family.

On August 11 (Monday), we arrived in Manila. The Philippine Airlines flight we arrived on was full of boisterous Filipinos who joked with the flight attendants, sang way off-key, and cheered as the plane touched the tarmac, but I didn’t mind losing sleep on that red-eye flight. I was home! And I was awake enough to try the dress I was going to wear for Diane’s wedding and make other arrangements…

On August 12, I celebrated my birthday with W- and my parents. My dad took W- and me to Raon (the electronics district) and R. Hidalgo (the street of photography shops), and he gave me a flash for my birthday. I look forward to using it to light stuff! In the afternoon, my mom took W- and me to Ateneo de Manila University and Philippine Science High School, so that W- could get an idea of what my campus years were like. We also passed by the University of Philippines, where my mom told W- stories about her campus years. In the evening, I snuck into the Benilde commencement ceremony and exhibit for the graduates of their photography diploma course, and I listened to my dad and my sister give the short commencement speech. Cool!

On August 13, my dad gave W- and me a few shooting lessons. We discovered that the flash I have can freeze water drops in action, and we also practiced lighting small objects. I took a picture of the Scrabble game that W- and I had played on the flight home. It was definitely worth recording. =) After our shooting lessons, my mom took us to Alabang, where we had a tea party with several of my godparents. We listened to hilarious stories about avoiding traffic tickets, taking pictures in restricted areas, and other hijinks that my parents and godparents had done.

Practically all of August 14 was taken up by Diane’s wedding. She was the first of my best friends to get married, so we were all excited about that. I couldn’t help going "squee!" every few minutes. I loved the video montage that Mario had put together: childhood pictures, high school pictures, all the way up to their engagement pictures. The wedding photographers delighted us by showing a video montage of Di’s wedding pictures during the reception itself – that was fast work!

We spent most of August 15 working at home. I was aghast to find out that a bug in my code had resulted in the deletion of users on our production system ( =( !), and that the project manager and the IT architect had to recreate many accounts by hand. That further emphasized the importance of test-first development and well-written tests. Ouch. I’ve paid the tuition for that mistake, now I just have to collect the paycheck; I can’t undo that mistake, but I can learn from it and become a better developer. To avoid that mistake in the future, I will write more tests using the Simpletest framework, and I’ll ask someone else to review the tests with me.

I perked up a bit more in the afternoon, when my mom took W- and me to a yoga class taught by one of her friends. It was more challenging than the yoga classes we normally attend, and it was a good break. The yoga teacher was so impressed that both W- and I do yoga, and waxed lyrical about how beautiful it was to see couples practice yoga together.

On August 16, I hosted a get-together for my friends. We spent the entire afternoon laughing at the best of the Internet memes: a pastiche of websites and Youtube videos that we or the others had missed. In the evening, we watched Shaun of the Dead and Hot Fuzz, which both left us practically crying from laughter (and maybe a little grossed out). It was so nice to laugh with my friends again!

We spent most of August 17 at a photography summit, where we learned about taking pictures of people. I’m looking forward to applying those lessons. I’d like to learn how to take good pictures of my friends! My mom also showed W- the rows and rows of stalls selling pearls at Greenhills. It was all quite overwhelming, and I was glad to get back to the photography summit.

So that was what last week was like. I hope next week will be a little calmer. <b>I plan to work during most of the week</b>. I’m going to be on a team call this evening, so I napped this afternoon to make sure I can stay up later. A quieter schedule should mean more early-morning writing time, too, and I’m looking forward to <b>posting more notes on browsing the Web from Emacs</b>.

Lastly, I’m thrilled at how BarCampManila’s turning out. I pinged a couple of people about having a geek get-together on Thursday, August 21, and gave them the responsibility for making it happen. And they did! So, if you’re near Metro Manila and you want to hang out, check out:

<a href="http://www.barcamp.org/BarCampManila">BarCamp Manila</a> – August 21, 2008, 7:00 PM @ G2VC Innovation Center<br />
<a href="http://www.barcamp.org/BarCampManila">SIGN UP ON BARCAMP.ORG!</a>