l-pdfview.lua /size: 5716 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['l-pdfview'] = {
2    version   = 1.001,
3    comment   = "companion to mtx-context.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
9-- Todo: add options in cnf file
10
11-- Todo: figure out pdfopen/pdfclose on linux. Calling e.g. okular directly
12-- doesn't work in linux when issued from scite as it blocks the editor (no
13-- & possible or so). Unfortunately pdfopen keeps changing with not keeping
14-- downward compatibility (command line arguments and so).
15
16-- no 2>&1 any more, needs checking on windows
17
18local format, concat = string.format, table.concat
19
20local report  = logs.reporter("pdfview")
21local replace = utilities.templates.replace
22
23pdfview = pdfview or { }
24
25local opencalls  -- a table with templates that open a given pdf document
26local closecalls -- a table with templates that close a given pdf document
27local allcalls   -- a table with templates that close all open pdf documents
28local runner     -- runner function
29local expander   -- filename cleanup function
30
31if os.type == "windows" then
32
33    -- os.setenv("path",os.getenv("path") .. ";" .. "c:/data/system/pdf-xchange")
34    -- os.setenv("path",os.getenv("path") .. ";" .. "c:/data/system/sumatrapdf")
35
36    -- start is more flexible as it locates binaries in more places and doesn't lock
37
38    opencalls = {
39        ['default']     = [[pdfopen --rxi --file "%filename%"]],
40        ['acrobat']     = [[pdfopen --rxi --file "%filename%"]],
41        ['fullacrobat'] = [[pdfopen --axi --file "%filename%"]],
42        ['okular']      = [[start "test" okular.exe --unique "%filename%"]],
43        ['pdfxcview']   = [[start "test" pdfxcview.exe /A "nolock=yes=OpenParameters" "%filename%"]],
44        ['sumatra']     = [[start "test" sumatrapdf.exe -reuse-instance -bg-color 0xCCCCCC "%filename%"]],
45        ['auto']        = [[start "" "%filename%"]],
46    }
47    closecalls= {
48        ['default']     = [[pdfclose --file "%filename%"]],
49        ['acrobat']     = [[pdfclose --file "%filename%"]],
50        ['okular']      = false,
51        ['pdfxcview']   = false, -- [[pdfxcview.exe /close:discard "%filename%"]],
52        ['sumatra']     = false,
53        ['auto']        = false,
54    }
55    allcalls = {
56        ['default']     = [[pdfclose --all]],
57        ['acrobat']     = [[pdfclose --all]],
58        ['okular']      = false,
59        ['pdfxcview']   = false,
60        ['sumatra']     = false,
61        ['auto']        = false,
62    }
63
64    pdfview.method = "acrobat" -- no longer useful due to green pop up line and clashing reader/full
65 -- pdfview.method = "pdfxcview"
66    pdfview.method = "sumatra"
67
68    runner = function(template,variables)
69        local cmd = replace(template,variables)
70     -- cmd = cmd  .. " > /null"
71        report("command: %s",cmd)
72        os.execute(cmd)
73    end
74
75    expander = function(name)
76        -- We need to avoid issues with chdir to UNC paths and therefore expand
77        -- the path when we're current. (We could use one of the helpers instead)
78        if file.pathpart(name) == "" then
79            return file.collapsepath(file.join(lfs.currentdir(),name))
80        else
81            return name
82        end
83    end
84
85else
86
87    opencalls = {
88        ['default']   = [[pdfopen "%filename%"]],
89        ['okular']    = [[okular --unique "%filename%"]],
90        ['sumatra']   = [[wine "sumatrapdf.exe" -reuse-instance -bg-color 0xCCCCCC "%filename%"]],
91        ['pdfxcview'] = [[wine "pdfxcview.exe" /A "nolock=yes=OpenParameters" "%filename%"]],
92        ['auto']      = [[open "%filename%"]], -- linux: xdg-open
93    }
94    closecalls= {
95        ['default']   = [[pdfclose --file "%filename%"]],
96        ['okular']    = false,
97        ['sumatra']   = false,
98        ['auto']      = false,
99    }
100    allcalls = {
101        ['default']   = [[pdfclose --all]],
102        ['okular']    = false,
103        ['sumatra']   = false,
104        ['auto']      = false,
105    }
106
107    pdfview.method = "okular"
108    pdfview.method = "sumatra" -- faster and more complete
109
110    runner = function(template,variables)
111        local cmd = replace(template,variables)
112        cmd = cmd .. " 1>/dev/null 2>/dev/null &"
113        report("command: %s",cmd)
114        os.execute(cmd)
115    end
116
117    expander = function(name)
118        return name
119    end
120
121end
122
123directives.register("pdfview.method", function(v)
124    pdfview.method = (opencalls[v] and v) or 'default'
125end)
126
127function pdfview.setmethod(method)
128    if method and opencalls[method] then
129        pdfview.method = method
130    end
131end
132
133function pdfview.methods()
134    return concat(table.sortedkeys(opencalls), " ")
135end
136
137function pdfview.status()
138    return format("pdfview methods: %s, current method: %s (directives_pdfview_method)",pdfview.methods(),tostring(pdfview.method))
139end
140
141local function fullname(name)
142    return file.addsuffix(name,"pdf")
143end
144
145function pdfview.open(...)
146    local opencall = opencalls[pdfview.method]
147    if opencall then
148        local t = { ... }
149        for i=1,#t do
150            local name = expander(fullname(t[i]))
151            if io.exists(name) then
152                runner(opencall,{ filename = name })
153            end
154        end
155    end
156end
157
158function pdfview.close(...)
159    local closecall = closecalls[pdfview.method]
160    if closecall then
161        local t = { ... }
162        for i=1,#t do
163            local name = expander(fullname(t[i]))
164            if io.exists(name) then
165                runner(closecall,{ filename = name })
166            end
167        end
168    end
169end
170
171function pdfview.closeall()
172    local allcall = allcalls[pdfview.method]
173    if allcall then
174        runner(allcall)
175    end
176end
177
178return pdfview
179