]> git.lyx.org Git - features.git/blob - src/insets/insetexternal.C
get rid of same_id from function signatures
[features.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 // Inset * InsetExternal::clone(Buffer const &, bool same_id) const
236 // {
237 //      InsetExternal * inset = new InsetExternal;
238 //      inset->params_ = params_;
239 //      inset->view_ = view_;
240 //      if (same_id)
241 //              inset->id_ = id_;
242 //      return inset;
243 // }
244
245
246 string const InsetExternal::getScreenLabel(Buffer const *) const
247 {
248         ExternalTemplate const & et = params_.templ;
249         if (et.guiName.empty())
250                 return _("External");
251         else
252                 return doSubstitution(0, et.guiName);
253 }
254
255
256 void InsetExternal::executeCommand(string const & s,
257                                    Buffer const * buffer) const
258 {
259         Path p(buffer->filePath());
260         Systemcall one;
261         if (lyxerr.debugging()) {
262                 lyxerr << "Executing '" << s << "' in '"
263                        << buffer->filePath() << '\'' << endl;
264         }
265         one.startscript(Systemcall::Wait, s);
266 }
267
268
269 string const InsetExternal::doSubstitution(Buffer const * buffer,
270                                            string const & s) const
271 {
272         string result;
273         string const basename = ChangeExtension(params_.filename, string());
274         string filepath;
275         if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
276                 filepath = buffer->filePath();
277         }
278         result = subst(s, "$$FName", params_.filename);
279         result = subst(result, "$$Basename", basename);
280         result = subst(result, "$$Parameters", params_.parameters);
281         result = subst(result, "$$FPath", filepath);
282         result = subst(result, "$$Tempname", tempname_);
283         result = subst(result, "$$Sysdir", system_lyxdir);
284
285         // Handle the $$Contents(filename) syntax
286         if (contains(result, "$$Contents(\"")) {
287
288                 string::size_type const pos = result.find("$$Contents(\"");
289                 string::size_type const end = result.find("\")", pos);
290                 string const file = result.substr(pos + 12, end - (pos + 12));
291                 string contents;
292                 if (buffer) {
293                         // Make sure we are in the directory of the buffer
294                         Path p(buffer->filePath());
295                         contents = GetFileContents(file);
296                 } else {
297                         contents = GetFileContents(file);
298                 }
299                 result = subst(result,
300                                ("$$Contents(\"" + file + "\")").c_str(),
301                                contents);
302         }
303
304         return result;
305 }
306
307
308 void InsetExternal::updateExternal() const
309 {
310         updateExternal("LaTeX", view_->buffer());
311 }
312
313 void InsetExternal::updateExternal(string const & format,
314                                    Buffer const * buf) const
315 {
316         ExternalTemplate const & et = params_.templ;
317         ExternalTemplate::Formats::const_iterator cit =
318                 et.formats.find(format);
319
320         if (cit == et.formats.end() ||
321             cit->second.updateCommand.empty() ||
322             !et.automaticProduction)
323                 return;
324
325         if (!cit->second.updateResult.empty()) {
326                 string const resultfile = doSubstitution(buf,
327                                                          cit->second.updateResult);
328                 FileInfo fi(params_.filename);
329                 FileInfo fi2(resultfile);
330                 if (fi2.exist() && fi.exist() &&
331                     difftime(fi2.getModificationTime(),
332                              fi.getModificationTime()) >= 0) {
333                         lyxerr[Debug::FILES] << resultfile
334                                              << " is up to date" << endl;
335                         return;
336                 }
337         }
338
339         executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
340 }
341
342
343 void InsetExternal::viewExternal() const
344 {
345         ExternalTemplate const & et = params_.templ;
346         if (et.viewCommand.empty())
347                 return;
348
349         updateExternal();
350         executeCommand(doSubstitution(view_->buffer(),
351                                       et.viewCommand),
352                        view_->buffer());
353 }
354
355
356 void InsetExternal::editExternal() const
357 {
358         ExternalTemplate const & et = params_.templ;
359         if (et.editCommand.empty())
360                 return;
361
362         updateExternal();
363         executeCommand(doSubstitution(view_->buffer(),
364                                       et.editCommand),
365                        view_->buffer());
366 }
367
368
369 string const InsetExternalMailer::name_("external");
370
371 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
372         : inset_(inset)
373 {}
374
375
376 string const InsetExternalMailer::inset2string() const
377 {
378         return params2string(inset_.params());
379 }
380
381
382 void InsetExternalMailer::string2params(string const & in,
383                                         InsetExternal::Params & params)
384 {
385         params = InsetExternal::Params();
386
387         if (in.empty())
388                 return;
389
390         istringstream data(STRCONV(in));
391         LyXLex lex(0,0);
392         lex.setStream(data);
393
394         if (lex.isOK()) {
395                 lex.next();
396                 string const token = lex.getString();
397                 if (token != name_)
398                         return;
399         }
400
401         // This is part of the inset proper that is usually swallowed
402         // by Buffer::readInset
403         if (lex.isOK()) {
404                 lex.next();
405                 string const token = lex.getString();
406                 if (token != "External")
407                         return;
408         }
409
410         if (lex.isOK()) {
411                 InsetExternal inset;
412                 inset.read(0, lex);
413                 params = inset.params();
414         }
415 }
416
417
418 string const
419 InsetExternalMailer::params2string(InsetExternal::Params const & params)
420 {
421         InsetExternal inset;
422         inset.setFromParams(params);
423         ostringstream data;
424         data << name_ << ' ';
425         inset.write(0, data);
426         data << "\\end_inset\n";
427         return STRCONV(data.str());
428 }