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