]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetexternal.C
fix #832
[lyx.git] / src / insets / insetexternal.C
index 33f26e05b50f4377bfed6d9ecd2897367947e0f4..fa27977806cde4790530e2b4a7290514338532fe 100644 (file)
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "insetexternal.h"
 #include "ExternalTemplate.h"
 #include "BufferView.h"
 #include "buffer.h"
-#include "frontends/LyXView.h"
+#include "funcrequest.h"
 #include "lyx_main.h"
 #include "LaTeXFeatures.h"
 #include "gettext.h"
 #include "debug.h"
 #include "lyxlex.h"
+#include "Lsstream.h"
 
+#include "frontends/LyXView.h"
 #include "frontends/Dialogs.h"
 
 #include "support/filetools.h"
@@ -36,7 +34,6 @@
 #include <cstdio>
 #include <utility>
 
-
 using std::ostream;
 using std::endl;
 
@@ -53,41 +50,57 @@ 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_;
 }
 
 
-void InsetExternal::setFromParams(Params const & p)
+dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd)
 {
-       params_.filename = p.filename;
-       params_.parameters = p.parameters;
-       params_.templ = p.templ;
-}
+       switch (cmd.action) {
+
+       case LFUN_INSET_MODIFY: {
+               InsetExternal::Params p;
+               InsetExternalMailer::string2params(cmd.argument, p);
+               if (!p.filename.empty()) {
+                       setFromParams(p);
+                       cmd.view()->updateInset(this);
+               }
+               return DISPATCHED;
+       }
 
+       case LFUN_INSET_DIALOG_UPDATE:
+               InsetExternalMailer(*this).updateDialog(cmd.view());
+               return DISPATCHED;
 
-string const InsetExternal::editMessage() const
-{
-       return doSubstitution(0, params_.templ.guiName);
+       case LFUN_MOUSE_RELEASE:
+       case LFUN_INSET_EDIT:
+               InsetExternalMailer(*this).showDialog(cmd.view());
+               return DISPATCHED;
+
+       default:
+               return UNDISPATCHED;
+       }
 }
 
 
-void InsetExternal::edit(BufferView * bv,
-                        int /*x*/, int /*y*/, mouse_button::state)
+void InsetExternal::setFromParams(Params const & p)
 {
-       view_ = bv;
-       view_->owner()->getDialogs().showExternal(this);
+       params_.filename = p.filename;
+       params_.parameters = p.parameters;
+       params_.templ = p.templ;
 }
 
 
-void InsetExternal::edit(BufferView * bv, bool)
+string const InsetExternal::editMessage() const
 {
-       edit(bv, 0, 0, mouse_button::none);
+       return doSubstitution(0, params_.templ.guiName);
 }
 
 
@@ -122,15 +135,15 @@ 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;
 }
 
 
@@ -224,7 +237,7 @@ void InsetExternal::executeCommand(string const & s,
        Systemcall one;
        if (lyxerr.debugging()) {
                lyxerr << "Executing '" << s << "' in '"
-                      << buffer->filePath() << "'" << endl;
+                      << buffer->filePath() << '\'' << endl;
        }
        one.startscript(Systemcall::Wait, s);
 }
@@ -245,7 +258,6 @@ string const InsetExternal::doSubstitution(Buffer const * buffer,
        result = subst(result, "$$FPath", filepath);
        result = subst(result, "$$Tempname", tempname_);
        result = subst(result, "$$Sysdir", system_lyxdir);
-       result = ReplaceEnvironmentPath(result);
 
        // Handle the $$Contents(filename) syntax
        if (contains(result, "$$Contents(\"")) {
@@ -331,17 +343,63 @@ 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 params2string(inset_.params());
+}
+
+
+void InsetExternalMailer::string2params(string const & in,
+                                       InsetExternal::Params & params)
 {
-       return ((left.filename   == right.filename) &&
-               (left.parameters == right.parameters) &&
-               (left.templ.lyxName == right.templ.lyxName));
+       params = InsetExternal::Params();
+
+       if (in.empty())
+               return;
+
+       istringstream data(STRCONV(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();
+       }
 }
 
 
-bool operator!=(InsetExternal::Params const & left,
-               InsetExternal::Params const & right)
+string const
+InsetExternalMailer::params2string(InsetExternal::Params const & params)
 {
-       return  !(left == right);
+       InsetExternal inset;
+       inset.setFromParams(params);
+       ostringstream data;
+       data << name_ << ' ';
+       inset.write(0, data);
+       data << "\\end_inset\n";
+       return STRCONV(data.str());
 }