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 "bufferlist.h"
16 #include "BufferView.h"
18 #include "funcrequest.h"
20 #include "LaTeXFeatures.h"
26 #include "frontends/Dialogs.h"
27 #include "frontends/LyXView.h"
28 #include "frontends/Painter.h"
30 #include "support/filetools.h"
31 #include "support/FileInfo.h"
32 #include "support/FileMonitor.h"
33 #include "support/lstrings.h" // contains
34 #include "support/tostr.h"
36 #include "graphics/PreviewedInset.h"
37 #include "graphics/PreviewImage.h"
39 #include <boost/bind.hpp>
48 extern BufferList bufferlist;
51 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
54 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
57 bool previewWanted() const;
59 string const latexString() const;
61 InsetInclude & parent() const {
62 return *static_cast<InsetInclude*>(inset());
66 bool monitoring() const { return monitor_.get(); }
68 void startMonitoring();
70 void stopMonitoring() { monitor_.reset(); }
73 /// Invoked by monitor_ should the parent file change.
74 void restartLoading();
76 boost::scoped_ptr<FileMonitor> monitor_;
82 string const uniqueID()
84 static unsigned int seed = 1000;
85 return "file" + tostr(++seed);
91 InsetInclude::InsetInclude(Params const & p)
92 : params_(p), include_label(uniqueID()),
93 preview_(new PreviewImpl(*this))
97 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
98 : include_label(uniqueID()),
99 preview_(new PreviewImpl(*this))
102 params_.masterFilename_ = b.fileName();
106 InsetInclude::~InsetInclude()
108 InsetIncludeMailer mailer(*this);
113 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
115 switch (cmd.action) {
117 case LFUN_INSET_MODIFY: {
118 InsetInclude::Params p;
119 InsetIncludeMailer::string2params(cmd.argument, p);
120 if (!p.cparams.getCmdName().empty()) {
122 params_.masterFilename_ = cmd.view()->buffer()->fileName();
123 cmd.view()->updateInset(this);
128 case LFUN_INSET_DIALOG_UPDATE:
129 InsetIncludeMailer(*this).updateDialog(cmd.view());
132 case LFUN_MOUSE_RELEASE:
133 case LFUN_INSET_EDIT:
134 InsetIncludeMailer(*this).showDialog(cmd.view());
143 InsetInclude::Params const & InsetInclude::params() const
149 bool InsetInclude::Params::operator==(Params const & o) const
151 return cparams == o.cparams && flag == o.flag &&
152 masterFilename_ == o.masterFilename_;
156 bool InsetInclude::Params::operator!=(Params const & o) const
158 return !(*this == o);
162 void InsetInclude::set(Params const & p)
168 switch (params_.flag) {
173 command="verbatiminput";
179 command="verbatiminput*";
183 params_.cparams.setCmdName(command);
185 if (preview_->monitoring())
186 preview_->stopMonitoring();
188 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
189 preview_->generatePreview();
193 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
196 p.masterFilename_ = buffer.fileName();
198 return new InsetInclude(p);
202 void InsetInclude::write(Buffer const *, ostream & os) const
204 os << "Include " << params_.cparams.getCommand() << '\n'
205 << "preview " << tostr(params_.cparams.preview()) << '\n';
209 void InsetInclude::read(Buffer const *, LyXLex & lex)
211 params_.cparams.read(lex);
213 if (params_.cparams.getCmdName() == "include")
214 params_.flag = INCLUDE;
215 else if (params_.cparams.getCmdName() == "input")
216 params_.flag = INPUT;
217 /* FIXME: is this logic necessary now ? */
218 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
220 if (params_.cparams.getCmdName() == "verbatiminput*")
221 params_.flag = VERBAST;
226 bool InsetInclude::display() const
228 return !(params_.flag == INPUT);
232 string const InsetInclude::getScreenLabel(Buffer const *) const
236 switch (params_.flag) {
237 case INPUT: temp += _("Input"); break;
238 case VERB: temp += _("Verbatim Input"); break;
239 case VERBAST: temp += _("Verbatim Input*"); break;
240 case INCLUDE: temp += _("Include"); break;
245 if (params_.cparams.getContents().empty())
248 temp += params_.cparams.getContents();
254 string const InsetInclude::getFileName() const
256 return MakeAbsPath(params_.cparams.getContents(),
257 OnlyPath(getMasterFilename()));
261 string const InsetInclude::getMasterFilename() const
263 return params_.masterFilename_;
267 bool InsetInclude::loadIfNeeded() const
272 if (!IsLyXFilename(getFileName()))
275 if (bufferlist.exists(getFileName()))
278 // the readonly flag can/will be wrong, not anymore I think.
279 FileInfo finfo(getFileName());
283 return bufferlist.loadLyXFile(getFileName(), false) != 0;
287 int InsetInclude::latex(Buffer const * buffer, ostream & os,
288 bool /*fragile*/, bool /*fs*/) const
290 string incfile(params_.cparams.getContents());
292 // Do nothing if no file name has been specified
296 if (loadIfNeeded()) {
297 Buffer * tmp = bufferlist.getBuffer(getFileName());
299 // FIXME: this should be a GUI warning
300 if (tmp->params.textclass != buffer->params.textclass) {
301 lyxerr << "WARNING: Included file `"
302 << MakeDisplayPath(getFileName())
303 << "' has textclass `"
304 << tmp->params.getLyXTextClass().name()
305 << "' while parent file has textclass `"
306 << buffer->params.getLyXTextClass().name()
311 // write it to a file (so far the complete file)
312 string writefile = ChangeExtension(getFileName(), ".tex");
314 if (!buffer->tmppath.empty()
315 && !buffer->niceFile) {
316 incfile = subst(incfile, '/','@');
318 incfile = subst(incfile, ':', '$');
320 writefile = AddName(buffer->tmppath, incfile);
322 writefile = getFileName();
323 writefile = ChangeExtension(writefile, ".tex");
324 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
325 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
327 tmp->markDepClean(buffer->tmppath);
329 tmp->makeLaTeXFile(writefile,
330 OnlyPath(getMasterFilename()),
331 buffer->niceFile, true);
335 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
336 } else if (params_.flag == INPUT) {
337 // \input wants file with extension (default is .tex)
338 if (!IsLyXFilename(getFileName())) {
339 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
341 os << '\\' << params_.cparams.getCmdName() << '{'
342 << ChangeExtension(incfile, ".tex")
346 // \include don't want extension and demands that the
347 // file really have .tex
348 os << '\\' << params_.cparams.getCmdName() << '{'
349 << ChangeExtension(incfile, string())
357 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
360 os << GetFileContents(getFileName());
365 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
367 string incfile(params_.cparams.getContents());
369 // Do nothing if no file name has been specified
373 if (loadIfNeeded()) {
374 Buffer * tmp = bufferlist.getBuffer(getFileName());
376 // write it to a file (so far the complete file)
377 string writefile = ChangeExtension(getFileName(), ".sgml");
378 if (!buffer->tmppath.empty() && !buffer->niceFile) {
379 incfile = subst(incfile, '/','@');
380 writefile = AddName(buffer->tmppath, incfile);
382 writefile = getFileName();
384 if (IsLyXFilename(getFileName()))
385 writefile = ChangeExtension(writefile, ".sgml");
387 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
388 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
390 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
395 << GetFileContents(getFileName())
398 os << '&' << include_label << ';';
404 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
405 bool /*mixcont*/) const
407 string incfile(params_.cparams.getContents());
409 // Do nothing if no file name has been specified
413 if (loadIfNeeded()) {
414 Buffer * tmp = bufferlist.getBuffer(getFileName());
416 // write it to a file (so far the complete file)
417 string writefile = ChangeExtension(getFileName(), ".sgml");
418 if (!buffer->tmppath.empty() && !buffer->niceFile) {
419 incfile = subst(incfile, '/','@');
420 writefile = AddName(buffer->tmppath, incfile);
422 writefile = getFileName();
423 if (IsLyXFilename(getFileName()))
424 writefile = ChangeExtension(writefile, ".sgml");
426 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
427 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
429 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
433 os << "<inlinegraphic fileref=\""
434 << '&' << include_label << ';'
435 << "\" format=\"linespecific\">";
437 os << '&' << include_label << ';';
443 void InsetInclude::validate(LaTeXFeatures & features) const
446 string incfile(params_.cparams.getContents());
449 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
451 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
452 incfile = subst(incfile, '/','@');
453 writefile = AddName(b->tmppath, incfile);
455 writefile = getFileName();
457 if (IsLyXFilename(getFileName()))
458 writefile = ChangeExtension(writefile, ".sgml");
460 features.includeFile(include_label, writefile);
463 features.require("verbatim");
465 // Here we must do the fun stuff...
466 // Load the file in the include if it needs
468 if (loadIfNeeded()) {
470 Buffer * const tmp = bufferlist.getBuffer(getFileName());
473 tmp->niceFile = b->niceFile;
474 tmp->validate(features);
480 vector<string> const InsetInclude::getLabelList() const
484 if (loadIfNeeded()) {
485 Buffer * tmp = bufferlist.getBuffer(getFileName());
486 tmp->setParentName("");
487 l = tmp->getLabelList();
488 tmp->setParentName(getMasterFilename());
495 void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
497 if (loadIfNeeded()) {
498 Buffer * tmp = bufferlist.getBuffer(getFileName());
499 tmp->setParentName("");
500 tmp->fillWithBibKeys(keys);
501 tmp->setParentName(getMasterFilename());
506 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
508 return preview_->previewReady() ?
509 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
513 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
515 return preview_->previewReady() ?
516 preview_->pimage()->descent() : InsetButton::descent(bv, font);
520 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
522 return preview_->previewReady() ?
523 preview_->pimage()->width() : InsetButton::width(bv, font);
527 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
531 if (!preview_->previewReady()) {
532 InsetButton::draw(bv, font, y, xx);
536 if (!preview_->monitoring())
537 preview_->startMonitoring();
539 int const x = int(xx);
540 int const w = width(bv, font);
541 int const d = descent(bv, font);
542 int const a = ascent(bv, font);
545 bv->painter().image(x, y - a, w, h,
546 *(preview_->pimage()->image()));
556 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
558 preview_->addPreview(ploader);
562 bool InsetInclude::PreviewImpl::previewWanted() const
564 return parent().params_.flag == InsetInclude::INPUT &&
565 parent().params_.cparams.preview() &&
566 IsFileReadable(parent().getFileName());
570 string const InsetInclude::PreviewImpl::latexString() const
572 if (!view() || !view()->buffer())
576 parent().latex(view()->buffer(), os, false, false);
578 return STRCONV(os.str());
582 void InsetInclude::PreviewImpl::startMonitoring()
584 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
585 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
590 void InsetInclude::PreviewImpl::restartLoading()
592 lyxerr << "restartLoading()" << std::endl;
595 view()->updateInset(&parent());
600 string const InsetIncludeMailer::name_("include");
602 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
607 string const InsetIncludeMailer::inset2string() const
609 return params2string(inset_.params());
613 void InsetIncludeMailer::string2params(string const & in,
614 InsetInclude::Params & params)
616 params = InsetInclude::Params();
621 istringstream data(STRCONV(in));
627 string const token = lex.getString();
632 // This is part of the inset proper that is usually swallowed
633 // by Buffer::readInset
636 string const token = lex.getString();
637 if (token != "Include")
642 InsetInclude inset(params);
644 params = inset.params();
650 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
652 InsetInclude inset(params);
655 data << name_ << ' ';
656 inset.write(0, data);
657 data << "\\end_inset\n";
658 return STRCONV(data.str());