Friday, February 20, 2009

Sending mail with emacs in Windows


Thanks to a commenter in my earlier blog post for pointing me to this way of sending mail that actually does work:

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

You need to download msstp from sourceforge and install the exe in your path.

http://sourceforge.net/projects/msmtp/

There is one change I needed to make to get this working. In the config file:

msmtprc.txt

You need to add a line

from your.email@address.here

Without that I was getting an error number and no email. If you run into a problem like that you can make sure MSSTP is running by trying it from the command line:

msmtp -a yourname some@recipient.com

Type some mail and hit Ctrl-Z and it should send correctly.

Once that is working you can enter gnus within emacs, and send a mail using:

M-x message-mail

Fill out the fields and type C-c C-c and it should send correctly.

A security issue with this is that it doesn't seem to work if you leave your password empty in the configuration file. From the command line it simply prompts you for your password and away you go, but from emacs it just hangs. So if you use this method you're taking the huge risk of having your password in a readable file on the hard drive. I'll be trying to find a way around that before using this permanently.

Monday, February 16, 2009

Who uses the A* code

I often wonder who uses the A* code. Sometimes people write to me to tell me they've used the code in their University assignments or just for fun, but has anyone used this in a project that I don't know about?

My A* algorithm on google code

During my time at Activision I worked on a code base that included some code from their game Gun, and was happy to find my code, verbatim included there too!

Also, today, I downloaded a 2d game prototyping engine called Angel, from some EA guys. When I looked at how their path finding worked, I was surprised to see my code in there too!

http://code.google.com/p/angel-engine/

It's cool that people are using this code. If you do use it in some commercial or interesting project, please let me know. A credit or thank you in your product would be nice too!

A* algorithm bug fixes

It's been a while since I checked the issues page on my A* algorithm google code project. So it turns out there's a possible memory leak and the Manhattan distance calculation in the findpath.cpp is wrong. I've investigated both of these and uploaded a fixed version.

In the case of the memory leak, there is some code in my "fast simple allocator" which allocates some memory, assigns it to a temporary pointer, then converts it and stores it in a member variable. Apparently, this shows up in memory tracking software as a leak, even though the memory is released in the destructor, so I've changed the code so that the allocation and free is done with the same variable.

The Manhattan distance refers to a simple heuristic for finding a path when you can only move horizontally and vertically through a grid. Like calculating how far you would have to walk in a city on a grid system, like NY, you simple count how many blocks along and how many blocks up you need to walk. Well I implemented this carelessly and forgot to make sure that I take the absolute value when calculating the difference between 'streets'. That's done now.

You can download the latest version of the A* algorithm here...

http://code.google.com/p/a-star-algorithm-implementation/downloads/list

Friday, February 13, 2009

Setting up lispworks personal

I've always used this excellent pair of blog posts by Bill Clementson to get lispworks personal working with emacs/slime on windows.

Configuring SLIME for 3 Win32 CL Implementations
Using LispWorks Personal with Emacs/SLIME on Windows

Having had the joy of a hard drive failure last week I had to reinstall everything, and I've found that with the latest CVS version of slime you need to change your ".slime.lisp" file to look like this:


(load "/YOURPATHTOSLIME/slime/swank-loader.lisp")
(swank-loader:init)
(swank::create-server :port 4005)


So if your slime/lispworks personal setup stops working when you upgrade slime, hopefully you found your way here.

Searching Reddit, Flickr, and Google from emacs = Rad

I came across another brilliantly useful corner of emacs today.

webjump by Neil Van Dyke is part of emacs and Xemacs, and gives you a way to assign memorable names to web site urls, and to build simple search queries. It then opens the website using emacs' built in browse-url function.

Set up is easy. All you need to add something like this to .emacs:


(require 'webjump)
(global-set-key [f2] 'webjump)
(setq webjump-sites
(append '(
("Reddit Search" .
[simple-query "www.reddit.com" "http://www.reddit.com/search?q=" ""])
("Google Image Search" .
[simple-query "images.google.com" "images.google.com/images?hl=en&q=" ""])
("Flickr Search" .
[simple-query "www.flickr.com" "flickr.com/search/?q=" ""])
)
webjump-sample-sites))


The first line just makes sure the feature is loaded, and then I bind the webjump function to F2 in the second line.

To use webjump, just press F2, and press TAB to expand the sample sites that are built in, and the ones we just added above. Type Flickr Search for example, and then you get a prompt for the search string. Once you enter that your browser should open with flickr showing the search.

I've added Reddit, Flickr, and Google image search above, but it's easy to add your own as follows:

Each entry in the jump list takes the form (name . url|builtin), where the simplest shortcut is just a name, url pair like this:


("astar algorithm" . "http://www.heyes-jones.com/astar")


There are some builtin functions that you can use. For example simple-query lets you enter some text that will be inserted between two strings. The format is:


("Reddit Search" .
[simple-query "www.reddit.com" "http://www.reddit.com/search?q=" ""])


When doing something more clever than an url you pass in a vector (hence the []'s), where the first argument is the function, the second is the default url (if the user doesn't enter anything, and the third and forth arguments are the prefix and the postfix wrapped around the users input to make the search work. In this case the last argument is the empty string.

You can provide your own functions to do something really fancy if you need to.

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.