UP | HOME

Ideas

Scheme source code highlighter and indentation analyser

This is in part because the Scheme syntax highlighting provided by e.g. Pygments is terrible anyway, and partly because I think source code (especially Lisp source code, and function programming source code in general) looks nice in when properly typeset in proportional fonts.

The indentation analyser would be fairly simple — its job would be to compare the first printing column of each line of code with a set of predefined ‘possible indentation points’ (usually open parens) in previous lines, and be able to construct output for the LaTeX tabbing environment so that things (like let bindings and cond clauses) which are vertically aligned in monospaced output can be typeset in a proportional font and remain vertically aligned. If something in a subsequent line is indented, but not by an amount matching a ‘possible indentation point’, it just gets a regular LaTeX \indent, or maybe several if the indent step is particularly large. Also useful for this would be a JavaScript hack to emulate LaTeX’s tabbing environment within HTML and CSS, so that code printed in proportional fonts can also look good on the web. (This would be easy and involve no JS if CSS’s layout system weren’t comically underpowered — it could probably easily be done in CCSS, for example.)

Highlighting-wise, the most useful thing it could do would be to identify three kinds of names in Scheme programs: syntax, free variables, and bound variables. The former could be printed in bold, free variables in roman type, and bound variables in italic type. (Continuing this, I think strings in monospaced font would be a nice perverse inversion of the usual order of things!) This typographical style comes from the typical habit in algorithmic and mathematical notation of printing the names of control flow structures in bold, the names of functions in roman type, and the names of variables in italic type. In Scheme, we don’t have separate namespaces for variables and functions, but within procedures, the names of other procedures tend to be free variables, except in when higher-order functions, where I feel like intuitively you’d want the variable containing the procedure to be italicized in all contexts anyway. The naïve recursive single-list map implementation highlighted in this style would look something like this:

(define (map f xs)
    (if (null? xs) ’()
        (cons (f (car xs)) (map f (cdr xs)))))

(The quote mark for the empty list would appear straight, ideally, but I can’t get Org mode to do that.) Quoted symbols (and indeed everything in a quoted datum), numbers, constants like #t and #f, etc would also likely most logically appear in roman type.