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