3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "insetinclude.h"
16 #include "buffer_funcs.h"
17 #include "bufferlist.h"
18 #include "bufferparams.h"
19 #include "BufferView.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
25 #include "LaTeXFeatures.h"
28 #include "metricsinfo.h"
29 #include "outputparams.h"
31 #include "frontends/Alert.h"
32 #include "frontends/LyXView.h"
33 #include "frontends/Painter.h"
35 #include "graphics/PreviewLoader.h"
37 #include "insets/render_preview.h"
39 #include "support/FileInfo.h"
40 #include "support/filename.h"
41 #include "support/filetools.h"
42 #include "support/lstrings.h" // contains
43 #include "support/lyxlib.h"
44 #include "support/tostr.h"
46 #include <boost/bind.hpp>
48 #include "support/std_ostream.h"
49 #include "support/std_sstream.h"
51 using lyx::support::AddName;
52 using lyx::support::AbsolutePath;
53 using lyx::support::bformat;
54 using lyx::support::ChangeExtension;
55 using lyx::support::contains;
56 using lyx::support::copy;
57 using lyx::support::FileInfo;
58 using lyx::support::FileName;
59 using lyx::support::GetFileContents;
60 using lyx::support::IsFileReadable;
61 using lyx::support::IsLyXFilename;
62 using lyx::support::MakeAbsPath;
63 using lyx::support::MakeDisplayPath;
64 using lyx::support::MakeRelPath;
65 using lyx::support::OnlyFilename;
66 using lyx::support::OnlyPath;
67 using lyx::support::subst;
68 using lyx::support::sum;
73 using std::istringstream;
75 using std::ostringstream;
78 extern BufferList bufferlist;
83 string const uniqueID()
85 static unsigned int seed = 1000;
86 return "file" + tostr(++seed);
92 InsetInclude::InsetInclude(InsetCommandParams const & p)
93 : params_(p), include_label(uniqueID()),
94 preview_(new RenderMonitoredPreview(this)),
97 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
101 InsetInclude::InsetInclude(InsetInclude const & other)
103 params_(other.params_),
104 include_label(other.include_label),
105 preview_(new RenderMonitoredPreview(this)),
106 set_label_(other.set_label_)
108 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
112 InsetInclude::~InsetInclude()
114 InsetIncludeMailer(*this).hideDialog();
118 void InsetInclude::priv_dispatch(LCursor & cur, FuncRequest & cmd)
120 switch (cmd.action) {
122 case LFUN_INSET_MODIFY: {
123 InsetCommandParams p;
124 InsetIncludeMailer::string2params(cmd.argument, p);
125 if (!p.getCmdName().empty()) {
126 set(p, cur.buffer());
132 case LFUN_INSET_DIALOG_UPDATE:
133 InsetIncludeMailer(*this).updateDialog(&cur.bv());
136 case LFUN_MOUSE_RELEASE:
137 if (button_.box().contains(cmd.x, cmd.y))
138 InsetIncludeMailer(*this).showDialog(&cur.bv());
141 case LFUN_INSET_DIALOG_SHOW:
142 InsetIncludeMailer(*this).showDialog(&cur.bv());
146 InsetOld::priv_dispatch(cur, cmd);
152 InsetCommandParams const & InsetInclude::params() const
160 /// the type of inclusion
169 Types type(InsetCommandParams const & params)
171 string const command_name = params.getCmdName();
173 if (command_name == "input")
175 if (command_name == "verbatiminput")
177 if (command_name == "verbatiminput*")
183 bool isVerbatim(InsetCommandParams const & params)
185 string const command_name = params.getCmdName();
186 return command_name == "verbatiminput" ||
187 command_name == "verbatiminput*";
191 string const masterFilename(Buffer const & buffer)
193 return buffer.getMasterBuffer()->fileName();
197 string const parentFilename(Buffer const & buffer)
199 return buffer.fileName();
203 string const includedFilename(Buffer const & buffer,
204 InsetCommandParams const & params)
206 return MakeAbsPath(params.getContents(),
207 OnlyPath(parentFilename(buffer)));
211 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
216 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
221 if (preview_->monitoring())
222 preview_->stopMonitoring();
224 if (type(params_) == INPUT)
225 add_preview(*preview_, *this, buffer);
229 auto_ptr<InsetBase> InsetInclude::clone() const
231 return auto_ptr<InsetBase>(new InsetInclude(*this));
235 void InsetInclude::write(Buffer const &, ostream & os) const
241 void InsetInclude::write(ostream & os) const
243 os << "Include " << params_.getCommand() << '\n'
244 << "preview " << tostr(params_.preview()) << '\n';
248 void InsetInclude::read(Buffer const &, LyXLex & lex)
254 void InsetInclude::read(LyXLex & lex)
260 string const InsetInclude::getScreenLabel(Buffer const &) const
264 switch (type(params_)) {
265 case INPUT: temp += _("Input"); break;
266 case VERB: temp += _("Verbatim Input"); break;
267 case VERBAST: temp += _("Verbatim Input*"); break;
268 case INCLUDE: temp += _("Include"); break;
273 if (params_.getContents().empty())
276 temp += OnlyFilename(params_.getContents());
284 /// return true if the file is or got loaded.
285 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
287 if (isVerbatim(params))
290 string const included_file = includedFilename(buffer, params);
291 if (!IsLyXFilename(included_file))
294 Buffer * buf = bufferlist.getBuffer(included_file);
296 // the readonly flag can/will be wrong, not anymore I think.
297 FileInfo finfo(included_file);
300 buf = bufferlist.newBuffer(included_file);
301 if (!loadLyXFile(buf, included_file))
305 buf->setParentName(parentFilename(buffer));
313 int InsetInclude::latex(Buffer const & buffer, ostream & os,
314 OutputParams const & runparams) const
316 string incfile(params_.getContents());
318 // Do nothing if no file name has been specified
322 string const included_file = includedFilename(buffer, params_);
323 Buffer const * const m_buffer = buffer.getMasterBuffer();
325 // if incfile is relative, make it relative to the master
327 if (!AbsolutePath(incfile)) {
328 incfile = MakeRelPath(included_file,
329 m_buffer->filePath());
332 // write it to a file (so far the complete file)
333 string writefile = ChangeExtension(included_file, ".tex");
335 if (!runparams.nice) {
336 incfile = FileName(writefile).mangledFilename();
337 writefile = MakeAbsPath(incfile, m_buffer->temppath());
339 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
340 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
342 if (loadIfNeeded(buffer, params_)) {
343 Buffer * tmp = bufferlist.getBuffer(included_file);
345 if (tmp->params().textclass != m_buffer->params().textclass) {
346 string text = bformat(_("Included file `%1$s'\n"
347 "has textclass `%2$s'\n"
348 "while parent file has textclass `%3$s'."),
349 MakeDisplayPath(included_file),
350 tmp->params().getLyXTextClass().name(),
351 m_buffer->params().getLyXTextClass().name());
352 Alert::warning(_("Different textclasses"), text);
356 tmp->markDepClean(m_buffer->temppath());
359 #warning Second argument is irrelevant!
360 // since only_body is true, makeLaTeXFile will not look at second
361 // argument. Should we set it to string(), or should makeLaTeXFile
362 // make use of it somehow? (JMarc 20031002)
364 tmp->makeLaTeXFile(writefile,
365 OnlyPath(masterFilename(buffer)),
367 } else if (!runparams.nice) {
368 // Copy the file to the temp dir, so that .aux files etc.
369 // are not created in the original dir. Files included by
370 // this file will be found via input@path, see ../buffer.C.
371 unsigned long const checksum_in = sum(included_file);
372 unsigned long const checksum_out = sum(writefile);
374 if (checksum_in != checksum_out) {
375 if (!copy(included_file, writefile)) {
377 << bformat(_("Could not copy the file\n%1$s\n"
378 "into the temporary directory."),
386 if (isVerbatim(params_)) {
387 os << '\\' << params_.getCmdName() << '{' << incfile << '}';
388 } else if (type(params_) == INPUT) {
389 // \input wants file with extension (default is .tex)
390 if (!IsLyXFilename(included_file)) {
391 os << '\\' << params_.getCmdName() << '{' << incfile << '}';
393 os << '\\' << params_.getCmdName() << '{'
394 << ChangeExtension(incfile, ".tex")
398 // \include don't want extension and demands that the
399 // file really have .tex
400 os << '\\' << params_.getCmdName() << '{'
401 << ChangeExtension(incfile, string())
409 int InsetInclude::plaintext(Buffer const & buffer, ostream & os,
410 OutputParams const &) const
412 if (isVerbatim(params_))
413 os << GetFileContents(includedFilename(buffer, params_));
418 int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os,
419 OutputParams const & runparams) const
421 string incfile(params_.getContents());
423 // Do nothing if no file name has been specified
427 string const included_file = includedFilename(buffer, params_);
429 if (loadIfNeeded(buffer, params_)) {
430 Buffer * tmp = bufferlist.getBuffer(included_file);
432 // write it to a file (so far the complete file)
433 string writefile = ChangeExtension(included_file, ".sgml");
435 if (!runparams.nice) {
436 incfile = FileName(writefile).mangledFilename();
437 writefile = MakeAbsPath(incfile,
438 buffer.getMasterBuffer()->temppath());
441 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
442 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
444 OutputParams runp = runparams;
445 tmp->makeLinuxDocFile(writefile, runp, true);
448 if (isVerbatim(params_)) {
450 << GetFileContents(included_file)
453 os << '&' << include_label << ';';
459 int InsetInclude::docbook(Buffer const & buffer, ostream & os,
460 OutputParams const & runparams) const
462 string incfile(params_.getContents());
464 // Do nothing if no file name has been specified
468 string const included_file = includedFilename(buffer, params_);
470 if (loadIfNeeded(buffer, params_)) {
471 Buffer * tmp = bufferlist.getBuffer(included_file);
473 // write it to a file (so far the complete file)
474 string writefile = ChangeExtension(included_file, ".sgml");
476 if (!runparams.nice) {
477 incfile = FileName(writefile).mangledFilename();
478 writefile = MakeAbsPath(incfile,
479 buffer.getMasterBuffer()->temppath());
482 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
483 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
485 OutputParams runp = runparams;
486 tmp->makeDocBookFile(writefile, runp, true);
489 if (isVerbatim(params_)) {
490 os << "<inlinegraphic fileref=\""
491 << '&' << include_label << ';'
492 << "\" format=\"linespecific\">";
494 os << '&' << include_label << ';';
500 void InsetInclude::validate(LaTeXFeatures & features) const
502 string incfile(params_.getContents());
505 Buffer const & buffer = features.buffer();
507 string const included_file = includedFilename(buffer, params_);
509 if (IsLyXFilename(included_file))
510 writefile = ChangeExtension(included_file, ".sgml");
512 writefile = included_file;
514 if (!features.nice() && !isVerbatim(params_)) {
515 incfile = FileName(writefile).mangledFilename();
516 writefile = MakeAbsPath(incfile,
517 buffer.getMasterBuffer()->temppath());
520 features.includeFile(include_label, writefile);
522 if (isVerbatim(params_))
523 features.require("verbatim");
525 // Here we must do the fun stuff...
526 // Load the file in the include if it needs
528 if (loadIfNeeded(buffer, params_)) {
530 Buffer * const tmp = bufferlist.getBuffer(included_file);
532 // We must temporarily change features.buffer,
533 // otherwise it would always be the master buffer,
534 // and nested includes would not work.
535 features.setBuffer(*tmp);
536 tmp->validate(features);
537 features.setBuffer(buffer);
543 void InsetInclude::getLabelList(Buffer const & buffer,
544 std::vector<string> & list) const
546 if (loadIfNeeded(buffer, params_)) {
547 string const included_file = includedFilename(buffer, params_);
548 Buffer * tmp = bufferlist.getBuffer(included_file);
549 tmp->setParentName("");
550 tmp->getLabelList(list);
551 tmp->setParentName(parentFilename(buffer));
556 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
557 std::vector<std::pair<string,string> > & keys) const
559 if (loadIfNeeded(buffer, params_)) {
560 string const included_file = includedFilename(buffer, params_);
561 Buffer * tmp = bufferlist.getBuffer(included_file);
562 tmp->setParentName("");
563 tmp->fillWithBibKeys(keys);
564 tmp->setParentName(parentFilename(buffer));
569 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
571 if (RenderPreview::activated() && preview_->previewReady()) {
572 preview_->metrics(mi, dim);
576 button_.update(getScreenLabel(*mi.base.bv->buffer()),
577 editable() != NOT_EDITABLE);
579 button_.metrics(mi, dim);
581 int center_indent = type(params_) == INPUT ?
582 0 : (mi.base.textwidth - dim.wid) / 2;
583 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
586 dim.wid = mi.base.textwidth;
591 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
593 setPosCache(pi, x, y);
595 if (!RenderPreview::activated() || !preview_->previewReady()) {
596 button_.draw(pi, x + button_.box().x1, y);
600 preview_->draw(pi, x + button_.box().x1, y);
608 void InsetInclude::fileChanged() const
610 Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
614 Buffer const & buffer = *buffer_ptr;
615 preview_->removePreview(buffer);
616 add_preview(*preview_.get(), *this, buffer);
617 preview_->startLoading(buffer);
623 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
625 string const included_file = includedFilename(buffer, params);
627 return type(params) == INPUT && params.preview() &&
628 IsFileReadable(included_file);
632 string const latex_string(InsetInclude const & inset, Buffer const & buffer)
635 OutputParams runparams;
636 runparams.flavor = OutputParams::LATEX;
637 inset.latex(buffer, os, runparams);
643 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
644 Buffer const & buffer)
646 InsetCommandParams const & params = inset.params();
647 if (RenderPreview::activated() && preview_wanted(params, buffer)) {
648 renderer.setAbsFile(includedFilename(buffer, params));
649 string const snippet = latex_string(inset, buffer);
650 renderer.addPreview(snippet, buffer);
657 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
659 Buffer const & buffer = ploader.buffer();
660 if (preview_wanted(params(), buffer)) {
661 preview_->setAbsFile(includedFilename(buffer, params()));
662 string const snippet = latex_string(*this, buffer);
663 preview_->addPreview(snippet, ploader);
668 string const InsetIncludeMailer::name_("include");
670 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
675 string const InsetIncludeMailer::inset2string(Buffer const &) const
677 return params2string(inset_.params());
681 void InsetIncludeMailer::string2params(string const & in,
682 InsetCommandParams & params)
684 params = InsetCommandParams();
688 istringstream data(in);
694 if (!lex || name != name_)
695 return print_mailer_error("InsetIncludeMailer", in, 1, name_);
697 // This is part of the inset proper that is usually swallowed
698 // by LyXText::readInset
701 if (!lex || id != "Include")
702 return print_mailer_error("InsetBoxMailer", in, 2, "Include");
704 InsetInclude inset(params);
706 params = inset.params();
711 InsetIncludeMailer::params2string(InsetCommandParams const & params)
713 InsetInclude inset(params);
715 data << name_ << ' ';
717 data << "\\end_inset\n";