summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luatexja/ltj-tangle.lua
blob: 752d7ae2bb49b579bda85f7a3fad42c21786ed1d (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
--
-- luatexja/tangle.lua
--
luatexbase.provides_module({
  name = 'luatexja.tangle',
  date = '2011/05/14',
  description = '',
})
module('luatexja.tangle', package.seeall)
local err, warn, info, log = luatexbase.errwarinf(_NAME)

--! ixbase0 からの移植

local _DONE, _TEX, _STOP = 0, 1, 2
local _current_co, _interrupted
local _resume, _check

local resume_code =
  "\\directlua{".._NAME..".resume()}\\relax"

function execute(func, ...)
  if _current_co then
    err("tangle is going now")
  end
  local args = { ... }
  local co = coroutine.create(function()
    return _DONE, { func(unpack(args)) }
  end)
  _current_co = co
  _interrupted = false
  return _check(coroutine.resume(co, ...))
end

function resume()
  return _resume(false)
end

function interrupt()
  return _resume(true)
end

function run_tex()
  coroutine.yield(_TEX, {})
end

function suspend(...)
  local intr = coroutine.yield(_STOP, { ... })
  if intr then
    _interrupted = true
    error("*INTR*") -- this error is caught later
  end
end

function _resume(intr)
  if not _current_co then
    err("tangle is not going")
  end
  local co = _current_co
  return _check(coroutine.resume(co, intr))
end

function _check(costat, tstat, extra)
  if not costat then  -- error in coroutine
    _current_co = nil
    if _interrupted then return end
    err(tstat)
  elseif tstat == _DONE then
    _current_co = nil
  elseif tstat == _TEX then
    tex.print(resume_code)
  end
  return unpack(extra)
end

-- EOF