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