diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/modules/mkiv/m-scite.mkiv')
-rw-r--r-- | Master/texmf-dist/tex/context/modules/mkiv/m-scite.mkiv | 273 |
1 files changed, 225 insertions, 48 deletions
diff --git a/Master/texmf-dist/tex/context/modules/mkiv/m-scite.mkiv b/Master/texmf-dist/tex/context/modules/mkiv/m-scite.mkiv index a7d9f8b5c3e..48f1022ad59 100644 --- a/Master/texmf-dist/tex/context/modules/mkiv/m-scite.mkiv +++ b/Master/texmf-dist/tex/context/modules/mkiv/m-scite.mkiv @@ -45,7 +45,7 @@ -- todo: hook into the pretty print code -- todo: a simple catcode regime with only \ { } -local gsub, sub, find = string.gsub, string.sub, string.find +local gsub, sub, find, lower = string.gsub, string.sub, string.find, string.lower local concat = table.concat local formatters = string.formatters local lpegmatch = lpeg.match @@ -57,13 +57,14 @@ buffers.scite = scite -- context output: local f_def_color = formatters["\\definecolor[slxc%s][h=%02X%02X%02X]%%"] -local f_fore_none = formatters["\\unexpanded\\def\\slx%s#1{{\\slxc%s#1}}%%"] -local f_fore_bold = formatters["\\unexpanded\\def\\slx%s#1{{\\slxc%s\\bf#1}}%%"] -local f_none_bold = formatters["\\unexpanded\\def\\slx%s#1{{\\bf#1}}%%"] -local f_none_none = formatters["\\unexpanded\\def\\slx%s#1{{#1}}%%"] -local f_texstyled = formatters["\\slx%s{%s}"] -local f_hanging = formatters["\\slxb{%s}%s\\slxe"] +local f_fore_none = formatters["\\unexpanded\\def\\slx%s#1{{\\slxc%s#1}\\slxbreak}%%"] +local f_fore_bold = formatters["\\unexpanded\\def\\slx%s#1{{\\slxc%s\\bf#1}\\slxbreak}%%"] +local f_none_bold = formatters["\\unexpanded\\def\\slx%s#1{{\\bf#1}\\slxbreak}%%"] +local f_none_none = formatters["\\unexpanded\\def\\slx%s#1{{#1}\\slxbreak}%%"] +local f_texstyled = formatters["\\slx%s{%s}\\slxbreak"] +local f_hanging = formatters["\\slxb{%r}%s\\slxe"] -- we need to round: lua 5.3 +local v_none = interfaces.variables.none local f_mapping = [[ \let\string\slxL\string\letterleftbrace @@ -152,7 +153,9 @@ local function exportstyled(lexer,text) local position = result[i+1] local txt = sub(text,start,position-1) txt = lpegmatch(replacer,txt) - if whites[style] then + if txt == "" then + -- skip + elseif whites[style] then buffer[#buffer+1] = txt else buffer[#buffer+1] = f_texstyled(style,txt) @@ -167,53 +170,147 @@ function scite.installcommands() context(exportcolors()) end -local p1 = lpeg.tsplitat(lpeg.patterns.newline) -local p2 = lpeg.P("\\slxS ")^1 -local p3 = lpeg.Cs((lpeg.Cp() * lpeg.P(" ") * lpeg.Cp() / function(b,e) return "\\slxF{" .. (e-b-1) .. "}" end - + (1-lpeg.P(" "))^1)^0) +local function slxF(b,e) + local d = (e - b)/6 + if d > 0 then + return "\\slxF{" .. d .. "}" + else + return " " + end +end --- slxb could have a \hskip so then we can replace the slxS's +local p1 = lpeg.tsplitat(lpeg.patterns.newline) +local p2 = lpeg.P("\\slxS ") +local p3 = p2^1 +local p4 = lpeg.Cs( ( (lpeg.Cp() * p2 * p2^1 * lpeg.Cp()) / slxF + lpeg.P(1) )^0 ) local function indent(str) local l = lpegmatch(p1,str) for i=1,#l do local s = l[i] if #s > 0 then - local n = lpegmatch(p2,s) + local n = lpegmatch(p3,s) if n then - n = (n-1)/6 + n = (n-1)/6 -- length of "\\slxS " else n = 0 end -s = lpegmatch(p3,s) -- can be combined + if n > 0 then + s = sub(s,n*6+1) + end + s = lpegmatch(p4,s) -- can be combined l[i] = f_hanging(n,s) -- "\\slxb{%s}%s\\slxe " --- print(">",s) --- print("<",l[i]) end end return concat(l,"\n") end +local assignbuffer = buffers.assign +local getcontent = buffers.getcontent +local loaddata = io.loaddata +local loadedlexers = scite.loadedlexers + local function lexdata(data,lexname) - local styled = exportstyled(scite.loadedlexers[lexname],data or "") - styled = indent(styled) - buffers.assign("lex",styled) + if not data then + data = "" + elseif data ~= "" then + if data and not find(data,"[\r\n]$") then + -- fix for lexbyline + data = data .. "\r" + end + if lexname == v_none then +-- data = string.formatters["%!tex!"](data) + data = exportstyled(loadedlexers["dummy"] or loadedlexers.tex,data) + else + data = exportstyled(loadedlexers[lexname] or loadedlexers.tex,data) + end + data = indent(data) + end +io.savedata("temp.logs ",data) + assignbuffer("lex",data) end scite.lexdata = lexdata function scite.lexbuffer(name,lexname) - lexdata(buffers.getcontent(name) or "",lexname or "tex") + lexdata(getcontent(name) or "",lexname or "tex") end function scite.lexfile(filename,lexname) - lexdata(io.loaddata(filename) or "",lexname or file.suffix(filename)) + lexdata(loaddata(filename) or "",lexname or file.suffix(filename)) +end + +interfaces.implement { + name = "scitelexfile", + arguments = "string", + actions = scite.lexfile, +} + +interfaces.implement { + name = "scitelexbuffer", + arguments = { "string", "string" }, + actions = scite.lexbuffer, +} + +interfaces.implement { + name = "sciteinstallcommands", + actions = scite.installcommands, +} + +local startInlineScite = context.startInlineScite +local stopInlineScite = context.stopInlineScite +local startDisplayScite = context.startDisplayScite +local stopDisplayScite = context.stopDisplayScite + +local lexdata = buffers.scite.lexdata + +local handler = visualizers.newhandler { + startinline = function(settings) startInlineScite(settings.name or "") end, + stopinline = function(settings) stopInlineScite() end, + startdisplay = function(settings) startDisplayScite(settings.name or "") end, + stopdisplay = function(settings) stopDisplayScite() end, + noescape = true, +} + +local knownlexers = scite.knownlexers + +local parser = function(content,specification) + local method = lower(specification.method) + lexdata(content,knownlexers[method] or method) end --- html output +local visualizer = { + handler = handler, + parser = parser, +} + +visualizers.register("cld", visualizer) +visualizers.register("tex", visualizer) +visualizers.register("lua", visualizer) +visualizers.register("mps", visualizer) +visualizers.register("xml", visualizer) +visualizers.register("bibtex",visualizer) +visualizers.register("btx", visualizer) +visualizers.register("web", visualizer) +visualizers.register("cpp", visualizer) +visualizers.register("txt", visualizer) +visualizers.register("bnf", visualizer) +visualizers.register("sql", visualizer) \stopluacode +\definetyping[TEX] [option=cld] +\definetyping[LUA] [option=lua] +\definetyping[BTX] [option=bibtex] +\definetyping[MPS] [option=mps] +\definetyping[MP] [option=mps] +\definetyping[CPP] [option=web] +\definetyping[WEB] [option=web] +\definetyping[TXT] [option=txt] +\definetyping[BNF] [option=bnf] % I might use this in the metafun manual. +\definetyping[SQL] [option=sql] % To be tested in an upcoming manual. +\definetyping[NONE][option=none] + % This is a preliminary interface. \unprotect @@ -221,22 +318,38 @@ end \newdimen\scitespaceskip \unexpanded\def\buff_scite_slxb#1% - {%\begingroup - \hangindent\numexpr#1+2\relax\scitespaceskip + {\hangindent\numexpr#1+2\relax\scitespaceskip + \hskip#1\scitespaceskip \hangafter 1\relax} \unexpanded\def\buff_scite_slxe - {\par - }%\endgroup} - -\unexpanded\def\installscitecommands - {\ctxlua{buffers.scite.installcommands()}% - \let\installscitecommands\relax - \scitespaceskip\fontcharwd\font`0\relax - \unexpanded\def\slxS{\hskip\scitespaceskip}% - \unexpanded\def\slxF##1{\hskip##1\scitespaceskip\relax}% + {\par} + +\unexpanded\def\buff_scite_slxs {\hskip\scitespaceskip\relax} +\unexpanded\def\buff_scite_slxf#1{\hskip#1\scitespaceskip\relax} + +\unexpanded\def\installscitecommandsinline + {\scitespaceskip\interwordspace % \fontcharwd\font`0\relax % brrrrr + \let\slxb\gobbleoneargument + \let\slxe\space + \let\installscitecommandsinline\relax} + +\unexpanded\def\installscitecommandsdisplay + {\scitespaceskip\interwordspace % \fontcharwd\font`0\relax % brrrrr \let\slxb\buff_scite_slxb - \let\slxe\buff_scite_slxe} + \let\slxe\buff_scite_slxe + \let\installscitecommandsdisplay\relax} + +\clf_sciteinstallcommands + +\installscitecommandsinline + +\let\slxS\buff_scite_slxs +\let\slxF\buff_scite_slxf + +\def\module_scite_inherit_typing + {\buff_verbatim_initialize_typing_one + \buff_verbatim_set_line_margin} \unexpanded\def\startscite {\begingroup @@ -254,10 +367,12 @@ end {\dosingleargument\module_scite_file} \unexpanded\def\module_scite_file[#1]% - {\start - \ctxlua{buffers.scite.lexfile("#1")}% - \installscitecommands + {\begingroup + \setcatcodetable\ctxcatcodes % needed in xml + \clf_scitelexfile{#1}% \tt + \installscitecommandsdisplay + \module_scite_inherit_typing \dontcomplain \raggedright \startcontextcode @@ -265,27 +380,80 @@ end \getbuffer[lex] \stoplines \stopcontextcode - \stop} + \endgroup} \unexpanded\def\scitebuffer - {\dodoubleargument\module_scite_buffer} + {\dodoubleempty\module_scite_buffer} \unexpanded\def\module_scite_buffer[#1][#2]% - {\start + {\begingroup + \setcatcodetable\ctxcatcodes % needed in xml \ifsecondargument - \ctxlua{buffers.scite.lexbuffer("#2","#1")}% + \clf_scitelexbuffer{#2}{#1}% \else - \ctxlua{buffers.scite.lexbuffer("#1","tex")}% + \clf_scitelexbuffer{#1}{tex}% \fi - \installscitecommands \tt + \installscitecommandsdisplay + \module_scite_inherit_typing \dontcomplain \raggedright - \setcatcodetable\ctxcatcodes % needed in xml \startlines - \getbuffer[lex]% + \getbuffer[lex] \stoplines - \stop} + \endgroup} + +\unexpanded\def\sciteinlinebuffer + {\dodoubleempty\module_scite_buffer_inline} + +\unexpanded\def\module_scite_buffer_inline[#1][#2]% + {\dontleavehmode + \begingroup + \setcatcodetable\ctxcatcodes % needed in xml + \ifsecondargument + \clf_scitelexbuffer{#2}{#1}% + \else + \clf_scitelexbuffer{#1}{tex}% + \fi + \tt + \installscitecommandsinline + \module_scite_inherit_typing + \dontcomplain + \getbuffer[lex]% + \endgroup} + +\unexpanded\def\startInlineScite#1% + {\dontleavehmode + \begingroup + \setcatcodetable\ctxcatcodes % needed in xml + \tt + \installscitecommandsinline + \dontcomplain + \ignorespaces + \clf_getbuffercontent{lex}% much faster than getbuffer + \removeunwantedspaces} + +\unexpanded\def\stopInlineScite + {\endgroup} + +\unexpanded\def\startDisplayScite#1% + {\begingroup + \setcatcodetable\ctxcatcodes % needed in xml + \tt + \installscitecommandsdisplay + \dontcomplain +\buff_verbatim_initialize_typing_one +\buff_verbatim_set_line_margin + \raggedright + \startlines + \clf_getbuffer{lex}} + +\unexpanded\def\stopDisplayScite + {\stoplines + \endgroup} + +\unexpanded\def\slxbreak + {\allowbreak} \protect @@ -335,4 +503,13 @@ end % \getbuffer[demo] \scitebuffer[demo] +\startbuffer[foo] +This is text. % line 1 +This is text. % line 2 +\stopbuffer + +\scitebuffer[none][foo] + +\sciteinlinebuffer[none][foo] + \stoptext |