260 Posts in Category 'Programming'
Shuffle lines in Vim
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 sort, even right inside Vim:
:%!sort -R
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 :h ruby and we have a successful, working kludge:
function! ShuffleLines()
ruby << EOF
buf = VIM::Buffer.current
firstnum = VIM::evaluate('a:firstline')
lastnum = VIM::evaluate('a:lastline')
lines = []
firstnum.upto(lastnum) do |lnum|
lines << buf[lnum]
end
lines.shuffle!
firstnum.upto(lastnum) do |lnum|
buf[lnum] = lines[lnum-firstnum]
end
EOF
endfunction
2011-07-07 23:32 - Edited to remove a superfluous line.
2011-07-09 21:33 - Wrong parameter for sort, oops.
Keyword Arguments: Ruby, Clojure, Common Lisp
And suddenly I return to blogging, rising from the ashes like some kind of zombie phoenix. Turns out writing a book 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.
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.
org-mode is awesome
I've seen org-mode 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.
Organizer? Nah.
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.
There are some nice agenda-related shortcuts that are probably worth using though, like typing C-c . to enter a datestamp, like <2011-01-20 Thu>. Then you can increment or decrement it one year/month/day at a time via S-up and S-down. I like this.
Plaintext editor? Yes!
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.
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.
There are ways to collapse and expand outlines, search for items and only display those items, and so on. The documentation for org-mode is very clear and took me less than an hour to read through. All-in-all a pleasant experience.
* 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 |
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.
I also highly recommend putting (setq org-startup-indented t) into .emacs to make everything look pretty on-screen. It still saves as the simple plaintext above, but it looks like this in Emacs:

I can definitely see using org-mode for TODO files in some of my projects. (You can mark entries as TODO (just by typing TODO in front), and then toggle between TODO/DONE via C-c C-t.) I can also see using it as a general-purpose note-taker.
org-mode also has a mobile version for iPhone and Android, synced via WebDAV or Dropbox, so you can org-mode on your phone while you're driving to the grocery store1. Again I don't really need this, but there it is.
The joy of plaintext
Plaintext is awesome.
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.
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.
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.
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.
[ ] is just two braces and a space, but org-mode lets me hit C-c C-c and turn the space into an X. This may seem banal, hardly worth caring about, but add to this shortcut thousands upon thousands of others. Things like org-mode, or paredit, or all of Vim's built-in magic... it all adds up to something wonderful.
And best of all, you always still have the option of manually keyboarding over and typing that X between the braces yourself. It's still just plaintext underneath. So you end up with the best of both worlds.
I do not recommend using org-mode while driving, for public safety reasons. ↩
Vim undo tree visualization
I wrote previously about an awsome plugin to give Emacs Vim-style undo trees.
Vim's undo trees are the best thing since sliced bread, but the interface for browsing through the tree is not pleasant. The Emacs undo-tree library has a way to visualize the tree and move through it with your keyboard, which solves this problem.
But now, thanks to Steve Losh, Vim has an undo-tree visualizer too. 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.
Thus continues the eternal Vim/Emacs arms race.

Vim :ruby and :rubydo scope
Note to self. In old Vim (tested in 7.2.320), I could do this:
:ruby x='foo'
:rubydo $_=x
Now every line in the file says foo. But in Vim 7.3 I get an error:
NameError: undefined local variable or method `x' for main:Object
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.
A workaround is to use global variables in Ruby instead. So this still works:
:ruby $x='foo'
:rubydo $_=$x
Phew.
Emacs undo trees
I've said it before: undo in Emacs is horrible. On the other hand, undo in Vim is awesome.
But this is true no longer. Now there are undo trees for Emacs! 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.
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.

In other news, Vim 7.3 is out and it now has persistent undo across reloads. It's like an arms race, and gleeful hackers reap the benefits.
gaka 0.2.0
Per many commenters' suggestions and thanks to code from Steve Purcell, you can now use maps for CSS attributes in gaka.
user> (println (gaka/css [:a {:color :red}]))
a {
color: red;}
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 significant1 in CSS.
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 PersistentArrayMap instead of a PersistentHashMap. But it's highly dangerous to rely on such a thing. It could change at any time in the future.
In any case, you can also mix and match maps, lists and "flat" keyvals. They'll all be flattened That can help preserve attribute order in those cases where you need to.
user> (println (gaka/css [:a :color "red" {:padding 0} (list :margin 0)]))
a {
color: red;
padding: 0;
margin: 0;}
I've also enhanced "mixins" 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 "Make my element have no padding, and make links within the element be red":
user> (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;}
You can get gaka from github or Clojars.
Order is only significant in cases where you're doing things like
padding: 0; padding-left: 1px. 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. ↩
Footnotes
Did you ever notice how footnotes make your writing seem more important1 somehow?
Maybe one reason is that "real" books use footnotes. At a glance, it looks like I have references2 backing up everything I say. In reality, I don't, but the connotation carries through somehow3. Now my blog seems scholarly and authoritative.
And if you're like me, you can't resist clicking footnotes to see what they refer to4. According to my estimates, by utilizing footnotes, in one fell swoop I have decreased my readers' average reading efficiency by 73%.
In any case, I've added experimental, rudimentary support for footnotes to cow-blog.
I'm loosely copying the syntax from Markdown Extra for this. Markdown is great, except when it isn't. The standard doesn't have support for some useful extensions. I use Showdown for Markdown support, and I'm probably going to work on adding more features of Markdown Extra to Showdown in the near future.
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.
(If there's a Showdown Extra out there already, drop me a URL. It'd be most appreciated. But I couldn't find one.)
Clojure syntax highlighting via SyntaxHighlighter
How do you syntax-highlight Clojure code for display on a website? The best way I can find is SyntaxHighlighter.
Daniel Gómez wrote a brush to give SyntaxHighlighter Clojure support. I tweaked it a bit myself and integrated it into cow-blog. I also converted my favorite color scheme to a SyntaxHighlighter theme. So when I write this code:
(defn- ip
"Given a request, return the IP. Looks for an x-forwarded-for
header, falls back to :remote-addr on the request."
[request]
(or (get-in request [:headers "x-forwarded-for"])
(request :remote-addr)))
You should see something like this:

...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.
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 <pre class="brush: clojure"> 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.
The code for all of this is on github with the rest of my blog.
