]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
This commit saves the need to check for lyx::use_gui in a number of places.
[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 namespace support = lyx::support;
48 namespace external = lyx::external;
49 namespace graphics = lyx::graphics;
50
51 using lyx::docstring;
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::use_gui)
566                 gparams.display = graphics::NoDisplay;
567
568         return gparams;
569 }
570
571
572 docstring 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 support::bformat((_("External template %1$s is not installed")),
580                                         lyx::from_utf8(params.templatename()));
581         // FIXME UNICODE
582         return lyx::from_utf8(external::doSubstitution(params, buffer,
583                                 ptr->guiName, false));
584 }
585
586 void add_preview_and_start_loading(RenderMonitoredPreview &,
587                                    InsetExternal const &,
588                                    Buffer const &);
589
590 } // namespace anon
591
592
593 InsetExternalParams const & InsetExternal::params() const
594 {
595         return params_;
596 }
597
598
599 void InsetExternal::setParams(InsetExternalParams const & p,
600                               Buffer const & buffer)
601 {
602         params_ = p;
603
604         // Subsequent calls to the InsetExternal::Params default constructor
605         // will use this.
606         defaultTemplateName = params_.templatename();
607
608         switch (getRenderType(params_)) {
609         case RENDERBUTTON: {
610                 RenderButton * button_ptr = renderer_->asButton();
611                 if (!button_ptr) {
612                         renderer_.reset(new RenderButton);
613                         button_ptr = renderer_->asButton();
614                 }
615
616                 button_ptr->update(getScreenLabel(params_, buffer), true);
617                 break;
618
619         } case RENDERGRAPHIC: {
620                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
621                 if (!graphic_ptr) {
622                         renderer_.reset(new RenderGraphic(this));
623                         graphic_ptr = renderer_->asGraphic();
624                 }
625
626                 graphic_ptr->update(get_grfx_params(params_));
627
628                 break;
629
630         } case RENDERPREVIEW: {
631                 RenderMonitoredPreview * preview_ptr =
632                         renderer_->asMonitoredPreview();
633                 if (!preview_ptr) {
634                         renderer_.reset(new RenderMonitoredPreview(this));
635                         preview_ptr = renderer_->asMonitoredPreview();
636                         preview_ptr->fileChanged(
637                                 boost::bind(&InsetExternal::fileChanged, this));
638                 }
639
640                 if (preview_ptr->monitoring())
641                         preview_ptr->stopMonitoring();
642                 add_preview_and_start_loading(*preview_ptr, *this, buffer);
643
644                 break;
645         }
646         }
647 }
648
649
650 void InsetExternal::fileChanged() const
651 {
652         Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
653         if (!buffer_ptr)
654                 return;
655
656         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
657         BOOST_ASSERT(ptr);
658
659         Buffer const & buffer = *buffer_ptr;
660         ptr->removePreview(buffer);
661         add_preview_and_start_loading(*ptr, *this, buffer);
662 }
663
664
665 void InsetExternal::write(Buffer const & buffer, ostream & os) const
666 {
667         params_.write(buffer, os);
668 }
669
670
671 void InsetExternal::read(Buffer const & buffer, LyXLex & lex)
672 {
673         InsetExternalParams params;
674         if (params.read(buffer, lex))
675                 setParams(params, buffer);
676 }
677
678
679 int InsetExternal::latex(Buffer const & buf, ostream & os,
680                          OutputParams const & runparams) const
681 {
682         if (params_.draft) {
683                 os << "\\fbox{\\ttfamily{}"
684                    << params_.filename.outputFilename(buf.filePath())
685                    << "}\n";
686                 return 1;
687         }
688
689         // "nice" means that the buffer is exported to LaTeX format but not
690         // run through the LaTeX compiler.
691         // If we're running through the LaTeX compiler, we should write the
692         // generated files in the bufer's temporary directory.
693         bool const external_in_tmpdir = !runparams.nice && !runparams.dryrun;
694
695         // If the template has specified a PDFLaTeX output, then we try and
696         // use that.
697         if (runparams.flavor == OutputParams::PDFLATEX) {
698                 external::Template const * const et_ptr =
699                         external::getTemplatePtr(params_);
700                 if (!et_ptr)
701                         return 0;
702                 external::Template const & et = *et_ptr;
703
704                 external::Template::Formats::const_iterator cit =
705                         et.formats.find("PDFLaTeX");
706                 if (cit != et.formats.end())
707                         return external::writeExternal(params_, "PDFLaTeX",
708                                                        buf, os,
709                                                        *(runparams.exportdata),
710                                                        external_in_tmpdir,
711                                                        runparams.inComment);
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, lyx::odocstream & os,
721                          OutputParams const & runparams) const
722 {
723         std::ostringstream oss;
724         int const retval = external::writeExternal(params_, "Ascii", buf, oss,
725                                        *(runparams.exportdata), false,
726                                        runparams.inComment);
727         // FIXME UNICODE
728         os << lyx::from_utf8(oss.str());
729         return retval;
730 }
731
732
733 int InsetExternal::docbook(Buffer const & buf, ostream & os,
734                            OutputParams const & runparams) const
735 {
736         return external::writeExternal(params_, "DocBook", buf, os,
737                                        *(runparams.exportdata), false,
738                                        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.addExternalPreamble(preamble);
786         }
787 }
788
789
790 //
791 // preview stuff
792 //
793
794 namespace {
795
796 bool preview_wanted(InsetExternalParams const & params)
797 {
798         string const included_file = params.filename.absFilename();
799
800         return params.display == external::PreviewDisplay &&
801                 support::isFileReadable(included_file);
802 }
803
804
805 string const latex_string(InsetExternal const & inset, Buffer const & buffer)
806 {
807         ostringstream os;
808         OutputParams runparams;
809         runparams.flavor = OutputParams::LATEX;
810         inset.latex(buffer, os, runparams);
811
812         return os.str();
813 }
814
815
816 void add_preview_and_start_loading(RenderMonitoredPreview & renderer,
817                                    InsetExternal const & inset,
818                                    Buffer const & buffer)
819 {
820         InsetExternalParams const & params = inset.params();
821
822         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
823             preview_wanted(params)) {
824                 renderer.setAbsFile(params.filename.absFilename());
825                 string const snippet = latex_string(inset, buffer);
826                 renderer.addPreview(snippet, buffer);
827                 renderer.startLoading(buffer);
828         }
829 }
830
831 } // namespace anon
832
833
834 void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const
835 {
836         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
837         if (!ptr)
838                 return;
839
840         if (preview_wanted(params())) {
841                 ptr->setAbsFile(params_.filename.absFilename());
842                 string const snippet = latex_string(*this, ploader.buffer());
843                 ptr->addPreview(snippet, ploader);
844         }
845 }
846
847
848 /// Mailer stuff
849
850 string const InsetExternalMailer::name_("external");
851
852 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
853         : inset_(inset)
854 {}
855
856
857 string const InsetExternalMailer::inset2string(Buffer const & buffer) const
858 {
859         return params2string(inset_.params(), buffer);
860 }
861
862
863 void InsetExternalMailer::string2params(string const & in,
864                                         Buffer const & buffer,
865                                         InsetExternalParams & params)
866 {
867         params = InsetExternalParams();
868         if (in.empty())
869                 return;
870
871         istringstream data(in);
872         LyXLex lex(0,0);
873         lex.setStream(data);
874
875         string name;
876         lex >> name;
877         if (!lex || name != name_)
878                 return print_mailer_error("InsetExternalMailer", in, 1, name_);
879
880         // This is part of the inset proper that is usually swallowed
881         // by LyXText::readInset
882         string id;
883         lex >> id;
884         if (!lex || id != "External")
885                 return print_mailer_error("InsetBoxMailer", in, 2, "External");
886
887         params.read(buffer, lex);
888 }
889
890
891 string const
892 InsetExternalMailer::params2string(InsetExternalParams const & params,
893                                    Buffer const & buffer)
894 {
895         ostringstream data;
896         data << name_ << ' ';
897         params.write(buffer, data);
898         data << "\\end_inset\n";
899         return data.str();
900 }