summaryrefslogtreecommitdiff
path: root/scripts/context/lua/mtx-texworks.lua
blob: ae5f2afa4193f1fb41e74c68d6904ae36d7da325 (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
if not modules then modules = { } end modules ['mtx-texworks'] = {
    version   = 1.002,
    comment   = "companion to mtxrun.lua",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local helpinfo = [[
<?xml version="1.0"?>
<application>
 <metadata>
  <entry name="name">mtx-texworks</entry>
  <entry name="detail">TeXworks Startup Script</entry>
  <entry name="version">1.00</entry>
 </metadata>
 <flags>
  <category name="basic">
   <subcategory>
    <flag name="start"><short>[<ref name="verbose]"/>   start texworks</short></flag>
    <flag name="test"><short>report what will happen</short></flag>
   </subcategory>
  </category>
 </flags>
</application>
]]

local application = logs.application {
    name     = "mtx-texworks",
    banner   = "TeXworks Startup Script 1.00",
    helpinfo = helpinfo,
}

local report = application.report

scripts          = scripts          or { }
scripts.texworks = scripts.texworks or { }

local texworkspaths = {
    "completion",
    "configuration",
    "dictionaries",
    "translations",
    "scripts",
    "templates",
    "TUG"
}

local texworkssignal = "texworks-context.rme"
local texworkininame = "texworks.ini"

function scripts.texworks.start(indeed)
    local workname = (os.type == "windows" and "texworks.exe") or "texworks"
    local fullname = nil
    local binpaths = file.splitpath(os.getenv("PATH")) or file.splitpath(os.getenv("path"))
    local usedsignal = texworkssignal
    local datapath = resolvers.findfile(usedsignal,"other text files") or ""
    if datapath ~= "" then
        datapath  = file.dirname(datapath) -- data
        if datapath == "" then
            datapath = resolvers.cleanpath(lfs.currentdir())
        end
    else
        usedsignal = texworkininame
        datapath = resolvers.findfile(usedsignal,"other text files") or ""
        if datapath == "" then
            usedsignal = string.lower(usedsignal)
            datapath = resolvers.findfile(usedsignal,"other text files") or ""
        end
        if datapath ~= "" and lfs.isfile(datapath) then
            datapath  = file.dirname(datapath) -- TUG
            datapath  = file.dirname(datapath) -- data
            if datapath == "" then
                datapath = resolvers.cleanpath(lfs.currentdir())
            end
        end
    end
    if datapath == "" then
        report("invalid datapath, maybe you need to regenerate the file database")
        return false
    end
    if not binpaths or #binpaths == 0 then
        report("invalid binpath")
        return false
    end
    for i=1,#binpaths do
        local p = file.join(binpaths[i],workname)
        if lfs.isfile(p) and lfs.attributes(p,"size") > 10000 then -- avoind stub
            fullname = p
            break
        end
    end
    if not fullname then
        report("unable to locate %s",workname)
        return false
    end
    for i=1,#texworkspaths do
        dir.makedirs(file.join(datapath,texworkspaths[i]))
    end
    os.setenv("TW_INIPATH",datapath)
    os.setenv("TW_LIBPATH",datapath)
    if not indeed or environment.argument("verbose") then
        report("used signal: %s", usedsignal)
        report("data path  : %s", datapath)
        report("full name  : %s", fullname)
        report("set paths  : TW_INIPATH TW_LIBPATH")
    end
    if indeed then
        os.launch(fullname)
    end
end

if environment.argument("start") then
    scripts.texworks.start(true)
elseif environment.argument("test") then
    scripts.texworks.start()
elseif environment.argument("exporthelp") then
    application.export(environment.argument("exporthelp"),environment.files[1])
else
    application.help()
end