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