diff options
Diffstat (limited to 'tex/context/base/buff-ini.lua')
-rw-r--r-- | tex/context/base/buff-ini.lua | 123 |
1 files changed, 82 insertions, 41 deletions
diff --git a/tex/context/base/buff-ini.lua b/tex/context/base/buff-ini.lua index f76494c43..45288d18b 100644 --- a/tex/context/base/buff-ini.lua +++ b/tex/context/base/buff-ini.lua @@ -17,6 +17,7 @@ local trace_grab = false trackers.register("buffers.grab", function local trace_visualize = false trackers.register("buffers.visualize", function(v) trace_visualize = v end) local report_buffers = logs.reporter("buffers","usage") +local report_typeset = logs.reporter("buffers","typeset") local report_grabbing = logs.reporter("buffers","grabbing") local context = context @@ -25,6 +26,7 @@ local commands = commands local variables = interfaces.variables local settings_to_array = utilities.parsers.settings_to_array local formatters = string.formatters +local addsuffix = file.addsuffix local v_yes = variables.yes @@ -43,15 +45,23 @@ local function erase(name) end local function assign(name,str,catcodes) - cache[name] = { data = str, catcodes = catcodes } + cache[name] = { + data = str, + catcodes = catcodes, + typeset = false, + } end local function append(name,str) local buffer = cache[name] if buffer then - buffer.data = buffer.data .. str + buffer.data = buffer.data .. str + buffer.typeset = false else - cache[name] = { data = str } + cache[name] = { + data = str, + typeset = false, + } end end @@ -69,10 +79,40 @@ local function getlines(name) return buffer and splitlines(buffer.data) end -local function collectcontent(names,separator) -- no print - if type(names) == "string" then - names = settings_to_array(names) +local function getnames(name) + if type(name) == "string" then + return settings_to_array(name) + else + return name + end +end + +local function istypeset(name) + local names = getnames(name) + if #names == 0 then + return false + end + for i=1,#names do + local c = cache[names[i]] + if c and not c.typeset then + return false + end end + return true +end + +local function markastypeset(name) + local names = getnames(name) + for i=1,#names do + local c = cache[names[i]] + if c then + c.typeset = true + end + end +end + +local function collectcontent(name,separator) -- no print + local names = getnames(name) local nnames = #names if nnames == 0 then return getcontent("") -- default buffer @@ -91,12 +131,10 @@ local function collectcontent(names,separator) -- no print end end -local function loadcontent(names) -- no print - if type(names) == "string" then - names = settings_to_array(names) - end +local function loadcontent(name) -- no print + local names = getnames(name) local nnames = #names - local ok = false + local ok = false if nnames == 0 then ok = load(getcontent("")) -- default buffer elseif nnames == 1 then @@ -128,7 +166,6 @@ local function loadcontent(names) -- no print end end - buffers.raw = getcontent buffers.erase = erase buffers.assign = assign @@ -234,7 +271,7 @@ end function commands.grabbuffer(name,begintag,endtag,bufferdata,catcodes) -- maybe move \\ to call local dn = getcontent(name) if dn == "" then - nesting = 0 + nesting = 0 continue = false end if trace_grab then @@ -252,8 +289,8 @@ function commands.grabbuffer(name,begintag,endtag,bufferdata,catcodes) -- maybe nesting = nesting + lpegmatch(counter,bufferdata) local more = nesting > 0 if more then - dn = dn .. sub(bufferdata,2,-1) .. endtag - nesting = nesting - 1 + dn = dn .. sub(bufferdata,2,-1) .. endtag + nesting = nesting - 1 continue = true else if continue then @@ -275,10 +312,7 @@ function commands.grabbuffer(name,begintag,endtag,bufferdata,catcodes) -- maybe commands.doifelse(more) end --- The optional prefix hack is there for the typesetbuffer feature and --- in mkii we needed that (this hidden feature is used in a manual). - -local function prepared(name,list,prefix) -- list is optional +function commands.savebuffer(list,name,prefix) -- name is optional if not list or list == "" then list = name end @@ -289,37 +323,44 @@ local function prepared(name,list,prefix) -- list is optional if content == "" then content = "empty buffer" end - if prefix then - local name = file.addsuffix(name,"tmp") - return tex.jobname .. "-" .. name, content - else - return name, content + if prefix == v_yes then + name = addsuffix(tex.jobname .. "-" .. name,"tmp") end + io.savedata(name,content) end -local capsule = "\\starttext\n%s\n\\stoptext\n" -local command = "context %s" +local files = { } +local last = 0 -function commands.runbuffer(name,list,encapsulate) - local name, content = prepared(name,list) - if encapsulate then - content = format(capsule,content) +function commands.runbuffer(name,encapsulate) -- we used to compare the saved file with content + local names = getnames(name) + local filename = files[name] + if not filename then + last = last + 1 + filename = formatters["%s-typeset-buffer-%03i"](tex.jobname,last) + files[name] = filename end - local data = io.loaddata(name) - if data ~= content then + if not istypeset(names) then if trace_run then - report_buffers("changes in %a, processing forced",name) + report_typeset("changes in %a, processing forced",name) end - io.savedata(name,content) - os.execute(format(command,name)) + local filename = addsuffix(filename,"tmp") + local content = collectcontent(names,nil) or "" + if content == "" then + content = "empty buffer" + end + if encapsulate then + content = formatters["\\starttext\n%s\n\\stoptext\n"](content) + end + report_typeset("\n") + io.savedata(filename,content) + os.execute(formatters["context %s"](filename)) + report_typeset("\n") + markastypeset(names) elseif trace_run then - report_buffers("no changes in %a, not processed",name) + report_typeset("no changes in %a, not processed",name) end -end - -function commands.savebuffer(list,name,prefix) -- name is optional - local name, content = prepared(name,list,prefix==v_yes) - io.savedata(name,content) + context(addsuffix(filename,"pdf")) end function commands.getbuffer(name) |