]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetexternal.C
Don't remove cell selections after fontchange.
[lyx.git] / src / insets / insetexternal.C
index 408a9ba40ec7efa4fc7aa5597cc7976c019d6a22..ec9047185275278363040ce066eaf3b49adf159f 100644 (file)
 #pragma implementation
 #endif
 
-#include <cstdio>
-#include <utility>
-
 #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 "gettext.h"
+#include "support/systemcall.h"
+#include "support/FileInfo.h"
 
+#include <cstdio>
+#include <utility>
+
+
+using std::ostream;
 using std::endl;
 
+
 InsetExternal::InsetExternal() 
        : view_(0)
 {
@@ -77,10 +84,16 @@ void InsetExternal::edit(BufferView * bv,
 }
 
 
-void InsetExternal::write(Buffer const *, std::ostream & os) const
+void InsetExternal::edit(BufferView * bv, bool)
 {
-       os << "External " << params_.templ.lyxName << ",\"" << params_.filename 
-          << "\",\"" << params_.parameters << "\"\n";
+       edit(bv, 0, 0, 0);
+}
+
+
+void InsetExternal::write(Buffer const *, ostream & os) const
+{
+       os << "External " << params_.templ.lyxName << ",\""
+          << params_.filename << "\",\"" << params_.parameters << "\"\n";
 }
 
 
@@ -90,13 +103,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;
        }
@@ -119,49 +134,44 @@ 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);
-       }
-       
+       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
+                        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);
 }
@@ -180,7 +190,7 @@ void InsetExternal::validate(LaTeXFeatures & features) const
                features.require(cit->second.requirement);
        }
        if (!cit->second.preamble.empty()) {
-               features.externalPreambles += cit->second.preamble + "\n";
+               features.addExternalPreamble(cit->second.preamble + "\n");
        }
 }
 
@@ -196,7 +206,7 @@ Inset * InsetExternal::clone(Buffer const &, bool same_id) const
 }
 
 
-string const InsetExternal::getScreenLabel() const
+string const InsetExternal::getScreenLabel(Buffer const *) const
 {
        ExternalTemplate const & et = params_.templ;
        if (et.guiName.empty())
@@ -209,26 +219,29 @@ 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, "$$Sysdir", system_lyxdir);
@@ -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,25 +270,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());
@@ -287,9 +320,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());