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