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
10
11
12
13
14
15
16
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
26local closecalls
27local allcalls
28local runner
29local expander
30
31if os . type = = " windows " then
32
33
34
35
36
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 ,
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 "
65
66 pdfview . method = " sumatra "
67
68 runner = function ( template , variables )
69 local cmd = replace ( template , variables )
70
71 report ( " command: %s " , cmd )
72 os . execute ( cmd )
73 end
74
75 expander = function ( name )
76
77
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%" ]] ,
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 "
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 |