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"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
25 #include "LaTeXFeatures.h"
28 #include "metricsinfo.h"
29 #include "outputparams.h"
31 #include "frontends/LyXView.h"
32 #include "frontends/Painter.h"
34 #include "graphics/PreviewLoader.h"
36 #include "insets/render_preview.h"
38 #include "support/FileInfo.h"
39 #include "support/filetools.h"
40 #include "support/lstrings.h" // contains
41 #include "support/tostr.h"
43 #include <boost/bind.hpp>
45 #include "support/std_ostream.h"
46 #include "support/std_sstream.h"
48 using lyx::support::AddName;
49 using lyx::support::ChangeExtension;
50 using lyx::support::contains;
51 using lyx::support::FileInfo;
52 using lyx::support::GetFileContents;
53 using lyx::support::IsFileReadable;
54 using lyx::support::IsLyXFilename;
55 using lyx::support::MakeAbsPath;
56 using lyx::support::MakeDisplayPath;
57 using lyx::support::OnlyFilename;
58 using lyx::support::OnlyPath;
59 using lyx::support::subst;
64 using std::istringstream;
66 using std::ostringstream;
69 extern BufferList bufferlist;
74 string const uniqueID()
76 static unsigned int seed = 1000;
77 return "file" + tostr(++seed);
83 InsetInclude::InsetInclude(InsetCommandParams const & p)
84 : params_(p), include_label(uniqueID()),
85 preview_(new RenderMonitoredPreview),
88 preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
89 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
93 InsetInclude::InsetInclude(InsetInclude const & other)
95 params_(other.params_),
96 include_label(other.include_label),
97 preview_(new RenderMonitoredPreview),
98 set_label_(other.set_label_)
100 preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
101 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
105 InsetInclude::~InsetInclude()
107 InsetIncludeMailer(*this).hideDialog();
111 void InsetInclude::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
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, *cur.bv().buffer());
125 case LFUN_INSET_DIALOG_UPDATE:
126 InsetIncludeMailer(*this).updateDialog(&cur.bv());
129 case LFUN_MOUSE_RELEASE:
130 if (button_.box().contains(cmd.x, cmd.y))
131 InsetIncludeMailer(*this).showDialog(&cur.bv());
134 case LFUN_INSET_DIALOG_SHOW:
135 InsetIncludeMailer(*this).showDialog(&cur.bv());
139 InsetOld::dispatch(cur, cmd);
145 InsetCommandParams const & InsetInclude::params() const
153 /// the type of inclusion
162 Types type(InsetCommandParams const & params)
164 string const command_name = params.getCmdName();
166 if (command_name == "input")
168 if (command_name == "verbatiminput")
170 if (command_name == "verbatiminput*")
176 bool isVerbatim(InsetCommandParams const & params)
178 string const command_name = params.getCmdName();
179 return command_name == "verbatiminput" ||
180 command_name == "verbatiminput*";
184 string const masterFilename(Buffer const & buffer)
186 return buffer.fileName();
190 string const includedFilename(Buffer const & buffer,
191 InsetCommandParams const & params)
193 return MakeAbsPath(params.getContents(),
194 OnlyPath(masterFilename(buffer)));
198 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
203 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
208 if (preview_->monitoring())
209 preview_->stopMonitoring();
211 if (type(params_) == INPUT)
212 add_preview(*preview_, *this, buffer);
216 auto_ptr<InsetBase> InsetInclude::clone() const
218 return auto_ptr<InsetBase>(new InsetInclude(*this));
222 void InsetInclude::write(Buffer const &, ostream & os) const
228 void InsetInclude::write(ostream & os) const
230 os << "Include " << params_.getCommand() << '\n'
231 << "preview " << tostr(params_.preview()) << '\n';
235 void InsetInclude::read(Buffer const &, LyXLex & lex)
241 void InsetInclude::read(LyXLex & lex)
247 string const InsetInclude::getScreenLabel(Buffer const &) const
251 switch (type(params_)) {
252 case INPUT: temp += _("Input"); break;
253 case VERB: temp += _("Verbatim Input"); break;
254 case VERBAST: temp += _("Verbatim Input*"); break;
255 case INCLUDE: temp += _("Include"); break;
260 if (params_.getContents().empty())
263 temp += OnlyFilename(params_.getContents());
271 /// return true if the file is or got loaded.
272 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
274 if (isVerbatim(params))
277 string const included_file = includedFilename(buffer, params);
278 if (!IsLyXFilename(included_file))
281 if (bufferlist.exists(included_file))
284 // the readonly flag can/will be wrong, not anymore I think.
285 FileInfo finfo(included_file);
288 return loadLyXFile(bufferlist.newBuffer(included_file),
296 int InsetInclude::latex(Buffer const & buffer, ostream & os,
297 OutputParams const & runparams) const
299 string incfile(params_.getContents());
301 // Do nothing if no file name has been specified
305 string const included_file = includedFilename(buffer, params_);
307 if (loadIfNeeded(buffer, params_)) {
308 Buffer * tmp = bufferlist.getBuffer(included_file);
310 // FIXME: this should be a GUI warning
311 if (tmp->params().textclass != buffer.params().textclass) {
312 lyxerr << "WARNING: Included file `"
313 << MakeDisplayPath(included_file)
314 << "' has textclass `"
315 << tmp->params().getLyXTextClass().name()
316 << "' while parent file has textclass `"
317 << buffer.params().getLyXTextClass().name()
322 // write it to a file (so far the complete file)
323 string writefile = ChangeExtension(included_file, ".tex");
325 if (!buffer.temppath().empty() && !runparams.nice) {
326 incfile = subst(incfile, '/','@');
328 incfile = subst(incfile, ':', '$');
330 writefile = AddName(buffer.temppath(), incfile);
332 writefile = included_file;
333 writefile = ChangeExtension(writefile, ".tex");
334 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
335 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
337 tmp->markDepClean(buffer.temppath());
339 tmp->makeLaTeXFile(writefile,
340 OnlyPath(masterFilename(buffer)),
344 if (isVerbatim(params_)) {
345 os << '\\' << params_.getCmdName() << '{' << incfile << '}';
346 } else if (type(params_) == INPUT) {
347 // \input wants file with extension (default is .tex)
348 if (!IsLyXFilename(included_file)) {
349 os << '\\' << params_.getCmdName() << '{' << incfile << '}';
351 os << '\\' << params_.getCmdName() << '{'
352 << ChangeExtension(incfile, ".tex")
356 // \include don't want extension and demands that the
357 // file really have .tex
358 os << '\\' << params_.getCmdName() << '{'
359 << ChangeExtension(incfile, string())
367 int InsetInclude::plaintext(Buffer const & buffer, ostream & os,
368 OutputParams const &) const
370 if (isVerbatim(params_))
371 os << GetFileContents(includedFilename(buffer, params_));
376 int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os,
377 OutputParams const & runparams) const
379 string incfile(params_.getContents());
381 // Do nothing if no file name has been specified
385 string const included_file = includedFilename(buffer, params_);
387 if (loadIfNeeded(buffer, params_)) {
388 Buffer * tmp = bufferlist.getBuffer(included_file);
390 // write it to a file (so far the complete file)
391 string writefile = ChangeExtension(included_file, ".sgml");
392 if (!buffer.temppath().empty() && !runparams.nice) {
393 incfile = subst(incfile, '/','@');
394 writefile = AddName(buffer.temppath(), incfile);
396 writefile = included_file;
398 if (IsLyXFilename(included_file))
399 writefile = ChangeExtension(writefile, ".sgml");
401 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
402 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
404 OutputParams runp = runparams;
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() && !runparams.nice) {
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 tmp->makeDocBookFile(writefile, runp, true);
450 if (isVerbatim(params_)) {
451 os << "<inlinegraphic fileref=\""
452 << '&' << include_label << ';'
453 << "\" format=\"linespecific\">";
455 os << '&' << include_label << ';';
461 void InsetInclude::validate(LaTeXFeatures & features) const
463 string incfile(params_.getContents());
466 Buffer const & buffer = features.buffer();
468 string const included_file = includedFilename(buffer, params_);
470 if (IsLyXFilename(included_file))
471 writefile = ChangeExtension(writefile, ".sgml");
472 else if (!buffer.temppath().empty() &&
474 !isVerbatim(params_)) {
475 incfile = subst(incfile, '/','@');
477 // FIXME: It seems that the following is necessary (see latex() above)
478 // incfile = subst(incfile, ':', '$');
480 writefile = AddName(buffer.temppath(), incfile);
482 writefile = included_file;
484 features.includeFile(include_label, writefile);
486 if (isVerbatim(params_))
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(buffer, params_)) {
494 Buffer * const tmp = bufferlist.getBuffer(included_file);
496 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 setPosCache(pi, x, y);
553 if (!RenderPreview::activated() || !preview_->previewReady()) {
554 button_.draw(pi, x + button_.box().x1, y);
558 preview_->draw(pi, x + button_.box().x1, y);
566 void InsetInclude::statusChanged() const
568 LyX::cref().updateInset(this);
572 void InsetInclude::fileChanged() const
574 Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
578 Buffer const & buffer = *buffer_ptr;
579 preview_->removePreview(buffer);
580 add_preview(*preview_.get(), *this, buffer);
581 preview_->startLoading(buffer);
587 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
589 string const included_file = includedFilename(buffer, params);
591 return type(params) == INPUT && params.preview() &&
592 IsFileReadable(included_file);
596 string const latex_string(InsetInclude const & inset, Buffer const & buffer)
599 OutputParams runparams;
600 runparams.flavor = OutputParams::LATEX;
601 inset.latex(buffer, os, runparams);
607 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
608 Buffer const & buffer)
610 InsetCommandParams const & params = inset.params();
611 if (RenderPreview::activated() && preview_wanted(params, buffer)) {
612 renderer.setAbsFile(includedFilename(buffer, params));
613 string const snippet = latex_string(inset, buffer);
614 renderer.addPreview(snippet, buffer);
621 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
623 Buffer const & buffer = ploader.buffer();
624 if (preview_wanted(params(), buffer)) {
625 preview_->setAbsFile(includedFilename(buffer, params()));
626 string const snippet = latex_string(*this, buffer);
627 preview_->addPreview(snippet, ploader);
632 string const InsetIncludeMailer::name_("include");
634 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
639 string const InsetIncludeMailer::inset2string(Buffer const &) const
641 return params2string(inset_.params());
645 void InsetIncludeMailer::string2params(string const & in,
646 InsetCommandParams & params)
648 params = InsetCommandParams();
652 istringstream data(in);
658 if (!lex || name != name_)
659 return print_mailer_error("InsetIncludeMailer", in, 1, name_);
661 // This is part of the inset proper that is usually swallowed
662 // by LyXText::readInset
665 if (!lex || id != "Include")
666 return print_mailer_error("InsetBoxMailer", in, 2, "Include");
668 InsetInclude inset(params);
670 params = inset.params();
675 InsetIncludeMailer::params2string(InsetCommandParams const & params)
677 InsetInclude inset(params);
679 data << name_ << ' ';
681 data << "\\end_inset\n";