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