]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
change a lot of methods to begin with small char
[lyx.git] / src / insets / insetinclude.C
1
2 #include <config.h>
3
4 #include <cstdlib>
5
6 #ifdef __GNUG__
7 #pragma implementation
8 #endif
9
10 #include "frontends/Dialogs.h"
11
12 #include "insetinclude.h"
13 #include "buffer.h"
14 #include "bufferlist.h"
15 #include "BufferView.h"
16 #include "debug.h"
17 #include "support/filetools.h"
18 #include "lyxrc.h"
19 #include "LyXView.h"
20 #include "LaTeXFeatures.h"
21 #include "gettext.h"
22 #include "support/FileInfo.h"
23 #include "layout.h"
24
25 using std::ostream;
26 using std::endl;
27 using std::vector;
28 using std::pair;
29
30 extern BufferList bufferlist;
31
32 namespace {
33
34 string const unique_id()
35 {
36         static unsigned int seed = 1000;
37
38         std::ostringstream ost;
39         ost << "file" << ++seed;
40
41         // Needed if we use lyxstring.
42         return ost.str().c_str();
43 }
44
45 } // namespace anon
46
47
48 InsetInclude::InsetInclude(Params const & p)
49         : params_(p), include_label(unique_id())
50 {}
51
52
53 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
54 {
55         params_.cparams = p;
56         params_.masterFilename_ = b.fileName();
57         include_label = unique_id();
58 }
59
60
61 InsetInclude::~InsetInclude()
62 {
63         hideDialog();
64 }
65
66
67 InsetInclude::Params const & InsetInclude::params() const
68 {
69         return params_;
70 }
71
72
73 bool InsetInclude::Params::operator==(Params const & o) const
74 {
75         if (cparams == o.cparams && flag == o.flag &&
76             noload == o.noload && masterFilename_ == o.masterFilename_)
77                 return true;
78         
79         return false;
80 }
81
82
83 bool InsetInclude::Params::operator!=(Params const & o) const
84 {
85         return !(*this == o);
86 }
87
88
89 void InsetInclude::set(Params const & p)
90 {
91         params_ = p;
92
93         // Just to be safe...
94         string command;
95  
96         switch (params_.flag) {
97                 case INCLUDE:
98                         command="include";
99                         break;
100                 case VERB:
101                         command="verbatiminput";
102                         break;
103                 case INPUT:
104                         command="input";
105                         break;
106                 case VERBAST:
107                         command="verbatiminput*";
108                         break;
109         }
110  
111         params_.cparams.setCmdName(command);
112 }
113
114
115 Inset * InsetInclude::clone(Buffer const & buffer) const
116 {
117         Params p(params_);
118         p.masterFilename_ = buffer.fileName();
119
120         return new InsetInclude(p);
121 }
122
123
124 void InsetInclude::edit(BufferView * bv, int, int, unsigned int)
125 {
126         bv->owner()->getDialogs()->showInclude(this);
127 }
128
129
130 void InsetInclude::write(Buffer const *, ostream & os) const
131 {
132         os << "Include " << params_.cparams.getCommand() << "\n";
133 }
134
135
136 void InsetInclude::read(Buffer const *, LyXLex & lex)
137 {
138         params_.cparams.read(lex);
139    
140         if (params_.cparams.getCmdName() == "include")
141                 params_.flag = INCLUDE;
142         else if (params_.cparams.getCmdName() == "input")
143                 params_.flag = INPUT;
144         /* FIXME: is this logic necessary now ? */
145         else if (contains(params_.cparams.getCmdName(), "verbatim")) {
146                 params_.flag = VERB;
147                 if (params_.cparams.getCmdName() == "verbatiminput*")
148                         params_.flag = VERBAST;
149         }
150 }
151
152
153 bool InsetInclude::display() const
154 {
155         return !(params_.flag == INPUT);
156 }
157
158
159 string const InsetInclude::getScreenLabel() const
160 {
161         string temp;
162
163         switch (params_.flag) {
164                 case INPUT: temp += _("Input"); break;
165                 case VERB: temp += _("Verbatim Input"); break;
166                 case VERBAST: temp += _("Verbatim Input*"); break;
167                 case INCLUDE: temp += _("Include"); break;
168         }
169
170         temp += ": ";
171         
172         if (params_.cparams.getContents().empty())
173                 temp += "???";
174         else
175                 temp += params_.cparams.getContents();
176
177         return temp;
178 }
179
180
181 string const InsetInclude::getRelFileBaseName() const
182 {
183         return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
184 }
185
186  
187 string const InsetInclude::getFileName() const
188 {
189         return MakeAbsPath(params_.cparams.getContents(),
190                            OnlyPath(getMasterFilename()));
191 }
192
193
194 string const InsetInclude::getMasterFilename() const
195 {
196         return params_.masterFilename_;
197 }
198
199
200 bool InsetInclude::loadIfNeeded() const
201 {
202         if (params_.noload || isVerbatim())
203                 return false;
204
205         if (!IsLyXFilename(getFileName()))
206                 return false;
207         
208         if (bufferlist.exists(getFileName()))
209                 return true;
210         
211         // the readonly flag can/will be wrong, not anymore I think.
212         FileInfo finfo(getFileName());
213         bool const ro = !finfo.writable();
214         return bufferlist.readFile(getFileName(), ro) != 0;
215 }
216
217
218 int InsetInclude::latex(Buffer const * buffer, ostream & os,
219                         bool /*fragile*/, bool /*fs*/) const
220 {
221         string incfile(params_.cparams.getContents());
222         
223         // Do nothing if no file name has been specified
224         if (incfile.empty())
225                 return 0;
226    
227         if (loadIfNeeded()) {
228                 Buffer * tmp = bufferlist.getBuffer(getFileName());
229
230                 // FIXME: this should be a GUI warning
231                 if (tmp->params.textclass != buffer->params.textclass) {
232                         lyxerr << "WARNING: Included file `"
233                                << MakeDisplayPath(getFileName())
234                                << "' has textclass `"
235                                << textclasslist.NameOfClass(tmp->params.textclass)
236                                << "' while parent file has textclass `"
237                                << textclasslist.NameOfClass(buffer->params.textclass)
238                                << "'." << endl;
239                         //return 0;
240                 }
241                 
242                 // write it to a file (so far the complete file)
243                 string writefile = ChangeExtension(getFileName(), ".tex");
244
245                 if (!buffer->tmppath.empty()
246                     && !buffer->niceFile) {
247                         incfile = subst(incfile, '/','@');
248 #ifdef __EMX__
249                         incfile = subst(incfile, ':', '$');
250 #endif
251                         writefile = AddName(buffer->tmppath, incfile);
252                 } else
253                         writefile = getFileName();
254                 writefile = ChangeExtension(writefile, ".tex");
255                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
256                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
257                 
258                 tmp->markDepClean(buffer->tmppath);
259                 
260                 tmp->makeLaTeXFile(writefile,
261                                    OnlyPath(getMasterFilename()),
262                                    buffer->niceFile, true);
263         }
264
265         if (isVerbatim()) {
266                 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
267         } else if (params_.flag == INPUT) {
268                 // \input wants file with extension (default is .tex)
269                 if (!IsLyXFilename(getFileName())) {
270                         os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
271                 } else {
272                         os << '\\' << params_.cparams.getCmdName() << '{'
273                            << ChangeExtension(incfile, ".tex")
274                            <<  '}';
275                 }
276         } else {
277                 // \include don't want extension and demands that the
278                 // file really have .tex
279                 os << '\\' << params_.cparams.getCmdName() << '{'
280                    << ChangeExtension(incfile, string())
281                    << '}';
282         }
283
284         return 0;
285 }
286
287
288 int InsetInclude::ascii(Buffer const *, std::ostream & os, int) const
289 {
290         if (isVerbatim())
291                 os << GetFileContents(getFileName());
292         return 0;
293 }
294
295
296 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
297 {
298         string incfile(params_.cparams.getContents());
299         
300         // Do nothing if no file name has been specified
301         if (incfile.empty())
302                 return 0;
303    
304         if (loadIfNeeded()) {
305                 Buffer * tmp = bufferlist.getBuffer(getFileName());
306
307                 // write it to a file (so far the complete file)
308                 string writefile = ChangeExtension(getFileName(), ".sgml");
309                 if (!buffer->tmppath.empty() && !buffer->niceFile) {
310                         incfile = subst(incfile, '/','@');
311                         writefile = AddName(buffer->tmppath, incfile);
312                 } else
313                         writefile = getFileName();
314
315                 if (IsLyXFilename(getFileName()))
316                         writefile = ChangeExtension(writefile, ".sgml");
317
318                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
319                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
320                 
321                 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
322         }
323
324         if (isVerbatim()) {
325                 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
326                    << "\" format=\"linespecific\">"
327                    << "</inlinegraphic>";
328         } else
329                 os << '&' << include_label << ';';
330         
331         return 0;
332 }
333
334
335 int InsetInclude::docBook(Buffer const * buffer, ostream & os) const
336 {
337         string incfile(params_.cparams.getContents());
338
339         // Do nothing if no file name has been specified
340         if (incfile.empty())
341                 return 0;
342    
343         if (loadIfNeeded()) {
344                 Buffer * tmp = bufferlist.getBuffer(getFileName());
345
346                 // write it to a file (so far the complete file)
347                 string writefile = ChangeExtension(getFileName(), ".sgml");
348                 if (!buffer->tmppath.empty() && !buffer->niceFile) {
349                         incfile = subst(incfile, '/','@');
350                         writefile = AddName(buffer->tmppath, incfile);
351                 } else
352                         writefile = getFileName();
353                 if (IsLyXFilename(getFileName()))
354                         writefile = ChangeExtension(writefile, ".sgml");
355
356                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
357                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
358                 
359                 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
360         }
361
362         if (isVerbatim()) {
363                 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
364                    << "\" format=\"linespecific\">"
365                    << "</inlinegraphic>";
366         } else
367                 os << '&' << include_label << ';';
368         
369         return 0;
370 }
371
372
373 void InsetInclude::validate(LaTeXFeatures & features) const
374 {
375
376         string incfile(params_.cparams.getContents());
377         string writefile;
378
379         Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
380
381         if (b && !b->tmppath.empty() && b->niceFile) {
382                 incfile = subst(incfile, '/','@');
383                 writefile = AddName(b->tmppath, incfile);
384         } else
385                 writefile = getFileName();
386
387         if (IsLyXFilename(getFileName()))
388                 writefile = ChangeExtension(writefile, ".sgml");
389
390         features.IncludedFiles[include_label] = writefile;
391
392         if (isVerbatim())
393                 features.verbatim = true;
394
395         // Here we must do the fun stuff...
396         // Load the file in the include if it needs
397         // to be loaded:
398         if (loadIfNeeded()) {
399                 // a file got loaded
400                 Buffer const * const tmp = bufferlist.getBuffer(getFileName());
401                 if (tmp)
402                         tmp->validate(features);
403         }
404 }
405
406
407 vector<string> const InsetInclude::getLabelList() const
408 {
409         vector<string> l;
410
411         if (loadIfNeeded()) {
412                 Buffer * tmp = bufferlist.getBuffer(getFileName());
413                 tmp->setParentName("");
414                 l = tmp->getLabelList();
415                 tmp->setParentName(getMasterFilename());
416         }
417
418         return l;
419 }
420
421
422 vector<pair<string,string> > const InsetInclude::getKeys() const
423 {
424         vector<pair<string,string> > keys;
425         
426         if (loadIfNeeded()) {
427                 Buffer * tmp = bufferlist.getBuffer(getFileName());
428                 tmp->setParentName("");
429                 keys = tmp->getBibkeyList();
430                 tmp->setParentName(getMasterFilename());
431         }
432         
433         return keys;
434 }