<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc=" http://purl.org/dc/elements/1.1/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>briancarper.net (λ) (Category: Programming)</title><link>http://briancarper.net/category/5/programming</link><description>Some guy's blog about programming and Linux and cows.</description><item><title>Shuffle lines in Vim</title><link>http://briancarper.net/blog/580/shuffle-lines-in-vim</link><guid>http://briancarper.net/blog/580/shuffle-lines-in-vim</guid><pubDate>Thu, 07 Jul 2011 14:07:41 -0700</pubDate><description>&lt;p&gt;In a pinch, I needed to randomize the order of a few thousand lines of plain text.  In Linux you can just pipe the file through &lt;code&gt;sort&lt;/code&gt;, even right inside Vim:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:%!sort -R
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But I was stuck on Windows.  And I don't know how to randomize a file in native Vim script.  But doing it in Ruby is pretty easy, and luckily, Vim has awesome Ruby support.  Tne minutes' work and a few peeks at &lt;code&gt;:h ruby&lt;/code&gt; and we have a successful, working kludge:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function! ShuffleLines()
ruby &amp;lt;&amp;lt; EOF
    buf = VIM::Buffer.current
    firstnum =  VIM::evaluate('a:firstline')
    lastnum = VIM::evaluate('a:lastline')
    lines = []
    firstnum.upto(lastnum) do |lnum|
      lines &amp;lt;&amp;lt; buf[lnum]
    end
    lines.shuffle!
    firstnum.upto(lastnum) do |lnum|
      buf[lnum] = lines[lnum-firstnum]
    end
EOF
endfunction
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;2011-07-07 23:32 - Edited to remove a superfluous line.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;2011-07-09 21:33 - Wrong parameter for &lt;code&gt;sort&lt;/code&gt;, oops.&lt;/em&gt;&lt;/p&gt;</description></item><item><title>Keyword Arguments: Ruby, Clojure, Common Lisp</title><link>http://briancarper.net/blog/579/keyword-arguments-ruby-clojure-common-lisp</link><guid>http://briancarper.net/blog/579/keyword-arguments-ruby-clojure-common-lisp</guid><pubDate>Fri, 24 Jun 2011 17:22:48 -0700</pubDate><description>&lt;p&gt;And suddenly I return to blogging, rising from the ashes like some kind of zombie phoenix.  Turns out &lt;a href=&quot;http://oreilly.com/catalog/0636920013754/&quot;&gt;writing a book&lt;/a&gt; is a good absorber of time, like some sort of heavy-duty temporal paper towel.  Now that I've gotten the terrible similes out of my system, let's talk about keyword arguments, one of my favorite features in any language that supports them.&lt;/p&gt;

&lt;p&gt;Ruby, Clojure, and Common Lisp are all languages I enjoy to some degree, and they all have keyword arguments.  Let's explore how keyword args differ in those languages.&lt;/p&gt;

&lt;!--more Read about keyword arguments. --&gt;

&lt;h1&gt;Why keyword arguments?&lt;/h1&gt;

&lt;p&gt;Why are keyword arguments good?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can omit arguments.&lt;/li&gt;
&lt;li&gt;You can supply arguments in an arbitrary order.&lt;/li&gt;
&lt;li&gt;Arguments are labeled, so you know what argument means what. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Positional arguments require mentally lining up the 7th argument in your function call with the 7th argument in the function signature, and so on.  Keyword arguments become more and more attractive the more arguments you have in your function signature.&lt;/p&gt;

&lt;p&gt;Keyword arguments trade a bit of verbosity for added explicitness, clarity and reduced mental burden.  (Kind of like Lisps do overall.  Fancy that.)&lt;/p&gt;

&lt;h1&gt;Ruby&lt;/h1&gt;

&lt;p&gt;Ruby doesn't have special support for keyword arguments.  But Ruby likes its hashes, so you can just pass one in as an argument to a function.  As some syntax sugar, if you pass a &quot;flat&quot; list of &lt;code&gt;:key =&amp;gt; val&lt;/code&gt; pairs, Ruby slurps them all together and stuffs them into a hash for you.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;def foo(arg)
  p arg
end

foo({:x =&amp;gt; 123})                  # =&amp;gt; {:x=&amp;gt;123}
foo(:x =&amp;gt; 123)                    # =&amp;gt; {:x=&amp;gt;123}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With even more added sugar, you can leave off the parens in Ruby function calls.  So this is pretty common in Ruby:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;foo :x =&amp;gt; 123                     # =&amp;gt; {:x=&amp;gt;123}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;How nice and punctuation-less.  But then things get ugly.  What about this?&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;foo {:x =&amp;gt; 123}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That won't even compile.  Ruby thinks &lt;code&gt;{:x =&amp;gt; 123}&lt;/code&gt; is a code block, and a bare key/value pair isn't valid syntax as the first thing in a code block.  You need the parens.  A bit unfortunate, but it gets worse...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;def bar(arg1,arg2)
  puts &quot;#{arg1} #{arg2}&quot;
end

bar :x =&amp;gt; 123, :y =&amp;gt; 456          # Runtime error
bar {:x =&amp;gt; 123}, {:y =&amp;gt; 456}      # Won't compile
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the first example, all of the key/value pairs are slurped into one hash and end up in &lt;code&gt;arg1&lt;/code&gt;.  There's nothing left for &lt;code&gt;arg2&lt;/code&gt;, so you get a &quot;wrong number of arguments&quot; exception.  The second example won't even compile, of course, because again Ruby thinks &lt;code&gt;{:x =&amp;gt; 123}&lt;/code&gt; must be a code block with invalid syntax.&lt;/p&gt;

&lt;p&gt;It gets worse if you change the argument list for &lt;code&gt;bar&lt;/code&gt; slightly...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;def bar2(arg1 = {}, arg2 = {})
  puts &quot;#{arg1} #{arg2}&quot;
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This way, you don't even have to supply any arguments.  This is nice, if all of your argument are optional.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;bar2    # =&amp;gt; {} {}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now suppose you want &lt;code&gt;arg1&lt;/code&gt; to be &lt;code&gt;{:x =&amp;gt; 123}&lt;/code&gt;, and &lt;code&gt;arg2&lt;/code&gt; to be &lt;code&gt;{:y =&amp;gt; 456}&lt;/code&gt;.  You might naively try this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;bar2 :x =&amp;gt; 123, :y =&amp;gt; 456         # =&amp;gt; {:x=&amp;gt;123, :y=&amp;gt;456} {}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Oops, it all got dumped into &lt;code&gt;arg1&lt;/code&gt;, and now instead of a missing argument error, &lt;code&gt;arg2&lt;/code&gt; silently ends up with a default, empty hash.  You have to explicitly pass in an empty value for &lt;code&gt;arg1&lt;/code&gt; so that everything is slurped into &lt;code&gt;arg2&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;bar2 {}, :x =&amp;gt; 123, :y =&amp;gt; 456     # =&amp;gt; WRONG!  Ruby thinks {} is a code block again.

bar2({}, :x =&amp;gt; 123, :y =&amp;gt; 456)    # =&amp;gt; {} {:x=&amp;gt;123, :y=&amp;gt;456}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So much for syntax sugar.  You might think you'd be unlikely to find this kind of thing in the wild, but in Ruby on Rails for example, there are quite a few functions whose argument lists look exactly like this.  One signature for &lt;code&gt;link_to&lt;/code&gt; is:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;link_to(body, url_options = {}, html_options = {})
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;link_to &quot;foo&quot;, :controller =&amp;gt; :x                          # OK
link_to &quot;foo&quot;, :controller =&amp;gt; :x, :class =&amp;gt; &quot;css_class&quot;   # WRONG! 
link_to &quot;foo&quot;, {:controller =&amp;gt; :x}, :class =&amp;gt; &quot;css_class&quot; # OK
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What about support for default arguments?  We might want to say that if you didn't pass in an &lt;code&gt;:x&lt;/code&gt; argument, we want it to have some default value.  You might think this would work:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;def baz(x = {:x =&amp;gt; 123})
   p x
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But you would be sadly mistaken.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;baz                               # =&amp;gt; {:x=&amp;gt;123}
baz :y =&amp;gt; 456                     # =&amp;gt; {:y=&amp;gt;456} ... oops
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ruby doesn't merge your keyword arguments into the map in the parameter list.  It uses that map if you don't supply any arguments, otherwise your map replaces the default entirely.  So to get default arguments, you need something like&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;def baz2(args = {})
  args = {:x =&amp;gt; 123}.merge(args)
  p args
end

baz2                              # =&amp;gt; {:x=&amp;gt;123}
baz2 :x =&amp;gt; 555                    # =&amp;gt; {:x=&amp;gt;555}
baz2 :y =&amp;gt; 456                    # =&amp;gt; {:x=&amp;gt;123, :y=&amp;gt;456}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Kind of messy, but that's OK.&lt;/p&gt;

&lt;p&gt;One last subtle ambiguity in Ruby is determining whether someone passed a &lt;code&gt;nil&lt;/code&gt; argument for a keyword explicitly, or omitted a keyword entirely.  It might make a difference in some circumstances.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: ruby&quot;&gt;def quux(args={})
  p args.include? :x
  args = {:x =&amp;gt; nil}.merge(args)
  p args
end

quux                              # true, {:x=&amp;gt;nil}
quux :x =&amp;gt; nil                    # false, {:x=&amp;gt;nil}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Pretty messy, but such is life.&lt;/p&gt;

&lt;h1&gt;Clojure&lt;/h1&gt;

&lt;p&gt;Part of the fun of Lisps is lack of ambiguity.  Everything is spelled out in all its parenthesized glory.  In Clojure, when you call a function like &lt;code&gt;(foo :x 123)&lt;/code&gt;, Clojure requires you to specify what you want to happen with those arguments.&lt;/p&gt;

&lt;p&gt;It used to be that Clojure didn't have much support for keyword args at all.  Clojure did have support for allowing variable numbers of arguments to functions though.  So in the beginning, people used to slurp all of their arguments together into a list, and then turn it into a map inside the function.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;user&amp;gt; (defn foo [&amp;amp; args]
        (let [args (apply hash-map args)]
          (prn args)))
#'user/foo
user&amp;gt; (foo)
{}
user&amp;gt; (foo :x 123)
{:x 123}
user&amp;gt; (foo :x 123 :y 456)
{:y 456, :x 123}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That worked.  It still works today.  But nowadays there's a better way.  Why not slurp your arguments directly into a hash-map?&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;user&amp;gt; (defn foo [&amp;amp; {:as args}]
        (prn args))
#'user/foo
user&amp;gt; (foo)
nil
user&amp;gt; (foo :x 123)
{:x 123}
user&amp;gt; (foo :x 123 :y 456)
{:y 456, :x 123}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is an example of &lt;em&gt;destructuring&lt;/em&gt;.  In this case, all of our arguments are slurped into a list (thanks to &lt;code&gt;&amp;amp;&lt;/code&gt;), then this list is matched against our destructuring pattern, in this case &lt;code&gt;{:as args}&lt;/code&gt;.  &lt;code&gt;:as&lt;/code&gt; says to take everything in the list, turn it into a map and give the resulting map the name &lt;code&gt;args&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This isn't a special feature of &lt;code&gt;defn&lt;/code&gt;.  Destructuring works anywhere you're setting up bindings, for example in &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;doseq&lt;/code&gt; etc.  Like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user&amp;gt; (let [{:as args} (list :x 123 :y 456)] args)
{:y 456, :x 123}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It just so happens that &lt;code&gt;&amp;amp;&lt;/code&gt; creates the list for you, out of the arguments you pass.&lt;/p&gt;

&lt;p&gt;Destructuring does lots more than that though.  We can immediately pull out the values for keywords we care about, so they'll be bound to names in our function body.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;user&amp;gt; (defn foo [&amp;amp; {:keys [x y z]}]
        (prn x y z))
#'user/foo

user&amp;gt; (foo :z 123 :x 456)
456 nil 123
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It's certainly a bit more verbose than Ruby in the function signature, but it lacks ambiguity and it saves you some repetition in the function body. &lt;/p&gt;

&lt;p&gt;Clojure's approach also has the benefit of specifying directly in the function signature which keywords you're expecting.  In a smart editor, like Emacs, you get an indicator of what kinds of keywords you should be passing in.   See at the bottom?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/clojure/function-signature.png&quot; alt=&quot;Emacs function signature display&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This is also available at the Clojure REPL via the &lt;code&gt;doc&lt;/code&gt; function.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;user&amp;gt; (doc foo)
-------------------------
user/foo
([&amp;amp; {:keys [a b c]}])
  nil
nil
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There's no better documentation than live, built-in documentation.  There's nothing more distracting when programming than context shifts, and having to dig into a web browser to check a function signature is a huge mental page fault.&lt;/p&gt;

&lt;p&gt;What about default values?  Sure.  You can use &lt;code&gt;:or&lt;/code&gt; to specify defaults for some or all of your keywords.  This works much more like I'd expect, compared to Ruby.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;user&amp;gt; (defn foo [&amp;amp; {:keys [x y z] :or {x 1 y 2 z 3}}]
        (prn x y z))
#'user/foo

user&amp;gt; (foo :y 555)
1 555 3
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What about determining whether the user passed &lt;code&gt;nil&lt;/code&gt; for a keyword or whether they omitted the keyword entirely?  For that, you have to resort to testing the argument map for the existence of the key, which isn't fun, but at least it's possible.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;user&amp;gt; (defn foo [&amp;amp; {:keys [x y z] 
                    :or {x 1 y 2 z 3}
                    :as args}]
        (prn x y z)
        (prn args)
        (doseq [k [:x :y :z]]
          (println &quot;Contains&quot; k &quot;=&amp;gt;&quot; (contains? args k))))
#'user/foo

user&amp;gt; (foo :x 1 :y 2)
1 2 3
{:x 1, :y 2}
Contains :x =&amp;gt; true
Contains :y =&amp;gt; true
Contains :z =&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Why do I keep mentioning this?  See Common Lisp below.&lt;/p&gt;

&lt;p&gt;In any case, our parameter list is becoming huge and unweildy.  The first time I saw sample Clojure code like this, I almost did a spit-take.  But after a bit of getting used to, I'm finding that this syntax is pretty comfortable.&lt;/p&gt;

