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