]> git.lyx.org Git - features.git/blob - src/insets/InsetExternal.cpp
Replace Boost.Signals with Boost.Signals2
[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                 write(buffer, lyxerr);
380         }
381
382         return !read_error;
383 }
384
385
386 namespace {
387
388 docstring screenLabel(InsetExternalParams const & params,
389                             Buffer const & buffer)
390 {
391         external::Template const * const ptr =
392                 external::getTemplatePtr(params);
393         if (!ptr)
394                 // FIXME UNICODE
395                 return bformat((_("External template %1$s is not installed")),
396                                         from_utf8(params.templatename()));
397         // FIXME UNICODE
398         docstring gui = _(ptr->guiName);
399         gui += ": ";
400
401         if (params.filename.empty())
402                 gui += "???";
403         else
404                 gui += from_utf8(params.filename.relFileName(buffer.filePath()));
405
406         return gui;
407 }
408
409 } // namespace anon
410
411
412
413 InsetExternal::InsetExternal(Buffer * buf)
414         : Inset(buf), renderer_(new RenderButton)
415 {
416 }
417
418
419 // Mouse hover is not copied and remains empty
420 InsetExternal::InsetExternal(InsetExternal const & other)
421         : Inset(other),
422           boost::signals2::trackable(),
423           params_(other.params_),
424           renderer_(other.renderer_->clone(this))
425 {}
426
427
428 InsetExternal::~InsetExternal()
429 {
430         hideDialogs("external", this);
431
432         map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
433         map<BufferView const *, bool>::iterator end = mouse_hover_.end();
434         for (; it != end; ++it)
435                 if (it->second)
436                         it->first->clearLastInset(this);
437 }
438
439
440 bool InsetExternal::setMouseHover(BufferView const * bv, bool mouse_hover) const
441 {
442         mouse_hover_[bv] = mouse_hover;
443         return true;
444 }
445
446
447 void InsetExternal::statusChanged() const
448 {
449         updateFrontend();
450 }
451
452
453 void InsetExternal::doDispatch(Cursor & cur, FuncRequest & cmd)
454 {
455         switch (cmd.action()) {
456
457         case LFUN_INSET_EDIT: {
458                 InsetExternalParams p =  params();
459                 if (!cmd.argument().empty())
460                         string2params(to_utf8(cmd.argument()), buffer(), p);
461                 external::editExternal(p, buffer());
462                 break;
463         }
464
465         case LFUN_INSET_MODIFY: {
466                 InsetExternalParams p;
467                 string2params(to_utf8(cmd.argument()), buffer(), p);
468                 cur.recordUndo();
469                 setParams(p);
470                 break;
471         }
472
473         case LFUN_INSET_DIALOG_UPDATE:
474                 cur.bv().updateDialog("external",
475                         params2string(params(), cur.bv().buffer()));
476                 break;
477
478         default:
479                 Inset::doDispatch(cur, cmd);
480         }
481 }
482
483
484 bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
485                 FuncStatus & flag) const
486 {
487         switch (cmd.action()) {
488
489         case LFUN_INSET_EDIT:
490         case LFUN_INSET_MODIFY:
491         case LFUN_INSET_DIALOG_UPDATE:
492                 flag.setEnabled(true);
493                 return true;
494
495         default:
496                 return Inset::getStatus(cur, cmd, flag);
497         }
498 }
499
500
501 void InsetExternal::addToToc(DocIterator const & cpit, bool output_active,
502                                                          UpdateType) const
503 {
504         DocIterator pit = cpit;
505         pit.push_back(CursorSlice(const_cast<InsetExternal &>(*this)));
506         shared_ptr<Toc> toc = buffer().tocBackend().toc("external");
507         docstring str = screenLabel(params_, buffer());
508         toc->push_back(TocItem(pit, 0, str, output_active));
509 }
510
511
512 bool InsetExternal::showInsetDialog(BufferView * bv) const
513 {
514         bv->showDialog("external", params2string(params(), bv->buffer()),
515                 const_cast<InsetExternal *>(this));
516         return true;
517 }
518
519
520 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
521 {
522         renderer_->metrics(mi, dim);
523 }
524
525
526 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
527 {
528         if (renderer_->asButton())
529                 renderer_->setRenderState(mouse_hover_[pi.base.bv]);
530         renderer_->draw(pi, x, y);
531 }
532
533
534 namespace {
535
536 graphics::Params get_grfx_params(InsetExternalParams const & eparams)
537 {
538         graphics::Params gparams;
539
540         gparams.filename = eparams.filename;
541         gparams.scale = eparams.lyxscale;
542         if (eparams.clipdata.clip)
543                 gparams.bb = eparams.clipdata.bbox;
544         gparams.angle = convert<double>(eparams.rotationdata.adjAngle());
545         gparams.display = eparams.display;
546
547         return gparams;
548 }
549
550 } // namespace anon
551
552
553 static bool isPreviewWanted(InsetExternalParams const & params)
554 {
555         return params.display && params.filename.isReadableFile();
556 }
557
558
559 static docstring latexString(InsetExternal const & inset)
560 {
561         TexRow texrow;
562         odocstringstream ods;
563         otexstream os(ods, texrow);
564         // We don't need to set runparams.encoding since it is not used by
565         // latex().
566         OutputParams runparams(0);
567         runparams.flavor = OutputParams::LATEX;
568         inset.latex(os, runparams);
569         return ods.str();
570 }
571
572
573 static void add_preview_and_start_loading(RenderMonitoredPreview & renderer,
574                                    InsetExternal const & inset,
575                                    Buffer const & buffer)
576 {
577         InsetExternalParams const & params = inset.params();
578
579         if (RenderPreview::previewText() && isPreviewWanted(params)) {
580                 renderer.setAbsFile(params.filename);
581                 docstring const snippet = latexString(inset);
582                 renderer.addPreview(snippet, buffer);
583                 renderer.startLoading(buffer);
584         }
585 }
586
587
588 InsetExternalParams const & InsetExternal::params() const
589 {
590         return params_;
591 }
592
593
594 void InsetExternal::updatePreview()
595 {
596         setParams(params_);
597 }
598
599
600 void InsetExternal::setParams(InsetExternalParams const & p)
601 {
602         params_ = p;
603
604         // Subsequent calls to the InsetExternal::Params default constructor
605         // will use this.
606         defaultTemplateName = params_.templatename();
607
608         if (!external::getTemplatePtr(params_) || params_.filename.empty()
609                 || !params_.display
610                 || !lyxrc.display_graphics
611                 || params_.preview_mode == PREVIEW_OFF
612                 || (params_.preview_mode == PREVIEW_INSTANT
613                     && !RenderPreview::previewText())) {
614                 RenderButton * button_ptr = renderer_->asButton();
615                 if (!button_ptr) {
616                         renderer_.reset(new RenderButton);
617                         button_ptr = renderer_->asButton();
618                 }
619                 button_ptr->update(screenLabel(params_, buffer()), true);
620                 return;
621         }
622
623         switch (params_.preview_mode) {
624         case PREVIEW_OFF:
625                 // Already taken care of above.
626                 LASSERT(false, return);
627                 break;
628         case PREVIEW_INSTANT: {
629                 renderer_.reset(new RenderMonitoredPreview(this));
630                 RenderMonitoredPreview * preview_ptr = renderer_->asMonitoredPreview();
631                 preview_ptr->fileChanged(bind(&InsetExternal::fileChanged, this));
632                 if (preview_ptr->monitoring())
633                         preview_ptr->stopMonitoring();
634                 add_preview_and_start_loading(*preview_ptr, *this, buffer());
635                 break;
636         } 
637         case PREVIEW_GRAPHICS: {
638                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
639                 if (!graphic_ptr) {
640                         renderer_.reset(new RenderGraphic(this));
641                         graphic_ptr = renderer_->asGraphic();
642                 }
643                 graphic_ptr->update(get_grfx_params(params_));
644                 break;
645         }
646         }
647 }
648
649
650 void InsetExternal::fileChanged() const
651 {
652         Buffer const * const buffer = updateFrontend();
653         if (!buffer)
654                 return;
655
656         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
657         LASSERT(ptr, return);
658
659         ptr->removePreview(*buffer);
660         add_preview_and_start_loading(*ptr, *this, *buffer);
661 }
662
663
664 void InsetExternal::write(ostream & os) const
665 {
666         params_.write(buffer(), os);
667 }
668
669
670 void InsetExternal::read(Lexer & lex)
671 {
672         InsetExternalParams params;
673         if (params.read(buffer(), lex))
674                 setParams(params);
675 }
676
677
678 void InsetExternal::latex(otexstream & os, OutputParams const & runparams) const
679 {
680         if (params_.draft) {
681                 // FIXME UNICODE
682                 os << "\\fbox{\\ttfamily{}"
683                    << from_utf8(params_.filename.outputFileName(buffer().filePath()))
684                    << "}\n";
685                 return;
686         }
687
688         // "nice" means that the buffer is exported to LaTeX format but not
689         // run through the LaTeX compiler.
690         // If we're running through the LaTeX compiler, we should write the
691         // generated files in the buffer's temporary directory.
692         bool const external_in_tmpdir = !runparams.nice;
693         bool const dryrun = runparams.dryrun || runparams.inComment;
694
695         // If the template has specified a PDFLaTeX output, then we try and
696         // use that.
697         if (runparams.flavor == OutputParams::PDFLATEX) {
698                 external::Template const * const et_ptr =
699                         external::getTemplatePtr(params_);
700                 if (!et_ptr)
701                         return;
702                 external::Template const & et = *et_ptr;
703
704                 external::Template::Formats::const_iterator cit =
705                         et.formats.find("PDFLaTeX");
706
707                 if (cit != et.formats.end()) {
708                         external::writeExternal(params_, "PDFLaTeX",
709                                                 buffer(), os,
710                                                 *(runparams.exportdata),
711                                                 external_in_tmpdir,
712                                                 dryrun);
713                         return;
714                 }
715         }
716
717         external::writeExternal(params_, "LaTeX", buffer(), os,
718                                 *(runparams.exportdata),
719                                 external_in_tmpdir,
720                                 dryrun);
721 }
722
723
724 int InsetExternal::plaintext(odocstringstream & os,
725                              OutputParams const & runparams, size_t) const
726 {
727         // this is too slow for constant use
728         if (runparams.for_tooltip)
729                 return 0;
730
731         TexRow texrow;
732         otexstream ots(os, texrow);
733         ots << '\n'; // output external material on a new line
734         external::writeExternal(params_, "Ascii", buffer(), ots,
735                                 *(runparams.exportdata), false,
736                                 runparams.dryrun || runparams.inComment);
737         return PLAINTEXT_NEWLINE;
738 }
739
740
741 int InsetExternal::docbook(odocstream & os,
742                            OutputParams const & runparams) const
743 {
744         TexRow texrow;
745         odocstringstream ods;
746         otexstream ots(ods, texrow);
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