]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
more unicode filenames
[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 string 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.next();
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                 // FIXME: We don't know the encoding of the filename
322                 temp += from_ascii(onlyFilename(to_utf8(params_["filename"])));
323
324         return temp;
325 }
326
327
328 namespace {
329
330 /// return the child buffer if the file is a LyX doc and is loaded
331 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
332 {
333         if (isVerbatim(params))
334                 return 0;
335
336         string const included_file = includedFilename(buffer, params);
337         if (!isLyXFilename(included_file))
338                 return 0;
339
340         return theBufferList().getBuffer(included_file);
341 }
342
343
344 /// return true if the file is or got loaded.
345 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
346 {
347         if (isVerbatim(params))
348                 return false;
349
350         string const included_file = includedFilename(buffer, params);
351         if (!isLyXFilename(included_file))
352                 return false;
353
354         Buffer * buf = theBufferList().getBuffer(included_file);
355         if (!buf) {
356                 // the readonly flag can/will be wrong, not anymore I think.
357                 FileName const fullname(included_file);
358                 if (!fs::exists(fullname.toFilesystemEncoding()))
359                         return false;
360                 buf = theBufferList().newBuffer(included_file);
361                 if (!loadLyXFile(buf, fullname))
362                         return false;
363         }
364         if (buf)
365                 buf->setParentName(parentFilename(buffer));
366         return buf != 0;
367 }
368
369
370 } // namespace anon
371
372
373 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
374                         OutputParams const & runparams) const
375 {
376         string incfile(to_utf8(params_["filename"]));
377
378         // Do nothing if no file name has been specified
379         if (incfile.empty())
380                 return 0;
381
382         FileName const included_file(includedFilename(buffer, params_));
383         Buffer const * const m_buffer = buffer.getMasterBuffer();
384
385         // if incfile is relative, make it relative to the master
386         // buffer directory.
387         if (!absolutePath(incfile)) {
388                 incfile = makeRelPath(included_file.absFilename(),
389                                       m_buffer->filePath());
390         }
391
392         // write it to a file (so far the complete file)
393         string const exportfile = changeExtension(incfile, ".tex");
394         string const mangled = DocFileName(changeExtension(included_file.absFilename(),
395                                                         ".tex")).mangledFilename();
396         FileName const writefile(makeAbsPath(mangled, m_buffer->temppath()));
397
398         if (!runparams.nice)
399                 incfile = mangled;
400         lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
401         lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
402         lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
403
404         if (runparams.inComment || runparams.dryrun)
405                 // Don't try to load or copy the file
406                 ;
407         else if (loadIfNeeded(buffer, params_)) {
408                 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
409
410                 if (tmp->params().textclass != m_buffer->params().textclass) {
411                         // FIXME UNICODE
412                         docstring text = bformat(_("Included file `%1$s'\n"
413                                                 "has textclass `%2$s'\n"
414                                                              "while parent file has textclass `%3$s'."),
415                                               makeDisplayPath(included_file.absFilename()),
416                                               from_utf8(tmp->params().getLyXTextClass().name()),
417                                               from_utf8(m_buffer->params().getLyXTextClass().name()));
418                         Alert::warning(_("Different textclasses"), text);
419                         //return 0;
420                 }
421
422                 tmp->markDepClean(m_buffer->temppath());
423
424 #ifdef WITH_WARNINGS
425 #warning handle non existing files
426 #warning Second argument is irrelevant!
427 // since only_body is true, makeLaTeXFile will not look at second
428 // argument. Should we set it to string(), or should makeLaTeXFile
429 // make use of it somehow? (JMarc 20031002)
430 #endif
431                 tmp->makeLaTeXFile(writefile.absFilename(),
432                                    onlyPath(masterFilename(buffer)),
433                                    runparams, false);
434         } else {
435                 // Copy the file to the temp dir, so that .aux files etc.
436                 // are not created in the original dir. Files included by
437                 // this file will be found via input@path, see ../buffer.C.
438                 unsigned long const checksum_in  = sum(included_file);
439                 unsigned long const checksum_out = sum(writefile);
440
441                 if (checksum_in != checksum_out) {
442                         if (!copy(included_file, writefile)) {
443                                 // FIXME UNICODE
444                                 lyxerr[Debug::LATEX]
445                                         << to_utf8(bformat(_("Could not copy the file\n%1$s\n"
446                                                                   "into the temporary directory."),
447                                                    from_utf8(included_file.absFilename())))
448                                         << endl;
449                                 return 0;
450                         }
451                 }
452         }
453
454         string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
455                         "latex" : "pdflatex";
456         if (isVerbatim(params_)) {
457                 incfile = latex_path(incfile);
458                 // FIXME UNICODE
459                 os << '\\' << from_ascii(params_.getCmdName()) << '{'
460                    << from_utf8(incfile) << '}';
461         } else if (type(params_) == INPUT) {
462                 runparams.exportdata->addExternalFile(tex_format, writefile,
463                                                       exportfile);
464
465                 // \input wants file with extension (default is .tex)
466                 if (!isLyXFilename(included_file.absFilename())) {
467                         incfile = latex_path(incfile);
468                         // FIXME UNICODE
469                         os << '\\' << from_ascii(params_.getCmdName())
470                            << '{' << from_utf8(incfile) << '}';
471                 } else {
472                 incfile = changeExtension(incfile, ".tex");
473                 incfile = latex_path(incfile);
474                         // FIXME UNICODE
475                         os << '\\' << from_ascii(params_.getCmdName())
476                            << '{' << from_utf8(incfile) <<  '}';
477                 }
478         } else {
479                 runparams.exportdata->addExternalFile(tex_format, writefile,
480                                                       exportfile);
481
482                 // \include don't want extension and demands that the
483                 // file really have .tex
484                 incfile = changeExtension(incfile, string());
485                 incfile = latex_path(incfile);
486                 // FIXME UNICODE
487                 os << '\\' << from_ascii(params_.getCmdName()) << '{'
488                    << from_utf8(incfile) << '}';
489         }
490
491         return 0;
492 }
493
494
495 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
496                         OutputParams const &) const
497 {
498         if (isVerbatim(params_)) {
499                 // FIXME: We don't know the encoding of the file
500                 docstring const str = from_utf8(
501                         getFileContents(FileName(includedFilename(buffer, params_))));
502                 os << str;
503                 // Return how many newlines we issued.
504                 return int(lyx::count(str.begin(), str.end(), '\n'));
505         }
506         return 0;
507 }
508
509
510 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
511                           OutputParams const & runparams) const
512 {
513         string incfile = to_utf8(params_["filename"]);
514
515         // Do nothing if no file name has been specified
516         if (incfile.empty())
517                 return 0;
518
519         string const included_file = includedFilename(buffer, params_);
520
521         // write it to a file (so far the complete file)
522         string const exportfile = changeExtension(incfile, ".sgml");
523         DocFileName writefile(changeExtension(included_file, ".sgml"));
524
525         if (loadIfNeeded(buffer, params_)) {
526                 Buffer * tmp = theBufferList().getBuffer(included_file);
527
528                 string const mangled = writefile.mangledFilename();
529                 writefile = makeAbsPath(mangled,
530                                         buffer.getMasterBuffer()->temppath());
531                 if (!runparams.nice)
532                         incfile = mangled;
533
534                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
535                 lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
536                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
537
538                 tmp->makeDocBookFile(writefile.absFilename(), runparams, true);
539         }
540
541         runparams.exportdata->addExternalFile("docbook", writefile,
542                                               exportfile);
543         runparams.exportdata->addExternalFile("docbook-xml", writefile,
544                                               exportfile);
545
546         if (isVerbatim(params_)) {
547                 os << "<inlinegraphic fileref=\""
548                    << '&' << include_label << ';'
549                    << "\" format=\"linespecific\">";
550         } else
551                 os << '&' << include_label << ';';
552
553         return 0;
554 }
555
556
557 void InsetInclude::validate(LaTeXFeatures & features) const
558 {
559         string incfile(to_utf8(params_["filename"]));
560         string writefile;
561
562         Buffer const & buffer = features.buffer();
563
564         string const included_file = includedFilename(buffer, params_);
565
566         if (isLyXFilename(included_file))
567                 writefile = changeExtension(included_file, ".sgml");
568         else
569                 writefile = included_file;
570
571         if (!features.runparams().nice && !isVerbatim(params_)) {
572                 incfile = DocFileName(writefile).mangledFilename();
573                 writefile = makeAbsPath(incfile,
574                                         buffer.getMasterBuffer()->temppath());
575         }
576
577         features.includeFile(include_label, writefile);
578
579         if (isVerbatim(params_))
580                 features.require("verbatim");
581
582         // Here we must do the fun stuff...
583         // Load the file in the include if it needs
584         // to be loaded:
585         if (loadIfNeeded(buffer, params_)) {
586                 // a file got loaded
587                 Buffer * const tmp = theBufferList().getBuffer(included_file);
588                 if (tmp) {
589                         // We must temporarily change features.buffer,
590                         // otherwise it would always be the master buffer,
591                         // and nested includes would not work.
592                         features.setBuffer(*tmp);
593                         tmp->validate(features);
594                         features.setBuffer(buffer);
595                 }
596         }
597 }
598
599
600 void InsetInclude::getLabelList(Buffer const & buffer,
601                                 std::vector<docstring> & list) const
602 {
603         if (loadIfNeeded(buffer, params_)) {
604                 string const included_file = includedFilename(buffer, params_);
605                 Buffer * tmp = theBufferList().getBuffer(included_file);
606                 tmp->setParentName("");
607                 tmp->getLabelList(list);
608                 tmp->setParentName(parentFilename(buffer));
609         }
610 }
611
612
613 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
614                                    std::vector<std::pair<string,string> > & keys) const
615 {
616         if (loadIfNeeded(buffer, params_)) {
617                 string const included_file = includedFilename(buffer, params_);
618                 Buffer * tmp = theBufferList().getBuffer(included_file);
619                 tmp->setParentName("");
620                 tmp->fillWithBibKeys(keys);
621                 tmp->setParentName(parentFilename(buffer));
622         }
623 }
624
625
626 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
627 {
628         Buffer * const tmp = getChildBuffer(buffer, params_);
629         if (tmp) {
630                 tmp->setParentName("");
631                 tmp->updateBibfilesCache();
632                 tmp->setParentName(parentFilename(buffer));
633         }
634 }
635
636
637 std::vector<FileName> const &
638 InsetInclude::getBibfilesCache(Buffer const & buffer) const
639 {
640         Buffer * const tmp = getChildBuffer(buffer, params_);
641         if (tmp) {
642                 tmp->setParentName("");
643                 std::vector<FileName> const & cache = tmp->getBibfilesCache();
644                 tmp->setParentName(parentFilename(buffer));
645                 return cache;
646         }
647         static std::vector<FileName> const empty;
648         return empty;
649 }
650
651
652 bool InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
653 {
654         BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
655
656         bool use_preview = false;
657         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
658                 graphics::PreviewImage const * pimage =
659                         preview_->getPreviewImage(*mi.base.bv->buffer());
660                 use_preview = pimage && pimage->image();
661         }
662
663         if (use_preview) {
664                 preview_->metrics(mi, dim);
665         } else {
666                 if (!set_label_) {
667                         set_label_ = true;
668                         button_.update(getScreenLabel(*mi.base.bv->buffer()),
669                                        true);
670                 }
671                 button_.metrics(mi, dim);
672         }
673
674         Box b(0, dim.wid, -dim.asc, dim.des);
675         button_.setBox(b);
676
677         bool const changed = dim_ != dim;
678         dim_ = dim;
679         return changed;
680 }
681
682
683 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
684 {
685         setPosCache(pi, x, y);
686
687         BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
688
689         bool use_preview = false;
690         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
691                 graphics::PreviewImage const * pimage =
692                         preview_->getPreviewImage(*pi.base.bv->buffer());
693                 use_preview = pimage && pimage->image();
694         }
695
696         if (use_preview)
697                 preview_->draw(pi, x, y);
698         else
699                 button_.draw(pi, x, y);
700 }
701
702 bool InsetInclude::display() const
703 {
704         return type(params_) != INPUT;
705 }
706
707
708
709 //
710 // preview stuff
711 //
712
713 void InsetInclude::fileChanged() const
714 {
715         Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
716         if (!buffer_ptr)
717                 return;
718
719         Buffer const & buffer = *buffer_ptr;
720         preview_->removePreview(buffer);
721         add_preview(*preview_.get(), *this, buffer);
722         preview_->startLoading(buffer);
723 }
724
725
726 namespace {
727
728 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
729 {
730         string const included_file = includedFilename(buffer, params);
731
732         return type(params) == INPUT && params.preview() &&
733                 isFileReadable(FileName(included_file));
734 }
735
736
737 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
738 {
739         odocstringstream os;
740         OutputParams runparams;
741         runparams.flavor = OutputParams::LATEX;
742         inset.latex(buffer, os, runparams);
743
744         return os.str();
745 }
746
747
748 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
749                  Buffer const & buffer)
750 {
751         InsetCommandParams const & params = inset.params();
752         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
753             preview_wanted(params, buffer)) {
754                 renderer.setAbsFile(FileName(includedFilename(buffer, params)));
755                 docstring const snippet = latex_string(inset, buffer);
756                 renderer.addPreview(snippet, buffer);
757         }
758 }
759
760 } // namespace anon
761
762
763 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
764 {
765         Buffer const & buffer = ploader.buffer();
766         if (preview_wanted(params(), buffer)) {
767                 preview_->setAbsFile(FileName(includedFilename(buffer, params())));
768                 docstring const snippet = latex_string(*this, buffer);
769                 preview_->addPreview(snippet, ploader);
770         }
771 }
772
773
774 void InsetInclude::addToToc(TocList & toclist, Buffer const & buffer) const
775 {
776         Buffer const * const childbuffer = getChildBuffer(buffer, params_);
777         if (!childbuffer)
778                 return;
779
780         TocList const & childtoclist = childbuffer->tocBackend().tocs();
781         TocList::const_iterator it = childtoclist.begin();
782         TocList::const_iterator const end = childtoclist.end();
783         for(; it != end; ++it)
784                 toclist[it->first].insert(toclist[it->first].end(),
785                                 it->second.begin(), it->second.end());
786 }
787
788
789 void InsetInclude::updateLabels(Buffer const & buffer) const
790 {
791         Buffer const * const childbuffer = getChildBuffer(buffer, params_);
792         if (!childbuffer)
793                 return;
794
795         lyx::updateLabels(*childbuffer, true);
796 }
797
798
799 string const InsetIncludeMailer::name_("include");
800
801 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
802         : inset_(inset)
803 {}
804
805
806 string const InsetIncludeMailer::inset2string(Buffer const &) const
807 {
808         return params2string(inset_.params());
809 }
810
811
812 void InsetIncludeMailer::string2params(string const & in,
813                                        InsetCommandParams & params)
814 {
815         params.clear();
816         if (in.empty())
817                 return;
818
819         istringstream data(in);
820         LyXLex lex(0,0);
821         lex.setStream(data);
822
823         string name;
824         lex >> name;
825         if (!lex || name != name_)
826                 return print_mailer_error("InsetIncludeMailer", in, 1, name_);
827
828         // This is part of the inset proper that is usually swallowed
829         // by LyXText::readInset
830         string id;
831         lex >> id;
832         if (!lex || id != "Include")
833                 return print_mailer_error("InsetIncludeMailer", in, 2, "Include");
834
835         InsetInclude inset(params);
836         inset.read(lex);
837         params = inset.params();
838 }
839
840
841 string const
842 InsetIncludeMailer::params2string(InsetCommandParams const & params)
843 {
844         InsetInclude inset(params);
845         ostringstream data;
846         data << name_ << ' ';
847         inset.write(data);
848         data << "\\end_inset\n";
849         return data.str();
850 }
851
852
853 } // namespace lyx