]> git.lyx.org Git - lyx.git/blob - src/insets/InsetExternal.cpp
Fix bad cursor positioning when entering an inset
[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 "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                 if (!tempname_.empty())
102                         tempname_.removeFile();
103                 support::TempFile f("lyxextXXXXXX.tmp");
104                 f.setAutoRemove(false);
105                 tempname_ = f.name(); 
106         }
107         return *this;
108 }
109
110
111 support::FileName TempName::operator()() const
112 {
113         return tempname_;
114 }
115
116 } // namespace external
117
118
119 InsetExternalParams::InsetExternalParams()
120         : display(true),
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                 write(buffer, lyxerr);
381         }
382
383         return !read_error;
384 }
385
386
387 namespace {
388
389 docstring screenLabel(InsetExternalParams const & params,
390                             Buffer const & buffer)
391 {
392         external::Template const * const ptr =
393                 external::getTemplatePtr(params);
394         if (!ptr)
395                 // FIXME UNICODE
396                 return bformat((_("External template %1$s is not installed")),
397                                         from_utf8(params.templatename()));
398         // FIXME UNICODE
399         docstring gui = _(ptr->guiName);
400         gui += ": ";
401
402         if (params.filename.empty())
403                 gui += "???";
404         else
405                 gui += from_utf8(params.filename.relFileName(buffer.filePath()));
406
407         return gui;
408 }
409
410 } // namespace anon
411
412
413
414 InsetExternal::InsetExternal(Buffer * buf)
415         : Inset(buf), renderer_(new RenderButton)
416 {
417 }
418
419
420 // Mouse hover is not copied and remains empty
421 InsetExternal::InsetExternal(InsetExternal const & other)
422         : Inset(other),
423           boost::signals::trackable(),
424           params_(other.params_),
425           renderer_(other.renderer_->clone(this))
426 {}
427
428
429 InsetExternal::~InsetExternal()
430 {
431         hideDialogs("external", this);
432
433         map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
434         map<BufferView const *, bool>::iterator end = mouse_hover_.end();
435         for (; it != end; ++it)
436                 if (it->second)
437                         it->first->clearLastInset(this);
438 }
439
440
441 bool InsetExternal::setMouseHover(BufferView const * bv, bool mouse_hover) const
442 {
443         mouse_hover_[bv] = mouse_hover;
444         return true;
445 }
446
447
448 void InsetExternal::statusChanged() const
449 {
450         updateFrontend();
451 }
452
453
454 void InsetExternal::doDispatch(Cursor & cur, FuncRequest & cmd)
455 {
456         switch (cmd.action()) {
457
458         case LFUN_INSET_EDIT: {
459                 InsetExternalParams p =  params();
460                 if (!cmd.argument().empty())
461                         string2params(to_utf8(cmd.argument()), buffer(), p);
462                 external::editExternal(p, buffer());
463                 break;
464         }
465
466         case LFUN_INSET_MODIFY: {
467                 InsetExternalParams p;
468                 string2params(to_utf8(cmd.argument()), buffer(), p);
469                 cur.recordUndo();
470                 setParams(p);
471                 break;
472         }
473
474         case LFUN_INSET_DIALOG_UPDATE:
475                 cur.bv().updateDialog("external",
476                         params2string(params(), cur.bv().buffer()));
477                 break;
478
479         default:
480                 Inset::doDispatch(cur, cmd);
481         }
482 }
483
484
485 bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
486                 FuncStatus & flag) const
487 {
488         switch (cmd.action()) {
489
490         case LFUN_INSET_EDIT:
491         case LFUN_INSET_MODIFY:
492         case LFUN_INSET_DIALOG_UPDATE:
493                 flag.setEnabled(true);
494                 return true;
495
496         default:
497                 return Inset::getStatus(cur, cmd, flag);
498         }
499 }
500
501
502 void InsetExternal::addToToc(DocIterator const & cpit, bool output_active,
503                                                          UpdateType) const
504 {
505         DocIterator pit = cpit;
506         pit.push_back(CursorSlice(const_cast<InsetExternal &>(*this)));
507         shared_ptr<Toc> toc = buffer().tocBackend().toc("external");
508         docstring str = screenLabel(params_, buffer());
509         toc->push_back(TocItem(pit, 0, str, output_active));
510 }
511
512
513 bool InsetExternal::showInsetDialog(BufferView * bv) const
514 {
515         bv->showDialog("external", params2string(params(), bv->buffer()),
516                 const_cast<InsetExternal *>(this));
517         return true;
518 }
519
520
521 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
522 {
523         renderer_->metrics(mi, dim);
524 }
525
526
527 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
528 {
529         if (renderer_->asButton())
530                 renderer_->setRenderState(mouse_hover_[pi.base.bv]);
531         renderer_->draw(pi, x, y);
532 }
533
534
535 namespace {
536
537 graphics::Params get_grfx_params(InsetExternalParams const & eparams)
538 {
539         graphics::Params gparams;
540
541         gparams.filename = eparams.filename;
542         gparams.scale = eparams.lyxscale;
543         if (eparams.clipdata.clip)
544                 gparams.bb = eparams.clipdata.bbox;
545         gparams.angle = convert<double>(eparams.rotationdata.adjAngle());
546         gparams.display = eparams.display;
547
548         return gparams;
549 }
550
551 } // namespace anon
552
553
554 static bool isPreviewWanted(InsetExternalParams const & params)
555 {
556         return params.display && params.filename.isReadableFile();
557 }
558
559
560 static docstring latexString(InsetExternal const & inset)
561 {
562         TexRow texrow;
563         odocstringstream ods;
564         otexstream os(ods, texrow);
565         // We don't need to set runparams.encoding since it is not used by
566         // latex().
567         OutputParams runparams(0);
568         runparams.flavor = OutputParams::LATEX;
569         inset.latex(os, runparams);
570         return ods.str();
571 }
572
573
574 static void add_preview_and_start_loading(RenderMonitoredPreview & renderer,
575                                    InsetExternal const & inset,
576                                    Buffer const & buffer)
577 {
578         InsetExternalParams const & params = inset.params();
579
580         if (RenderPreview::previewText() && isPreviewWanted(params)) {
581                 renderer.setAbsFile(params.filename);
582                 docstring const snippet = latexString(inset);
583                 renderer.addPreview(snippet, buffer);
584                 renderer.startLoading(buffer);
585         }
586 }
587
588
589 InsetExternalParams const & InsetExternal::params() const
590 {
591         return params_;
592 }
593
594
595 void InsetExternal::updatePreview()
596 {
597         setParams(params_);
598 }
599
600
601 void InsetExternal::setParams(InsetExternalParams const & p)
602 {
603         params_ = p;
604
605         // Subsequent calls to the InsetExternal::Params default constructor
606         // will use this.
607         defaultTemplateName = params_.templatename();
608
609         if (!external::getTemplatePtr(params_) || params_.filename.empty()
610                 || !params_.display
611                 || !lyxrc.display_graphics
612                 || params_.preview_mode == PREVIEW_OFF
613                 || (params_.preview_mode == PREVIEW_INSTANT
614                     && !RenderPreview::previewText())) {
615                 RenderButton * button_ptr = renderer_->asButton();
616                 if (!button_ptr) {
617                         renderer_.reset(new RenderButton);
618                         button_ptr = renderer_->asButton();
619                 }
620                 button_ptr->update(screenLabel(params_, buffer()), true);
621                 return;
622         }
623
624         switch (params_.preview_mode) {
625         case PREVIEW_OFF:
626                 // Already taken care of above.
627                 LASSERT(false, return);
628                 break;
629         case PREVIEW_INSTANT: {
630                 renderer_.reset(new RenderMonitoredPreview(this));
631                 RenderMonitoredPreview * preview_ptr = renderer_->asMonitoredPreview();
632                 preview_ptr->fileChanged(bind(&InsetExternal::fileChanged, this));
633                 if (preview_ptr->monitoring())
634                         preview_ptr->stopMonitoring();
635                 add_preview_and_start_loading(*preview_ptr, *this, buffer());
636                 break;
637         } 
638         case PREVIEW_GRAPHICS: {
639                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
640                 if (!graphic_ptr) {
641                         renderer_.reset(new RenderGraphic(this));
642                         graphic_ptr = renderer_->asGraphic();
643                 }
644                 graphic_ptr->update(get_grfx_params(params_));
645                 break;
646         }
647         }
648 }
649
650
651 void InsetExternal::fileChanged() const
652 {
653         Buffer const * const buffer = updateFrontend();
654         if (!buffer)
655                 return;
656
657         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
658         LASSERT(ptr, return);
659
660         ptr->removePreview(*buffer);
661         add_preview_and_start_loading(*ptr, *this, *buffer);
662 }
663
664
665 void InsetExternal::write(ostream & os) const
666 {
667         params_.write(buffer(), os);
668 }
669
670
671 void InsetExternal::read(Lexer & lex)
672 {
673         InsetExternalParams params;
674         if (params.read(buffer(), lex))
675                 setParams(params);
676 }
677
678
679 void InsetExternal::latex(otexstream & os, OutputParams const & runparams) const
680 {
681         if (params_.draft) {
682                 // FIXME UNICODE
683                 os << "\\fbox{\\ttfamily{}"
684                    << from_utf8(params_.filename.outputFileName(buffer().filePath()))
685                    << "}\n";
686                 return;
687         }
688
689         // "nice" means that the buffer is exported to LaTeX format but not
690         // run through the LaTeX compiler.
691         // If we're running through the LaTeX compiler, we should write the
692         // generated files in the buffer's temporary directory.
693         bool const external_in_tmpdir = !runparams.nice;
694         bool const dryrun = runparams.dryrun || runparams.inComment;
695
696         // If the template has specified a PDFLaTeX output, then we try and
697         // use that.
698         if (runparams.flavor == OutputParams::PDFLATEX) {
699                 external::Template const * const et_ptr =
700                         external::getTemplatePtr(params_);
701                 if (!et_ptr)
702                         return;
703                 external::Template const & et = *et_ptr;
704
705                 external::Template::Formats::const_iterator cit =
706                         et.formats.find("PDFLaTeX");
707
708                 if (cit != et.formats.end()) {
709                         external::writeExternal(params_, "PDFLaTeX",
710                                                 buffer(), os,
711                                                 *(runparams.exportdata),
712                                                 external_in_tmpdir,
713                                                 dryrun);
714                         return;
715                 }
716         }
717
718         external::writeExternal(params_, "LaTeX", buffer(), os,
719                                 *(runparams.exportdata),
720                                 external_in_tmpdir,
721                                 dryrun);
722 }
723
724
725 int InsetExternal::plaintext(odocstringstream & os,
726                              OutputParams const & runparams, size_t) const
727 {
728         // this is too slow for constant use
729         if (runparams.for_tooltip)
730                 return 0;
731
732         TexRow texrow;
733         otexstream ots(os, texrow);
734         bool const external_in_tmpdir = !runparams.nice;
735         bool const dryrun = runparams.dryrun || runparams.inComment;
736         ots << '\n'; // output external material on a new line
737         external::writeExternal(params_, "Ascii", buffer(), ots,
738                                 *(runparams.exportdata), external_in_tmpdir, dryrun);
739         return PLAINTEXT_NEWLINE;
740 }
741
742
743 int InsetExternal::docbook(odocstream & os,
744                            OutputParams const & runparams) const
745 {
746         TexRow texrow;
747         odocstringstream ods;
748         otexstream ots(ods, texrow);
749         bool const external_in_tmpdir = !runparams.nice;
750         bool const dryrun = runparams.dryrun || runparams.inComment;
751         external::writeExternal(params_, "DocBook", buffer(), ots,
752                                 *(runparams.exportdata), external_in_tmpdir, dryrun);
753         os << ods.str();
754         return int(count(ods.str().begin(), ods.str().end(), '\n'));
755 }
756
757
758 docstring InsetExternal::xhtml(XHTMLStream & xs,
759                         OutputParams const & runparams) const
760 {
761         bool const external_in_tmpdir = !runparams.nice;
762         bool const dryrun = runparams.dryrun || runparams.inComment;
763     TexRow texrow;
764     odocstringstream ods;
765     otexstream ots(ods, texrow);
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         it  = cit->second.preambleNames.begin();
833         end = cit->second.preambleNames.end();
834         for (; it != end; ++it) {
835                 string const preamble = etm.getPreambleDefByName(*it);
836                 if (!preamble.empty())
837                         features.addPreambleSnippet(preamble);
838         }
839 }
840
841
842 void InsetExternal::addPreview(DocIterator const & /*inset_pos*/,
843         graphics::PreviewLoader & ploader) const        
844 {
845         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
846         if (!ptr)
847                 return;
848
849         if (isPreviewWanted(params())) {
850                 ptr->setAbsFile(params_.filename);
851                 docstring const snippet = latexString(*this);
852                 ptr->addPreview(snippet, ploader);
853         }
854 }
855
856
857 string InsetExternal::contextMenuName() const
858 {
859         return "context-external";
860 }
861
862
863 void InsetExternal::string2params(string const & in, Buffer const & buffer,
864         InsetExternalParams & params)
865 {
866         params = InsetExternalParams();
867         if (in.empty())
868                 return;
869
870         istringstream data(in);
871         Lexer lex;
872         lex.setStream(data);
873
874         string name;
875         lex >> name;
876         if (!lex || name != "external") {
877                 LYXERR0("InsetExternal::string2params(" << in << ")\n"
878                                           "Expected arg 1 to be \"external\"\n");
879                 return;
880         }
881
882         // This is part of the inset proper that is usually swallowed
883         // by Text::readInset
884         string id;
885         lex >> id;
886         if (!lex || id != "External") {
887                 LYXERR0("InsetExternal::string2params(" << in << ")\n"
888                                           "Expected arg 2 to be \"External\"\n");
889                 return;
890         }
891
892         params.read(buffer, lex);
893 }
894
895
896 string InsetExternal::params2string(InsetExternalParams const & params,
897         Buffer const & buffer)
898 {
899         ostringstream data;
900         data << "external" << ' ';
901         params.write(buffer, data);
902         data << "\\end_inset\n";
903         return data.str();
904 }
905
906 } // namespace lyx