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