]> git.lyx.org Git - lyx.git/blob - src/insets/InsetExternal.cpp
rename InsetBase to Inset
[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::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         support::FileName const tempname(support::tempName(support::FileName(), "lyxext"));
74         // FIXME: This is unsafe
75         support::unlink(tempname);
76         // must have an extension for the converter code to work correctly.
77         tempname_ = support::FileName(tempname.absFilename() + ".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, Lexer & 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.eatLine();
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 = Length(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 = Length(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         : Inset(other),
404           boost::signals::trackable(),
405           params_(other.params_),
406           renderer_(other.renderer_->clone(this))
407 {}
408
409
410 auto_ptr<Inset> InsetExternal::doClone() const
411 {
412         return auto_ptr<Inset>(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(Cursor & 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                 Inset::doDispatch(cur, cmd);
458         }
459 }
460
461
462 bool InsetExternal::getStatus(Cursor & 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 Inset::getStatus(cur, cmd, flag);
475         }
476 }
477
478
479 void InsetExternal::edit(Cursor & cur, bool)
480 {
481         InsetExternalMailer(*this).showDialog(&cur.bv());
482 }
483
484
485 bool InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
486 {
487         renderer_->metrics(mi, dim);
488         bool const changed = dim_ != dim;
489         dim_ = dim;
490         return changed;
491 }
492
493
494 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
495 {
496         setPosCache(pi, x, y);
497         renderer_->draw(pi, x, y);
498 }
499
500
501 namespace {
502
503 enum RenderType {
504         RENDERBUTTON,
505         RENDERGRAPHIC,
506         RENDERPREVIEW
507 };
508
509
510 RenderType getRenderType(InsetExternalParams const & p)
511 {
512         if (!external::getTemplatePtr(p) ||
513             p.filename.empty() ||
514             p.display == external::NoDisplay)
515                 return RENDERBUTTON;
516
517         if (p.display == external::PreviewDisplay) {
518                 if (RenderPreview::status() != LyXRC::PREVIEW_OFF)
519                         return RENDERPREVIEW;
520                 return RENDERBUTTON;
521         }
522
523         if (p.display == external::DefaultDisplay &&
524             lyxrc.display_graphics == graphics::NoDisplay)
525                 return RENDERBUTTON;
526         return RENDERGRAPHIC;
527 }
528
529
530 graphics::Params get_grfx_params(InsetExternalParams const & eparams)
531 {
532         graphics::Params gparams;
533
534         gparams.filename = eparams.filename;
535         gparams.scale = eparams.lyxscale;
536         if (eparams.clipdata.clip)
537                 gparams.bb = eparams.clipdata.bbox;
538         gparams.angle = convert<double>(eparams.rotationdata.adjAngle());
539
540         switch (eparams.display) {
541         case external::DefaultDisplay:
542                 gparams.display = graphics::DefaultDisplay;
543                 break;
544         case external::MonochromeDisplay:
545                 gparams.display = graphics::MonochromeDisplay;
546                 break;
547         case external::GrayscaleDisplay:
548                 gparams.display = graphics::GrayscaleDisplay;
549                 break;
550         case external::ColorDisplay:
551                 gparams.display = graphics::ColorDisplay;
552                 break;
553         case external::NoDisplay:
554                 gparams.display = graphics::NoDisplay;
555                 break;
556         default:
557                 BOOST_ASSERT(false);
558         }
559         if (gparams.display == graphics::DefaultDisplay)
560                 gparams.display = lyxrc.display_graphics;
561         // Override the above if we're not using a gui
562         if (!use_gui)
563                 gparams.display = graphics::NoDisplay;
564
565         return gparams;
566 }
567
568
569 docstring const getScreenLabel(InsetExternalParams const & params,
570                             Buffer const & buffer)
571 {
572         external::Template const * const ptr =
573                 external::getTemplatePtr(params);
574         if (!ptr)
575                 // FIXME UNICODE
576                 return support::bformat((_("External template %1$s is not installed")),
577                                         from_utf8(params.templatename()));
578         // FIXME UNICODE
579         return from_utf8(external::doSubstitution(params, buffer,
580                                 ptr->guiName, false));
581 }
582
583 void add_preview_and_start_loading(RenderMonitoredPreview &,
584                                    InsetExternal const &,
585                                    Buffer const &);
586
587 } // namespace anon
588
589
590 InsetExternalParams const & InsetExternal::params() const
591 {
592         return params_;
593 }
594
595
596 void InsetExternal::setParams(InsetExternalParams const & p,
597                               Buffer const & buffer)
598 {
599         params_ = p;
600
601         // Subsequent calls to the InsetExternal::Params default constructor
602         // will use this.
603         defaultTemplateName = params_.templatename();
604
605         switch (getRenderType(params_)) {
606         case RENDERBUTTON: {
607                 RenderButton * button_ptr = renderer_->asButton();
608                 if (!button_ptr) {
609                         renderer_.reset(new RenderButton);
610                         button_ptr = renderer_->asButton();
611                 }
612
613                 button_ptr->update(getScreenLabel(params_, buffer), true);
614                 break;
615
616         } case RENDERGRAPHIC: {
617                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
618                 if (!graphic_ptr) {
619                         renderer_.reset(new RenderGraphic(this));
620                         graphic_ptr = renderer_->asGraphic();
621                 }
622
623                 graphic_ptr->update(get_grfx_params(params_));
624
625                 break;
626
627         } case RENDERPREVIEW: {
628                 RenderMonitoredPreview * preview_ptr =
629                         renderer_->asMonitoredPreview();
630                 if (!preview_ptr) {
631                         renderer_.reset(new RenderMonitoredPreview(this));
632                         preview_ptr = renderer_->asMonitoredPreview();
633                         preview_ptr->fileChanged(
634                                 boost::bind(&InsetExternal::fileChanged, this));
635                 }
636
637                 if (preview_ptr->monitoring())
638                         preview_ptr->stopMonitoring();
639                 add_preview_and_start_loading(*preview_ptr, *this, buffer);
640
641                 break;
642         }
643         }
644 }
645
646
647 void InsetExternal::fileChanged() const
648 {
649         Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
650         if (!buffer_ptr)
651                 return;
652
653         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
654         BOOST_ASSERT(ptr);
655
656         Buffer const & buffer = *buffer_ptr;
657         ptr->removePreview(buffer);
658         add_preview_and_start_loading(*ptr, *this, buffer);
659 }
660
661
662 void InsetExternal::write(Buffer const & buffer, ostream & os) const
663 {
664         params_.write(buffer, os);
665 }
666
667
668 void InsetExternal::read(Buffer const & buffer, Lexer & lex)
669 {
670         InsetExternalParams params;
671         if (params.read(buffer, lex))
672                 setParams(params, buffer);
673 }
674
675
676 int InsetExternal::latex(Buffer const & buf, odocstream & os,
677                          OutputParams const & runparams) const
678 {
679         if (params_.draft) {
680                 // FIXME UNICODE
681                 os << "\\fbox{\\ttfamily{}"
682                    << from_utf8(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 buffer's temporary directory.
691         bool const external_in_tmpdir = !runparams.nice;
692         bool const dryrun = runparams.dryrun || runparams.inComment;
693
694         // If the template has specified a PDFLaTeX output, then we try and
695         // use that.
696         if (runparams.flavor == OutputParams::PDFLATEX) {
697                 external::Template const * const et_ptr =
698                         external::getTemplatePtr(params_);
699                 if (!et_ptr)
700                         return 0;
701                 external::Template const & et = *et_ptr;
702
703                 external::Template::Formats::const_iterator cit =
704                         et.formats.find("PDFLaTeX");
705
706                 if (cit != et.formats.end()) {
707                         return external::writeExternal(params_, "PDFLaTeX",
708                                                        buf, os,
709                                                        *(runparams.exportdata),
710                                                        external_in_tmpdir,
711                                                        dryrun);
712                 }
713         }
714
715         return external::writeExternal(params_, "LaTeX", buf, os,
716                                        *(runparams.exportdata),
717                                        external_in_tmpdir,
718                                        dryrun);
719 }
720
721
722 int InsetExternal::plaintext(Buffer const & buf, odocstream & os,
723                              OutputParams const & runparams) const
724 {
725         os << '\n'; // output external material on a new line
726         external::writeExternal(params_, "Ascii", buf, os,
727                                 *(runparams.exportdata), false,
728                                 runparams.dryrun || runparams.inComment);
729         return PLAINTEXT_NEWLINE;
730 }
731
732
733 int InsetExternal::docbook(Buffer const & buf, odocstream & os,
734                            OutputParams const & runparams) const
735 {
736         return external::writeExternal(params_, "DocBook", buf, os,
737                                        *(runparams.exportdata), false,
738                                        runparams.dryrun || runparams.inComment);
739 }
740
741
742 void InsetExternal::validate(LaTeXFeatures & features) const
743 {
744         if (params_.draft)
745                 return;
746
747         external::Template const * const et_ptr =
748                 external::getTemplatePtr(params_);
749         if (!et_ptr)
750                 return;
751         external::Template const & et = *et_ptr;
752
753         string format;
754         switch (features.runparams().flavor) {
755         case OutputParams::LATEX:
756                 format = "LaTeX";
757                 break;
758         case OutputParams::PDFLATEX:
759                 format = "PDFLaTeX";
760                 break;
761         case OutputParams::XML:
762                 format = "DocBook";
763                 break;
764         }
765         external::Template::Formats::const_iterator cit =
766                 et.formats.find(format);
767         if (cit == et.formats.end())
768                 return;
769
770         // FIXME: We don't need that always
771         features.require("lyxdot");
772
773         vector<string>::const_iterator it  = cit->second.requirements.begin();
774         vector<string>::const_iterator end = cit->second.requirements.end();
775         for (; it != end; ++it)
776                 features.require(*it);
777
778         external::TemplateManager & etm = external::TemplateManager::get();
779
780         it  = cit->second.preambleNames.begin();
781         end = cit->second.preambleNames.end();
782         for (; it != end; ++it) {
783                 string const preamble = etm.getPreambleDefByName(*it);
784                 if (!preamble.empty())
785                         features.addPreambleSnippet(preamble);
786         }
787 }
788
789
790 //
791 // preview stuff
792 //
793
794 namespace {
795
796 bool preview_wanted(InsetExternalParams const & params)
797 {
798         return params.display == external::PreviewDisplay &&
799                 support::isFileReadable(params.filename);
800 }
801
802
803 docstring const latex_string(InsetExternal const & inset, Buffer const & buffer)
804 {
805         odocstringstream os;
806         // We don't need to set runparams.encoding since it is not used by
807         // latex().
808         OutputParams runparams(0);
809         runparams.flavor = OutputParams::LATEX;
810         inset.latex(buffer, os, runparams);
811         return os.str();
812 }
813
814
815 void add_preview_and_start_loading(RenderMonitoredPreview & renderer,
816                                    InsetExternal const & inset,
817                                    Buffer const & buffer)
818 {
819         InsetExternalParams const & params = inset.params();
820
821         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
822             preview_wanted(params)) {
823                 renderer.setAbsFile(params.filename);
824                 docstring const snippet = latex_string(inset, buffer);
825                 renderer.addPreview(snippet, buffer);
826                 renderer.startLoading(buffer);
827         }
828 }
829
830 } // namespace anon
831
832
833 void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const
834 {
835         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
836         if (!ptr)
837                 return;
838
839         if (preview_wanted(params())) {
840                 ptr->setAbsFile(params_.filename);
841                 docstring const snippet = latex_string(*this, ploader.buffer());
842                 ptr->addPreview(snippet, ploader);
843         }
844 }
845
846
847 /// Mailer stuff
848
849 string const InsetExternalMailer::name_("external");
850
851 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
852         : inset_(inset)
853 {}
854
855
856 string const InsetExternalMailer::inset2string(Buffer const & buffer) const
857 {
858         return params2string(inset_.params(), buffer);
859 }
860
861
862 void InsetExternalMailer::string2params(string const & in,
863                                         Buffer const & buffer,
864                                         InsetExternalParams & params)
865 {
866         params = InsetExternalParams();
867         if (in.empty())
868                 return;
869
870         istringstream data(in);
871         Lexer lex(0,0);
872         lex.setStream(data);
873
874         string name;
875         lex >> name;
876         if (!lex || name != name_)
877                 return print_mailer_error("InsetExternalMailer", in, 1, name_);
878
879         // This is part of the inset proper that is usually swallowed
880         // by LyXText::readInset
881         string id;
882         lex >> id;
883         if (!lex || id != "External")
884                 return print_mailer_error("InsetBoxMailer", in, 2, "External");
885
886         params.read(buffer, lex);
887 }
888
889
890 string const
891 InsetExternalMailer::params2string(InsetExternalParams const & params,
892                                    Buffer const & buffer)
893 {
894         ostringstream data;
895         data << name_ << ' ';
896         params.write(buffer, data);
897         data << "\\end_inset\n";
898         return data.str();
899 }
900
901 } // namespace lyx