&lt;p&gt;The simplest case may not be as concise as Ruby, but aside from lack of ambiguity, the benefit of Clojure's approach is being able to safely do powerful (and borderline insane) things, like nested destructuring.  And this works everywhere you're setting up a binding.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;(defn ow-my-eyes [a &amp;amp; {[b {:keys [x y]}
                        &amp;amp; {[g h] :r
                           :keys [p q]
                           :or {q 123}}]
                       :x}]
  (prn a b g h p q x y))



user&amp;gt; (ow-my-eyes 444 :x [1 {:x 555 :y 666} :p 3 :r [888 999]])
444 1 888 999 3 123 555 666
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you wrote code like that in real life, you'd likely be defenestrated, but at least you know you can do it.  And destructuring in Clojure could likely be extended even further in the future, if someone came up with a use case for something that isn't supported.&lt;/p&gt;

&lt;p&gt;Official documentation for all of this is &lt;a href=&quot;http://clojure.org/special_forms&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;Common Lisp&lt;/h1&gt;

&lt;p&gt;When it comes to keyword arguments, Common Lisp supports mostly everything that Clojure does, and some things it doesn't.  CL was likely a big inspiration for Clojure's destructuring.  I highly recommend reading Practical Common Lisp to learn more about CL &lt;a href=&quot;http://www.gigamonkeys.com/book/functions.html&quot;&gt;keyword arguments&lt;/a&gt; and &lt;a href=&quot;http://www.gigamonkeys.com/book/beyond-lists-other-uses-for-cons-cells.html&quot;&gt;list destructuring&lt;/a&gt;.  (Read the rest of the book while you're at it.)&lt;/p&gt;

&lt;p&gt;Keyword arguments in CL look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; (defun foo (&amp;amp;key x (y 123) (z 456 z-supplied-p))
    (pprint (list x y z z-supplied-p)))
FOO

&amp;gt; (foo)
(NIL 123 456 NIL)

&amp;gt; (foo :z nil)             ; note, z-supplied-p tells us whether z was omitted or not
(NIL 123 NIL T)

&amp;gt; (foo :z 555 :x 666 :y 777)
(666 777 555 T)

&amp;gt; (foo :x 123)
(123 123 456 NIL)

&amp;gt; (foo :x 555)
(555 123 456 NIL)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Yeah, there's direct support for distinguishing supplied keys with nil values, and un-supplied keys.  That's pretty nice.&lt;/p&gt;

&lt;p&gt;Common Lisp also supports insane things like having keyword arguments' default values be a function of other arguments in the parameter list.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; (defun bar (&amp;amp;key (x 123) (y (+ x 1000)))
    (pprint (list x y)))
BAR

&amp;gt; (bar)
(123 1123)

&amp;gt; (bar :x 5)
(5 1005)

&amp;gt; (bar :x 5 :y 6)
(5 6)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Clojure destructuring can't do this by default, so you'd have to resort to a &lt;code&gt;let&lt;/code&gt; in the function body.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defn bar [&amp;amp; {:keys [x y] :or {x 123}}]
  (let [y (or y (+ x 1000))]
    (prn x y)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There's nothing stopping Clojure's destructuring from being patched to support this, of course.  But this isn't a feature I've ever found myself wanting particularly badly.&lt;/p&gt;

&lt;p&gt;As for how to destructure CL keyword arguments into a vector or hash-map, CL doesn't directly support doing it in function parameter lists like Clojure does.  (Though there's nothing stopping someone from throwing together a reader macro to let CL do this, of course.)  This isn't really surprising, because CL loves its cons cells, while Clojure embraces maps (and vectors and sets etc.), offering them default reader syntax and lots of other built-in support.&lt;/p&gt;

&lt;p&gt;So there you have it.  Keyword arguments.  Use them.  Love them.&lt;/p&gt;

&lt;p&gt;Lessons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sugar can be bad for your health.&lt;/li&gt;
&lt;li&gt;Ambiguous, bad.  Explicit, good.&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>org-mode is awesome</title><link>http://briancarper.net/blog/577/org-mode-is-awesome</link><guid>http://briancarper.net/blog/577/org-mode-is-awesome</guid><pubDate>Thu, 20 Jan 2011 16:14:44 -0800</pubDate><description>&lt;p&gt;I've seen &lt;a href=&quot;http://orgmode.org/&quot;&gt;org-mode&lt;/a&gt; for Emacs mentioned very frequently around the interwebs, so it went into my mental queue of topics to learn.  It finally bubbled to the top this week, so I took a look.&lt;/p&gt;

&lt;h1&gt;Organizer?  Nah.&lt;/h1&gt;

&lt;p&gt;As an organizer/calendar, well, I doubt I'll need it.  Enforced use of MS Outlook is mandated by work.  My Post-it-notes-all-over-my-desk method of organization will also continue to serve me well.&lt;/p&gt;

&lt;p&gt;There are some nice agenda-related shortcuts that are probably worth using though, like typing &lt;code&gt;C-c .&lt;/code&gt; to enter a datestamp, like &lt;code&gt;&amp;lt;2011-01-20 Thu&amp;gt;&lt;/code&gt;.  Then you can increment or decrement it one year/month/day at a time via &lt;code&gt;S-up&lt;/code&gt; and &lt;code&gt;S-down&lt;/code&gt;.  I like this.&lt;/p&gt;

&lt;h1&gt;Plaintext editor?  Yes!&lt;/h1&gt;

&lt;p&gt;As a plaintext outline and table editor... wow.  org-mode rocks.  Do you know how many hours of my life could have been saved by having a good ASCII table/bullet-list editor?  org-mode lines everything up and keeps it all nice and neat for you.&lt;/p&gt;

&lt;p&gt;You can also make plaintext check boxes and check/uncheck them.  And you can insert hyperlinks and footnotes, and click them to open web pages or jump back and forth between footnote and reference.&lt;/p&gt;

&lt;p&gt;There are ways to collapse and expand outlines, search for items and only display those items, and so on.  The &lt;a href=&quot;http://orgmode.org/guide/index.html&quot;&gt;documentation&lt;/a&gt; for org-mode is very clear and took me less than an hour to read through.  All-in-all a pleasant experience.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;* Agenda
** Things to learn
1. [X] Clojure
2. [X] org-mode (see [fn:diagram1])
3. [ ] Haskell
4. [ ] Japanese
   1. [X] Hiragana
   2. [X] Katakana
   3. [ ] Kanji
5. [ ] The true meaning of friendship

* Footnotes
[fn:diagram1]

| Task                              | Annoyance (1-10) |
|-----------------------------------+------------------|
| Making ASCII tables by hand       |              9.5 |
| Making ASCII bullet lists by hand |              7.2 |
| Using org-mode                    |              0.4 |
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It looks nice plastered into my blog, but you don't get a real idea of how many cool things you can do with it until you open it in Emacs and start shuffling items around, bumping them up/down a level in headlines, creating properly-numbered bullet items with one key, and seeing the columns in the table auto-resize as you type.&lt;/p&gt;

&lt;p&gt;I also highly recommend putting &lt;code&gt;(setq org-startup-indented t)&lt;/code&gt; into &lt;code&gt;.emacs&lt;/code&gt; to make everything look pretty on-screen.  It still saves as the simple plaintext above, but it looks like this in Emacs:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/emacs/org-mode.png&quot; alt=&quot;org-mode&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I can definitely see using org-mode for TODO files in some of my projects.  (You can mark entries as TODO (just by typing &lt;code&gt;TODO&lt;/code&gt; in front), and then toggle between TODO/DONE via &lt;code&gt;C-c C-t&lt;/code&gt;.)   I can also see using it as a general-purpose note-taker.&lt;/p&gt;

&lt;p&gt;org-mode also has a &lt;a href=&quot;http://mobileorg.ncogni.to/doc/&quot;&gt;mobile version&lt;/a&gt; for iPhone and Android, synced via WebDAV or Dropbox, so you can org-mode on your phone while you're driving to the grocery store&lt;sup id=&quot;fnref:dontdothis&quot;&gt;&lt;a href=&quot;#fn:dontdothis&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;.  Again I don't really need this, but there it is.&lt;/p&gt;

&lt;h1&gt;The joy of plaintext&lt;/h1&gt;

&lt;p&gt;Plaintext is awesome.&lt;/p&gt;

&lt;p&gt;It's the universal file format.  It's readable and writeable by scripting langauges, terminals, text editors, IDEs, word processors, web browsers, even lowly humans.&lt;/p&gt;

&lt;p&gt;Plaintext's one shortcoming is its lack of structure.  It's just a bunch of letters.  It doesn't have a color, it doesn't have a style, it doesn't line up into columns without a lot of effort.  There's nothing stopping you from opening a parenthesized list and forgetting the closing paren.&lt;/p&gt;

&lt;p&gt;Computers don't care about these problems, but humans are bad at producting plaintext by hand, and bad at editing it once it's produced.  Our clumsy, stumpy fingers and inconsistent, chaotic brains can't handle the freedom.&lt;/p&gt;

&lt;p&gt;Emacs (and Vim) are awesome because they let you do magical things to plaintext.  They enforce structure.  They provide shortcuts so you can get your plainext right the first time.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;[ ]&lt;/code&gt; is just two braces and a space, but org-mode lets me hit &lt;code&gt;C-c C-c&lt;/code&gt; and turn the space into an &lt;code&gt;X&lt;/code&gt;.  This may seem banal, hardly worth caring about, but add to this shortcut thousands upon thousands of others.  Things like org-mode, or &lt;a href=&quot;http://www.emacswiki.org/emacs/ParEdit&quot;&gt;paredit&lt;/a&gt;, or all of Vim's built-in magic... it all adds up to something wonderful.&lt;/p&gt;

&lt;p&gt;And best of all, you always still have the option of manually keyboarding over and typing that &lt;code&gt;X&lt;/code&gt; between the braces yourself.  It's still just plaintext underneath.  So you end up with the best of both worlds.&lt;/p&gt;&lt;div class=&quot;footnotes&quot;&gt;&lt;ol&gt;&lt;li id=&quot;fn:dontdothis&quot;&gt;&lt;p&gt;I do not recommend using org-mode while driving, for public safety reasons. &lt;a rev=&quot;footnote&quot; href=&quot;#fnref:dontdothis&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;</description></item><item><title>Vim undo tree visualization</title><link>http://briancarper.net/blog/573/vim-undo-tree-visualization</link><guid>http://briancarper.net/blog/573/vim-undo-tree-visualization</guid><pubDate>Mon, 18 Oct 2010 15:21:16 -0700</pubDate><description>&lt;p&gt;I wrote &lt;a href=&quot;http://briancarper.net/blog/568/emacs-undo-trees&quot;&gt;previously&lt;/a&gt; about an awsome plugin to give Emacs Vim-style undo trees.&lt;/p&gt;

&lt;p&gt;Vim's undo trees are the best thing since sliced bread, but the interface for browsing through the tree is not pleasant.  The &lt;a href=&quot;http://www.dr-qubit.org/undo-tree/undo-tree.el&quot;&gt;Emacs undo-tree library&lt;/a&gt; has a way to visualize the tree and move through it with your keyboard, which solves this problem.&lt;/p&gt;

&lt;p&gt;But now, thanks to Steve Losh, &lt;a href=&quot;http://bitbucket.org/sjl/gundo.vim/src&quot;&gt;Vim has an undo-tree visualizer too&lt;/a&gt;.  Delicious.  Though it's still beta and promises to eat your babies, it seems to work pretty well.  I think the diff view of the changes for the undo is a really good idea.&lt;/p&gt;

