]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetexternal.C
My patch from yesterday
[lyx.git] / src / insets / insetexternal.C
index 0349abad2bfc46bab2c6a12d49a2d5426dd3a071..fd5fe30017f1c40deb2e1380df6d4912eb95d9e1 100644 (file)
@@ -1,41 +1,45 @@
-/* This file is part of
- * ======================================================
+/**
+ * \file insetexternal.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *           LyX, The Document Processor
- *     
- *         Copyright 1995 Matthias Ettrich
- *          Copyright 1995-2001 The LyX Team.
+ * \author Asger Alstrup Nielsen
  *
- * ====================================================== */
+ * Full author contact details are available in file CREDITS
+ */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#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 "funcrequest.h"
 #include "lyx_main.h"
 #include "LaTeXFeatures.h"
+#include "gettext.h"
+#include "debug.h"
+#include "lyxlex.h"
+
+#include "frontends/LyXView.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 "debug.h"
+#include "support/systemcall.h"
 #include "support/FileInfo.h"
 
+#include <cstdio>
+#include <utility>
+
+
+using std::ostream;
 using std::endl;
 
-InsetExternal::InsetExternal() 
+
+InsetExternal::InsetExternal()
        : view_(0)
 {
        tempname_ = lyx::tempName(string(), "lyxext");
@@ -47,22 +51,58 @@ InsetExternal::InsetExternal()
 InsetExternal::~InsetExternal()
 {
        lyx::unlink(tempname_);
-       hideDialog();
+       InsetExternalMailer mailer(*this);
+       mailer.hideDialog();
 }
 
 
-InsetExternal::Params InsetExternal::params() const
+InsetExternal::Params const & InsetExternal::params() const
 {
        return params_;
 }
+
+
+dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd)
+{
+       dispatch_result result = UNDISPATCHED;
+
+       switch (cmd.action) {
+       case LFUN_INSET_MODIFY: {
+               InsetExternal::Params p;
+               InsetExternalMailer::string2params(cmd.argument, p);
+               if (p.filename.empty())
+                       break;
+
+               setFromParams(p);
+               cmd.view()->updateInset(this);
+               result = DISPATCHED;
+       }
+       break;
+
+       case LFUN_INSET_DIALOG_UPDATE: {
+               InsetExternalMailer mailer(*this);
+               mailer.updateDialog(cmd.view());
+       }
+       break;
+
+       case LFUN_MOUSE_RELEASE:
+               edit(cmd.view(), cmd.x, cmd.y, cmd.button());
+               break;
+
+       default:
+               break;
+       }
+
+       return result;
+}
+
+
 void InsetExternal::setFromParams(Params const & p)
 {
        params_.filename = p.filename;
        params_.parameters = p.parameters;
        params_.templ = p.templ;
-}      
+}
 
 
 string const InsetExternal::editMessage() const
@@ -71,24 +111,23 @@ string const InsetExternal::editMessage() const
 }
 
 
-void InsetExternal::edit(BufferView * bv,
-                        int /*x*/, int /*y*/, unsigned int /*button*/)
+void InsetExternal::edit(BufferView * bv, int, int, mouse_button::state)
 {
-       view_ = bv;
-       view_->owner()->getDialogs()->showExternal(this);
+       InsetExternalMailer mailer(*this);
+       mailer.showDialog(bv);
 }
 
 
 void InsetExternal::edit(BufferView * bv, bool)
 {
-       edit(bv, 0, 0, 0);
+       edit(bv, 0, 0, mouse_button::none);
 }
 
 
-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";
 }
 
 
@@ -103,7 +142,7 @@ void InsetExternal::read(Buffer const *, LyXLex & lex)
        } else {
                lex.printError("InsetExternal: Parse error: `$$Token'");
        }
-       
+
        while (lex.isOK()) {
                lex.nextToken();
                token = lex.getString();
@@ -116,31 +155,31 @@ void InsetExternal::read(Buffer const *, LyXLex & lex)
        }
 
        // Parse string format...
-       string::size_type const pos1 = format.find(",");
+       string::size_type const pos1 = format.find(',');
        params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
        string::size_type const pos2 = format.find("\",\"", pos1);
        params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
        params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
 
        lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
