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 "dispatchresult.h"
22 #include "funcrequest.h"
24 #include "LaTeXFeatures.h"
27 #include "metricsinfo.h"
28 #include "outputparams.h"
30 #include "frontends/LyXView.h"
31 #include "frontends/Painter.h"
33 #include "graphics/PreviewLoader.h"
35 #include "insets/render_preview.h"
37 #include "support/FileInfo.h"
38 #include "support/filetools.h"
39 #include "support/lstrings.h" // contains
40 #include "support/tostr.h"
42 #include <boost/bind.hpp>
44 #include "support/std_ostream.h"
45 #include "support/std_sstream.h"
47 using lyx::support::AddName;
48 using lyx::support::ChangeExtension;
49 using lyx::support::contains;
50 using lyx::support::FileInfo;
51 using lyx::support::GetFileContents;
52 using lyx::support::IsFileReadable;
53 using lyx::support::IsLyXFilename;
54 using lyx::support::MakeAbsPath;
55 using lyx::support::MakeDisplayPath;
56 using lyx::support::OnlyFilename;
57 using lyx::support::OnlyPath;
58 using lyx::support::subst;
63 using std::istringstream;
65 using std::ostringstream;
68 extern BufferList bufferlist;
73 string const uniqueID()
75 static unsigned int seed = 1000;
76 return "file" + tostr(++seed);
82 InsetInclude::InsetInclude(InsetCommandParams const & p)
83 : params_(p), include_label(uniqueID()),
84 preview_(new RenderMonitoredPreview),
87 preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
88 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
92 InsetInclude::InsetInclude(InsetInclude const & other)
94 params_(other.params_),
95 include_label(other.include_label),
96 preview_(new RenderMonitoredPreview),
97 set_label_(other.set_label_)
99 preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
100 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
104 InsetInclude::~InsetInclude()
106 InsetIncludeMailer(*this).hideDialog();
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()->update();
122 return DispatchResult(true, true);
125 case LFUN_INSET_DIALOG_UPDATE:
126 InsetIncludeMailer(*this).updateDialog(cmd.view());
127 return DispatchResult(true, true);
129 case LFUN_MOUSE_RELEASE:
130 if (button_.box().contains(cmd.x, cmd.y))
131 InsetIncludeMailer(*this).showDialog(cmd.view());
132 return DispatchResult(true, true);
134 case LFUN_INSET_DIALOG_SHOW:
135 InsetIncludeMailer(*this).showDialog(cmd.view());
136 return DispatchResult(true, true);
139 return DispatchResult(false);
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 add_preview(RenderMonitoredPreview &, 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 add_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 OutputParams 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::plaintext(Buffer const & buffer, ostream & os,
367 OutputParams const &) const
369 if (isVerbatim(params_))
370 os << GetFileContents(includedFilename(buffer, params_));
375 int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os,
376 OutputParams const & runparams) const
378 string incfile(params_.getContents());
380 // Do nothing if no file name has been specified
384 string const included_file = includedFilename(buffer, params_);
386 if (loadIfNeeded(buffer, params_)) {
387 Buffer * tmp = bufferlist.getBuffer(included_file);
389 // write it to a file (so far the complete file)
390 string writefile = ChangeExtension(included_file, ".sgml");
391 if (!buffer.temppath().empty() && !buffer.niceFile()) {
392 incfile = subst(incfile, '/','@');
393 writefile = AddName(buffer.temppath(), incfile);
395 writefile = included_file;
397 if (IsLyXFilename(included_file))
398 writefile = ChangeExtension(writefile, ".sgml");
400 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
401 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
403 OutputParams runp = runparams;
404 runp.nice = buffer.niceFile();
405 tmp->makeLinuxDocFile(writefile, runp, true);
408 if (isVerbatim(params_)) {
410 << GetFileContents(included_file)
413 os << '&' << include_label << ';';
419 int InsetInclude::docbook(Buffer const & buffer, ostream & os,
420 OutputParams const & runparams) const
422 string incfile(params_.getContents());
424 // Do nothing if no file name has been specified
428 string const included_file = includedFilename(buffer, params_);
430 if (loadIfNeeded(buffer, params_)) {
431 Buffer * tmp = bufferlist.getBuffer(included_file);
433 // write it to a file (so far the complete file)
434 string writefile = ChangeExtension(included_file, ".sgml");
435 if (!buffer.temppath().empty() && !buffer.niceFile()) {
436 incfile = subst(incfile, '/','@');
437 writefile = AddName(buffer.temppath(), incfile);
439 writefile = included_file;
440 if (IsLyXFilename(included_file))
441 writefile = ChangeExtension(writefile, ".sgml");
443 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
444 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
446 OutputParams runp = runparams;
447 runp.nice = buffer.niceFile();
448 tmp->makeDocBookFile(writefile, runp, true);
451 if (isVerbatim(params_)) {
452 os << "<inlinegraphic fileref=\""
453 << '&' << include_label << ';'
454 << "\" format=\"linespecific\">";
456 os << '&' << include_label << ';';
462 void InsetInclude::validate(LaTeXFeatures & features) const
464 string incfile(params_.getContents());
467 Buffer const & buffer = features.buffer();
469 string const included_file = includedFilename(buffer, params_);
471 if (!buffer.temppath().empty() &&
472 !buffer.niceFile() &&
473 !isVerbatim(params_)) {
474 incfile = subst(incfile, '/','@');
475 writefile = AddName(buffer.temppath(), incfile);
477 writefile = included_file;
479 if (IsLyXFilename(included_file))
480 writefile = ChangeExtension(writefile, ".sgml");
482 features.includeFile(include_label, writefile);
484 if (isVerbatim(params_))
485 features.require("verbatim");
487 // Here we must do the fun stuff...
488 // Load the file in the include if it needs
490 if (loadIfNeeded(buffer, params_)) {
492 Buffer * const tmp = bufferlist.getBuffer(included_file);
494 tmp->niceFile() = buffer.niceFile();
495 tmp->validate(features);
501 void InsetInclude::getLabelList(Buffer const & buffer,
502 std::vector<string> & list) const
504 if (loadIfNeeded(buffer, params_)) {
505 string const included_file = includedFilename(buffer, params_);
506 Buffer * tmp = bufferlist.getBuffer(included_file);
507 tmp->setParentName("");
508 tmp->getLabelList(list);
509 tmp->setParentName(masterFilename(buffer));
514 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
515 std::vector<std::pair<string,string> > & keys) const
517 if (loadIfNeeded(buffer, params_)) {
518 string const included_file = includedFilename(buffer, params_);
519 Buffer * tmp = bufferlist.getBuffer(included_file);
520 tmp->setParentName("");
521 tmp->fillWithBibKeys(keys);
522 tmp->setParentName(masterFilename(buffer));
527 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
529 if (RenderPreview::activated() && preview_->previewReady()) {
530 preview_->metrics(mi, dim);
534 button_.update(getScreenLabel(*mi.base.bv->buffer()),
535 editable() != NOT_EDITABLE);
537 button_.metrics(mi, dim);
539 int center_indent = type(params_) == INPUT ?
540 0 : (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
551 if (!RenderPreview::activated() || !preview_->previewReady()) {
552 button_.draw(pi, x + button_.box().x1, y);
556 preview_->draw(pi, x + button_.box().x1, y);
564 void InsetInclude::statusChanged() const
566 LyX::cref().updateInset(this);
570 void InsetInclude::fileChanged() const
572 Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
576 Buffer const & buffer = *buffer_ptr;
577 preview_->removePreview(buffer);
578 add_preview(*preview_.get(), *this, buffer);
579 preview_->startLoading(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 OutputParams runparams;
598 runparams.flavor = OutputParams::LATEX;
599 inset.latex(buffer, os, runparams);
605 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
606 Buffer const & buffer)
608 InsetCommandParams const & params = inset.params();
609 if (RenderPreview::activated() && preview_wanted(params, buffer)) {
610 renderer.setAbsFile(includedFilename(buffer, params));
611 string const snippet = latex_string(inset, buffer);
612 renderer.addPreview(snippet, buffer);
619 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
621 Buffer const & buffer = ploader.buffer();
622 if (preview_wanted(params(), buffer)) {
623 preview_->setAbsFile(includedFilename(buffer, params()));
624 string const snippet = latex_string(*this, buffer);
625 preview_->addPreview(snippet, ploader);
630 string const InsetIncludeMailer::name_("include");
632 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
637 string const InsetIncludeMailer::inset2string(Buffer const &) const
639 return params2string(inset_.params());
643 void InsetIncludeMailer::string2params(string const & in,
644 InsetCommandParams & params)
646 params = InsetCommandParams();
651 istringstream data(in);
657 string const token = lex.getString();
662 // This is part of the inset proper that is usually swallowed
663 // by Buffer::readInset
666 string const token = lex.getString();
667 if (token != "Include")
672 InsetInclude inset(params);
674 params = inset.params();
680 InsetIncludeMailer::params2string(InsetCommandParams const & params)
682 InsetInclude inset(params);
684 data << name_ << ' ';
686 data << "\\end_inset\n";