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