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