Sunday, May 03, 2009

XMonad sending keys to windows

At the moment my typical coding projects involves running the same program in multiple windows. So there is a lot of ctrl+c ctrl+p RET alt+tab and then repeat for the next window when I need to restart the program.

This can be automated as I'm using the XMonad window manager. For this to work you need both XMonad and XMonadContrib from the darcs repositories. The functionality should be available in the yet-to-be-released 0.9 version.

The crucial parts of my xmonad.hs looks like this:
import XMonad.Util.Paste (sendKey)
(...)
, ((modm, xK_i ), sequence_ [sendKey controlMask xK_c,
sendKey controlMask xK_p,
sendKey controlMask xK_j,
windows W.focusDown])


ctrl+j is equivalent to the enter key. So as you can guess this keybinding sends ctrl+c ctrl+p enter and then moves the focus to the next window. Perfect for my needs. :)

Thanks to byorgey and ml of #XMonad of the freenode IRC network for helping out!

Friday, May 01, 2009

SQL tree to dot / graphviz using Common Lisp


Code.

This tree

`(query
(sfw "SELECT"
(sellist (attribute "a"))
"FROM"
,(mk-fromlist (list "Kunde" "Ordre" "Ordrelinje" "Varegruppetilhorlighet"))
"WHERE"
,(mk-cond-and (list (attr-eql "Kunde.KundeID" "Ordre.KundeID")
(attr-eql "Ordre.OrdreID" "Ordrelinje.OrdreID")
(attr-eql "Ordrelinje.VareID" "Varegruppetilhorlighet.VareID")
'((attribute "Kunde.ftype") "=" (pattern "'E'"))
'((attribute "Varegruppetilhorlighet.Varegr") "=" (pattern "'GaffelTrucker'"))
'((attribute "OrdreDate") "LIKE" (pattern "'2008%'"))))))


will produce an image like the one above (full image here).

Wednesday, March 11, 2009

Erlang-mode, distel: Always split window on compile

On the initial Erlang compile of an .erl file the entire window goes the Erlang shell in Emacs. I always like the windows split, and I absolutely don't like to hop around with C-x o to fix it. Another thing I like is the shell/compile window to always be the lowest window.

Fortunately, this is not too hard to fix:
(defun erlang-compile-always-split ()
"If you only have one window, split the window, then compile.
If you have 2 windows, just compile.
The compilation window should always be the lowest window."
(interactive)
(cond ((= (count-windows) 2)
(erlang-compile)) ; already two windows, just compile
(t
(let* ((o-window (eql nil (get-buffer erlang-shell-buffer-name))))
(split-window-vertically)
(when o-window (other-window 1))
(erlang-compile)
(when o-window (other-window 1))))))

(add-hook 'erlang-extended-mode-hook
(lambda ()
(define-key erlang-extended-mode-map "\C-c\C-k" 'erlang-compile-always-split)))


If the formatting is incorrect, try this.

More info on Distel, erlang-mode and Emacs.

This small modification was inspired by Steve Yegge's swap-windows function.

Happy Emacs time.

Saturday, February 28, 2009

Better looking Emacs in Ubuntu / Debian


The default Emacs you get from Ubuntu looks really bad. The one pictured here is what you get from the following:

sudo apt-get install emacs-snapshot-gtk emacs-snapshot-el

This is much, much better. I almost cannot believe I didn't find this before.

While you're at it you could also do
sudo apt-get install emacs-goodies-el
to get different color themes. M-x color-theme-select to launch.

ANTLR: Calling Java functions from parsed input with a variable number of arguments

I don't remember seeing quite an example like this, so here is one.
This is not exactly rocket science: The point is that you can write code like this:

add(1,2,3,4,5)

And parsing that will call the Java method "add" which looks like this:

public Integer add(Integer...arguments) {
Integer sum = new Integer(0);
for (Integer x : arguments) {
sum+= x;
}
return sum;
}
}


So if you need a simple interface language over Java methods, you could use this approach.

ANTLR parse code (not that beautiful).
The ANTLR Parser Generator.

Good luck with your parser.

Friday, February 27, 2009

Scrapy: Adding signal handler on domain_closed

I didn't find an example for this on the web, so here is one:

SPIDER = UioFagSpider()

def domain_closed(domain, spider, status):
if status=='cancelled':
return
print "saving state for %s" % (str(domain))
spider.save_state()

from pydispatch import dispatcher
from scrapy.core import signals

dispatcher.connect(domain_closed, signal=signals.domain_closed)


Full code. Very similar to the Scrapy tutorial code.

Reinteract python shell

Reinteract is a system for interactive experimentation with python. You enter Python code and expressions and immediately see the results. What distinguishes Reinteract from a shell (such as IPython or the builtin interactive mode) is that you can go back and edit expressions you entered earlier and the results will flow through the part of the worksheet after the changed portion.
Source

The Reinteract shell deserves more attention. screenshotThis is excellent for rapid experimentation.

Found through Virgil Griffith from the video Polyworld: Using Evolution to Design Artificial Intelligence.

Have a nice day.

Erlang shell pretty printing sets and dictionary

After reading Extending the Erlang shell (part 1) by Ulf Wiger from 2007, I updated his code to work with Erlang's stdlib 1.15.3.

My code adds support for both pretty printing erlang dictionaries and sets. You can find the code here: http://ivarref.at.ifi.uio.no/erlang/. If you need to port this code to work with other versions of the stdlib, I inserted "From Ulf" and the likes around the relevant areas.

It's quite easy to add support for new data types, the hard work was done by Ulf. Part two of his article is located here.

Adding pretty printing for sets goes like this (copy and paste in the shell):
rr(code:which(sets)),
fa(sets,fun(#sets{}=S) -> {custom,sets,true,lists:sort(sets:to_list(S))}; (_) -> no end).


lists:sort is not strictly needed, but simply my preference to ensure some consistency. This code is also given in the readme.txt.

Have a good day. :)

Sunday, August 17, 2008

Logo programming

Update 30h August: I added a simple gallery available here.



I've been playing around a little bit with the Logo programming language.

It's fun, easy and fast.

I'm using ACSLogo for Mac OS X. For Windows MSWLogo is OK.

Update: For linux XLogo looks OK. And it's cross platform. See more options.


More pictures:


Saturday, June 07, 2008

DivaScheme!

Here it is:



This is a really excellent editing mode for DrScheme. More info here.

I have to say I pretty much prefer this over both Emacs and Vim. (For editing s-expressions anyway.)

You can also do some nice drawing in DrScheme, example here. Too bad my Scheme course is just over.