]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInclude.cpp
InsetListings: change the interface of diaplay function and allow AlignLeft. Applied...
[lyx.git] / src / insets / InsetInclude.cpp
1 /**
2  * \file InsetInclude.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetInclude.h"
14
15 #include "Buffer.h"
16 #include "buffer_funcs.h"
17 #include "BufferList.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "debug.h"
22 #include "DispatchResult.h"
23 #include "Exporter.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "gettext.h"
27 #include "LaTeXFeatures.h"
28 #include "LyX.h"
29 #include "LyXRC.h"
30 #include "Lexer.h"
31 #include "MetricsInfo.h"
32 #include "OutputParams.h"
33 #include "TocBackend.h"
34
35 #include "frontends/alert.h"
36 #include "frontends/Painter.h"
37
38 #include "graphics/PreviewImage.h"
39 #include "graphics/PreviewLoader.h"
40
41 #include "insets/RenderPreview.h"
42 #include "insets/InsetListingsParams.h"
43
44 #include "support/filetools.h"
45 #include "support/lstrings.h" // contains
46 #include "support/lyxalgo.h"
47 #include "support/lyxlib.h"
48 #include "support/convert.h"
49
50 #include <boost/bind.hpp>
51 #include <boost/filesystem/operations.hpp>
52
53
54 namespace lyx {
55
56 using support::addName;
57 using support::absolutePath;
58 using support::bformat;
59 using support::changeExtension;
60 using support::contains;
61 using support::copy;
62 using support::DocFileName;
63 using support::FileName;
64 using support::getFileContents;
65 using support::isFileReadable;
66 using support::isLyXFilename;
67 using support::latex_path;
68 using support::makeAbsPath;
69 using support::makeDisplayPath;
70 using support::makeRelPath;
71 using support::onlyFilename;
72 using support::onlyPath;
73 using support::subst;
74 using support::sum;
75
76 using std::endl;
77 using std::string;
78 using std::auto_ptr;
79 using std::istringstream;
80 using std::ostream;
81 using std::ostringstream;
82
83 namespace Alert = frontend::Alert;
84 namespace fs = boost::filesystem;
85
86
87 namespace {
88
89 docstring const uniqueID()
90 {
91         static unsigned int seed = 1000;
92         return "file" + convert<docstring>(++seed);
93 }
94
95 } // namespace anon
96
97
98 InsetInclude::InsetInclude(InsetCommandParams const & p)
99         : params_(p), include_label(uniqueID()),
100           preview_(new RenderMonitoredPreview(this)),
101           set_label_(false)
102 {
103         preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
104 }
105
106
107 InsetInclude::InsetInclude(InsetInclude const & other)
108         : Inset(other),
109           params_(other.params_),
110           include_label(other.include_label),
111           preview_(new RenderMonitoredPreview(this)),
112           set_label_(false)
113 {
114         preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
115 }
116
117
118 InsetInclude::~InsetInclude()
119 {
120         InsetIncludeMailer(*this).hideDialog();
121 }
122
123
124 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
125 {
126         switch (cmd.action) {
127
128         case LFUN_INSET_MODIFY: {
129                 InsetCommandParams p("include");
130                 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), p);
131                 if (!p.getCmdName().empty()) {
132                         set(p, cur.buffer());
133                         cur.buffer().updateBibfilesCache();
134                 } else
135                         cur.noUpdate();
136                 break;
137         }
138
139         case LFUN_INSET_DIALOG_UPDATE:
140                 InsetIncludeMailer(*this).updateDialog(&cur.bv());
141                 break;
142
143         case LFUN_MOUSE_RELEASE:
144                 InsetIncludeMailer(*this).showDialog(&cur.bv());
145                 break;
146
147         default:
148                 Inset::doDispatch(cur, cmd);
149                 break;
150         }
151 }
152
153
154 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
155                 FuncStatus & flag) const
156 {
157         switch (cmd.action) {
158
159         case LFUN_INSET_MODIFY:
160         case LFUN_INSET_DIALOG_UPDATE:
161                 flag.enabled(true);
162                 return true;
163
164         default:
165                 return Inset::getStatus(cur, cmd, flag);
166         }
167 }
168
169
170 InsetCommandParams const & InsetInclude::params() const
171 {
172         return params_;
173 }
174
175
176 namespace {
177
178 /// the type of inclusion
179 enum Types {
180         INCLUDE = 0,
181         VERB = 1,
182         INPUT = 2,
183         VERBAST = 3,
184         LISTINGS = 4,
185 };
186
187
188 Types type(InsetCommandParams const & params)
189 {
190         string const command_name = params.getCmdName();
191
192         if (command_name == "input")
193                 return INPUT;
194         if  (command_name == "verbatiminput")
195                 return VERB;
196         if  (command_name == "verbatiminput*")
197                 return VERBAST;
198         if  (command_name == "lstinputlisting")
199                 return LISTINGS;
200         return INCLUDE;
201 }
202
203
204 bool isVerbatim(InsetCommandParams const & params)
205 {
206         string const command_name = params.getCmdName();
207         return command_name == "verbatiminput" ||
208                 command_name == "verbatiminput*";
209 }
210
211
212 bool isListings(InsetCommandParams const & params)
213 {
214         return params.getCmdName() == "lstinputlisting";
215 }
216
217
218 string const masterFilename(Buffer const & buffer)
219 {
220         return buffer.getMasterBuffer()->fileName();
221 }
222
223
224 string const parentFilename(Buffer const & buffer)
225 {
226         return buffer.fileName();
227 }
228
229
230 FileName const includedFilename(Buffer const & buffer,
231                               InsetCommandParams const & params)
232 {
233         return makeAbsPath(to_utf8(params["filename"]),
234                            onlyPath(parentFilename(buffer)));
235 }
236
237
238 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
239
240 } // namespace anon
241
242
243 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
244 {
245         params_ = p;
246         set_label_ = false;
247
248         if (preview_->monitoring())
249                 preview_->stopMonitoring();
250
251         if (type(params_) == INPUT)
252                 add_preview(*preview_, *this, buffer);
253 }
254
255
256 auto_ptr<Inset> InsetInclude::doClone() const
257 {
258         return auto_ptr<Inset>(new InsetInclude(*this));
259 }
260
261
262 void InsetInclude::write(Buffer const &, ostream & os) const
263 {
264         write(os);
265 }
266
267
268 void InsetInclude::write(ostream & os) const
269 {
270         os << "Include " << to_utf8(params_.getCommand()) << '\n'
271            << "preview " << convert<string>(params_.preview()) << '\n';
272 }
273
274
275 void InsetInclude::read(Buffer const &, Lexer & lex)
276 {
277         read(lex);
278 }
279
280
281 void InsetInclude::read(Lexer & lex)
282 {
283         if (lex.isOK()) {
284                 lex.eatLine();
285                 string const command = lex.getString();
286                 params_.scanCommand(command);
287         }
288         string token;
289         while (lex.isOK()) {
290                 lex.next();
291                 token = lex.getString();
292                 if (token == "\\end_inset")
293                         break;
294                 if (token == "preview") {
295                         lex.next();
296                         params_.preview(lex.getBool());
297                 } else
298                         lex.printError("Unknown parameter name `$$Token' for command " + params_.getCmdName());
299         }
300         if (token != "\\end_inset") {
301                 lex.printError("Missing \\end_inset at this point. "
302                                "Read: `$$Token'");
303         }
304 }
305
306
307 docstring const InsetInclude::getScreenLabel(Buffer const & buf) const
308 {
309         docstring temp;
310
311         switch (type(params_)) {
312                 case INPUT:
313                         temp += buf.B_("Input");
314                         break;
315                 case VERB:
316                         temp += buf.B_("Verbatim Input");
317                         break;
318                 case VERBAST:
319                         temp += buf.B_("Verbatim Input*");
320                         break;
321                 case INCLUDE:
322                         temp += buf.B_("Include");
323                         break;
324                 case LISTINGS:
325                         temp += buf.B_("Program Listing");
326                         break;
327         }
328
329         temp += ": ";
330
331         if (params_["filename"].empty())
332                 temp += "???";
333         else
334                 temp += from_utf8(onlyFilename(to_utf8(params_["filename"])));
335
336         return temp;
337 }
338
339
340 namespace {
341
342 /// return the child buffer if the file is a LyX doc and is loaded
343 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
344 {
345         if (isVerbatim(params) || isListings(params))
346                 return 0;
347
348         string const included_file = includedFilename(buffer, params).absFilename();
349         if (!isLyXFilename(included_file))
350                 return 0;
351
352         return theBufferList().getBuffer(included_file);
353 }
354
355
356 /// return true if the file is or got loaded.
357 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
358 {
359         if (isVerbatim(params) || isListings(params))
360                 return false;
361
362         FileName const included_file = includedFilename(buffer, params);
363         if (!isLyXFilename(included_file.absFilename()))
364                 return false;
365
366         Buffer * buf = theBufferList().getBuffer(included_file.absFilename());
367         if (!buf) {
368                 // the readonly flag can/will be wrong, not anymore I think.
369                 if (!fs::exists(included_file.toFilesystemEncoding()))
370                         return false;
371                 buf = theBufferList().newBuffer(included_file.absFilename());
372                 if (!loadLyXFile(buf, included_file))
373                         return false;
374         }
375         if (buf)
376                 buf->setParentName(parentFilename(buffer));
377         return buf != 0;
378 }
379
380
381 } // namespace anon
382
383
384 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
385                         OutputParams const & runparams) const
386 {
387         string incfile(to_utf8(params_["filename"]));
388
389         // Do nothing if no file name has been specified
390         if (incfile.empty())
391                 return 0;
392
393         FileName const included_file(includedFilename(buffer, params_));
394         Buffer const * const m_buffer = buffer.getMasterBuffer();
395
396         // if incfile is relative, make it relative to the master
397         // buffer directory.
398         if (!absolutePath(incfile)) {
399                 // FIXME UNICODE
400                 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
401                                               from_utf8(m_buffer->filePath())));
402         }
403
404         // write it to a file (so far the complete file)
405         string const exportfile = changeExtension(incfile, ".tex");
406         string const mangled = DocFileName(changeExtension(included_file.absFilename(),
407                                                         ".tex")).mangledFilename();
408         FileName const writefile(makeAbsPath(mangled, m_buffer->temppath()));
409
410         if (!runparams.nice)
411                 incfile = mangled;
412         LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
413         LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
414         LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
415
416         if (runparams.inComment || runparams.dryrun)
417                 // Don't try to load or copy the file
418                 ;
419         else if (loadIfNeeded(buffer, params_)) {
420                 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
421
422                 if (tmp->params().textclass != m_buffer->params().textclass) {
423                         // FIXME UNICODE
424                         docstring text = bformat(_("Included file `%1$s'\n"
425                                                 "has textclass `%2$s'\n"
426                                                              "while parent file has textclass `%3$s'."),
427                                               makeDisplayPath(included_file.absFilename()),
428                                               from_utf8(tmp->params().getTextClass().name()),
429                                               from_utf8(m_buffer->params().getTextClass().name()));
430                         Alert::warning(_("Different textclasses"), text);
431                         //return 0;
432                 }
433
434                 tmp->markDepClean(m_buffer->temppath());
435
436 #ifdef WITH_WARNINGS
437 #warning handle non existing files
438 #warning Second argument is irrelevant!
439 // since only_body is true, makeLaTeXFile will not look at second
440 // argument. Should we set it to string(), or should makeLaTeXFile
441 // make use of it somehow? (JMarc 20031002)
442 #endif
443                 // The included file might be written in a different encoding
444                 Encoding const * const oldEnc = runparams.encoding;
445                 runparams.encoding = &tmp->params().encoding();
446                 tmp->makeLaTeXFile(writefile,
447                                    onlyPath(masterFilename(buffer)),
448                                    runparams, false);
449                 runparams.encoding = oldEnc;
450         } else {
451                 // Copy the file to the temp dir, so that .aux files etc.
452                 // are not created in the original dir. Files included by
453                 // this file will be found via input@path, see ../Buffer.cpp.
454                 unsigned long const checksum_in  = sum(included_file);
455                 unsigned long const checksum_out = sum(writefile);
456
457                 if (checksum_in != checksum_out) {
458                         if (!copy(included_file, writefile)) {
459                                 // FIXME UNICODE
460                                 LYXERR(Debug::LATEX)
461                                         << to_utf8(bformat(_("Could not copy the file\n%1$s\n"
462                                                                   "into the temporary directory."),
463                                                    from_utf8(included_file.absFilename())))
464                                         << endl;
465                                 return 0;
466                         }
467                 }
468         }
469
470         string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
471                         "latex" : "pdflatex";
472         if (isVerbatim(params_)) {
473                 incfile = latex_path(incfile);
474                 // FIXME UNICODE
475                 os << '\\' << from_ascii(params_.getCmdName()) << '{'
476                    << from_utf8(incfile) << '}';
477         } else if (type(params_) == INPUT) {
478                 runparams.exportdata->addExternalFile(tex_format, writefile,
479                                                       exportfile);
480
481                 // \input wants file with extension (default is .tex)
482                 if (!isLyXFilename(included_file.absFilename())) {
483                         incfile = latex_path(incfile);
484                         // FIXME UNICODE
485                         os << '\\' << from_ascii(params_.getCmdName())
486                            << '{' << from_utf8(incfile) << '}';
487                 } else {
488                 incfile = changeExtension(incfile, ".tex");
489                 incfile = latex_path(incfile);
490                         // FIXME UNICODE
491                         os << '\\' << from_ascii(params_.getCmdName())
492                            << '{' << from_utf8(incfile) <<  '}';
493                 }
494         } else if (type(params_) == LISTINGS) {
495                 os << '\\' << from_ascii(params_.getCmdName());
496                 string opt = params_.getOptions();
497                 // opt is set in QInclude dialog and should have passed validation.
498                 InsetListingsParams params(opt);
499                 if (!params.params().empty())
500                         os << "[" << from_utf8(params.encodedString()) << "]";
501                 os << '{'  << from_utf8(incfile) << '}';
502         } else {
503                 runparams.exportdata->addExternalFile(tex_format, writefile,
504                                                       exportfile);
505
506                 // \include don't want extension and demands that the
507                 // file really have .tex
508                 incfile = changeExtension(incfile, string());
509                 incfile = latex_path(incfile);
510                 // FIXME UNICODE
511                 os << '\\' << from_ascii(params_.getCmdName()) << '{'
512                    << from_utf8(incfile) << '}';
513         }
514
515         return 0;
516 }
517
518
519 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
520                             OutputParams const &) const
521 {
522         if (isVerbatim(params_) || isListings(params_)) {
523                 os << '[' << getScreenLabel(buffer) << '\n';
524                 // FIXME: We don't know the encoding of the file
525                 docstring const str =
526                      from_utf8(getFileContents(includedFilename(buffer, params_)));
527                 os << str;
528                 os << "\n]";
529                 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
530         } else {
531                 docstring const str = '[' + getScreenLabel(buffer) + ']';
532                 os << str;
533                 return str.size();
534         }
535 }
536
537
538 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
539                           OutputParams const & runparams) const
540 {
541         string incfile = to_utf8(params_["filename"]);
542
543         // Do nothing if no file name has been specified
544         if (incfile.empty())
545                 return 0;
546
547         string const included_file = includedFilename(buffer, params_).absFilename();
548
549         // write it to a file (so far the complete file)
550         string const exportfile = changeExtension(incfile, ".sgml");
551         DocFileName writefile(changeExtension(included_file, ".sgml"));
552
553         if (loadIfNeeded(buffer, params_)) {
554                 Buffer * tmp = theBufferList().getBuffer(included_file);
555
556                 string const mangled = writefile.mangledFilename();
557                 writefile = makeAbsPath(mangled,
558                                         buffer.getMasterBuffer()->temppath());
559                 if (!runparams.nice)
560                         incfile = mangled;
561
562                 LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
563                 LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
564                 LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
565
566                 tmp->makeDocBookFile(writefile, runparams, true);
567         }
568
569         runparams.exportdata->addExternalFile("docbook", writefile,
570                                               exportfile);
571         runparams.exportdata->addExternalFile("docbook-xml", writefile,
572                                               exportfile);
573
574         if (isVerbatim(params_) || isListings(params_)) {
575                 os << "<inlinegraphic fileref=\""
576                    << '&' << include_label << ';'
577                    << "\" format=\"linespecific\">";
578         } else
579                 os << '&' << include_label << ';';
580
581         return 0;
582 }
583
584
585 void InsetInclude::validate(LaTeXFeatures & features) const
586 {
587         string incfile(to_utf8(params_["filename"]));
588         string writefile;
589
590         Buffer const & buffer = features.buffer();
591
592         string const included_file = includedFilename(buffer, params_).absFilename();
593
594         if (isLyXFilename(included_file))
595                 writefile = changeExtension(included_file, ".sgml");
596         else
597                 writefile = included_file;
598
599         if (!features.runparams().nice && !isVerbatim(params_) && !isListings(params_)) {
600                 incfile = DocFileName(writefile).mangledFilename();
601                 writefile = makeAbsPath(incfile,
602                                         buffer.getMasterBuffer()->temppath()).absFilename();
603         }
604
605         features.includeFile(include_label, writefile);
606
607         if (isVerbatim(params_))
608                 features.require("verbatim");
609         else if (isListings(params_))
610                 features.require("listings");
611
612         // Here we must do the fun stuff...
613         // Load the file in the include if it needs
614         // to be loaded:
615         if (loadIfNeeded(buffer, params_)) {
616                 // a file got loaded
617                 Buffer * const tmp = theBufferList().getBuffer(included_file);
618                 if (tmp) {
619                         // We must temporarily change features.buffer,
620                         // otherwise it would always be the master buffer,
621                         // and nested includes would not work.
622                         features.setBuffer(*tmp);
623                         tmp->validate(features);
624                         features.setBuffer(buffer);
625                 }
626         }
627 }
628
629
630 void InsetInclude::getLabelList(Buffer const & buffer,
631                                 std::vector<docstring> & list) const
632 {
633         if (loadIfNeeded(buffer, params_)) {
634                 string const included_file = includedFilename(buffer, params_).absFilename();
635                 Buffer * tmp = theBufferList().getBuffer(included_file);
636                 tmp->setParentName("");
637                 tmp->getLabelList(list);
638                 tmp->setParentName(parentFilename(buffer));
639         }
640 }
641
642
643 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
644                 std::vector<std::pair<string, docstring> > & keys) const
645 {
646         if (loadIfNeeded(buffer, params_)) {
647                 string const included_file = includedFilename(buffer, params_).absFilename();
648                 Buffer * tmp = theBufferList().getBuffer(included_file);
649                 tmp->setParentName("");
650                 tmp->fillWithBibKeys(keys);
651                 tmp->setParentName(parentFilename(buffer));
652         }
653 }
654
655
656 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
657 {
658         Buffer * const tmp = getChildBuffer(buffer, params_);
659         if (tmp) {
660                 tmp->setParentName("");
661                 tmp->updateBibfilesCache();
662                 tmp->setParentName(parentFilename(buffer));
663         }
664 }
665
666
667 std::vector<FileName> const &
668 InsetInclude::getBibfilesCache(Buffer const & buffer) const
669 {
670         Buffer * const tmp = getChildBuffer(buffer, params_);
671         if (tmp) {
672                 tmp->setParentName("");
673                 std::vector<FileName> const & cache = tmp->getBibfilesCache();
674                 tmp->setParentName(parentFilename(buffer));
675                 return cache;
676         }
677         static std::vector<FileName> const empty;
678         return empty;
679 }
680
681
682 bool InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
683 {
684         BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
685
686         bool use_preview = false;
687         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
688                 graphics::PreviewImage const * pimage =
689                         preview_->getPreviewImage(*mi.base.bv->buffer());
690                 use_preview = pimage && pimage->image();
691         }
692
693         if (use_preview) {
694                 preview_->metrics(mi, dim);
695         } else {
696                 if (!set_label_) {
697                         set_label_ = true;
698                         button_.update(getScreenLabel(*mi.base.bv->buffer()),
699                                        true);
700                 }
701                 button_.metrics(mi, dim);
702         }
703
704         Box b(0, dim.wid, -dim.asc, dim.des);
705         button_.setBox(b);
706
707         bool const changed = dim_ != dim;
708         dim_ = dim;
709         return changed;
710 }
711
712
713 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
714 {
715         setPosCache(pi, x, y);
716
717         BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
718
719         bool use_preview = false;
720         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
721                 graphics::PreviewImage const * pimage =
722                         preview_->getPreviewImage(*pi.base.bv->buffer());
723                 use_preview = pimage && pimage->image();
724         }
725
726         if (use_preview)
727                 preview_->draw(pi, x, y);
728         else
729                 button_.draw(pi, x, y);
730 }
731
732
733 Inset::DisplayType InsetInclude::display() const
734 {
735         return type(params_) == INPUT ? Inline : AlignCenter;
736 }
737
738
739
740 //
741 // preview stuff
742 //
743
744 void InsetInclude::fileChanged() const
745 {
746         Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
747         if (!buffer_ptr)
748                 return;
749
750         Buffer const & buffer = *buffer_ptr;
751         preview_->removePreview(buffer);
752         add_preview(*preview_.get(), *this, buffer);
753         preview_->startLoading(buffer);
754 }
755
756
757 namespace {
758
759 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
760 {
761         FileName const included_file = includedFilename(buffer, params);
762
763         return type(params) == INPUT && params.preview() &&
764                 isFileReadable(included_file);
765 }
766
767
768 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
769 {
770         odocstringstream os;
771         // We don't need to set runparams.encoding since this will be done
772         // by latex() anyway.
773         OutputParams runparams(0);
774         runparams.flavor = OutputParams::LATEX;
775         inset.latex(buffer, os, runparams);
776
777         return os.str();
778 }
779
780
781 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
782                  Buffer const & buffer)
783 {
784         InsetCommandParams const & params = inset.params();
785         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
786             preview_wanted(params, buffer)) {
787                 renderer.setAbsFile(includedFilename(buffer, params));
788                 docstring const snippet = latex_string(inset, buffer);
789                 renderer.addPreview(snippet, buffer);
790         }
791 }
792
793 } // namespace anon
794
795
796 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
797 {
798         Buffer const & buffer = ploader.buffer();
799         if (preview_wanted(params(), buffer)) {
800                 preview_->setAbsFile(includedFilename(buffer, params()));
801                 docstring const snippet = latex_string(*this, buffer);
802                 preview_->addPreview(snippet, ploader);
803         }
804 }
805
806
807 void InsetInclude::addToToc(TocList & toclist, Buffer const & buffer) const
808 {
809         Buffer const * const childbuffer = getChildBuffer(buffer, params_);
810         if (!childbuffer)
811                 return;
812
813         TocList const & childtoclist = childbuffer->tocBackend().tocs();
814         TocList::const_iterator it = childtoclist.begin();
815         TocList::const_iterator const end = childtoclist.end();
816         for(; it != end; ++it)
817                 toclist[it->first].insert(toclist[it->first].end(),
818                                 it->second.begin(), it->second.end());
819 }
820
821
822 void InsetInclude::updateLabels(Buffer const & buffer) const
823 {
824         Buffer const * const childbuffer = getChildBuffer(buffer, params_);
825         if (!childbuffer)
826                 return;
827
828         lyx::updateLabels(*childbuffer, true);
829 }
830
831
832 string const InsetIncludeMailer::name_("include");
833
834 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
835         : inset_(inset)
836 {}
837
838
839 string const InsetIncludeMailer::inset2string(Buffer const &) const
840 {
841         return params2string(inset_.params());
842 }
843
844
845 void InsetIncludeMailer::string2params(string const & in,
846                                        InsetCommandParams & params)
847 {
848         params.clear();
849         if (in.empty())
850                 return;
851
852         istringstream data(in);
853         Lexer lex(0,0);
854         lex.setStream(data);
855
856         string name;
857         lex >> name;
858         if (!lex || name != name_)
859                 return print_mailer_error("InsetIncludeMailer", in, 1, name_);
860
861         // This is part of the inset proper that is usually swallowed
862         // by Text::readInset
863         string id;
864         lex >> id;
865         if (!lex || id != "Include")
866                 return print_mailer_error("InsetIncludeMailer", in, 2, "Include");
867
868         InsetInclude inset(params);
869         inset.read(lex);
870         params = inset.params();
871 }
872
873
874 string const
875 InsetIncludeMailer::params2string(InsetCommandParams const & params)
876 {
877         InsetInclude inset(params);
878         ostringstream data;
879         data << name_ << ' ';
880         inset.write(data);
881         data << "\\end_inset\n";
882         return data.str();
883 }
884
885
886 } // namespace lyx