&lt;p&gt;Thus continues the eternal Vim/Emacs arms race.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vim/vim-undo-tree.png&quot; alt=&quot;Vim undo tree&quot; title=&quot;&quot; /&gt;&lt;/p&gt;</description></item><item><title>Vim :ruby and :rubydo scope</title><link>http://briancarper.net/blog/569/vim-ruby-and-rubydo-scope</link><guid>http://briancarper.net/blog/569/vim-ruby-and-rubydo-scope</guid><pubDate>Tue, 31 Aug 2010 10:40:51 -0700</pubDate><description>&lt;p&gt;Note to self.  In old Vim (tested in 7.2.320), I could do this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:ruby x='foo'
:rubydo $_=x
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now every line in the file says &lt;code&gt;foo&lt;/code&gt;.  But in Vim 7.3 I get an error:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NameError: undefined local variable or method `x' for main:Object
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The scoping rules for Ruby in Vim must have changed somewhere along the line.  I was abusing this feature to do some handy things, so this is sad.&lt;/p&gt;

&lt;p&gt;A workaround is to use global variables in Ruby instead.  So this still works:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:ruby $x='foo'
:rubydo $_=$x
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Phew.&lt;/p&gt;</description></item><item><title>Emacs undo trees</title><link>http://briancarper.net/blog/568/emacs-undo-trees</link><guid>http://briancarper.net/blog/568/emacs-undo-trees</guid><pubDate>Tue, 17 Aug 2010 14:06:33 -0700</pubDate><description>&lt;p&gt;I've said it before: &lt;a href=&quot;/blog/361/emacs-undo-is-horrible&quot;&gt;undo in Emacs is horrible&lt;/a&gt;.  On the other hand, &lt;a href=&quot;http://stackoverflow.com/questions/1088864/how-is-vims-undo-tree-used&quot;&gt;undo in Vim is awesome&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But this is true no longer.  Now there are &lt;strong&gt;&lt;em&gt;&lt;a href=&quot;http://www.dr-qubit.org/undo-tree/undo-tree.el&quot;&gt;undo trees for Emacs!&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;  Yes, this news is so important I had to italicize and bold it.  It's like Emacs has been punching me in the face for years, and today I got it to stop.  I never thought I'd see the day.&lt;/p&gt;

&lt;p&gt;And it works great too.  You can even view the tree visually and navigate it with the cursor keys, which is a step up on what Vim offers out of the box.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/emacs/emacs-undo-trees.png&quot; alt=&quot;Emacs undo trees&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In other news, Vim 7.3 is out and it now has &lt;a href=&quot;https://groups.google.com/group/vim_announce/browse_thread/thread/66c02efd1523554b&quot;&gt;persistent undo&lt;/a&gt; across reloads.  It's like an arms race, and gleeful hackers reap the benefits.&lt;/p&gt;</description></item><item><title>gaka 0.2.0</title><link>http://briancarper.net/blog/562/gaka-020</link><guid>http://briancarper.net/blog/562/gaka-020</guid><pubDate>Thu, 29 Jul 2010 13:59:22 -0700</pubDate><description>&lt;p&gt;Per &lt;a href=&quot;http://briancarper.net/blog/543/introducing-gaka&quot;&gt;many commenters' suggestions&lt;/a&gt; and thanks to code from &lt;a href=&quot;http://github.com/purcell/&quot;&gt;Steve Purcell&lt;/a&gt;, you can now use maps for CSS attributes in &lt;a href=&quot;http://github.com/briancarper/gaka&quot;&gt;gaka&lt;/a&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;user&amp;gt; (println (gaka/css [:a {:color :red}]))
a {
  color: red;}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This looks more like vanilla CSS thanks to the curlies, which is nice.   You just have to keep in mind that your key/value pairs could end up being printed in random order, and order is significant&lt;sup id=&quot;fnref:css&quot;&gt;&lt;a href=&quot;#fn:css&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; in CSS.&lt;/p&gt;

&lt;p&gt;It just so happens that maps are implemented in Clojure right now such that if they only have a few entries (16 key/value pairs), the order will be preserved, because you get a &lt;code&gt;PersistentArrayMap&lt;/code&gt; instead of a &lt;code&gt;PersistentHashMap&lt;/code&gt;.  But it's &lt;em&gt;highly dangerous&lt;/em&gt; to rely on such a thing.  It could change at any time in the future.&lt;/p&gt;

&lt;p&gt;In any case, you can also mix and match maps, lists and &quot;flat&quot; keyvals.  They'll all be flattened  That can help preserve attribute order in those cases where you need to.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;user&amp;gt; (println (gaka/css [:a :color &quot;red&quot; {:padding 0} (list :margin 0)]))
a {
  color: red;
  padding: 0;
  margin: 0;}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I've also enhanced &quot;mixins&quot; a bit further.  You can now mixin entire tags as well as attributes.  Or a combination of both.  Say you want a mixin that means &quot;&lt;em&gt;Make my element have no padding, and make links within the element be red&lt;/em&gt;&quot;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;user&amp;gt; (println (gaka/css [:div.foo mixin :margin 0]
                         [:div.bar mixin]))
div.foo {
  padding: 0;
  margin: 0;}

  div.foo a {
    color: red;}

div.bar {
  padding: 0;}

  div.bar a {
    color: red;}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can get gaka from &lt;a href=&quot;http://github.com/briancarper/gaka&quot;&gt;github&lt;/a&gt; or &lt;a href=&quot;http://clojars.org/gaka&quot;&gt;Clojars&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;footnotes&quot;&gt;&lt;ol&gt;&lt;li id=&quot;fn:css&quot;&gt;&lt;p&gt;Order is only significant in cases where you're doing things like &lt;code&gt;padding: 0; padding-left: 1px&lt;/code&gt;.  This is arguably bad CSS style, but it's valid, and it's also possible you'll have this kind of thing if you're generating CSS procedurally.  But most of the time, order is not significant.  e.g. it doesn't matter if you set text color first and background color second, or vice versa.  So maybe this isn't so much of a problem in practice. &lt;a rev=&quot;footnote&quot; href=&quot;#fnref:css&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;</description></item><item><title>Footnotes</title><link>http://briancarper.net/blog/560/footnotes</link><guid>http://briancarper.net/blog/560/footnotes</guid><pubDate>Tue, 27 Jul 2010 12:30:57 -0700</pubDate><description>&lt;p&gt;Did you ever notice how footnotes make your writing seem more important&lt;sup id=&quot;fnref:important&quot;&gt;&lt;a href=&quot;#fn:important&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; somehow?&lt;/p&gt;

&lt;p&gt;Maybe one reason is that &quot;real&quot; books use footnotes.  At a glance, it looks like I have references&lt;sup id=&quot;fnref:refs&quot;&gt;&lt;a href=&quot;#fn:refs&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; backing up everything I say.  In reality, I don't, but the connotation carries through somehow&lt;sup id=&quot;fnref:telepathy&quot;&gt;&lt;a href=&quot;#fn:telepathy&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;.  Now my blog seems scholarly and authoritative.&lt;/p&gt;

&lt;p&gt;And if you're like me, you can't resist clicking footnotes to see what they refer to&lt;sup id=&quot;fnref:clickme&quot;&gt;&lt;a href=&quot;#fn:clickme&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;.  According to my estimates, by utilizing footnotes, in one fell swoop I have decreased my readers' average reading efficiency by 73%.&lt;/p&gt;

&lt;p&gt;In any case, I've added experimental, rudimentary support for footnotes to &lt;a href=&quot;http://github.com/briancarper/cow-blog&quot;&gt;cow-blog&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;I'm loosely copying the syntax from &lt;a href=&quot;http://michelf.com/projects/php-markdown/extra/&quot;&gt;Markdown Extra&lt;/a&gt; for this.  Markdown is great, except when it isn't.  The standard doesn't have support for some useful extensions.  I use &lt;a href=&quot;http://attacklab.net/showdown/&quot;&gt;Showdown&lt;/a&gt; for Markdown support, and I'm probably going to work on adding more features of Markdown Extra to Showdown in the near future.&lt;/p&gt;

&lt;p&gt;I just dread actually doing it.  Showdown (like Markdown itself) is implemented as a series of hackish regex transformations of blobs of text.  It's not a proper grammar.  Implementing more of Markdown Extra means more regex blobbing.  It's brittle and fragile and even getting incomplete support for footnotes was less than enjoyable.  But at the same time I find myself wanting to do things that Markdown can't so, so I may have to bite the bullet.&lt;/p&gt;

&lt;p&gt;(If there's a Showdown Extra out there already, drop me a URL.  It'd be most appreciated.  But I couldn't find one.)&lt;/p&gt;&lt;div class=&quot;footnotes&quot;&gt;&lt;ol&gt;&lt;li id=&quot;fn:important&quot;&gt;&lt;p&gt;In reality nothing I say is important. &lt;a rev=&quot;footnote&quot; href=&quot;#fnref:important&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;&lt;li id=&quot;fn:refs&quot;&gt;&lt;p&gt;Does my inner dialog count as a reference? &lt;a rev=&quot;footnote&quot; href=&quot;#fnref:refs&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;&lt;li id=&quot;fn:telepathy&quot;&gt;&lt;p&gt;Via telepathy. &lt;a rev=&quot;footnote&quot; href=&quot;#fnref:telepathy&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;&lt;li id=&quot;fn:clickme&quot;&gt;&lt;p&gt;See? &lt;a rev=&quot;footnote&quot; href=&quot;#fnref:clickme&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;</description></item><item><title>Clojure syntax highlighting via SyntaxHighlighter</title><link>http://briancarper.net/blog/556/clojure-syntax-highlighting-via-syntaxhighlighter</link><guid>http://briancarper.net/blog/556/clojure-syntax-highlighting-via-syntaxhighlighter</guid><pubDate>Wed, 21 Jul 2010 15:17:42 -0700</pubDate><description>&lt;p&gt;How do you syntax-highlight Clojure code for display on a website?   The best way I can find is &lt;a href=&quot;http://alexgorbatchev.com/SyntaxHighlighter/&quot;&gt;SyntaxHighlighter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Daniel Gómez wrote a &lt;a href=&quot;http://www.deepbluelambda.org/programs/sh-clojure/new-clojure-brush-for-syntaxhighlighter&quot;&gt;brush&lt;/a&gt; to give SyntaxHighlighter Clojure support.  I &lt;a href=&quot;http://github.com/briancarper/cow-blog/blob/master/public/js/brushes/shBrushClojure.js&quot;&gt;tweaked it&lt;/a&gt; a bit myself and integrated it into cow-blog.  I also converted my &lt;a href=&quot;http://github.com/briancarper/dotfiles/blob/master/.vim/colors/gentooish.vim&quot;&gt;favorite color scheme&lt;/a&gt; to a &lt;a href=&quot;http://github.com/briancarper/cow-blog/blob/master/public/css/shThemeCow.css&quot;&gt;SyntaxHighlighter theme&lt;/a&gt;.  So when I write this code:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;brush: clojure&quot;&gt;(defn- ip
  &quot;Given a request, return the IP.  Looks for an x-forwarded-for
  header, falls back to :remote-addr on the request.&quot;
  [request]
  (or (get-in request [:headers &quot;x-forwarded-for&quot;])
      (request :remote-addr)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/clojure/syntax-highlight.png&quot; alt=&quot;Syntax highlighting example&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;...unless you're reading this via RSS, or in a browser without Javascript enabled, in which case you'll see plain, depressing black and white.  But that's one nice thing about SyntaxHighlighter.  It degrades nicely.&lt;/p&gt;

&lt;p&gt;One bad thing about SyntaxHighlighter is that it doesn't play nicely with Markdown.  Or rather, Markdown isn't powerful enough to let you specify the class of any markdown-generated HTML tags.  If you want the &lt;code&gt;&amp;lt;pre class=&quot;brush: clojure&quot;&amp;gt;&lt;/code&gt; that SyntaxHighlighter requires, you have to write out the HTML by hand.  But I hacked Showdown a bit to let me specify classes more easily, so I can avoid having to do that.&lt;/p&gt;

&lt;p&gt;The code for all of this is &lt;a href=&quot;http://github.com/briancarper/cow-blog&quot;&gt;on github&lt;/a&gt; with the rest of my blog.&lt;/p&gt;</description></item><item><title>ANTLR via Clojure</title><link>http://briancarper.net/blog/554/antlr-via-clojure</link><guid>http://briancarper.net/blog/554/antlr-via-clojure</guid><pubDate>Fri, 16 Jul 2010 17:41:04 -0700</pubDate><description>&lt;p&gt;&lt;a href=&quot;http://www.antlr.org/&quot;&gt;ANTLR&lt;/a&gt; is a parser-generator for Java.  Can you use it from Clojure?  Sure.  Would you want to?  Maybe.&lt;/p&gt;

&lt;p&gt;Here's how to do it, start to finish.&lt;/p&gt;

&lt;p&gt;For the impatient among you, all of the code below is on &lt;a href=&quot;http://github.com/briancarper/clojure-antlr-example&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone git://github.com/briancarper/clojure-antlr-example.git
&lt;/code&gt;&lt;/pre&gt;

&lt;!--more Grammars, grammars, everywhere --&gt;

&lt;h1&gt;Setup&lt;/h1&gt;

&lt;p&gt;I'm going to use leiningen for this project.  Let's make a new project called &lt;code&gt;antlr-example&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ lein new antlr-example
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now edit &lt;code&gt;project.clj&lt;/code&gt; to tell lein to fetch ANTLR (and Swank, since I use Emacs).  ANTLR is available in Maven Central, so leiningen can grab it for us.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defproject antlr-example &quot;1.0.0-SNAPSHOT&quot;
  :description &quot;ANTLR Clojure example&quot;
  :dependencies [[org.clojure/clojure &quot;1.2.0-beta1&quot;]
                 [org.clojure/clojure-contrib &quot;1.2.0-beta1&quot;]
                 [org.antlr/antlr &quot;3.2&quot;]]
  :dev-dependencies [[swank-clojure &quot;1.3.0-SNAPSHOT&quot;]])
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then a simple &lt;code&gt;lein deps&lt;/code&gt; will download all of the jars and all of ANTLR's dependencies for you.  Handy.  You end up with this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;antlr-example
├── classes
├── lib
│   ├── antlr-2.7.7.jar
│   ├── antlr-3.2.jar
│   ├── antlr-runtime-3.2.jar
│   ├── clojure-1.2.0-beta1.jar
│   ├── clojure-contrib-1.2.0-beta1.jar
│   ├── stringtemplate-3.2.jar
│   └── swank-clojure-1.3.0-20100502.112537-1.jar
├── project.clj
├── README
├── src
│   └── antlr_example
│       └── core.clj
└── test
    └── antlr_example
        └── core_test.clj
&lt;/code&gt;&lt;/pre&gt;

&lt;h1&gt;Interactive development?  Kind of...&lt;/h1&gt;

&lt;p&gt;One weakness of ANTLR via Clojure is that development is no longer REPL-based.  The ANTLR workflow is essentially a Java workflow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Write a grammar&lt;/li&gt;
&lt;li&gt;Compile the grammar into Java source code&lt;/li&gt;
&lt;li&gt;Compile the Java source into Java .class files&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Until and unless someone writes a Clojure backend for ANTLR, so that ANTLR can directly produce Clojure source code (and it seems like it should be possible to do so), you're back to a write/compile/debug cycle.  Joy!&lt;/p&gt;

&lt;p&gt;This also means restarting your REPL every time you alter and recompile your grammar.&lt;/p&gt;

&lt;p&gt;But the good thing is that there's &lt;a href=&quot;http://antlr.org/works/index.html&quot;&gt;ANTLR Works&lt;/a&gt;, a free GUI for writing ANTLR grammars.  ANTLR Works has an interactive interpreter, which is nice, and it has a compiler/debugger, which is better.  These let you test your grammar as you write it, by trying inputs and seeing the resulting AST in graphical form, which is as good as you could hope for.  This is actually even a bit nicer than a plaintext REPL.  &lt;/p&gt;

&lt;p&gt;Plus, it has a nice editor for ANTLR code.  Emacs was freaking out over the indentation on a few grammars I tried.&lt;/p&gt;

&lt;p&gt;So a decent workflow might be to write and debug your grammar in ANTLR Works, then fire up Clojure afterwards to consume the parser.&lt;/p&gt;

&lt;h1&gt;A simple grammar&lt;/h1&gt;

&lt;p&gt;We're going to use a classic textbook example, simple arithmetic expressions, as a proof of concept.&lt;/p&gt;

&lt;p&gt;For Java (hence Clojure) to find this code, it has to be named properly and it has to be put into the proper directory on &lt;code&gt;CLASSPATH&lt;/code&gt;.  I'm going to create the grammar file &lt;code&gt;src/antlr_example/Expr.g&lt;/code&gt;.  (My grammar file, Java source, and Clojure source will all be jumbled together in &lt;code&gt;src&lt;/code&gt;.   You can easily do it differently if this offends your sensibilities.)&lt;/p&gt;

&lt;p&gt;The first line in the grammar names the grammar (and the classes we'll consume from Clojure):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;grammar Expr;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Clojure being a s-exp based language, it might be nice to have ANTLR generate an AST.  ANTLR has good support for this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;options {
    output=AST;
    ASTLabelType=CommonTree;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Helper tokens for the grammar:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tokens {
    PLUS = '+';
    MINUS = '-';
    MULT = '*';
    DIV = '/';
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These next lines are important.  We want to generate classes &lt;code&gt;antlr_example.ExprLexer&lt;/code&gt; and &lt;code&gt;antlr_example.ExprParser&lt;/code&gt;, so we have to set our code up in the proper package, &lt;code&gt;antlr_example&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;This code includes both a parser and lexer, so you have to set the package for both in ANTLR.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@header        {package antlr_example;}
@lexer::header {package antlr_example;}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And then the grammar itself, which is simple:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;expr: term ((PLUS | MINUS)^ term)* ;
term: factor ((MULT | DIV)^ factor)* ;
factor: INT ;

INT :   '0'..'9'+ ;
WS  :   ( ' ' | '\t' | '\r' | '\n')+ {$channel=HIDDEN;} ;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One thing to note is &lt;code&gt;(PLUS | MINUS)^&lt;/code&gt;; the &lt;code&gt;^&lt;/code&gt; here tells ANTLR to treat &lt;code&gt;(PLUS | MINUS)&lt;/code&gt; as the parent of the node created for this rule.  This means the &lt;code&gt;term&lt;/code&gt; on either side will be children of this node.  We do the same for &lt;code&gt;MULT&lt;/code&gt; and &lt;code&gt;DIV&lt;/code&gt;.  This will give us a nice tree.  Otherwise you end up with a flat list of tokens.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{$channel=HIDDEN;}&lt;/code&gt; for the whitespace rule tells ANTLR to ignore whitespace, also useful.&lt;/p&gt;

&lt;p&gt;If you use ANTLR Works with this grammar, on the input &lt;code&gt;1 + 2 * 3 + 4&lt;/code&gt;, you can see that it works OK.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/random/antlr.png&quot; alt=&quot;ANTLR&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;That was an awful lot of boilerplate and setup and ugly syntax though.  See how much Clojure spoils you?  But for a simple grammar, it's not that much code.&lt;/p&gt;

&lt;p&gt;For a longer or more complex grammar, you might end up having to embed inline Java code into your ANTLR grammar.  But again, that's not the end of the world.&lt;/p&gt;

&lt;h1&gt;Compile everything&lt;/h1&gt;

&lt;p&gt;Once you write your grammar (presumably in ANTLR Works), you can generate the Java code and compile it thusly (run from your project's base directory):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ java -cp 'lib/*' org.antlr.Tool src/antlr_example/Expr.g
$ javac -cp 'lib/*' -d classes src/antlr_example/Expr{Lexer,Parser}.java
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first command should generate &lt;code&gt;src/antlr_example/ExprLexer.java&lt;/code&gt; and &lt;code&gt;src/antlr_example/ExprParser.java&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second command should spew tons of class files into &lt;code&gt;classes/&lt;/code&gt;.  Leiningen and other tools generally include &lt;code&gt;classes/&lt;/code&gt; on your &lt;code&gt;CLASSPATH&lt;/code&gt;, so that's a good place for them.  Again putting them here is a fairly arbitrary decision on my part.  You can put the source and class files anywhere, as long as the class files end up on your &lt;code&gt;CLASSPATH&lt;/code&gt; when you start Clojure.&lt;/p&gt;

&lt;p&gt;If you're really so lazy that you can't run these two commands, you could use ANTLR Works' built-in commands for compiling your grammar.  The only way I could find to invoke it was by starting a Debug session, which compiled my code as a side-effect.  And it spews files into places I don't want them, so I like the command line version better.&lt;/p&gt;

&lt;p&gt;You could probably also set up a leiningen task for this if you're re-running these commands a lot, or configure your build tool of choice to do the same.  I didn't bother.&lt;/p&gt;

&lt;p&gt;Now (re)start your REPL and let's write some Clojure.&lt;/p&gt;

&lt;h1&gt;Clojure code&lt;/h1&gt;

&lt;p&gt;It's pretty easy to consume an ANTLR parser from Clojure.  I'm putting the code below into &lt;code&gt;src/antlr_example/core.clj&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;First import the classes.  You probably need some ANTLR helper classes to set up the parser; you also need the classes you just generated.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(ns antlr-example.core
  (:import (org.antlr.runtime ANTLRStringStream
                              CommonTokenStream)
           (antlr_example ExprLexer ExprParser)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here's a function to parse a string using our new parser class:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defn parse-expr [s]
  (let [lexer (ExprLexer. (ANTLRStringStream. s))
        tokens (CommonTokenStream. lexer)
        parser (ExprParser. tokens)]
    (.expr parser)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;.expr&lt;/code&gt; is the name of the top-level rule we want to invoke from our grammar.  The rest is boilerplate; just plug in the proper class names.&lt;/p&gt;

&lt;p&gt;So let's see what we've got.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user&amp;gt; (in-ns 'antlr-example.core)
#&amp;lt;Namespace antlr-example.core&amp;gt;
antlr-example.core&amp;gt; (parse-expr &quot;1 + 2 * 3 + 4&quot;)
#&amp;lt;expr_return antlr_example.ExprParser$expr_return@3755e508&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Er, OK.  A good way to inspect Java objects is via &lt;code&gt;bean&lt;/code&gt;, so let's view the guts of this object:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;antlr-example.core&amp;gt; (bean *1)
{:tree #&amp;lt;CommonTree +&amp;gt;,
 :template nil,
 :stop #&amp;lt;CommonToken [@12,12:12='4',&amp;lt;8&amp;gt;,1:12]&amp;gt;,
 :start #&amp;lt;CommonToken [@0,0:0='1',&amp;lt;8&amp;gt;,1:0]&amp;gt;,
 :class antlr_example.ExprParser$expr_return}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I see.  We set up our grammar above to generate an AST, so &lt;code&gt;:tree&lt;/code&gt; on the bean will give us that.  This translates to &lt;code&gt;.getTree&lt;/code&gt; on the Java object.  So we can edit our function to call this for us, since we always want to have the tree.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defn parse-expr [s]
  (let [lexer (ExprLexer. (ANTLRStringStream. s))
        tokens (CommonTokenStream. lexer)
        parser (ExprParser. tokens)]
    (.getTree (.expr parser))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;antlr-example.core&amp;gt; (parse-expr &quot;1 + 2 * 3 + 4&quot;)
#&amp;lt;CommonTree +&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;OK, it appears we have one node of the tree, the root node.  Let's peak inside:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;antlr-example.core&amp;gt; (bean *1)
{:children #&amp;lt;ArrayList [+, 4]&amp;gt;,
 :childIndex -1,
 :parent nil,
 :text &quot;+&quot;,
 :nil false,
 :token #&amp;lt;CommonToken [@10,10:10='+',&amp;lt;4&amp;gt;,1:10]&amp;gt;,
 :class org.antlr.runtime.tree.CommonTree,
 :ancestors nil,
 :tokenStartIndex 0,
 :type 4,
 :childCount 2,
 :charPositionInLine 10,
 :tokenStopIndex 12,
 :line 1}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looks like our children are in &lt;code&gt;:children&lt;/code&gt; on the bean, which translates to &lt;code&gt;.getChildren&lt;/code&gt; on the Java object.  And we have &lt;code&gt;.getChildCount&lt;/code&gt; to count the children, and &lt;code&gt;.getText&lt;/code&gt; to get the text of the current node.  (We could've learned all of this by reading the javadocs for ANTLR too, but what fun is that?)&lt;/p&gt;

&lt;p&gt;Since we're dealing with a tree, we can use &lt;code&gt;tree-seq&lt;/code&gt; in Clojure to get a flat list of all the tokens in our text.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;tree-seq&lt;/code&gt; takes three arguments.  First a function that returns true if the node has children, false otherwise.  Second a function that returns the children for a node that has children.  Third the root node of our tree.&lt;/p&gt;

&lt;p&gt;That'll give us a seq of tree nodes.  So finally we call &lt;code&gt;.getText&lt;/code&gt; on all the resulting nodes in the list, to turn node objects into strings.&lt;/p&gt;

&lt;p&gt;Easy enough:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defn node-seq [x]
  (map #(.getText %)
   (tree-seq #(not (zero? (.getChildCount %)))
             #(.getChildren %)
             x)))

antlr-example.core&amp;gt; (node-seq (parse-expr &quot;1 + 2 * 3 + 4&quot;))
(&quot;+&quot; &quot;+&quot; &quot;1&quot; &quot;*&quot; &quot;2&quot; &quot;3&quot; &quot;4&quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But that's no good.  We lost our tree structure.  &lt;/p&gt;

&lt;p&gt;We'd rather have something like nested vectors or lists.  It's easy enough to roll something by hand.  This should do it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defn AST [node]
  (if (zero? (.getChildCount node))
    (.getText node)
    (let [children (map AST (.getChildren node))
          txt (.getText node)]
      (if txt
        (cons txt children)
        children))))

antlr-example.core&amp;gt; (AST (parse-expr &quot;1 + 2 * 3 + 4&quot;))
(&quot;+&quot; (&quot;+&quot; &quot;1&quot; (&quot;*&quot; &quot;2&quot; &quot;3&quot;)) &quot;4&quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That's better, but it's a list of strings.  These strings all happen to be literal representations of Clojure objects, so a call to &lt;code&gt;read-string&lt;/code&gt; in the proper places should give us something we can work with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defn AST [node]
  (if (zero? (.getChildCount node))
    (read-string (.getText node))
    (let [children (map AST (.getChildren node))
          txt (read-string (.getText node))]
      (if txt
        (apply list txt children)
        children))))

antlr-example.core&amp;gt; (AST (parse-expr &quot;1 + 2 * 3 + 4&quot;))
(+ (+ 1 (* 2 3)) 4)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Hey look, now we have something we can evaluate.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;antlr-example.core&amp;gt; (eval *1)
11
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Yeah I kind of planned that ahead.  If you had a more complex grammar, you might not get away with something quite this easy.&lt;/p&gt;

&lt;h1&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;The advantage of ANTLR is that it's mature, widely used, actively developed, and well-documented.  (There's a whole book about ANTLR, &lt;em&gt;The Definitive ANTLR Reference&lt;/em&gt; by Terence Parr.)  There's also a lot of tooling available for it, not just ANTLR Works, but plugins for other Java IDEs.&lt;/p&gt;

&lt;p&gt;The disadvantage is that it's a Java library, and as always, there will be some friction when consuming it from Clojure.  But it's not that bad.&lt;/p&gt;

&lt;p&gt;For pure-Clojure parser generator alternatives, there are &lt;a href=&quot;http://github.com/joshua-choi/fnparse&quot;&gt;fnparse&lt;/a&gt; and &lt;a href=&quot;http://github.com/cypher/clojure-pg&quot;&gt;clojure-pg&lt;/a&gt;, neither of which I've tried much.&lt;/p&gt;</description></item><item><title>Vim and plaintext data files</title><link>http://briancarper.net/blog/552/vim-and-plaintext-data-files</link><guid>http://briancarper.net/blog/552/vim-and-plaintext-data-files</guid><pubDate>Mon, 12 Jul 2010 10:42:48 -0700</pubDate><description>&lt;p&gt;I use Vim to work with plaintext datasets.  Here are some habits and code snippets I've picked up which make data files a bit easier to deal with.&lt;/p&gt;

&lt;!--more Read on for Vim trickery...--&gt;

&lt;h1&gt;Tab-delimited files&lt;/h1&gt;

&lt;p&gt;If you have a file full of tab-separated values, the columns may not line up very well due to the variable display-length of tabs.  You'll often end up with something that looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;foo bar
longvaluehere    quux
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But things will line up if you set tabstop to a value greater than the longest column.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:setlocal tabstop=16
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then it'll look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;foo             bar
longvaluehere   quux
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can use visual-block mode to add and remove columns easily.&lt;/p&gt;

&lt;p&gt;If you're working with TSV files, you'll want to set the &lt;code&gt;listchars&lt;/code&gt; option in such a way that tabs are displayed specially.  I use this, stolen/adapted from &lt;a href=&quot;http://github.com/ciaranm/dotfiles-ciaranm/blob/master/vimrc&quot;&gt;here&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:set listchars=eol:\ ,tab:»-,trail:·,precedes:…,extends:…,nbsp:‗
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Remember also that you can insert a literal tab into your file no matter what you have &lt;code&gt;expandtab&lt;/code&gt; set to, by hitting &lt;code&gt;CTRL-v&lt;/code&gt; first.  &lt;/p&gt;

&lt;p&gt;Those things are just about all I need to work with TSV files comfortably.&lt;/p&gt;

&lt;h1&gt;Dealing with long lines&lt;/h1&gt;

&lt;p&gt;Sometimes I'll run some data through a statistics program and it'll tell me &quot;Invalid character at line 576, column 9438&quot;.  How do you jump to that location in your data?  Easily enough in normal mode:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;576G9438|
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;G&lt;/code&gt; jumps you to a line, &lt;code&gt;|&lt;/code&gt; jumps you to a column.  A good mnemonic to remember these: &quot;g = goto line&quot; and &quot;| looks like a column&quot;.&lt;/p&gt;

&lt;p&gt;Suppose (like me) you turn scrollbars off in your GVim window, or you work from a terminal.  How do you scroll horizontally?  With &lt;code&gt;zL&lt;/code&gt; and &lt;code&gt;zH&lt;/code&gt;.  But those are hard to type and harder to remember, so I map them to CTRL+Shift+Right and Left.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;nnoremap &amp;lt;C-S-Right&amp;gt; zL
nnoremap &amp;lt;C-S-Left&amp;gt; zH
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you have a lot of long lines, it behooves you to set up a mapping to turn soft line-wrapping on and off quickly:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;nnoremap &amp;lt;Leader&amp;gt;w :setlocal nowrap!&amp;lt;CR&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can jump back and forth between wrapped and unwrapped views of your data via &lt;code&gt;\w&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;Diffs on long lines&lt;/h1&gt;

&lt;p&gt;When using vimdiff, &lt;code&gt;]c&lt;/code&gt; and &lt;code&gt;[c&lt;/code&gt; will jump you to lines that contain differences.  But if your lines are thousands of characters long, it doesn't always help to know that two lines are different: you want to know where they differ.  &lt;code&gt;]c&lt;/code&gt; puts you at the first column in the line, which isn't helpful.&lt;/p&gt;

&lt;p&gt;This function will jump you to the column where the difference between two lines starts:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function! IsDiff(col)
    let hlID = diff_hlID(&quot;.&quot;, a:col)
    return hlID == 24
endfunction

function! FindDiffOnLine()
    let c = 1
    while c &amp;lt; col(&quot;$&quot;)
        if IsDiff(c)
            call cursor(&quot;.&quot;, c)
            return
        endif
        let c += 1
    endwhile
endfunction
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This seems fragile, so if you know a better way please leave a comment and let me know.&lt;/p&gt;

&lt;h1&gt;Diff a buffer against itself&lt;/h1&gt;

&lt;p&gt;Sometimes I have files with lots of lines of data, and some lines might be duplicate.  It's really easy to get rid of the duplicate lines:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:sort u
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But what if I want to see what lines were just removed?  I don't know of a built-in way to do it, so I use this simple function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function! MarkDuplicateLines()
    let x = {}
    let count_dupes = 0
    for lnum in range(1, line('$'))
        let line = getline(lnum)
        if has_key(x, line)
            exe lnum . 'norm I *****'
            let count_dupes += 1
        else
            let x[line] = 1
        endif
    endfor
    echomsg count_dupes . &quot; dupe(s) found&quot;
endfunction
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That'll put a bunch of asterisks at the beginning of every line that's a duplicate of a previous line.&lt;/p&gt;

&lt;h1&gt;Fix broken punctuation&lt;/h1&gt;

&lt;p&gt;Ever had to work with textual data that someone else sent you in MS Word or Excel?  I have.  It hurts.  If you copy/paste from an MS document into a plaintext file, you'll probably end up with a bunch of funky unreadable or undisplayable characters.&lt;/p&gt;

&lt;p&gt;This function fixes the most common Word-spawned &quot;smart&quot; punctuation characters that you're likely to run across:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function! FixInvisiblePunctuation()
    silent! %s/\%u2018/'/g
    silent! %s/\%u2019/'/g
    silent! %s/\%u2026/.../g
    silent! %s/\%uf0e0/-&amp;gt;/g
    silent! %s/\%u0092/'/g
    silent! %s/\%u2013/--/g
    silent! %s/\%u2014/--/g
    silent! %s/\%u201C/&quot;/g
    silent! %s/\%u201D/&quot;/g
    silent! %s/\%u0052\%u20ac\%u2122/'/g
    silent! %s/\%ua0/ /g
    retab
endfunction
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function was built up during years of choking on special characters that crept up in my data, so I'm sure I'll be adding more to it in the future.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\%u####&lt;/code&gt; here is a regex escape to represent a character as a hexidecimal Unicode codepoint.&lt;/p&gt;

&lt;p&gt;Many of these characters show up as blank (whitespace) if you lack the font to display them.  If you ever run across a character you can't see and you want to inspect it, put the cursor on it and do&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:ascii
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That's a bad name for the command, since it works on Unicode characters too.  That'll give you the numeric code you can use in a regex to replace them all with something sane.&lt;/p&gt;</description></item><item><title>In which border-radius is abused</title><link>http://briancarper.net/blog/548/in-which-border-radius-is-abused</link><guid>http://briancarper.net/blog/548/in-which-border-radius-is-abused</guid><pubDate>Mon, 05 Jul 2010 15:54:24 -0700</pubDate><description>&lt;p&gt;I threw together a new blog layout today.  I scaled back the level of cows a bit (just a bit, don't worry!) Criticism / feedback welcome.  (IE-related feedback should be dropped off in the circular file by my desk.)&lt;/p&gt;

&lt;p&gt;In what is surely a prelude to the future of the internets, I'm abusing &lt;code&gt;border-radius&lt;/code&gt; pretty heavily in this layout.  &lt;code&gt;border-radius&lt;/code&gt; is likely to become the new &lt;code&gt;marquee&lt;/code&gt; HTML tag or &lt;code&gt;text-shadow&lt;/code&gt; CSS attribute: Maybe an OK idea at first, but then everyone uses it so much it makes your eyes bleed.&lt;/p&gt;

&lt;p&gt;So I figured I'd best get my border-radiusing in early while it's still cool.  IE8 users, you still get pointy corners. Sucks to be you.&lt;/p&gt;

&lt;p&gt;Also, if you have any ideas for features I should implement for &lt;a href=&quot;http://github.com/briancarper/cow-blog&quot;&gt;cow-blog&lt;/a&gt;, please let me know.  I've been crawling the internet looking at blogs for ideas of things to implement and features to steal, but I'm running out of ideas.  It does everything I want now, but I'm not a reader.&lt;/p&gt;</description></item><item><title>Goodbye Tokyo Cabinet, hello PostgreSQL?</title><link>http://briancarper.net/blog/545/goodbye-tokyo-cabinet-hello-postgresql</link><guid>http://briancarper.net/blog/545/goodbye-tokyo-cabinet-hello-postgresql</guid><pubDate>Tue, 29 Jun 2010 11:32:58 -0700</pubDate><description>&lt;p&gt;The first version of this blog used MySQL; then I switched to Tokyo Cabinet.  But now I've switched back to PostgreSQL.  Here's why.&lt;/p&gt;

&lt;!--more Read on.--&gt;

&lt;h1&gt;Why did I switch to TC to begin with?&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;There weren't any good ORM-type libraries for Clojure at the time (over a year ago).  So there was a bit of an impedance mismatch trying to query and work with my data.  In the DB I have separate tables for posts, comments, tags, categories.  But 90% of the time I want to fetch a post and end up in Clojure with all related tags, comments etc.  (A JOIN won't work here without a lot of work to un-mangle the results; you usually need multiple queries.)  &lt;/p&gt;

&lt;p&gt;With TC I could store anything in the DB, so I just dumped a post into the DB as serialized hash-maps with all the comments, tags, and categories as sub-keys.  So querying was easy.  (Or was it?  More below.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;MySQL was really slow. This is largely because my queries were terrible, as I tried to solve the problem from #1 via brute force.  TC on the other hand is fast.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I tried to solve #2 by using a Clojure ref as a cache.  But tying the STM to a database's transaction system is as far as I know difficult or impossible right now (per many threads on the mailing list).  I had a lot of potential race conditions, which (as far as I know) never bit me, but probably would've eventually.  I had to deal with keeping the cache up-to-date as comments were posted and posts were added and deleted and renamed.  Remember:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;&quot;There are only two hard problems in Computer Science: cache invalidation and naming things.&quot; --Phil Karlton&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;So why did I stop using TC?&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;I have no idea how to use a key/value store database properly.  TC will take anything you dump into it, which is both a strength and a weakness.&lt;/p&gt;

&lt;p&gt;There's a lot of crap you have to do by hand that a proper database does for you.  Consider checking for null values, for example; I ended up with a lot of &lt;code&gt;nil&lt;/code&gt;s in my data because my validations weren't 100% foolproof, or because I imported data via code that didn't run the validations and I never noticed..  Or enforcing uniqueness of values; I had tag objects in the database with the same key but different values (due to capitalization differences), which screwed up a lot of stuff.&lt;/p&gt;

&lt;p&gt;On the other hand, there's a lot of information about how to use RDBMS properly, and I have a lot of experience with it already.  Constraints are easy to set up.  Columns have types, which is nice.  (Strange that I gravitate toward statically-type databases while I gravitate toward dynamically-typed programming languages.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I have to compile and install Tokyo Cabinet by hand on my Linux distro.  It's probably not worth distro maintainers to maintain a package that so few people use.  MySQL and PostgreSQL have lots of people working on keeping them running OK on most Linux distros.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Some kinds of queries were still awkward in TC.  &quot;Give me post X&quot; was great: I'd also get all the tags, categories etc. for free.  But then how do you query to get all tags across all posts?  Or all comments?  Fetch all the posts and iterate over them, collecting their tags, then uniquify the resulting list?  Not so pretty, and not so fast.  So I was back to caching again, which still gave me nightmares about race conditions and dirty data.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;So now why am I using an RDBMS again?&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;An RDBMS is exactly what I really need, if I could just query the thing concisely and get it to run fast.  Thankfully there are some ORM-like libraries for Clojure in the works nowadays, already usable for a hobby project like this blog.  There are &lt;a href=&quot;http://github.com/duelinmarkers/clj-record&quot;&gt;clj-record&lt;/a&gt;, &lt;a href=&quot;http://github.com/brentonashworth/carte&quot;&gt;Carte&lt;/a&gt;, &lt;a href=&quot;http://gitorious.org/clojureql/&quot;&gt;ClojureQL&lt;/a&gt;, and my own &lt;a href=&quot;http://github.com/briancarper/oyako&quot;&gt;Oyako&lt;/a&gt;, and possibly others in the works.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For my tiny blog's database, Oyako gives me slightly slower performance than TC, but along the same order of magnitude, which is good enough.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Via Oyako I can (fairly concisely) fetch posts and get the associated tags, comments etc.  But I can also easily fetch all tags, or all comments, since they're in their own tables.  The &quot;relational&quot; part of RDBMS does come in handy sometimes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;Summary version&lt;/h1&gt;

&lt;p&gt;I switched to TC to begin with because I was using SQL wrong, and it was too slow and clumsy.  Once I figured out how to use SQL correctly, it was a no-brainer to go back.&lt;/p&gt;</description></item><item><title>Introducing Gaka</title><link>http://briancarper.net/blog/543/introducing-gaka</link><guid>http://briancarper.net/blog/543/introducing-gaka</guid><pubDate>Mon, 28 Jun 2010 17:59:26 -0700</pubDate><description>&lt;p&gt;The CSS for &lt;a href=&quot;http://github.com/briancarper/cow-blog&quot;&gt;my blog&lt;/a&gt; is now being generated via &lt;a href=&quot;http://github.com/briancarper/gaka&quot;&gt;gaka&lt;/a&gt;, a CSS-generating library I wrote this afternoon.  It's extremely simple, but it got the job done for me.  I turned around 600 lines of CSS into around 250 lines of Clojure without much effort.  It looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user&amp;gt; (require '(gaka [core :as gaka]))
nil
user&amp;gt; (def rules [:div#foo
                  :margin &quot;0px&quot;
                  [:span.bar
                   :color &quot;black&quot;
                   :font-weight &quot;bold&quot;
                   [:a:hover
                    :text-decoration &quot;none&quot;]]])
#'user/rules
user&amp;gt; (println (gaka/css rules))
div#foo {
  margin: 0px;}

  div#foo span.bar {
    color: black;
    font-weight: bold;}

    div#foo span.bar a:hover {
      text-decoration: none;}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Gaka is partly inspired by &lt;a href=&quot;http://sass-lang.com/&quot;&gt;Sass&lt;/a&gt;, which I found very pleasant to work with recently.  And it's partly inspired by &lt;a href=&quot;http://github.com/weavejester/hiccup&quot;&gt;Hiccup&lt;/a&gt;, which is a delicious way to generate HTML in Clojure.  &lt;/p&gt;

&lt;p&gt;There's more info and more examples on &lt;a href=&quot;http://github.com/briancarper/gaka&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>New blog engine up and running</title><link>http://briancarper.net/blog/542/new-blog-engine-up-and-running</link><guid>http://briancarper.net/blog/542/new-blog-engine-up-and-running</guid><pubDate>Wed, 23 Jun 2010 15:48:24 -0700</pubDate><description>&lt;p&gt;Well, my new blog is up and running.  Sorry for the temporary lack of cows in my layout.  I'm dogfood-testing the blog engine in a fairly vanilla state until I work out some of the bugs.  This layout is based upon &lt;a href=&quot;http://shaheeilyas.com/archives/barecity/&quot;&gt;barecity&lt;/a&gt;, a minimalist Wordpress theme that I adapted easily enough to my blog.&lt;/p&gt;

&lt;p&gt;As a bonus, I applied a dirty hack to my RSS feed that I think should help avoid screwing up people's RSS readers with duplicate entries.&lt;/p&gt;

&lt;p&gt;I'll write again soon with some info about the blog engine and some things I learned writing it.&lt;/p&gt;

&lt;p&gt;(As mentioned previously, &lt;a href=&quot;http://github.com/briancarper/cow-blog/tree/0.2.0&quot;&gt;here's the code&lt;/a&gt;.)&lt;/p&gt;</description></item><item><title>Breaking links is easy to do</title><link>http://briancarper.net/blog/breaking-links-is-easy-to-do</link><guid>http://briancarper.net/blog/breaking-links-is-easy-to-do</guid><pubDate>Tue, 22 Jun 2010 23:23:27 -0700</pubDate><description>&lt;p&gt;I apologize in advance to everyone who subscribes to my blog's RSS feed, but this week your RSS reader is probably going to suddenly find 25 &quot;new&quot; posts from me.&lt;/p&gt;

&lt;p&gt;My blog currently uses &lt;code&gt;/blog/title&lt;/code&gt; as the URL scheme, with similar URLs for categories and tags etc.  Soon, I'm probably going to change it to &lt;code&gt;/blog/123/title&lt;/code&gt;, as part of the impending release of version 0.2 of my blog engine.  (The code-in-progress is in a &lt;a href=&quot;http://github.com/briancarper/cow-blog/tree/0.2.0&quot;&gt;branch on github&lt;/a&gt;, for the daring and foolish among you.)&lt;/p&gt;

&lt;p&gt;This way, I can change the title of a post without breaking everything.  I have heretofore lacked this ability.  It's easy to code, you just tell Compojure to ignore everything after the number in a route.  Something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defroutes foo
  (GET [&quot;/blog/:id:etc&quot; :id #&quot;\d+&quot; :etc #&quot;(/[^/]*)?&quot;] [id]
    (pages/post-page id)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It's only a few lines of code to change, but the ramifications are widespread.  It'll instantly break every link to my blog, for example.  At least it's pretty easy to set up a bunch of redirects in Compojure to avoid that.  I think this'll work:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require (blog [db :as db]
               [link :as link])
         (oyako [core :as oyako])
         (ring.util [response :as response]))

(defn redirect-post [name]
  (when-let [post (oyako/fetch-one db/posts :url name)]
    (response/redirect (link/url post))))

(defroutes redirect-routes
  (GET [&quot;/blog/:name&quot; :name #&quot;[^/]+$&quot;] [name]
    (redirect-post name)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(&lt;a href=&quot;http://github.com/briancarper/oyako&quot;&gt;Oyako&lt;/a&gt; here is the experimental ORM-like library I'm using to interface with PostgreSQL nowadays, having ditched Tokyo Cabinet.)&lt;/p&gt;

&lt;p&gt;Changing my URL scheme is also going to mess up RSS though, because I (foolishly) used post URLs as the GUIDs in my RSS feed up to this point.  This problem I don't know how to avoid.  I might reduce the number of posts included in my feed temporarily, to limit the damage.&lt;/p&gt;</description></item><item><title>Vim regex - remove kind-of-matching lines</title><link>http://briancarper.net/blog/vim-regex---remove-kind-of-matching-lines</link><guid>http://briancarper.net/blog/vim-regex---remove-kind-of-matching-lines</guid><pubDate>Sun, 20 Jun 2010 22:49:19 -0700</pubDate><description>&lt;p&gt;I have a file where every line starts with a number (followed by whitespace and a bunch of other stuff).  Every number appears on either one or two lines, and if two, the second line always has a &lt;code&gt;b&lt;/code&gt; after the number.&lt;/p&gt;

&lt;p&gt;I need to delete every line for which there's a corresponding &lt;code&gt;b&lt;/code&gt; line.  But if there's no corresponding &lt;code&gt;b&lt;/code&gt; line I want to leave the original line there.&lt;/p&gt;

&lt;p&gt;Before:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;123 foo bar
456 blarg
789 quux
123b foo baz
789b quux blurble
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;123b foo baz
456 blarg
789b quux blurble
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Except in my real file, I have a thousand lines and it'd take a year to do by hand.  Vim to the rescue:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:sort
:%s/^\v((\d+).*\n)(\2b.*)/\3/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And that is why Vim is awesome.  Can you think of a shorter way to do this, in Vim or Emacs?&lt;/p&gt;</description></item><item><title>Emacs creating zombie buffers</title><link>http://briancarper.net/blog/emacs-creating-zombie-buffers</link><guid>http://briancarper.net/blog/emacs-creating-zombie-buffers</guid><pubDate>Mon, 14 Jun 2010 15:44:18 -0700</pubDate><description>&lt;p&gt;Note to self:&lt;/p&gt;

&lt;p&gt;Using bleeding-edge swank-clojure and Slime, Emacs was creating buffers that didn't point to any file, every time &lt;code&gt;C-c C-k&lt;/code&gt; wanted to display warnings.  This was really quite annoying as it resulted in a mess of empty buffers being created.  This fixed it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(setq slime-highlight-compiler-notes nil)
&lt;/code&gt;&lt;/pre&gt;</description></item><item><title>Clojure, from a Ruby perspective</title><link>http://briancarper.net/blog/clojure-from-a-ruby-perspective</link><guid>http://briancarper.net/blog/clojure-from-a-ruby-perspective</guid><pubDate>Wed, 09 Jun 2010 13:22:58 -0700</pubDate><description>&lt;p&gt;Fogus' recent article &quot;&lt;a href=&quot;http://blog.fogus.me/2010/06/09/clojure-rb/&quot;&gt;clojure.rb&lt;/a&gt;&quot; speculates about why there seem to be so many Ruby users adopting Clojure.  As a Ruby user who adopted Clojure, I figured I'd write about my experiences.&lt;/p&gt;

&lt;p&gt;What do Ruby and Clojure have in common, that would attract a Rubyist to Clojure?  A lot.  Obviously, this is somewhat subjective and I don't expect anyone else to agree, but this is what did it for me.&lt;/p&gt;

&lt;!--more Ruby and Clojure comparison ahead--&gt;

&lt;h1&gt;Semantic consistency&lt;/h1&gt;

&lt;p&gt;In Ruby, everything is an object.  It makes it simple to write code without worrying much about what kind of thing you have.  &lt;code&gt;foo.some_method(1,2,3)&lt;/code&gt; will generally work for any foo.&lt;/p&gt;

&lt;p&gt;In Clojure, everything is &lt;em&gt;not&lt;/em&gt; an object, specifically because it inherits primitives from Java land (though this doesn't hurt much in the kind of everyday use I put Clojure to).  But also because Clojure by design doesn't even attempt to be object-oriented.&lt;/p&gt;

&lt;p&gt;Clojure does have abstractions though.  For example there's an abstraction that says &quot;this thing can be called like a function&quot;.  And then you can treat any callable-thing as a function without worrying about what it is.&lt;/p&gt;

&lt;p&gt;In Ruby, a lot of things are Enumerable, which means you can do &lt;code&gt;foo.each{}&lt;/code&gt; and other similar things for a lot of different types of foo.  Clojure has something similar with its &lt;code&gt;seq&lt;/code&gt; abstraction.  Similarly, most Clojure data types and many Java ones are &lt;code&gt;seq&lt;/code&gt;-able, and most built-in core functions can iterate over the guts of various things using &lt;code&gt;seq&lt;/code&gt;s.  This includes regex matches, strings, directories of files, and so on.&lt;/p&gt;

&lt;p&gt;Ruby-style OOP brings a lot of complexity and baggage which Clojure avoids by not being OOP.  For example (ignoring Java for the moment), in Clojure land you don't have to worry about a member being public/private/protected, and there are few times when you have to worry about inheritance and class hierarchies.  (And there's the whole thread-safety thing.)  In Clojure data is stupid and immutable, and functions are just things that take input and give output, usually side-effect free.  The separation is clean and this results in programs that are very easy to reason about.&lt;/p&gt;

&lt;p&gt;Another example of consistency: Expressions.  In both Ruby and Clojure, everything has a value.  Things that are &quot;statements&quot; in other languages are instead expressions that return something.  This alone makes a lot of programs just a little bit better/easier to write.&lt;/p&gt;

&lt;h1&gt;Aesthetics&lt;/h1&gt;

&lt;p&gt;Like Ruby, Clojure code tends to be terse and expressive.&lt;/p&gt;

&lt;p&gt;Ruby reads like poetry, because it's mostly words and not so much punctuation.  In Ruby, you have none of Perl's sigils, very little of C's semi-colon line-endings and curly-delimited blocks.  Especially when you start omitting optional parentheses, it has a very minimalistic vibe which is appealing to many.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    def foo(bar)
      bar.each do |x|
        puts x
      end
    end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Clojure's s-expressions are another story, of course.  Some love them, some hate them.  Personally, I love them.  The tired old trope about Lispers not paying attention to parentheses is true; after a while they blend into the background.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    (defn foo [bar]
      (doseq [x bar]
        (prn x)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What I see:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    defn foo [bar]
      doseq [x bar]
        prn x
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Clojure has the same minimalistic feel to it, in my eyes.  It also helps that so many Clojure function names are short and concise.  And being able to use punctuation like &lt;code&gt;?&lt;/code&gt; and &lt;code&gt;!&lt;/code&gt; in variable/function names (&lt;code&gt;true?&lt;/code&gt; and &lt;code&gt;false?&lt;/code&gt; are function names), hyphens instead of underscores... these things help Clojure read smoothly.&lt;/p&gt;

&lt;p&gt;But along with aesthetics, in Clojure you get the benefits of s-exps: no order of operations to deal with, and absolute consistency.  Everything is &lt;code&gt;(function param1 param2 param3)&lt;/code&gt;.  Look at how many syntax rules you have to memorize for the Ruby code above to make sense.  Dot means method call, blocks are &lt;code&gt;do&lt;/code&gt;/&lt;code&gt;end&lt;/code&gt; delimited and have those weird pipes in there, etc. &lt;/p&gt;

&lt;h1&gt;Literals and syntax sugar&lt;/h1&gt;

&lt;p&gt;Ruby has literal syntax for many types.  This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;:symbols&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[arrays]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;{:hash =&amp;gt; :maps}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/regex/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;do block end&lt;/code&gt; and &lt;code&gt;{block}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This really is a big deal.  I'm spoiled and I can't use a language without these things nowadays.  It saves a ton of typing and it makes those things stand out in the code, making it both easier to write and to read.  I use those structures all the time in every program I write, so they should have a terse representation.&lt;/p&gt;

&lt;p&gt;Clojure has literal support for the same types as Ruby, and they even look mostly the same (with &lt;code&gt;#&quot;regex&quot;&lt;/code&gt; being a change I can live with).  And then it also has (among others):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;#{sets}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#(function-literals %)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;'(quoted forms)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;`(quasi-quoted ~forms)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Is this a contradiction of my last point?  What happened to s-expressions and consistency?  Well, in Clojure, the reader shortcuts are just sugar that reduce to s-exps.  You can avoid all use of that sugar if you hate it.  &lt;code&gt;(hash-map :key &quot;val&quot;)&lt;/code&gt;, &lt;code&gt;(vector 1 2 3)&lt;/code&gt;, &lt;code&gt;(quote foo)&lt;/code&gt; etc.&lt;/p&gt;

