mtx-texworks.lua /size: 3754 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['mtx-texworks'] = {
2    version   = 1.002,
3    comment   = "companion to mtxrun.lua",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local helpinfo = [[
10<?xml version="1.0"?>
11<application>
12 <metadata>
13  <entry name="name">mtx-texworks</entry>
14  <entry name="detail">TeXworks Startup Script</entry>
15  <entry name="version">1.00</entry>
16 </metadata>
17 <flags>
18  <category name="basic">
19   <subcategory>
20    <flag name="start"><short>[<ref name="verbose]"/>   start texworks</short></flag>
21    <flag name="test"><short>report what will happen</short></flag>
22   </subcategory>
23  </category>
24 </flags>
25</application>
26]]
27
28local application = logs.application {
29    name     = "mtx-texworks",
30    banner   = "TeXworks Startup Script 1.00",
31    helpinfo = helpinfo,
32}
33
34local report = application.report
35
36scripts          = scripts          or { }
37scripts.texworks = scripts.texworks or { }
38
39local texworkspaths = {
40    "completion",
41    "configuration",
42    "dictionaries",
43    "translations",
44    "scripts",
45    "templates",
46    "TUG"
47}
48
49local texworkssignal = "texworks-context.rme"
50local texworkininame = "texworks.ini"
51
52function scripts.texworks.start(indeed)
53    local workname = (os.type == "windows" and "texworks.exe") or "texworks"
54    local fullname = nil
55    local binpaths = file.splitpath(os.getenv("PATH")) or file.splitpath(os.getenv("path"))
56    local usedsignal = texworkssignal
57    local datapath = resolvers.findfile(usedsignal,"other text files") or ""
58    if datapath ~= "" then
59        datapath  = file.dirname(datapath) -- data
60        if datapath == "" then
61            datapath = resolvers.cleanpath(lfs.currentdir())
62        end
63    else
64        usedsignal = texworkininame
65        datapath = resolvers.findfile(usedsignal,"other text files") or ""
66        if datapath == "" then
67            usedsignal = string.lower(usedsignal)
68            datapath = resolvers.findfile(usedsignal,"other text files") or ""
69        end
70        if datapath ~= "" and lfs.isfile(datapath) then
71            datapath  = file.dirname(datapath) -- TUG
72            datapath  = file.dirname(datapath) -- data
73            if datapath == "" then
74                datapath = resolvers.cleanpath(lfs.currentdir())
75            end
76        end
77    end
78    if datapath == "" then
79        report("invalid datapath, maybe you need to regenerate the file database")
80        return false
81    end
82    if not binpaths or #binpaths == 0 then
83        report("invalid binpath")
84        return false
85    end
86    for i=1,#binpaths do
87        local p = file.join(binpaths[i],workname)
88        if lfs.isfile(p) and lfs.attributes(p,"size") > 10000 then -- avoind stub
89            fullname = p
90            break
91        end
92    end
93    if not fullname then
94        report("unable to locate %s",workname)
95        return false
96    end
97    for i=1,#texworkspaths do
98        dir.makedirs(file.join(datapath,texworkspaths[i]))
99    end
100    os.setenv("TW_INIPATH",datapath)
101    os.setenv("TW_LIBPATH",datapath)
102    if not indeed or environment.argument("verbose") then
103        report("used signal: %s", usedsignal)
104        report("data path  : %s", datapath)
105        report("full name  : %s", fullname)
106        report("set paths  : TW_INIPATH TW_LIBPATH")
107    end
108    if indeed then
109        os.launch(fullname)
110    end
111end
112
113if environment.argument("start") then
114    scripts.texworks.start(true)
115elseif environment.argument("test") then
116    scripts.texworks.start()
117elseif environment.argument("exporthelp") then
118    application.export(environment.argument("exporthelp"),environment.files[1])
119else
120    application.help()
121end
122