diff --git a/pret-pas.lua b/pret-pas.lua index 97ac9bd..0439fea 100644 --- a/pret-pas.lua +++ b/pret-pas.lua @@ -6,22 +6,9 @@ if not modules then modules = { } end modules ['pret-pas'] = { license = "see context related readme files" } -local utf = unicode.utf8 - -local utfcharacters, utfvalues = string.utfcharacters, string.utfvalues -local utfbyte, utffind = utf.byte, utf.find -local byte, sub, find, match = string.byte, string.sub, string.find, string.match -local texsprint, texwrite = tex.sprint, tex.write -local ctxcatcodes = tex.ctxcatcodes - -local buffers = buffers - -local changestate, finishstate = buffers.changestate, buffers.finishstate - local visualizer = buffers.newvisualizer("pas") -- reserved words taken from http://www.freepascal.org/docs-html/ref/refse3.html - visualizer.reservedwords = { -- Turbo Pascal "absolute", "and", "array", "asm", "begin", "case", "const", "constructor", @@ -94,32 +81,31 @@ end local function written(state,c,i) if c == " " then - state = finishstate(state) - texsprint(ctxcatcodes,"\\obs") + state = buffers.finishstate(state) + tex.sprint(tex.ctxcatcodes,"\\obs") elseif c == "\t" then - state = finishstate(state) - texsprint(ctxcatcodes,"\\obs") + state = buffers.finishstate(state) + tex.sprint(tex.ctxcatcodes,"\\obs") if buffers.visualizers.enabletab then - texsprint(ctxcatcodes,rep("\\obs ",i%buffers.visualizers.tablength)) + tex.sprint(tex.ctxcatcodes,rep("\\obs ",i%buffers.visualizers.tablength)) end else - texwrite(c) + tex.write(c) end return state, 0 end function visualizer.flush_line(str, nested) --[[ - buffers.currentcolors = colors local identifier = nil - for c in string.utfcharacters(str) do - if string.string.find(c, "^[%a%_]$") then + for c in string.string.utfcharacters(str) do + if string.string.string.find(c, "^[%a%_]$") then if identifier then identifier = identifier .. c else identifier = c end - elseif string.string.find(c, "^[%d]$") + elseif string.string.string.find(c, "^[%d]$") if identifier and #identifier > 1 then identifier = identifier .. c end @@ -134,23 +120,23 @@ function visualizer.flush_line(str, nested) ]] local state, instr, inesc, word = 0, false, false, nil buffers.currentcolors = colors - local code, comment = match(str,"^(.-)%-%-%[%[(.*)$") + local code, comment = string.match(str,"^(.-)%-%-%[%[(.*)$") if comment then -- process the code and then flush the comment elseif incomment then - comment, code = match(str,"^(.-)%]%](.*)$") + comment, code = string.match(str,"^(.-)%]%](.*)$") if comment then -- flush the comment and then process the code - for c in utfcharacters(comment) do - if c == " " then texsprint(ctxcatcodes,"\\obs") else texwrite(c) end + for c in string.utfcharacters(comment) do + if c == " " then tex.sprint(tex.ctxcatcodes,"\\obs") else tex.write(c) end end - state = changestate(states['--'], state) - texwrite("]]") - state = finishstate(state) + state = buffers.changestate(states['--'], state) + tex.write("]]") + state = buffers.finishstate(state) incomment = false else - for c in utfcharacters(str) do - if c == " " then texsprint(ctxcatcodes,"\\obs") else texwrite(c) end + for c in string.utfcharacters(str) do + if c == " " then tex.sprint(tex.ctxcatcodes,"\\obs") else tex.write(c) end end end comment = nil @@ -158,27 +144,27 @@ function visualizer.flush_line(str, nested) code = str end if code and code ~= "" then - local pre, post = match(code,"^(.-)%-%-(.*)$") + local pre, post = string.match(code,"^(.-)%-%-(.*)$") if pre then code = pre end local p, s, i = nil, nil, 0 - for c in utfcharacters(code) do + for c in string.utfcharacters(code) do i = i + 1 if instr then if p then - texwrite(p) + tex.write(p) p = nil end if c == s then if inesc then - texwrite(c) + tex.write(c) inesc = false else - state = changestate(states[c],state) + state = buffers.changestate(states[c],state) instr = false - texwrite(c) - state = finishstate(state) + tex.write(c) + state = buffers.finishstate(state) end s = nil else @@ -191,14 +177,14 @@ function visualizer.flush_line(str, nested) end elseif c == "[" then if word then - texwrite(word) + tex.write(word) word = nil end if p == "[" then inlongstring = true - state = changestate(states["[["],state) - texwrite(p,c) - state = finishstate(state) + state = buffers.changestate(states["[["],state) + tex.write(p,c) + state = buffers.finishstate(state) p = nil else if p then @@ -208,14 +194,14 @@ function visualizer.flush_line(str, nested) end elseif c == "]" then if word then - texwrite(word) + tex.write(word) word = nil end if p == "]" then inlongstring = false - state = changestate(states["]]"],state) - texwrite(p,c) - state = finishstate(state) + state = buffers.changestate(states["]]"],state) + tex.write(p,c) + state = buffers.finishstate(state) p = nil else if p then @@ -225,9 +211,9 @@ function visualizer.flush_line(str, nested) end else if p then - state = changestate(states[p],state) - texwrite(p) - state = finishstate(state) + state = buffers.changestate(states[p],state) + tex.write(p) + state = buffers.finishstate(state) p = nil end if c == " " or c == "\t" then @@ -244,48 +230,48 @@ function visualizer.flush_line(str, nested) word = nil end instr = true - state = changestate(states[c],state) + state = buffers.changestate(states[c],state) state, i = written(state,c,i) - state = finishstate(state) + state = buffers.finishstate(state) s = c - elseif find(c,"^[%a]$") then - state = finishstate(state) + elseif string.find(c,"^[%a]$") then + state = buffers.finishstate(state) if word then word = word .. c else word = c end - elseif word and (#word > 1) and find(c,"^[%d%.%_]$") then + elseif word and (#word > 1) and string.find(c,"^[%d%.%_]$") then if word then word = word .. c else word = c end else flush_pas_word(word) word = nil - state = changestate(states[c],state) - texwrite(c) + state = buffers.changestate(states[c],state) + tex.write(c) instr = (c == '"') end end end if p then - texwrite(p) + tex.write(p) -- state, i = written(state,p,i) p = nil end flush_pas_word(word) if post then - state = changestate(states['--'], state) - texwrite("--") - state = finishstate(state) - for c in utfcharacters(post) do + state = buffers.changestate(states['--'], state) + tex.write("--") + state = buffers.finishstate(state) + for c in string.utfcharacters(post) do state, i = written(state,c,i) end end end if comment then incomment = true - state = changestate(states['--'], state) - texwrite("[[") - state = finishstate(state) - -- texwrite(comment) -- maybe also split and - for c in utfcharacters(comment) do + state = buffers.changestate(states['--'], state) + tex.write("[[") + state = buffers.finishstate(state) + -- tex.write(comment) -- maybe also split and + for c in string.utfcharacters(comment) do state, i = written(state,c,i) end end - state = finishstate(state) + state = buffers.finishstate(state) end \ No newline at end of file