-                           << " " << params_.filename
-                           << " " << params_.parameters << endl;
+                           << ' ' << params_.filename
+                           << ' ' << params_.parameters << endl;
 }
 
 
 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;
        }
-       
+
        updateExternal(format, buf);
        os << doSubstitution(buf, cit->second.product);
        return 0; // CHECK  (FIXME check what ? - jbl)
@@ -148,25 +187,25 @@ int InsetExternal::write(string const & format,
 
 
 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, bool) const
 {
        return write("DocBook", buf, os);
 }
@@ -180,7 +219,7 @@ void InsetExternal::validate(LaTeXFeatures & features) const
 
        if (cit == et.formats.end())
                return;
-       
+
        if (!cit->second.requirement.empty()) {
                features.require(cit->second.requirement);
        }
@@ -192,7 +231,7 @@ void InsetExternal::validate(LaTeXFeatures & features) const
 
 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
 {
-       InsetExternal * inset = new InsetExternal();
+       InsetExternal * inset = new InsetExternal;
        inset->params_ = params_;
        inset->view_ = view_;
        if (same_id)
@@ -215,32 +254,31 @@ void InsetExternal::executeCommand(string const & s,
                                   Buffer const * buffer) const
 {
        Path p(buffer->filePath());
-       Systemcalls one;
+       Systemcall one;
        if (lyxerr.debugging()) {
                lyxerr << "Executing '" << s << "' in '"
-                      << buffer->filePath() << "'" << 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)) {
+       if (buffer && !buffer->tmppath.empty() && !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);
-       
+
        // Handle the $$Contents(filename) syntax
        if (contains(result, "$$Contents(\"")) {
 
@@ -266,7 +304,7 @@ string const InsetExternal::doSubstitution(Buffer const * buffer,
 
 void InsetExternal::updateExternal() const
 {
-       updateExternal("LaTeX", view_->buffer());       
+       updateExternal("LaTeX", view_->buffer());
 }
 
 void InsetExternal::updateExternal(string const & format,
@@ -280,16 +318,16 @@ void InsetExternal::updateExternal(string const & format,
            cit->second.updateCommand.empty() ||
            !et.automaticProduction)
                return;
-       
+
        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 
+                   difftime(fi2.getModificationTime(),
+                            fi.getModificationTime()) >= 0) {
+                       lyxerr[Debug::FILES] << resultfile
                                             << " is up to date" << endl;
                        return;
                }
@@ -325,18 +363,61 @@ void InsetExternal::editExternal() const
 }
 
 
-bool operator==(InsetExternal::Params const & left,
-               InsetExternal::Params const & right)
+string const InsetExternalMailer::name_("external");
+
+InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
+       : inset_(inset)
+{}
+
+
+string const InsetExternalMailer::inset2string() const
 {
-       return ((left.filename   == right.filename) &&
-               (left.parameters == right.parameters) &&
-               (left.templ.lyxName == right.templ.lyxName));
+       return params2string(inset_.params());
 }
 
 
-bool operator!=(InsetExternal::Params const & left,
-               InsetExternal::Params const & right)
+void InsetExternalMailer::string2params(string const & in,
+                                       InsetExternal::Params & params)
 {
-       return  !(left == right);
+       params = InsetExternal::Params();
+
+       istringstream data(in);
+       LyXLex lex(0,0);
+       lex.setStream(data);
+
+       if (lex.isOK()) {
+               lex.next();
+               string const token = lex.getString();
+               if (token != name_)
+                       return;
+       }
+
+       // This is part of the inset proper that is usually swallowed
+       // by Buffer::readInset
+       if (lex.isOK()) {
+               lex.next();
+               string const token = lex.getString();
+               if (token != "External")
+                       return;
+       }
+
+       if (lex.isOK()) {
+               InsetExternal inset;    
+               inset.read(0, lex);
+               params = inset.params();
+       }
 }
 
+
+string const
+InsetExternalMailer::params2string(InsetExternal::Params const & params)
+{
+       InsetExternal inset;
+       inset.setFromParams(params);
+       ostringstream data;
+       data << name_ << ' ';
+       inset.write(0, data);
+       data << "\\end_inset\n";
+
+       return data.str();
+}