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"
21 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
24 #include "latexrunparams.h"
26 #include "metricsinfo.h"
28 #include "frontends/Painter.h"
30 #include "graphics/PreviewedInset.h"
31 #include "graphics/PreviewImage.h"
33 #include "support/FileInfo.h"
34 #include "support/FileMonitor.h"
35 #include "support/filetools.h"
36 #include "support/lstrings.h" // contains
37 #include "support/tostr.h"
39 #include <boost/bind.hpp>
41 #include "support/std_sstream.h"
43 using lyx::support::AddName;
44 using lyx::support::ChangeExtension;
45 using lyx::support::contains;
46 using lyx::support::FileInfo;
47 using lyx::support::FileMonitor;
48 using lyx::support::GetFileContents;
49 using lyx::support::IsFileReadable;
50 using lyx::support::IsLyXFilename;
51 using lyx::support::MakeAbsPath;
52 using lyx::support::MakeDisplayPath;
53 using lyx::support::OnlyPath;
54 using lyx::support::subst;
59 using std::istringstream;
61 using std::ostringstream;
64 extern BufferList bufferlist;
67 class InsetInclude::PreviewImpl : public lyx::graphics::PreviewedInset {
70 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
73 bool previewWanted() const;
75 string const latexString() const;
77 InsetInclude const & parent() const {
78 return dynamic_cast<InsetInclude const &>(inset());
82 bool monitoring() const { return monitor_.get(); }
84 void startMonitoring();
86 void stopMonitoring() { monitor_.reset(); }
89 /// Invoked by monitor_ should the parent file change.
90 void restartLoading();
92 boost::scoped_ptr<FileMonitor> monitor_;
98 string const uniqueID()
100 static unsigned int seed = 1000;
101 return "file" + tostr(++seed);
107 InsetInclude::InsetInclude(Params const & p)
108 : params_(p), include_label(uniqueID()),
109 preview_(new PreviewImpl(*this)),
114 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
115 : include_label(uniqueID()),
116 preview_(new PreviewImpl(*this)),
120 params_.masterFilename_ = b.fileName();
124 InsetInclude::InsetInclude(InsetInclude const & other)
126 params_(other.params_),
127 include_label(other.include_label),
128 preview_(new PreviewImpl(*this)),
129 set_label_(other.set_label_)
133 InsetInclude::~InsetInclude()
135 InsetIncludeMailer mailer(*this);
140 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
142 switch (cmd.action) {
144 case LFUN_INSET_MODIFY: {
145 InsetInclude::Params p;
146 InsetIncludeMailer::string2params(cmd.argument, p);
147 if (!p.cparams.getCmdName().empty()) {
148 p.masterFilename_ = cmd.view()->buffer()->fileName();
150 cmd.view()->updateInset(this);
155 case LFUN_INSET_DIALOG_UPDATE:
156 InsetIncludeMailer(*this).updateDialog(cmd.view());
159 case LFUN_MOUSE_RELEASE:
160 if (button_.box().contains(cmd.x, cmd.y))
161 InsetIncludeMailer(*this).showDialog(cmd.view());
164 case LFUN_INSET_DIALOG_SHOW:
165 InsetIncludeMailer(*this).showDialog(cmd.view());
169 return InsetOld::localDispatch(cmd);
174 InsetInclude::Params const & InsetInclude::params() const
180 bool InsetInclude::Params::operator==(Params const & o) const
182 return cparams == o.cparams && flag == o.flag &&
183 masterFilename_ == o.masterFilename_;
187 bool InsetInclude::Params::operator!=(Params const & o) const
189 return !(*this == o);
193 void InsetInclude::set(Params const & p)
199 switch (params_.flag) {
204 command="verbatiminput";
210 command="verbatiminput*";
214 params_.cparams.setCmdName(command);
216 if (preview_->monitoring())
217 preview_->stopMonitoring();
219 if (lyx::graphics::PreviewedInset::activated() && params_.flag == INPUT)
220 preview_->generatePreview();
224 auto_ptr<InsetBase> InsetInclude::clone() const
227 //p.masterFilename_ = buffer.fileName();
228 #warning FIXME: broken cross-doc copy/paste - must fix
230 return auto_ptr<InsetBase>(new InsetInclude(params_));
234 void InsetInclude::write(Buffer const &, ostream & os) const
240 void InsetInclude::write(ostream & os) const
242 os << "Include " << params_.cparams.getCommand() << '\n'
243 << "preview " << tostr(params_.cparams.preview()) << '\n';
247 void InsetInclude::read(Buffer const &, LyXLex & lex)
253 void InsetInclude::read(LyXLex & lex)
255 params_.cparams.read(lex);
257 if (params_.cparams.getCmdName() == "include")
258 params_.flag = INCLUDE;
259 else if (params_.cparams.getCmdName() == "input")
260 params_.flag = INPUT;
261 /* FIXME: is this logic necessary now ? */
262 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
264 if (params_.cparams.getCmdName() == "verbatiminput*")
265 params_.flag = VERBAST;
270 string const InsetInclude::getScreenLabel(Buffer const &) const
274 switch (params_.flag) {
275 case INPUT: temp += _("Input"); break;
276 case VERB: temp += _("Verbatim Input"); break;
277 case VERBAST: temp += _("Verbatim Input*"); break;
278 case INCLUDE: temp += _("Include"); break;
283 if (params_.cparams.getContents().empty())
286 temp += params_.cparams.getContents();
292 string const InsetInclude::getFileName() const
294 return MakeAbsPath(params_.cparams.getContents(),
295 OnlyPath(getMasterFilename()));
299 string const InsetInclude::getMasterFilename() const
301 return params_.masterFilename_;
305 bool InsetInclude::loadIfNeeded() const
310 if (!IsLyXFilename(getFileName()))
313 if (bufferlist.exists(getFileName()))
316 // the readonly flag can/will be wrong, not anymore I think.
317 FileInfo finfo(getFileName());
320 return loadLyXFile(bufferlist.newBuffer(getFileName()),
325 int InsetInclude::latex(Buffer const & buffer, ostream & os,
326 LatexRunParams const & runparams) const
328 string incfile(params_.cparams.getContents());
330 // Do nothing if no file name has been specified
334 if (loadIfNeeded()) {
335 Buffer * tmp = bufferlist.getBuffer(getFileName());
337 // FIXME: this should be a GUI warning
338 if (tmp->params().textclass != buffer.params().textclass) {
339 lyxerr << "WARNING: Included file `"
340 << MakeDisplayPath(getFileName())
341 << "' has textclass `"
342 << tmp->params().getLyXTextClass().name()
343 << "' while parent file has textclass `"
344 << buffer.params().getLyXTextClass().name()
349 // write it to a file (so far the complete file)
350 string writefile = ChangeExtension(getFileName(), ".tex");
352 if (!buffer.temppath().empty() && !runparams.nice) {
353 incfile = subst(incfile, '/','@');
355 incfile = subst(incfile, ':', '$');
357 writefile = AddName(buffer.temppath(), incfile);
359 writefile = getFileName();
360 writefile = ChangeExtension(writefile, ".tex");
361 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
362 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
364 tmp->markDepClean(buffer.temppath());
366 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
371 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
372 } else if (params_.flag == INPUT) {
373 // \input wants file with extension (default is .tex)
374 if (!IsLyXFilename(getFileName())) {
375 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
377 os << '\\' << params_.cparams.getCmdName() << '{'
378 << ChangeExtension(incfile, ".tex")
382 // \include don't want extension and demands that the
383 // file really have .tex
384 os << '\\' << params_.cparams.getCmdName() << '{'
385 << ChangeExtension(incfile, string())
393 int InsetInclude::ascii(Buffer const &, ostream & os, int) const
396 os << GetFileContents(getFileName());
401 int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os) const
403 string incfile(params_.cparams.getContents());
405 // Do nothing if no file name has been specified
409 if (loadIfNeeded()) {
410 Buffer * tmp = bufferlist.getBuffer(getFileName());
412 // write it to a file (so far the complete file)
413 string writefile = ChangeExtension(getFileName(), ".sgml");
414 if (!buffer.temppath().empty() && !buffer.niceFile()) {
415 incfile = subst(incfile, '/','@');
416 writefile = AddName(buffer.temppath(), incfile);
418 writefile = getFileName();
420 if (IsLyXFilename(getFileName()))
421 writefile = ChangeExtension(writefile, ".sgml");
423 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
424 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
426 tmp->makeLinuxDocFile(writefile, buffer.niceFile(), true);
431 << GetFileContents(getFileName())
434 os << '&' << include_label << ';';
440 int InsetInclude::docbook(Buffer const & buffer, ostream & os,
441 bool /*mixcont*/) const
443 string incfile(params_.cparams.getContents());
445 // Do nothing if no file name has been specified
449 if (loadIfNeeded()) {
450 Buffer * tmp = bufferlist.getBuffer(getFileName());
452 // write it to a file (so far the complete file)
453 string writefile = ChangeExtension(getFileName(), ".sgml");
454 if (!buffer.temppath().empty() && !buffer.niceFile()) {
455 incfile = subst(incfile, '/','@');
456 writefile = AddName(buffer.temppath(), incfile);
458 writefile = getFileName();
459 if (IsLyXFilename(getFileName()))
460 writefile = ChangeExtension(writefile, ".sgml");
462 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
463 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
465 tmp->makeDocBookFile(writefile, buffer.niceFile(), true);
469 os << "<inlinegraphic fileref=\""
470 << '&' << include_label << ';'
471 << "\" format=\"linespecific\">";
473 os << '&' << include_label << ';';
479 void InsetInclude::validate(LaTeXFeatures & features) const
482 string incfile(params_.cparams.getContents());
485 Buffer const * buffer_ptr = bufferlist.getBuffer(getMasterFilename());
486 BOOST_ASSERT(buffer_ptr);
487 Buffer const & b = *buffer_ptr;
489 if (!b.temppath().empty() && !b.niceFile() && !isVerbatim()) {
490 incfile = subst(incfile, '/','@');
491 writefile = AddName(b.temppath(), incfile);
493 writefile = getFileName();
495 if (IsLyXFilename(getFileName()))
496 writefile = ChangeExtension(writefile, ".sgml");
498 features.includeFile(include_label, writefile);
501 features.require("verbatim");
503 // Here we must do the fun stuff...
504 // Load the file in the include if it needs
506 if (loadIfNeeded()) {
508 Buffer * const tmp = bufferlist.getBuffer(getFileName());
510 tmp->niceFile() = b.niceFile();
511 tmp->validate(features);
517 void InsetInclude::getLabelList(std::vector<string> & list) const
519 if (loadIfNeeded()) {
520 Buffer * tmp = bufferlist.getBuffer(getFileName());
521 tmp->setParentName("");
522 tmp->getLabelList(list);
523 tmp->setParentName(getMasterFilename());
528 void InsetInclude::fillWithBibKeys(std::vector<std::pair<string,string> > & keys) const
530 if (loadIfNeeded()) {
531 Buffer * tmp = bufferlist.getBuffer(getFileName());
532 tmp->setParentName("");
533 tmp->fillWithBibKeys(keys);
534 tmp->setParentName(getMasterFilename());
539 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
541 if (preview_->previewReady()) {
542 dim.asc = preview_->pimage()->ascent();
543 dim.des = preview_->pimage()->descent();
544 dim.wid = preview_->pimage()->width();
548 button_.update(getScreenLabel(*mi.base.bv->buffer()),
549 editable() != NOT_EDITABLE);
551 button_.metrics(mi, dim);
553 int center_indent = (params_.flag == INPUT ? 0 :
554 (mi.base.textwidth - dim.wid) / 2);
555 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
558 dim.wid = mi.base.textwidth;
563 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
566 if (!preview_->previewReady()) {
567 button_.draw(pi, x + button_.box().x1, y);
571 if (!preview_->monitoring())
572 preview_->startMonitoring();
574 pi.pain.image(x + button_.box().x1, y - dim_.asc, dim_.wid, dim_.height(),
575 *(preview_->pimage()->image()));
579 BufferView * InsetInclude::view() const
581 return button_.view();
589 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
591 preview_->addPreview(ploader);
595 bool InsetInclude::PreviewImpl::previewWanted() const
597 return parent().params_.flag == InsetInclude::INPUT &&
598 parent().params_.cparams.preview() &&
599 IsFileReadable(parent().getFileName());
603 string const InsetInclude::PreviewImpl::latexString() const
605 if (!view() || !view()->buffer())
609 LatexRunParams runparams;
610 runparams.flavor = LatexRunParams::LATEX;
611 parent().latex(*view()->buffer(), os, runparams);
617 void InsetInclude::PreviewImpl::startMonitoring()
619 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
620 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
625 void InsetInclude::PreviewImpl::restartLoading()
629 view()->updateInset(&parent());
634 string const InsetIncludeMailer::name_("include");
636 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
641 string const InsetIncludeMailer::inset2string(Buffer const &) const
643 return params2string(inset_.params());
647 void InsetIncludeMailer::string2params(string const & in,
648 InsetInclude::Params & params)
650 params = InsetInclude::Params();
655 istringstream data(in);
661 string const token = lex.getString();
666 // This is part of the inset proper that is usually swallowed
667 // by Buffer::readInset
670 string const token = lex.getString();
671 if (token != "Include")
676 InsetInclude inset(params);
678 params = inset.params();
684 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
686 InsetInclude inset(params);
689 data << name_ << ' ';
691 data << "\\end_inset\n";