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 "BufferView.h"
20 #include "funcrequest.h"
22 #include "LaTeXFeatures.h"
23 #include "latexrunparams.h"
25 #include "metricsinfo.h"
27 #include "frontends/Painter.h"
29 #include "graphics/PreviewedInset.h"
30 #include "graphics/PreviewImage.h"
32 #include "support/FileInfo.h"
33 #include "support/FileMonitor.h"
34 #include "support/filetools.h"
35 #include "support/lstrings.h" // contains
36 #include "support/tostr.h"
38 #include <boost/bind.hpp>
42 using namespace lyx::support;
51 extern BufferList bufferlist;
54 class InsetInclude::PreviewImpl : public lyx::graphics::PreviewedInset {
57 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
60 bool previewWanted() const;
62 string const latexString() const;
64 InsetInclude & parent() const {
65 return *static_cast<InsetInclude*>(inset());
69 bool monitoring() const { return monitor_.get(); }
71 void startMonitoring();
73 void stopMonitoring() { monitor_.reset(); }
76 /// Invoked by monitor_ should the parent file change.
77 void restartLoading();
79 boost::scoped_ptr<FileMonitor> monitor_;
85 string const uniqueID()
87 static unsigned int seed = 1000;
88 return "file" + tostr(++seed);
94 InsetInclude::InsetInclude(Params const & p)
95 : params_(p), include_label(uniqueID()),
96 preview_(new PreviewImpl(*this)),
101 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
102 : include_label(uniqueID()),
103 preview_(new PreviewImpl(*this)),
107 params_.masterFilename_ = b.fileName();
111 InsetInclude::InsetInclude(InsetInclude const & other)
113 params_(other.params_),
114 include_label(other.include_label),
115 preview_(new PreviewImpl(*this)),
116 set_label_(other.set_label_)
120 InsetInclude::~InsetInclude()
122 InsetIncludeMailer mailer(*this);
127 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
129 switch (cmd.action) {
131 case LFUN_INSET_MODIFY: {
132 InsetInclude::Params p;
133 InsetIncludeMailer::string2params(cmd.argument, p);
134 if (!p.cparams.getCmdName().empty()) {
136 params_.masterFilename_ = cmd.view()->buffer()->fileName();
137 cmd.view()->updateInset(this);
142 case LFUN_INSET_DIALOG_UPDATE:
143 InsetIncludeMailer(*this).updateDialog(cmd.view());
146 case LFUN_MOUSE_RELEASE:
147 if (button_.box().contains(cmd.x, cmd.y))
148 InsetIncludeMailer(*this).showDialog(cmd.view());
151 case LFUN_INSET_DIALOG_SHOW:
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
227 void InsetInclude::write(ostream & os) const
229 os << "Include " << params_.cparams.getCommand() << '\n'
230 << "preview " << tostr(params_.cparams.preview()) << '\n';
234 void InsetInclude::read(Buffer const &, LyXLex & lex)
240 void InsetInclude::read(LyXLex & lex)
242 params_.cparams.read(lex);
244 if (params_.cparams.getCmdName() == "include")
245 params_.flag = INCLUDE;
246 else if (params_.cparams.getCmdName() == "input")
247 params_.flag = INPUT;
248 /* FIXME: is this logic necessary now ? */
249 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
251 if (params_.cparams.getCmdName() == "verbatiminput*")
252 params_.flag = VERBAST;
257 string const InsetInclude::getScreenLabel(Buffer const &) const
261 switch (params_.flag) {
262 case INPUT: temp += _("Input"); break;
263 case VERB: temp += _("Verbatim Input"); break;
264 case VERBAST: temp += _("Verbatim Input*"); break;
265 case INCLUDE: temp += _("Include"); break;
270 if (params_.cparams.getContents().empty())
273 temp += params_.cparams.getContents();
279 string const InsetInclude::getFileName() const
281 return MakeAbsPath(params_.cparams.getContents(),
282 OnlyPath(getMasterFilename()));
286 string const InsetInclude::getMasterFilename() const
288 return params_.masterFilename_;
292 bool InsetInclude::loadIfNeeded() const
297 if (!IsLyXFilename(getFileName()))
300 if (bufferlist.exists(getFileName()))
303 // the readonly flag can/will be wrong, not anymore I think.
304 FileInfo finfo(getFileName());
307 return loadLyXFile(bufferlist.newBuffer(getFileName()),
312 int InsetInclude::latex(Buffer const & buffer, ostream & os,
313 LatexRunParams const & runparams) const
315 string incfile(params_.cparams.getContents());
317 // Do nothing if no file name has been specified
321 if (loadIfNeeded()) {
322 Buffer * tmp = bufferlist.getBuffer(getFileName());
324 // FIXME: this should be a GUI warning
325 if (tmp->params.textclass != buffer.params.textclass) {
326 lyxerr << "WARNING: Included file `"
327 << MakeDisplayPath(getFileName())
328 << "' has textclass `"
329 << tmp->params.getLyXTextClass().name()
330 << "' while parent file has textclass `"
331 << buffer.params.getLyXTextClass().name()
336 // write it to a file (so far the complete file)
337 string writefile = ChangeExtension(getFileName(), ".tex");
339 if (!buffer.tmppath.empty() && !runparams.nice) {
340 incfile = subst(incfile, '/','@');
342 incfile = subst(incfile, ':', '$');
344 writefile = AddName(buffer.tmppath, incfile);
346 writefile = getFileName();
347 writefile = ChangeExtension(writefile, ".tex");
348 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
349 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
351 tmp->markDepClean(buffer.tmppath);
353 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
358 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
359 } else if (params_.flag == INPUT) {
360 // \input wants file with extension (default is .tex)
361 if (!IsLyXFilename(getFileName())) {
362 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
364 os << '\\' << params_.cparams.getCmdName() << '{'
365 << ChangeExtension(incfile, ".tex")
369 // \include don't want extension and demands that the
370 // file really have .tex
371 os << '\\' << params_.cparams.getCmdName() << '{'
372 << ChangeExtension(incfile, string())
380 int InsetInclude::ascii(Buffer const &, ostream & os, int) const
383 os << GetFileContents(getFileName());
388 int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os) const
390 string incfile(params_.cparams.getContents());
392 // Do nothing if no file name has been specified
396 if (loadIfNeeded()) {
397 Buffer * tmp = bufferlist.getBuffer(getFileName());
399 // write it to a file (so far the complete file)
400 string writefile = ChangeExtension(getFileName(), ".sgml");
401 if (!buffer.tmppath.empty() && !buffer.niceFile) {
402 incfile = subst(incfile, '/','@');
403 writefile = AddName(buffer.tmppath, incfile);
405 writefile = getFileName();
407 if (IsLyXFilename(getFileName()))
408 writefile = ChangeExtension(writefile, ".sgml");
410 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
411 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
413 tmp->makeLinuxDocFile(writefile, buffer.niceFile, true);
418 << GetFileContents(getFileName())
421 os << '&' << include_label << ';';
427 int InsetInclude::docbook(Buffer const & buffer, ostream & os,
428 bool /*mixcont*/) const
430 string incfile(params_.cparams.getContents());
432 // Do nothing if no file name has been specified
436 if (loadIfNeeded()) {
437 Buffer * tmp = bufferlist.getBuffer(getFileName());
439 // write it to a file (so far the complete file)
440 string writefile = ChangeExtension(getFileName(), ".sgml");
441 if (!buffer.tmppath.empty() && !buffer.niceFile) {
442 incfile = subst(incfile, '/','@');
443 writefile = AddName(buffer.tmppath, incfile);
445 writefile = getFileName();
446 if (IsLyXFilename(getFileName()))
447 writefile = ChangeExtension(writefile, ".sgml");
449 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
450 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
452 tmp->makeDocBookFile(writefile, buffer.niceFile, true);
456 os << "<inlinegraphic fileref=\""
457 << '&' << include_label << ';'
458 << "\" format=\"linespecific\">";
460 os << '&' << include_label << ';';
466 void InsetInclude::validate(LaTeXFeatures & features) const
469 string incfile(params_.cparams.getContents());
472 Buffer const & b = *bufferlist.getBuffer(getMasterFilename());
474 if (!b.tmppath.empty() && !b.niceFile && !isVerbatim()) {
475 incfile = subst(incfile, '/','@');
476 writefile = AddName(b.tmppath, incfile);
478 writefile = getFileName();
480 if (IsLyXFilename(getFileName()))
481 writefile = ChangeExtension(writefile, ".sgml");
483 features.includeFile(include_label, writefile);
486 features.require("verbatim");
488 // Here we must do the fun stuff...
489 // Load the file in the include if it needs
491 if (loadIfNeeded()) {
493 Buffer * const tmp = bufferlist.getBuffer(getFileName());
495 tmp->niceFile = b.niceFile;
496 tmp->validate(features);
502 void InsetInclude::getLabelList(std::vector<string> & list) const
504 if (loadIfNeeded()) {
505 Buffer * tmp = bufferlist.getBuffer(getFileName());
506 tmp->setParentName("");
507 tmp->getLabelList(list);
508 tmp->setParentName(getMasterFilename());
513 void InsetInclude::fillWithBibKeys(std::vector<std::pair<string,string> > & keys) const
515 if (loadIfNeeded()) {
516 Buffer * tmp = bufferlist.getBuffer(getFileName());
517 tmp->setParentName("");
518 tmp->fillWithBibKeys(keys);
519 tmp->setParentName(getMasterFilename());
524 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
526 if (preview_->previewReady()) {
527 dim.asc = preview_->pimage()->ascent();
528 dim.des = preview_->pimage()->descent();
529 dim.wid = preview_->pimage()->width();
533 button_.update(getScreenLabel(*mi.base.bv->buffer()),
534 editable() != NOT_EDITABLE);
536 button_.metrics(mi, dim);
538 int center_indent = (params_.flag == INPUT ? 0 :
539 (mi.base.textwidth - dim.wid) / 2);
540 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
543 dim.wid = mi.base.textwidth;
548 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
551 if (!preview_->previewReady()) {
552 button_.draw(pi, x + button_.box().x1, y);
556 if (!preview_->monitoring())
557 preview_->startMonitoring();
559 pi.pain.image(x + button_.box().x1, y - dim_.asc, dim_.wid, dim_.height(),
560 *(preview_->pimage()->image()));
564 BufferView * InsetInclude::view() const
566 return button_.view();
574 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
576 preview_->addPreview(ploader);
580 bool InsetInclude::PreviewImpl::previewWanted() const
582 return parent().params_.flag == InsetInclude::INPUT &&
583 parent().params_.cparams.preview() &&
584 IsFileReadable(parent().getFileName());
588 string const InsetInclude::PreviewImpl::latexString() const
590 if (!view() || !view()->buffer())
594 LatexRunParams runparams;
595 runparams.flavor = LatexRunParams::LATEX;
596 parent().latex(*view()->buffer(), os, runparams);
598 return STRCONV(os.str());
602 void InsetInclude::PreviewImpl::startMonitoring()
604 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
605 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
610 void InsetInclude::PreviewImpl::restartLoading()
612 lyxerr << "restartLoading()" << std::endl;
615 view()->updateInset(&parent());
620 string const InsetIncludeMailer::name_("include");
622 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
627 string const InsetIncludeMailer::inset2string(Buffer const &) const
629 return params2string(inset_.params());
633 void InsetIncludeMailer::string2params(string const & in,
634 InsetInclude::Params & params)
636 params = InsetInclude::Params();
641 istringstream data(STRCONV(in));
647 string const token = lex.getString();
652 // This is part of the inset proper that is usually swallowed
653 // by Buffer::readInset
656 string const token = lex.getString();
657 if (token != "Include")
662 InsetInclude inset(params);
664 params = inset.params();
670 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
672 InsetInclude inset(params);
675 data << name_ << ' ';
677 data << "\\end_inset\n";
678 return STRCONV(data.str());