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