Monday, December 26, 2011

Making a emacs lisp expression expand itself to XML

My response to the blog article An Emacs Programming Challenge

The goal is to make the emacs lisp record below execute itself and produce XML. My lisp has gotten rusty so it took me a couple of hours to get this working, but I think I got there...

You can see it properly formatted and coloured here

It's fairly straightforward. The only complexity is the use of lexical-let. emacs lisp is dynamically scoped, so in the function I create for each keyword, where it prints the symbol name is a free variable at runtime. So if sym-name is not defined you'll get an error, otherwise you'll get whatever it is defined as instead of the value you wanted.

By using lexical-let any variables are bound lexically, that is stored in an environment that stays with the function we create when it is executed (a closure).
(require 'cl) ;; uses lexical-let

(defun make-xml-izer(name)
"Given a symbol NAME this makes a function that outputs an xml string for that
symbol and using fset binds it to the same symbol so it becomes executable.
This pollutes the global function namespace so be careful with which names you
pass in"
(lexical-let ((sym-name name))
(fset (intern name)
(lambda(&rest input)
"returns a string representing the xml encoding of the input sexp"
(let ((res (format "<%s>" sym-name)))
(dolist (item input)
(if (listp item)
(setf res (concat res (eval item)))
(setf res (format "%s%s" res item))))
(setf res (format "%s" res sym-name))
res)))))

;; executing this makes all the symbols in the list below executable
(mapcar (lambda(s)
(make-xml-izer
(symbol-name s)))
'(record date millis sequence logger level class method thread emessage exception frame line))

;; the sample record
(record
(date "2005-02-21T18:57:39")
(millis 1109041059800)
(sequence 1)
(logger nil)
(level 'SEVERE)
(class "java.util.logging.LogManager$RootLogger")
(method 'log)
(thread 10)
(emessage "A very very bad thing has happened!")
(exception
(emessage "java.lang.Exception")
(frame
(class "logtest")
(method 'main)
(line 30))))

Saturday, July 30, 2011

eredis update

I've been busy on my emacs redis client eredis and it now supports the entire API. It still needs a bit more polish but it should be a workable Redis client now, and I will continue to play with the org-mode table integration which I think has a lot of potential uses: for example making a gui to edit server parameters in just a few seconds.

Also made a new video showing some of the new org table creating commands and the monitor mode that shows the Redis commands as they are run on the server:


Monday, July 25, 2011

eredis: a Redis client in emacs lisp



I set up a google code project today for eredis. A Redis client in emacs lisp.

The program consists of a single emacs lisp file eredis.el

emacs lisp includes facilities for writing network applications. In my code I use `make-network-process' to open a connection to a specified redis server. Then the Redis api is exposed.

One nice feature of emacs I have used is org-table-mode. This lets you edit and manage the data in a Redis server in an org table. For example, you can grab all keys matching a pattern and create an org table from the key value pairs, then edit that table. You can then send it back to Redis with interactive commands that send either the whole table, or just the current row back to Redis using mset or set.

This work flow is not safe when working with multiple users, if you care about overwriting each others data. For example, I could store the last values you got from Redis in addition to your edited values. When you go to set a new value I first grab it from Redis, check if it has changed since you got it, and if so warn you (showing you the new value). For many work flows this would work well. For example the use case of a group of users editing a shared DB of configuration data.



Tuesday, June 14, 2011

Emacs progress indication

When programming in emacs lisp, there is an easy way to show progress feedback to the user when a task will take some time. Here's a block of code from the elisp manual showing how to do it.

(let ((progress-reporter
(make-progress-reporter "Collecting mana for Emacs..."
0 500)))
(dotimes (k 500)
(sit-for 0.01)
(progress-reporter-update progress-reporter k))
(progress-reporter-done progress-reporter))

I've incorporated this into my duplicate files code, linked below...

http://code.google.com/p/justinhj-emacs-utils/source/browse/trunk/duplicates.el


Sunday, June 5, 2011

More on duplicate file handling in emacs dired

I had some good feedback on my last post, that it would be useful to be leave just the superfluous duplicate files marked in the dired buffer. After doing that you can then copy them to another folder, or delete them.

I've added a function to do this `dired-mark-duplicate-files', and updated the google code site with the changes.

To copy to another folder use R and select a folder to move the dupes to. In order to delete them hit D.



Wednesday, June 1, 2011

Finding duplicate files in a dired buffer



picture by Donald MacLeod

This is a an example of programming emacs in emacs-lisp just to give an idea of what you can put together in an hour or two. I was looking at a dired buffer with a bunch of photos in, and some were the same photo that I'd downloaded twice. So I started thinking about writing a utility in emacs to automatically find and remove the duplicate files. In this post I'll just show the code for finding the files and display their filenames in a buffer.

I've put the source on google code.

After downloading you can load the source into emacs and call `eval-buffer', then open up a dired buffer to try it out. For this to be useful you need some duplicated files, so make some if you need to.

Mark the files you want to check for duplicates. For example to mark all jpg files you would type %m to mark files matching a regexp and type .*\.jpg

Now execute the command `dired-show-marked-duplicate-files' and after a short delay (in my test 80 jpg photos took about 5 seconds) you'll see a buffer called 'Duplicated files' which contains a list of the files which have the same contents.

Next steps for this little project will be to give you an interactive way to delete the duplicated files. I haven't decided quite how I'd like that to work, drop me an email if you have an idea. I've been thinking about perhaps resetting which files are marked so that only the duplicates are marked. At that point you can then hit R to move them to another spot, or delete them with x.

Now some comments about the code involved...

Most of the work is done in the function dired-show-marked-duplicate-files. First line " (interactive)" makes it an interactive function, meaning the user of emacs can invoke it.

"(if (eq major-mode 'dired-mode)" will check that we're in the right kind of buffer, because it makes no sense to run this in another mode.

In order to find the duplicate files I just need to walk the list of marked files, generate the md5 value of the contents of each one and add it to hash table. The keys in the hash table will be the md5, and the values will be a list of files with that md5. Once we've done that, finding duplicates is a simple matter of walking the hash table keys and displaying any where the value has multiple entries.

"(let ((md5-map (make-hash-table :test 'equal :size 40)))" Creates the hash table, making sure we use 'equal to match our filenames.

"(let ((filenames (dired-get-marked-files)))" this gets the marked files as a list of filenames

The next little bit of code is just to store the item in the hash table after getting the md5. There's no function in emacs to get the md5 of a file, but you can get the md5 of a string, so I wrote a helper function for getting the contents of a file into a temporary buffer first.

(defun md5-file(filename)
"Open FILENAME, load it into a buffer and generate the md5 of its contents"
(interactive "f")
(with-temp-buffer
(insert-file-contents filename)
(md5 (current-buffer))))

Finally I want to display the results, so I create a buffer and then use maphash (walks the keys of a hash table executing a function on each) with a helper function `show-duplicate' which simply writes the values of the hash table entry into that buffer.

Tuesday, April 26, 2011

Programmer tips for Mac OSX

Some tips for programmers on the Mac.

emacs: the best place to get emacs for Mac seems to be here http://emacsformacosx.com/ which is also the most no nonsense website design ever

Not really a Mac tip, but I stick my .emacs configuration file in a Dropbox folder, along with any emacs libraries and emacs lisp code I write. Then where-ever I install emacs I make a simple .emacs that points to the one in the Dropbox folder. This also forces me to make sure any platform specific emacs stuff is properly handled.

Clipboard: copy and paste between the terminal and other apps can be done with pbcopy and pbpaste. For example a long complicated command line you want to email to yourself, just do:

echo "long complicated bash command line you don't want to retype" | pbcopy

And then you can Command-V that into your email window. Going the other way is just as simple; Command-C the text you want and pop it into the terminal window with pbpaste.

Open: If you want open an application from the command line you can do it like this:

open -a SomeApp /Users/yourname/SomeFile.hai

You can open a folder in finder

open /Folder/

or

open /Folder/SomeFile.hai

to open that file with it's default application.

Check out the help 'man open', to see other stuff like how you pipe stdout into an application.

Finally check out this guys OpenTerminalHere script. This pops an icon in finder that lets you open a terminal window in the highlighted folder.