]> git.lyx.org Git - features.git/blob - src/insets/InsetExternal.cpp
False positive on coverity #23366.
[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/RenderButton.h"
16 #include "insets/RenderGraphic.h"
17 #include "insets/RenderPreview.h"
18
19 #include "Buffer.h"
20 #include "BufferView.h"
21 #include "Cursor.h"
22 #include "DispatchResult.h"
23 #include "Exporter.h"
24 #include "FuncStatus.h"
25 #include "FuncRequest.h"
26 #include "LaTeXFeatures.h"
27 #include "Lexer.h"
28 #include "LyX.h" // use_gui
29 #include "LyXRC.h"
30 #include "MetricsInfo.h"
31 #include "OutputParams.h"
32 #include "output_latex.h"
33 #include "TocBackend.h"
34
35 #include "frontends/alert.h"
36 #include "frontends/Application.h"
37
38 #include "graphics/PreviewLoader.h"
39
40 #include "support/bind.h"
41 #include "support/convert.h"
42 #include "support/debug.h"
43 #include "support/ExceptionMessage.h"
44 #include "support/filetools.h"
45 #include "support/gettext.h"
46 #include "support/lassert.h"
47 #include "support/lstrings.h"
48 #include "support/lyxlib.h"
49 #include "support/TempFile.h"
50
51 #include <sstream>
52 #include <vector>
53
54 using namespace std;
55 using namespace lyx::support;
56
57 namespace {
58
59 unsigned int const defaultLyxScale = 100;
60
61 string defaultTemplateName;
62
63 } // namespace anon
64
65
66 namespace lyx {
67
68 namespace Alert = frontend::Alert;
69
70 namespace external {
71
72 TempName::TempName()
73 {
74         // must have an extension for the converter code to work correctly.
75         support::TempFile f("lyxextXXXXXX.tmp");
76         // Let f go out of scope here and delete the file ourselves in
77         // ~TempName(), since otherwise external processes would not be able
78         // to use the file on windows (bug 9925). This is not as safe as
79         // keeping a support::TempFile member would be, but the best we can do.
80         f.setAutoRemove(false);
81         tempname_ = f.name();
82 }
83
84
85 TempName::TempName(TempName const & that)
86 {
87         *this = that;
88 }
89
90
91 TempName::~TempName()
92 {
93         tempname_.removeFile();
94 }
95
96
97 TempName & TempName::operator=(TempName const & other)
98 {
99         if (this != &other) {
100                 tempname_.removeFile();
101                 support::TempFile f("lyxextXXXXXX.tmp");
102                 f.setAutoRemove(false);
103                 tempname_ = f.name(); 
104         }
105         return *this;
106 }
107
108
109 support::FileName TempName::operator()() const
110 {
111         return tempname_;
112 }
113
114 } // namespace external
115
116
117 InsetExternalParams::InsetExternalParams()
118         : display(true),
119           preview_mode(PREVIEW_OFF),
120           lyxscale(defaultLyxScale),
121           draft(false)
122 {
123         if (defaultTemplateName.empty()) {
124                 external::TemplateManager const & etm =
125                         external::TemplateManager::get();
126                 if (!etm.getTemplates().empty())
127                         templatename_ = etm.getTemplates().begin()->first;
128         } else
129                 templatename_ = defaultTemplateName;
130 }
131
132
133 namespace {
134
135 template <typename T>
136 void clearIfNotFound(T & data, external::TransformID value,
137                      vector<external::TransformID> const & ids)
138 {
139         typedef vector<external::TransformID>::const_iterator
140                 const_iterator;
141
142         const_iterator it  = ids.begin();
143         const_iterator end = ids.end();
144         it = find(it, end, value);
145         if (it == end)
146                 data = T();
147 }
148
149 } // namespace anon
150
151
152 void InsetExternalParams::settemplate(string const & name)
153 {
154         templatename_ = name;
155
156         external::TemplateManager const & etm =
157                 external::TemplateManager::get();
158         external::Template const * const et = etm.getTemplateByName(name);
159         if (!et)
160                 // Be safe. Don't lose data.
161                 return;
162
163         // Ascertain which transforms the template supports.
164         // Empty all those that it doesn't.
165         vector<external::TransformID> const & ids = et->transformIds;
166         clearIfNotFound(clipdata,     external::Clip,   ids);
167         clearIfNotFound(extradata,    external::Extra,  ids);
168         clearIfNotFound(resizedata,   external::Resize, ids);
169         clearIfNotFound(rotationdata, external::Rotate, ids);
170
171         // 
172         preview_mode = et->preview_mode;
173 }
174
175
176 void InsetExternalParams::write(Buffer const & buf, ostream & os) const
177 {
178         os << "External\n"
179            << "\ttemplate " << templatename() << '\n';
180
181         if (!filename.empty())
182                 os << "\tfilename " << filename.outputFileName(buf.filePath()) << '\n';
183
184         if (!display)
185                 os << "\tdisplay false\n";
186
187         if (lyxscale != defaultLyxScale)
188                 os << "\tlyxscale " << convert<string>(lyxscale) << '\n';
189
190         if (draft)
191                 os << "\tdraft\n";
192
193         if (!clipdata.bbox.empty())
194                 os << "\tboundingBox " << clipdata.bbox << '\n';
195         if (clipdata.clip)
196                 os << "\tclip\n";
197
198         external::ExtraData::const_iterator it  = extradata.begin();
199         external::ExtraData::const_iterator end = extradata.end();
200         for (; it != end; ++it) {
201                 if (!it->second.empty())
202                         os << "\textra " << it->first << " \""
203                            << it->second << "\"\n";
204         }
205
206         if (!rotationdata.no_rotation()) {
207                 os << "\trotateAngle " << rotationdata.adjAngle() << '\n';
208                 if (rotationdata.origin() != external::RotationData::DEFAULT)
209                         os << "\trotateOrigin "
210                            << rotationdata.originString() << '\n';
211         }
212
213         if (!resizedata.no_resize()) {
214                 double const scl = convert<double>(resizedata.scale);
215                 if (!float_equal(scl, 0.0, 0.05)) {
216                         if (!float_equal(scl, 100.0, 0.05))
217                                 os << "\tscale "
218                                    << resizedata.scale << '\n';
219                 } else {
220                         if (!resizedata.width.zero())
221                                 os << "\twidth "
222                                    << resizedata.width.asString() << '\n';
223                         if (!resizedata.height.zero())
224                                 os << "\theight "
225                                    << resizedata.height.asString() << '\n';
226                 }
227                 if (resizedata.keepAspectRatio)
228                         os << "\tkeepAspectRatio\n";
229         }
230 }
231
232
233 bool InsetExternalParams::read(Buffer const & buffer, Lexer & lex)
234 {
235         enum {
236                 EX_TEMPLATE = 1,
237                 EX_FILENAME,
238                 EX_DISPLAY,
239                 EX_LYXSCALE,
240                 EX_DRAFT,
241                 EX_BOUNDINGBOX,
242                 EX_CLIP,
243                 EX_EXTRA,
244                 EX_HEIGHT,
245                 EX_KEEPASPECTRATIO,
246                 EX_ROTATEANGLE,
247                 EX_ROTATEORIGIN,
248                 EX_SCALE,
249                 EX_WIDTH,
250                 EX_END
251         };
252
253         LexerKeyword external_tags[] = {
254                 { "\\end_inset",     EX_END },
255                 { "boundingBox",     EX_BOUNDINGBOX },
256                 { "clip",            EX_CLIP },
257                 { "display",         EX_DISPLAY},
258                 { "draft",           EX_DRAFT},
259                 { "extra",           EX_EXTRA },
260                 { "filename",        EX_FILENAME},
261                 { "height",          EX_HEIGHT },
262                 { "keepAspectRatio", EX_KEEPASPECTRATIO },
263                 { "lyxscale",        EX_LYXSCALE},
264                 { "rotateAngle",     EX_ROTATEANGLE },
265                 { "rotateOrigin",    EX_ROTATEORIGIN },
266                 { "scale",           EX_SCALE },
267                 { "template",        EX_TEMPLATE },
268                 { "width",           EX_WIDTH }
269         };
270
271         PushPopHelper pph(lex, external_tags);
272
273         bool found_end  = false;
274         bool read_error = false;
275
276         while (lex.isOK()) {
277                 switch (lex.lex()) {
278                 case EX_TEMPLATE:
279                         lex.next();
280                         templatename_ = lex.getString();
281                         break;
282
283                 case EX_FILENAME: {
284                         lex.eatLine();
285                         string const name = lex.getString();
286                         filename = buffer.getReferencedFileName(name);
287                         break;
288                 }
289                 
290                 case EX_DISPLAY: {
291                         lex.next();
292                         display = lex.getString() != "false";
293                         break;
294                 }
295
296                 case EX_LYXSCALE:
297                         lex.next();
298                         lyxscale = lex.getInteger();
299                         break;
300
301                 case EX_DRAFT:
302                         draft = true;
303                         break;
304
305                 case EX_BOUNDINGBOX:
306                         lex.next();
307                         clipdata.bbox.xl = Length(lex.getString());
308                         lex.next();
309                         clipdata.bbox.yb = Length(lex.getString());
310                         lex.next();
311                         clipdata.bbox.xr = Length(lex.getString());
312                         lex.next();
313                         clipdata.bbox.yt = Length(lex.getString());
314                         break;
315
316                 case EX_CLIP:
317                         clipdata.clip = true;
318                         break;
319
320                 case EX_EXTRA: {
321                         lex.next();
322                         string const name = lex.getString();
323                         lex.next();
324                         extradata.set(name, lex.getString());
325                         break;
326                 }
327
328                 case EX_HEIGHT:
329                         lex.next();
330                         resizedata.height = Length(lex.getString());
331                         break;
332
333                 case EX_KEEPASPECTRATIO:
334                         resizedata.keepAspectRatio = true;
335                         break;
336
337                 case EX_ROTATEANGLE:
338                         lex.next();
339                         rotationdata.angle = lex.getString();
340                         break;
341
342                 case EX_ROTATEORIGIN:
343                         lex.next();
344                         rotationdata.origin(lex.getString());
345                         break;
346
347                 case EX_SCALE:
348                         lex.next();
349                         resizedata.scale = lex.getString();
350                         break;
351
352                 case EX_WIDTH:
353                         lex.next();
354                         resizedata.width = Length(lex.getString());
355                         break;
356
357                 case EX_END:
358                         found_end = true;
359                         break;
360
361                 default:
362                         lex.printError("ExternalInset::read: Wrong tag: $$Token");
363                         read_error = true;
364                         break;
365                 }
366
367                 if (found_end || read_error)
368                         break;
369         }
370
371         if (!found_end)
372                 lex.printError("ExternalInsetParams::read: Missing \\end_inset.");
373
374         // This is a trick to make sure that the data are self-consistent.
375         settemplate(templatename_);
376
377         if (lyxerr.debugging(Debug::EXTERNAL)) {
378                 lyxerr  << "InsetExternalParams::read:\n";
379                 // false positive
380                 // coverity[NEGATIVE_RETURNS]
381                 write(buffer, lyxerr);
382         }
383
384         return !read_error;
385 }
386
387
388 namespace {
389
390 docstring screenLabel(InsetExternalParams const & params,
391                             Buffer const & buffer)
392 {
393         external::Template const * const ptr =
394                 external::getTemplatePtr(params);
395         if (!ptr)
396                 // FIXME UNICODE
397                 return bformat((_("External template %1$s is not installed")),
398                                         from_utf8(params.templatename()));
399         // FIXME UNICODE
400         docstring gui = _(ptr->guiName);
401         gui += ": ";
402
403         if (params.filename.empty())
404                 gui += "???";
405         else
406                 gui += from_utf8(params.filename.relFileName(buffer.filePath()));
407
408         return gui;
409 }
410
411 } // namespace anon
412
413
414
415 InsetExternal::InsetExternal(Buffer * buf)
416         : Inset(buf), renderer_(new RenderButton)
417 {
418 }
419
420
421 // Mouse hover is not copied and remains empty
422 InsetExternal::InsetExternal(InsetExternal const & other)
423         : Inset(other),
424           boost::signals2::trackable(),
425           params_(other.params_),
426           renderer_(other.renderer_->clone(this))
427 {}
428
429
430 InsetExternal::~InsetExternal()
431 {
432         hideDialogs("external", this);
433
434         map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
435         map<BufferView const *, bool>::iterator end = mouse_hover_.end();
436         for (; it != end; ++it)
437                 if (it->second)
438                         it->first->clearLastInset(this);
439 }
440
441
442 bool InsetExternal::setMouseHover(BufferView const * bv, bool mouse_hover) const
443 {
444         mouse_hover_[bv] = mouse_hover;
445         return true;
446 }
447
448
449 void InsetExternal::statusChanged() const
450 {
451         updateFrontend();
452 }
453
454
455 void InsetExternal::doDispatch(Cursor & cur, FuncRequest & cmd)
456 {
457         switch (cmd.action()) {
458
459         case LFUN_INSET_EDIT: {
460                 InsetExternalParams p =  params();
461                 if (!cmd.argument().empty())
462                         string2params(to_utf8(cmd.argument()), buffer(), p);
463                 external::editExternal(p, buffer());
464                 break;
465         }
466
467         case LFUN_INSET_MODIFY: {
468                 InsetExternalParams p;
469                 string2params(to_utf8(cmd.argument()), buffer(), p);
470                 cur.recordUndo();
471                 setParams(p);
472                 break;
473         }
474
475         case LFUN_INSET_DIALOG_UPDATE:
476                 cur.bv().updateDialog("external",
477                         params2string(params(), cur.bv().buffer()));
478                 break;
479
480         default:
481                 Inset::doDispatch(cur, cmd);
482         }
483 }
484
485
486 bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
487                 FuncStatus & flag) const
488 {
489         switch (cmd.action()) {
490
491         case LFUN_INSET_EDIT:
492         case LFUN_INSET_MODIFY:
493         case LFUN_INSET_DIALOG_UPDATE:
494                 flag.setEnabled(true);
495                 return true;
496
497         default:
498                 return Inset::getStatus(cur, cmd, flag);
499         }
500 }
501
502
503 void InsetExternal::addToToc(DocIterator const & cpit, bool output_active,
504                                                          UpdateType) const
505 {
506         DocIterator pit = cpit;
507         pit.push_back(CursorSlice(const_cast<InsetExternal &>(*this)));
508         shared_ptr<Toc> toc = buffer().tocBackend().toc("external");
509         docstring str = screenLabel(params_, buffer());
510         toc->push_back(TocItem(pit, 0, str, output_active));
511 }
512
513
514 bool InsetExternal::showInsetDialog(BufferView * bv) const
515 {
516         bv->showDialog("external", params2string(params(), bv->buffer()),
517                 const_cast<InsetExternal *>(this));
518         return true;
519 }
520
521
522 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
523 {
524         renderer_->metrics(mi, dim);
525 }
526
527
528 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
529 {
530         if (renderer_->asButton())
531                 renderer_->setRenderState(mouse_hover_[pi.base.bv]);
532         renderer_->draw(pi, x, y);
533 }
534
535
536 namespace {
537
538 graphics::Params get_grfx_params(InsetExternalParams const & eparams)
539 {
540         graphics::Params gparams;
541
542         gparams.filename = eparams.filename;
543         gparams.scale = eparams.lyxscale;
544         if (eparams.clipdata.clip)
545                 gparams.bb = eparams.clipdata.bbox;
546         gparams.angle = convert<double>(eparams.rotationdata.adjAngle());
547         gparams.display = eparams.display;
548
549         return gparams;
550 }
551
552 } // namespace anon
553
554
555 static bool isPreviewWanted(InsetExternalParams const & params)
556 {
557         return params.display && params.filename.isReadableFile();
558 }
559
560
561 static docstring latexString(InsetExternal const & inset)
562 {
563         TexRow texrow;
564         odocstringstream ods;
565         otexstream os(ods, texrow);
566         // We don't need to set runparams.encoding since it is not used by
567         // latex().
568         OutputParams runparams(0);
569         runparams.flavor = OutputParams::LATEX;
570         inset.latex(os, runparams);
571         return ods.str();
572 }
573
574
575 static void add_preview_and_start_loading(RenderMonitoredPreview & renderer,
576                                    InsetExternal const & inset,
577                                    Buffer const & buffer)
578 {
579         InsetExternalParams const & params = inset.params();
580
581         if (RenderPreview::previewText() && isPreviewWanted(params)) {
582                 renderer.setAbsFile(params.filename);
583                 docstring const snippet = latexString(inset);
584                 renderer.addPreview(snippet, buffer);
585                 renderer.startLoading(buffer);
586         }
587 }
588
589
590 InsetExternalParams const & InsetExternal::params() const
591 {
592         return params_;
593 }
594
595
596 void InsetExternal::updatePreview()
597 {
598         setParams(params_);
599 }
600
601
602 void InsetExternal::setParams(InsetExternalParams const & p)
603 {
604         params_ = p;
605
606         // Subsequent calls to the InsetExternal::Params default constructor
607         // will use this.
608         defaultTemplateName = params_.templatename();
609
610         if (!external::getTemplatePtr(params_) || params_.filename.empty()
611                 || !params_.display
612                 || !lyxrc.display_graphics
613                 || params_.preview_mode == PREVIEW_OFF
614                 || (params_.preview_mode == PREVIEW_INSTANT
615                     && !RenderPreview::previewText())) {
616                 RenderButton * button_ptr = renderer_->asButton();
617                 if (!button_ptr) {
618                         renderer_.reset(new RenderButton);
619                         button_ptr = renderer_->asButton();
620                 }
621                 button_ptr->update(screenLabel(params_, buffer()), true);
622                 return;
623         }
624
625         switch (params_.preview_mode) {
626         case PREVIEW_OFF:
627                 // Already taken care of above.
628                 LASSERT(false, return);
629                 break;
630         case PREVIEW_INSTANT: {
631                 renderer_.reset(new RenderMonitoredPreview(this));
632                 RenderMonitoredPreview * preview_ptr = renderer_->asMonitoredPreview();
633                 preview_ptr->fileChanged(bind(&InsetExternal::fileChanged, this));
634                 if (preview_ptr->monitoring())
635                         preview_ptr->stopMonitoring();
636                 add_preview_and_start_loading(*preview_ptr, *this, buffer());
637                 break;
638         } 
639         case PREVIEW_GRAPHICS: {
640                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
641                 if (!graphic_ptr) {
642                         renderer_.reset(new RenderGraphic(this));
643                         graphic_ptr = renderer_->asGraphic();
644                 }
645                 graphic_ptr->update(get_grfx_params(params_));
646                 break;
647         }
648         }
649 }
650
651
652 void InsetExternal::fileChanged() const
653 {
654         Buffer const * const buffer = updateFrontend();
655         if (!buffer)
656                 return;
657
658         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
659         LASSERT(ptr, return);
660
661         ptr->removePreview(*buffer);
662         add_preview_and_start_loading(*ptr, *this, *buffer);
663 }
664
665
666 void InsetExternal::write(ostream & os) const
667 {
668         params_.write(buffer(), os);
669 }
670
671
672 void InsetExternal::read(Lexer & lex)
673 {
674         InsetExternalParams params;
675         if (params.read(buffer(), lex))
676                 setParams(params);
677 }
678
679
680 void InsetExternal::latex(otexstream & os, OutputParams const & runparams) const
681 {
682         if (params_.draft) {
683                 // FIXME UNICODE
684                 os << "\\fbox{\\ttfamily{}"
685                    << from_utf8(params_.filename.outputFileName(buffer().filePath()))
686                    << "}\n";
687                 return;
688         }
689
690         // "nice" means that the buffer is exported to LaTeX format but not
691         // run through the LaTeX compiler.
692         // If we're running through the LaTeX compiler, we should write the
693         // generated files in the buffer's temporary directory.
694         bool const external_in_tmpdir = !runparams.nice;
695         bool const dryrun = runparams.dryrun || runparams.inComment;
696
697         // If the template has specified a PDFLaTeX output, then we try and
698         // use that.
699         if (runparams.flavor == OutputParams::PDFLATEX) {
700                 external::Template const * const et_ptr =
701                         external::getTemplatePtr(params_);
702                 if (!et_ptr)
703                         return;
704                 external::Template const & et = *et_ptr;
705
706                 external::Template::Formats::const_iterator cit =
707                         et.formats.find("PDFLaTeX");
708
709                 if (cit != et.formats.end()) {
710                         external::writeExternal(params_, "PDFLaTeX",
711                                                 buffer(), os,
712                                                 *(runparams.exportdata),
713                                                 external_in_tmpdir,
714                                                 dryrun);
715                         return;
716                 }
717         }
718
719         external::writeExternal(params_, "LaTeX", buffer(), os,
720                                 *(runparams.exportdata),
721                                 external_in_tmpdir,
722                                 dryrun);
723 }
724
725
726 int InsetExternal::plaintext(odocstringstream & os,
727                              OutputParams const & runparams, size_t) const
728 {
729         // this is too slow for constant use
730         if (runparams.for_tooltip)
731                 return 0;
732
733         TexRow texrow;
734         otexstream ots(os, texrow);
735         ots << '\n'; // output external material on a new line
736         external::writeExternal(params_, "Ascii", buffer(), ots,
737                                 *(runparams.exportdata), false,
738                                 runparams.dryrun || runparams.inComment);
739         return PLAINTEXT_NEWLINE;
740 }
741
742
743 int InsetExternal::docbook(odocstream & os,
744                            OutputParams const & runparams) const
745 {
746         TexRow texrow;
747         odocstringstream ods;
748         otexstream ots(ods, texrow);
749         external::writeExternal(params_, "DocBook", buffer(), ots,
750                                 *(runparams.exportdata), false,
751                                 runparams.dryrun || runparams.inComment);
752         os << ods.str();
753         return int(count(ods.str().begin(), ods.str().end(), '\n'));
754 }
755
756
757 docstring InsetExternal::xhtml(XHTMLStream  & /*xs*/,
758                         OutputParams const & /*rp*/) const
759 {
760 //      external::writeExternal(params_, "XHTML", buffer(), os,
761 //                                     *(runparams.exportdata), false,
762 //                                     runparams.dryrun || runparams.inComment);
763         return docstring();
764 }
765
766
767 void InsetExternal::validate(LaTeXFeatures & features) const
768 {
769         if (params_.draft)
770                 return;
771
772         external::Template const * const et_ptr =
773                 external::getTemplatePtr(params_);
774         if (!et_ptr)
775                 return;
776         external::Template const & et = *et_ptr;
777
778         string format;
779         switch (features.runparams().flavor) {
780         case OutputParams::LATEX:
781         case OutputParams::DVILUATEX:
782                 format = "LaTeX";
783                 break;
784         case OutputParams::LUATEX:
785         case OutputParams::PDFLATEX:
786         case OutputParams::XETEX:
787                 format = "PDFLaTeX";
788                 break;
789         case OutputParams::XML:
790                 format = "DocBook";
791                 break;
792         case OutputParams::HTML:
793                 format = "html";
794                 break;
795         case OutputParams::TEXT:
796                 format = "text";
797                 break;
798         case OutputParams::LYX:
799                 format = "lyx";
800                 break;
801         }
802         external::Template::Formats::const_iterator cit =
803                 et.formats.find(format);
804
805         if (cit == et.formats.end()) {
806                 // If the template has not specified a PDFLaTeX output,
807                 // we try the LaTeX format.
808                 if (format == "PDFLaTeX") {
809                         cit = et.formats.find("LaTeX");
810                         if (cit == et.formats.end())
811                                 return;
812                 } else
813                         return;
814         }
815
816         // FIXME: We don't need that always, see InsetGraphics
817         features.require("lyxdot");
818
819         vector<string>::const_iterator it  = cit->second.requirements.begin();
820         vector<string>::const_iterator end = cit->second.requirements.end();
821         for (; it != end; ++it)
822                 features.require(*it);
823
824         external::TemplateManager & etm = external::TemplateManager::get();
825
826         it  = cit->second.preambleNames.begin();
827         end = cit->second.preambleNames.end();
828         for (; it != end; ++it) {
829                 string const preamble = etm.getPreambleDefByName(*it);
830                 if (!preamble.empty())
831                         features.addPreambleSnippet(preamble);
832         }
833 }
834
835
836 void InsetExternal::addPreview(DocIterator const & /*inset_pos*/,
837         graphics::PreviewLoader & ploader) const        
838 {
839         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
840         if (!ptr)
841                 return;
842
843         if (isPreviewWanted(params())) {
844                 ptr->setAbsFile(params_.filename);
845                 docstring const snippet = latexString(*this);
846                 ptr->addPreview(snippet, ploader);
847         }
848 }
849
850
851 string InsetExternal::contextMenuName() const
852 {
853         return "context-external";
854 }
855
856
857 void InsetExternal::string2params(string const & in, Buffer const & buffer,
858         InsetExternalParams & params)
859 {
860         params = InsetExternalParams();
861         if (in.empty())
862                 return;
863
864         istringstream data(in);
865         Lexer lex;
866         lex.setStream(data);
867
868         string name;
869         lex >> name;
870         if (!lex || name != "external") {
871                 LYXERR0("InsetExternal::string2params(" << in << ")\n"
872                                           "Expected arg 1 to be \"external\"\n");
873                 return;
874         }
875
876         // This is part of the inset proper that is usually swallowed
877         // by Text::readInset
878         string id;
879         lex >> id;
880         if (!lex || id != "External") {
881                 LYXERR0("InsetExternal::string2params(" << in << ")\n"
882                                           "Expected arg 2 to be \"External\"\n");
883                 return;
884         }
885
886         params.read(buffer, lex);
887 }
888
889
890 string InsetExternal::params2string(InsetExternalParams const & params,
891         Buffer const & buffer)
892 {
893         ostringstream data;
894         data << "external" << ' ';
895         params.write(buffer, data);
896         data << "\\end_inset\n";
897         return data.str();
898 }
899
900 } // namespace lyx