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"
15 #include "buffer_funcs.h"
16 #include "bufferlist.h"
17 #include "BufferView.h"
19 #include "funcrequest.h"
21 #include "LaTeXFeatures.h"
22 #include "latexrunparams.h"
26 #include "metricsinfo.h"
27 #include "dimension.h"
29 #include "frontends/Dialogs.h"
30 #include "frontends/LyXView.h"
31 #include "frontends/Painter.h"
33 #include "support/filetools.h"
34 #include "support/FileInfo.h"
35 #include "support/FileMonitor.h"
36 #include "support/lstrings.h" // contains
37 #include "support/tostr.h"
39 #include "graphics/PreviewedInset.h"
40 #include "graphics/PreviewImage.h"
42 #include <boost/bind.hpp>
46 using namespace lyx::support;
55 extern BufferList bufferlist;
58 class InsetInclude::PreviewImpl : public lyx::graphics::PreviewedInset {
61 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
64 bool previewWanted() const;
66 string const latexString() const;
68 InsetInclude & parent() const {
69 return *static_cast<InsetInclude*>(inset());
73 bool monitoring() const { return monitor_.get(); }
75 void startMonitoring();
77 void stopMonitoring() { monitor_.reset(); }
80 /// Invoked by monitor_ should the parent file change.
81 void restartLoading();
83 boost::scoped_ptr<FileMonitor> monitor_;
89 string const uniqueID()
91 static unsigned int seed = 1000;
92 return "file" + tostr(++seed);
98 InsetInclude::InsetInclude(Params const & p)
99 : params_(p), include_label(uniqueID()),
100 preview_(new PreviewImpl(*this)),
105 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
106 : include_label(uniqueID()),
107 preview_(new PreviewImpl(*this)),
111 params_.masterFilename_ = b.fileName();
115 InsetInclude::InsetInclude(InsetInclude const & other)
117 params_(other.params_),
118 include_label(other.include_label),
119 preview_(new PreviewImpl(*this)),
120 set_label_(other.set_label_)
124 InsetInclude::~InsetInclude()
126 InsetIncludeMailer mailer(*this);
131 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
133 switch (cmd.action) {
135 case LFUN_INSET_MODIFY: {
136 InsetInclude::Params p;
137 InsetIncludeMailer::string2params(cmd.argument, p);
138 if (!p.cparams.getCmdName().empty()) {
140 params_.masterFilename_ = cmd.view()->buffer()->fileName();
141 cmd.view()->updateInset(this);
146 case LFUN_INSET_DIALOG_UPDATE:
147 InsetIncludeMailer(*this).updateDialog(cmd.view());
150 case LFUN_MOUSE_RELEASE:
151 case LFUN_INSET_EDIT:
152 InsetIncludeMailer(*this).showDialog(cmd.view());
156 return InsetOld::localDispatch(cmd);
161 InsetInclude::Params const & InsetInclude::params() const
167 bool InsetInclude::Params::operator==(Params const & o) const
169 return cparams == o.cparams && flag == o.flag &&
170 masterFilename_ == o.masterFilename_;
174 bool InsetInclude::Params::operator!=(Params const & o) const
176 return !(*this == o);
180 void InsetInclude::set(Params const & p)
186 switch (params_.flag) {
191 command="verbatiminput";
197 command="verbatiminput*";
201 params_.cparams.setCmdName(command);
203 if (preview_->monitoring())
204 preview_->stopMonitoring();
206 if (lyx::graphics::PreviewedInset::activated() && params_.flag == INPUT)
207 preview_->generatePreview();
211 auto_ptr<InsetBase> InsetInclude::clone() const
214 //p.masterFilename_ = buffer.fileName();
215 #warning FIXME: broken cross-doc copy/paste - must fix
217 return auto_ptr<InsetBase>(new InsetInclude(params_));
221 void InsetInclude::write(Buffer const *, ostream & os) const
223 os << "Include " << params_.cparams.getCommand() << '\n'
224 << "preview " << tostr(params_.cparams.preview()) << '\n';
228 void InsetInclude::read(Buffer const *, LyXLex & lex)
230 params_.cparams.read(lex);
232 if (params_.cparams.getCmdName() == "include")
233 params_.flag = INCLUDE;
234 else if (params_.cparams.getCmdName() == "input")
235 params_.flag = INPUT;
236 /* FIXME: is this logic necessary now ? */
237 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
239 if (params_.cparams.getCmdName() == "verbatiminput*")
240 params_.flag = VERBAST;
245 bool InsetInclude::display() const
247 return !(params_.flag == INPUT);
251 string const InsetInclude::getScreenLabel(Buffer const *) const
255 switch (params_.flag) {
256 case INPUT: temp += _("Input"); break;
257 case VERB: temp += _("Verbatim Input"); break;
258 case VERBAST: temp += _("Verbatim Input*"); break;
259 case INCLUDE: temp += _("Include"); break;
264 if (params_.cparams.getContents().empty())
267 temp += params_.cparams.getContents();
273 string const InsetInclude::getFileName() const
275 return MakeAbsPath(params_.cparams.getContents(),
276 OnlyPath(getMasterFilename()));
280 string const InsetInclude::getMasterFilename() const
282 return params_.masterFilename_;
286 bool InsetInclude::loadIfNeeded() const
291 if (!IsLyXFilename(getFileName()))
294 if (bufferlist.exists(getFileName()))
297 // the readonly flag can/will be wrong, not anymore I think.
298 FileInfo finfo(getFileName());
301 return loadLyXFile(bufferlist.newBuffer(getFileName()),
306 int InsetInclude::latex(Buffer const * buffer, ostream & os,
307 LatexRunParams const & runparams) const
309 string incfile(params_.cparams.getContents());
311 // Do nothing if no file name has been specified
315 if (loadIfNeeded()) {
316 Buffer * tmp = bufferlist.getBuffer(getFileName());
318 // FIXME: this should be a GUI warning
319 if (tmp->params.textclass != buffer->params.textclass) {
320 lyxerr << "WARNING: Included file `"
321 << MakeDisplayPath(getFileName())
322 << "' has textclass `"
323 << tmp->params.getLyXTextClass().name()
324 << "' while parent file has textclass `"
325 << buffer->params.getLyXTextClass().name()
330 // write it to a file (so far the complete file)
331 string writefile = ChangeExtension(getFileName(), ".tex");
333 if (!buffer->tmppath.empty() && !runparams.nice) {
334 incfile = subst(incfile, '/','@');
336 incfile = subst(incfile, ':', '$');
338 writefile = AddName(buffer->tmppath, incfile);
340 writefile = getFileName();
341 writefile = ChangeExtension(writefile, ".tex");
342 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
343 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
345 tmp->markDepClean(buffer->tmppath);
347 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
352 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
353 } else if (params_.flag == INPUT) {
354 // \input wants file with extension (default is .tex)
355 if (!IsLyXFilename(getFileName())) {
356 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
358 os << '\\' << params_.cparams.getCmdName() << '{'
359 << ChangeExtension(incfile, ".tex")
363 // \include don't want extension and demands that the
364 // file really have .tex
365 os << '\\' << params_.cparams.getCmdName() << '{'
366 << ChangeExtension(incfile, string())
374 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
377 os << GetFileContents(getFileName());
382 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
384 string incfile(params_.cparams.getContents());
386 // Do nothing if no file name has been specified
390 if (loadIfNeeded()) {
391 Buffer * tmp = bufferlist.getBuffer(getFileName());
393 // write it to a file (so far the complete file)
394 string writefile = ChangeExtension(getFileName(), ".sgml");
395 if (!buffer->tmppath.empty() && !buffer->niceFile) {
396 incfile = subst(incfile, '/','@');
397 writefile = AddName(buffer->tmppath, incfile);
399 writefile = getFileName();
401 if (IsLyXFilename(getFileName()))
402 writefile = ChangeExtension(writefile, ".sgml");
404 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
405 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
407 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
412 << GetFileContents(getFileName())
415 os << '&' << include_label << ';';
421 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
422 bool /*mixcont*/) const
424 string incfile(params_.cparams.getContents());
426 // Do nothing if no file name has been specified
430 if (loadIfNeeded()) {
431 Buffer * tmp = bufferlist.getBuffer(getFileName());
433 // write it to a file (so far the complete file)
434 string writefile = ChangeExtension(getFileName(), ".sgml");
435 if (!buffer->tmppath.empty() && !buffer->niceFile) {
436 incfile = subst(incfile, '/','@');
437 writefile = AddName(buffer->tmppath, incfile);
439 writefile = getFileName();
440 if (IsLyXFilename(getFileName()))
441 writefile = ChangeExtension(writefile, ".sgml");
443 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
444 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
446 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
450 os << "<inlinegraphic fileref=\""
451 << '&' << include_label << ';'
452 << "\" format=\"linespecific\">";
454 os << '&' << include_label << ';';
460 void InsetInclude::validate(LaTeXFeatures & features) const
463 string incfile(params_.cparams.getContents());
466 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
468 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
469 incfile = subst(incfile, '/','@');
470 writefile = AddName(b->tmppath, incfile);
472 writefile = getFileName();
474 if (IsLyXFilename(getFileName()))
475 writefile = ChangeExtension(writefile, ".sgml");
477 features.includeFile(include_label, writefile);
480 features.require("verbatim");
482 // Here we must do the fun stuff...
483 // Load the file in the include if it needs
485 if (loadIfNeeded()) {
487 Buffer * const tmp = bufferlist.getBuffer(getFileName());
490 tmp->niceFile = b->niceFile;
491 tmp->validate(features);
497 void InsetInclude::getLabelList(std::vector<string> & list) const
499 if (loadIfNeeded()) {
500 Buffer * tmp = bufferlist.getBuffer(getFileName());
501 tmp->setParentName("");
502 tmp->getLabelList(list);
503 tmp->setParentName(getMasterFilename());
508 void InsetInclude::fillWithBibKeys(std::vector<std::pair<string,string> > & keys) const
510 if (loadIfNeeded()) {
511 Buffer * tmp = bufferlist.getBuffer(getFileName());
512 tmp->setParentName("");
513 tmp->fillWithBibKeys(keys);
514 tmp->setParentName(getMasterFilename());
519 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
521 if (preview_->previewReady()) {
522 dim.asc = preview_->pimage()->ascent();
523 dim.des = preview_->pimage()->descent();
524 dim.wid = preview_->pimage()->width();
528 button_.update(getScreenLabel(mi.base.bv->buffer()),
529 editable() != NOT_EDITABLE);
531 button_.metrics(mi, dim);
537 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
540 if (!preview_->previewReady()) {
541 button_.draw(pi, x, y);
545 if (!preview_->monitoring())
546 preview_->startMonitoring();
548 pi.pain.image(x, y - dim_.asc, dim_.wid, dim_.height(),
549 *(preview_->pimage()->image()));
553 BufferView * InsetInclude::view() const
555 return button_.view();
563 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
565 preview_->addPreview(ploader);
569 bool InsetInclude::PreviewImpl::previewWanted() const
571 return parent().params_.flag == InsetInclude::INPUT &&
572 parent().params_.cparams.preview() &&
573 IsFileReadable(parent().getFileName());
577 string const InsetInclude::PreviewImpl::latexString() const
579 if (!view() || !view()->buffer())
583 LatexRunParams runparams;
584 runparams.flavor = LatexRunParams::LATEX;
585 parent().latex(view()->buffer(), os, runparams);
587 return STRCONV(os.str());
591 void InsetInclude::PreviewImpl::startMonitoring()
593 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
594 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
599 void InsetInclude::PreviewImpl::restartLoading()
601 lyxerr << "restartLoading()" << std::endl;
604 view()->updateInset(&parent());
609 string const InsetIncludeMailer::name_("include");
611 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
616 string const InsetIncludeMailer::inset2string(Buffer const &) const
618 return params2string(inset_.params());
622 void InsetIncludeMailer::string2params(string const & in,
623 InsetInclude::Params & params)
625 params = InsetInclude::Params();
630 istringstream data(STRCONV(in));
636 string const token = lex.getString();
641 // This is part of the inset proper that is usually swallowed
642 // by Buffer::readInset
645 string const token = lex.getString();
646 if (token != "Include")
651 InsetInclude inset(params);
653 params = inset.params();
659 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
661 InsetInclude inset(params);
664 data << name_ << ' ';
665 inset.write(0, data);
666 data << "\\end_inset\n";
667 return STRCONV(data.str());