OSDN Git Service

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