]> git.lyx.org Git - lyx.git/blob - src/insets/InsetExternal.cpp
Transfer tempName() implementation to FileName.
[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 "support/debug.h"
23 #include "DispatchResult.h"
24 #include "Exporter.h"
25 #include "FuncStatus.h"
26 #include "FuncRequest.h"
27 #include "support/gettext.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "LyXRC.h"
31 #include "MetricsInfo.h"
32 #include "OutputParams.h"
33
34 #include "graphics/PreviewLoader.h"
35
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
38 #include "support/lyxlib.h"
39 #include "support/convert.h"
40 #include "support/Translator.h"
41
42 #include <boost/bind.hpp>
43
44 #include <sstream>
45
46 using namespace std;
47 using namespace lyx::support;
48
49 namespace {
50
51 lyx::external::DisplayType const defaultDisplayType = lyx::external::NoDisplay;
52
53 unsigned int const defaultLyxScale = 100;
54
55 string defaultTemplateName;
56
57 } // namespace anon
58
59
60 namespace lyx {
61
62 extern bool use_gui;
63
64 namespace external {
65
66 TempName::TempName()
67 {
68         FileName const tempname = FileName::tempName(FileName(), "lyxext");
69         // FIXME: This is unsafe
70         tempname.removeFile();
71         // must have an extension for the converter code to work correctly.
72         tempname_ = FileName(tempname.absFilename() + ".tmp");
73 }
74
75
76 TempName::TempName(TempName const &)
77 {
78         tempname_ = TempName()();
79 }
80
81
82 TempName::~TempName()
83 {
84         tempname_.removeFile();
85 }
86
87
88 TempName & TempName::operator=(TempName const & other)
89 {
90         if (this != &other)
91                 tempname_ = TempName()();
92         return *this;
93 }
94
95
96 namespace {
97
98 /// The translator between the Display enum and corresponding lyx string.
99 Translator<DisplayType, string> const initTranslator()
100 {
101         Translator<DisplayType, string> translator(DefaultDisplay, "default");
102
103         // Fill the display translator
104         translator.addPair(MonochromeDisplay, "monochrome");
105         translator.addPair(GrayscaleDisplay, "grayscale");
106         translator.addPair(ColorDisplay, "color");
107         translator.addPair(PreviewDisplay, "preview");
108         translator.addPair(NoDisplay, "none");
109
110         return translator;
111 }
112
113 } // namespace anon
114
115
116 Translator<DisplayType, string> const & displayTranslator()
117 {
118         static Translator<DisplayType, string> const translator =
119                 initTranslator();
120         return translator;
121 }
122
123 } // namespace external
124
125
126 InsetExternalParams::InsetExternalParams()
127         : display(defaultDisplayType),
128           lyxscale(defaultLyxScale),
129           draft(false)
130 {
131         if (defaultTemplateName.empty()) {
132                 external::TemplateManager const & etm =
133                         external::TemplateManager::get();
134                 templatename_ = etm.getTemplates().begin()->first;
135         } else
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 = 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                 // when we save, we still use the original filename
187                 EmbeddedFiles::EmbeddedFileList::const_iterator it = 
188                         buffer.embeddedFiles().find(filename.toFilesystemEncoding());
189                 if (it != buffer.embeddedFiles().end())
190                         os << "\tfilename " << DocFileName(it->absFilename()).outputFilename(buffer.filePath()) << '\n';
191                 else
192                         os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
193         }
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                 double const scl = convert<double>(resizedata.scale);
228                 if (!float_equal(scl, 0.0, 0.05)) {
229                         if (!float_equal(scl, 100.0, 0.05))
230                                 os << "\tscale "
231                                    << resizedata.scale << '\n';
232                 } else {
233                         if (!resizedata.width.zero())
234                                 os << "\twidth "
235                                    << resizedata.width.asString() << '\n';
236                         if (!resizedata.height.zero())
237                                 os << "\theight "
238                                    << resizedata.height.asString() << '\n';
239                 }
240                 if (resizedata.keepAspectRatio)
241                         os << "\tkeepAspectRatio\n";
242         }
243 }
244
245
246 bool InsetExternalParams::read(Buffer const & buffer, Lexer & lex)
247 {
248         enum ExternalTags {
249                 EX_TEMPLATE = 1,
250                 EX_FILENAME,
251                 EX_DISPLAY,
252                 EX_LYXSCALE,
253                 EX_DRAFT,
254                 EX_BOUNDINGBOX,
255                 EX_CLIP,
256                 EX_EXTRA,
257                 EX_HEIGHT,
258                 EX_KEEPASPECTRATIO,
259                 EX_ROTATEANGLE,
260                 EX_ROTATEORIGIN,
261                 EX_SCALE,
262                 EX_WIDTH,
263                 EX_END
264         };
265
266         keyword_item external_tags[] = {
267                 { "\\end_inset",     EX_END },
268                 { "boundingBox",     EX_BOUNDINGBOX },
269                 { "clip",            EX_CLIP },
270                 { "display",         EX_DISPLAY},
271                 { "draft",           EX_DRAFT},
272                 { "extra",           EX_EXTRA },
273                 { "filename",        EX_FILENAME},
274                 { "height",          EX_HEIGHT },
275                 { "keepAspectRatio", EX_KEEPASPECTRATIO },
276                 { "lyxscale",        EX_LYXSCALE},
277                 { "rotateAngle",     EX_ROTATEANGLE },
278                 { "rotateOrigin",    EX_ROTATEORIGIN },
279                 { "scale",           EX_SCALE },
280                 { "template",        EX_TEMPLATE },
281                 { "width",           EX_WIDTH }
282         };
283
284         PushPopHelper pph(lex, external_tags, EX_END);
285
286         bool found_end  = false;
287         bool read_error = false;
288
289         while (lex.isOK()) {
290                 switch (lex.lex()) {
291                 case EX_TEMPLATE:
292                         lex.next();
293                         templatename_ = lex.getString();
294                         break;
295
296                 case EX_FILENAME: {
297                         lex.eatLine();
298                         string const name = lex.getString();
299                         filename.set(name, buffer.filePath());
300                         // maybe this file is embedded
301                         EmbeddedFiles::EmbeddedFileList::const_iterator it = buffer.embeddedFiles().find(filename.toFilesystemEncoding());
302                         if (it != buffer.embeddedFiles().end())
303                                 // using available file, embedded or external, depending on file availability and
304                                 // embedding status.
305                                 filename = DocFileName(it->availableFile(&buffer));
306                         break;
307                 }
308
309                 case EX_DISPLAY: {
310                         lex.next();
311                         string const name = lex.getString();
312                         display = external::displayTranslator().find(name);
313                         break;
314                 }
315
316                 case EX_LYXSCALE:
317                         lex.next();
318                         lyxscale = lex.getInteger();
319                         break;
320
321                 case EX_DRAFT:
322                         draft = true;
323                         break;
324
325                 case EX_BOUNDINGBOX:
326                         lex.next();
327                         clipdata.bbox.xl = lex.getInteger();
328                         lex.next();
329                         clipdata.bbox.yb = lex.getInteger();
330                         lex.next();
331                         clipdata.bbox.xr = lex.getInteger();
332                         lex.next();
333                         clipdata.bbox.yt = lex.getInteger();
334                         break;
335
336                 case EX_CLIP:
337                         clipdata.clip = true;
338                         break;
339
340                 case EX_EXTRA: {
341                         lex.next();
342                         string const name = lex.getString();
343                         lex.next();
344                         extradata.set(name, lex.getString());
345                         break;
346                 }
347
348                 case EX_HEIGHT:
349                         lex.next();
350                         resizedata.height = Length(lex.getString());
351                         break;
352
353                 case EX_KEEPASPECTRATIO:
354                         resizedata.keepAspectRatio = true;
355                         break;
356
357                 case EX_ROTATEANGLE:
358                         lex.next();
359                         rotationdata.angle = lex.getString();
360                         break;
361
362                 case EX_ROTATEORIGIN:
363                         lex.next();
364                         rotationdata.origin(lex.getString());
365                         break;
366
367                 case EX_SCALE:
368                         lex.next();
369                         resizedata.scale = lex.getString();
370                         break;
371
372                 case EX_WIDTH:
373                         lex.next();
374                         resizedata.width = Length(lex.getString());
375                         break;
376
377                 case EX_END:
378                         found_end = true;
379                         break;
380
381                 default:
382                         lex.printError("ExternalInset::read: Wrong tag: $$Token");
383                         read_error = true;
384                         break;
385                 }
386
387                 if (found_end || read_error)
388                         break;
389         }
390
391         if (!found_end)
392                 lex.printError("ExternalInsetParams::read: Missing \\end_inset.");
393
394         // This is a trick to make sure that the data are self-consistent.
395         settemplate(templatename_);
396
397         if (lyxerr.debugging(Debug::EXTERNAL)) {
398                 lyxerr  << "InsetExternalParams::read:\n";
399                 write(buffer, lyxerr);
400         }
401
402         return !read_error;
403 }
404
405
406 InsetExternal::InsetExternal()
407         : renderer_(new RenderButton)
408 {}
409
410
411 InsetExternal::InsetExternal(InsetExternal const & other)
412         : Inset(other),
413           boost::signals::trackable(),
414           params_(other.params_),
415           renderer_(other.renderer_->clone(this))
416 {}
417
418
419 Inset * InsetExternal::clone() const
420 {
421         return new InsetExternal(*this);
422 }
423
424
425 InsetExternal::~InsetExternal()
426 {
427         InsetExternalMailer(*this).hideDialog();
428 }
429
430
431 void InsetExternal::statusChanged() const
432 {
433         updateFrontend();
434 }
435
436
437 void InsetExternal::doDispatch(Cursor & cur, FuncRequest & cmd)
438 {
439         switch (cmd.action) {
440
441         case LFUN_EXTERNAL_EDIT: {
442                 Buffer const & buffer = cur.buffer();
443                 InsetExternalParams p;
444                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, p);
445                 external::editExternal(p, buffer);
446                 break;
447         }
448
449         case LFUN_INSET_MODIFY: {
450                 Buffer const & buffer = cur.buffer();
451                 InsetExternalParams p;
452                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, p);
453                 setParams(p, buffer);
454                 break;
455         }
456
457         case LFUN_INSET_DIALOG_UPDATE:
458                 InsetExternalMailer(*this).updateDialog(&cur.bv());
459                 break;
460
461         case LFUN_MOUSE_RELEASE:
462                 if (!cur.selection())
463                         InsetExternalMailer(*this).showDialog(&cur.bv());
464                 break;
465
466         default:
467                 Inset::doDispatch(cur, cmd);
468         }
469 }
470
471
472 bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
473                 FuncStatus & flag) const
474 {
475         switch (cmd.action) {
476
477         case LFUN_EXTERNAL_EDIT:
478         case LFUN_INSET_MODIFY:
479         case LFUN_INSET_DIALOG_UPDATE:
480                 flag.enabled(true);
481                 return true;
482
483         default:
484                 return Inset::getStatus(cur, cmd, flag);
485         }
486 }
487
488
489 void InsetExternal::registerEmbeddedFiles(Buffer const &,
490         EmbeddedFiles & files) const
491 {
492         files.registerFile(params_.filename.absFilename(), false, this);
493 }
494
495
496 void InsetExternal::edit(Cursor & cur, bool)
497 {
498         InsetExternalMailer(*this).showDialog(&cur.bv());
499 }
500
501
502 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
503 {
504         renderer_->metrics(mi, dim);
505 }
506
507
508 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
509 {
510         renderer_->draw(pi, x, y);
511 }
512
513
514 namespace {
515
516 enum RenderType {
517         RENDERBUTTON,
518         RENDERGRAPHIC,
519         RENDERPREVIEW
520 };
521
522
523 RenderType getRenderType(InsetExternalParams const & p)
524 {
525         if (!external::getTemplatePtr(p) ||
526             p.filename.empty() ||
527             p.display == external::NoDisplay)
528                 return RENDERBUTTON;
529
530         if (p.display == external::PreviewDisplay) {
531                 if (RenderPreview::status() != LyXRC::PREVIEW_OFF)
532                         return RENDERPREVIEW;
533                 return RENDERBUTTON;
534         }
535
536         if (p.display == external::DefaultDisplay &&
537             lyxrc.display_graphics == graphics::NoDisplay)
538                 return RENDERBUTTON;
539         return RENDERGRAPHIC;
540 }
541
542
543 graphics::Params get_grfx_params(InsetExternalParams const & eparams)
544 {
545         graphics::Params gparams;
546
547         gparams.filename = eparams.filename;
548         gparams.scale = eparams.lyxscale;
549         if (eparams.clipdata.clip)
550                 gparams.bb = eparams.clipdata.bbox;
551         gparams.angle = convert<double>(eparams.rotationdata.adjAngle());
552
553         switch (eparams.display) {
554         case external::DefaultDisplay:
555                 gparams.display = graphics::DefaultDisplay;
556                 break;
557         case external::MonochromeDisplay:
558                 gparams.display = graphics::MonochromeDisplay;
559                 break;
560         case external::GrayscaleDisplay:
561                 gparams.display = graphics::GrayscaleDisplay;
562                 break;
563         case external::ColorDisplay:
564                 gparams.display = graphics::ColorDisplay;
565                 break;
566         case external::NoDisplay:
567                 gparams.display = graphics::NoDisplay;
568                 break;
569         default:
570                 BOOST_ASSERT(false);
571         }
572         if (gparams.display == graphics::DefaultDisplay)
573                 gparams.display = graphics::DisplayType(lyxrc.display_graphics);
574         // Override the above if we're not using a gui
575         if (!use_gui)
576                 gparams.display = graphics::NoDisplay;
577
578         return gparams;
579 }
580
581
582 docstring const getScreenLabel(InsetExternalParams const & params,
583                             Buffer const & buffer)
584 {
585         external::Template const * const ptr =
586                 external::getTemplatePtr(params);
587         if (!ptr)
588                 // FIXME UNICODE
589                 return bformat((_("External template %1$s is not installed")),
590                                         from_utf8(params.templatename()));
591         // FIXME UNICODE
592         docstring gui = _(ptr->guiName);
593         return from_utf8(external::doSubstitution(params, buffer,
594                                 to_utf8(gui), false));
595 }
596
597 void add_preview_and_start_loading(RenderMonitoredPreview &,
598                                    InsetExternal const &,
599                                    Buffer const &);
600
601 } // namespace anon
602
603
604 InsetExternalParams const & InsetExternal::params() const
605 {
606         return params_;
607 }
608
609
610 void InsetExternal::setParams(InsetExternalParams const & p,
611                               Buffer const & buffer)
612 {
613         params_ = p;
614
615         // Subsequent calls to the InsetExternal::Params default constructor
616         // will use this.
617         defaultTemplateName = params_.templatename();
618
619         switch (getRenderType(params_)) {
620         case RENDERBUTTON: {
621                 RenderButton * button_ptr = renderer_->asButton();
622                 if (!button_ptr) {
623                         renderer_.reset(new RenderButton);
624                         button_ptr = renderer_->asButton();
625                 }
626
627                 button_ptr->update(getScreenLabel(params_, buffer), true);
628                 break;
629         }
630
631         case RENDERGRAPHIC: {
632                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
633                 if (!graphic_ptr) {
634                         renderer_.reset(new RenderGraphic(this));
635                         graphic_ptr = renderer_->asGraphic();
636                 }
637
638                 graphic_ptr->update(get_grfx_params(params_));
639
640                 break;
641         }
642
643         case RENDERPREVIEW: {
644                 RenderMonitoredPreview * preview_ptr =
645                         renderer_->asMonitoredPreview();
646                 if (!preview_ptr) {
647                         renderer_.reset(new RenderMonitoredPreview(this));
648                         preview_ptr = renderer_->asMonitoredPreview();
649                         preview_ptr->fileChanged(
650                                 boost::bind(&InsetExternal::fileChanged, this));
651                 }
652
653                 if (preview_ptr->monitoring())
654                         preview_ptr->stopMonitoring();
655                 add_preview_and_start_loading(*preview_ptr, *this, buffer);
656
657                 break;
658         }
659         }
660 }
661
662
663 void InsetExternal::fileChanged() const
664 {
665         Buffer const * const buffer = updateFrontend();
666         if (!buffer)
667                 return;
668
669         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
670         BOOST_ASSERT(ptr);
671
672         ptr->removePreview(*buffer);
673         add_preview_and_start_loading(*ptr, *this, *buffer);
674 }
675
676
677 void InsetExternal::write(Buffer const & buffer, ostream & os) const
678 {
679         params_.write(buffer, os);
680 }
681
682
683 void InsetExternal::read(Buffer const & buffer, Lexer & lex)
684 {
685         InsetExternalParams params;
686         if (params.read(buffer, lex))
687                 setParams(params, buffer);
688 }
689
690
691 int InsetExternal::latex(Buffer const & buf, odocstream & os,
692                          OutputParams const & runparams) const
693 {
694         if (params_.draft) {
695                 // FIXME UNICODE
696                 os << "\\fbox{\\ttfamily{}"
697                    << from_utf8(params_.filename.outputFilename(buf.filePath()))
698                    << "}\n";
699                 return 1;
700         }
701
702         // "nice" means that the buffer is exported to LaTeX format but not
703         // run through the LaTeX compiler.
704         // If we're running through the LaTeX compiler, we should write the
705         // generated files in the buffer's temporary directory.
706         bool const external_in_tmpdir = !runparams.nice;
707         bool const dryrun = runparams.dryrun || runparams.inComment;
708
709         // If the template has specified a PDFLaTeX output, then we try and
710         // use that.
711         if (runparams.flavor == OutputParams::PDFLATEX) {
712                 external::Template const * const et_ptr =
713                         external::getTemplatePtr(params_);
714                 if (!et_ptr)
715                         return 0;
716                 external::Template const & et = *et_ptr;
717
718                 external::Template::Formats::const_iterator cit =
719                         et.formats.find("PDFLaTeX");
720
721                 if (cit != et.formats.end()) {
722                         return external::writeExternal(params_, "PDFLaTeX",
723                                                        buf, os,
724                                                        *(runparams.exportdata),
725                                                        external_in_tmpdir,
726                                                        dryrun);
727                 }
728         }
729
730         return external::writeExternal(params_, "LaTeX", buf, os,
731                                        *(runparams.exportdata),
732                                        external_in_tmpdir,
733                                        dryrun);
734 }
735
736
737 int InsetExternal::plaintext(Buffer const & buf, odocstream & os,
738                              OutputParams const & runparams) const
739 {
740         os << '\n'; // output external material on a new line
741         external::writeExternal(params_, "Ascii", buf, os,
742                                 *(runparams.exportdata), false,
743                                 runparams.dryrun || runparams.inComment);
744         return PLAINTEXT_NEWLINE;
745 }
746
747
748 int InsetExternal::docbook(Buffer const & buf, odocstream & os,
749                            OutputParams const & runparams) const
750 {
751         return external::writeExternal(params_, "DocBook", buf, os,
752                                        *(runparams.exportdata), false,
753                                        runparams.dryrun || runparams.inComment);
754 }
755
756
757 void InsetExternal::validate(LaTeXFeatures & features) const
758 {
759         if (params_.draft)
760                 return;
761
762         external::Template const * const et_ptr =
763                 external::getTemplatePtr(params_);
764         if (!et_ptr)
765                 return;
766         external::Template const & et = *et_ptr;
767
768         string format;
769         switch (features.runparams().flavor) {
770         case OutputParams::LATEX:
771                 format = "LaTeX";
772                 break;
773         case OutputParams::PDFLATEX:
774                 format = "PDFLaTeX";
775                 break;
776         case OutputParams::XML:
777                 format = "DocBook";
778                 break;
779         }
780         external::Template::Formats::const_iterator cit =
781                 et.formats.find(format);
782         if (cit == et.formats.end())
783                 return;
784
785         // FIXME: We don't need that always
786         features.require("lyxdot");
787
788         vector<string>::const_iterator it  = cit->second.requirements.begin();
789         vector<string>::const_iterator end = cit->second.requirements.end();
790         for (; it != end; ++it)
791                 features.require(*it);
792
793         external::TemplateManager & etm = external::TemplateManager::get();
794
795         it  = cit->second.preambleNames.begin();
796         end = cit->second.preambleNames.end();
797         for (; it != end; ++it) {
798                 string const preamble = etm.getPreambleDefByName(*it);
799                 if (!preamble.empty())
800                         features.addPreambleSnippet(preamble);
801         }
802 }
803
804
805 //
806 // preview stuff
807 //
808
809 namespace {
810
811 bool preview_wanted(InsetExternalParams const & params)
812 {
813         return params.display == external::PreviewDisplay &&
814                 params.filename.isReadableFile();
815 }
816
817
818 docstring const latex_string(InsetExternal const & inset, Buffer const & buffer)
819 {
820         odocstringstream os;
821         // We don't need to set runparams.encoding since it is not used by
822         // latex().
823         OutputParams runparams(0);
824         runparams.flavor = OutputParams::LATEX;
825         inset.latex(buffer, os, runparams);
826         return os.str();
827 }
828
829
830 void add_preview_and_start_loading(RenderMonitoredPreview & renderer,
831                                    InsetExternal const & inset,
832                                    Buffer const & buffer)
833 {
834         InsetExternalParams const & params = inset.params();
835
836         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
837             preview_wanted(params)) {
838                 renderer.setAbsFile(params.filename);
839                 docstring const snippet = latex_string(inset, buffer);
840                 renderer.addPreview(snippet, buffer);
841                 renderer.startLoading(buffer);
842         }
843 }
844
845 } // namespace anon
846
847
848 void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const
849 {
850         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
851         if (!ptr)
852                 return;
853
854         if (preview_wanted(params())) {
855                 ptr->setAbsFile(params_.filename);
856                 docstring const snippet = latex_string(*this, ploader.buffer());
857                 ptr->addPreview(snippet, ploader);
858         }
859 }
860
861
862 /// Mailer stuff
863
864 string const InsetExternalMailer::name_("external");
865
866 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
867         : inset_(inset)
868 {}
869
870
871 string const InsetExternalMailer::inset2string(Buffer const & buffer) const
872 {
873         return params2string(inset_.params(), buffer);
874 }
875
876
877 void InsetExternalMailer::string2params(string const & in,
878                                         Buffer const & buffer,
879                                         InsetExternalParams & params)
880 {
881         params = InsetExternalParams();
882         if (in.empty())
883                 return;
884
885         istringstream data(in);
886         Lexer lex(0,0);
887         lex.setStream(data);
888
889         string name;
890         lex >> name;
891         if (!lex || name != name_)
892                 return print_mailer_error("InsetExternalMailer", in, 1, name_);
893
894         // This is part of the inset proper that is usually swallowed
895         // by Text::readInset
896         string id;
897         lex >> id;
898         if (!lex || id != "External")
899                 return print_mailer_error("InsetBoxMailer", in, 2, "External");
900
901         params.read(buffer, lex);
902 }
903
904
905 string const
906 InsetExternalMailer::params2string(InsetExternalParams const & params,
907                                    Buffer const & buffer)
908 {
909         ostringstream data;
910         data << name_ << ' ';
911         params.write(buffer, data);
912         data << "\\end_inset\n";
913         return data.str();
914 }
915
916 } // namespace lyx