]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetexternal.C
2001-12-28 Lars Gullik Bj�nnes <larsbj@birdstep.com>
[lyx.git] / src / insets / insetexternal.C
index 172f9b6418c6ca20899f9aabdeea504ab5006669..735480991cc9c7671cbe9a4382ff3aa2172c4ed5 100644 (file)
@@ -30,6 +30,8 @@
 #include "support/path.h"
 #include "support/syscall.h"
 #include "gettext.h"
+#include "debug.h"
+#include "support/FileInfo.h"
 
 using std::endl;
 
@@ -77,6 +79,12 @@ void InsetExternal::edit(BufferView * bv,
 }
 
 
+void InsetExternal::edit(BufferView * bv, bool)
+{
+       edit(bv, 0, 0, 0);
+}
+
+
 void InsetExternal::write(Buffer const *, std::ostream & os) const
 {
        os << "External " << params_.templ.lyxName << ",\"" << params_.filename 
@@ -90,13 +98,15 @@ void InsetExternal::read(Buffer const *, LyXLex & lex)
        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;
        }
@@ -131,13 +141,8 @@ int InsetExternal::write(string const & format,
                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)
 }
 
@@ -161,7 +166,7 @@ int InsetExternal::linuxdoc(Buffer const * buf, std::ostream & os) const
 }
 
 
-int InsetExternal::docBook(Buffer const * buf, std::ostream & os) const
+int InsetExternal::docbook(Buffer const * buf, std::ostream & os) const
 {
        return write("DocBook", buf, os);
 }
@@ -176,25 +181,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())
@@ -224,9 +231,14 @@ string const InsetExternal::doSubstitution(Buffer const * buffer,
 {
        string result;
        string const basename = ChangeExtension(params_.filename, string());
+       string filepath = "";
+       if (buffer && (!buffer->niceFile)) {
+               filepath = OnlyPath(MakeAbsPath(buffer->fileName()));
+       }
        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, "$$Sysdir", system_lyxdir);
@@ -257,25 +269,47 @@ 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;
 
+       updateExternal();
        executeCommand(doSubstitution(view_->buffer(),
                                      et.viewCommand),
                       view_->buffer());
@@ -285,9 +319,10 @@ void InsetExternal::viewExternal() const
 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());