]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
3c5d14ea6598a7fe4e80a167eb4ad755fbb41911
[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         support::unlink(tempname_);
81         // must have an extension for the converter code to work correctly.
82         tempname_ += ".tmp";
83 }
84
85
86 TempName::TempName(TempName const &)
87 {
88         tempname_ = TempName()();
89 }
90
91
92 TempName::~TempName()
93 {
94         support::unlink(tempname_);
95 }
96
97
98 TempName &
99 TempName::operator=(TempName const & other)
100 {
101         if (this != &other)
102                 tempname_ = TempName()();
103         return *this;
104 }
105
106
107 namespace {
108
109 /// The translator between the Display enum and corresponding lyx string.
110 Translator<DisplayType, string> const initTranslator()
111 {
112         Translator<DisplayType, string> translator(DefaultDisplay, "default");
113
114         // Fill the display translator
115         translator.addPair(MonochromeDisplay, "monochrome");
116         translator.addPair(GrayscaleDisplay, "grayscale");
117         translator.addPair(ColorDisplay, "color");
118         translator.addPair(PreviewDisplay, "preview");
119         translator.addPair(NoDisplay, "none");
120
121         return translator;
122 }
123
124 } // namespace anon
125
126
127 Translator<DisplayType, string> const & displayTranslator()
128 {
129         static Translator<DisplayType, string> const translator =
130                 initTranslator();
131         return translator;
132 }
133
134 } // namespace external
135 } // namespace lyx
136
137
138 InsetExternalParams::InsetExternalParams()
139         : display(defaultDisplayType),
140           lyxscale(defaultLyxScale),
141           draft(false),
142           templatename_(defaultTemplateName)
143 {}
144
145
146 namespace {
147
148 template <typename T>
149 void clearIfNotFound(T & data, external::TransformID value,
150                      vector<external::TransformID> const & ids)
151 {
152         typedef vector<external::TransformID>::const_iterator
153                 const_iterator;
154
155         const_iterator it  = ids.begin();
156         const_iterator end = ids.end();
157         it = std::find(it, end, value);
158         if (it == end)
159                 data = T();
160 }
161
162 } // namespace anon
163
164
165 void InsetExternalParams::settemplate(string const & name)
166 {
167         templatename_ = name;
168
169         external::TemplateManager const & etm =
170                 external::TemplateManager::get();
171         external::Template const * const et = etm.getTemplateByName(name);
172         if (!et)
173                 // Be safe. Don't lose data.
174                 return;
175
176         // Ascertain which transforms the template supports.
177         // Empty all those that it doesn't.
178         vector<external::TransformID> const & ids = et->transformIds;
179         clearIfNotFound(clipdata,     external::Clip,   ids);
180         clearIfNotFound(extradata,    external::Extra,  ids);
181         clearIfNotFound(resizedata,   external::Resize, ids);
182         clearIfNotFound(rotationdata, external::Rotate, ids);
183 }
184
185
186 void InsetExternalParams::write(Buffer const & buffer, ostream & os) const
187 {
188         os << "External\n"
189            << "\ttemplate " << templatename() << '\n';
190
191         if (!filename.empty())
192                 os << "\tfilename "
193                    << filename.outputFilename(buffer.filePath())
194                    << '\n';
195
196         if (display != defaultDisplayType)
197                 os << "\tdisplay "
198                    << external::displayTranslator().find(display)
199                    << '\n';
200
201         if (lyxscale != defaultLyxScale)
202                 os << "\tlyxscale " << tostr(lyxscale) << '\n';
203
204         if (draft)
205                 os << "\tdraft\n";
206
207         if (!clipdata.bbox.empty())
208                 os << "\tboundingBox " << clipdata.bbox << '\n';
209         if (clipdata.clip)
210                 os << "\tclip\n";
211
212         external::ExtraData::const_iterator it  = extradata.begin();
213         external::ExtraData::const_iterator end = extradata.end();
214         for (; it != end; ++it) {
215                 if (!it->second.empty())
216                         os << "\textra " << it->first << " \""
217                            << it->second << "\"\n";
218         }
219
220         if (!rotationdata.no_rotation()) {
221                 os << "\trotateAngle " << rotationdata.angle() << '\n';
222                 if (rotationdata.origin() != external::RotationData::DEFAULT)
223                         os << "\trotateOrigin "
224                            << rotationdata.originString() << '\n';
225         }
226
227         if (!resizedata.no_resize()) {
228                 using support::float_equal;
229
230                 if (!float_equal(resizedata.scale, 0.0, 0.05)) {
231                         if (!float_equal(resizedata.scale, 100.0, 0.05))
232                                 os << "\tscale "
233                                    << resizedata.scale << '\n';
234                 } else {
235                         if (!resizedata.width.zero())
236                                 os << "\twidth "
237                                    << resizedata.width.asString() << '\n';
238                         if (!resizedata.height.zero())
239                                 os << "\theight "
240                                    << resizedata.height.asString() << '\n';
241                 }
242                 if (resizedata.keepAspectRatio)
243                         os << "\tkeepAspectRatio\n";
244         }
245 }
246
247
248 bool InsetExternalParams::read(Buffer const & buffer, LyXLex & lex)
249 {
250         enum ExternalTags {
251                 EX_TEMPLATE = 1,
252                 EX_FILENAME,
253                 EX_DISPLAY,
254                 EX_LYXSCALE,
255                 EX_DRAFT,
256                 EX_BOUNDINGBOX,
257                 EX_CLIP,
258                 EX_EXTRA,
259                 EX_HEIGHT,
260                 EX_KEEPASPECTRATIO,
261                 EX_ROTATEANGLE,
262                 EX_ROTATEORIGIN,
263                 EX_SCALE,
264                 EX_WIDTH,
265                 EX_END
266         };
267
268         keyword_item external_tags[] = {
269                 { "\\end_inset",     EX_END },
270                 { "boundingBox",     EX_BOUNDINGBOX },
271                 { "clip",            EX_CLIP },
272                 { "display",         EX_DISPLAY},
273                 { "draft",           EX_DRAFT},
274                 { "extra",           EX_EXTRA },
275                 { "filename",        EX_FILENAME},
276                 { "height",          EX_HEIGHT },
277                 { "keepAspectRatio", EX_KEEPASPECTRATIO },
278                 { "lyxscale",        EX_LYXSCALE},
279                 { "rotateAngle",     EX_ROTATEANGLE },
280                 { "rotateOrigin",    EX_ROTATEORIGIN },
281                 { "scale",           EX_SCALE },
282                 { "template",        EX_TEMPLATE },
283                 { "width",           EX_WIDTH }
284         };
285
286         pushpophelper pph(lex, external_tags, EX_END);
287
288         bool found_end  = false;
289         bool read_error = false;
290
291         while (lex.isOK()) {
292                 switch (lex.lex()) {
293                 case EX_TEMPLATE:
294                         lex.next();
295                         templatename_ = lex.getString();
296                         break;
297
298                 case EX_FILENAME: {
299                         lex.next();
300                         string const name = lex.getString();
301                         filename.set(name, buffer.filePath());
302                         break;
303                 }
304
305                 case EX_DISPLAY: {
306                         lex.next();
307                         string const name = lex.getString();
308                         display = external::displayTranslator().find(name);
309                         break;
310                 }
311
312                 case EX_LYXSCALE:
313                         lex.next();
314                         lyxscale = lex.getInteger();
315                         break;
316
317                 case EX_DRAFT:
318                         draft = true;
319                         break;
320
321                 case EX_BOUNDINGBOX:
322                         lex.next();
323                         clipdata.bbox.xl = lex.getInteger();
324                         lex.next();
325                         clipdata.bbox.yb = lex.getInteger();
326                         lex.next();
327                         clipdata.bbox.xr = lex.getInteger();
328                         lex.next();
329                         clipdata.bbox.yt = lex.getInteger();
330                         break;
331
332                 case EX_CLIP:
333                         clipdata.clip = true;
334                         break;
335
336                 case EX_EXTRA: {
337                         lex.next();
338                         string const name = lex.getString();
339                         lex.next();
340                         extradata.set(name, lex.getString());
341                         break;
342                 }
343
344                 case EX_HEIGHT:
345                         lex.next();
346                         resizedata.height = LyXLength(lex.getString());
347                         break;
348
349                 case EX_KEEPASPECTRATIO:
350                         resizedata.keepAspectRatio = true;
351                         break;
352
353                 case EX_ROTATEANGLE:
354                         lex.next();
355                         rotationdata.angle(lex.getFloat());
356                         break;
357
358                 case EX_ROTATEORIGIN:
359                         lex.next();
360                         rotationdata.origin(lex.getString());
361                         break;
362
363                 case EX_SCALE:
364                         lex.next();
365                         resizedata.scale = lex.getFloat();
366                         break;
367
368                 case EX_WIDTH:
369                         lex.next();
370                         resizedata.width = LyXLength(lex.getString());
371                         break;
372
373                 case EX_END:
374                         found_end = true;
375                         break;
376
377                 default:
378                         lex.printError("ExternalInset::read: "
379                                        "Wrong tag: $$Token");
380                         read_error = true;
381                         break;
382                 }
383
384                 if (found_end || read_error)
385                         break;
386         }
387
388         if (!found_end)
389                 lex.printError("ExternalInsetParams::read: Missing \\end_inset.");
390
391         // This is a trick to make sure that the data are self-consistent.
392         settemplate(templatename_);
393
394         if (lyxerr.debugging(Debug::EXTERNAL)) {
395                 lyxerr  << "InsetExternalParams::read:\n";
396                 write(buffer, lyxerr);
397         }
398
399         return !read_error;
400 }
401
402
403 InsetExternal::InsetExternal()
404         : renderer_(new RenderButton)
405 {}
406
407
408 InsetExternal::InsetExternal(InsetExternal const & other)
409         : InsetOld(other),
410           boost::signals::trackable(),
411           params_(other.params_),
412           renderer_(other.renderer_->clone())
413 {
414         if (renderer_->asMonitoredPreview() != 0) {
415                 RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
416                 ptr->connect(boost::bind(&InsetExternal::statusChanged, this));
417                 ptr->fileChanged(boost::bind(&InsetExternal::fileChanged, this));
418
419         } else if (renderer_->asGraphic() != 0 ) {
420                 RenderGraphic * const ptr = renderer_->asGraphic();
421                 ptr->connect(boost::bind(&InsetExternal::statusChanged, this));
422         }
423 }
424
425
426 auto_ptr<InsetBase> InsetExternal::clone() const
427 {
428         return auto_ptr<InsetBase>(new InsetExternal(*this));
429 }
430
431
432 InsetExternal::~InsetExternal()
433 {
434         InsetExternalMailer(*this).hideDialog();
435 }
436
437
438 void InsetExternal::statusChanged() const
439 {
440         LyX::cref().updateInset(this);
441 }
442
443
444 void InsetExternal::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
445 {
446         switch (cmd.action) {
447
448         case LFUN_EXTERNAL_EDIT: {
449                 Buffer const & buffer = *cur.bv().buffer();
450                 InsetExternalParams p;
451                 InsetExternalMailer::string2params(cmd.argument, buffer, p);
452                 external::editExternal(p, buffer);
453                 break;
454         }
455
456         case LFUN_INSET_MODIFY: {
457                 Buffer const & buffer = *cur.bv().buffer();
458                 InsetExternalParams p;
459                 InsetExternalMailer::string2params(cmd.argument, buffer, p);
460                 setParams(p, buffer);
461                 cur.bv().update();
462                 break;
463         }
464
465         case LFUN_INSET_DIALOG_UPDATE:
466                 InsetExternalMailer(*this).updateDialog(&cur.bv());
467                 break;
468
469         case LFUN_MOUSE_RELEASE:
470                 InsetExternalMailer(*this).showDialog(&cur.bv());
471                 break;
472
473         default:
474                 InsetOld::dispatch(cur, cmd);
475         }
476 }
477
478
479 void InsetExternal::edit(LCursor & cur, bool)
480 {
481         InsetExternalMailer(*this).showDialog(&cur.bv());
482 }
483
484
485 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
486 {
487         renderer_->metrics(mi, dim);
488         dim_ = dim;
489 }
490
491
492 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
493 {
494         setPosCache(pi, x, y);
495         renderer_->draw(pi, x, y);
496 }
497
498
499 namespace {
500
501 enum RenderType {
502         RENDERBUTTON,
503         RENDERGRAPHIC,
504         RENDERPREVIEW
505 };
506
507
508 RenderType getRenderType(InsetExternalParams const & p)
509 {
510         if (!external::getTemplatePtr(p) ||
511             p.filename.empty() ||
512             p.display == external::NoDisplay)
513                 return RENDERBUTTON;
514
515         if (p.display == external::PreviewDisplay) {
516                 if (RenderPreview::activated())
517                         return RENDERPREVIEW;
518                 return RENDERBUTTON;
519         }
520
521         if (p.display == external::DefaultDisplay &&
522             lyxrc.display_graphics == graphics::NoDisplay)
523                 return RENDERBUTTON;
524         return RENDERGRAPHIC;
525 }
526
527
528 graphics::Params get_grfx_params(InsetExternalParams const & eparams)
529 {
530         graphics::Params gparams;
531
532         gparams.filename = eparams.filename.absFilename();
533         gparams.scale = eparams.lyxscale;
534         if (eparams.clipdata.clip)
535                 gparams.bb = eparams.clipdata.bbox;
536         gparams.angle = eparams.rotationdata.angle();
537
538         switch (eparams.display) {
539         case external::DefaultDisplay:
540                 gparams.display = graphics::DefaultDisplay;
541                 break;
542         case external::MonochromeDisplay:
543                 gparams.display = graphics::MonochromeDisplay;
544                 break;
545         case external::GrayscaleDisplay:
546                 gparams.display = graphics::GrayscaleDisplay;
547                 break;
548         case external::ColorDisplay:
549                 gparams.display = graphics::ColorDisplay;
550                 break;
551         case external::NoDisplay:
552                 gparams.display = graphics::NoDisplay;
553                 break;
554         default:
555                 BOOST_ASSERT(false);
556         }
557         if (gparams.display == graphics::DefaultDisplay)
558                 gparams.display = lyxrc.display_graphics;
559         // Override the above if we're not using a gui
560         if (!lyx_gui::use_gui)
561                 gparams.display = graphics::NoDisplay;
562
563         return gparams;
564 }
565
566
567 string const getScreenLabel(InsetExternalParams const & params,
568                             Buffer const & buffer)
569 {
570         external::Template const * const ptr =
571                 external::getTemplatePtr(params);
572         if (!ptr)
573                 return support::bformat(_("External template %1$s is not installed"),
574                                         params.templatename());
575         return external::doSubstitution(params, buffer, ptr->guiName);
576 }
577
578 void add_preview(RenderMonitoredPreview &, InsetExternal const &, Buffer const &);
579
580 } // namespace anon
581
582
583 InsetExternalParams const & InsetExternal::params() const
584 {
585         return params_;
586 }
587
588
589 void InsetExternal::setParams(InsetExternalParams const & p,
590                               Buffer const & buffer)
591 {
592         params_ = p;
593
594         // Subsequent calls to the InsetExternal::Params default constructor
595         // will use this.
596         defaultTemplateName = params_.templatename();
597
598         switch (getRenderType(params_)) {
599         case RENDERBUTTON: {
600                 RenderButton * button_ptr = renderer_->asButton();
601                 if (!button_ptr) {
602                         renderer_.reset(new RenderButton);
603                         button_ptr = renderer_->asButton();
604                 }
605
606                 button_ptr->update(getScreenLabel(params_, buffer), true);
607                 break;
608
609         } case RENDERGRAPHIC: {
610                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
611                 if (!graphic_ptr) {
612                         renderer_.reset(new RenderGraphic);
613                         graphic_ptr = renderer_->asGraphic();
614                         graphic_ptr->connect(
615                                 boost::bind(&InsetExternal::statusChanged, this));
616                 }
617
618                 graphic_ptr->update(get_grfx_params(params_));
619
620                 break;
621
622         } case RENDERPREVIEW: {
623                 RenderMonitoredPreview * preview_ptr =
624                         renderer_->asMonitoredPreview();
625                 if (!preview_ptr) {
626                         renderer_.reset(new RenderMonitoredPreview);
627                         preview_ptr = renderer_->asMonitoredPreview();
628                         preview_ptr->connect(
629                                 boost::bind(&InsetExternal::statusChanged, this));
630                         preview_ptr->fileChanged(
631                                 boost::bind(&InsetExternal::fileChanged, this));
632                 }
633
634                 if (preview_ptr->monitoring())
635                         preview_ptr->stopMonitoring();
636                 add_preview(*preview_ptr, *this, buffer);
637
638                 break;
639         }
640         }
641 }
642
643
644 void InsetExternal::fileChanged() const
645 {
646         Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
647         if (!buffer_ptr)
648                 return;
649
650         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
651         BOOST_ASSERT(ptr);
652
653         Buffer const & buffer = *buffer_ptr;
654         ptr->removePreview(buffer);
655         add_preview(*ptr, *this, buffer);
656         ptr->startLoading(buffer);
657 }
658
659
660 void InsetExternal::write(Buffer const & buffer, ostream & os) const
661 {
662         params_.write(buffer, os);
663 }
664
665
666 void InsetExternal::read(Buffer const & buffer, LyXLex & lex)
667 {
668         InsetExternalParams params;
669         if (params.read(buffer, lex))
670                 setParams(params, buffer);
671 }
672
673
674 int InsetExternal::latex(Buffer const & buf, ostream & os,
675                          OutputParams const & runparams) const
676 {
677         if (params_.draft) {
678                 os << "\\fbox{\\ttfamily{}"
679                    << params_.filename.outputFilename(buf.filePath())
680                    << "}\n";
681                 return 1;
682         }
683
684         // "nice" means that the buffer is exported to LaTeX format but not
685         // run through the LaTeX compiler.
686         // If we're running through the LaTeX compiler, we should write the
687         // generated files in the bufer's temporary directory.
688         bool const external_in_tmpdir =
689                 lyxrc.use_tempdir && !buf.temppath().empty() && !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 }