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