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