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