]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
Fix crash with included files
[lyx.git] / src / insets / insetinclude.C
1 /**
2  * \file insetinclude.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetinclude.h"
14
15 #include "buffer.h"
16 #include "buffer_funcs.h"
17 #include "bufferlist.h"
18 #include "bufferparams.h"
19 #include "BufferView.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "dispatchresult.h"
23 #include "exporter.h"
24 #include "funcrequest.h"
25 #include "FuncStatus.h"
26 #include "gettext.h"
27 #include "LaTeXFeatures.h"
28 #include "lyx_main.h"
29 #include "lyxrc.h"
30 #include "lyxlex.h"
31 #include "metricsinfo.h"
32 #include "outputparams.h"
33 #include "TocBackend.h"
34
35 #include "frontends/Alert.h"
36 #include "frontends/Painter.h"
37
38 #include "graphics/PreviewImage.h"
39 #include "graphics/PreviewLoader.h"
40
41 #include "insets/render_preview.h"
42
43 #include "support/filetools.h"
44 #include "support/lstrings.h" // contains
45 #include "support/lyxalgo.h"
46 #include "support/lyxlib.h"
47 #include "support/convert.h"
48
49 #include <boost/bind.hpp>
50 #include <boost/filesystem/operations.hpp>
51
52
53 namespace lyx {
54
55 using support::addName;
56 using support::absolutePath;
57 using support::bformat;
58 using support::changeExtension;
59 using support::contains;
60 using support::copy;
61 using support::DocFileName;
62 using support::FileName;
63 using support::getFileContents;
64 using support::isFileReadable;
65 using support::isLyXFilename;
66 using support::latex_path;
67 using support::makeAbsPath;
68 using support::makeDisplayPath;
69 using support::makeRelPath;
70 using support::onlyFilename;
71 using support::onlyPath;
72 using support::subst;
73 using support::sum;
74
75 using std::endl;
76 using std::string;
77 using std::auto_ptr;
78 using std::istringstream;
79 using std::ostream;
80 using std::ostringstream;
81
82 namespace Alert = frontend::Alert;
83 namespace fs = boost::filesystem;
84
85
86 namespace {
87
88 docstring const uniqueID()
89 {
90         static unsigned int seed = 1000;
91         return "file" + convert<docstring>(++seed);
92 }
93
94 } // namespace anon
95
96
97 InsetInclude::InsetInclude(InsetCommandParams const & p)
98         : params_(p), include_label(uniqueID()),
99           preview_(new RenderMonitoredPreview(this)),
100           set_label_(false)
101 {
102         preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
103 }
104
105
106 InsetInclude::InsetInclude(InsetInclude const & other)
107         : InsetOld(other),
108           params_(other.params_),
109           include_label(other.include_label),
110           preview_(new RenderMonitoredPreview(this)),
111           set_label_(false)
112 {
113         preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
114 }
115
116
117 InsetInclude::~InsetInclude()
118 {
119         InsetIncludeMailer(*this).hideDialog();
120 }
121
122
123 void InsetInclude::doDispatch(LCursor & cur, FuncRequest & cmd)
124 {
125         switch (cmd.action) {
126
127         case LFUN_INSET_MODIFY: {
128                 InsetCommandParams p("include");
129                 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), p);
130                 if (!p.getCmdName().empty()) {
131                         set(p, cur.buffer());
132                         cur.buffer().updateBibfilesCache();
133                 } else
134                         cur.noUpdate();
135                 break;
136         }
137
138         case LFUN_INSET_DIALOG_UPDATE:
139                 InsetIncludeMailer(*this).updateDialog(&cur.bv());
140                 break;
141
142         case LFUN_MOUSE_RELEASE:
143                 InsetIncludeMailer(*this).showDialog(&cur.bv());
144                 break;
145
146         default:
147                 InsetBase::doDispatch(cur, cmd);
148                 break;
149         }
150 }
151
152
153 bool InsetInclude::getStatus(LCursor & cur, FuncRequest const & cmd,
154                 FuncStatus & flag) const
155 {
156         switch (cmd.action) {
157
158         case LFUN_INSET_MODIFY:
159         case LFUN_INSET_DIALOG_UPDATE:
160                 flag.enabled(true);
161                 return true;
162
163         default:
164                 return InsetBase::getStatus(cur, cmd, flag);
165         }
166 }
167
168
169 InsetCommandParams const & InsetInclude::params() const
170 {
171         return params_;
172 }
173
174
175 namespace {
176
177 /// the type of inclusion
178 enum Types {
179         INCLUDE = 0,
180         VERB = 1,
181         INPUT = 2,
182         VERBAST = 3
183 };
184
185
186 Types type(InsetCommandParams const & params)
187 {
188         string const command_name = params.getCmdName();
189
190         if (command_name == "input")
191                 return INPUT;
192         if  (command_name == "verbatiminput")
193                 return VERB;
194         if  (command_name == "verbatiminput*")
195                 return VERBAST;
196         return INCLUDE;
197 }
198
199
200 bool isVerbatim(InsetCommandParams const & params)
201 {
202         string const command_name = params.getCmdName();
203         return command_name == "verbatiminput" ||
204                 command_name == "verbatiminput*";
205 }
206
207
208 string const masterFilename(Buffer const & buffer)
209 {
210         return buffer.getMasterBuffer()->fileName();
211 }
212
213
214 string const parentFilename(Buffer const & buffer)
215 {
216         return buffer.fileName();
217 }
218
219
220 FileName const includedFilename(Buffer const & buffer,
221                               InsetCommandParams const & params)
222 {
223         return makeAbsPath(to_utf8(params["filename"]),
224                            onlyPath(parentFilename(buffer)));
225 }
226
227
228 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
229
230 } // namespace anon
231
232
233 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
234 {
235         params_ = p;
236         set_label_ = false;
237
238         if (preview_->monitoring())
239                 preview_->stopMonitoring();
240
241         if (type(params_) == INPUT)
242                 add_preview(*preview_, *this, buffer);
243 }
244
245
246 auto_ptr<InsetBase> InsetInclude::doClone() const
247 {
248         return auto_ptr<InsetBase>(new InsetInclude(*this));
249 }
250
251
252 void InsetInclude::write(Buffer const &, ostream & os) const
253 {
254         write(os);
255 }
256
257
258 void InsetInclude::write(ostream & os) const
259 {
260         os << "Include " << to_utf8(params_.getCommand()) << '\n'
261            << "preview " << convert<string>(params_.preview()) << '\n';
262 }
263
264
265 void InsetInclude::read(Buffer const &, LyXLex & lex)
266 {
267         read(lex);
268 }
269
270
271 void InsetInclude::read(LyXLex & lex)
272 {
273         if (lex.isOK()) {
274                 lex.eatLine();
275                 string const command = lex.getString();
276                 params_.scanCommand(command);
277         }
278         string token;
279         while (lex.isOK()) {
280                 lex.next();
281                 token = lex.getString();
282                 if (token == "\\end_inset")
283                         break;
284                 if (token == "preview") {
285                         lex.next();
286                         params_.preview(lex.getBool());
287                 } else
288                         lex.printError("Unknown parameter name `$$Token' for command " + params_.getCmdName());
289         }
290         if (token != "\\end_inset") {
291                 lex.printError("Missing \\end_inset at this point. "
292                                "Read: `$$Token'");
293         }
294 }
295
296
297 docstring const InsetInclude::getScreenLabel(Buffer const &) const
298 {
299         docstring temp;
300
301         switch (type(params_)) {
302                 case INPUT:
303                         temp += _("Input");
304                         break;
305                 case VERB:
306                         temp += _("Verbatim Input");
307                         break;
308                 case VERBAST:
309                         temp += _("Verbatim Input*");
310                         break;
311                 case INCLUDE:
312                         temp += _("Include");
313                         break;
314         }
315
316         temp += ": ";
317
318         if (params_["filename"].empty())
319                 temp += "???";
320         else
321                 temp += from_utf8(onlyFilename(to_utf8(params_["filename"])));
322
323         return temp;
324 }
325
326
327 namespace {
328
329 /// return the child buffer if the file is a LyX doc and is loaded
330 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
331 {
332         if (isVerbatim(params))
333                 return 0;
334
335         string const included_file = includedFilename(buffer, params).absFilename();
336         if (!isLyXFilename(included_file))
337                 return 0;
338
339         return theBufferList().getBuffer(included_file);
340 }
341
342
343 /// return true if the file is or got loaded.
344 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
345 {
346         if (isVerbatim(params))
347                 return false;
348
349         FileName const included_file = includedFilename(buffer, params);
350         if (!isLyXFilename(included_file.absFilename()))
351                 return false;
352
353         Buffer * buf = theBufferList().getBuffer(included_file.absFilename());
354         if (!buf) {
355                 // the readonly flag can/will be wrong, not anymore I think.
356                 if (!fs::exists(included_file.toFilesystemEncoding()))
357                         return false;
358                 buf = theBufferList().newBuffer(included_file.absFilename());
359                 if (!loadLyXFile(buf, included_file))
360                         return false;
361         }
362         if (buf)
363                 buf->setParentName(parentFilename(buffer));
364         return buf != 0;
365 }
366
367
368 } // namespace anon
369
370
371 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
372                         OutputParams const & runparams) const
373 {
374         string incfile(to_utf8(params_["filename"]));
375
376         // Do nothing if no file name has been specified
377         if (incfile.empty())
378                 return 0;
379
380         FileName const included_file(includedFilename(buffer, params_));
381         Buffer const * const m_buffer = buffer.getMasterBuffer();
382
383         // if incfile is relative, make it relative to the master
384         // buffer directory.
385         if (!absolutePath(incfile)) {
386                 incfile = makeRelPath(included_file.absFilename(),
387                                       m_buffer->filePath());
388         }
389
390         // write it to a file (so far the complete file)
391         string const exportfile = changeExtension(incfile, ".tex");
392         string const mangled = DocFileName(changeExtension(included_file.absFilename(),
393                                                         ".tex")).mangledFilename();
394         FileName const writefile(makeAbsPath(mangled, m_buffer->temppath()));
395
396         if (!runparams.nice)
397                 incfile = mangled;
398         lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
399         lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
400         lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
401
402         if (runparams.inComment || runparams.dryrun)
403                 // Don't try to load or copy the file
404                 ;
405         else if (loadIfNeeded(buffer, params_)) {
406                 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
407
408                 if (tmp->params().textclass != m_buffer->params().textclass) {
409                         // FIXME UNICODE
410                         docstring text = bformat(_("Included file `%1$s'\n"
411                                                 "has textclass `%2$s'\n"
412                                                              "while parent file has textclass `%3$s'."),
413                                               makeDisplayPath(included_file.absFilename()),
414                                               from_utf8(tmp->params().getLyXTextClass().name()),
415                                               from_utf8(m_buffer->params().getLyXTextClass().name()));
416                         Alert::warning(_("Different textclasses"), text);
417                         //return 0;
418                 }
419
420                 tmp->markDepClean(m_buffer->temppath());
421
422 #ifdef WITH_WARNINGS
423 #warning handle non existing files
424 #warning Second argument is irrelevant!
425 // since only_body is true, makeLaTeXFile will not look at second
426 // argument. Should we set it to string(), or should makeLaTeXFile
427 // make use of it somehow? (JMarc 20031002)
428 #endif
429                 // The included file might be written in a different encoding
430                 Encoding const * const oldEnc = runparams.encoding;
431                 runparams.encoding = &tmp->params().encoding();
432                 tmp->makeLaTeXFile(writefile,
433                                    onlyPath(masterFilename(buffer)),
434                                    runparams, false);
435                 runparams.encoding = oldEnc;
436         } else {
437                 // Copy the file to the temp dir, so that .aux files etc.
438                 // are not created in the original dir. Files included by
439                 // this file will be found via input@path, see ../buffer.C.
440                 unsigned long const checksum_in  = sum(included_file);
441                 unsigned long const checksum_out = sum(writefile);
442
443                 if (checksum_in != checksum_out) {
444                         if (!copy(included_file, writefile)) {
445                                 // FIXME UNICODE
446                                 lyxerr[Debug::LATEX]
447                                         << to_utf8(bformat(_("Could not copy the file\n%1$s\n"
448                                                                   "into the temporary directory."),
449                                                    from_utf8(included_file.absFilename())))
450                                         << endl;
451                                 return 0;
452                         }
453                 }
454         }
455
456         string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
457                         "latex" : "pdflatex";
458         if (isVerbatim(params_)) {
459                 incfile = latex_path(incfile);
460                 // FIXME UNICODE
461                 os << '\\' << from_ascii(params_.getCmdName()) << '{'
462                    << from_utf8(incfile) << '}';
463         } else if (type(params_) == INPUT) {
464                 runparams.exportdata->addExternalFile(tex_format, writefile,
465                                                       exportfile);
466
467                 // \input wants file with extension (default is .tex)
468                 if (!isLyXFilename(included_file.absFilename())) {
469                         incfile = latex_path(incfile);
470                         // FIXME UNICODE
471                         os << '\\' << from_ascii(params_.getCmdName())
472                            << '{' << from_utf8(incfile) << '}';
473                 } else {
474                 incfile = changeExtension(incfile, ".tex");
475                 incfile = latex_path(incfile);
476                         // FIXME UNICODE
477                         os << '\\' << from_ascii(params_.getCmdName())
478                            << '{' << from_utf8(incfile) <<  '}';
479                 }
480         } else {
481                 runparams.exportdata->addExternalFile(tex_format, writefile,
482                                                       exportfile);
483
484                 // \include don't want extension and demands that the
485                 // file really have .tex
486                 incfile = changeExtension(incfile, string());
487                 incfile = latex_path(incfile);
488                 // FIXME UNICODE
489                 os << '\\' << from_ascii(params_.getCmdName()) << '{'
490                    << from_utf8(incfile) << '}';
491         }
492
493         return 0;
494 }
495
496
497 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
498                             OutputParams const &) const
499 {
500         if (isVerbatim(params_)) {
501                 os << '[' << getScreenLabel(buffer) << '\n';
502                 // FIXME: We don't know the encoding of the file
503                 docstring const str =
504                      from_utf8(getFileContents(includedFilename(buffer, params_)));
505                 os << str;
506                 os << "\n]";
507                 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
508         } else {
509                 docstring const str = '[' + getScreenLabel(buffer) + ']';
510                 os << str;
511                 return str.size();
512         }
513 }
514
515
516 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
517                           OutputParams const & runparams) const
518 {
519         string incfile = to_utf8(params_["filename"]);
520
521         // Do nothing if no file name has been specified
522         if (incfile.empty())
523                 return 0;
524
525         string const included_file = includedFilename(buffer, params_).absFilename();
526
527         // write it to a file (so far the complete file)
528         string const exportfile = changeExtension(incfile, ".sgml");
529         DocFileName writefile(changeExtension(included_file, ".sgml"));
530
531         if (loadIfNeeded(buffer, params_)) {
532                 Buffer * tmp = theBufferList().getBuffer(included_file);
533
534                 string const mangled = writefile.mangledFilename();
535                 writefile = makeAbsPath(mangled,
536                                         buffer.getMasterBuffer()->temppath());
537                 if (!runparams.nice)
538                         incfile = mangled;
539
540                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
541                 lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
542                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
543
544                 tmp->makeDocBookFile(writefile, runparams, true);
545         }
546
547         runparams.exportdata->addExternalFile("docbook", writefile,
548                                               exportfile);
549         runparams.exportdata->addExternalFile("docbook-xml", writefile,
550                                               exportfile);
551
552         if (isVerbatim(params_)) {
553                 os << "<inlinegraphic fileref=\""
554                    << '&' << include_label << ';'
555                    << "\" format=\"linespecific\">";
556         } else
557                 os << '&' << include_label << ';';
558
559         return 0;
560 }
561
562
563 void InsetInclude::validate(LaTeXFeatures & features) const
564 {
565         string incfile(to_utf8(params_["filename"]));
566         string writefile;
567
568         Buffer const & buffer = features.buffer();
569
570         string const included_file = includedFilename(buffer, params_).absFilename();
571
572         if (isLyXFilename(included_file))
573                 writefile = changeExtension(included_file, ".sgml");
574         else
575                 writefile = included_file;
576
577         if (!features.runparams().nice && !isVerbatim(params_)) {
578                 incfile = DocFileName(writefile).mangledFilename();
579                 writefile = makeAbsPath(incfile,
580                                         buffer.getMasterBuffer()->temppath()).absFilename();
581         }
582
583         features.includeFile(include_label, writefile);
584
585         if (isVerbatim(params_))
586                 features.require("verbatim");
587
588         // Here we must do the fun stuff...
589         // Load the file in the include if it needs
590         // to be loaded:
591         if (loadIfNeeded(buffer, params_)) {
592                 // a file got loaded
593                 Buffer * const tmp = theBufferList().getBuffer(included_file);
594                 if (tmp) {
595                         // We must temporarily change features.buffer,
596                         // otherwise it would always be the master buffer,
597                         // and nested includes would not work.
598                         features.setBuffer(*tmp);
599                         tmp->validate(features);
600                         features.setBuffer(buffer);
601                 }
602         }
603 }
604
605
606 void InsetInclude::getLabelList(Buffer const & buffer,
607                                 std::vector<docstring> & list) const
608 {
609         if (loadIfNeeded(buffer, params_)) {
610                 string const included_file = includedFilename(buffer, params_).absFilename();
611                 Buffer * tmp = theBufferList().getBuffer(included_file);
612                 tmp->setParentName("");
613                 tmp->getLabelList(list);
614                 tmp->setParentName(parentFilename(buffer));
615         }
616 }
617
618
619 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
620                 std::vector<std::pair<string, docstring> > & keys) const
621 {
622         if (loadIfNeeded(buffer, params_)) {
623                 string const included_file = includedFilename(buffer, params_).absFilename();
624                 Buffer * tmp = theBufferList().getBuffer(included_file);
625                 tmp->setParentName("");
626                 tmp->fillWithBibKeys(keys);
627                 tmp->setParentName(parentFilename(buffer));
628         }
629 }
630
631
632 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
633 {
634         Buffer * const tmp = getChildBuffer(buffer, params_);
635         if (tmp) {
636                 tmp->setParentName("");
637                 tmp->updateBibfilesCache();
638                 tmp->setParentName(parentFilename(buffer));
639         }
640 }
641
642
643 std::vector<FileName> const &
644 InsetInclude::getBibfilesCache(Buffer const & buffer) const
645 {
646         Buffer * const tmp = getChildBuffer(buffer, params_);
647         if (tmp) {
648                 tmp->setParentName("");
649                 std::vector<FileName> const & cache = tmp->getBibfilesCache();
650                 tmp->setParentName(parentFilename(buffer));
651                 return cache;
652         }
653         static std::vector<FileName> const empty;
654         return empty;
655 }
656
657
658 bool InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
659 {
660         BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
661
662         bool use_preview = false;
663         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
664                 graphics::PreviewImage const * pimage =
665                         preview_->getPreviewImage(*mi.base.bv->buffer());
666                 use_preview = pimage && pimage->image();
667         }
668
669         if (use_preview) {
670                 preview_->metrics(mi, dim);
671         } else {
672                 if (!set_label_) {
673                         set_label_ = true;
674                         button_.update(getScreenLabel(*mi.base.bv->buffer()),
675                                        true);
676                 }
677                 button_.metrics(mi, dim);
678         }
679
680         Box b(0, dim.wid, -dim.asc, dim.des);
681         button_.setBox(b);
682
683         bool const changed = dim_ != dim;
684         dim_ = dim;
685         return changed;
686 }
687
688
689 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
690 {
691         setPosCache(pi, x, y);
692
693         BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
694
695         bool use_preview = false;
696         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
697                 graphics::PreviewImage const * pimage =
698                         preview_->getPreviewImage(*pi.base.bv->buffer());
699                 use_preview = pimage && pimage->image();
700         }
701
702         if (use_preview)
703                 preview_->draw(pi, x, y);
704         else
705                 button_.draw(pi, x, y);
706 }
707
708 bool InsetInclude::display() const
709 {
710         return type(params_) != INPUT;
711 }
712
713
714
715 //
716 // preview stuff
717 //
718
719 void InsetInclude::fileChanged() const
720 {
721         Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
722         if (!buffer_ptr)
723                 return;
724
725         Buffer const & buffer = *buffer_ptr;
726         preview_->removePreview(buffer);
727         add_preview(*preview_.get(), *this, buffer);
728         preview_->startLoading(buffer);
729 }
730
731
732 namespace {
733
734 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
735 {
736         FileName const included_file = includedFilename(buffer, params);
737
738         return type(params) == INPUT && params.preview() &&
739                 isFileReadable(included_file);
740 }
741
742
743 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
744 {
745         odocstringstream os;
746         OutputParams runparams(0);
747         runparams.flavor = OutputParams::LATEX;
748         inset.latex(buffer, os, runparams);
749
750         return os.str();
751 }
752
753
754 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
755                  Buffer const & buffer)
756 {
757         InsetCommandParams const & params = inset.params();
758         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
759             preview_wanted(params, buffer)) {
760                 renderer.setAbsFile(includedFilename(buffer, params));
761                 docstring const snippet = latex_string(inset, buffer);
762                 renderer.addPreview(snippet, buffer);
763         }
764 }
765
766 } // namespace anon
767
768
769 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
770 {
771         Buffer const & buffer = ploader.buffer();
772         if (preview_wanted(params(), buffer)) {
773                 preview_->setAbsFile(includedFilename(buffer, params()));
774                 docstring const snippet = latex_string(*this, buffer);
775                 preview_->addPreview(snippet, ploader);
776         }
777 }
778
779
780 void InsetInclude::addToToc(TocList & toclist, Buffer const & buffer) const
781 {
782         Buffer const * const childbuffer = getChildBuffer(buffer, params_);
783         if (!childbuffer)
784                 return;
785
786         TocList const & childtoclist = childbuffer->tocBackend().tocs();
787         TocList::const_iterator it = childtoclist.begin();
788         TocList::const_iterator const end = childtoclist.end();
789         for(; it != end; ++it)
790                 toclist[it->first].insert(toclist[it->first].end(),
791                                 it->second.begin(), it->second.end());
792 }
793
794
795 void InsetInclude::updateLabels(Buffer const & buffer) const
796 {
797         Buffer const * const childbuffer = getChildBuffer(buffer, params_);
798         if (!childbuffer)
799                 return;
800
801         lyx::updateLabels(*childbuffer, true);
802 }
803
804
805 string const InsetIncludeMailer::name_("include");
806
807 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
808         : inset_(inset)
809 {}
810
811
812 string const InsetIncludeMailer::inset2string(Buffer const &) const
813 {
814         return params2string(inset_.params());
815 }
816
817
818 void InsetIncludeMailer::string2params(string const & in,
819                                        InsetCommandParams & params)
820 {
821         params.clear();
822         if (in.empty())
823                 return;
824
825         istringstream data(in);
826         LyXLex lex(0,0);
827         lex.setStream(data);
828
829         string name;
830         lex >> name;
831         if (!lex || name != name_)
832                 return print_mailer_error("InsetIncludeMailer", in, 1, name_);
833
834         // This is part of the inset proper that is usually swallowed
835         // by LyXText::readInset
836         string id;
837         lex >> id;
838         if (!lex || id != "Include")
839                 return print_mailer_error("InsetIncludeMailer", in, 2, "Include");
840
841         InsetInclude inset(params);
842         inset.read(lex);
843         params = inset.params();
844 }
845
846
847 string const
848 InsetIncludeMailer::params2string(InsetCommandParams const & params)
849 {
850         InsetInclude inset(params);
851         ostringstream data;
852         data << name_ << ' ';
853         inset.write(data);
854         data << "\\end_inset\n";
855         return data.str();
856 }
857
858
859 } // namespace lyx