X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2Finsetexternal.C;h=ec9047185275278363040ce066eaf3b49adf159f;hb=4a5b7a5952ad2381fcdf4830511293e184c7c5a1;hp=6639eacbbe4bb006dfc2809bad2b66ef1e409288;hpb=a097c6236f0e56d6748b4522a64a30972c02d22d;p=lyx.git diff --git a/src/insets/insetexternal.C b/src/insets/insetexternal.C index 6639eacbbe..ec90471852 100644 --- a/src/insets/insetexternal.C +++ b/src/insets/insetexternal.C @@ -4,7 +4,7 @@ * LyX, The Document Processor * * Copyright 1995 Matthias Ettrich - * Copyright 1995-2000 The LyX Team. + * Copyright 1995-2001 The LyX Team. * * ====================================================== */ @@ -14,32 +14,36 @@ #pragma implementation #endif -#include -#include - #include "insetexternal.h" #include "ExternalTemplate.h" #include "BufferView.h" #include "buffer.h" #include "LyXView.h" -#include "frontends/Dialogs.h" #include "lyx_main.h" #include "LaTeXFeatures.h" +#include "gettext.h" +#include "debug.h" + +#include "frontends/Dialogs.h" + #include "support/filetools.h" #include "support/lstrings.h" #include "support/path.h" -#include "support/syscall.h" +#include "support/systemcall.h" +#include "support/FileInfo.h" + +#include +#include -#ifdef SIGC_CXX_NAMESPACES -using SigC::slot; -#endif +using std::ostream; using std::endl; + InsetExternal::InsetExternal() - : view(0) + : view_(0) { - tempname = lyx::tempName(string(), "lyxext"); + tempname_ = lyx::tempName(string(), "lyxext"); //ExternalTemplateManager::Templates::const_iterator i1; params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second; } @@ -47,18 +51,18 @@ InsetExternal::InsetExternal() InsetExternal::~InsetExternal() { - lyx::unlink(tempname); + lyx::unlink(tempname_); hideDialog(); } -InsetExternal::InsetExternalParams InsetExternal::params() const +InsetExternal::Params InsetExternal::params() const { return params_; } -void InsetExternal::setFromParams(InsetExternalParams const & p) +void InsetExternal::setFromParams(Params const & p) { params_.filename = p.filename; params_.parameters = p.parameters; @@ -66,40 +70,48 @@ void InsetExternal::setFromParams(InsetExternalParams const & p) } -string const InsetExternal::EditMessage() const +string const InsetExternal::editMessage() const { return doSubstitution(0, params_.templ.guiName); } -void InsetExternal::Edit(BufferView * bv, +void InsetExternal::edit(BufferView * bv, int /*x*/, int /*y*/, unsigned int /*button*/) { - view = bv; - view->owner()->getDialogs()->showExternal(this); + view_ = bv; + view_->owner()->getDialogs()->showExternal(this); +} + + +void InsetExternal::edit(BufferView * bv, bool) +{ + edit(bv, 0, 0, 0); } -void InsetExternal::Write(Buffer const *, std::ostream & os) const +void InsetExternal::write(Buffer const *, ostream & os) const { - os << "External " << params_.templ.lyxName << ",\"" << params_.filename - << "\",\"" << params_.parameters << "\"\n"; + os << "External " << params_.templ.lyxName << ",\"" + << params_.filename << "\",\"" << params_.parameters << "\"\n"; } -void InsetExternal::Read(Buffer const *, LyXLex & lex) +void InsetExternal::read(Buffer const *, LyXLex & lex) { string format; string token; // Read inset data from lex and store in format - if (lex.EatLine()) { - format = lex.GetString(); - } else + if (lex.eatLine()) { + format = lex.getString(); + } else { lex.printError("InsetExternal: Parse error: `$$Token'"); - while (lex.IsOK()) { + } + + while (lex.isOK()) { lex.nextToken(); - token = lex.GetString(); + token = lex.getString(); if (token == "\\end_inset") break; } @@ -122,55 +134,50 @@ void InsetExternal::Read(Buffer const *, LyXLex & lex) int InsetExternal::write(string const & format, - Buffer const * buf, std::ostream & os) const + Buffer const * buf, ostream & os) const { ExternalTemplate const & et = params_.templ; ExternalTemplate::Formats::const_iterator cit = et.formats.find(format); if (cit == et.formats.end()) { lyxerr << "External template format '" << format - << "' not specified in template " << params_.templ.lyxName - << endl; + << "' not specified in template " + << params_.templ.lyxName << endl; return 0; } - if (et.automaticProduction) { - executeCommand(doSubstitution(buf, - (*cit).second.updateCommand), - buf); - } - - os << doSubstitution(buf, (*cit).second.product); + updateExternal(format, buf); + os << doSubstitution(buf, cit->second.product); return 0; // CHECK (FIXME check what ? - jbl) } -int InsetExternal::Latex(Buffer const * buf, - std::ostream & os, bool, bool) const +int InsetExternal::latex(Buffer const * buf, + ostream & os, bool, bool) const { return write("LaTeX", buf, os); } -int InsetExternal::Ascii(Buffer const * buf, std::ostream & os, int) const +int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const { return write("Ascii", buf, os); } -int InsetExternal::Linuxdoc(Buffer const * buf, std::ostream & os) const +int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const { return write("LinuxDoc", buf, os); } -int InsetExternal::DocBook(Buffer const * buf, std::ostream & os) const +int InsetExternal::docbook(Buffer const * buf, ostream & os) const { return write("DocBook", buf, os); } -void InsetExternal::Validate(LaTeXFeatures & features) const +void InsetExternal::validate(LaTeXFeatures & features) const { ExternalTemplate const & et = params_.templ; ExternalTemplate::Formats::const_iterator cit = @@ -179,24 +186,27 @@ void InsetExternal::Validate(LaTeXFeatures & features) const if (cit == et.formats.end()) return; - if (!(*cit).second.requirement.empty()) { - features.require((*cit).second.requirement); + if (!cit->second.requirement.empty()) { + features.require(cit->second.requirement); } - if (!(*cit).second.preamble.empty()) { - features.externalPreambles += (*cit).second.preamble + "\n"; + if (!cit->second.preamble.empty()) { + features.addExternalPreamble(cit->second.preamble + "\n"); } } -Inset * InsetExternal::Clone(Buffer const &) const +Inset * InsetExternal::clone(Buffer const &, bool same_id) const { InsetExternal * inset = new InsetExternal(); inset->params_ = params_; + inset->view_ = view_; + if (same_id) + inset->id_ = id_; return inset; } -string const InsetExternal::getScreenLabel() const +string const InsetExternal::getScreenLabel(Buffer const *) const { ExternalTemplate const & et = params_.templ; if (et.guiName.empty()) @@ -209,28 +219,31 @@ string const InsetExternal::getScreenLabel() const void InsetExternal::executeCommand(string const & s, Buffer const * buffer) const { - string buf = MakeAbsPath(buffer->fileName()); - string path = OnlyPath(buf); - Path p(path); - Systemcalls one; + Path p(buffer->filePath()); + Systemcall one; if (lyxerr.debugging()) { lyxerr << "Executing '" << s << "' in '" - << path << "'" << endl; + << buffer->filePath() << "'" << endl; } - one.startscript(Systemcalls::Wait, s); + one.startscript(Systemcall::Wait, s); } string const InsetExternal::doSubstitution(Buffer const * buffer, - string const & s) const + string const & s) const { string result; string const basename = ChangeExtension(params_.filename, string()); + string filepath; + if (buffer && (!buffer->niceFile)) { + filepath = buffer->filePath(); + } result = subst(s, "$$FName", params_.filename); result = subst(result, "$$Basename", basename); result = subst(result, "$$Parameters", params_.parameters); + result = subst(result, "$$FPath", filepath); result = ReplaceEnvironmentPath(result); - result = subst(result, "$$Tempname", tempname); + result = subst(result, "$$Tempname", tempname_); result = subst(result, "$$Sysdir", system_lyxdir); // Handle the $$Contents(filename) syntax @@ -242,9 +255,7 @@ string const InsetExternal::doSubstitution(Buffer const * buffer, string contents; if (buffer) { // Make sure we are in the directory of the buffer - string const buf = MakeAbsPath(buffer->fileName()); - string const path = OnlyPath(buf); - Path p(path); + Path p(buffer->filePath()); contents = GetFileContents(file); } else { contents = GetFileContents(file); @@ -259,38 +270,78 @@ string const InsetExternal::doSubstitution(Buffer const * buffer, void InsetExternal::updateExternal() const +{ + updateExternal("LaTeX", view_->buffer()); +} + +void InsetExternal::updateExternal(string const & format, + Buffer const * buf) const { ExternalTemplate const & et = params_.templ; ExternalTemplate::Formats::const_iterator cit = - et.formats.find("LaTeX"); - if (cit == et.formats.end()) + et.formats.find(format); + + if (cit == et.formats.end() || + cit->second.updateCommand.empty() || + !et.automaticProduction) return; - executeCommand(doSubstitution(view->buffer(), - (*cit).second.updateCommand), - view->buffer()); + if (!cit->second.updateResult.empty()) { + string const resultfile = doSubstitution(buf, + cit->second.updateResult); + FileInfo fi(params_.filename); + FileInfo fi2(resultfile); + if (fi2.exist() && fi.exist() && + ::difftime(fi2.getModificationTime(), + fi.getModificationTime()) >= 0) { + lyxerr[Debug::FILES] << resultfile + << " is up to date" << endl; + return; + } + } + + executeCommand(doSubstitution(buf, cit->second.updateCommand), buf); } void InsetExternal::viewExternal() const { ExternalTemplate const & et = params_.templ; - if (et.automaticProduction) - updateExternal(); + if (et.viewCommand.empty()) + return; - executeCommand(doSubstitution(view->buffer(), - et.viewCommand), - view->buffer()); + updateExternal(); + executeCommand(doSubstitution(view_->buffer(), + et.viewCommand), + view_->buffer()); } void InsetExternal::editExternal() const { ExternalTemplate const & et = params_.templ; - if (et.automaticProduction) - updateExternal(); + if (et.editCommand.empty()) + return; + + updateExternal(); + executeCommand(doSubstitution(view_->buffer(), + et.editCommand), + view_->buffer()); +} + + +bool operator==(InsetExternal::Params const & left, + InsetExternal::Params const & right) +{ + return ((left.filename == right.filename) && + (left.parameters == right.parameters) && + (left.templ.lyxName == right.templ.lyxName)); +} + - executeCommand(doSubstitution(view->buffer(), - et.editCommand), - view->buffer()); +bool operator!=(InsetExternal::Params const & left, + InsetExternal::Params const & right) +{ + return !(left == right); } +