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>
40 #include "support/std_sstream.h"
42 using namespace lyx::support;
47 using std::istringstream;
49 using std::ostringstream;
52 extern BufferList bufferlist;
55 class InsetInclude::PreviewImpl : public lyx::graphics::PreviewedInset {
58 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
61 bool previewWanted() const;
63 string const latexString() const;
65 InsetInclude & parent() const {
66 return *static_cast<InsetInclude*>(inset());
70 bool monitoring() const { return monitor_.get(); }
72 void startMonitoring();
74 void stopMonitoring() { monitor_.reset(); }
77 /// Invoked by monitor_ should the parent file change.
78 void restartLoading();
80 boost::scoped_ptr<FileMonitor> monitor_;
86 string const uniqueID()
88 static unsigned int seed = 1000;
89 return "file" + tostr(++seed);
95 InsetInclude::InsetInclude(Params const & p)
96 : params_(p), include_label(uniqueID()),
97 preview_(new PreviewImpl(*this)),
102 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
103 : include_label(uniqueID()),
104 preview_(new PreviewImpl(*this)),
108 params_.masterFilename_ = b.fileName();
112 InsetInclude::InsetInclude(InsetInclude const & other)
114 params_(other.params_),
115 include_label(other.include_label),
116 preview_(new PreviewImpl(*this)),
117 set_label_(other.set_label_)
121 InsetInclude::~InsetInclude()
123 InsetIncludeMailer mailer(*this);
128 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
130 switch (cmd.action) {
132 case LFUN_INSET_MODIFY: {
133 InsetInclude::Params p;
134 InsetIncludeMailer::string2params(cmd.argument, p);
135 if (!p.cparams.getCmdName().empty()) {
137 params_.masterFilename_ = cmd.view()->buffer()->fileName();
138 cmd.view()->updateInset(this);
143 case LFUN_INSET_DIALOG_UPDATE:
144 InsetIncludeMailer(*this).updateDialog(cmd.view());
147 case LFUN_MOUSE_RELEASE:
148 if (button_.box().contains(cmd.x, cmd.y))
149 InsetIncludeMailer(*this).showDialog(cmd.view());
152 case LFUN_INSET_DIALOG_SHOW:
153 InsetIncludeMailer(*this).showDialog(cmd.view());
157 return InsetOld::localDispatch(cmd);
162 InsetInclude::Params const & InsetInclude::params() const
168 bool InsetInclude::Params::operator==(Params const & o) const
170 return cparams == o.cparams && flag == o.flag &&
171 masterFilename_ == o.masterFilename_;
175 bool InsetInclude::Params::operator!=(Params const & o) const
177 return !(*this == o);
181 void InsetInclude::set(Params const & p)
187 switch (params_.flag) {
192 command="verbatiminput";
198 command="verbatiminput*";
202 params_.cparams.setCmdName(command);
204 if (preview_->monitoring())
205 preview_->stopMonitoring();
207 if (lyx::graphics::PreviewedInset::activated() && params_.flag == INPUT)
208 preview_->generatePreview();
212 auto_ptr<InsetBase> InsetInclude::clone() const
215 //p.masterFilename_ = buffer.fileName();
216 #warning FIXME: broken cross-doc copy/paste - must fix
218 return auto_ptr<InsetBase>(new InsetInclude(params_));
222 void InsetInclude::write(Buffer const &, ostream & os) const
228 void InsetInclude::write(ostream & os) const
230 os << "Include " << params_.cparams.getCommand() << '\n'
231 << "preview " << tostr(params_.cparams.preview()) << '\n';
235 void InsetInclude::read(Buffer const &, LyXLex & lex)
241 void InsetInclude::read(LyXLex & lex)
243 params_.cparams.read(lex);
245 if (params_.cparams.getCmdName() == "include")
246 params_.flag = INCLUDE;
247 else if (params_.cparams.getCmdName() == "input")
248 params_.flag = INPUT;
249 /* FIXME: is this logic necessary now ? */
250 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
252 if (params_.cparams.getCmdName() == "verbatiminput*")
253 params_.flag = VERBAST;
258 string const InsetInclude::getScreenLabel(Buffer const &) const
262 switch (params_.flag) {
263 case INPUT: temp += _("Input"); break;
264 case VERB: temp += _("Verbatim Input"); break;
265 case VERBAST: temp += _("Verbatim Input*"); break;
266 case INCLUDE: temp += _("Include"); break;
271 if (params_.cparams.getContents().empty())
274 temp += params_.cparams.getContents();
280 string const InsetInclude::getFileName() const
282 return MakeAbsPath(params_.cparams.getContents(),
283 OnlyPath(getMasterFilename()));
287 string const InsetInclude::getMasterFilename() const
289 return params_.masterFilename_;
293 bool InsetInclude::loadIfNeeded() const
298 if (!IsLyXFilename(getFileName()))
301 if (bufferlist.exists(getFileName()))
304 // the readonly flag can/will be wrong, not anymore I think.
305 FileInfo finfo(getFileName());
308 return loadLyXFile(bufferlist.newBuffer(getFileName()),
313 int InsetInclude::latex(Buffer const & buffer, ostream & os,
314 LatexRunParams const & runparams) const
316 string incfile(params_.cparams.getContents());
318 // Do nothing if no file name has been specified
322 if (loadIfNeeded()) {
323 Buffer * tmp = bufferlist.getBuffer(getFileName());
325 // FIXME: this should be a GUI warning
326 if (tmp->params.textclass != buffer.params.textclass) {
327 lyxerr << "WARNING: Included file `"
328 << MakeDisplayPath(getFileName())
329 << "' has textclass `"
330 << tmp->params.getLyXTextClass().name()
331 << "' while parent file has textclass `"
332 << buffer.params.getLyXTextClass().name()
337 // write it to a file (so far the complete file)
338 string writefile = ChangeExtension(getFileName(), ".tex");
340 if (!buffer.tmppath.empty() && !runparams.nice) {
341 incfile = subst(incfile, '/','@');
343 incfile = subst(incfile, ':', '$');
345 writefile = AddName(buffer.tmppath, incfile);
347 writefile = getFileName();
348 writefile = ChangeExtension(writefile, ".tex");
349 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
350 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
352 tmp->markDepClean(buffer.tmppath);
354 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
359 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
360 } else if (params_.flag == INPUT) {
361 // \input wants file with extension (default is .tex)
362 if (!IsLyXFilename(getFileName())) {
363 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
365 os << '\\' << params_.cparams.getCmdName() << '{'
366 << ChangeExtension(incfile, ".tex")
370 // \include don't want extension and demands that the
371 // file really have .tex
372 os << '\\' << params_.cparams.getCmdName() << '{'
373 << ChangeExtension(incfile, string())
381 int InsetInclude::ascii(Buffer const &, ostream & os, int) const
384 os << GetFileContents(getFileName());
389 int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os) const
391 string incfile(params_.cparams.getContents());
393 // Do nothing if no file name has been specified
397 if (loadIfNeeded()) {
398 Buffer * tmp = bufferlist.getBuffer(getFileName());
400 // write it to a file (so far the complete file)
401 string writefile = ChangeExtension(getFileName(), ".sgml");
402 if (!buffer.tmppath.empty() && !buffer.niceFile) {
403 incfile = subst(incfile, '/','@');
404 writefile = AddName(buffer.tmppath, incfile);
406 writefile = getFileName();
408 if (IsLyXFilename(getFileName()))
409 writefile = ChangeExtension(writefile, ".sgml");
411 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
412 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
414 tmp->makeLinuxDocFile(writefile, buffer.niceFile, true);
419 << GetFileContents(getFileName())
422 os << '&' << include_label << ';';
428 int InsetInclude::docbook(Buffer const & buffer, ostream & os,
429 bool /*mixcont*/) const
431 string incfile(params_.cparams.getContents());
433 // Do nothing if no file name has been specified
437 if (loadIfNeeded()) {
438 Buffer * tmp = bufferlist.getBuffer(getFileName());
440 // write it to a file (so far the complete file)
441 string writefile = ChangeExtension(getFileName(), ".sgml");
442 if (!buffer.tmppath.empty() && !buffer.niceFile) {
443 incfile = subst(incfile, '/','@');
444 writefile = AddName(buffer.tmppath, incfile);
446 writefile = getFileName();
447 if (IsLyXFilename(getFileName()))
448 writefile = ChangeExtension(writefile, ".sgml");
450 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
451 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
453 tmp->makeDocBookFile(writefile, buffer.niceFile, true);
457 os << "<inlinegraphic fileref=\""
458 << '&' << include_label << ';'
459 << "\" format=\"linespecific\">";
461 os << '&' << include_label << ';';
467 void InsetInclude::validate(LaTeXFeatures & features) const
470 string incfile(params_.cparams.getContents());
473 Buffer const & b = *bufferlist.getBuffer(getMasterFilename());
475 if (!b.tmppath.empty() && !b.niceFile && !isVerbatim()) {
476 incfile = subst(incfile, '/','@');
477 writefile = AddName(b.tmppath, incfile);
479 writefile = getFileName();
481 if (IsLyXFilename(getFileName()))
482 writefile = ChangeExtension(writefile, ".sgml");
484 features.includeFile(include_label, writefile);
487 features.require("verbatim");
489 // Here we must do the fun stuff...
490 // Load the file in the include if it needs
492 if (loadIfNeeded()) {
494 Buffer * const tmp = bufferlist.getBuffer(getFileName());
496 tmp->niceFile = b.niceFile;
497 tmp->validate(features);
503 void InsetInclude::getLabelList(std::vector<string> & list) const
505 if (loadIfNeeded()) {
506 Buffer * tmp = bufferlist.getBuffer(getFileName());
507 tmp->setParentName("");
508 tmp->getLabelList(list);
509 tmp->setParentName(getMasterFilename());
514 void InsetInclude::fillWithBibKeys(std::vector<std::pair<string,string> > & keys) const
516 if (loadIfNeeded()) {
517 Buffer * tmp = bufferlist.getBuffer(getFileName());
518 tmp->setParentName("");
519 tmp->fillWithBibKeys(keys);
520 tmp->setParentName(getMasterFilename());
525 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
527 if (preview_->previewReady()) {
528 dim.asc = preview_->pimage()->ascent();
529 dim.des = preview_->pimage()->descent();
530 dim.wid = preview_->pimage()->width();
534 button_.update(getScreenLabel(*mi.base.bv->buffer()),
535 editable() != NOT_EDITABLE);
537 button_.metrics(mi, dim);
539 int center_indent = (params_.flag == INPUT ? 0 :
540 (mi.base.textwidth - dim.wid) / 2);
541 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
544 dim.wid = mi.base.textwidth;
549 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
552 if (!preview_->previewReady()) {
553 button_.draw(pi, x + button_.box().x1, y);
557 if (!preview_->monitoring())
558 preview_->startMonitoring();
560 pi.pain.image(x + button_.box().x1, y - dim_.asc, dim_.wid, dim_.height(),
561 *(preview_->pimage()->image()));
565 BufferView * InsetInclude::view() const
567 return button_.view();
575 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
577 preview_->addPreview(ploader);
581 bool InsetInclude::PreviewImpl::previewWanted() const
583 return parent().params_.flag == InsetInclude::INPUT &&
584 parent().params_.cparams.preview() &&
585 IsFileReadable(parent().getFileName());
589 string const InsetInclude::PreviewImpl::latexString() const
591 if (!view() || !view()->buffer())
595 LatexRunParams runparams;
596 runparams.flavor = LatexRunParams::LATEX;
597 parent().latex(*view()->buffer(), os, runparams);
599 return STRCONV(os.str());
603 void InsetInclude::PreviewImpl::startMonitoring()
605 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
606 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
611 void InsetInclude::PreviewImpl::restartLoading()
613 lyxerr << "restartLoading()" << std::endl;
616 view()->updateInset(&parent());
621 string const InsetIncludeMailer::name_("include");
623 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
628 string const InsetIncludeMailer::inset2string(Buffer const &) const
630 return params2string(inset_.params());
634 void InsetIncludeMailer::string2params(string const & in,
635 InsetInclude::Params & params)
637 params = InsetInclude::Params();
642 istringstream data(STRCONV(in));
648 string const token = lex.getString();
653 // This is part of the inset proper that is usually swallowed
654 // by Buffer::readInset
657 string const token = lex.getString();
658 if (token != "Include")
663 InsetInclude inset(params);
665 params = inset.params();
671 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
673 InsetInclude inset(params);
676 data << name_ << ' ';
678 data << "\\end_inset\n";
679 return STRCONV(data.str());