]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInclude.cpp
Fix bug #12795
[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 Kimberly 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 "Converter.h"
22 #include "Cursor.h"
23 #include "Encoding.h"
24 #include "ErrorList.h"
25 #include "Exporter.h"
26 #include "Format.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "LaTeXFeatures.h"
30 #include "LayoutModuleList.h"
31 #include "LyX.h"
32 #include "MetricsInfo.h"
33 #include "output_plaintext.h"
34 #include "output_xhtml.h"
35 #include "texstream.h"
36 #include "TextClass.h"
37 #include "TocBackend.h"
38
39 #include "frontends/alert.h"
40 #include "frontends/Painter.h"
41
42 #include "graphics/PreviewImage.h"
43 #include "graphics/PreviewLoader.h"
44
45 #include "insets/InsetLabel.h"
46 #include "insets/InsetListingsParams.h"
47 #include "insets/RenderPreview.h"
48
49 #include "mathed/MacroTable.h"
50
51 #include "support/convert.h"
52 #include "support/debug.h"
53 #include "support/docstream.h"
54 #include "support/FileName.h"
55 #include "support/FileNameList.h"
56 #include "support/filetools.h"
57 #include "support/gettext.h"
58 #include "support/lassert.h"
59 #include "support/lstrings.h" // contains
60 #include "support/mutex.h"
61 #include "support/ExceptionMessage.h"
62
63 using namespace std;
64 using namespace lyx::support;
65
66 namespace lyx {
67
68 namespace Alert = frontend::Alert;
69
70
71 namespace {
72
73 docstring const uniqueID()
74 {
75         static unsigned int seed = 1000;
76         static Mutex mutex;
77         Mutex::Locker lock(&mutex);
78         return "file" + convert<docstring>(++seed);
79 }
80
81
82 /// the type of inclusion
83 enum Types {
84         INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
85 };
86
87
88 Types type(string const & s)
89 {
90         if (s == "input")
91                 return INPUT;
92         if (s == "verbatiminput")
93                 return VERB;
94         if (s == "verbatiminput*")
95                 return VERBAST;
96         if (s == "lstinputlisting" || s == "inputminted")
97                 return LISTINGS;
98         if (s == "include")
99                 return INCLUDE;
100         return NONE;
101 }
102
103
104 Types type(InsetCommandParams const & params)
105 {
106         return type(params.getCmdName());
107 }
108
109
110 bool isListings(InsetCommandParams const & params)
111 {
112         return type(params) == LISTINGS;
113 }
114
115
116 bool isVerbatim(InsetCommandParams const & params)
117 {
118         Types const t = type(params);
119         return t == VERB || t == VERBAST;
120 }
121
122
123 bool isInputOrInclude(InsetCommandParams const & params)
124 {
125         Types const t = type(params);
126         return t == INPUT || t == INCLUDE;
127 }
128
129
130 FileName const masterFileName(Buffer const & buffer)
131 {
132         return buffer.masterBuffer()->fileName();
133 }
134
135
136 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
137
138
139 string const parentFileName(Buffer const & buffer)
140 {
141         return buffer.absFileName();
142 }
143
144
145 FileName const includedFileName(Buffer const & buffer,
146                               InsetCommandParams const & params)
147 {
148         return makeAbsPath(ltrim(to_utf8(params["filename"])),
149                         onlyPath(parentFileName(buffer)));
150 }
151
152
153 InsetLabel * createLabel(Buffer * buf, docstring const & label_str)
154 {
155         if (label_str.empty())
156                 return nullptr;
157         InsetCommandParams icp(LABEL_CODE);
158         icp["name"] = label_str;
159         return new InsetLabel(buf, icp);
160 }
161
162
163 char_type replaceCommaInBraces(docstring & params)
164 {
165         // Code point from private use area
166         char_type private_char = 0xE000;
167         int count = 0;
168         for (char_type & c : params) {
169                 if (c == '{')
170                         ++count;
171                 else if (c == '}')
172                         --count;
173                 else if (c == ',' && count)
174                         c = private_char;
175         }
176         return private_char;
177 }
178
179 } // namespace
180
181
182 InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p)
183         : InsetCommand(buf, p), include_label(uniqueID()),
184           preview_(make_unique<RenderMonitoredPreview>(this)), failedtoload_(false),
185           label_(nullptr), child_buffer_(nullptr), file_exist_(false),
186           recursion_error_(false)
187 {
188         preview_->connect([this](){ fileChanged(); });
189
190         if (isListings(params())) {
191                 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
192                 label_ = createLabel(buffer_, from_utf8(listing_params.getParamValue("label")));
193         } else if (isInputOrInclude(params()) && buf)
194                 loadIfNeeded();
195 }
196
197
198 InsetInclude::InsetInclude(InsetInclude const & other)
199         : InsetCommand(other), include_label(other.include_label),
200           preview_(make_unique<RenderMonitoredPreview>(this)), failedtoload_(false),
201           label_(nullptr), child_buffer_(nullptr),
202           file_exist_(other.file_exist_),recursion_error_(other.recursion_error_)
203 {
204         preview_->connect([this](){ fileChanged(); });
205
206         if (other.label_)
207                 label_ = new InsetLabel(*other.label_);
208 }
209
210
211 InsetInclude::~InsetInclude()
212 {
213         delete label_;
214 }
215
216
217 void InsetInclude::setBuffer(Buffer & buffer)
218 {
219         InsetCommand::setBuffer(buffer);
220         if (label_)
221                 label_->setBuffer(buffer);
222 }
223
224
225 void InsetInclude::setChildBuffer(Buffer * buffer)
226 {
227         child_buffer_ = buffer;
228 }
229
230
231 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
232 {
233         // FIXME
234         // This is only correct for the case of listings, but it'll do for now.
235         // In the other cases, this second parameter should just be empty.
236         static ParamInfo param_info_;
237         if (param_info_.empty()) {
238                 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
239                 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
240                 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
241         }
242         return param_info_;
243 }
244
245
246 bool InsetInclude::isCompatibleCommand(string const & s)
247 {
248         return type(s) != NONE;
249 }
250
251
252 bool InsetInclude::needsCProtection(bool const /*maintext*/, bool const fragile) const
253 {
254         // We need to \cprotect all types in fragile context
255         return fragile;
256 }
257
258
259 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
260 {
261         switch (cmd.action()) {
262
263         case LFUN_INSET_EDIT: {
264                 editIncluded(ltrim(to_utf8(params()["filename"])));
265                 break;
266         }
267
268         case LFUN_INSET_MODIFY: {
269                 // It should be OK just to invalidate the cache in setParams()
270                 // If not....
271                 // child_buffer_ = 0;
272                 InsetCommandParams p(INCLUDE_CODE);
273                 if (cmd.getArg(0) == "changetype") {
274                         cur.recordUndo();
275                         InsetCommand::doDispatch(cur, cmd);
276                         p = params();
277                 } else
278                         InsetCommand::string2params(to_utf8(cmd.argument()), p);
279                 if (!p.getCmdName().empty()) {
280                         if (isListings(p)){
281                                 InsetListingsParams new_params(to_utf8(p["lstparams"]));
282                                 docstring const new_label =
283                                         from_utf8(new_params.getParamValue("label"));
284
285                                 if (new_label.empty()) {
286                                         delete label_;
287                                         label_ = nullptr;
288                                 } else {
289                                         docstring old_label;
290                                         if (label_)
291                                                 old_label = label_->getParam("name");
292                                         else {
293                                                 label_ = createLabel(buffer_, new_label);
294                                                 label_->setBuffer(buffer());
295                                         }
296
297                                         if (new_label != old_label) {
298                                                 label_->updateLabelAndRefs(new_label, &cur);
299                                                 // the label might have been adapted (duplicate)
300                                                 if (new_label != label_->getParam("name")) {
301                                                         new_params.addParam("label", "{" +
302                                                                 to_utf8(label_->getParam("name")) + "}", true);
303                                                         p["lstparams"] = from_utf8(new_params.params());
304                                                 }
305                                         }
306                                 }
307                         }
308                         cur.recordUndo();
309                         setParams(p);
310                         cur.forceBufferUpdate();
311                 } else
312                         cur.noScreenUpdate();
313                 break;
314         }
315
316         case LFUN_MOUSE_RELEASE: {
317                 if (cmd.modifier() == ControlModifier) {
318                         FileName const incfile = includedFileName(buffer(), params());
319                         string const & incname = incfile.absFileName();
320                         editIncluded(incname);
321                         break;
322                 }
323         }
324         // fall through
325
326         //pass everything else up the chain
327         default:
328                 InsetCommand::doDispatch(cur, cmd);
329                 break;
330         }
331 }
332
333
334 void InsetInclude::editIncluded(string const & f)
335 {
336         if (isLyXFileName(f)) {
337                 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, f);
338                 lyx::dispatch(fr);
339         } else
340                 // tex file or other text file in verbatim mode
341                 theFormats().edit(buffer(),
342                         support::makeAbsPath(f, support::onlyPath(buffer().absFileName())),
343                         "text");
344 }
345
346
347 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
348                 FuncStatus & flag) const
349 {
350         switch (cmd.action()) {
351
352         case LFUN_INSET_EDIT:
353                 flag.setEnabled(true);
354                 return true;
355
356         case LFUN_INSET_MODIFY:
357                 if (cmd.getArg(0) == "changetype")
358                         return InsetCommand::getStatus(cur, cmd, flag);
359                 else
360                         flag.setEnabled(true);
361                 return true;
362
363         default:
364                 return InsetCommand::getStatus(cur, cmd, flag);
365         }
366 }
367
368
369 void InsetInclude::setParams(InsetCommandParams const & p)
370 {
371         // invalidate the cache
372         child_buffer_ = nullptr;
373
374         // reset in order to allow loading new file
375         failedtoload_ = false;
376         recursion_error_ = false;
377
378         InsetCommand::setParams(p);
379
380         if (preview_->monitoring())
381                 preview_->stopMonitoring();
382
383         if (type(params()) == INPUT)
384                 add_preview(*preview_, *this, buffer());
385 }
386
387
388 bool InsetInclude::isChildIncluded() const
389 {
390         std::list<std::string> includeonlys =
391                 buffer().params().getIncludedChildren();
392         if (includeonlys.empty())
393                 return true;
394         return (std::find(includeonlys.begin(),
395                           includeonlys.end(),
396                           ltrim(to_utf8(params()["filename"]))) != includeonlys.end());
397 }
398
399
400 docstring InsetInclude::screenLabel() const
401 {
402         docstring pre = file_exist_ ? docstring() : _("MISSING:");
403
404         docstring temp;
405
406         switch (type(params())) {
407                 case INPUT:
408                         temp = buffer().B_("Input");
409                         break;
410                 case VERB:
411                         temp = buffer().B_("Verbatim");
412                         break;
413                 case VERBAST:
414                         temp = buffer().B_("Verbatim*");
415                         break;
416                 case INCLUDE:
417                         if (isChildIncluded())
418                                 temp = buffer().B_("Include");
419                         else
420                                 temp += buffer().B_("Include (excluded)");
421                         break;
422                 case LISTINGS:
423                         temp = listings_label_;
424                         break;
425                 case NONE:
426                         LASSERT(false, temp = buffer().B_("Unknown"));
427                         break;
428         }
429
430         temp += ": ";
431
432         if (ltrim(params()["filename"]).empty())
433                 temp += "???";
434         else
435                 temp += from_utf8(onlyFileName(ltrim(to_utf8(params()["filename"]))));
436
437         return pre.empty() ? temp : pre + from_ascii(" ") + temp;
438 }
439
440
441 Buffer * InsetInclude::loadIfNeeded() const
442 {
443         // This is for background export and preview. We don't even want to
444         // try to load the cloned child document again.
445         if (buffer().isClone())
446                 return child_buffer_;
447
448         // Don't try to load it again if we failed before.
449         if (failedtoload_ || isVerbatim(params()) || isListings(params()))
450                 return nullptr;
451
452         FileName const included_file = includedFileName(buffer(), params());
453         // Use cached Buffer if possible.
454         if (child_buffer_ != nullptr) {
455                 if (theBufferList().isLoaded(child_buffer_)
456                     // additional sanity check: make sure the Buffer really is
457                     // associated with the file we want.
458                     && child_buffer_ == theBufferList().getBuffer(included_file))
459                         return child_buffer_;
460                 // Buffer vanished, so invalidate cache and try to reload.
461                 child_buffer_ = nullptr;
462         }
463
464         if (!isLyXFileName(included_file.absFileName()))
465                 return nullptr;
466
467         Buffer * child = theBufferList().getBuffer(included_file);
468         if (checkForRecursiveInclude(child))
469                 return child;
470
471         if (!child) {
472                 // the readonly flag can/will be wrong, not anymore I think.
473                 if (!included_file.exists()) {
474                         failedtoload_ = true;
475                         return nullptr;
476                 }
477
478                 child = theBufferList().newBuffer(included_file.absFileName());
479                 if (!child)
480                         // Buffer creation is not possible.
481                         return nullptr;
482
483                 buffer().pushIncludedBuffer(child);
484                 // Set parent before loading, such that macros can be tracked
485                 child->setParent(&buffer());
486
487                 if (child->loadLyXFile() != Buffer::ReadSuccess) {
488                         failedtoload_ = true;
489                         child->setParent(nullptr);
490                         //close the buffer we just opened
491                         theBufferList().release(child);
492                         buffer().popIncludedBuffer();
493                         return nullptr;
494                 }
495
496                 buffer().popIncludedBuffer();
497                 if (!child->errorList("Parse").empty()) {
498                         // FIXME: Do something.
499                 }
500         } else {
501                 // The file was already loaded, so, simply
502                 // inform parent buffer about local macros.
503                 Buffer const * parent = &buffer();
504                 child->setParent(parent);
505                 MacroNameSet macros;
506                 child->listMacroNames(macros);
507                 MacroNameSet::const_iterator cit = macros.begin();
508                 MacroNameSet::const_iterator end = macros.end();
509                 for (; cit != end; ++cit)
510                         parent->usermacros.insert(*cit);
511         }
512
513         // Cache the child buffer.
514         child_buffer_ = child;
515         return child;
516 }
517
518
519 bool InsetInclude::checkForRecursiveInclude(
520         Buffer const * cbuf, bool silent) const
521 {
522         if (recursion_error_)
523                 return true;
524
525         if (!buffer().isBufferIncluded(cbuf))
526                 return false;
527
528         if (!silent) {
529                 docstring const msg = _("The file\n%1$s\n has attempted to include itself.\n"
530                         "The document set will not work properly until this is fixed!");
531                 frontend::Alert::warning(_("Recursive Include"),
532                         bformat(msg, from_utf8(cbuf->fileName().absFileName())));
533         }
534         recursion_error_ = true;
535         return true;
536 }
537
538
539 void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const
540 {
541         string incfile = ltrim(to_utf8(params()["filename"]));
542
543         // Warn if no file name has been specified
544         if (incfile.empty()) {
545                 frontend::Alert::warning(_("No file name specified"),
546                         _("An included file name is empty.\n"
547                            "Ignoring Inclusion"),
548                         true);
549                 return;
550         }
551         // Warn if file doesn't exist
552         if (!includedFileExist()) {
553                 frontend::Alert::warning(_("Included file not found"),
554                         bformat(_("The included file\n"
555                                   "'%1$s'\n"
556                                   "has not been found. LyX will ignore the inclusion."),
557                                 from_utf8(incfile)),
558                         true);
559                  return;
560         }
561
562         FileName const included_file = includedFileName(buffer(), params());
563         Buffer const * const masterBuffer = buffer().masterBuffer();
564
565         if (runparams.inDeletedInset) {
566                 // We cannot strike-out whole children,
567                 // so we just output a note.
568                 os << "\\textbf{"
569                    << bformat(buffer().B_("[INCLUDED FILE %1$s DELETED!]"),
570                               from_utf8(included_file.onlyFileName()))
571                    << "}";
572                 return;
573         }
574
575         // if incfile is relative, make it relative to the master
576         // buffer directory.
577         if (!FileName::isAbsolute(incfile)) {
578                 // FIXME UNICODE
579                 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFileName()),
580                                               from_utf8(masterBuffer->filePath())));
581         }
582
583         string exppath = incfile;
584         if (!runparams.export_folder.empty()) {
585                 exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
586         }
587
588         // write it to a file (so far the complete file)
589         string exportfile;
590         string mangled;
591         // bug 5681
592         if (type(params()) == LISTINGS) {
593                 exportfile = exppath;
594                 mangled = DocFileName(included_file).mangledFileName();
595         } else {
596                 exportfile = changeExtension(exppath, ".tex");
597                 mangled = DocFileName(changeExtension(included_file.absFileName(), ".tex")).
598                         mangledFileName();
599         }
600
601         if (!runparams.nice)
602                 incfile = mangled;
603         else if (!runparams.silent)
604                 ; // no warning wanted
605         else if (!isValidLaTeXFileName(incfile)) {
606                 frontend::Alert::warning(_("Invalid filename"),
607                         _("The following filename will cause troubles "
608                                 "when running the exported file through LaTeX: ") +
609                         from_utf8(incfile));
610         } else if (!isValidDVIFileName(incfile)) {
611                 frontend::Alert::warning(_("Problematic filename for DVI"),
612                         _("The following filename can cause troubles "
613                                 "when running the exported file through LaTeX "
614                                 "and opening the resulting DVI: ") +
615                         from_utf8(incfile), true);
616         }
617
618         FileName const writefile(makeAbsPath(mangled, runparams.for_preview ?
619                                                  buffer().temppath() : masterBuffer->temppath()));
620
621         LYXERR(Debug::OUTFILE, "incfile:" << incfile);
622         LYXERR(Debug::OUTFILE, "exportfile:" << exportfile);
623         LYXERR(Debug::OUTFILE, "writefile:" << writefile);
624
625         string const tex_format = flavor2format(runparams.flavor);
626
627         switch (type(params())) {
628         case VERB:
629         case VERBAST: {
630                 incfile = latex_path(incfile);
631                 // FIXME UNICODE
632                 os << '\\' << from_ascii(params().getCmdName()) << '{'
633                    << from_utf8(incfile) << '}';
634                 break;
635         }
636         case INPUT: {
637                 runparams.exportdata->addExternalFile(tex_format, writefile,
638                                                       exportfile);
639
640                 // \input wants file with extension (default is .tex)
641                 if (!isLyXFileName(included_file.absFileName())) {
642                         incfile = latex_path(incfile);
643                         // FIXME UNICODE
644                         os << '\\' << from_ascii(params().getCmdName())
645                            << '{' << from_utf8(incfile) << '}';
646                 } else {
647                         incfile = changeExtension(incfile, ".tex");
648                         incfile = latex_path(incfile);
649                         // FIXME UNICODE
650                         os << '\\' << from_ascii(params().getCmdName())
651                            << '{' << from_utf8(incfile) <<  '}';
652                 }
653                 break;
654         }
655         case LISTINGS: {
656                 // Here, listings and minted have slightly different behaviors.
657                 // Using listings, it is always possible to have a caption,
658                 // even for non-floats. Using minted, only floats can have a
659                 // caption. So, with minted we use the following strategy.
660                 // If a caption was specified but the float parameter was not,
661                 // we ourselves add a caption above the listing (because the
662                 // listing comes from a file and might span several pages).
663                 // Otherwise, if float was specified, the floating listing
664                 // environment provided by minted is used. In either case, the
665                 // label parameter is taken as the label by which the float
666                 // can be referenced, otherwise it will have the meaning
667                 // intended by minted. In this last case, the label will
668                 // serve as a sort of caption that, however, will be shown
669                 // by minted only if the frame parameter is also specified.
670                 bool const use_minted = buffer().params().use_minted;
671                 runparams.exportdata->addExternalFile(tex_format, writefile,
672                                                       exportfile);
673                 string const opt = to_utf8(params()["lstparams"]);
674                 // opt is set in QInclude dialog and should have passed validation.
675                 InsetListingsParams lstparams(opt);
676                 docstring parameters = from_utf8(lstparams.params());
677                 docstring language;
678                 docstring caption;
679                 docstring label;
680                 docstring placement;
681                 bool isfloat = lstparams.isFloat();
682                 // We are going to split parameters at commas, so
683                 // replace commas that are not parameter separators
684                 // with a code point from the private use area
685                 char_type comma = replaceCommaInBraces(parameters);
686                 // Get float placement, language, caption, and
687                 // label, then remove the relative options if minted.
688                 vector<docstring> opts =
689                         getVectorFromString(parameters, from_ascii(","), false);
690                 vector<docstring> latexed_opts;
691                 for (size_t i = 0; i < opts.size(); ++i) {
692                         // Restore replaced commas
693                         opts[i] = subst(opts[i], comma, ',');
694                         if (use_minted && prefixIs(opts[i], from_ascii("float"))) {
695                                 if (prefixIs(opts[i], from_ascii("float=")))
696                                         placement = opts[i].substr(6);
697                                 opts.erase(opts.begin() + i--);
698                         } else if (use_minted && prefixIs(opts[i], from_ascii("language="))) {
699                                 language = opts[i].substr(9);
700                                 opts.erase(opts.begin() + i--);
701                         } else if (prefixIs(opts[i], from_ascii("caption="))) {
702                                 caption = params().prepareCommand(runparams, trim(opts[i].substr(8), "{}"),
703                                                                   ParamInfo::HANDLING_LATEXIFY);
704                                 opts.erase(opts.begin() + i--);
705                                 if (!use_minted)
706                                         latexed_opts.push_back(from_ascii("caption={") + caption + "}");
707                         } else if (prefixIs(opts[i], from_ascii("label="))) {
708                                 label = params().prepareCommand(runparams, trim(opts[i].substr(6), "{}"),
709                                                                 ParamInfo::HANDLING_ESCAPE);
710                                 opts.erase(opts.begin() + i--);
711                                 if (!use_minted)
712                                         latexed_opts.push_back(from_ascii("label={") + label + "}");
713                         }
714                         if (use_minted && !label.empty()) {
715                                 if (isfloat || !caption.empty())
716                                         label = trim(label, "{}");
717                                 else
718                                         opts.push_back(from_ascii("label=") + label);
719                         }
720                 }
721                 if (!latexed_opts.empty())
722                         opts.insert(opts.end(), latexed_opts.begin(), latexed_opts.end());
723                 parameters = getStringFromVector(opts, from_ascii(","));
724                 if (language.empty())
725                         language = from_ascii("TeX");
726                 if (use_minted && isfloat) {
727                         os << breakln << "\\begin{listing}";
728                         if (!placement.empty())
729                                 os << '[' << placement << "]";
730                         os << breakln;
731                 } else if (use_minted && !caption.empty()) {
732                         os << breakln << "\\lyxmintcaption[t]{" << caption;
733                         if (!label.empty())
734                                 os << "\\label{" << label << "}";
735                         os << "}\n";
736                 }
737                 os << (use_minted ? "\\inputminted" : "\\lstinputlisting");
738                 if (!parameters.empty())
739                         os << "[" << parameters << "]";
740                 if (use_minted)
741                         os << '{'  << ascii_lowercase(language) << '}';
742                 os << '{'  << incfile << '}';
743                 if (use_minted && isfloat) {
744                         if (!caption.empty())
745                                 os << breakln << "\\caption{" << caption << "}";
746                         if (!label.empty())
747                                 os << breakln << "\\label{" << label << "}";
748                         os << breakln << "\\end{listing}\n";
749                 }
750                 break;
751         }
752         case INCLUDE: {
753                 runparams.exportdata->addExternalFile(tex_format, writefile,
754                                                       exportfile);
755
756                 // \include don't want extension and demands that the
757                 // file really have .tex
758                 incfile = changeExtension(incfile, string());
759                 incfile = latex_path(incfile);
760                 // FIXME UNICODE
761                 os << '\\' << from_ascii(params().getCmdName()) << '{'
762                    << from_utf8(incfile) << '}';
763                 break;
764         }
765         case NONE:
766                 break;
767         }
768
769         if (runparams.inComment || runparams.dryrun)
770                 // Don't try to load or copy the file if we're
771                 // in a comment or doing a dryrun
772                 return;
773
774         if (!isInputOrInclude(params()) ||
775                  !isLyXFileName(included_file.absFileName())) {
776                 // In this case, it's not a LyX file, so we copy the file
777                 // to the temp dir, so that .aux files etc. are not created
778                 // in the original dir. Files included by this file will be
779                 // found via either the environment variable TEXINPUTS, or
780                 // input@path, see ../Buffer.cpp.
781                 unsigned long const checksum_in  = included_file.checksum();
782                 unsigned long const checksum_out = writefile.checksum();
783                 if (checksum_in != checksum_out) {
784                         if (!included_file.copyTo(writefile)) {
785                                 // FIXME UNICODE
786                                 LYXERR(Debug::OUTFILE,
787                                         to_utf8(bformat(_("Could not copy the file\n%1$s\n"
788                                                                         "into the temporary directory."),
789                                                          from_utf8(included_file.absFileName()))));
790                         }
791                 }
792                 return;
793         }
794
795                 
796         // it's a LyX file and we're inputting or including, so
797         // try to load it so we can write the associated latex
798         Buffer * tmp = loadIfNeeded();
799         if (!tmp) {
800                 if (!runparams.silent) {
801                         docstring text = bformat(_("Could not load included "
802                                 "file\n`%1$s'\n"
803                                 "Please, check whether it actually exists."),
804                                 included_file.displayName());
805                         throw ExceptionMessage(ErrorException, _("Error: "),
806                                                    text);
807                 }
808                 return;
809         }
810
811         if (recursion_error_)
812                 return;
813
814         if (!runparams.silent) {
815                 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
816                         // FIXME UNICODE
817                         docstring text = bformat(_("Included file `%1$s'\n"
818                                 "has textclass `%2$s'\n"
819                                 "while parent file has textclass `%3$s'."),
820                                 included_file.displayName(),
821                                 from_utf8(tmp->params().documentClass().name()),
822                                 from_utf8(masterBuffer->params().documentClass().name()));
823                         Alert::warning(_("Different textclasses"), text, true);
824                 }
825
826                 string const child_tf = tmp->params().useNonTeXFonts ? "true" : "false";
827                 string const master_tf = masterBuffer->params().useNonTeXFonts ? "true" : "false";
828                 if (tmp->params().useNonTeXFonts != masterBuffer->params().useNonTeXFonts) {
829                         docstring text = bformat(_("Included file `%1$s'\n"
830                                 "has use-non-TeX-fonts set to `%2$s'\n"
831                                 "while parent file has use-non-TeX-fonts set to `%3$s'."),
832                                 included_file.displayName(),
833                                 from_utf8(child_tf),
834                                 from_utf8(master_tf));
835                         Alert::warning(_("Different use-non-TeX-fonts settings"), text, true);
836                 } 
837                 else if (tmp->params().inputenc != masterBuffer->params().inputenc) {
838                         docstring text = bformat(_("Included file `%1$s'\n"
839                                 "uses input encoding \"%2$s\" [%3$s]\n"
840                                 "while parent file uses input encoding \"%4$s\" [%5$s]."),
841                                 included_file.displayName(),
842                                 _(tmp->params().inputenc),
843                                 from_utf8(tmp->params().encoding().guiName()),
844                                 _(masterBuffer->params().inputenc),
845                                 from_utf8(masterBuffer->params().encoding().guiName()));
846                         Alert::warning(_("Different LaTeX input encodings"), text, true);
847                 }
848
849                 // Make sure modules used in child are all included in master
850                 // FIXME It might be worth loading the children's modules into the master
851                 // over in BufferParams rather than doing this check.
852                 LayoutModuleList const masterModules = masterBuffer->params().getModules();
853                 LayoutModuleList const childModules = tmp->params().getModules();
854                 LayoutModuleList::const_iterator it = childModules.begin();
855                 LayoutModuleList::const_iterator end = childModules.end();
856                 for (; it != end; ++it) {
857                         string const module = *it;
858                         LayoutModuleList::const_iterator found =
859                                 find(masterModules.begin(), masterModules.end(), module);
860                         if (found == masterModules.end()) {
861                                 docstring text = bformat(_("Included file `%1$s'\n"
862                                         "uses module `%2$s'\n"
863                                         "which is not used in parent file."),
864                                         included_file.displayName(), from_utf8(module));
865                                 Alert::warning(_("Module not found"), text, true);
866                         }
867                 }
868         }
869
870         tmp->markDepClean(masterBuffer->temppath());
871
872         // Don't assume the child's format is latex
873         string const inc_format = tmp->params().bufferFormat();
874         FileName const tmpwritefile(changeExtension(writefile.absFileName(),
875                 theFormats().extension(inc_format)));
876
877         // FIXME: handle non existing files
878         // The included file might be written in a different encoding
879         // and language.
880         Encoding const * const oldEnc = runparams.encoding;
881         Language const * const oldLang = runparams.master_language;
882         // If the master uses non-TeX fonts (XeTeX, LuaTeX),
883         // the children must be encoded in plain utf8!
884         if (masterBuffer->params().useNonTeXFonts)
885                 runparams.encoding = encodings.fromLyXName("utf8-plain");
886         else if (oldEnc)
887                 runparams.encoding = oldEnc;
888         else runparams.encoding = &tmp->params().encoding();
889         runparams.master_language = buffer().params().language;
890         runparams.par_begin = 0;
891         runparams.par_end = tmp->paragraphs().size();
892         runparams.is_child = true;
893         Buffer::ExportStatus retval =
894                 tmp->makeLaTeXFile(tmpwritefile, masterFileName(buffer()).
895                         onlyPath().absFileName(), runparams, Buffer::OnlyBody);
896         if (retval == Buffer::ExportKilled && buffer().isClone() &&
897                   buffer().isExporting()) {
898           // We really shouldn't get here, I don't think.
899           LYXERR0("No conversion exception?");
900                 throw ConversionException();
901         }
902         else if (retval != Buffer::ExportSuccess) {
903                 if (!runparams.silent) {
904                         docstring msg = bformat(_("Included file `%1$s' "
905                                 "was not exported correctly.\n "
906                                 "LaTeX export is probably incomplete."),
907                                 included_file.displayName());
908                         ErrorList const & el = tmp->errorList("Export");
909                         if (!el.empty())
910                                 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
911                                                 msg, el.begin()->error, el.begin()->description);
912                         throw ExceptionMessage(ErrorException, _("Error: "), msg);
913                 }
914         }
915         runparams.encoding = oldEnc;
916         runparams.master_language = oldLang;
917         runparams.is_child = false;
918
919         // If needed, use converters to produce a latex file from the child
920         if (tmpwritefile != writefile) {
921                 ErrorList el;
922                 Converters::RetVal const conv_retval =
923                         theConverters().convert(tmp, tmpwritefile, writefile,
924                                 included_file, inc_format, tex_format, el);
925                 if (conv_retval == Converters::KILLED && buffer().isClone() &&
926                         buffer().isExporting()) {
927                         // We really shouldn't get here, I don't think.
928                         LYXERR0("No conversion exception?");
929                         throw ConversionException();
930                 } else if (conv_retval != Converters::SUCCESS && !runparams.silent) {
931                         docstring msg = bformat(_("Included file `%1$s' "
932                                         "was not exported correctly.\n "
933                                         "LaTeX export is probably incomplete."),
934                                         included_file.displayName());
935                         if (!el.empty())
936                                 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
937                                                 msg, el.begin()->error, el.begin()->description);
938                         throw ExceptionMessage(ErrorException, _("Error: "), msg);
939                 }
940         }
941 }
942
943
944 docstring InsetInclude::xhtml(XMLStream & xs, OutputParams const & rp) const
945 {
946         if (rp.inComment)
947                  return docstring();
948
949         // For verbatim and listings, we just include the contents of the file as-is.
950         // In the case of listings, we wrap it in <pre>.
951         bool const listing = isListings(params());
952         if (listing || isVerbatim(params())) {
953                 if (listing)
954                         xs << xml::StartTag("pre");
955                 // FIXME: We don't know the encoding of the file, default to UTF-8.
956                 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
957                 if (listing)
958                         xs << xml::EndTag("pre");
959                 return docstring();
960         }
961
962         // We don't (yet) know how to Input or Include non-LyX files.
963         // (If we wanted to get really arcane, we could run some tex2html
964         // converter on the included file. But that's just masochistic.)
965         FileName const included_file = includedFileName(buffer(), params());
966         if (!isLyXFileName(included_file.absFileName())) {
967                 if (!rp.silent)
968                         frontend::Alert::warning(_("Unsupported Inclusion"),
969                                          bformat(_("LyX does not know how to include non-LyX files when "
970                                                    "generating HTML output. Offending file:\n%1$s"),
971                                                     ltrim(params()["filename"])));
972                 return docstring();
973         }
974
975         // In the other cases, we will generate the HTML and include it.
976
977         Buffer const * const ibuf = loadIfNeeded();
978         if (!ibuf)
979                 return docstring();
980
981         if (recursion_error_)
982                 return docstring();
983
984         // are we generating only some paragraphs, or all of them?
985         bool const all_pars = !rp.dryrun ||
986                         (rp.par_begin == 0 &&
987                          rp.par_end == (int)buffer().text().paragraphs().size());
988
989         OutputParams op = rp;
990         if (all_pars) {
991                 op.par_begin = 0;
992                 op.par_end = 0;
993                 ibuf->writeLyXHTMLSource(xs.os(), op, Buffer::IncludedFile);
994         } else {
995                 xs << XMLStream::ESCAPE_NONE << "<!-- Included file: ";
996                 xs << from_utf8(included_file.absFileName());
997                 xs << XMLStream::ESCAPE_NONE << " -->";
998         }
999         
1000         return docstring();
1001 }
1002
1003
1004 int InsetInclude::plaintext(odocstringstream & os,
1005         OutputParams const & op, size_t) const
1006 {
1007         // just write the filename if we're making a tooltip or toc entry,
1008         // or are generating this for advanced search
1009         if (op.for_tooltip || op.for_toc || op.find_effective()) {
1010                 os << '[' << screenLabel() << '\n'
1011                    << ltrim(getParam("filename")) << "\n]";
1012                 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
1013         }
1014
1015         if (isVerbatim(params()) || isListings(params())) {
1016                 if (op.find_effective()) {
1017                         os << '[' << screenLabel() << ']';
1018                 }
1019                 else {
1020                         os << '[' << screenLabel() << '\n'
1021                            // FIXME: We don't know the encoding of the file, default to UTF-8.
1022                            << includedFileName(buffer(), params()).fileContents("UTF-8")
1023                            << "\n]";
1024                 }
1025                 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
1026         }
1027
1028         Buffer const * const ibuf = loadIfNeeded();
1029         if (!ibuf) {
1030                 docstring const str = '[' + screenLabel() + ']';
1031                 os << str;
1032                 return str.size();
1033         }
1034
1035         if (recursion_error_)
1036                 return 0;
1037
1038         writePlaintextFile(*ibuf, os, op);
1039         return 0;
1040 }
1041
1042
1043 void InsetInclude::docbook(XMLStream & xs, OutputParams const & rp) const
1044 {
1045         if (rp.inComment)
1046                 return;
1047
1048         // For verbatim and listings, we just include the contents of the file as-is.
1049         bool const verbatim = isVerbatim(params());
1050         bool const listing = isListings(params());
1051         if (listing || verbatim) {
1052                 if (listing)
1053                         xs << xml::StartTag("programlisting");
1054                 else if (verbatim)
1055                         xs << xml::StartTag("literallayout");
1056
1057                 // FIXME: We don't know the encoding of the file, default to UTF-8.
1058                 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
1059
1060                 if (listing)
1061                         xs << xml::EndTag("programlisting");
1062                 else if (verbatim)
1063                         xs << xml::EndTag("literallayout");
1064
1065                 return;
1066         }
1067
1068         // We don't know how to input or include non-LyX files. Input it as a comment.
1069         FileName const included_file = includedFileName(buffer(), params());
1070         if (!isLyXFileName(included_file.absFileName())) {
1071                 if (!rp.silent)
1072                         frontend::Alert::warning(_("Unsupported Inclusion"),
1073                                                  bformat(_("LyX does not know how to process included non-LyX files when "
1074                                                            "generating DocBook output. The content of the file will be output as a "
1075                                                                                    "comment. Offending file:\n%1$s"),
1076                                                          ltrim(params()["filename"])));
1077
1078                 // Read the file, output it wrapped into comments.
1079                 xs << XMLStream::ESCAPE_NONE << "<!-- Included file: ";
1080                 xs << from_utf8(included_file.absFileName());
1081                 xs << XMLStream::ESCAPE_NONE << " -->";
1082
1083                 xs << XMLStream::ESCAPE_NONE << "<!-- ";
1084                 xs << included_file.fileContents("UTF-8");
1085                 xs << XMLStream::ESCAPE_NONE << " -->";
1086
1087                 xs << XMLStream::ESCAPE_NONE << "<!-- End of included file: ";
1088                 xs << from_utf8(included_file.absFileName());
1089                 xs << XMLStream::ESCAPE_NONE << " -->";
1090         }
1091
1092         // In the other cases, we generate the DocBook version and include it.
1093         Buffer const * const ibuf = loadIfNeeded();
1094         if (!ibuf)
1095                 return;
1096
1097         if (recursion_error_)
1098                 return;
1099
1100         // are we generating only some paragraphs, or all of them?
1101         bool const all_pars = !rp.dryrun ||
1102                               (rp.par_begin == 0 &&
1103                                rp.par_end == (int) buffer().text().paragraphs().size());
1104
1105         OutputParams op = rp;
1106         if (all_pars) {
1107                 op.par_begin = 0;
1108                 op.par_end = 0;
1109                 op.inInclude = true;
1110                 op.docbook_in_par = false;
1111                 ibuf->writeDocBookSource(xs.os(), op, Buffer::IncludedFile);
1112         } else {
1113                 xs << XMLStream::ESCAPE_NONE << "<!-- Included file: ";
1114                 xs << from_utf8(included_file.absFileName());
1115                 xs << XMLStream::ESCAPE_NONE << " -->";
1116         }
1117 }
1118
1119
1120 void InsetInclude::validate(LaTeXFeatures & features) const
1121 {
1122         LATTEST(&buffer() == &features.buffer());
1123
1124         string incfile = ltrim(to_utf8(params()["filename"]));
1125         string const included_file =
1126                 includedFileName(buffer(), params()).absFileName();
1127
1128         string writefile;
1129         if (isLyXFileName(included_file))
1130                 writefile = changeExtension(included_file, ".sgml");
1131         else
1132                 writefile = included_file;
1133
1134         if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
1135                 incfile = DocFileName(writefile).mangledFileName();
1136                 writefile = makeAbsPath(incfile,
1137                                         buffer().masterBuffer()->temppath()).absFileName();
1138         }
1139
1140         features.includeFile(include_label, writefile);
1141
1142         features.useInsetLayout(getLayout());
1143         if (isVerbatim(params()))
1144                 features.require("verbatim");
1145         else if (isListings(params())) {
1146                 if (buffer().params().use_minted) {
1147                         features.require("minted");
1148                         string const opts = to_utf8(params()["lstparams"]);
1149                         InsetListingsParams lstpars(opts);
1150                         if (!lstpars.isFloat() && contains(opts, "caption="))
1151                                 features.require("lyxmintcaption");
1152                 } else
1153                         features.require("listings");
1154         }
1155
1156         // Here we must do the fun stuff...
1157         // Load the file in the include if it needs
1158         // to be loaded:
1159         Buffer * const tmp = loadIfNeeded();
1160         if (!tmp) 
1161                 return;
1162
1163         // the file is loaded
1164         if (checkForRecursiveInclude(tmp))
1165                 return;
1166         buffer().pushIncludedBuffer(tmp);
1167
1168         // We must temporarily change features.buffer,
1169         // otherwise it would always be the master buffer,
1170         // and nested includes would not work.
1171         features.setBuffer(*tmp);
1172         // Maybe this is already a child
1173         bool const is_child =
1174                 features.runparams().is_child;
1175         features.runparams().is_child = true;
1176         tmp->validate(features);
1177         features.runparams().is_child = is_child;
1178         features.setBuffer(buffer());
1179         
1180         buffer().popIncludedBuffer();
1181 }
1182
1183
1184 void InsetInclude::collectBibKeys(InsetIterator const & /*di*/, FileNameList & checkedFiles) const
1185 {
1186         Buffer * ibuf = loadIfNeeded();
1187         if (!ibuf)
1188                 return;
1189
1190         if (checkForRecursiveInclude(ibuf))
1191                 return;
1192         buffer().pushIncludedBuffer(ibuf);
1193         ibuf->collectBibKeys(checkedFiles);
1194         buffer().popIncludedBuffer();   
1195 }
1196
1197
1198 bool InsetInclude::inheritFont() const
1199 {
1200         return !isVerbatim(params());
1201 }
1202
1203
1204 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
1205 {
1206         LBUFERR(mi.base.bv);
1207
1208         bool use_preview = false;
1209         if (RenderPreview::previewText()) {
1210                 graphics::PreviewImage const * pimage =
1211                         preview_->getPreviewImage(mi.base.bv->buffer());
1212                 use_preview = pimage && pimage->image();
1213         }
1214
1215         if (use_preview) {
1216                 preview_->metrics(mi, dim);
1217         } else {
1218                 setBroken(!file_exist_ || recursion_error_);
1219                 InsetCommand::metrics(mi, dim);
1220         }
1221 }
1222
1223
1224 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
1225 {
1226         LBUFERR(pi.base.bv);
1227
1228         bool use_preview = false;
1229         if (RenderPreview::previewText()) {
1230                 graphics::PreviewImage const * pimage =
1231                         preview_->getPreviewImage(pi.base.bv->buffer());
1232                 use_preview = pimage && pimage->image();
1233         }
1234
1235         if (use_preview)
1236                 preview_->draw(pi, x, y);
1237         else
1238                 InsetCommand::draw(pi, x, y);
1239 }
1240
1241
1242 void InsetInclude::write(ostream & os) const
1243 {
1244         params().Write(os, &buffer());
1245 }
1246
1247
1248 string InsetInclude::contextMenuName() const
1249 {
1250         return "context-include";
1251 }
1252
1253
1254 int InsetInclude::rowFlags() const
1255 {
1256         return type(params()) == INPUT ? Inline : Display;
1257 }
1258
1259
1260 docstring InsetInclude::layoutName() const
1261 {
1262         if (isListings(params()))
1263                 return from_ascii("IncludeListings");
1264         return InsetCommand::layoutName();
1265 }
1266
1267
1268 //
1269 // preview stuff
1270 //
1271
1272 void InsetInclude::fileChanged() const
1273 {
1274         Buffer const * const buffer = updateFrontend();
1275         if (!buffer)
1276                 return;
1277
1278         preview_->removePreview(*buffer);
1279         add_preview(*preview_, *this, *buffer);
1280         preview_->startLoading(*buffer);
1281 }
1282
1283
1284 namespace {
1285
1286 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
1287 {
1288         FileName const included_file = includedFileName(buffer, params);
1289
1290         return type(params) == INPUT && params.preview() &&
1291                 included_file.isReadableFile();
1292 }
1293
1294
1295 docstring latexString(InsetInclude const & inset)
1296 {
1297         odocstringstream ods;
1298         otexstream os(ods);
1299         // We don't need to set runparams.encoding since this will be done
1300         // by latex() anyway.
1301         OutputParams runparams(nullptr);
1302         runparams.flavor = Flavor::LaTeX;
1303         runparams.for_preview = true;
1304         inset.latex(os, runparams);
1305
1306         return ods.str();
1307 }
1308
1309
1310 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
1311                  Buffer const & buffer)
1312 {
1313         InsetCommandParams const & params = inset.params();
1314         if (RenderPreview::previewText() && preview_wanted(params, buffer)) {
1315                 renderer.setAbsFile(includedFileName(buffer, params));
1316                 docstring snippet;
1317                 try {
1318                         // InsetInclude::latex() throws if generation of LaTeX
1319                         // fails, e.g. if lyx2lyx fails because file is too
1320                         // new, or knitr fails.
1321                         snippet = latexString(inset);
1322                 } catch (...) {
1323                         // remove current preview because it is likely
1324                         // associated with the previous included file name
1325                         renderer.removePreview(buffer);
1326                         LYXERR0("Preview of include failed.");
1327                         return;
1328                 }
1329                 renderer.addPreview(snippet, buffer);
1330         }
1331 }
1332
1333 } // namespace
1334
1335
1336 void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
1337         graphics::PreviewLoader & ploader) const
1338 {
1339         Buffer const & buffer = ploader.buffer();
1340         if (!preview_wanted(params(), buffer))
1341                 return;
1342         preview_->setAbsFile(includedFileName(buffer, params()));
1343         docstring const snippet = latexString(*this);
1344         preview_->addPreview(snippet, ploader);
1345 }
1346
1347
1348 void InsetInclude::addToToc(DocIterator const & cpit, bool output_active,
1349                             UpdateType utype, TocBackend & backend) const
1350 {
1351         if (isListings(params())) {
1352                 if (label_)
1353                         label_->addToToc(cpit, output_active, utype, backend);
1354                 TocBuilder & b = backend.builder("listing");
1355                 b.pushItem(cpit, screenLabel(), output_active);
1356                 InsetListingsParams p(to_utf8(params()["lstparams"]));
1357                 b.argumentItem(from_utf8(p.getParamValue("caption")));
1358                 b.pop();
1359                 return;
1360         }
1361         if (isVerbatim(params())) {
1362                 TocBuilder & b = backend.builder("child");
1363                 b.pushItem(cpit, screenLabel(), output_active);
1364                 b.pop();
1365                 return;
1366         }
1367         // the common case
1368         Buffer const * const childbuffer = loadIfNeeded();
1369
1370         TocBuilder & b = backend.builder("child");
1371         string const fname = ltrim(to_utf8(params()["filename"]));
1372         // mark non-existent file with MISSING
1373         docstring const str = (file_exist_ ? from_ascii("") : _("MISSING: "))
1374                 + from_utf8(onlyFileName(fname)) + " (" + from_utf8(fname) + ")";
1375         b.pushItem(cpit, str, output_active);
1376         b.pop();
1377
1378         if (!childbuffer)
1379                 return;
1380
1381         if (checkForRecursiveInclude(childbuffer))
1382                 return;
1383         buffer().pushIncludedBuffer(childbuffer);
1384         // Update the child's tocBackend. The outliner uses the master's, but
1385         // the navigation menu uses the child's.
1386         childbuffer->tocBackend().update(output_active, utype);
1387         // Include Tocs from children
1388         childbuffer->inset().addToToc(DocIterator(), output_active, utype,
1389                                                                   backend);
1390         buffer().popIncludedBuffer();
1391         // Copy missing outliner names (though the user has been warned against
1392         // having different document class and module selection between master
1393         // and child).
1394         for (auto const & name
1395                          : childbuffer->params().documentClass().outlinerNames())
1396                 backend.addName(name.first, translateIfPossible(name.second));
1397 }
1398
1399
1400 void InsetInclude::updateCommand()
1401 {
1402         if (!label_)
1403                 return;
1404
1405         docstring old_label = label_->getParam("name");
1406         label_->updateLabel(old_label);
1407         // the label might have been adapted (duplicate)
1408         docstring new_label = label_->getParam("name");
1409         if (old_label == new_label)
1410                 return;
1411
1412         // update listings parameters...
1413         InsetCommandParams p(INCLUDE_CODE);
1414         p = params();
1415         InsetListingsParams par(to_utf8(params()["lstparams"]));
1416         par.addParam("label", "{" + to_utf8(new_label) + "}", true);
1417         p["lstparams"] = from_utf8(par.params());
1418         setParams(p);
1419 }
1420
1421
1422 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
1423 {
1424         file_exist_ = includedFileExist();
1425         Buffer const * const childbuffer = loadIfNeeded();
1426         if (childbuffer) {
1427                 if (!checkForRecursiveInclude(childbuffer))
1428                         childbuffer->updateBuffer(Buffer::UpdateChildOnly, utype);
1429                 return;
1430         }
1431
1432         if (!isListings(params()))
1433                 return;
1434
1435         if (label_)
1436                 label_->updateBuffer(it, utype, deleted);
1437
1438         InsetListingsParams const par(to_utf8(params()["lstparams"]));
1439         if (par.getParamValue("caption").empty()) {
1440                 listings_label_ = buffer().B_("Program Listing");
1441                 return;
1442         }
1443         Buffer const & master = *buffer().masterBuffer();
1444         Counters & counters = master.params().documentClass().counters();
1445         docstring const cnt = from_ascii("listing");
1446         listings_label_ = master.B_("Program Listing");
1447         if (counters.hasCounter(cnt)) {
1448                 counters.step(cnt, utype);
1449                 listings_label_ += " " + convert<docstring>(counters.value(cnt));
1450         }
1451 }
1452
1453
1454 bool InsetInclude::includedFileExist() const
1455 {
1456         // check whether the included file exist
1457         string incFileName = ltrim(to_utf8(params()["filename"]));
1458         FileName fn =
1459                 support::makeAbsPath(incFileName,
1460                                      support::onlyPath(buffer().absFileName()));
1461         return fn.exists();
1462 }
1463
1464 } // namespace lyx