--- /dev/null
+-- filename : type-lam.lua
+-- comment  : Pretty printing of (extended) lambda calculus
+-- author   : Matthijs Kooijman, Universiteit Twente, NL
+-- copyright: Matthijs Kooijman
+-- license  : None
+
+local utf = unicode.utf8
+
+if not buffers                 then buffers                 = { } end
+if not buffers.visualizers     then buffers.visualizers     = { } end
+if not buffers.visualizers.lam then buffers.visualizers.lam = { } end
+
+buffers.visualizers.lam.colors = {
+    "prettytwo",
+    "prettyone",
+    "prettythree",
+    "prettyfour"
+}
+
+buffers.visualizers.lam.tokens = {
+    [' '] = {repr = '\\obs '},
+    ['->'] = {repr = '\\rightarrow'},
+    ['case'] = {style = 'bold'},
+    ['of'] = {style = 'bold'},
+    ['let'] = {style = 'bold'},
+}
+
+function buffers.visualizers.lam.flush_line(str,nested)
+    local result, state = { }, 0
+    local finish, change = buffers.finish_state, buffers.change_state
+    -- Set the colorscheme, which is used by finish_state and change_state
+    buffers.currentcolors = buffers.visualizers.lam.colors
+
+    while str ~= "" do
+        found = false
+        for tok,props in pairs(buffers.visualizers.lam.tokens) do
+            -- Try to remove tok from the start of str 
+            str, count = utf.gsub(str, "^" .. tok, "")
+            if count ~= 0 then
+                -- Replace the token with the repr, or just itself
+                result[#result+1] = props.repr or tok
+                -- Enclose the token in {\style .. }
+                if props.style then
+                    result[#result] = "{\\" .. props.style ..
+                                        " " .. result[#result] ..
+                                        "}"
+                end
+                -- We found a token, now start over
+                found = true
+                break
+            end
+        end
+        if not found then
+            result[#result+1] = utf.sub(str, 1, 1)
+            str = utf.sub(str, 2)
+        end
+    end
+    state = finish(state, result)
+    buffers.flush_result(result,nested)
+end
+
+-- vim: set sw=4 sts=4 expandtab ai: