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