]> git.lyx.org Git - lyx.git/blob - src/insets/InsetExternal.cpp
Embedding: move file validation from InsetExternal to GuiExternal
[lyx.git] / src / insets / InsetExternal.cpp
1 /**
2  * \file InsetExternal.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetExternal.h"
14 #include "insets/ExternalSupport.h"
15 #include "insets/ExternalTemplate.h"
16 #include "insets/RenderButton.h"
17 #include "insets/RenderGraphic.h"
18 #include "insets/RenderPreview.h"
19
20 #include "Buffer.h"
21 #include "Cursor.h"
22 #include "DispatchResult.h"
23 #include "Exporter.h"
24 #include "FuncStatus.h"
25 #include "FuncRequest.h"
26 #include "LaTeXFeatures.h"
27 #include "Lexer.h"
28 #include "LyX.h" // use_gui
29 #include "LyXRC.h"
30 #include "MetricsInfo.h"
31 #include "OutputParams.h"
32
33 #include "graphics/PreviewLoader.h"
34
35 #include "support/debug.h"
36 #include "support/filetools.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
39 #include "support/lyxlib.h"
40 #include "support/convert.h"
41 #include "support/Translator.h"
42
43 #include <boost/bind.hpp>
44
45 #include <sstream>
46
47 using namespace std;
48 using namespace lyx::support;
49
50 namespace {
51
52 lyx::external::DisplayType const defaultDisplayType = lyx::external::NoDisplay;
53
54 unsigned int const defaultLyxScale = 100;
55
56 string defaultTemplateName;
57
58 } // namespace anon
59
60
61 namespace lyx {
62
63 namespace external {
64
65 TempName::TempName()
66 {
67         FileName const tempname = FileName::tempName("lyxext");
68         // FIXME: This is unsafe
69         tempname.removeFile();
70         // must have an extension for the converter code to work correctly.
71         tempname_ = FileName(tempname.absFilename() + ".tmp");
72 }
73
74
75 TempName::TempName(TempName const &)
76 {
77         tempname_ = TempName()();
78 }
79
80
81 TempName::~TempName()
82 {
83         tempname_.removeFile();
84 }
85
86
87 TempName & TempName::operator=(TempName const & other)
88 {
89         if (this != &other)
90                 tempname_ = TempName()();
91         return *this;
92 }
93
94
95 namespace {
96
97 /// The translator between the Display enum and corresponding lyx string.
98 Translator<DisplayType, string> const initTranslator()
99 {
100         Translator<DisplayType, string> translator(DefaultDisplay, "default");
101
102         // Fill the display translator
103         translator.addPair(MonochromeDisplay, "monochrome");
104         translator.addPair(GrayscaleDisplay, "grayscale");
105         translator.addPair(ColorDisplay, "color");
106         translator.addPair(PreviewDisplay, "preview");
107         translator.addPair(NoDisplay, "none");
108
109         return translator;
110 }
111
112 } // namespace anon
113
114
115 Translator<DisplayType, string> const & displayTranslator()
116 {
117         static Translator<DisplayType, string> const translator =
118                 initTranslator();
119         return translator;
120 }
121
122 } // namespace external
123
124
125 InsetExternalParams::InsetExternalParams()
126         : display(defaultDisplayType),
127           lyxscale(defaultLyxScale),
128           draft(false)
129 {
130         if (defaultTemplateName.empty()) {
131                 external::TemplateManager const & etm =
132                         external::TemplateManager::get();
133                 templatename_ = etm.getTemplates().begin()->first;
134         } else
135                 templatename_ = defaultTemplateName;
136 }
137
138
139 namespace {
140
141 template <typename T>
142 void clearIfNotFound(T & data, external::TransformID value,
143                      vector<external::TransformID> const & ids)
144 {
145         typedef vector<external::TransformID>::const_iterator
146                 const_iterator;
147
148         const_iterator it  = ids.begin();
149         const_iterator end = ids.end();
150         it = find(it, end, value);
151         if (it == end)
152                 data = T();
153 }
154
155 } // namespace anon
156
157
158 void InsetExternalParams::settemplate(string const & name)
159 {
160         templatename_ = name;
161
162         external::TemplateManager const & etm =
163                 external::TemplateManager::get();
164         external::Template const * const et = etm.getTemplateByName(name);
165         if (!et)
166                 // Be safe. Don't lose data.
167                 return;
168
169         // Ascertain which transforms the template supports.
170         // Empty all those that it doesn't.
171         vector<external::TransformID> const & ids = et->transformIds;
172         clearIfNotFound(clipdata,     external::Clip,   ids);
173         clearIfNotFound(extradata,    external::Extra,  ids);
174         clearIfNotFound(resizedata,   external::Resize, ids);
175         clearIfNotFound(rotationdata, external::Rotate, ids);
176 }
177
178
179 void InsetExternalParams::write(Buffer const & buf, ostream & os) const
180 {
181         os << "External\n"
182            << "\ttemplate " << templatename() << '\n';
183
184         if (!filename.empty()) {
185                 os << "\tfilename " << filename.outputFilename(buf.filePath()) << '\n';
186                 os << "\tembed " << (filename.embedded() ? filename.inzipName() : "\"\"") << '\n';
187         }
188         if (display != defaultDisplayType)
189                 os << "\tdisplay "
190                    << external::displayTranslator().find(display)
191                    << '\n';
192
193         if (lyxscale != defaultLyxScale)
194                 os << "\tlyxscale " << convert<string>(lyxscale) << '\n';
195
196         if (draft)
197                 os << "\tdraft\n";
198
199         if (!clipdata.bbox.empty())
200                 os << "\tboundingBox " << clipdata.bbox << '\n';
201         if (clipdata.clip)
202                 os << "\tclip\n";
203
204         external::ExtraData::const_iterator it  = extradata.begin();
205         external::ExtraData::const_iterator end = extradata.end();
206         for (; it != end; ++it) {
207                 if (!it->second.empty())
208                         os << "\textra " << it->first << " \""
209                            << it->second << "\"\n";
210         }
211
212         if (!rotationdata.no_rotation()) {
213                 os << "\trotateAngle " << rotationdata.adjAngle() << '\n';
214                 if (rotationdata.origin() != external::RotationData::DEFAULT)
215                         os << "\trotateOrigin "
216                            << rotationdata.originString() << '\n';
217         }
218
219         if (!resizedata.no_resize()) {
220                 double const scl = convert<double>(resizedata.scale);
221                 if (!float_equal(scl, 0.0, 0.05)) {
222                         if (!float_equal(scl, 100.0, 0.05))
223                                 os << "\tscale "
224                                    << resizedata.scale << '\n';
225                 } else {
226                         if (!resizedata.width.zero())
227                                 os << "\twidth "
228                                    << resizedata.width.asString() << '\n';
229                         if (!resizedata.height.zero())
230                                 os << "\theight "
231                                    << resizedata.height.asString() << '\n';
232                 }
233                 if (resizedata.keepAspectRatio)
234                         os << "\tkeepAspectRatio\n";
235         }
236 }
237
238
239 bool InsetExternalParams::read(Buffer const & buffer, Lexer & lex)
240 {
241         enum ExternalTags {
242                 EX_TEMPLATE = 1,
243                 EX_FILENAME,
244                 EX_EMBED,
245                 EX_DISPLAY,
246                 EX_LYXSCALE,
247                 EX_DRAFT,
248                 EX_BOUNDINGBOX,
249                 EX_CLIP,
250                 EX_EXTRA,
251                 EX_HEIGHT,
252                 EX_KEEPASPECTRATIO,
253                 EX_ROTATEANGLE,
254                 EX_ROTATEORIGIN,
255                 EX_SCALE,
256                 EX_WIDTH,
257                 EX_END
258         };
259
260         keyword_item external_tags[] = {
261                 { "\\end_inset",     EX_END },
262                 { "boundingBox",     EX_BOUNDINGBOX },
263                 { "clip",            EX_CLIP },
264                 { "display",         EX_DISPLAY},
265                 { "draft",           EX_DRAFT},
266                 { "embed",           EX_EMBED},
267                 { "extra",           EX_EXTRA },
268                 { "filename",        EX_FILENAME},
269                 { "height",          EX_HEIGHT },
270                 { "keepAspectRatio", EX_KEEPASPECTRATIO },
271                 { "lyxscale",        EX_LYXSCALE},
272                 { "rotateAngle",     EX_ROTATEANGLE },
273                 { "rotateOrigin",    EX_ROTATEORIGIN },
274                 { "scale",           EX_SCALE },
275                 { "template",        EX_TEMPLATE },
276                 { "width",           EX_WIDTH }
277         };
278
279         PushPopHelper pph(lex, external_tags, EX_END);
280
281         bool found_end  = false;
282         bool read_error = false;
283
284         while (lex.isOK()) {
285                 switch (lex.lex()) {
286                 case EX_TEMPLATE:
287                         lex.next();
288                         templatename_ = lex.getString();
289                         break;
290
291                 case EX_FILENAME: {
292                         lex.eatLine();
293                         string const name = lex.getString();
294                         filename.set(name, buffer.filePath());
295                         break;
296                 }
297                 
298                 case EX_EMBED: {
299                         lex.next();
300                         string const name = lex.getString();
301                         filename.setInzipName(name);
302                         filename.setEmbed(!name.empty());
303                         break;
304                 }
305
306                 case EX_DISPLAY: {
307                         lex.next();
308                         string const name = lex.getString();
309                         display = external::displayTranslator().find(name);
310                         break;
311                 }
312
313                 case EX_LYXSCALE:
314                         lex.next();
315                         lyxscale = lex.getInteger();
316                         break;
317
318                 case EX_DRAFT:
319                         draft = true;
320                         break;
321
322                 case EX_BOUNDINGBOX:
323                         lex.next();
324                         clipdata.bbox.xl = lex.getInteger();
325                         lex.next();
326                         clipdata.bbox.yb = lex.getInteger();
327                         lex.next();
328                         clipdata.bbox.xr = lex.getInteger();
329                         lex.next();
330                         clipdata.bbox.yt = lex.getInteger();
331                         break;
332
333                 case EX_CLIP:
334                         clipdata.clip = true;
335                         break;
336
337                 case EX_EXTRA: {
338                         lex.next();
339                         string const name = lex.getString();
340                         lex.next();
341                         extradata.set(name, lex.getString());
342                         break;
343                 }
344
345                 case EX_HEIGHT:
346                         lex.next();
347                         resizedata.height = Length(lex.getString());
348                         break;
349
350                 case EX_KEEPASPECTRATIO:
351                         resizedata.keepAspectRatio = true;
352                         break;
353
354                 case EX_ROTATEANGLE:
355                         lex.next();
356                         rotationdata.angle = lex.getString();
357                         break;
358
359                 case EX_ROTATEORIGIN:
360                         lex.next();
361                         rotationdata.origin(lex.getString());
362                         break;
363
364                 case EX_SCALE:
365                         lex.next();
366                         resizedata.scale = lex.getString();
367                         break;
368
369                 case EX_WIDTH:
370                         lex.next();
371                         resizedata.width = Length(lex.getString());
372                         break;
373
374                 case EX_END:
375                         found_end = true;
376                         break;
377
378                 default:
379                         lex.printError("ExternalInset::read: Wrong tag: $$Token");
380                         read_error = true;
381                         break;
382                 }
383
384                 if (found_end || read_error)
385                         break;
386         }
387
388         if (!found_end)
389                 lex.printError("ExternalInsetParams::read: Missing \\end_inset.");
390
391         // This is a trick to make sure that the data are self-consistent.
392         settemplate(templatename_);
393
394         if (lyxerr.debugging(Debug::EXTERNAL)) {
395                 lyxerr  << "InsetExternalParams::read:\n";
396                 write(buffer, lyxerr);
397         }
398
399         return !read_error;
400 }
401
402
403 InsetExternal::InsetExternal()
404         : renderer_(new RenderButton)
405 {}
406
407
408 InsetExternal::InsetExternal(InsetExternal const & other)
409         : Inset(other),
410           boost::signals::trackable(),
411           params_(other.params_),
412           renderer_(other.renderer_->clone(this))
413 {}
414
415
416 InsetExternal::~InsetExternal()
417 {
418         InsetExternalMailer(*this).hideDialog();
419 }
420
421
422 void InsetExternal::statusChanged() const
423 {
424         updateFrontend();
425 }
426
427
428 void InsetExternal::doDispatch(Cursor & cur, FuncRequest & cmd)
429 {
430         switch (cmd.action) {
431
432         case LFUN_EXTERNAL_EDIT: {
433                 InsetExternalParams p;
434                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer(), p);
435                 external::editExternal(p, buffer());
436                 break;
437         }
438
439         case LFUN_INSET_MODIFY: {
440                 InsetExternalParams p;
441                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer(), p);
442                 setParams(p);
443                 break;
444         }
445
446         case LFUN_INSET_DIALOG_UPDATE:
447                 InsetExternalMailer(*this).updateDialog(&cur.bv());
448                 break;
449
450         case LFUN_MOUSE_RELEASE:
451                 if (!cur.selection())
452                         InsetExternalMailer(*this).showDialog(&cur.bv());
453                 break;
454
455         default:
456                 Inset::doDispatch(cur, cmd);
457         }
458 }
459
460
461 bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
462                 FuncStatus & flag) const
463 {
464         switch (cmd.action) {
465
466         case LFUN_EXTERNAL_EDIT:
467         case LFUN_INSET_MODIFY:
468         case LFUN_INSET_DIALOG_UPDATE:
469                 flag.enabled(true);
470                 return true;
471
472         default:
473                 return Inset::getStatus(cur, cmd, flag);
474         }
475 }
476
477
478 void InsetExternal::registerEmbeddedFiles(EmbeddedFileList & files) const
479 {
480         files.registerFile(params_.filename, this, buffer());
481 }
482
483
484 void InsetExternal::updateEmbeddedFile(EmbeddedFile const & file)
485 {
486         params_.filename = file;
487 }
488
489
490 void InsetExternal::edit(Cursor & cur, bool, EntryDirection)
491 {
492         InsetExternalMailer(*this).showDialog(&cur.bv());
493 }
494
495
496 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
497 {
498         renderer_->metrics(mi, dim);
499 }
500
501
502 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
503 {
504         renderer_->draw(pi, x, y);
505 }
506
507
508 namespace {
509
510 enum RenderType {
511         RENDERBUTTON,
512         RENDERGRAPHIC,
513         RENDERPREVIEW
514 };
515
516
517 RenderType getRenderType(InsetExternalParams const & p)
518 {
519         if (!external::getTemplatePtr(p) ||
520             p.filename.empty() ||
521             p.display == external::NoDisplay)
522                 return RENDERBUTTON;
523
524         if (p.display == external::PreviewDisplay) {
525                 if (RenderPreview::status() != LyXRC::PREVIEW_OFF)
526                         return RENDERPREVIEW;
527                 return RENDERBUTTON;
528         }
529
530         if (p.display == external::DefaultDisplay &&
531             lyxrc.display_graphics == graphics::NoDisplay)
532                 return RENDERBUTTON;
533         return RENDERGRAPHIC;
534 }
535
536
537 graphics::Params get_grfx_params(InsetExternalParams const & eparams)
538 {
539         graphics::Params gparams;
540
541         gparams.filename = eparams.filename.availableFile();
542         gparams.icon = eparams.filename.embedded() ? "pin.png" : "";
543         gparams.scale = eparams.lyxscale;
544         if (eparams.clipdata.clip)
545                 gparams.bb = eparams.clipdata.bbox;
546         gparams.angle = convert<double>(eparams.rotationdata.adjAngle());
547
548         switch (eparams.display) {
549         case external::DefaultDisplay:
550                 gparams.display = graphics::DefaultDisplay;
551                 break;
552         case external::MonochromeDisplay:
553                 gparams.display = graphics::MonochromeDisplay;
554                 break;
555         case external::GrayscaleDisplay:
556                 gparams.display = graphics::GrayscaleDisplay;
557                 break;
558         case external::ColorDisplay:
559                 gparams.display = graphics::ColorDisplay;
560                 break;
561         case external::NoDisplay:
562                 gparams.display = graphics::NoDisplay;
563                 break;
564         default:
565                 BOOST_ASSERT(false);
566         }
567         if (gparams.display == graphics::DefaultDisplay)
568                 gparams.display = graphics::DisplayType(lyxrc.display_graphics);
569         // Override the above if we're not using a gui
570         if (!use_gui)
571                 gparams.display = graphics::NoDisplay;
572
573         return gparams;
574 }
575
576
577 docstring screenLabel(InsetExternalParams const & params,
578                             Buffer const & buffer)
579 {
580         external::Template const * const ptr =
581                 external::getTemplatePtr(params);
582         if (!ptr)
583                 // FIXME UNICODE
584                 return bformat((_("External template %1$s is not installed")),
585                                         from_utf8(params.templatename()));
586         // FIXME UNICODE
587         docstring gui = _(ptr->guiName);
588         return from_utf8(external::doSubstitution(params, buffer,
589                                 to_utf8(gui), false));
590 }
591
592 } // namespace anon
593
594
595 static bool isPreviewWanted(InsetExternalParams const & params)
596 {
597         return params.display == external::PreviewDisplay &&
598                 params.filename.isReadableFile();
599 }
600
601
602 static docstring latexString(InsetExternal const & inset)
603 {
604         odocstringstream os;
605         // We don't need to set runparams.encoding since it is not used by
606         // latex().
607         OutputParams runparams(0);
608         runparams.flavor = OutputParams::LATEX;
609         inset.latex(os, runparams);
610         return os.str();
611 }
612
613
614 static void add_preview_and_start_loading(RenderMonitoredPreview & renderer,
615                                    InsetExternal const & inset,
616                                    Buffer const & buffer)
617 {
618         InsetExternalParams const & params = inset.params();
619
620         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
621             isPreviewWanted(params)) {
622                 renderer.setAbsFile(params.filename);
623                 docstring const snippet = latexString(inset);
624                 renderer.addPreview(snippet, buffer);
625                 renderer.startLoading(buffer);
626         }
627 }
628
629
630 InsetExternalParams const & InsetExternal::params() const
631 {
632         return params_;
633 }
634
635
636 void InsetExternal::setParams(InsetExternalParams const & p)
637 {
638         params_ = p;
639
640         // Subsequent calls to the InsetExternal::Params default constructor
641         // will use this.
642         defaultTemplateName = params_.templatename();
643
644         switch (getRenderType(params_)) {
645         case RENDERBUTTON: {
646                 RenderButton * button_ptr = renderer_->asButton();
647                 if (!button_ptr) {
648                         renderer_.reset(new RenderButton);
649                         button_ptr = renderer_->asButton();
650                 }
651
652                 button_ptr->update(screenLabel(params_, buffer()), true);
653                 break;
654         }
655
656         case RENDERGRAPHIC: {
657                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
658                 if (!graphic_ptr) {
659                         renderer_.reset(new RenderGraphic(this));
660                         graphic_ptr = renderer_->asGraphic();
661                 }
662
663                 graphic_ptr->update(get_grfx_params(params_));
664
665                 break;
666         }
667
668         case RENDERPREVIEW: {
669                 RenderMonitoredPreview * preview_ptr =
670                         renderer_->asMonitoredPreview();
671                 if (!preview_ptr) {
672                         renderer_.reset(new RenderMonitoredPreview(this));
673                         preview_ptr = renderer_->asMonitoredPreview();
674                         preview_ptr->fileChanged(
675                                 boost::bind(&InsetExternal::fileChanged, this));
676                 }
677
678                 if (preview_ptr->monitoring())
679                         preview_ptr->stopMonitoring();
680                 add_preview_and_start_loading(*preview_ptr, *this, buffer());
681
682                 break;
683         }
684         }
685 }
686
687
688 void InsetExternal::fileChanged() const
689 {
690         Buffer const * const buffer = updateFrontend();
691         if (!buffer)
692                 return;
693
694         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
695         BOOST_ASSERT(ptr);
696
697         ptr->removePreview(*buffer);
698         add_preview_and_start_loading(*ptr, *this, *buffer);
699 }
700
701
702 void InsetExternal::write(ostream & os) const
703 {
704         params_.write(buffer(), os);
705 }
706
707
708 void InsetExternal::read(Lexer & lex)
709 {
710         InsetExternalParams params;
711         if (params.read(buffer(), lex)) {
712                 // exception handling is not needed as long as embedded files are in place.
713                 params.filename.enable(buffer().embedded(), &buffer(), false);
714                 setParams(params);
715         }
716 }
717
718
719 int InsetExternal::latex(odocstream & os, OutputParams const & runparams) const
720 {
721         if (params_.draft) {
722                 // FIXME UNICODE
723                 os << "\\fbox{\\ttfamily{}"
724                    << from_utf8(params_.filename.outputFilename(buffer().filePath()))
725                    << "}\n";
726                 return 1;
727         }
728
729         // "nice" means that the buffer is exported to LaTeX format but not
730         // run through the LaTeX compiler.
731         // If we're running through the LaTeX compiler, we should write the
732         // generated files in the buffer's temporary directory.
733         bool const external_in_tmpdir = !runparams.nice;
734         bool const dryrun = runparams.dryrun || runparams.inComment;
735
736         // If the template has specified a PDFLaTeX output, then we try and
737         // use that.
738         if (runparams.flavor == OutputParams::PDFLATEX) {
739                 external::Template const * const et_ptr =
740                         external::getTemplatePtr(params_);
741                 if (!et_ptr)
742                         return 0;
743                 external::Template const & et = *et_ptr;
744
745                 external::Template::Formats::const_iterator cit =
746                         et.formats.find("PDFLaTeX");
747
748                 if (cit != et.formats.end()) {
749                         return external::writeExternal(params_, "PDFLaTeX",
750                                                        buffer(), os,
751                                                        *(runparams.exportdata),
752                                                        external_in_tmpdir,
753                                                        dryrun);
754                 }
755         }
756
757         return external::writeExternal(params_, "LaTeX", buffer(), os,
758                                        *(runparams.exportdata),
759                                        external_in_tmpdir,
760                                        dryrun);
761 }
762
763
764 int InsetExternal::plaintext(odocstream & os,
765                              OutputParams const & runparams) const
766 {
767         os << '\n'; // output external material on a new line
768         external::writeExternal(params_, "Ascii", buffer(), os,
769                                 *(runparams.exportdata), false,
770                                 runparams.dryrun || runparams.inComment);
771         return PLAINTEXT_NEWLINE;
772 }
773
774
775 int InsetExternal::docbook(odocstream & os,
776                            OutputParams const & runparams) const
777 {
778         return external::writeExternal(params_, "DocBook", buffer(), os,
779                                        *(runparams.exportdata), false,
780                                        runparams.dryrun || runparams.inComment);
781 }
782
783
784 void InsetExternal::validate(LaTeXFeatures & features) const
785 {
786         if (params_.draft)
787                 return;
788
789         external::Template const * const et_ptr =
790                 external::getTemplatePtr(params_);
791         if (!et_ptr)
792                 return;
793         external::Template const & et = *et_ptr;
794
795         string format;
796         switch (features.runparams().flavor) {
797         case OutputParams::LATEX:
798                 format = "LaTeX";
799                 break;
800         case OutputParams::PDFLATEX:
801                 format = "PDFLaTeX";
802                 break;
803         case OutputParams::XML:
804                 format = "DocBook";
805                 break;
806         }
807         external::Template::Formats::const_iterator cit =
808                 et.formats.find(format);
809         if (cit == et.formats.end())
810                 return;
811
812         // FIXME: We don't need that always
813         features.require("lyxdot");
814
815         vector<string>::const_iterator it  = cit->second.requirements.begin();
816         vector<string>::const_iterator end = cit->second.requirements.end();
817         for (; it != end; ++it)
818                 features.require(*it);
819
820         external::TemplateManager & etm = external::TemplateManager::get();
821
822         it  = cit->second.preambleNames.begin();
823         end = cit->second.preambleNames.end();
824         for (; it != end; ++it) {
825                 string const preamble = etm.getPreambleDefByName(*it);
826                 if (!preamble.empty())
827                         features.addPreambleSnippet(preamble);
828         }
829 }
830
831
832 //
833 // preview stuff
834 //
835
836 void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const
837 {
838         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
839         if (!ptr)
840                 return;
841
842         if (isPreviewWanted(params())) {
843                 ptr->setAbsFile(params_.filename);
844                 docstring const snippet = latexString(*this);
845                 ptr->addPreview(snippet, ploader);
846         }
847 }
848
849
850 /// Mailer stuff
851
852 string const InsetExternalMailer::name_("external");
853
854 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
855         : inset_(inset)
856 {}
857
858
859 string const InsetExternalMailer::inset2string(Buffer const & buffer) const
860 {
861         return params2string(inset_.params(), buffer);
862 }
863
864
865 void InsetExternalMailer::string2params(string const & in,
866                                         Buffer const & buffer,
867                                         InsetExternalParams & params)
868 {
869         params = InsetExternalParams();
870         if (in.empty())
871                 return;
872
873         istringstream data(in);
874         Lexer lex(0,0);
875         lex.setStream(data);
876
877         string name;
878         lex >> name;
879         if (!lex || name != name_)
880                 return print_mailer_error("InsetExternalMailer", in, 1, name_);
881
882         // This is part of the inset proper that is usually swallowed
883         // by Text::readInset
884         string id;
885         lex >> id;
886         if (!lex || id != "External")
887                 return print_mailer_error("InsetBoxMailer", in, 2, "External");
888
889         params.read(buffer, lex);
890 }
891
892
893 string const
894 InsetExternalMailer::params2string(InsetExternalParams const & params,
895                                    Buffer const & buffer)
896 {
897         ostringstream data;
898         data << name_ << ' ';
899         params.write(buffer, data);
900         data << "\\end_inset\n";
901         return data.str();
902 }
903
904 } // namespace lyx