OSDN Git Service

Fix ticket 29311.
[luatex-ja/luatexja.git] / src / ltj-tangle.lua
1 --
2 -- luatexja/tangle.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.tangle',
6   date = '2011/04/01',
7   version = '0.1',
8   description = '',
9 })
10 module('luatexja.tangle', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
13 --! ixbase0 からの移植
14
15 local _DONE, _TEX, _STOP = 0, 1, 2
16 local _current_co, _interrupted
17 local _resume, _check
18
19 local resume_code =
20   "\\directlua{".._NAME..".resume()}\\relax"
21
22 function execute(func, ...)
23   if _current_co then
24     err("tangle is going now")
25   end
26   local args = { ... }
27   local co = coroutine.create(function()
28     return _DONE, { func(unpack(args)) }
29   end)
30   _current_co = co
31   _interrupted = false
32   return _check(coroutine.resume(co, ...))
33 end
34
35 function resume()
36   return _resume(false)
37 end
38
39 function interrupt()
40   return _resume(true)
41 end
42
43 function run_tex()
44   coroutine.yield(_TEX, {})
45 end
46
47 function suspend(...)
48   local intr = coroutine.yield(_STOP, { ... })
49   if intr then
50     _interrupted = true
51     error("*INTR*") -- this error is caught later
52   end
53 end
54
55 function _resume(intr)
56   if not _current_co then
57     err("tangle is not going")
58   end
59   local co = _current_co
60   return _check(coroutine.resume(co, intr))
61 end
62
63 function _check(costat, tstat, extra)
64   if not costat then  -- error in coroutine
65     _current_co = nil
66     if _interrupted then return end
67     err(tstat)
68   elseif tstat == _DONE then
69     _current_co = nil
70   elseif tstat == _TEX then
71     tex.print(resume_code)
72   end
73   return unpack(extra)
74 end
75
76 -- EOF
77