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