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/Alert.h"
32 #include "frontends/LyXView.h"
33 #include "frontends/Painter.h"
35 #include "graphics/PreviewLoader.h"
37 #include "insets/render_preview.h"
39 #include "support/FileInfo.h"
40 #include "support/filename.h"
41 #include "support/filetools.h"
42 #include "support/lstrings.h" // contains
43 #include "support/tostr.h"
45 #include <boost/bind.hpp>
47 #include "support/std_ostream.h"
48 #include "support/std_sstream.h"
50 using lyx::support::AddName;
51 using lyx::support::bformat;
52 using lyx::support::ChangeExtension;
53 using lyx::support::contains;
54 using lyx::support::FileInfo;
55 using lyx::support::FileName;
56 using lyx::support::GetFileContents;
57 using lyx::support::IsFileReadable;
58 using lyx::support::IsLyXFilename;
59 using lyx::support::MakeAbsPath;
60 using lyx::support::MakeDisplayPath;
61 using lyx::support::OnlyFilename;
62 using lyx::support::OnlyPath;
63 using lyx::support::subst;
68 using std::istringstream;
70 using std::ostringstream;
73 extern BufferList bufferlist;
78 string const uniqueID()
80 static unsigned int seed = 1000;
81 return "file" + tostr(++seed);
87 InsetInclude::InsetInclude(InsetCommandParams const & p)
88 : params_(p), include_label(uniqueID()),
89 preview_(new RenderMonitoredPreview),
92 preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
93 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
97 InsetInclude::InsetInclude(InsetInclude const & other)
99 params_(other.params_),
100 include_label(other.include_label),
101 preview_(new RenderMonitoredPreview),
102 set_label_(other.set_label_)
104 preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
105 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
109 InsetInclude::~InsetInclude()
111 InsetIncludeMailer(*this).hideDialog();
115 void InsetInclude::priv_dispatch(LCursor & cur, FuncRequest & cmd)
117 switch (cmd.action) {
119 case LFUN_INSET_MODIFY: {
120 InsetCommandParams p;
121 InsetIncludeMailer::string2params(cmd.argument, p);
122 if (!p.getCmdName().empty()) {
123 set(p, *cur.bv().buffer());
129 case LFUN_INSET_DIALOG_UPDATE:
130 InsetIncludeMailer(*this).updateDialog(&cur.bv());
133 case LFUN_MOUSE_RELEASE:
134 if (button_.box().contains(cmd.x, cmd.y))
135 InsetIncludeMailer(*this).showDialog(&cur.bv());
138 case LFUN_INSET_DIALOG_SHOW:
139 InsetIncludeMailer(*this).showDialog(&cur.bv());
143 InsetOld::dispatch(cur, cmd);
149 InsetCommandParams const & InsetInclude::params() const
157 /// the type of inclusion
166 Types type(InsetCommandParams const & params)
168 string const command_name = params.getCmdName();
170 if (command_name == "input")
172 if (command_name == "verbatiminput")
174 if (command_name == "verbatiminput*")
180 bool isVerbatim(InsetCommandParams const & params)
182 string const command_name = params.getCmdName();
183 return command_name == "verbatiminput" ||
184 command_name == "verbatiminput*";
188 string const masterFilename(Buffer const & buffer)
190 return buffer.fileName();
194 string const includedFilename(Buffer const & buffer,
195 InsetCommandParams const & params)
197 return MakeAbsPath(params.getContents(),
198 OnlyPath(masterFilename(buffer)));
202 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
207 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
212 if (preview_->monitoring())
213 preview_->stopMonitoring();
215 if (type(params_) == INPUT)
216 add_preview(*preview_, *this, buffer);
220 auto_ptr<InsetBase> InsetInclude::clone() const
222 return auto_ptr<InsetBase>(new InsetInclude(*this));
226 void InsetInclude::write(Buffer const &, ostream & os) const
232 void InsetInclude::write(ostream & os) const
234 os << "Include " << params_.getCommand() << '\n'
235 << "preview " << tostr(params_.preview()) << '\n';
239 void InsetInclude::read(Buffer const &, LyXLex & lex)
245 void InsetInclude::read(LyXLex & lex)
251 string const InsetInclude::getScreenLabel(Buffer const &) const
255 switch (type(params_)) {
256 case INPUT: temp += _("Input"); break;
257 case VERB: temp += _("Verbatim Input"); break;
258 case VERBAST: temp += _("Verbatim Input*"); break;
259 case INCLUDE: temp += _("Include"); break;
264 if (params_.getContents().empty())
267 temp += OnlyFilename(params_.getContents());
275 /// return true if the file is or got loaded.
276 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
278 if (isVerbatim(params))
281 string const included_file = includedFilename(buffer, params);
282 if (!IsLyXFilename(included_file))
285 if (bufferlist.exists(included_file))
288 // the readonly flag can/will be wrong, not anymore I think.
289 FileInfo finfo(included_file);
292 return loadLyXFile(bufferlist.newBuffer(included_file),
300 int InsetInclude::latex(Buffer const & buffer, ostream & os,
301 OutputParams const & runparams) const
303 string incfile(params_.getContents());
305 // Do nothing if no file name has been specified
309 string const included_file = includedFilename(buffer, params_);
311 if (loadIfNeeded(buffer, params_)) {
312 Buffer * tmp = bufferlist.getBuffer(included_file);
314 if (tmp->params().textclass != buffer.params().textclass) {
315 string text = bformat(_("Included file `%1$s'\n"
316 "has textclass `%2$s'\n"
317 "while parent file has textclass `%3$s'."),
318 MakeDisplayPath(included_file),
319 tmp->params().getLyXTextClass().name(),
320 buffer.params().getLyXTextClass().name());
321 Alert::warning(_("Different textclasses"), text);
325 // write it to a file (so far the complete file)
326 string writefile = ChangeExtension(included_file, ".tex");
328 if (!runparams.nice) {
329 incfile = FileName(writefile).mangledFilename();
330 writefile = MakeAbsPath(incfile, buffer.temppath());
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)
391 if (IsLyXFilename(included_file))
392 writefile = ChangeExtension(included_file, ".sgml");
394 writefile = included_file;
396 if (!runparams.nice) {
397 incfile = FileName(writefile).mangledFilename();
398 writefile = MakeAbsPath(incfile, buffer.temppath());
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)
435 if (IsLyXFilename(included_file))
436 writefile = ChangeExtension(included_file, ".sgml");
438 writefile = included_file;
440 if (!runparams.nice) {
441 incfile = FileName(writefile).mangledFilename();
442 writefile = MakeAbsPath(incfile, buffer.temppath());
445 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
446 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
448 OutputParams runp = runparams;
449 tmp->makeDocBookFile(writefile, runp, true);
452 if (isVerbatim(params_)) {
453 os << "<inlinegraphic fileref=\""
454 << '&' << include_label << ';'
455 << "\" format=\"linespecific\">";
457 os << '&' << include_label << ';';
463 void InsetInclude::validate(LaTeXFeatures & features) const
465 string incfile(params_.getContents());
468 Buffer const & buffer = features.buffer();
470 string const included_file = includedFilename(buffer, params_);
472 if (IsLyXFilename(included_file))
473 writefile = ChangeExtension(included_file, ".sgml");
475 writefile = included_file;
477 if (!features.nice() && !isVerbatim(params_)) {
478 incfile = FileName(writefile).mangledFilename();
479 writefile = MakeAbsPath(incfile, buffer.temppath());
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->validate(features);
499 void InsetInclude::getLabelList(Buffer const & buffer,
500 std::vector<string> & list) const
502 if (loadIfNeeded(buffer, params_)) {
503 string const included_file = includedFilename(buffer, params_);
504 Buffer * tmp = bufferlist.getBuffer(included_file);
505 tmp->setParentName("");
506 tmp->getLabelList(list);
507 tmp->setParentName(masterFilename(buffer));
512 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
513 std::vector<std::pair<string,string> > & keys) const
515 if (loadIfNeeded(buffer, params_)) {
516 string const included_file = includedFilename(buffer, params_);
517 Buffer * tmp = bufferlist.getBuffer(included_file);
518 tmp->setParentName("");
519 tmp->fillWithBibKeys(keys);
520 tmp->setParentName(masterFilename(buffer));
525 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
527 if (RenderPreview::activated() && preview_->previewReady()) {
528 preview_->metrics(mi, dim);
532 button_.update(getScreenLabel(*mi.base.bv->buffer()),
533 editable() != NOT_EDITABLE);
535 button_.metrics(mi, dim);
537 int center_indent = type(params_) == INPUT ?
538 0 : (mi.base.textwidth - dim.wid) / 2;
539 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
542 dim.wid = mi.base.textwidth;
547 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
549 setPosCache(pi, x, y);
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();
650 istringstream data(in);
656 if (!lex || name != name_)
657 return print_mailer_error("InsetIncludeMailer", in, 1, name_);
659 // This is part of the inset proper that is usually swallowed
660 // by LyXText::readInset
663 if (!lex || id != "Include")
664 return print_mailer_error("InsetBoxMailer", in, 2, "Include");
666 InsetInclude inset(params);
668 params = inset.params();
673 InsetIncludeMailer::params2string(InsetCommandParams const & params)
675 InsetInclude inset(params);
677 data << name_ << ' ';
679 data << "\\end_inset\n";