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