]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
Wrap all #warning calls inside #ifdef WITH_WARNINGS blocks.
[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.bv().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.bv().buffer();
459                 InsetExternalParams p;
460                 InsetExternalMailer::string2params(cmd.argument, buffer, p);
461                 setParams(p, buffer);
462                 cur.bv().update();
463                 break;
464         }
465
466         case LFUN_INSET_DIALOG_UPDATE:
467                 InsetExternalMailer(*this).updateDialog(&cur.bv());
468                 break;
469
470         case LFUN_MOUSE_RELEASE:
471                 InsetExternalMailer(*this).showDialog(&cur.bv());
472                 break;
473
474         default:
475                 InsetOld::dispatch(cur, cmd);
476         }
477 }
478
479
480 void InsetExternal::edit(LCursor & cur, bool)
481 {
482         InsetExternalMailer(*this).showDialog(&cur.bv());
483 }
484
485
486 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
487 {
488         renderer_->metrics(mi, dim);
489         dim_ = dim;
490 }
491
492
493 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
494 {
495         setPosCache(pi, x, y);
496         renderer_->draw(pi, x, y);
497 }
498
499
500 namespace {
501
502 enum RenderType {
503         RENDERBUTTON,
504         RENDERGRAPHIC,
505         RENDERPREVIEW
506 };
507
508
509 RenderType getRenderType(InsetExternalParams const & p)
510 {
511         if (!external::getTemplatePtr(p) ||
512             p.filename.empty() ||
513             p.display == external::NoDisplay)
514                 return RENDERBUTTON;
515
516         if (p.display == external::PreviewDisplay) {
517                 if (RenderPreview::activated())
518                         return RENDERPREVIEW;
519                 return RENDERBUTTON;
520         }
521
522         if (p.display == external::DefaultDisplay &&
523             lyxrc.display_graphics == graphics::NoDisplay)
524                 return RENDERBUTTON;
525         return RENDERGRAPHIC;
526 }
527
528
529 graphics::Params get_grfx_params(InsetExternalParams const & eparams)
530 {
531         graphics::Params gparams;
532
533         gparams.filename = eparams.filename.absFilename();
534         gparams.scale = eparams.lyxscale;
535         if (eparams.clipdata.clip)
536                 gparams.bb = eparams.clipdata.bbox;
537         gparams.angle = eparams.rotationdata.angle();
538
539         switch (eparams.display) {
540         case external::DefaultDisplay:
541                 gparams.display = graphics::DefaultDisplay;
542                 break;
543         case external::MonochromeDisplay:
544                 gparams.display = graphics::MonochromeDisplay;
545                 break;
546         case external::GrayscaleDisplay:
547                 gparams.display = graphics::GrayscaleDisplay;
548                 break;
549         case external::ColorDisplay:
550                 gparams.display = graphics::ColorDisplay;
551                 break;
552         case external::NoDisplay:
553                 gparams.display = graphics::NoDisplay;
554                 break;
555         default:
556                 BOOST_ASSERT(false);
557         }
558         if (gparams.display == graphics::DefaultDisplay)
559                 gparams.display = lyxrc.display_graphics;
560         // Override the above if we're not using a gui
561         if (!lyx_gui::use_gui)
562                 gparams.display = graphics::NoDisplay;
563
564         return gparams;
565 }
566
567
568 string const getScreenLabel(InsetExternalParams const & params,
569                             Buffer const & buffer)
570 {
571         external::Template const * const ptr =
572                 external::getTemplatePtr(params);
573         if (!ptr)
574                 return support::bformat(_("External template %1$s is not installed"),
575                                         params.templatename());
576         return external::doSubstitution(params, buffer, ptr->guiName);
577 }
578
579 void add_preview(RenderMonitoredPreview &, InsetExternal const &, Buffer const &);
580
581 } // namespace anon
582
583
584 InsetExternalParams const & InsetExternal::params() const
585 {
586         return params_;
587 }
588
589
590 void InsetExternal::setParams(InsetExternalParams const & p,
591                               Buffer const & buffer)
592 {
593         params_ = p;
594
595         // Subsequent calls to the InsetExternal::Params default constructor
596         // will use this.
597         defaultTemplateName = params_.templatename();
598
599         switch (getRenderType(params_)) {
600         case RENDERBUTTON: {
601                 RenderButton * button_ptr = renderer_->asButton();
602                 if (!button_ptr) {
603                         renderer_.reset(new RenderButton);
604                         button_ptr = renderer_->asButton();
605                 }
606
607                 button_ptr->update(getScreenLabel(params_, buffer), true);
608                 break;
609
610         } case RENDERGRAPHIC: {
611                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
612                 if (!graphic_ptr) {
613                         renderer_.reset(new RenderGraphic);
614                         graphic_ptr = renderer_->asGraphic();
615                         graphic_ptr->connect(
616                                 boost::bind(&InsetExternal::statusChanged, this));
617                 }
618
619                 graphic_ptr->update(get_grfx_params(params_));
620
621                 break;
622
623         } case RENDERPREVIEW: {
624                 RenderMonitoredPreview * preview_ptr =
625                         renderer_->asMonitoredPreview();
626                 if (!preview_ptr) {
627                         renderer_.reset(new RenderMonitoredPreview);
628                         preview_ptr = renderer_->asMonitoredPreview();
629                         preview_ptr->connect(
630                                 boost::bind(&InsetExternal::statusChanged, this));
631                         preview_ptr->fileChanged(
632                                 boost::bind(&InsetExternal::fileChanged, this));
633                 }
634
635                 if (preview_ptr->monitoring())
636                         preview_ptr->stopMonitoring();
637                 add_preview(*preview_ptr, *this, buffer);
638
639                 break;
640         }
641         }
642 }
643
644
645 void InsetExternal::fileChanged() const
646 {
647         Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
648         if (!buffer_ptr)
649                 return;
650
651         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
652         BOOST_ASSERT(ptr);
653
654         Buffer const & buffer = *buffer_ptr;
655         ptr->removePreview(buffer);
656         add_preview(*ptr, *this, buffer);
657         ptr->startLoading(buffer);
658 }
659
660
661 void InsetExternal::write(Buffer const & buffer, ostream & os) const
662 {
663         params_.write(buffer, os);
664 }
665
666
667 void InsetExternal::read(Buffer const & buffer, LyXLex & lex)
668 {
669         InsetExternalParams params;
670         if (params.read(buffer, lex))
671                 setParams(params, buffer);
672 }
673
674
675 int InsetExternal::latex(Buffer const & buf, ostream & os,
676                          OutputParams const & runparams) const
677 {
678         if (params_.draft) {
679                 os << "\\fbox{\\ttfamily{}"
680                    << params_.filename.outputFilename(buf.filePath())
681                    << "}\n";
682                 return 1;
683         }
684
685         // "nice" means that the buffer is exported to LaTeX format but not
686         // run through the LaTeX compiler.
687         // If we're running through the LaTeX compiler, we should write the
688         // generated files in the bufer's temporary directory.
689         bool const external_in_tmpdir = !runparams.nice;
690
691         // If the template has specified a PDFLaTeX output, then we try and
692         // use that.
693         if (runparams.flavor == OutputParams::PDFLATEX) {
694                 external::Template const * const et_ptr =
695                         external::getTemplatePtr(params_);
696                 if (!et_ptr)
697                         return 0;
698                 external::Template const & et = *et_ptr;
699
700                 external::Template::Formats::const_iterator cit =
701                         et.formats.find("PDFLaTeX");
702                 if (cit != et.formats.end())
703                         return external::writeExternal(params_, "PDFLaTeX",
704                                              buf, os, external_in_tmpdir);
705         }
706
707         return external::writeExternal(params_, "LaTeX",
708                                        buf, os, external_in_tmpdir);
709 }
710
711
712 int InsetExternal::plaintext(Buffer const & buf, ostream & os,
713                          OutputParams const &) const
714 {
715         return external::writeExternal(params_, "Ascii", buf, os);
716 }
717
718
719 int InsetExternal::linuxdoc(Buffer const & buf, ostream & os,
720                             OutputParams const &) const
721 {
722         return external::writeExternal(params_, "LinuxDoc", buf, os);
723 }
724
725
726 int InsetExternal::docbook(Buffer const & buf, ostream & os,
727                            OutputParams const &) const
728 {
729         return external::writeExternal(params_, "DocBook", buf, os);
730 }
731
732
733 void InsetExternal::validate(LaTeXFeatures & features) const
734 {
735         if (params_.draft)
736                 return;
737
738         external::Template const * const et_ptr =
739                 external::getTemplatePtr(params_);
740         if (!et_ptr)
741                 return;
742         external::Template const & et = *et_ptr;
743
744         external::Template::Formats::const_iterator cit =
745                 et.formats.find("LaTeX");
746         if (cit == et.formats.end())
747                 return;
748
749         if (!cit->second.requirement.empty())
750                 features.require(cit->second.requirement);
751
752         external::TemplateManager & etm = external::TemplateManager::get();
753
754         vector<string>::const_iterator it  = cit->second.preambleNames.begin();
755         vector<string>::const_iterator end = cit->second.preambleNames.end();
756         for (; it != end; ++it) {
757                 string const preamble = etm.getPreambleDefByName(*it);
758                 if (!preamble.empty())
759                         features.addExternalPreamble(preamble);
760         }
761 }
762
763
764 //
765 // preview stuff
766 //
767
768 namespace {
769
770 bool preview_wanted(InsetExternalParams const & params)
771 {
772         string const included_file = params.filename.absFilename();
773
774         return params.display == external::PreviewDisplay &&
775                 support::IsFileReadable(included_file);
776 }
777
778
779 string const latex_string(InsetExternal const & inset, Buffer const & buffer)
780 {
781         ostringstream os;
782         OutputParams runparams;
783         runparams.flavor = OutputParams::LATEX;
784         inset.latex(buffer, os, runparams);
785
786         return os.str();
787 }
788
789
790 void add_preview(RenderMonitoredPreview & renderer, InsetExternal const & inset,
791                  Buffer const & buffer)
792 {
793         InsetExternalParams const & params = inset.params();
794         if (RenderPreview::activated() && preview_wanted(params)) {
795                 renderer.setAbsFile(params.filename.absFilename());
796                 string const snippet = latex_string(inset, buffer);
797                 renderer.addPreview(snippet, buffer);
798         }
799 }
800
801 } // namespace anon
802
803
804 void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const
805 {
806         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
807         if (!ptr)
808                 return;
809
810         if (preview_wanted(params())) {
811                 ptr->setAbsFile(params_.filename.absFilename());
812                 string const snippet = latex_string(*this, ploader.buffer());
813                 ptr->addPreview(snippet, ploader);
814         }
815 }
816
817
818 /// Mailer stuff
819
820 string const InsetExternalMailer::name_("external");
821
822 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
823         : inset_(inset)
824 {}
825
826
827 string const InsetExternalMailer::inset2string(Buffer const & buffer) const
828 {
829         return params2string(inset_.params(), buffer);
830 }
831
832
833 void InsetExternalMailer::string2params(string const & in,
834                                         Buffer const & buffer,
835                                         InsetExternalParams & params)
836 {
837         params = InsetExternalParams();
838         if (in.empty())
839                 return;
840
841         istringstream data(in);
842         LyXLex lex(0,0);
843         lex.setStream(data);
844
845         string name;
846         lex >> name;
847         if (!lex || name != name_)
848                 return print_mailer_error("InsetExternalMailer", in, 1, name_);
849
850         // This is part of the inset proper that is usually swallowed
851         // by LyXText::readInset
852         string id;
853         lex >> id;
854         if (!lex || id != "External")
855                 return print_mailer_error("InsetBoxMailer", in, 2, "External");
856
857         params.read(buffer, lex);
858 }
859
860
861 string const
862 InsetExternalMailer::params2string(InsetExternalParams const & params,
863                                    Buffer const & buffer)
864 {
865         ostringstream data;
866         data << name_ << ' ';
867         params.write(buffer, data);
868         data << "\\end_inset\n";
869         return data.str();
870 }