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