]> git.lyx.org Git - lyx.git/blob - src/insets/InsetExternal.cpp
6417013696a3f043dc12ae4ea3ed3014bb2f0583
[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                 //FIXME: why is the value below immediately forgotten?
603                 RenderMonitoredPreview * preview_ptr = renderer_->asMonitoredPreview();
604                 renderer_.reset(new RenderMonitoredPreview(this));
605                 preview_ptr = renderer_->asMonitoredPreview();
606                 preview_ptr->fileChanged(bind(&InsetExternal::fileChanged, this));
607                 if (preview_ptr->monitoring())
608                         preview_ptr->stopMonitoring();
609                 add_preview_and_start_loading(*preview_ptr, *this, buffer());
610                 break;
611         } 
612         case PREVIEW_GRAPHICS: {
613                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
614                 if (!graphic_ptr) {
615                         renderer_.reset(new RenderGraphic(this));
616                         graphic_ptr = renderer_->asGraphic();
617                 }
618                 graphic_ptr->update(get_grfx_params(params_));
619                 break;
620         }
621         }
622 }
623
624
625 void InsetExternal::fileChanged() const
626 {
627         Buffer const * const buffer = updateFrontend();
628         if (!buffer)
629                 return;
630
631         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
632         LASSERT(ptr, return);
633
634         ptr->removePreview(*buffer);
635         add_preview_and_start_loading(*ptr, *this, *buffer);
636 }
637
638
639 void InsetExternal::write(ostream & os) const
640 {
641         params_.write(buffer(), os);
642 }
643
644
645 void InsetExternal::read(Lexer & lex)
646 {
647         InsetExternalParams params;
648         if (params.read(buffer(), lex))
649                 setParams(params);
650 }
651
652
653 void InsetExternal::latex(otexstream & os, OutputParams const & runparams) const
654 {
655         if (params_.draft) {
656                 // FIXME UNICODE
657                 os << "\\fbox{\\ttfamily{}"
658                    << from_utf8(params_.filename.outputFileName(buffer().filePath()))
659                    << "}\n";
660                 return;
661         }
662
663         // "nice" means that the buffer is exported to LaTeX format but not
664         // run through the LaTeX compiler.
665         // If we're running through the LaTeX compiler, we should write the
666         // generated files in the buffer's temporary directory.
667         bool const external_in_tmpdir = !runparams.nice;
668         bool const dryrun = runparams.dryrun || runparams.inComment;
669
670         // If the template has specified a PDFLaTeX output, then we try and
671         // use that.
672         if (runparams.flavor == OutputParams::PDFLATEX) {
673                 external::Template const * const et_ptr =
674                         external::getTemplatePtr(params_);
675                 if (!et_ptr)
676                         return;
677                 external::Template const & et = *et_ptr;
678
679                 external::Template::Formats::const_iterator cit =
680                         et.formats.find("PDFLaTeX");
681
682                 if (cit != et.formats.end()) {
683                         external::writeExternal(params_, "PDFLaTeX",
684                                                 buffer(), os,
685                                                 *(runparams.exportdata),
686                                                 external_in_tmpdir,
687                                                 dryrun);
688                         return;
689                 }
690         }
691
692         external::writeExternal(params_, "LaTeX", buffer(), os,
693                                 *(runparams.exportdata),
694                                 external_in_tmpdir,
695                                 dryrun);
696 }
697
698
699 int InsetExternal::plaintext(odocstringstream & os,
700                              OutputParams const & runparams, size_t) const
701 {
702         // this is too slow for constant use
703         if (runparams.for_tooltip)
704                 return 0;
705
706         TexRow texrow;
707         otexstream ots(os, texrow);
708         ots << '\n'; // output external material on a new line
709         external::writeExternal(params_, "Ascii", buffer(), ots,
710                                 *(runparams.exportdata), false,
711                                 runparams.dryrun || runparams.inComment);
712         return PLAINTEXT_NEWLINE;
713 }
714
715
716 int InsetExternal::docbook(odocstream & os,
717                            OutputParams const & runparams) const
718 {
719         TexRow texrow;
720         odocstringstream ods;
721         otexstream ots(ods, texrow);
722         external::writeExternal(params_, "DocBook", buffer(), ots,
723                                 *(runparams.exportdata), false,
724                                 runparams.dryrun || runparams.inComment);
725         os << ods.str();
726         return int(count(ods.str().begin(), ods.str().end(), '\n'));
727 }
728
729
730 docstring InsetExternal::xhtml(XHTMLStream  & /*xs*/,
731                         OutputParams const & /*rp*/) const
732 {
733 //      external::writeExternal(params_, "XHTML", buffer(), os,
734 //                                     *(runparams.exportdata), false,
735 //                                     runparams.dryrun || runparams.inComment);
736         return docstring();
737 }
738
739
740 void InsetExternal::validate(LaTeXFeatures & features) const
741 {
742         if (params_.draft)
743                 return;
744
745         external::Template const * const et_ptr =
746                 external::getTemplatePtr(params_);
747         if (!et_ptr)
748                 return;
749         external::Template const & et = *et_ptr;
750
751         string format;
752         switch (features.runparams().flavor) {
753         case OutputParams::LATEX:
754         case OutputParams::DVILUATEX:
755                 format = "LaTeX";
756                 break;
757         case OutputParams::LUATEX:
758         case OutputParams::PDFLATEX:
759         case OutputParams::XETEX:
760                 format = "PDFLaTeX";
761                 break;
762         case OutputParams::XML:
763                 format = "DocBook";
764                 break;
765         case OutputParams::HTML:
766                 format = "html";
767                 break;
768         case OutputParams::TEXT:
769                 format = "text";
770                 break;
771         case OutputParams::LYX:
772                 format = "lyx";
773                 break;
774         }
775         external::Template::Formats::const_iterator cit =
776                 et.formats.find(format);
777
778         if (cit == et.formats.end()) {
779                 // If the template has not specified a PDFLaTeX output,
780                 // we try the LaTeX format.
781                 if (format == "PDFLaTeX") {
782                         cit = et.formats.find("LaTeX");
783                         if (cit == et.formats.end())
784                                 return;
785                 } else
786                         return;
787         }
788
789         // FIXME: We don't need that always, see InsetGraphics
790         features.require("lyxdot");
791
792         vector<string>::const_iterator it  = cit->second.requirements.begin();
793         vector<string>::const_iterator end = cit->second.requirements.end();
794         for (; it != end; ++it)
795                 features.require(*it);
796
797         external::TemplateManager & etm = external::TemplateManager::get();
798
799         it  = cit->second.preambleNames.begin();
800         end = cit->second.preambleNames.end();
801         for (; it != end; ++it) {
802                 string const preamble = etm.getPreambleDefByName(*it);
803                 if (!preamble.empty())
804                         features.addPreambleSnippet(preamble);
805         }
806 }
807
808
809 void InsetExternal::addPreview(DocIterator const & /*inset_pos*/,
810         graphics::PreviewLoader & ploader) const        
811 {
812         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
813         if (!ptr)
814                 return;
815
816         if (isPreviewWanted(params())) {
817                 ptr->setAbsFile(params_.filename);
818                 docstring const snippet = latexString(*this);
819                 ptr->addPreview(snippet, ploader);
820         }
821 }
822
823
824 string InsetExternal::contextMenuName() const
825 {
826         return "context-external";
827 }
828
829
830 void InsetExternal::string2params(string const & in, Buffer const & buffer,
831         InsetExternalParams & params)
832 {
833         params = InsetExternalParams();
834         if (in.empty())
835                 return;
836
837         istringstream data(in);
838         Lexer lex;
839         lex.setStream(data);
840
841         string name;
842         lex >> name;
843         if (!lex || name != "external") {
844                 LYXERR0("InsetExternal::string2params(" << in << ")\n"
845                                           "Expected arg 1 to be \"external\"\n");
846                 return;
847         }
848
849         // This is part of the inset proper that is usually swallowed
850         // by Text::readInset
851         string id;
852         lex >> id;
853         if (!lex || id != "External") {
854                 LYXERR0("InsetExternal::string2params(" << in << ")\n"
855                                           "Expected arg 2 to be \"External\"\n");
856                 return;
857         }
858
859         params.read(buffer, lex);
860 }
861
862
863 string InsetExternal::params2string(InsetExternalParams const & params,
864         Buffer const & buffer)
865 {
866         ostringstream data;
867         data << "external" << ' ';
868         params.write(buffer, data);
869         data << "\\end_inset\n";
870         return data.str();
871 }
872
873 } // namespace lyx