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