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