]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
"Inter-word Space"
[lyx.git] / src / insets / insetexternal.C
1 /**
2  * \file insetexternal.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "insetexternal.h"
14
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "debug.h"
18 #include "ExternalTemplate.h"
19 #include "funcrequest.h"
20 #include "gettext.h"
21 #include "LaTeXFeatures.h"
22 #include "latexrunparams.h"
23 #include "lyx_main.h"
24 #include "lyxlex.h"
25 #include "Lsstream.h"
26
27 #include "frontends/LyXView.h"
28 #include "frontends/Dialogs.h"
29
30 #include "support/filetools.h"
31 #include "support/lstrings.h"
32 #include "support/lyxalgo.h"
33 #include "support/path.h"
34 #include "support/systemcall.h"
35 #include "support/FileInfo.h"
36
37 #include <cstdio>
38 #include <utility>
39
40 using std::ostream;
41 using std::endl;
42
43
44 InsetExternal::InsetExternal()
45         : view_(0)
46 {
47         tempname_ = lyx::tempName(string(), "lyxext");
48         //ExternalTemplateManager::Templates::const_iterator i1;
49         params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
50 }
51
52
53 InsetExternal::~InsetExternal()
54 {
55         lyx::unlink(tempname_);
56         InsetExternalMailer mailer(*this);
57         mailer.hideDialog();
58 }
59
60
61 InsetExternal::Params const & InsetExternal::params() const
62 {
63         return params_;
64 }
65
66
67 dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd)
68 {
69         switch (cmd.action) {
70
71         case LFUN_INSET_MODIFY: {
72                 InsetExternal::Params p;
73                 InsetExternalMailer::string2params(cmd.argument, p);
74                 if (!p.filename.empty()) {
75                         setFromParams(p);
76                         cmd.view()->updateInset(this);
77                 }
78                 return DISPATCHED;
79         }
80
81         case LFUN_INSET_DIALOG_UPDATE:
82                 InsetExternalMailer(*this).updateDialog(cmd.view());
83                 return DISPATCHED;
84
85         case LFUN_MOUSE_RELEASE:
86         case LFUN_INSET_EDIT:
87                 InsetExternalMailer(*this).showDialog(cmd.view());
88                 return DISPATCHED;
89
90         default:
91                 return UNDISPATCHED;
92         }
93 }
94
95
96 void InsetExternal::setFromParams(Params const & p)
97 {
98         params_.filename = p.filename;
99         params_.parameters = p.parameters;
100         params_.templ = p.templ;
101 }
102
103
104 string const InsetExternal::editMessage() const
105 {
106         return doSubstitution(0, params_.templ.guiName);
107 }
108
109
110 void InsetExternal::write(Buffer const *, ostream & os) const
111 {
112         os << "External " << params_.templ.lyxName << ",\""
113            << params_.filename << "\",\"" << params_.parameters << "\"\n";
114 }
115
116
117 void InsetExternal::read(Buffer const *, LyXLex & lex)
118 {
119         string format;
120         string token;
121
122         // Read inset data from lex and store in format
123         if (lex.eatLine()) {
124                 format = lex.getString();
125         } else {
126                 lex.printError("InsetExternal: Parse error: `$$Token'");
127         }
128
129         while (lex.isOK()) {
130                 lex.nextToken();
131                 token = lex.getString();
132                 if (token == "\\end_inset")
133                         break;
134         }
135         if (token != "\\end_inset") {
136                 lex.printError("Missing \\end_inset at this point. "
137                                "Read: `$$Token'");
138         }
139
140         // Parse string format...
141         string::size_type const pos1 = format.find(',');
142         params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
143         string::size_type const pos2 = format.find("\",\"", pos1);
144         params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
145         params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
146
147         lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
148                             << ' ' << params_.filename
149                             << ' ' << params_.parameters << endl;
150 }
151
152
153 int InsetExternal::write(string const & format,
154                          Buffer const * buf, ostream & os) const
155 {
156         ExternalTemplate const & et = params_.templ;
157         ExternalTemplate::Formats::const_iterator cit =
158                 et.formats.find(format);
159         if (cit == et.formats.end()) {
160                 lyxerr << "External template format '" << format
161                        << "' not specified in template "
162                        << params_.templ.lyxName << endl;
163                 return 0;
164         }
165
166         updateExternal(format, buf);
167         string const str = doSubstitution(buf, cit->second.product);
168         os << str;
169         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
170 }
171
172
173 int InsetExternal::latex(Buffer const * buf, ostream & os,
174                          LatexRunParams const & runparams) const
175 {
176         // If the template has specified a PDFLaTeX output, then we try and
177         // use that.
178         if (runparams.flavor == LatexRunParams::PDFLATEX) {
179                 ExternalTemplate const & et = params_.templ;
180                 ExternalTemplate::Formats::const_iterator cit =
181                         et.formats.find("PDFLaTeX");
182                 if (cit != et.formats.end())
183                         return write("PDFLaTeX", buf, os);
184         }
185
186         return write("LaTeX", buf, os);
187 }
188
189
190 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
191 {
192         return write("Ascii", buf, os);
193 }
194
195
196 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
197 {
198         return write("LinuxDoc", buf, os);
199 }
200
201
202 int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
203 {
204         return write("DocBook", buf, os);
205 }
206
207
208 void InsetExternal::validate(LaTeXFeatures & features) const
209 {
210         ExternalTemplate const & et = params_.templ;
211         ExternalTemplate::Formats::const_iterator cit =
212                 et.formats.find("LaTeX");
213
214         if (cit == et.formats.end())
215                 return;
216
217         if (!cit->second.requirement.empty()) {
218                 features.require(cit->second.requirement);
219         }
220         if (!cit->second.preamble.empty()) {
221                 features.addExternalPreamble(cit->second.preamble + "\n");
222         }
223 }
224
225
226 Inset * InsetExternal::clone(Buffer const &) const
227 {
228         InsetExternal * inset = new InsetExternal;
229         inset->params_ = params_;
230         inset->view_ = view_;
231         return inset;
232 }
233
234
235 string const InsetExternal::getScreenLabel(Buffer const *) const
236 {
237         ExternalTemplate const & et = params_.templ;
238         if (et.guiName.empty())
239                 return _("External");
240         else
241                 return doSubstitution(0, et.guiName);
242 }
243
244
245 void InsetExternal::executeCommand(string const & s,
246                                    Buffer const * buffer) const
247 {
248         Path p(buffer->filePath());
249         Systemcall one;
250         if (lyxerr.debugging()) {
251                 lyxerr << "Executing '" << s << "' in '"
252                        << buffer->filePath() << '\'' << endl;
253         }
254         one.startscript(Systemcall::Wait, s);
255 }
256
257
258 string const InsetExternal::doSubstitution(Buffer const * buffer,
259                                            string const & s) const
260 {
261         string result;
262         string const basename = ChangeExtension(params_.filename, string());
263         string filepath;
264         if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
265                 filepath = buffer->filePath();
266         }
267         result = subst(s, "$$FName", params_.filename);
268         result = subst(result, "$$Basename", basename);
269         result = subst(result, "$$Parameters", params_.parameters);
270         result = subst(result, "$$FPath", filepath);
271         result = subst(result, "$$Tempname", tempname_);
272         result = subst(result, "$$Sysdir", system_lyxdir);
273
274         // Handle the $$Contents(filename) syntax
275         if (contains(result, "$$Contents(\"")) {
276
277                 string::size_type const pos = result.find("$$Contents(\"");
278                 string::size_type const end = result.find("\")", pos);
279                 string const file = result.substr(pos + 12, end - (pos + 12));
280                 string contents;
281                 if (buffer) {
282                         // Make sure we are in the directory of the buffer
283                         Path p(buffer->filePath());
284                         contents = GetFileContents(file);
285                 } else {
286                         contents = GetFileContents(file);
287                 }
288                 result = subst(result,
289                                ("$$Contents(\"" + file + "\")").c_str(),
290                                contents);
291         }
292
293         return result;
294 }
295
296
297 void InsetExternal::updateExternal() const
298 {
299         updateExternal("LaTeX", view_->buffer());
300 }
301
302 void InsetExternal::updateExternal(string const & format,
303                                    Buffer const * buf) const
304 {
305         ExternalTemplate const & et = params_.templ;
306         ExternalTemplate::Formats::const_iterator cit =
307                 et.formats.find(format);
308
309         if (cit == et.formats.end() ||
310             cit->second.updateCommand.empty() ||
311             !et.automaticProduction)
312                 return;
313
314         if (!cit->second.updateResult.empty()) {
315                 string const resultfile = doSubstitution(buf,
316                                                          cit->second.updateResult);
317                 FileInfo fi(params_.filename);
318                 FileInfo fi2(resultfile);
319                 if (fi2.exist() && fi.exist() &&
320                     difftime(fi2.getModificationTime(),
321                              fi.getModificationTime()) >= 0) {
322                         lyxerr[Debug::FILES] << resultfile
323                                              << " is up to date" << endl;
324                         return;
325                 }
326         }
327
328         executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
329 }
330
331
332 void InsetExternal::viewExternal() const
333 {
334         ExternalTemplate const & et = params_.templ;
335         if (et.viewCommand.empty())
336                 return;
337
338         updateExternal();
339         executeCommand(doSubstitution(view_->buffer(),
340                                       et.viewCommand),
341                        view_->buffer());
342 }
343
344
345 void InsetExternal::editExternal() const
346 {
347         ExternalTemplate const & et = params_.templ;
348         if (et.editCommand.empty())
349                 return;
350
351         updateExternal();
352         executeCommand(doSubstitution(view_->buffer(),
353                                       et.editCommand),
354                        view_->buffer());
355 }
356
357
358 string const InsetExternalMailer::name_("external");
359
360 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
361         : inset_(inset)
362 {}
363
364
365 string const InsetExternalMailer::inset2string() const
366 {
367         return params2string(inset_.params());
368 }
369
370
371 void InsetExternalMailer::string2params(string const & in,
372                                         InsetExternal::Params & params)
373 {
374         params = InsetExternal::Params();
375
376         if (in.empty())
377                 return;
378
379         istringstream data(STRCONV(in));
380         LyXLex lex(0,0);
381         lex.setStream(data);
382
383         if (lex.isOK()) {
384                 lex.next();
385                 string const token = lex.getString();
386                 if (token != name_)
387                         return;
388         }
389
390         // This is part of the inset proper that is usually swallowed
391         // by Buffer::readInset
392         if (lex.isOK()) {
393                 lex.next();
394                 string const token = lex.getString();
395                 if (token != "External")
396                         return;
397         }
398
399         if (lex.isOK()) {
400                 InsetExternal inset;
401                 inset.read(0, lex);
402                 params = inset.params();
403         }
404 }
405
406
407 string const
408 InsetExternalMailer::params2string(InsetExternal::Params const & params)
409 {
410         InsetExternal inset;
411         inset.setFromParams(params);
412         ostringstream data;
413         data << name_ << ' ';
414         inset.write(0, data);
415         data << "\\end_inset\n";
416         return STRCONV(data.str());
417 }