]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
updates to minipage inset
[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 #ifdef SIGC_CXX_NAMESPACES
34 using SigC::slot;
35 #endif
36
37 using std::endl;
38
39 InsetExternal::InsetExternal() 
40         : view(0)
41 {
42         tempname = lyx::tempName(string(), "lyxext");
43         //ExternalTemplateManager::Templates::const_iterator i1;
44         params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
45 }
46
47
48 InsetExternal::~InsetExternal()
49 {
50         lyx::unlink(tempname);
51         hideDialog();
52 }
53
54
55 InsetExternal::InsetExternalParams InsetExternal::params() const
56 {
57         return params_;
58 }
59  
60  
61 void InsetExternal::setFromParams(InsetExternalParams const & p)
62 {
63         params_.filename = p.filename;
64         params_.parameters = p.parameters;
65         params_.templ = p.templ;
66 }      
67
68
69 string const InsetExternal::EditMessage() const
70 {
71         return doSubstitution(0, params_.templ.guiName);
72 }
73
74
75 void InsetExternal::Edit(BufferView * bv,
76                          int /*x*/, int /*y*/, unsigned int /*button*/)
77 {
78         view = bv;
79         view->owner()->getDialogs()->showExternal(this);
80 }
81
82
83 void InsetExternal::Write(Buffer const *, std::ostream & os) const
84 {
85         os << "External " << params_.templ.lyxName << ",\"" << params_.filename 
86            << "\",\"" << params_.parameters << "\"\n";
87 }
88
89
90 void InsetExternal::Read(Buffer const *, LyXLex & lex)
91 {
92         string format;
93         string token;
94
95         // Read inset data from lex and store in format
96         if (lex.EatLine()) {
97                 format = lex.GetString();
98         } else
99                 lex.printError("InsetExternal: Parse error: `$$Token'");
100         while (lex.IsOK()) {
101                 lex.nextToken();
102                 token = lex.GetString();
103                 if (token == "\\end_inset")
104                         break;
105         }
106         if (token != "\\end_inset") {
107                 lex.printError("Missing \\end_inset at this point. "
108                                "Read: `$$Token'");
109         }
110
111         // Parse string format...
112         string::size_type const pos1 = format.find(",");
113         params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
114         string::size_type const pos2 = format.find("\",\"", pos1);
115         params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
116         params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
117
118         lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
119                             << " " << params_.filename
120                             << " " << params_.parameters << endl;
121 }
122
123
124 int InsetExternal::write(string const & format,
125                          Buffer const * buf, std::ostream & os) const
126 {
127         ExternalTemplate const & et = params_.templ;
128         ExternalTemplate::Formats::const_iterator cit =
129                 et.formats.find(format);
130         if (cit == et.formats.end()) {
131                 lyxerr << "External template format '" << format
132                        << "' not specified in template " << params_.templ.lyxName
133                        << endl;
134                 return 0;
135         }
136         
137         if (et.automaticProduction) {
138                 executeCommand(doSubstitution(buf,
139                                               (*cit).second.updateCommand),
140                                buf);
141         }
142         
143         os << doSubstitution(buf, (*cit).second.product);
144         return 0; // CHECK  (FIXME check what ? - jbl)
145 }
146
147
148 int InsetExternal::Latex(Buffer const * buf,
149                          std::ostream & os, bool, bool) const
150 {
151         return write("LaTeX", buf, os);
152 }
153
154
155 int InsetExternal::Ascii(Buffer const * buf, std::ostream & os, int) const
156 {
157         return write("Ascii", buf, os);
158 }
159
160
161 int InsetExternal::Linuxdoc(Buffer const * buf, std::ostream & os) const
162 {
163         return write("LinuxDoc", buf, os);
164 }
165
166
167 int InsetExternal::DocBook(Buffer const * buf, std::ostream & os) const
168 {
169         return write("DocBook", buf, os);
170 }
171
172
173 void InsetExternal::Validate(LaTeXFeatures & features) const
174 {
175         ExternalTemplate const & et = params_.templ;
176         ExternalTemplate::Formats::const_iterator cit =
177                 et.formats.find("LaTeX");
178
179         if (cit == et.formats.end())
180                 return;
181         
182         if (!(*cit).second.requirement.empty()) {
183                 features.require((*cit).second.requirement);
184         }
185         if (!(*cit).second.preamble.empty()) {
186                 features.externalPreambles += (*cit).second.preamble + "\n";
187         }
188 }
189
190
191 Inset * InsetExternal::Clone(Buffer const &) const
192 {
193         InsetExternal * inset = new InsetExternal();
194         inset->params_ = params_;
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 }