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