]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
more cursor dispatch
[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 DispatchResult
445 InsetExternal::priv_dispatch(LCursor & cur, FuncRequest const & 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                 return DispatchResult(true);
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                 return DispatchResult(true, true);
464         }
465
466         case LFUN_INSET_DIALOG_UPDATE:
467                 InsetExternalMailer(*this).updateDialog(&cur.bv());
468                 return DispatchResult(true, true);
469
470         case LFUN_MOUSE_RELEASE:
471                 InsetExternalMailer(*this).showDialog(&cur.bv());
472                 return DispatchResult(true, true);
473
474         default:
475                 return DispatchResult(false);
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 =
690                 lyxrc.use_tempdir && !buf.temppath().empty() && !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 }