]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
47a7afdce1e86548e6a82a33bb387aaac9bff84b
[lyx.git] / src / insets / insetexternal.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *      
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <cstdio>
18 #include <utility>
19
20 #include "insetexternal.h"
21 #include "ExternalTemplate.h"
22 #include "BufferView.h"
23 #include "buffer.h"
24 #include "LyXView.h"
25 #include "frontends/Dialogs.h"
26 #include "lyx_main.h"
27 #include "LaTeXFeatures.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30 #include "support/path.h"
31 #include "support/syscall.h"
32 #include "gettext.h"
33
34 using std::endl;
35
36 InsetExternal::InsetExternal() 
37         : view_(0)
38 {
39         tempname_ = lyx::tempName(string(), "lyxext");
40         //ExternalTemplateManager::Templates::const_iterator i1;
41         params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
42 }
43
44
45 InsetExternal::~InsetExternal()
46 {
47         lyx::unlink(tempname_);
48         hideDialog();
49 }
50
51
52 InsetExternal::Params InsetExternal::params() const
53 {
54         return params_;
55 }
56  
57  
58 void InsetExternal::setFromParams(Params const & p)
59 {
60         params_.filename = p.filename;
61         params_.parameters = p.parameters;
62         params_.templ = p.templ;
63 }      
64
65
66 string const InsetExternal::editMessage() const
67 {
68         return doSubstitution(0, params_.templ.guiName);
69 }
70
71
72 void InsetExternal::edit(BufferView * bv,
73                          int /*x*/, int /*y*/, unsigned int /*button*/)
74 {
75         view_ = bv;
76         view_->owner()->getDialogs()->showExternal(this);
77 }
78
79
80 void InsetExternal::edit(BufferView * bv, bool)
81 {
82         edit(bv, 0, 0, 0);
83 }
84
85
86 void InsetExternal::write(Buffer const *, std::ostream & os) const
87 {
88         os << "External " << params_.templ.lyxName << ",\"" << params_.filename 
89            << "\",\"" << params_.parameters << "\"\n";
90 }
91
92
93 void InsetExternal::read(Buffer const *, LyXLex & lex)
94 {
95         string format;
96         string token;
97
98         // Read inset data from lex and store in format
99         if (lex.EatLine()) {
100                 format = lex.GetString();
101         } else
102                 lex.printError("InsetExternal: Parse error: `$$Token'");
103         while (lex.IsOK()) {
104                 lex.nextToken();
105                 token = lex.GetString();
106                 if (token == "\\end_inset")
107                         break;
108         }
109         if (token != "\\end_inset") {
110                 lex.printError("Missing \\end_inset at this point. "
111                                "Read: `$$Token'");
112         }
113
114         // Parse string format...
115         string::size_type const pos1 = format.find(",");
116         params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
117         string::size_type const pos2 = format.find("\",\"", pos1);
118         params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
119         params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
120
121         lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
122                             << " " << params_.filename
123                             << " " << params_.parameters << endl;
124 }
125
126
127 int InsetExternal::write(string const & format,
128                          Buffer const * buf, std::ostream & os) const
129 {
130         ExternalTemplate const & et = params_.templ;
131         ExternalTemplate::Formats::const_iterator cit =
132                 et.formats.find(format);
133         if (cit == et.formats.end()) {
134                 lyxerr << "External template format '" << format
135                        << "' not specified in template " << params_.templ.lyxName
136                        << endl;
137                 return 0;
138         }
139         
140         if (et.automaticProduction) {
141                 executeCommand(doSubstitution(buf,
142                                               cit->second.updateCommand),
143                                buf);
144         }
145         
146         os << doSubstitution(buf, cit->second.product);
147         return 0; // CHECK  (FIXME check what ? - jbl)
148 }
149
150
151 int InsetExternal::latex(Buffer const * buf,
152                          std::ostream & os, bool, bool) const
153 {
154         return write("LaTeX", buf, os);
155 }
156
157
158 int InsetExternal::ascii(Buffer const * buf, std::ostream & os, int) const
159 {
160         return write("Ascii", buf, os);
161 }
162
163
164 int InsetExternal::linuxdoc(Buffer const * buf, std::ostream & os) const
165 {
166         return write("LinuxDoc", buf, os);
167 }
168
169
170 int InsetExternal::docBook(Buffer const * buf, std::ostream & os) const
171 {
172         return write("DocBook", buf, os);
173 }
174
175
176 void InsetExternal::validate(LaTeXFeatures & features) const
177 {
178         ExternalTemplate const & et = params_.templ;
179         ExternalTemplate::Formats::const_iterator cit =
180                 et.formats.find("LaTeX");
181
182         if (cit == et.formats.end())
183                 return;
184         
185         if (!cit->second.requirement.empty()) {
186                 features.require(cit->second.requirement);
187         }
188         if (!cit->second.preamble.empty()) {
189                 features.externalPreambles += cit->second.preamble + "\n";
190         }
191 }
192
193
194 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
195 {
196         InsetExternal * inset = new InsetExternal();
197         inset->params_ = params_;
198         inset->view_ = view_;
199         if (same_id)
200                 inset->id_ = id_;
201         return inset;
202 }
203
204
205 string const InsetExternal::getScreenLabel(Buffer const *) const
206 {
207         ExternalTemplate const & et = params_.templ;
208         if (et.guiName.empty())
209                 return _("External");
210         else
211                 return doSubstitution(0, et.guiName);
212 }
213
214
215 void InsetExternal::executeCommand(string const & s,
216                                    Buffer const * buffer) const
217 {
218         string buf = MakeAbsPath(buffer->fileName());
219         string path = OnlyPath(buf);
220         Path p(path);
221         Systemcalls one;
222         if (lyxerr.debugging()) {
223                 lyxerr << "Executing '" << s << "' in '"
224                        << path << "'" << endl;
225         }
226         one.startscript(Systemcalls::Wait, s);
227 }
228
229
230 string const InsetExternal::doSubstitution(Buffer const * buffer,
231                                      string const & s) const
232 {
233         string result;
234         string const basename = ChangeExtension(params_.filename, string());
235         result = subst(s, "$$FName", params_.filename);
236         result = subst(result, "$$Basename", basename);
237         result = subst(result, "$$Parameters", params_.parameters);
238         result = ReplaceEnvironmentPath(result);
239         result = subst(result, "$$Tempname", tempname_);
240         result = subst(result, "$$Sysdir", system_lyxdir);
241         
242         // Handle the $$Contents(filename) syntax
243         if (contains(result, "$$Contents(\"")) {
244
245                 string::size_type const pos = result.find("$$Contents(\"");
246                 string::size_type const end = result.find("\")", pos);
247                 string const file = result.substr(pos + 12, end - (pos + 12));
248                 string contents;
249                 if (buffer) {
250                         // Make sure we are in the directory of the buffer
251                         string const buf = MakeAbsPath(buffer->fileName());
252                         string const path = OnlyPath(buf);
253                         Path p(path);
254                         contents = GetFileContents(file);
255                 } else {
256                         contents = GetFileContents(file);
257                 }
258                 result = subst(result,
259                                ("$$Contents(\"" + file + "\")").c_str(),
260                                contents);
261         }
262
263         return result;
264 }
265
266
267 void InsetExternal::updateExternal() const
268 {
269         ExternalTemplate const & et = params_.templ;
270         ExternalTemplate::Formats::const_iterator cit =
271                 et.formats.find("LaTeX");
272         if (cit == et.formats.end())
273                 return;
274         
275         executeCommand(doSubstitution(view_->buffer(),
276                                       cit->second.updateCommand),
277                        view_->buffer());
278 }
279
280
281 void InsetExternal::viewExternal() const
282 {
283         ExternalTemplate const & et = params_.templ;
284         if (et.automaticProduction)
285                 updateExternal();
286
287         executeCommand(doSubstitution(view_->buffer(),
288                                       et.viewCommand),
289                        view_->buffer());
290 }
291
292
293 void InsetExternal::editExternal() const
294 {
295         ExternalTemplate const & et = params_.templ;
296         if (et.automaticProduction)
297                 updateExternal();
298
299         executeCommand(doSubstitution(view_->buffer(),
300                                       et.editCommand),
301                        view_->buffer());
302 }
303
304
305 bool operator==(InsetExternal::Params const & left,
306                 InsetExternal::Params const & right)
307 {
308         return ((left.filename   == right.filename) &&
309                 (left.parameters == right.parameters) &&
310                 (left.templ.lyxName == right.templ.lyxName));
311 }
312
313
314 bool operator!=(InsetExternal::Params const & left,
315                 InsetExternal::Params const & right)
316 {
317         return  !(left == right);
318 }
319