Skip to content

Commit e831f37

Browse files
authored
Merge pull request #610 from wpferguson/fix_selection_to_pdf_open
official/selection_to_pdf
2 parents db505f1 + d696e6e commit e831f37

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

official/selection_to_pdf.lua

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ local function _(msg)
4444
return gettext(msg)
4545
end
4646

47+
local NIX_OPEN <const> = "xdg-open "
48+
local MAC_OPEN <const> = "open "
49+
local WIN_OPEN <const> = "start "
50+
4751
-- return data structure for script_manager
4852

4953
local script_data = {}
@@ -63,8 +67,8 @@ script_data.show = nil -- only required for libs since the destroy_method only h
6367
dt.preferences.register
6468
("selection_to_pdf","Open with","string",
6569
_("a pdf viewer"),
66-
_("can be an absolute pathname or the tool may be in the PATH"),
67-
"xdg-open")
70+
_("can be an absolute pathname or the tool may be in the PATH\nif not specified the system default will be used to open the pdf"),
71+
"")
6872

6973
local title_widget = dt.new_widget("entry") {
7074
placeholder = _("title")
@@ -115,8 +119,9 @@ local function thumbnail(latexfile,i,image,file)
115119
-- fact is that latex will get confused if the filename has multiple dots.
116120
-- so \includegraphics{file.01.jpg} wont work. We need to output the filename
117121
-- and extention separated, e.g: \includegraphics{{file.01}.jpg}
118-
local filenoext=string.gsub(file, "(.*)(%..*)", "%1")
119-
local ext=string.gsub(file, "(.*)(%..*)", "%2")
122+
123+
local filenoext = string.gsub(file, "(.*)(%..*)", "%1")
124+
local ext = string.gsub(file, "(.*)(%..*)", "%2")
120125
my_write(latexfile,"\\begin{minipage}[b]{"..width.."\\textwidth}\n")
121126
my_write(latexfile,"\\includegraphics[width=\\textwidth]{{"..filenoext.."}"..ext.."}\\newline\n")
122127
my_write(latexfile,"\\centering{"..i..": \\verb|"..title.."|}\n")
@@ -138,7 +143,7 @@ dt.register_storage("export_pdf", _("export thumbnails to pdf"),
138143
end
139144
local thumbs_per_line = no_of_thumbs_widget.value
140145
thumbs_per_line = tonumber(thumbs_per_line)
141-
width = (1/thumbs_per_line) - 0.01
146+
width = (1 / thumbs_per_line) - 0.01
142147

143148
local preamble = [[
144149
\documentclass[a4paper,10pt]{article}
@@ -148,31 +153,31 @@ dt.register_storage("export_pdf", _("export thumbnails to pdf"),
148153
\usepackage{geometry}
149154
\geometry{a4paper,left=5mm,right=5mm, top=5mm, bottom=5mm}
150155
\begin{document}
151-
{\Large\bfseries ]]..my_title..[[} \\
156+
{\Large\bfseries ]] .. my_title .. [[} \\
152157
\bigskip\bigskip
153158
]]
154159

155160
local errmsg
156161
local latexfile
157-
latexfile,errmsg=io.open(filename,"w")
162+
latexfile,errmsg = io.open(filename, "w")
158163
if not latexfile then
159164
error(errmsg)
160165
end
161-
my_write(latexfile,preamble)
166+
my_write(latexfile, preamble)
162167
local i = 1
163-
for img,file in pairs(image_table) do
164-
thumbnail(latexfile,i,img,file)
168+
for img, file in pairs(image_table) do
169+
thumbnail(latexfile, i, img, file)
165170
if i % thumbs_per_line == 0 then
166-
my_write(latexfile,"\n\\bigskip\n")
171+
my_write(latexfile, "\n\\bigskip\n")
167172
end
168-
i = i+1
173+
i = i + 1
169174
end
170175
my_write(latexfile,ending)
171176
latexfile:close()
172177

173178
-- convert to PDF
174-
local dir=string.gsub(filename, "(.*/)(.*)", "%1")
175-
local locfile=string.gsub(filename, "(.*/)(.*)", "%2")
179+
local dir = string.gsub(filename, "(.*/)(.*)", "%1")
180+
local locfile = string.gsub(filename, "(.*/)(.*)", "%2")
176181
local command = "pdflatex -halt-on-error -output-directory "..dir.." "..locfile
177182
local result = dt.control.execute(command)
178183
if result ~= 0 then
@@ -181,9 +186,18 @@ dt.register_storage("export_pdf", _("export thumbnails to pdf"),
181186
end
182187

183188
-- open the PDF
184-
local pdffile=string.gsub(filename, ".tex", ".pdf")
189+
local pdffile = string.gsub(filename, ".tex", ".pdf")
185190
command = dt.preferences.read("selection_to_pdf","Open with","string")
186-
command = command.." "..pdffile
191+
if command == "" then
192+
if dt.configuration.running_os == "windows" then
193+
command = WIN_OPEN
194+
elseif dt.configuration.running_os == "macos" then
195+
command = MAC_OPEN
196+
else
197+
command = NIX_OPEN
198+
end
199+
end
200+
command = command .. " " .. pdffile
187201
local result = dt.control.execute(command)
188202
if result ~= 0 then
189203
dt.print(_("problem running pdf viewer")) -- this one is probably usefull to the user

0 commit comments

Comments
 (0)