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"
26 #include "metricsinfo.h"
28 #include "frontends/Painter.h"
30 #include "graphics/PreviewImage.h"
31 #include "graphics/PreviewLoader.h"
33 #include "insets/render_preview.h"
35 #include "support/FileInfo.h"
36 #include "support/filetools.h"
37 #include "support/lstrings.h" // contains
38 #include "support/tostr.h"
40 #include <boost/bind.hpp>
42 #include "support/std_ostream.h"
43 #include "support/std_sstream.h"
45 using lyx::support::AddName;
46 using lyx::support::ChangeExtension;
47 using lyx::support::contains;
48 using lyx::support::FileInfo;
49 using lyx::support::GetFileContents;
50 using lyx::support::IsFileReadable;
51 using lyx::support::IsLyXFilename;
52 using lyx::support::MakeAbsPath;
53 using lyx::support::MakeDisplayPath;
54 using lyx::support::OnlyFilename;
55 using lyx::support::OnlyPath;
56 using lyx::support::subst;
61 using std::istringstream;
63 using std::ostringstream;
66 extern BufferList bufferlist;
71 string const uniqueID()
73 static unsigned int seed = 1000;
74 return "file" + tostr(++seed);
80 InsetInclude::InsetInclude(InsetCommandParams const & p)
81 : params_(p), include_label(uniqueID()),
82 preview_(new RenderMonitoredPreview),
85 preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
86 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
90 InsetInclude::InsetInclude(InsetInclude const & other)
92 params_(other.params_),
93 include_label(other.include_label),
94 preview_(new RenderMonitoredPreview),
95 set_label_(other.set_label_)
97 preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
98 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
102 InsetInclude::~InsetInclude()
104 InsetIncludeMailer mailer(*this);
109 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
111 switch (cmd.action) {
113 case LFUN_INSET_MODIFY: {
114 InsetCommandParams p;
115 InsetIncludeMailer::string2params(cmd.argument, p);
116 if (!p.getCmdName().empty()) {
117 set(p, *cmd.view()->buffer());
118 cmd.view()->updateInset(this);
123 case LFUN_INSET_DIALOG_UPDATE:
124 InsetIncludeMailer(*this).updateDialog(cmd.view());
127 case LFUN_MOUSE_RELEASE:
128 if (button_.box().contains(cmd.x, cmd.y))
129 InsetIncludeMailer(*this).showDialog(cmd.view());
132 case LFUN_INSET_DIALOG_SHOW:
133 InsetIncludeMailer(*this).showDialog(cmd.view());
137 return InsetOld::localDispatch(cmd);
142 InsetCommandParams const & InsetInclude::params() const
150 /// the type of inclusion
159 Types type(InsetCommandParams const & params)
161 string const command_name = params.getCmdName();
163 if (command_name == "input")
165 if (command_name == "verbatiminput")
167 if (command_name == "verbatiminput*")
173 bool isVerbatim(InsetCommandParams const & params)
175 string const command_name = params.getCmdName();
176 return command_name == "verbatiminput" ||
177 command_name == "verbatiminput*";
181 string const masterFilename(Buffer const & buffer)
183 return buffer.fileName();
187 string const includedFilename(Buffer const & buffer,
188 InsetCommandParams const & params)
190 return MakeAbsPath(params.getContents(),
191 OnlyPath(masterFilename(buffer)));
195 void generate_preview(RenderPreview &, InsetInclude const &, Buffer const &);
200 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
205 if (preview_->monitoring())
206 preview_->stopMonitoring();
208 if (type(params_) == INPUT)
209 generate_preview(*preview_, *this, buffer);
213 auto_ptr<InsetBase> InsetInclude::clone() const
215 return auto_ptr<InsetBase>(new InsetInclude(*this));
219 void InsetInclude::write(Buffer const &, ostream & os) const
225 void InsetInclude::write(ostream & os) const
227 os << "Include " << params_.getCommand() << '\n'
228 << "preview " << tostr(params_.preview()) << '\n';
232 void InsetInclude::read(Buffer const &, LyXLex & lex)
238 void InsetInclude::read(LyXLex & lex)
244 string const InsetInclude::getScreenLabel(Buffer const &) const
248 switch (type(params_)) {
249 case INPUT: temp += _("Input"); break;
250 case VERB: temp += _("Verbatim Input"); break;
251 case VERBAST: temp += _("Verbatim Input*"); break;
252 case INCLUDE: temp += _("Include"); break;
257 if (params_.getContents().empty())
260 temp += OnlyFilename(params_.getContents());
268 /// return true if the file is or got loaded.
269 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
271 if (isVerbatim(params))
274 string const included_file = includedFilename(buffer, params);
275 if (!IsLyXFilename(included_file))
278 if (bufferlist.exists(included_file))
281 // the readonly flag can/will be wrong, not anymore I think.
282 FileInfo finfo(included_file);
285 return loadLyXFile(bufferlist.newBuffer(included_file),
293 int InsetInclude::latex(Buffer const & buffer, ostream & os,
294 LatexRunParams const & runparams) const
296 string incfile(params_.getContents());
298 // Do nothing if no file name has been specified
302 string const included_file = includedFilename(buffer, params_);
304 if (loadIfNeeded(buffer, params_)) {
305 Buffer * tmp = bufferlist.getBuffer(included_file);
307 // FIXME: this should be a GUI warning
308 if (tmp->params().textclass != buffer.params().textclass) {
309 lyxerr << "WARNING: Included file `"
310 << MakeDisplayPath(included_file)
311 << "' has textclass `"
312 << tmp->params().getLyXTextClass().name()
313 << "' while parent file has textclass `"
314 << buffer.params().getLyXTextClass().name()
319 // write it to a file (so far the complete file)
320 string writefile = ChangeExtension(included_file, ".tex");
322 if (!buffer.temppath().empty() && !runparams.nice) {
323 incfile = subst(incfile, '/','@');
325 incfile = subst(incfile, ':', '$');
327 writefile = AddName(buffer.temppath(), incfile);
329 writefile = included_file;
330 writefile = ChangeExtension(writefile, ".tex");
331 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
332 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
334 tmp->markDepClean(buffer.temppath());
336 tmp->makeLaTeXFile(writefile,
337 OnlyPath(masterFilename(buffer)),
341 if (isVerbatim(params_)) {
342 os << '\\' << params_.getCmdName() << '{' << incfile << '}';
343 } else if (type(params_) == INPUT) {
344 // \input wants file with extension (default is .tex)
345 if (!IsLyXFilename(included_file)) {
346 os << '\\' << params_.getCmdName() << '{' << incfile << '}';
348 os << '\\' << params_.getCmdName() << '{'
349 << ChangeExtension(incfile, ".tex")
353 // \include don't want extension and demands that the
354 // file really have .tex
355 os << '\\' << params_.getCmdName() << '{'
356 << ChangeExtension(incfile, string())
364 int InsetInclude::ascii(Buffer const & buffer, ostream & os, int) const
366 if (isVerbatim(params_))
367 os << GetFileContents(includedFilename(buffer, params_));
372 int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os) const
374 string incfile(params_.getContents());
376 // Do nothing if no file name has been specified
380 string const included_file = includedFilename(buffer, params_);
382 if (loadIfNeeded(buffer, params_)) {
383 Buffer * tmp = bufferlist.getBuffer(included_file);
385 // write it to a file (so far the complete file)
386 string writefile = ChangeExtension(included_file, ".sgml");
387 if (!buffer.temppath().empty() && !buffer.niceFile()) {
388 incfile = subst(incfile, '/','@');
389 writefile = AddName(buffer.temppath(), incfile);
391 writefile = included_file;
393 if (IsLyXFilename(included_file))
394 writefile = ChangeExtension(writefile, ".sgml");
396 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
397 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
399 tmp->makeLinuxDocFile(writefile, buffer.niceFile(), true);
402 if (isVerbatim(params_)) {
404 << GetFileContents(included_file)
407 os << '&' << include_label << ';';
413 int InsetInclude::docbook(Buffer const & buffer, ostream & os,
414 bool /*mixcont*/) const
416 string incfile(params_.getContents());
418 // Do nothing if no file name has been specified
422 string const included_file = includedFilename(buffer, params_);
424 if (loadIfNeeded(buffer, params_)) {
425 Buffer * tmp = bufferlist.getBuffer(included_file);
427 // write it to a file (so far the complete file)
428 string writefile = ChangeExtension(included_file, ".sgml");
429 if (!buffer.temppath().empty() && !buffer.niceFile()) {
430 incfile = subst(incfile, '/','@');
431 writefile = AddName(buffer.temppath(), incfile);
433 writefile = included_file;
434 if (IsLyXFilename(included_file))
435 writefile = ChangeExtension(writefile, ".sgml");
437 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
438 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
440 tmp->makeDocBookFile(writefile, buffer.niceFile(), true);
443 if (isVerbatim(params_)) {
444 os << "<inlinegraphic fileref=\""
445 << '&' << include_label << ';'
446 << "\" format=\"linespecific\">";
448 os << '&' << include_label << ';';
454 void InsetInclude::validate(LaTeXFeatures & features) const
456 string incfile(params_.getContents());
459 Buffer const & buffer = features.buffer();
461 string const included_file = includedFilename(buffer, params_);
463 if (!buffer.temppath().empty() &&
464 !buffer.niceFile() &&
465 !isVerbatim(params_)) {
466 incfile = subst(incfile, '/','@');
467 writefile = AddName(buffer.temppath(), incfile);
469 writefile = included_file;
471 if (IsLyXFilename(included_file))
472 writefile = ChangeExtension(writefile, ".sgml");
474 features.includeFile(include_label, writefile);
476 if (isVerbatim(params_))
477 features.require("verbatim");
479 // Here we must do the fun stuff...
480 // Load the file in the include if it needs
482 if (loadIfNeeded(buffer, params_)) {
484 Buffer * const tmp = bufferlist.getBuffer(included_file);
486 tmp->niceFile() = buffer.niceFile();
487 tmp->validate(features);
493 void InsetInclude::getLabelList(Buffer const & buffer,
494 std::vector<string> & list) const
496 if (loadIfNeeded(buffer, params_)) {
497 string const included_file = includedFilename(buffer, params_);
498 Buffer * tmp = bufferlist.getBuffer(included_file);
499 tmp->setParentName("");
500 tmp->getLabelList(list);
501 tmp->setParentName(masterFilename(buffer));
506 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
507 std::vector<std::pair<string,string> > & keys) const
509 if (loadIfNeeded(buffer, params_)) {
510 string const included_file = includedFilename(buffer, params_);
511 Buffer * tmp = bufferlist.getBuffer(included_file);
512 tmp->setParentName("");
513 tmp->fillWithBibKeys(keys);
514 tmp->setParentName(masterFilename(buffer));
519 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
521 if (preview_->previewReady()) {
522 dim.asc = preview_->pimage()->ascent();
523 dim.des = preview_->pimage()->descent();
524 dim.wid = preview_->pimage()->width();
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
547 if (!preview_->previewReady()) {
548 button_.draw(pi, x + button_.box().x1, y);
552 if (!preview_->monitoring()) {
553 string const included_file =
554 includedFilename(*view()->buffer(), params_);
555 preview_->startMonitoring(included_file);
558 pi.pain.image(x + button_.box().x1, y - dim_.asc, dim_.wid, dim_.height(),
559 *(preview_->pimage()->image()));
563 BufferView * InsetInclude::view() const
565 return button_.view();
573 void InsetInclude::statusChanged() const
576 view()->updateInset(this);
580 void InsetInclude::fileChanged() const
582 BufferView * const bv = view();
585 bv->updateInset(this);
589 Buffer const & buffer = *bv->buffer();
591 preview_->removePreview(buffer);
592 generate_preview(*preview_.get(), *this, buffer);
598 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
600 string const included_file = includedFilename(buffer, params);
602 return type(params) == INPUT && params.preview() &&
603 IsFileReadable(included_file);
607 string const latex_string(InsetInclude const & inset, Buffer const & buffer)
610 LatexRunParams runparams;
611 runparams.flavor = LatexRunParams::LATEX;
612 inset.latex(buffer, os, runparams);
618 void generate_preview(RenderPreview & renderer,
619 InsetInclude const & inset,
620 Buffer const & buffer)
622 InsetCommandParams const & params = inset.params();
623 if (RenderPreview::activated() && preview_wanted(params, buffer)) {
624 string const snippet = latex_string(inset, buffer);
625 renderer.generatePreview(snippet, buffer);
632 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
634 if (preview_wanted(params(), ploader.buffer())) {
635 string const snippet = latex_string(*this, ploader.buffer());
636 preview_->addPreview(snippet, ploader);
641 string const InsetIncludeMailer::name_("include");
643 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
648 string const InsetIncludeMailer::inset2string(Buffer const &) const
650 return params2string(inset_.params());
654 void InsetIncludeMailer::string2params(string const & in,
655 InsetCommandParams & params)
657 params = InsetCommandParams();
662 istringstream data(in);
668 string const token = lex.getString();
673 // This is part of the inset proper that is usually swallowed
674 // by Buffer::readInset
677 string const token = lex.getString();
678 if (token != "Include")
683 InsetInclude inset(params);
685 params = inset.params();
691 InsetIncludeMailer::params2string(InsetCommandParams const & params)
693 InsetInclude inset(params);
695 data << name_ << ' ';
697 data << "\\end_inset\n";