summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkiv/m-scite.mkiv
blob: c381475d0eee17f64032a8ea9001f394f567e34e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
%D \module
%D   [       file=m-scite,
%D        version=2014.04.28,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=\SCITE\ lexers,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% We can simplify the scite lexers, as long as we're able to return the
% lexed result table and provide alexer module with the functions that
% the lexer expects (so I need to decipher the cxx file).
%
% lexer._TOKENSTYLES : table
% lexer._CHILDREN    : flag
% lexer._EXTRASTYLES : table
% lexer._GRAMMAR     : flag
%
% lexers.load        : function
% lexers.lex         : function
%
% And some properties that map styles onto scintilla styling. I get the
% impression that we end up with something simpler, a hybrid between the
% scite lexing and the current context way, so we get an intermediate
% step, with some penalty for context, but at least I don't have to
% maintain two sets (three sets as we also have a line based series).

% TODO: as these files are in tds we can locate them and set the lexer root
% to that one. Currently we're on: we're on context/documents.

% TODO: tab

% This is an experiment: eventually we need to hook it into the verbatim code
% and deal with widow lines and so.

\startluacode

-- todo: merge with collapse
-- todo: prehash whitespaces

-- todo: hook into the pretty print code
-- todo: a simple catcode regime with only \ { }

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
local setmetatableindex = table.setmetatableindex

local scite   = require("util-sci")
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_mapping   = [[
\let\string\slxL\string\letterleftbrace
\let\string\slxR\string\letterrightbrace
\let\string\slxM\string\letterdollar
\let\string\slxV\string\letterbar
\let\string\slxU\string\letterhat
\let\string\slxD\string\letterunderscore
\let\string\slxH\string\letterhash
\let\string\slxB\string\letterbackslash
\let\string\slxP\string\letterpercent
\let\string\slxT\string\lettertilde
\let\string\slxS\string\fixedspace
%]]

local replacer = lpeg.replacer {
    ["{"]  = "\\slxL ",
    ["}"]  = "\\slxR ",
    ["$"]  = "\\slxM ",
    ["^"]  = "\\slxU ",
    ["_"]  = "\\slxD ",
    ["|"]  = "\\slxV ",
    ["#"]  = "\\slxH ",
    ["\\"] = "\\slxB ",
    ["%"]  = "\\slxP ",
    ["~"]  = "\\slxT ",
    [" "]  = "\\slxS ", -- can be made more efficient: \\slxF{n}
}

local colors = nil

local function exportcolors()
    if not colors then
        scite.loadscitelexer()
        local function black(f)
            return (f[1] == f[2]) and (f[2] == f[3]) and (f[3] == 0)
        end
        local result, r = { f_mapping }, 1
        for k, v in table.sortedhash(lexer.context.styles) do
            local fore = v.fore
            if fore and not black(fore) then
                r = r + 1
                result[r] = f_def_color(k,fore[1],fore[2] or fore[1],fore[3] or fore[1])
            end
        end
        r = r + 1
        result[r] = "%"
        for k, v in table.sortedhash(lexer.context.styles) do
            local bold = v.bold
            local fore = v.fore
            r = r + 1
            if fore and not black(fore) then
                if bold then
                    result[r] = f_fore_bold(k,k)
                else
                    result[r] = f_fore_none(k,k)
                end
            else
                if bold then
                    result[r] = f_none_bold(k)
                else
                    result[r] = f_none_none(k)
                end
            end
        end
        colors = concat(result,"\n")
    end
    return colors
end

local function exportwhites()
    return setmetatableindex(function(t,k)
        local v = find(k,"white") and true or false
        t[k] = v
        return v
    end)
end

local function exportstyled(lexer,text)
    local result = lexer.lex(lexer,text,0)
    local start = 1
    local whites = exportwhites()
    local buffer = { }
    for i=1,#result,2 do
        local style = result[i]
        local position = result[i+1]
        local txt = sub(text,start,position-1)
        txt = lpegmatch(replacer,txt)
        if whites[style] then
            buffer[#buffer+1] = txt
        else
            buffer[#buffer+1] = f_texstyled(style,txt)
        end
        start = position
    end
    buffer = concat(buffer)
    return buffer
end

function scite.installcommands()
    context(exportcolors())
end

local function slxF(b,e)
    local d = (e - b)/6
    if d > 0 then
        return "\\slxF{" .. d .. "}"
    else
        return " "
    end
end

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(p3,s)
            if n then
                n = (n-1)/6 -- length of "\\slxS "
            else
                n = 0
            end
            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 "
        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(loadedlexers[lexname],data or "")
    styled = indent(styled)
    assignbuffer("lex",styled)
end

scite.lexdata = lexdata

function scite.lexbuffer(name,lexname)
    lexdata(getcontent(name) or "",lexname or "tex")
end

function scite.lexfile(filename,lexname)
    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,
}

local knownlexers = scite.knownlexers

local parser = function(content,specification)
    local method = lower(specification.method)
    lexdata(content,knownlexers[method] or method)
end

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)
-----------.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]

