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"
25 #include "latexrunparams.h"
28 #include "metricsinfo.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 mailer(*this);
112 InsetInclude::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
114 switch (cmd.action) {
116 case LFUN_INSET_MODIFY: {
117 InsetCommandParams p;
118 InsetIncludeMailer::string2params(cmd.argument, p);
119 if (!p.getCmdName().empty()) {
120 set(p, *cmd.view()->buffer());
121 cmd.view()->updateInset(this);
123 return DispatchResult(DISPATCHED);
126 case LFUN_INSET_DIALOG_UPDATE:
127 InsetIncludeMailer(*this).updateDialog(cmd.view());
128 return DispatchResult(DISPATCHED);
130 case LFUN_MOUSE_RELEASE:
131 if (button_.box().contains(cmd.x, cmd.y))
132 InsetIncludeMailer(*this).showDialog(cmd.view());
133 return DispatchResult(DISPATCHED);
135 case LFUN_INSET_DIALOG_SHOW:
136 InsetIncludeMailer(*this).showDialog(cmd.view());
137 return DispatchResult(DISPATCHED);
140 return DispatchResult(UNDISPATCHED);
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 LatexRunParams 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::ascii(Buffer const & buffer, ostream & os, int) const
369 if (isVerbatim(params_))
370 os << GetFileContents(includedFilename(buffer, params_));
375 int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os) const
377 string incfile(params_.getContents());
379 // Do nothing if no file name has been specified
383 string const included_file = includedFilename(buffer, params_);
385 if (loadIfNeeded(buffer, params_)) {
386 Buffer * tmp = bufferlist.getBuffer(included_file);
388 // write it to a file (so far the complete file)
389 string writefile = ChangeExtension(included_file, ".sgml");
390 if (!buffer.temppath().empty() && !buffer.niceFile()) {
391 incfile = subst(incfile, '/','@');
392 writefile = AddName(buffer.temppath(), incfile);
394 writefile = included_file;
396 if (IsLyXFilename(included_file))
397 writefile = ChangeExtension(writefile, ".sgml");
399 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
400 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
402 tmp->makeLinuxDocFile(writefile, buffer.niceFile(), true);
405 if (isVerbatim(params_)) {
407 << GetFileContents(included_file)
410 os << '&' << include_label << ';';
416 int InsetInclude::docbook(Buffer const & buffer, ostream & os,
417 bool /*mixcont*/) const
419 string incfile(params_.getContents());
421 // Do nothing if no file name has been specified
425 string const included_file = includedFilename(buffer, params_);
427 if (loadIfNeeded(buffer, params_)) {
428 Buffer * tmp = bufferlist.getBuffer(included_file);
430 // write it to a file (so far the complete file)
431 string writefile = ChangeExtension(included_file, ".sgml");
432 if (!buffer.temppath().empty() && !buffer.niceFile()) {
433 incfile = subst(incfile, '/','@');
434 writefile = AddName(buffer.temppath(), incfile);
436 writefile = included_file;
437 if (IsLyXFilename(included_file))
438 writefile = ChangeExtension(writefile, ".sgml");
440 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
441 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
443 tmp->makeDocBookFile(writefile, buffer.niceFile(), true);
446 if (isVerbatim(params_)) {
447 os << "<inlinegraphic fileref=\""
448 << '&' << include_label << ';'
449 << "\" format=\"linespecific\">";
451 os << '&' << include_label << ';';
457 void InsetInclude::validate(LaTeXFeatures & features) const
459 string incfile(params_.getContents());
462 Buffer const & buffer = features.buffer();
464 string const included_file = includedFilename(buffer, params_);
466 if (!buffer.temppath().empty() &&
467 !buffer.niceFile() &&
468 !isVerbatim(params_)) {
469 incfile = subst(incfile, '/','@');
470 writefile = AddName(buffer.temppath(), incfile);
472 writefile = included_file;
474 if (IsLyXFilename(included_file))
475 writefile = ChangeExtension(writefile, ".sgml");
477 features.includeFile(include_label, writefile);
479 if (isVerbatim(params_))
480 features.require("verbatim");
482 // Here we must do the fun stuff...
483 // Load the file in the include if it needs
485 if (loadIfNeeded(buffer, params_)) {
487 Buffer * const tmp = bufferlist.getBuffer(included_file);
489 tmp->niceFile() = buffer.niceFile();
490 tmp->validate(features);
496 void InsetInclude::getLabelList(Buffer const & buffer,
497 std::vector<string> & list) const
499 if (loadIfNeeded(buffer, params_)) {
500 string const included_file = includedFilename(buffer, params_);
501 Buffer * tmp = bufferlist.getBuffer(included_file);
502 tmp->setParentName("");
503 tmp->getLabelList(list);
504 tmp->setParentName(masterFilename(buffer));
509 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
510 std::vector<std::pair<string,string> > & keys) const
512 if (loadIfNeeded(buffer, params_)) {
513 string const included_file = includedFilename(buffer, params_);
514 Buffer * tmp = bufferlist.getBuffer(included_file);
515 tmp->setParentName("");
516 tmp->fillWithBibKeys(keys);
517 tmp->setParentName(masterFilename(buffer));
522 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
524 if (RenderPreview::activated() && preview_->previewReady()) {
525 preview_->metrics(mi, dim);
529 button_.update(getScreenLabel(*mi.base.bv->buffer()),
530 editable() != NOT_EDITABLE);
532 button_.metrics(mi, dim);
534 int center_indent = type(params_) == INPUT ?
535 0 : (mi.base.textwidth - dim.wid) / 2;
536 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
539 dim.wid = mi.base.textwidth;
544 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
546 if (!RenderPreview::activated() || !preview_->previewReady()) {
547 button_.draw(pi, x + button_.box().x1, y);
551 preview_->draw(pi, x + button_.box().x1, y);
559 void InsetInclude::statusChanged() const
561 LyX::cref().updateInset(this);
565 void InsetInclude::fileChanged() const
567 Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
571 Buffer const & buffer = *buffer_ptr;
572 preview_->removePreview(buffer);
573 add_preview(*preview_.get(), *this, buffer);
574 preview_->startLoading(buffer);
580 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
582 string const included_file = includedFilename(buffer, params);
584 return type(params) == INPUT && params.preview() &&
585 IsFileReadable(included_file);
589 string const latex_string(InsetInclude const & inset, Buffer const & buffer)
592 LatexRunParams runparams;
593 runparams.flavor = LatexRunParams::LATEX;
594 inset.latex(buffer, os, runparams);
600 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
601 Buffer const & buffer)
603 InsetCommandParams const & params = inset.params();
604 if (RenderPreview::activated() && preview_wanted(params, buffer)) {
605 renderer.setAbsFile(includedFilename(buffer, params));
606 string const snippet = latex_string(inset, buffer);
607 renderer.addPreview(snippet, buffer);
614 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
616 Buffer const & buffer = ploader.buffer();
617 if (preview_wanted(params(), buffer)) {
618 preview_->setAbsFile(includedFilename(buffer, params()));
619 string const snippet = latex_string(*this, buffer);
620 preview_->addPreview(snippet, ploader);
625 string const InsetIncludeMailer::name_("include");
627 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
632 string const InsetIncludeMailer::inset2string(Buffer const &) const
634 return params2string(inset_.params());
638 void InsetIncludeMailer::string2params(string const & in,
639 InsetCommandParams & params)
641 params = InsetCommandParams();
646 istringstream data(in);
652 string const token = lex.getString();
657 // This is part of the inset proper that is usually swallowed
658 // by Buffer::readInset
661 string const token = lex.getString();
662 if (token != "Include")
667 InsetInclude inset(params);
669 params = inset.params();
675 InsetIncludeMailer::params2string(InsetCommandParams const & params)
677 InsetInclude inset(params);
679 data << name_ << ' ';
681 data << "\\end_inset\n";