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