]> git.lyx.org Git - lyx.git/blob - src/insets/InsetExternal.cpp
Whitespace cleanup
[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                 if (!cur.selection())
454                         InsetExternalMailer(*this).showDialog(&cur.bv());
455                 break;
456
457         default:
458                 Inset::doDispatch(cur, cmd);
459         }
460 }
461
462
463 bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
464                 FuncStatus & flag) const
465 {
466         switch (cmd.action) {
467
468         case LFUN_EXTERNAL_EDIT:
469         case LFUN_INSET_MODIFY:
470         case LFUN_INSET_DIALOG_UPDATE:
471                 flag.enabled(true);
472                 return true;
473
474         default:
475                 return Inset::getStatus(cur, cmd, flag);
476         }
477 }
478
479
480 void InsetExternal::edit(Cursor & cur, bool)
481 {
482         InsetExternalMailer(*this).showDialog(&cur.bv());
483 }
484
485
486 bool InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
487 {
488         renderer_->metrics(mi, dim);
489         bool const changed = dim_ != dim;
490         dim_ = dim;
491         return changed;
492 }
493
494
495 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
496 {
497         setPosCache(pi, x, y);
498         renderer_->draw(pi, x, y);
499 }
500
501
502 namespace {
503
504 enum RenderType {
505         RENDERBUTTON,
506         RENDERGRAPHIC,
507         RENDERPREVIEW
508 };
509
510
511 RenderType getRenderType(InsetExternalParams const & p)
512 {
513         if (!external::getTemplatePtr(p) ||
514             p.filename.empty() ||
515             p.display == external::NoDisplay)
516                 return RENDERBUTTON;
517
518         if (p.display == external::PreviewDisplay) {
519                 if (RenderPreview::status() != LyXRC::PREVIEW_OFF)
520                         return RENDERPREVIEW;
521                 return RENDERBUTTON;
522         }
523
524         if (p.display == external::DefaultDisplay &&
525             lyxrc.display_graphics == graphics::NoDisplay)
526                 return RENDERBUTTON;
527         return RENDERGRAPHIC;
528 }
529
530
531 graphics::Params get_grfx_params(InsetExternalParams const & eparams)
532 {
533         graphics::Params gparams;
534
535         gparams.filename = eparams.filename;
536         gparams.scale = eparams.lyxscale;
537         if (eparams.clipdata.clip)
538                 gparams.bb = eparams.clipdata.bbox;
539         gparams.angle = convert<double>(eparams.rotationdata.adjAngle());
540
541         switch (eparams.display) {
542         case external::DefaultDisplay:
543                 gparams.display = graphics::DefaultDisplay;
544                 break;
545         case external::MonochromeDisplay:
546                 gparams.display = graphics::MonochromeDisplay;
547                 break;
548         case external::GrayscaleDisplay:
549                 gparams.display = graphics::GrayscaleDisplay;
550                 break;
551         case external::ColorDisplay:
552                 gparams.display = graphics::ColorDisplay;
553                 break;
554         case external::NoDisplay:
555                 gparams.display = graphics::NoDisplay;
556                 break;
557         default:
558                 BOOST_ASSERT(false);
559         }
560         if (gparams.display == graphics::DefaultDisplay)
561                 gparams.display = lyxrc.display_graphics;
562         // Override the above if we're not using a gui
563         if (!use_gui)
564                 gparams.display = graphics::NoDisplay;
565
566         return gparams;
567 }
568
569
570 docstring const getScreenLabel(InsetExternalParams const & params,
571                             Buffer const & buffer)
572 {
573         external::Template const * const ptr =
574                 external::getTemplatePtr(params);
575         if (!ptr)
576                 // FIXME UNICODE
577                 return support::bformat((_("External template %1$s is not installed")),
578                                         from_utf8(params.templatename()));
579         // FIXME UNICODE
580         return from_utf8(external::doSubstitution(params, buffer,
581                                 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, Lexer & 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, odocstream & os,
678                          OutputParams const & runparams) const
679 {
680         if (params_.draft) {
681                 // FIXME UNICODE
682                 os << "\\fbox{\\ttfamily{}"
683                    << from_utf8(params_.filename.outputFilename(buf.filePath()))
684                    << "}\n";
685                 return 1;
686         }
687
688         // "nice" means that the buffer is exported to LaTeX format but not
689         // run through the LaTeX compiler.
690         // If we're running through the LaTeX compiler, we should write the
691         // generated files in the buffer's temporary directory.
692         bool const external_in_tmpdir = !runparams.nice;
693         bool const dryrun = runparams.dryrun || runparams.inComment;
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
707                 if (cit != et.formats.end()) {
708                         return external::writeExternal(params_, "PDFLaTeX",
709                                                        buf, os,
710                                                        *(runparams.exportdata),
711                                                        external_in_tmpdir,
712                                                        dryrun);
713                 }
714         }
715
716         return external::writeExternal(params_, "LaTeX", buf, os,
717                                        *(runparams.exportdata),
718                                        external_in_tmpdir,
719                                        dryrun);
720 }
721
722
723 int InsetExternal::plaintext(Buffer const & buf, odocstream & os,
724                              OutputParams const & runparams) const
725 {
726         os << '\n'; // output external material on a new line
727         external::writeExternal(params_, "Ascii", buf, os,
728                                 *(runparams.exportdata), false,
729                                 runparams.dryrun || runparams.inComment);
730         return PLAINTEXT_NEWLINE;
731 }
732
733
734 int InsetExternal::docbook(Buffer const & buf, odocstream & os,
735                            OutputParams const & runparams) const
736 {
737         return external::writeExternal(params_, "DocBook", buf, os,
738                                        *(runparams.exportdata), false,
739                                        runparams.dryrun || runparams.inComment);
740 }
741
742
743 void InsetExternal::validate(LaTeXFeatures & features) const
744 {
745         if (params_.draft)
746                 return;
747
748         external::Template const * const et_ptr =
749                 external::getTemplatePtr(params_);
750         if (!et_ptr)
751                 return;
752         external::Template const & et = *et_ptr;
753
754         string format;
755         switch (features.runparams().flavor) {
756         case OutputParams::LATEX:
757                 format = "LaTeX";
758                 break;
759         case OutputParams::PDFLATEX:
760                 format = "PDFLaTeX";
761                 break;
762         case OutputParams::XML:
763                 format = "DocBook";
764                 break;
765         }
766         external::Template::Formats::const_iterator cit =
767                 et.formats.find(format);
768         if (cit == et.formats.end())
769                 return;
770
771         // FIXME: We don't need that always
772         features.require("lyxdot");
773
774         vector<string>::const_iterator it  = cit->second.requirements.begin();
775         vector<string>::const_iterator end = cit->second.requirements.end();
776         for (; it != end; ++it)
777                 features.require(*it);
778
779         external::TemplateManager & etm = external::TemplateManager::get();
780
781         it  = cit->second.preambleNames.begin();
782         end = cit->second.preambleNames.end();
783         for (; it != end; ++it) {
784                 string const preamble = etm.getPreambleDefByName(*it);
785                 if (!preamble.empty())
786                         features.addPreambleSnippet(preamble);
787         }
788 }
789
790
791 //
792 // preview stuff
793 //
794
795 namespace {
796
797 bool preview_wanted(InsetExternalParams const & params)
798 {
799         return params.display == external::PreviewDisplay &&
800                 support::isFileReadable(params.filename);
801 }
802
803
804 docstring const latex_string(InsetExternal const & inset, Buffer const & buffer)
805 {
806         odocstringstream os;
807         // We don't need to set runparams.encoding since it is not used by
808         // latex().
809         OutputParams runparams(0);
810         runparams.flavor = OutputParams::LATEX;
811         inset.latex(buffer, os, runparams);
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);
825                 docstring 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);
842                 docstring 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         Lexer 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 Text::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 }
901
902 } // namespace lyx