Clojure Source Files in Quick Look on MacOSX

Do you like to see the content of Clojure source files in Quick Look? I do.

Unfortunately this is not as straightforward as it should be. Having XCode on my machine didn’t help either. It took me a while to get this working. Maybe this will safe you some time. :-)

Here we go..

Quick Look doesn’t know about clj files. Which is ok because it can’t possibly know every programming language out there. The quick look default behaviour for unknown files is to simply show some not so interesting file information:

I found a plugin called QLColorCode (version 2.0.2) which adds Quick Look support for a large number of programming languages including syntax highlighting.

The plugin has some installation notes and how to handle the XCode issue. Nevertheless it takes a couple of manual steps to get it running for clj source files.

First a few words about the way Quick Look works.

Quick Look uses something called UTI (Uniform Type Identifiers Reference) to identify the type of a file and to select the proper plugin for displaying it.

It is possible to check what Quick Look knows about a clojure files by running qlmanage. The -d 1 prints debug information and the -p option takes the file.

Here is the output:

$ qlmanage -d 1 -p src/clj/clojure/core.clj
Testing Quick Look preview with files:
[DEBUG] Registering for public.image
[DEBUG] Preview test for src/clj/clojure/core.clj -- /Users/hauner/Development/clojure.git/. Content type UTI: dyn.ah62d4rv4ge80g5dn
[DEBUG] Previewing /Users/hauner/Development/clojure.git/src/clj/clojure/core.clj. Content type UTI: dyn.ah62d4rv4ge80g5dn. Generator used: None

The Content Type UTI (Lines 5 & 6) is only “garbage”. Which means that Quick Look has no idea how to handle clj files.

Back to the installation.

Disabling XCodes “source-code” plugin

The QLColorCode plugin will register itself for the “public.source-code” UTI.

With XCode installed the “public.source-code” UTI is already registered to a plugin provided by XCode (XCode issue) as can be seen by running qlmanage again.

$ qlmanage -m | grep source-code
public.source-code -> /Developer/Applications/ (1613)

To get rid of it, I used the Disable XCode QL from QLColorCodeScripts-1.0. It will rename the XCode to SourceCode.qlgenerator.disabled which is enough to remove it. Simply renaming it will work too.

Running qlmanage again, it is gone.

$ qlmanage -m | grep source-code

Before installing the QLColorCode plugin is has to be tweaked a littel bit which I will describe next.

Adjusting the plugin

MacOSX doesn’t offer any convinient way to register new or change file types (in this case the clojure files). To register it with MacOSX I added an xml to qlcolorcode’s Info.plist file, which is in QLColorCode-2.0.2/QLColorCode.qlgenerator/Contents/Info.plist.

I simply copied and adjusted the scala entry.

    <string>Clojure Source Code</string>

There is another small tweak required to make the plugin use syntax highlighting. The plugin contains highlighting rules for Clojure but expects .clojure as filename extension instead of .clj. To fix it I simply copied clojure.lang to clj.lang in the plugin folder QLColorCode-2.0.2/QLColorCode.qlgenerator/Contents/Resources/highlight/share/highlight/.

Nearly done, only one step left :-)

Installing the QLColorCode plugin

After tweaking the plugin I copied the plugin to ~/Library/QuickLook (/Library/QuickLook would be fine too).

Installation is described in the plugins ReadMe.txt along with some instructions for customizing font, font size and other options.

I had to run

$ qlmanage -r
qlmanage: resetting quicklookd

before Quick Look recognized the new plugin and qlmanage -m reported QLColorCode
as plugin for “source-code”.

$ qlmanage -m | grep source-code
public.source-code -> /Users/hauner/Library/QuickLook/QLColorCode.qlgenerator (2.0.1)

And then finally the reward… :-)