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"
27 #include "metricsinfo.h"
29 #include "frontends/LyXView.h"
30 #include "frontends/Painter.h"
32 #include "graphics/PreviewLoader.h"
34 #include "insets/render_preview.h"
36 #include "support/FileInfo.h"
37 #include "support/filetools.h"
38 #include "support/lstrings.h" // contains
39 #include "support/tostr.h"
41 #include <boost/bind.hpp>
43 #include "support/std_ostream.h"
44 #include "support/std_sstream.h"
46 using lyx::support::AddName;
47 using lyx::support::ChangeExtension;
48 using lyx::support::contains;
49 using lyx::support::FileInfo;
50 using lyx::support::GetFileContents;
51 using lyx::support::IsFileReadable;
52 using lyx::support::IsLyXFilename;
53 using lyx::support::MakeAbsPath;
54 using lyx::support::MakeDisplayPath;
55 using lyx::support::OnlyFilename;
56 using lyx::support::OnlyPath;
57 using lyx::support::subst;
62 using std::istringstream;
64 using std::ostringstream;
67 extern BufferList bufferlist;
72 string const uniqueID()
74 static unsigned int seed = 1000;
75 return "file" + tostr(++seed);
81 InsetInclude::InsetInclude(InsetCommandParams const & p)
82 : params_(p), include_label(uniqueID()),
83 preview_(new RenderMonitoredPreview),
86 preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
87 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
91 InsetInclude::InsetInclude(InsetInclude const & other)
93 params_(other.params_),
94 include_label(other.include_label),
95 preview_(new RenderMonitoredPreview),
96 set_label_(other.set_label_)
98 preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
99 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
103 InsetInclude::~InsetInclude()
105 InsetIncludeMailer mailer(*this);
111 InsetInclude::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
113 switch (cmd.action) {
115 case LFUN_INSET_MODIFY: {
116 InsetCommandParams p;
117 InsetIncludeMailer::string2params(cmd.argument, p);
118 if (!p.getCmdName().empty()) {
119 set(p, *cmd.view()->buffer());
120 cmd.view()->updateInset(this);
125 case LFUN_INSET_DIALOG_UPDATE:
126 InsetIncludeMailer(*this).updateDialog(cmd.view());
129 case LFUN_MOUSE_RELEASE:
130 if (button_.box().contains(cmd.x, cmd.y))
131 InsetIncludeMailer(*this).showDialog(cmd.view());
134 case LFUN_INSET_DIALOG_SHOW:
135 InsetIncludeMailer(*this).showDialog(cmd.view());
144 InsetCommandParams const & InsetInclude::params() const
152 /// the type of inclusion
161 Types type(InsetCommandParams const & params)
163 string const command_name = params.getCmdName();
165 if (command_name == "input")
167 if (command_name == "verbatiminput")
169 if (command_name == "verbatiminput*")
175 bool isVerbatim(InsetCommandParams const & params)
177 string const command_name = params.getCmdName();
178 return command_name == "verbatiminput" ||
179 command_name == "verbatiminput*";
183 string const masterFilename(Buffer const & buffer)
185 return buffer.fileName();
189 string const includedFilename(Buffer const & buffer,
190 InsetCommandParams const & params)
192 return MakeAbsPath(params.getContents(),
193 OnlyPath(masterFilename(buffer)));
197 void generate_preview(RenderPreview &, InsetInclude const &, Buffer const &);
202 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
207 if (preview_->monitoring())
208 preview_->stopMonitoring();
210 if (type(params_) == INPUT)
211 generate_preview(*preview_, *this, buffer);
215 auto_ptr<InsetBase> InsetInclude::clone() const
217 return auto_ptr<InsetBase>(new InsetInclude(*this));
221 void InsetInclude::write(Buffer const &, ostream & os) const
227 void InsetInclude::write(ostream & os) const
229 os << "Include " << params_.getCommand() << '\n'
230 << "preview " << tostr(params_.preview()) << '\n';
234 void InsetInclude::read(Buffer const &, LyXLex & lex)
240 void InsetInclude::read(LyXLex & lex)
246 string const InsetInclude::getScreenLabel(Buffer const &) const
250 switch (type(params_)) {
251 case INPUT: temp += _("Input"); break;
252 case VERB: temp += _("Verbatim Input"); break;
253 case VERBAST: temp += _("Verbatim Input*"); break;
254 case INCLUDE: temp += _("Include"); break;
259 if (params_.getContents().empty())
262 temp += OnlyFilename(params_.getContents());
270 /// return true if the file is or got loaded.
271 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
273 if (isVerbatim(params))
276 string const included_file = includedFilename(buffer, params);
277 if (!IsLyXFilename(included_file))
280 if (bufferlist.exists(included_file))
283 // the readonly flag can/will be wrong, not anymore I think.
284 FileInfo finfo(included_file);
287 return loadLyXFile(bufferlist.newBuffer(included_file),
295 int InsetInclude::latex(Buffer const & buffer, ostream & os,
296 LatexRunParams const & runparams) const
298 string incfile(params_.getContents());
300 // Do nothing if no file name has been specified
304 string const included_file = includedFilename(buffer, params_);
306 if (loadIfNeeded(buffer, params_)) {
307 Buffer * tmp = bufferlist.getBuffer(included_file);
309 // FIXME: this should be a GUI warning
310 if (tmp->params().textclass != buffer.params().textclass) {
311 lyxerr << "WARNING: Included file `"
312 << MakeDisplayPath(included_file)
313 << "' has textclass `"
314 << tmp->params().getLyXTextClass().name()
315 << "' while parent file has textclass `"
316 << buffer.params().getLyXTextClass().name()
321 // write it to a file (so far the complete file)
322 string writefile = ChangeExtension(included_file, ".tex");
324 if (!buffer.temppath().empty() && !runparams.nice) {
325 incfile = subst(incfile, '/','@');
327 incfile = subst(incfile, ':', '$');
329 writefile = AddName(buffer.temppath(), incfile);
331 writefile = included_file;
332 writefile = ChangeExtension(writefile, ".tex");
333 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
334 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
336 tmp->markDepClean(buffer.temppath());
338 tmp->makeLaTeXFile(writefile,
339 OnlyPath(masterFilename(buffer)),
343 if (isVerbatim(params_)) {
344 os << '\\' << params_.getCmdName() << '{' << incfile << '}';
345 } else if (type(params_) == INPUT) {
346 // \input wants file with extension (default is .tex)
347 if (!IsLyXFilename(included_file)) {
348 os << '\\' << params_.getCmdName() << '{' << incfile << '}';
350 os << '\\' << params_.getCmdName() << '{'
351 << ChangeExtension(incfile, ".tex")
355 // \include don't want extension and demands that the
356 // file really have .tex
357 os << '\\' << params_.getCmdName() << '{'
358 << ChangeExtension(incfile, string())
366 int InsetInclude::ascii(Buffer const & buffer, ostream & os, int) const
368 if (isVerbatim(params_))
369 os << GetFileContents(includedFilename(buffer, params_));
374 int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os) const
376 string incfile(params_.getContents());
378 // Do nothing if no file name has been specified
382 string const included_file = includedFilename(buffer, params_);
384 if (loadIfNeeded(buffer, params_)) {
385 Buffer * tmp = bufferlist.getBuffer(included_file);
387 // write it to a file (so far the complete file)
388 string writefile = ChangeExtension(included_file, ".sgml");
389 if (!buffer.temppath().empty() && !buffer.niceFile()) {
390 incfile = subst(incfile, '/','@');
391 writefile = AddName(buffer.temppath(), incfile);
393 writefile = included_file;
395 if (IsLyXFilename(included_file))
396 writefile = ChangeExtension(writefile, ".sgml");
398 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
399 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
401 tmp->makeLinuxDocFile(writefile, buffer.niceFile(), true);
404 if (isVerbatim(params_)) {
406 << GetFileContents(included_file)
409 os << '&' << include_label << ';';
415 int InsetInclude::docbook(Buffer const & buffer, ostream & os,
416 bool /*mixcont*/) const
418 string incfile(params_.getContents());
420 // Do nothing if no file name has been specified
424 string const included_file = includedFilename(buffer, params_);
426 if (loadIfNeeded(buffer, params_)) {
427 Buffer * tmp = bufferlist.getBuffer(included_file);
429 // write it to a file (so far the complete file)
430 string writefile = ChangeExtension(included_file, ".sgml");
431 if (!buffer.temppath().empty() && !buffer.niceFile()) {
432 incfile = subst(incfile, '/','@');
433 writefile = AddName(buffer.temppath(), incfile);
435 writefile = included_file;
436 if (IsLyXFilename(included_file))
437 writefile = ChangeExtension(writefile, ".sgml");
439 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
440 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
442 tmp->makeDocBookFile(writefile, buffer.niceFile(), true);
445 if (isVerbatim(params_)) {
446 os << "<inlinegraphic fileref=\""
447 << '&' << include_label << ';'
448 << "\" format=\"linespecific\">";
450 os << '&' << include_label << ';';
456 void InsetInclude::validate(LaTeXFeatures & features) const
458 string incfile(params_.getContents());
461 Buffer const & buffer = features.buffer();
463 string const included_file = includedFilename(buffer, params_);
465 if (!buffer.temppath().empty() &&
466 !buffer.niceFile() &&
467 !isVerbatim(params_)) {
468 incfile = subst(incfile, '/','@');
469 writefile = AddName(buffer.temppath(), incfile);
471 writefile = included_file;
473 if (IsLyXFilename(included_file))
474 writefile = ChangeExtension(writefile, ".sgml");
476 features.includeFile(include_label, writefile);
478 if (isVerbatim(params_))
479 features.require("verbatim");
481 // Here we must do the fun stuff...
482 // Load the file in the include if it needs
484 if (loadIfNeeded(buffer, params_)) {
486 Buffer * const tmp = bufferlist.getBuffer(included_file);
488 tmp->niceFile() = buffer.niceFile();
489 tmp->validate(features);
495 void InsetInclude::getLabelList(Buffer const & buffer,
496 std::vector<string> & list) const
498 if (loadIfNeeded(buffer, params_)) {
499 string const included_file = includedFilename(buffer, params_);
500 Buffer * tmp = bufferlist.getBuffer(included_file);
501 tmp->setParentName("");
502 tmp->getLabelList(list);
503 tmp->setParentName(masterFilename(buffer));
508 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
509 std::vector<std::pair<string,string> > & keys) const
511 if (loadIfNeeded(buffer, params_)) {
512 string const included_file = includedFilename(buffer, params_);
513 Buffer * tmp = bufferlist.getBuffer(included_file);
514 tmp->setParentName("");
515 tmp->fillWithBibKeys(keys);
516 tmp->setParentName(masterFilename(buffer));
521 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
523 if (RenderPreview::activated() && preview_->previewReady()) {
524 preview_->metrics(mi, dim);
528 button_.update(getScreenLabel(*mi.base.bv->buffer()),
529 editable() != NOT_EDITABLE);
531 button_.metrics(mi, dim);
533 int center_indent = type(params_) == INPUT ?
534 0 : (mi.base.textwidth - dim.wid) / 2;
535 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
538 dim.wid = mi.base.textwidth;
543 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
545 if (!RenderPreview::activated() || !preview_->previewReady()) {
546 button_.draw(pi, x + button_.box().x1, y);
550 BOOST_ASSERT(pi.base.bv);
551 Buffer const * const buffer = pi.base.bv->buffer();
552 if (!preview_->monitoring() && buffer) {
553 string const included_file = includedFilename(*buffer, params_);
554 preview_->startMonitoring(included_file);
557 preview_->draw(pi, x + button_.box().x1, y);
565 void InsetInclude::statusChanged() const
567 LyX::cref().updateInset(this);
571 void InsetInclude::fileChanged() const
573 Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
577 Buffer const & buffer = *buffer_ptr;
578 preview_->removePreview(buffer);
579 generate_preview(*preview_.get(), *this, buffer);
585 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
587 string const included_file = includedFilename(buffer, params);
589 return type(params) == INPUT && params.preview() &&
590 IsFileReadable(included_file);
594 string const latex_string(InsetInclude const & inset, Buffer const & buffer)
597 LatexRunParams runparams;
598 runparams.flavor = LatexRunParams::LATEX;
599 inset.latex(buffer, os, runparams);
605 void generate_preview(RenderPreview & renderer,
606 InsetInclude const & inset,
607 Buffer const & buffer)
609 InsetCommandParams const & params = inset.params();
610 if (RenderPreview::activated() && preview_wanted(params, buffer)) {
611 string const snippet = latex_string(inset, buffer);
612 renderer.generatePreview(snippet, buffer);
619 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
621 if (preview_wanted(params(), ploader.buffer())) {
622 string const snippet = latex_string(*this, ploader.buffer());
623 preview_->addPreview(snippet, ploader);
628 string const InsetIncludeMailer::name_("include");
630 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
635 string const InsetIncludeMailer::inset2string(Buffer const &) const
637 return params2string(inset_.params());
641 void InsetIncludeMailer::string2params(string const & in,
642 InsetCommandParams & params)
644 params = InsetCommandParams();
649 istringstream data(in);
655 string const token = lex.getString();
660 // This is part of the inset proper that is usually swallowed
661 // by Buffer::readInset
664 string const token = lex.getString();
665 if (token != "Include")
670 InsetInclude inset(params);
672 params = inset.params();
678 InsetIncludeMailer::params2string(InsetCommandParams const & params)
680 InsetInclude inset(params);
682 data << name_ << ' ';
684 data << "\\end_inset\n";