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