% This is a preliminary interface.

\unprotect

\newdimen\scitespaceskip

\unexpanded\def\buff_scite_slxb#1%
  {\hangindent\numexpr#1+2\relax\scitespaceskip
   \hskip#1\scitespaceskip
   \hangafter 1\relax}

\unexpanded\def\buff_scite_slxe
  {\par}

\unexpanded\def\buff_scite_slxs  {\hskip\scitespaceskip}
\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\relax
   \let\installscitecommandsinline\relax}

\unexpanded\def\installscitecommandsdisplay
  {\scitespaceskip\interwordspace % \fontcharwd\font`0\relax % brrrrr
   \let\slxb\buff_scite_slxb
   \let\slxe\buff_scite_slxe
   \let\installscitecommandsdisplay\relax}

\clf_sciteinstallcommands

\installscitecommandsinline

\let\slxS\buff_scite_slxs
\let\slxF\buff_scite_slxf

\unexpanded\def\startscite
  {\begingroup
   \dosingleempty\buff_scite_start}

\def\buff_scite_start[#1]%
  {\edef\currentscitelexer{#1}%
   \dostartbuffer[@scite@][startscite][stopscite]}

\unexpanded\def\stopscite
  {\scitebuffer[\ifx\currentscitelexer\empty tex\else\currentscitelexer\fi][@scite@]%
   \endgroup}

\unexpanded\def\scitefile
  {\dosingleargument\module_scite_file}

\unexpanded\def\module_scite_file[#1]%
  {\begingroup
   \setcatcodetable\ctxcatcodes % needed in xml
   \clf_scitelexfile{#1}%
   \tt
   \installscitecommandsdisplay
   \dontcomplain
   \raggedright
   \startcontextcode
   \startlines
   \getbuffer[lex]
   \stoplines
   \stopcontextcode
   \endgroup}

\unexpanded\def\scitebuffer
  {\dodoubleargument\module_scite_buffer}

\unexpanded\def\module_scite_buffer[#1][#2]%
  {\begingroup
   \setcatcodetable\ctxcatcodes % needed in xml
   \ifsecondargument
     \clf_scitelexbuffer{#2}{#1}%
   \else
     \clf_scitelexbuffer{#1}{tex}%
   \fi
   \tt
   \installscitecommandsdisplay
   \dontcomplain
   \raggedright
   \startlines
   \getbuffer[lex]%
   \stoplines
   \endgroup}

\unexpanded\def\sciteinlinebuffer
  {\dodoubleargument\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
   \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
   \raggedright
   \startlines
   \clf_getbuffer{lex}}

\unexpanded\def\stopDisplayScite
  {\stoplines
   \endgroup}

\protect

\continueifinputfile{m-scite.mkiv}

\setupbodyfont[dejavu,8pt]

\setuplayout
  [width=middle,
   height=middle,
   header=1cm,
   footer=1cm,
   topspace=1cm,
   bottomspace=1cm,
   backspace=1cm]

\startbuffer[demo]
\startsubsubject[title={oeps}]

\startMPcode
    draw fullcircle
        scaled 2cm
        withpen pencircle scaled 1mm
        withcolor .5green;
    draw textext (
        lua (
            "local function f(s) return string.upper(s) end mp.quoted(f('foo'))"
        )
    ) withcolor .5red ;
\stopMPcode

\startluacode
    context("foo")
\stopluacode

\stopsubsubject
\stopbuffer

\starttext

% \scitefile[../lexers/scite-context-lexer.lua]  \page
% \scitefile[t:/manuals/about/about-metafun.tex] \page
% \scitefile[t:/sources/strc-sec.mkiv]           \page
% \scitefile[e:/tmp/mp.w]                        \page
% \scitefile[t:/manuals/hybrid/tugboat.bib]        \page
\scitefile[e:/tmp/test.bib]        \page

% \getbuffer[demo] \scitebuffer[demo]

\stoptext