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