&lt;p&gt;But more importantly, let's draw a(n arbitrary) distinction between &quot;good&quot; syntax and &quot;bad&quot; syntax.  &lt;/p&gt;

&lt;p&gt;Clojure's reader-macro sugar makes your code shorter, but doesn't change the structure of your code.  Take a function call &lt;code&gt;(f x y z)&lt;/code&gt;, and you can always substitute a vector or regex literal or quoted form into it.  &lt;code&gt;(f [vector] #{set} #&quot;regex&quot;)&lt;/code&gt;.  The syntax sugar is very local, very self-contained.  It doesn't leak into the surrounding code.  And of course you can combine them in nearly arbitrary ways:  &lt;code&gt;'[quoted vector]&lt;/code&gt;, &lt;code&gt;{:hash-map-containing-a #{:set 'of #(functions)}}&lt;/code&gt;.  This is &quot;good&quot; syntax.&lt;/p&gt;

&lt;p&gt;Compare this to things like the &lt;code&gt;x ? y : z&lt;/code&gt; construct, or heredocs.  These things are not as orthogonal.  They not only mean something on the &quot;inside&quot;, they also influence and interact with the code before and after them, thanks to precedence rules and special parsing rules.  Can you stick a heredoc in the middle of a function call?  Maybe (I don't even know), but have fun with the indentation and line-breaks if so.  When should you use &lt;code&gt;do&lt;/code&gt;/&lt;code&gt;end&lt;/code&gt; and when should you use &lt;code&gt;{}&lt;/code&gt; for blocks?  When do you need parens around your ternary if-then-else construct and when don't you?  When do you need to use &lt;code&gt;and&lt;/code&gt; and when &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;?  That's the &quot;bad&quot; kind of syntax.  Sometimes, maybe even most of the time, it makes your code shorter, but there are a lot of rules to memorize and you never know when you'll be bitten.&lt;/p&gt;

&lt;p&gt;Clojure largely avoids the &quot;bad&quot; syntax while taking advantage of the &quot;good&quot;.  Reader macros make your code shorter and visually easier to scan, but they rarely require you to do backflips to get your code to compile or run properly.&lt;/p&gt;

&lt;h1&gt;First-class functions&lt;/h1&gt;

&lt;p&gt;Ruby's blocks and &lt;code&gt;yield&lt;/code&gt; and friends let you deal with first-class functions.  This is a huge step in the Lisp direction already, and it's one of the things that makes Ruby great.  But there are limits.  Blocks use funky, special syntax, and in idiomatic Ruby, you will pass around only one block per method.  There's the whole &lt;code&gt;lambda&lt;/code&gt; and &lt;code&gt;proc&lt;/code&gt; mess, and then there are methods-as-objects which are different still.  And a lot of Ruby just calls &lt;code&gt;.send&lt;/code&gt; on an object and passes in a method name as a symbol.&lt;/p&gt;

&lt;p&gt;Being a Lisp, Clojure takes this a bit further.  First-class functions are ingrained in nearly everything you do in Clojure.  And they are easy to define and easy to call.  Define &lt;code&gt;f&lt;/code&gt; via &lt;code&gt;defn&lt;/code&gt; (to make it top-level), &lt;code&gt;fn&lt;/code&gt; (for a local function), or &lt;code&gt;#()&lt;/code&gt; (sugar for &lt;code&gt;fn&lt;/code&gt;), and then call it like &lt;code&gt;(f)&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Clojure also takes advantage of some functional-programming mainstays like &lt;code&gt;partial&lt;/code&gt; and &lt;code&gt;complement&lt;/code&gt; and &lt;code&gt;comp&lt;/code&gt;(osition).  We're not in full-blown Haskell territory, but it's a lot more FP than idiomatic Ruby.&lt;/p&gt;

&lt;p&gt;And hash-maps, vectors, sets, keywords, and symbols are also callable as functions in Clojure.  &lt;code&gt;({:foo 1} :foo)&lt;/code&gt; =&gt; &lt;code&gt;1&lt;/code&gt;.  Many things can be treated as functions.&lt;/p&gt;

&lt;h1&gt;Metaprogramming&lt;/h1&gt;

&lt;p&gt;In Ruby you can mess with the innards of any class you want.  There are facilities for defining methods dynamically, opening and inspecting classes at runtime, catch-all handlers for undefined methods, and all kinds of other dark magic.  But again there are limits... Ruby needs to make use of &lt;code&gt;eval&lt;/code&gt; to get certain things done.  And monkey-patching is a shotgun aimed at your foot.&lt;/p&gt;

&lt;p&gt;Well, if you like metaprogramming, Lisp macros are top of the line.  You can abstract away boilerplate with a vengeance.  Macros are the ultimate application of DRY.&lt;/p&gt;

&lt;p&gt;Clojure doesn't deal much with classes, so there isn't much of that kind of introspection, but the Lisp principle of code-as-data enables a kind of introspection that you won't find in Ruby.  The line between compile-time and run-time is very blurry, which enables all kinds of magic.&lt;/p&gt;

&lt;p&gt;Java itself does offer Ruby-style reflection and such, if you need it, but you won't often, while in Clojure land.&lt;/p&gt;

&lt;p&gt;Multimethods (and soon, protocols and &lt;code&gt;defrecord&lt;/code&gt;) let you avoid monkey-patching and get some of the same kinds of &quot;extend a class&quot; things done in a &lt;a href=&quot;http://kirindave.tumblr.com/post/658770511/monkey-patching-gorilla-engineering-protocols-in&quot;&gt;saner and safer way&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;So?&lt;/h1&gt;

&lt;p&gt;Fogus suspects:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Ruby programmers being the adventurous lot to begin with, are not satisfied with “halfway to Lisp”. Instead, they want it all.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is true in my case.  I like Ruby largely insofar as it borrowed and adapted many great features of Lisp.  It only makes sense that I would like Clojure, which takes most of those things one step further.  Clojure in particular, as a &quot;modern&quot; Lisp with vaguely Ruby-like syntax in certain places, is an obvious choice.&lt;/p&gt;

&lt;p&gt;On top of that, Clojure is fast, thanks to the JVM.  Ruby has JRuby too, but vanilla Ruby is not known for its speed.  Clojure integrates with a REPL in a way that Ruby really doesn't, making interactive development enjoyable.  Clojure is a compiled language, which has benefits for deployment.  And again, there's the whole thread-safety thing.  Clojure is awesome for writing sane, safe multi-threaded programs.  These things are rather appealing.&lt;/p&gt;

&lt;p&gt;I do still use Ruby though.  Ruby is great for scripting, Clojure not so much, thanks to the JVM startup time, among other things.  Ruby can be banged out quickly in any editor, but Clojure isn't much fun to edit in any editor that lacks good paren-matching support and REPL integration.  &lt;/p&gt;

&lt;p&gt;Rubygems offers dead-simple install of a ton of libraries, whereas Clojure is still working out the details of a standard build tool and install tool.  Ruby has a library for anything, and while Clojure can use Java libraries, Java libraries tend to be huge and feature-rich, sometimes too huge for one-off tasks where a small Ruby library is a perfect fit.&lt;/p&gt;</description></item><item><title>Emacs isn't for everyone</title><link>http://briancarper.net/blog/emacs-isnt-for-everyone</link><guid>http://briancarper.net/blog/emacs-isnt-for-everyone</guid><pubDate>Tue, 08 Jun 2010 11:26:15 -0700</pubDate><description>&lt;p&gt;Chas Emerick recently posted the results of his &lt;a href=&quot;http://muckandbrass.com/web/display/~cemerick/2010/06/07/Results+from+the+State+of+Clojure%252C+Summer+2010+Survey&quot;&gt;State of Clojure survey&lt;/a&gt;.  It turns out that the (self-selected) group of Clojure-using respondents happen to prefer Emacs as their IDE of choice, eclipsing all other editors by a large margin.&lt;/p&gt;

&lt;p&gt;Chas then has this to say:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I continue to maintain that broad acceptance and usage of Clojure will require that there be top-notch development environments for it that mere mortals can use and not be intimidated by...and IMO, while emacs is hugely capable, I think it falls down badly on a number of counts related to usability, community/ecosystem, and interoperability.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As an avid, die-hard Vim and Emacs user for life, I'm going to agree.&lt;/p&gt;

&lt;!--more Read on --&gt;

&lt;h1&gt;Mere mortals?&lt;/h1&gt;

&lt;p&gt;Emacs isn't difficult to learn.  Not in the sense of requiring skill or cleverness.  It is however extremely painful to learn.  I think there's a difference.&lt;/p&gt;

&lt;p&gt;The key word is &lt;em&gt;tedium&lt;/em&gt;.  Learning Emacs is a long process of rote memorization and repetition of commands until they become muscle memory.  If you're smart enough to write programs, you can learn Emacs.  You just have to keep dumping time into the task until you become comfortable.&lt;/p&gt;

&lt;p&gt;Until you're comfortable, you face the unpleasant task of un-learning all of your habits and forming new ones.  And you're trying to do this at the same time you're undertaking another, even harder task: writing programs.  And if you're a new Clojurist, and you're learning Emacs and Clojure from scratch at the same time, well, get the headache medication ready.&lt;/p&gt;

&lt;p&gt;As a programmer and someone who sits in front of a computer 12+ hours a day, I consider myself pretty flexible and capable of picking up a new user interface.  As someone who had been using Vim for years prior to trying Emacs, I considered myself more than capable of learning even a strange and foreign interface.  I'd done it once before.&lt;/p&gt;

&lt;p&gt;But learning Emacs still hurt.  Oh how it hurt.  I blogged while I was learning it, and you can &lt;strong&gt;&lt;a href=&quot;http://briancarper.net/blog/arrrrgh-emacs&quot;&gt;see&lt;/a&gt; &lt;a href=&quot;http://briancarper.net/blog/emacs-pain-continued&quot;&gt;my&lt;/a&gt; &lt;a href=&quot;http://briancarper.net/blog/emacs-undying-hatred&quot;&gt;pain&lt;/a&gt;&lt;/strong&gt; firsthand.  I sometimes hear people say &quot;I tried Emacs for a whole month and I still couldn't get it&quot;.  Well, it took me &lt;em&gt;over a year&lt;/em&gt; to be able to sit down at Emacs and use it fluidly for long periods of time without tripping over the editor.&lt;/p&gt;

&lt;p&gt;To be fair, I'm talking here about using Emacs as a programming environment.  Using Emacs as a Notepad replacement could be learned in short order.  &lt;code&gt;C-x C-f&lt;/code&gt;, &lt;code&gt;C-x C-s&lt;/code&gt;, or use the menus, there you go.  Using it comfortably as a full-fledged IDE is significantly harder and requires you to touch (and master) many more features.  Syntax highlighting, tab-completion, directory traversal and cwd issues, enabling line numbers, version-control integration, build tool integration, Emacs' funky regex syntax for search/replace, Emacs' bizarre kill rings and undo rings, the list goes on.  These things are very flexible in Emacs, which is a great thing, but it's also an impediment to learning how to configure and use them.  There's no getting around the time investment.&lt;/p&gt;

&lt;p&gt;And it's not just a matter of learning some new keyboard shortcuts.  There's a new vocabulary to learn.  You don't open files, you visit them.  What's a buffer?  What's a window?  (Not what you think it is.)  What's a point?  What's a mark?  Kill?  Yank?  &quot;&lt;em&gt;Apropos&lt;/em&gt;&quot;?  Huh?  &lt;code&gt;C-c M-o&lt;/code&gt; means what exactly?  My keyboard doesn't have a Meta key.  Yeah, you can use CUA mode and get your modernized Copy/Cut/Paste shortcuts back, but that's the tip of the iceberg.  It's hard even to know where to begin looking for help.&lt;/p&gt;

&lt;p&gt;Yeah, Emacs came first, before our more common and more modern conventions were established, and that explains why it's so different.  That doesn't change the fact that Emacs today is a strange beast.&lt;/p&gt;

&lt;h1&gt;Community and ecosystem&lt;/h1&gt;

&lt;p&gt;Personally I find the Emacs community to be a pretty nice bunch.  In the highest tradition of hackerdom and open source software, Emacs users seem to be eager and willing to share their elisp snippets and bend over backwards to help other people learn the editor.  I got lots of help when I was struggling and learning Emacs.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://www.emacswiki.org/&quot;&gt;Emacs wiki&lt;/a&gt; is an awesome resource.  The &lt;a href=&quot;http://www.gnu.org/software/emacs/manual/emacs.html&quot;&gt;official documentation&lt;/a&gt; is so complete (and so long) that it leaves me speechless sometimes.  And there are a million 3rd-party scripts for it.  Whatever you want Emacs to do is generally a short google away.&lt;/p&gt;

&lt;p&gt;If there's anything wrong with the Emacs community, it'd be people who take Emacs evangelism overboard.  The answer to &quot;&lt;strong&gt;&lt;em&gt;I don't want to have to use Emacs to use your language&lt;/em&gt;&lt;/strong&gt;&quot; can't be &quot;&lt;em&gt;Be quiet and learn more Emacs&lt;/em&gt;&quot;, or &quot;&lt;em&gt;If you're too dumb to learn Emacs, go away&lt;/em&gt;&quot;.  In some communities there is certainly some of that.  But thankfully I don't see it much in the Clojure community.  Let's hope it stays that way.&lt;/p&gt;

&lt;h1&gt;Interoperability&lt;/h1&gt;

&lt;p&gt;Once someone spends the time to write a suitable amount of elisp, Emacs can interoperate with anything.  I think so many people use SLIME for Clojure development precisely because it interoperates so darned well with Lisps.  SLIME is amazing.  You probably can't beat &lt;a href=&quot;http://www.emacswiki.org/emacs/ParEdit&quot;&gt;Paredit&lt;/a&gt; either, and Emacs' flexibility is precisely what makes things like Paredit possible.&lt;/p&gt;

&lt;p&gt;The problem is the amount of time you have to spend to get that interoperability set up and to learn how to use it.  After two years of using Emacs and Clojure together, every once in a while I still find myself bashing my face on my desk trying to get the latest SLIME or swank to work just right, or trying to get a broken key binding fixed, or tweaking some other aspect of Emacs that's driving me crazy.  One day, curly braces stopped being recognized as matched pairs by Paredit.  Why?  No idea; I fixed it, but it was a half hour of wasted time.&lt;/p&gt;

&lt;p&gt;Emacs is good at integrating with Git too.  So good that there are four or five different Emacs-Git libraries, each with a different interface and feature set.  I gave up eventually and went back to using the command line.  (You can embed a shell / command line right in Emacs.  There are three or four different libraries to do that too.)&lt;/p&gt;

&lt;p&gt;The wealth of options of ways to do things in Emacs is simultaneously a good thing, overwhelming and confusing.  If all you want is something that works and gets out of your way, too many options can be worse than one option, even if that one option isn't entirely ideal.&lt;/p&gt;

&lt;p&gt;Emacs' Java interop, I know nothing about.  Almost certainly, Emacs can come close to a modern Java IDE for fancy features like tab-completion and document lookups and project management.  But how long is it going to take you to figure out that tab-completion is called &lt;code&gt;hippie-expand&lt;/code&gt; in Emacs?  That and a million other surprises await you.&lt;/p&gt;

&lt;h1&gt;What's my point?&lt;/h1&gt;

&lt;p&gt;There was a pithy quote floating around on Twitter a while back (I think quoting Rich Hickey):&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;One possible way to deal with being unfamiliar with something is to become familiar with it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's true, and you could say that of Emacs.  I strongly believe that when it comes to computers, &lt;a href=&quot;http://briancarper.net/blog/lisp-syntax-doesnt-suck&quot;&gt;there's no such thing as &quot;intuitive&quot;&lt;/a&gt;.  There's stuff you've already spent a lot of time getting used to, and there's stuff you haven't.&lt;/p&gt;

&lt;p&gt;But certain things require more of a time investment than others.  Could I learn Clojure if all the keywords were in Russian or Chinese instead of my native English?  Sure, but it'd take me a long time.  I'd certainly have to have a good reason to attempt it.&lt;/p&gt;

&lt;p&gt;I learned Emacs partly because it was hard.  I saw it as a challenge.  It was fun, yet painful, but more pain, more glory.  Mastering it makes me feel like I've accomplished something.  I'd encourage other people to learn Emacs and Vim too.  I think the benefits of knowing them outweigh the cost and time investment of learning them.&lt;/p&gt;

&lt;p&gt;But I didn't learn Emacs with the goal of being productive.  I learned it for the same reason some people build cars in their garages, while most people just buy one and drive it to and from work every day.  I learned Emacs because I love programming and I love playing with toys, and Vim or Emacs are as nice a toy as I could ask for.  (I love programming enough to form strong opinions and write huge blog posts about text editors.)  For me, productivity was a beneficial side-effect.&lt;/p&gt;

&lt;p&gt;There are only so many hours in a day.  There are a lot of other challenges to conquer, some of which offer more tangible benefits than Emacs mastery would get you.  Mastering an arcane text editor isn't necessarily going to be on the top of the list of everyone's goals in life, especially when there are other editors that are easier to use and give you a significant subset of what Emacs would give you.  We have to pick our battles.&lt;/p&gt;

&lt;p&gt;So I understand when people say they don't want to learn Emacs.  I think maybe so many Clojurists use Emacs right now because we're still in the early adopter stage.  If you're using Clojure today, you're probably pretty enthusiastic about programming.  You're likely invested enough to be willing to burn the required time to learn Emacs.&lt;/p&gt;

&lt;p&gt;If Clojure becomes &quot;big&quot;, there are going to be a lot of casual users.  A casual user of Clojure isn't going to learn Emacs.  They're going to silently move on to another language.  And I really think that new blood is vital to the strength of a community and necessary for the continued healthy existence of a programming language.&lt;/p&gt;

&lt;p&gt;So Clojure does need alternatives.  I'll stick with Emacs myself, but there should be practical alternatives.  I'd encourage the Clojure community to continue to support and enjoy Emacs, but don't push it too hard.&lt;/p&gt;</description></item><item><title>Emacs: Yank lines as lines</title><link>http://briancarper.net/blog/emacs-yank-lines-as-lines</link><guid>http://briancarper.net/blog/emacs-yank-lines-as-lines</guid><pubDate>Fri, 21 May 2010 12:07:13 -0700</pubDate><description>&lt;p&gt;One thing nice about Vim is manipulating whole lines at a time.  &lt;code&gt;dd&lt;/code&gt; deletes a line (including trailing newline), regardless of where the cursor is on the line.  Then, &lt;code&gt;p&lt;/code&gt; puts that line (with its newline) as a new line after the current line, and &lt;code&gt;P&lt;/code&gt; puts it above the current line, again regardless of where your cursor is at the moment.  (It also jumps the cursor to the beginning of the text you just inserted, which is nice.)&lt;/p&gt;

&lt;p&gt;Emacs has &lt;code&gt;kill-whole-line&lt;/code&gt; (&lt;code&gt;C-S-Backspace&lt;/code&gt;) which is like Vim's &lt;code&gt;dd&lt;/code&gt;.  But I didn't find an equivalent of &lt;code&gt;p&lt;/code&gt; and &lt;code&gt;P&lt;/code&gt;.  So here's my version:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun yank-with-newline ()
  &quot;Yank, appending a newline if the yanked text doesn't end with one.&quot;
  (yank)
  (when (not (string-match &quot;\n$&quot; (current-kill 0)))
    (newline-and-indent)))

(defun yank-as-line-above ()
  &quot;Yank text as a new line above the current line.

Also moves point to the beginning of the text you just yanked.&quot;
  (interactive)
  (let ((lnum (line-number-at-pos (point))))
    (beginning-of-line)
    (yank-with-newline)
    (goto-line lnum)))

(defun yank-as-line-below ()
  &quot;Yank text as a new line below the current line.

Also moves point to the beginning of the text you just yanked.&quot;
  (interactive)
  (let* ((lnum (line-number-at-pos (point)))
         (lnum (if (eobp) lnum (1+ lnum))))
    (if (and (eobp) (not (bolp)))
        (newline-and-indent)
      (forward-line 1))
    (yank-with-newline)
    (goto-line lnum)))

(global-set-key &quot;\M-P&quot; 'yank-as-line-above)
(global-set-key &quot;\M-p&quot; 'yank-as-line-below)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Just one more step along the path to Vimmify my Emacs setup.  Emacs has some weird edge cases because you can move the cursor one &quot;line&quot; past the last real line in the file.  But I think I worked out something comfortable for myself.&lt;/p&gt;

&lt;p&gt;PS: I've written about this before, but if you use &lt;code&gt;C-S-Backspace&lt;/code&gt; a lot in Emacs on Linux, I highly recommend putting this into your X11 config:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Option &quot;DontZap&quot; &quot;True&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It's really easy to mix up &lt;code&gt;C-S-Backspace&lt;/code&gt; and &lt;code&gt;C-M-Backspace&lt;/code&gt; (the latter of which kills your X server).  It's not fun to mix those up.  Not fun at all.&lt;/p&gt;

&lt;p&gt;PPS: &lt;a href=&quot;http://stackoverflow.com/questions/2173324/emacs-equivalents-of-vims-dd-o-o&quot;&gt;This thread on Stack Overflow&lt;/a&gt; has some Emacs equivalents of Vim's &lt;code&gt;o&lt;/code&gt; and &lt;code&gt;O&lt;/code&gt; which are pretty nice too.&lt;/p&gt;</description></item><item><title>Vim vs. Emacs: Indenting text before copying</title><link>http://briancarper.net/blog/vim-vs-emacs-indenting-text-before-copying</link><guid>http://briancarper.net/blog/vim-vs-emacs-indenting-text-before-copying</guid><pubDate>Thu, 13 May 2010 16:21:27 -0700</pubDate><description>&lt;p&gt;I use Markdown on my blog for posts and comments, and I post at other sites that use Markdown (e.g. &lt;a href=&quot;http://stackoverflow.com&quot;&gt;Stack Overflow&lt;/a&gt;).  In Markdown, text indented four spaces is displayed as code, in &lt;code&gt;pre&lt;/code&gt; tags.  &lt;/p&gt;

&lt;p&gt;I find myself often writing code in Vim or Emacs and needing to copy/paste it into a browser in a Markdown-suitable way.it back.  This is easy to do in Vim and Emacs, only a few keystrokes.  But &quot;a few&quot; is still greater than &quot;one&quot;, so the heck with that.  Let's script it.&lt;/p&gt;

&lt;h1&gt;Vim version&lt;/h1&gt;

&lt;p&gt;This keymapping in Vim will do it all for me:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;vmap &amp;lt;Leader&amp;gt;y :s/^/    /&amp;lt;CR&amp;gt;gv&quot;+ygv:s/^    //&amp;lt;CR&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One clumsy thing about Vim is needing to restore the previous visual selection after each regex-replacement.  I could use the marks &lt;code&gt;'&amp;lt;&lt;/code&gt; and &lt;code&gt;'&amp;gt;&lt;/code&gt; as ranges to &lt;code&gt;:s&lt;/code&gt; instead, but that's more typing than simply doing &lt;code&gt;gv&lt;/code&gt; in the mapping.  Copying to the system clipboard is easy because Vim has a register &lt;code&gt;&quot;+&lt;/code&gt; for that purpose.&lt;/p&gt;

&lt;p&gt;This took me maybe 45 seconds to write, probably due to being pretty familiar with Vim already.  But in Vim, mappings are easy.  You just type the characters that you'd type if you were doing it manually.&lt;/p&gt;

&lt;h1&gt;Emacs version&lt;/h1&gt;

&lt;p&gt;Trying to do the same in Emacs was painful.  My &lt;em&gt;Emacs-fu&lt;/em&gt; is sorely inadequate, compared to my &lt;em&gt;Vim-jitsu&lt;/em&gt;.  This seems to work, but ugh:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;;; adapted from http://www.emacswiki.org/emacs/.emacs-ChristianRovner.el
(defun expand-region-linewise ()
  (interactive)
  (let ((start (region-beginning))
        (end (region-end)))
   (goto-char start)
   (beginning-of-line)
   (set-mark (point))
   (goto-char end)
   (unless (bolp) (end-of-line))))

(defun markdown-copy ()
  (interactive)
  (save-window-excursion
   (save-excursion
     (save-restriction
       (expand-region-linewise)
       (narrow-to-region (region-beginning) (region-end))
       (goto-char (point-min))
       (replace-regexp &quot;^&quot; &quot;    &quot;)
       (clipboard-kill-ring-save (point-min) (point-max))
       (goto-char (point-min))
       (replace-regexp &quot;^    &quot; &quot;&quot;)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Writing this involved a long journey through the Emacs documentation.&lt;/p&gt;

&lt;p&gt;One difficulty was getting Emacs to play friendly with my fat-fingered region-marking.  I don't always highlight from the beginning of the first line to the end of the last.  That's why Vim's visual-line mode is awesome; the cursor can be anywhere on the line, it still selects the whole line.  The handy function above (found on the Emacs wiki) takes care of that.  I don't know how long it would've taken me to come up with that on my own.&lt;/p&gt;

&lt;p&gt;Then it was a matter of rooting through millions of Emacs functions until I found the ones that move the point around and copy text to the clipboard.  Along the way I discovered the wonders of &quot;narrowing&quot;, which limits Emacs to work on some region of text, and all those macros to undo the messes I make while moving around.&lt;/p&gt;

&lt;p&gt;Maybe I could've done this with an Emacs keyboard macro, and then called &lt;code&gt;apply-macro-to-region-lines&lt;/code&gt;.  And maybe I could use &lt;code&gt;append-next-kill&lt;/code&gt; to build up the indented text one line at a time.  But my efforts to do this or anything like it failed horribly.&lt;/p&gt;

&lt;p&gt;In any case I thought it was an interesting comparison.  Improvements to either version are welcome.&lt;/p&gt;

&lt;p&gt;EDIT: This works too (thanks Holger Durer):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun markdown-copy ()
  (interactive)
  (save-excursion
    (expand-region-linewise)
    (indent-rigidly (region-beginning) (region-end) 4)
    (clipboard-kill-ring-save (region-beginning) (region-end))
    (indent-rigidly (region-beginning) (region-end) -4)))
&lt;/code&gt;&lt;/pre&gt;</description></item><item><title>Printing a nicely formatted plaintext table of data in Clojure</title><link>http://briancarper.net/blog/printing-a-nicely-formatted-plaintext-table-of-data-in-clojure</link><guid>http://briancarper.net/blog/printing-a-nicely-formatted-plaintext-table-of-data-in-clojure</guid><pubDate>Fri, 16 Apr 2010 11:49:02 -0700</pubDate><description>&lt;p&gt;I use this fairly often while data-munging, when I want to quickly view a list of hash-maps of data in a simple table format.  Usually this is coming out of database.  Maybe someone will find it useful.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defn table
  &quot;Given a seq of hash-maps, prints a plaintext table of the values of the hash-maps.
  If passed a list of keys, displays only those keys.  Otherwise displays all the
  keys in the first hash-map in the seq.&quot;
  ([xs]
     (table xs (keys (first xs))))
  ([xs ks]
     (when (seq xs)
       (let [f (fn [old-widths x]
                 (reduce (fn [new-widths k]
                           (let [length (inc (count (str (k x))))]
                             (if (&amp;gt; length (k new-widths 0))
                               (assoc new-widths k length)
                               new-widths)))
                         old-widths ks))
             widths (reduce f {} (conj xs (zipmap ks ks)))
             total-width (reduce + (vals widths))
             format-string (str &quot;~{&quot;
                                (reduce #(str %1 &quot;~&quot; (%2 widths) &quot;A&quot;) &quot;&quot; ks)
                                &quot;~}~%&quot;)]
         (cl-format true format-string (map str ks))
         (cl-format true &quot;~{~A~}~%&quot; (repeat total-width \-))
         (doseq [x xs]
           (cl-format true format-string (map x ks)))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then you can do this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user&amp;gt; (def data [{:name &quot;Brian&quot; :job &quot;Code monkey&quot; :age 29}
                 {:name &quot;Bob&quot; :job &quot;Janitor&quot; :age 97}
                 {:name &quot;Johnny McLongname&quot; :job &quot;None&quot; :age 3}])
#'user/data
user&amp;gt; (table data)
:name             :job        :age 
-----------------------------------
Brian             Code monkey 29   
Bob               Janitor     97   
Johnny McLongname None        3    
nil
user&amp;gt; (table data [:age :name])
:age :name             
-----------------------
29   Brian             
97   Bob               
3    Johnny McLongname 
nil
&lt;/code&gt;&lt;/pre&gt;</description></item><item><title>Installing Clojure with Emacs and SLIME: so easy, yet so hard</title><link>http://briancarper.net/blog/installing-clojure-with-emacs-and-slime-so-easy-yet-so-hard</link><guid>http://briancarper.net/blog/installing-clojure-with-emacs-and-slime-so-easy-yet-so-hard</guid><pubDate>Wed, 14 Apr 2010 16:02:55 -0700</pubDate><description>&lt;p&gt;How do you install Clojure with SLIME/Emacs support?  The answer is either really easy or really hard, depending.&lt;/p&gt;

&lt;h1&gt;Easy!&lt;/h1&gt;

&lt;p&gt;If you're new to Clojure and want to get it working fast, you are in luck.  Go to &lt;a href=&quot;http://github.com/technomancy/swank-clojure&quot;&gt;swank-clojure's&lt;/a&gt; home on github and follow the directions to install it via ELPA.  This is very easy and fast and it works.&lt;/p&gt;

&lt;p&gt;If you want more documentation, there is (thankfully) a single, central, official-ish location where documentation for installing Clojure is slowly but surely accumulating.  You can read about it at &lt;a href=&quot;http://www.assembla.com/wiki/show/clojure/Getting_Started&quot;&gt;http://www.assembla.com/wiki/show/clojure/Getting_Started&lt;/a&gt;.  This site includes instructions for Emacs, Netbeans, Eclipse, Maven, and everything else under the sun.  This is a great resource, and attempts to solve the problem of out-of-date Clojure documentation.  (This problem is natural given Clojure's young age and rapid growth, but it's painful for people trying to get started, I bet.)&lt;/p&gt;

&lt;h1&gt;Or is it...&lt;/h1&gt;

&lt;p&gt;This is all great.  But if you want to do anything non-standard, you run into a bit of a wall.  How do you install Clojure+SLIME on Windows?  I find that sometimes it works, but sometimes things mysteriously fail, and I have no idea where even to begin to debug it, because there's so much automation going on in the background nowadays.  What if you want to build a &lt;code&gt;.emacs.d&lt;/code&gt; directory you can simply copy from one system to another and have everything work?  (You can do this with most Emacs libs.)&lt;/p&gt;

&lt;p&gt;Or how do you install and use a bleeding-edge Clojure or clojure-contrib tree with SLIME?  There's good reason to want to, because it has some awesome new features, e.g. deftypes and protocols and rest-parameter destructuring.  But most of the &quot;official&quot; methods of installing things give you stable versions of everything.  &lt;/p&gt;

&lt;p&gt;And then, you generally end up with a bunch of opaque .jar files, auto-downloaded all over your system, which isn't helpful if you want to hack on anything yourself.  Most of these jars end up in &lt;code&gt;~/.m2&lt;/code&gt; nowadays, I think?  Used to be &lt;code&gt;~/.swank-clojure&lt;/code&gt; or &lt;code&gt;~/.clojure&lt;/code&gt; or something?  And &lt;code&gt;~/.emacs.d/elpa&lt;/code&gt; gets stuff too?  Color me continually confused.  &lt;/p&gt;

&lt;p&gt;The automated build tools we have nowadays (ELPA, Lein, Maven) are awesome when they work, but extremely painful when they fail or when you don't know which knobs to turn when you want to step slightly outside of the mainstream.  This is incidental complexity if I ever saw it.  While it's far more manual labor, I find it to be so much simpler to download things myself, build everything myself, put it all somewhere on my system myself, and configure Emacs to tell it where to find everything.&lt;/p&gt;

&lt;p&gt;Well, &lt;a href=&quot;http://tentclube.blogspot.com/&quot;&gt;this site&lt;/a&gt; is as close as I can find to good up-to-date documentation, and what I did to get set up was almost identical to this.  But it will likely stop working in a month or two.  There's also a recent thread on &lt;a href=&quot;http://groups.google.com/group/clojure/browse_frm/thread/3dba26708b164c55#&quot;&gt;the Clojure mailing list&lt;/a&gt; which has some more tips (and valid complaints).  &lt;/p&gt;

&lt;p&gt;I will push my Emacs setup to my github repo once I get the kinks worked out.  But if you want to get bleeding-edge everything working yourself, all I have to say is God help you, because it's largely unsupported.  Be prepared to hack and patch until it works.  I'm looking forward to the day when most of the kinks are worked out and things settle down a bit.&lt;/p&gt;</description></item><item><title>Getting list of referers out of Apache logs</title><link>http://briancarper.net/blog/getting-list-of-referers-out-of-apache-logs</link><guid>http://briancarper.net/blog/getting-list-of-referers-out-of-apache-logs</guid><pubDate>Sun, 21 Feb 2010 12:46:34 -0800</pubDate><description>&lt;p&gt;I use Google Analytics, but it has a noticeable lag in updating its information.  When my site is being hammered, I'd like to see where all the traffic is coming from.  It'd also be nice to see how many hits my RSS feed is getting, and how many images and static files are being direct-linked, which Google Analytics currently isn't tracking for me at all.  &lt;/p&gt;

&lt;p&gt;So this script will look in my Apache logs and print referers for some URL, thanks to &lt;a href=&quot;http://www.simonecarletti.com/blog/2009/02/apache-log-regex-a-lightweight-ruby-apache-log-parser/&quot;&gt;ApacheLogRegex&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/usr/bin/ruby

require 'apachelogregex'

raise &quot;USAGE: #{$0} log_filename desired_url&quot; unless ARGV[0] and ARGV[1]

format = '%v:%p %h %l %u %t \&quot;%r\&quot; %&amp;gt;s %b \&quot;%{Referer}i\&quot; \&quot;%{User-Agent}i\&quot;'
parser = ApacheLogRegex.new(format)
pat = Regexp.new(ARGV[1])
refs = {}

File.readlines(ARGV[0]).each do |line|
  x = parser.parse(line)
  if pat.match(x[&quot;%r&quot;])
    r = x[&quot;%{Referer}i&quot;]
    refs[r] = (refs[r] || 0) + 1
  end
end
refs.sort_by{|k,v| -v}.each do |ref,count|
  puts &quot;%s: %s&quot; % [count,ref]
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I used to use awstats for this, but it was too heavyweight and a hassle to set up and keep running.  Google Analytics is a no-brainer to use, even though the accuracy isn't as good as parsing Apache logs.  At least I get an idea of which of my blatherings people are most interested in.&lt;/p&gt;</description></item></channel></rss>

