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