Skip to content

Instantly share code, notes, and snippets.

@dohyunkim
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save dohyunkim/5f2dc71b1ff85adb3978 to your computer and use it in GitHub Desktop.

Select an option

Save dohyunkim/5f2dc71b1ff85adb3978 to your computer and use it in GitHub Desktop.
colorize verbatims
luatexbase.provides_module({
name = 'colorVerb',
date = '2011/01/20',
version = 0.1,
description = 'Colorize Commands/Braces/Brackets in Verbatim',
author = 'Dohyun Kim',
license = 'Public Domain',
})
colorVerb = colorVerb or {}
local colorVerb = colorVerb
local colors = {
cscolor = {0, 0.5, 0.5},
bracecolor = {0.9, 0, 0 },
bracketcolor = {0.9, 0, 0 },
parencolor = {0.9, 0, 0 },
commentcolor = {0.5, 0.5, 0.5},
operatecolor = {0.5, 0.5, 0 },
}
colorVerb.colors = colors
local cscolor = colors.cscolor
local bracecolor = colors.bracecolor
local bracketcolor = colors.bracketcolor
local parencolor = colors.parencolor
local commentcolor = colors.commentcolor
local operatecolor = colors.operatecolor
local attrVerb = luatexbase.attributes.attrVerb
local glyph = node.id("glyph")
local hlist = node.id("hlist")
local vlist = node.id("vlist")
local kern = node.id("kern")
local nodecopy = node.copy
local nodetail = node.tail
local has_attr = node.has_attribute
local insert_before = node.insert_before
local insert_after = node.insert_after
local stringchar = unicode.utf8.char
local concat = table.concat
local addtocallback = luatexbase.add_to_callback
local colorpush = node.new("whatsit", "pdf_colorstack")
colorpush.stack = 0
colorpush.command = 1
local colorpop = nodecopy(colorpush)
colorpop.command = 2
local parenchars = {
["("] = parencolor,
[")"] = parencolor,
["{"] = bracecolor,
["}"] = bracecolor,
["["] = bracketcolor,
["]"] = bracketcolor,
["&"] = operatecolor,
}
local keywords = {
{ }, -- TeX --
{ -- Lua --
["and"] = operatecolor,
["break"] = cscolor,
["do"] = operatecolor,
["else"] = operatecolor,
["elseif"] = operatecolor,
["end"] = operatecolor,
["error"] = cscolor,
["false"] = {1, 0, 0},
["for"] = operatecolor,
["function"] = cscolor,
["if"] = operatecolor,
["in"] = operatecolor,
["ipairs"] = cscolor,
["local"] = operatecolor,
["next"] = cscolor,
["nil"] = {1, 0, 0},
["not"] = operatecolor,
["or"] = operatecolor,
["pairs"] = cscolor,
["require"] = cscolor,
["return"] = cscolor,
["then"] = operatecolor,
["tonumber"] = cscolor,
["tostring"] = cscolor,
["true"] = {1, 0, 0},
["type"] = cscolor,
["while"] = operatecolor,
},
{ -- MetaPost --
["beginfig"] = cscolor,
["btex"] = cscolor,
["buildcycle"] = cscolor,
["color"] = operatecolor,
["cosd"] = cscolor,
["cycle"] = operatecolor,
["dashed"] = operatecolor,
["decimal"] = cscolor,
["def"] = operatecolor,
["dir"] = cscolor,
["dotlabel"] = cscolor,
["draw"] = cscolor,
["drawarrow"] = cscolor,
["drawoptions"] = cscolor,
["else"] = operatecolor,
["elseif"] = operatecolor,
["enddef"] = operatecolor,
["endfig"] = cscolor,
["endfor"] = operatecolor,
["etex"] = cscolor,
["expr"] = operatecolor,
["fi"] = operatecolor,
["fill"] = cscolor,
["for"] = operatecolor,
["if"] = operatecolor,
["intersectionpoint"] = operatecolor,
["label"] = cscolor,
["pair"] = operatecolor,
["path"] = operatecolor,
["picture"] = operatecolor,
["scaled"] = operatecolor,
["sind"] = cscolor,
["shifted"] = operatecolor,
["sqrt"] = cscolor,
["thelabel"] = cscolor,
["unfill"] = cscolor,
["uniformdeviate"] = cscolor,
["upto"] = operatecolor,
["vardef"] = operatecolor,
["withcolor"] = operatecolor,
["xpart"] = cscolor,
["ypart"] = cscolor,
},
}
local function do_colorize(head, start, stop, color)
local colorstart = nodecopy(colorpush)
local colorstop = nodecopy(colorpop)
color = concat(color," ")
colorstart.data = color:find(" rg") and color or color.." rg"
head = insert_before(head, start, colorstart)
head, colorstop = insert_after (head, stop, colorstop)
return head, colorstop
end
local function do_color_Verb (head)
local curr = head
while curr do
local id = curr.id
if id == hlist or id == vlist then
curr.head = do_color_Verb(curr.head)
elseif id == glyph then
local attr = has_attr(curr, attrVerb)
-- attrVerb == 1 : TeX
-- attrVerb == 2 : Lua
-- attrVerb == 3 : MetaPost
if attr then
local chr = stringchar(curr.char)
if attr ~= 1 and chr == "\"" or chr == "\'" then
local nn = curr.next
while nn do
curr = nn
if nn.id == glyph then
local thischr = stringchar(nn.char)
if thischr == "\\" then
nn = nn.next
if not nn then break end
elseif thischr == chr then
break
end
end
nn = nn.next
end
elseif chr == "\\" then
local nn = curr.next
local startnode, stopnode = curr, nn
while nn and nn.id == glyph and stringchar(nn.char):find("%a") do
stopnode = nn
nn = nn.next
end
if stopnode then
head, curr = do_colorize(head, startnode, stopnode, cscolor)
end
elseif attr ~= 1 and chr:find("[%a%._]") then
local startnode, t = curr, { chr }
local nn = curr.next
while nn and nn.id == glyph do
local chr = stringchar(nn.char)
if chr:find("[%a%._]") then
t[#t + 1] = chr
curr = nn
nn = nn.next
else
break
end
end
local color = keywords[attr][concat(t)]
if color then
head, curr = do_colorize(head, startnode, curr, color)
end
elseif parenchars[chr] then
head, curr = do_colorize(head, curr, curr, parenchars[chr])
elseif chr == "%" and attr ~= 2 then -- TeX, MetaPost comment
return do_colorize(head, curr, nodetail(curr), commentcolor)
elseif chr == "-" and attr == 2 then -- Lua comment; box comment is not supported
local nn = curr.next
if nn.id == kern and nn.kern == 0 then
local nn = nn.next
if nn.id == glyph and stringchar(nn.char) == chr then
return do_colorize(head, curr, nodetail(curr), commentcolor)
end
end
end
end
end
curr = curr.next
end
return head
end
addtocallback("pre_output_filter", do_color_Verb, "colorVerb.pre_output")
% written by Dohyun Kim
% Public Domain
\ProvidesPackage{colorVerb}
[2011/01/20 v0.1 Color Commands/Braces/Brackets in Verbatim]
\ifx\directlua\undefined
\let\usecolorVerb\relax
\let\usecolorLua\relax
\let\usecolorMP\relax
\def\colorVerbextractcolor#1 to #2{}
\endinput\fi
\newluatexattribute\attrVerb
\def\usecolorVerb{\attrVerb\@ne}
\def\usecolorLua {\attrVerb\tw@}
\def\usecolorMP {\attrVerb\thr@@}
\RequireLuaModule{colorVerb}
\def\cscolor #1{\directlua{ colorVerb.colors.cscolor = {"#1"} }}
\def\bracecolor #1{\directlua{ colorVerb.colors.bracecolor = {"#1"} }}
\def\bracketcolor#1{\directlua{ colorVerb.colors.bracketcolor = {"#1"} }}
\def\parencolor #1{\directlua{ colorVerb.colors.parencolor = {"#1"} }}
\def\commentcolor#1{\directlua{ colorVerb.colors.commentcolor = {"#1"} }}
%%% extract color defined with xcolor.sty
% eg. \colorVerbextractcolor{green!45!blue} to \cscolor
\def\colorVerbextractcolor#1 to #2{%
\begingroup
\def\set@color{#2{\current@color}}%
\color{#1}%
\endgroup
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment