Saturday, February 7, 2009

Fun with RSS

Today I've been playing around with emacs, and one of the things I've wanted to write is something to grab a web page, do some manipluations to it and dump that to a buffer.

So what I've got going is two things that are not quite joined up yet. Firstly grabbing some URL and sticking it in a buffer...


(defun url-to-buffer(url)
(interactive "sEnter site url : ")
(let ((buffer (get-buffer-create "*url-to-buffer*")))
(shell-command (format "c:/coreutils/bin/wget.exe -q -O - %s" url) buffer)))


As you can see I'm using windows here, but as long as you have a path to wget this should work. I'm simply running wget and capturing the output to a buffer.

So running that on BBC's news RSS feed, I then saved it to a file called bbc.xml.

Since this is an XML file I can parse it without any effort using xml.el. Here is some code that walks through the items in the RSS file and prints them out in human readable format (ie: not XML) into a buffer.

There is some helpful info about xml.el here

http://www.emacswiki.org/emacs/XmlParserExamples

which was neccesary to figure to figure out the syntax to grab the text from a tag. I wrote helper function to go from a node to the text for that node, since the syntax is quite verbose:


(defun get-item(tag node)
(let ((child-node (car (xml-get-children node tag))))
(car (xml-node-children child-node))))


This lets you go from a tag name (eg title) and a node that you parsed from the xml file, to the item.


(defun parse-rss(filename)
(interactive "fRss file :")
(let ((parsed-xml
(xml-parse-file filename))
(buffer
(get-buffer-create (format "%s-parsed.txt" filename))))
(goto-line 1 buffer)
(erase-buffer)
(let* ((rss (car parsed-xml))
(channel (xml-get-children rss 'channel))
(items (xml-get-children (car channel) 'item)))
(dolist (item items)
(let ((title (get-item 'title item))
(description (get-item 'description item))
(date (get-item 'pubDate item)))
(insert (format "*%s*\n%s\n%s\n\n" title description date)))))))


xml-parse-file is our one shot call to parse the xml file, which returns a nested lisp structure representing the xml document. I grab the channel, then grab any items within the channel and print out three items that I've grabbed from them, the title, description and date.

So each item output looks like this:

*Obama defends economic stimulus*
The US president defends his economic stimulus plan as "absolutely necessary", and urges Congress to approve it quickly.
Sat, 07 Feb 2009 22:23:20 GMT

Note that if you want to read RSS feeds in emacs, look up the builtin function newsticker. All this information I'm writing about is really useful if you want to code up something custom though.

Sending email via gmail in emacs

I got this working in linux, and it's fairly easy. I'm still trying to get it working on my win32 machines, and I'll post about that later if I get it working.

This post has most of the details I needed:

http://obfuscatedcode.wordpress.com/2007/04/26/configuring-emacs-for-gmails-smtp/

So I put the following in my .emacs


; install starttls from here (no need for patch)
; http://josefsson.org/emacs-smtp-starttls.html

(setq send-mail-function 'smtpmail-send-it
message-send-mail-function 'smtpmail-send-it
smtpmail-starttls-credentials
'(("smtp.gmail.com" 587 nil nil))
smtpmail-auth-credentials
(expand-file-name "~/.authinfo")
smtpmail-default-smtp-server "smtp.gmail.com"
smtpmail-smtp-server "smtp.gmail.com"
smtpmail-smtp-service 587
smtpmail-debug-info t
starttls-extra-arguments nil
smtpmail-warn-about-unknown-extensions t
starttls-use-gnutls nil)


And followed the instructions to make the .authinfo file containing:

machine smtp.gmail.com login [your name]@gmail.com password [your password]

And finally download, unzip, make and install startttls:

http://josefsson.org/emacs-smtp-starttls.html

You don't need to apply the patch there, it works without as far as I can tell.

Finally you can create and send mail.

Friday, January 23, 2009

Simple perforce checkout in emacs using elisp

I know there are all kinds of clever perforce modes, but I prefer to have a few common perforce actions that I write myself and can easily extend and customise as needed.

For example this is all you need to checkout a file you are visiting.


(defun p4-checkout-buffer()
"Check out the current buffer from perforce using a shell command line"
(interactive)
(shell-command (format "p4.exe edit %s" (buffer-file-name)))
(toggle-read-only -1))

Monday, January 12, 2009

Finding writable files in a directory in a windows command line

Simple thing, but if you want to find which files in a directory are writable, use this dos command ...


dir /a-r-d /s /b


/a is to search for attributes. In this case r is read only and d is directory. The minus signs negate those attributes. So we're looking for writable files only.

/s means recurse subdirectories

/b means bare format. Path and filename only.