summaryrefslogtreecommitdiff
path: root/tex/context/base/l-pdfview.lua
blob: 80033900f8c868440c7496eb16b4ea98c0d5d52c (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
if not modules then modules = { } end modules ['l-pdfview'] = {
    version   = 1.001,
    comment   = "companion to mtx-context.lua",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- Todo: figure out pdfopen/pdfclose on linux. Calling e.g. okular directly
-- doesn't work in linux when issued from scite as it blocks the editor (no
-- & possible or so). Unfortunately pdfopen keeps changing with not keeping
-- downward compatibility (command line arguments and so).

-- no 2>&1 any more, needs checking on windows

local format, concat = string.format, table.concat

pdfview = pdfview or { }

local opencalls, closecalls, allcalls, runner

if os.type == "windows" then

    opencalls = {
        ['default']     = "pdfopen --rxi --file",
        ['acrobat']     = "pdfopen --rxi --file",
        ['fullacrobat'] = "pdfopen --axi --file",
        ['okular']      = 'start "test" "c:/data/system/kde/bin/okular.exe" --unique', -- todo!
        ['sumatra']     = 'start "test" "c:/data/system/sumatrapdf/sumatrapdf.exe" -reuse-instance',
        ['okular']      = 'start "test" "okular.exe" --unique',
        ['sumatra']     = 'start "test" "sumatrapdf.exe" -reuse-instance -bg-color 0xCCCCCC',
    }
    closecalls= {
        ['default'] = "pdfclose --file",
        ['acrobat'] = "pdfclose --file",
        ['okular']  = false,
        ['sumatra'] = false,
    }
    allcalls = {
        ['default'] = "pdfclose --all",
        ['acrobat'] = "pdfclose --all",
        ['okular']  = false,
        ['sumatra'] = false,
    }

    pdfview.method = "acrobat"

    runner = function(...)
--         os.spawn(...)
        os.execute(...)
    end

else

    opencalls = {
        ['default'] = "pdfopen", -- we could pass the default here
        ['okular']  = 'okular --unique'
    }
    closecalls= {
        ['default'] = "pdfclose --file",
        ['okular']  = false,
    }
    allcalls = {
        ['default'] = "pdfclose --all",
        ['okular']  = false,
    }

    pdfview.method = "okular"

    runner = function(...)
        os.spawn(...)
    end

end

directives.register("pdfview.method", function(v)
    pdfview.method = (opencalls[v] and v) or 'default'
end)

function pdfview.setmethod(method)
    if method and opencalls[method] then
        pdfview.method = method
    end
end

function pdfview.methods()
    return concat(table.sortedkeys(opencalls), " ")
end

function pdfview.status()
    return format("pdfview methods: %s, current method: %s (directives_pdfview_method)",pdfview.methods(),tostring(pdfview.method))
end

-- local openedfiles = { }

local function fullname(name)
    return file.addsuffix(name,"pdf")
end

function pdfview.open(...)
    local opencall = opencalls[pdfview.method]
    if opencall then
        local t = { ... }
        for i=1,#t do
            local name = fullname(t[i])
            if io.exists(name) then
                runner(format('%s "%s"', opencall, name))
             -- openedfiles[name] = true
            end
        end
    end
end

function pdfview.close(...)
    local closecall = closecalls[pdfview.method]
    if closecall then
        local t = { ... }
        for i=1,#t do
            local name = fullname(t[i])
         -- if openedfiles[name] then
                runner(format('%s "%s"', closecall, name))
         --     openedfiles[name] = nil
         -- else
         --     pdfview.closeall()
         --     break
         -- end
        end
    end
end

function pdfview.closeall()
    local allcall = allcalls[pdfview.method]
    if allcall then
        runner(format('%s', allcall))
    end
 -- openedfiles = { }
end

--~ pdfview.open("t:/document/show-exa.pdf")
--~ os.sleep(3)
--~ pdfview.close("t:/document/show-exa.pdf")

return pdfview