From 822efad7794c454537bb41876d967834eee5e904 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Wed, 11 Jun 2003 11:01:34 +0000 Subject: [PATCH] LFUN_EXTERNAL_EDIT. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7144 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 7 + src/LyXAction.C | 1 + src/frontends/controllers/ChangeLog | 5 + src/frontends/controllers/ControlExternal.C | 6 +- src/insets/ChangeLog | 11 ++ src/insets/graphicinset.C | 11 +- src/insets/graphicinset.h | 2 - src/insets/insetexternal.C | 154 +++++++++++--------- src/insets/insetexternal.h | 16 +- src/lfuns.h | 1 + src/lyxfunc.C | 7 + 11 files changed, 122 insertions(+), 99 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 137b7d4ba6..0578d83184 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2003-06-10 Angus Leeming + + * lfuns.h: + * LyXAction.C (init): new LFUN_EXTERNAL_EDIT. + + * lyxfunc.C (dispatch): invoke it. + 2003-06-11 Lars Gullik Bjønnes * main.C: protect with HAVE_IOS diff --git a/src/LyXAction.C b/src/LyXAction.C index 5dc982296d..bf89d50509 100644 --- a/src/LyXAction.C +++ b/src/LyXAction.C @@ -325,6 +325,7 @@ void LyXAction::init() { LFUN_INSET_SETTINGS, "inset-settings", ReadOnly }, { LFUN_PARAGRAPH_APPLY, "paragraph-params-apply", Noop }, { LFUN_PARAGRAPH_UPDATE, "", Noop }, + { LFUN_EXTERNAL_EDIT, "external-edit", Noop }, { LFUN_NOACTION, "", Noop } }; diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index a9572740e1..b9c6dd957e 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,8 @@ +2003-06-10 Angus Leeming + + * ControlExternal.C (editExternal): invokes new LFUN_EXTERNAL_EDIT rather + than generate a new inset itself. + 2003-06-06 John Levon * ControlDocument.C: make sure a language change diff --git a/src/frontends/controllers/ControlExternal.C b/src/frontends/controllers/ControlExternal.C index f6463e2c30..c985ef4fd5 100644 --- a/src/frontends/controllers/ControlExternal.C +++ b/src/frontends/controllers/ControlExternal.C @@ -70,10 +70,8 @@ void ControlExternal::editExternal() lyx::Assert(params_.get()); dialog().view().apply(); - InsetExternal inset; - inset.setParams(*params_, kernel().buffer()->filePath()); - inset.cache(kernel().bufferview()); - inset.editExternal(); + string const lfun = InsetExternalMailer::params2string(params()); + kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun)); } diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 291dab45ee..c5c7a3b631 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -2,6 +2,17 @@ * insettext.C (appendParagraphs): remove some commented code. +2003-06-10 Angus Leeming + + * graphicinset.[Ch] (view): removed the publicly accessible function to set + the cache. + + * insetexternal.[Ch]: move tempname_ into the Params struct. + (Params d-tor): new function, to clean-up this temp file. + (localDispatch): dispatch LFUN_EXTERNAL_EDIT. + (cache): removed. + (editExternal, doSubstitution): move out of class. + 2003-06-10 Angus Leeming * graphicinset.[Ch] (params): removed. diff --git a/src/insets/graphicinset.C b/src/insets/graphicinset.C index 04894eceee..7d80bfeec4 100644 --- a/src/insets/graphicinset.C +++ b/src/insets/graphicinset.C @@ -60,13 +60,6 @@ bool GraphicInset::hasFileChanged() const } -void GraphicInset::view(BufferView * bv) const -{ - if (bv) - view_ = bv->owner()->view(); -} - - BufferView * GraphicInset::view() const { return view_.lock().get(); @@ -184,8 +177,8 @@ void GraphicInset::metrics(MetricsInfo & mi, Dimension & dim) const void GraphicInset::draw(PainterInfo & pi, int x, int y) const { - // Cache the BufferView. - view(pi.base.bv); + if (pi.base.bv) + view_ = pi.base.bv->owner()->view(); #if 0 // Comment this out and see if anything goes wrong. diff --git a/src/insets/graphicinset.h b/src/insets/graphicinset.h index ed79d49eb4..cccc74b892 100644 --- a/src/insets/graphicinset.h +++ b/src/insets/graphicinset.h @@ -51,8 +51,6 @@ public: /// Is the stored checksum different to that of the graphics loader? bool hasFileChanged() const; - /// Set the cached store. - void view(BufferView *) const; /// An accessor function to the cached store. BufferView * view() const; diff --git a/src/insets/insetexternal.C b/src/insets/insetexternal.C index d0f23a0d33..0cfc8586e5 100644 --- a/src/insets/insetexternal.C +++ b/src/insets/insetexternal.C @@ -54,13 +54,31 @@ grfx::DisplayType const defaultDisplayType = grfx::NoDisplay; unsigned int defaultLyxScale = 100; +/// Substitute meta-variables in string s, makeing use of params and buffer. +string const doSubstitution(InsetExternal::Params const & params, + Buffer const * buffer, string const & s); + +/// Invoke the external editor. +void editExternal(InsetExternal::Params const & params, Buffer const * buffer); + } // namespace anon InsetExternal::Params::Params() : display(defaultDisplayType), lyxscale(defaultLyxScale) -{} +{ + tempname = lyx::tempName(string(), "lyxext"); + lyx::unlink(tempname); + // must have an extension for the converter code to work correctly. + tempname += ".tmp"; +} + + +InsetExternal::Params::~Params() +{ + lyx::unlink(tempname); +} InsetExternal::InsetExternal() @@ -90,8 +108,6 @@ Inset * InsetExternal::clone() const InsetExternal::~InsetExternal() { - if (!tempname_.empty()) - lyx::unlink(tempname_); InsetExternalMailer(*this).hideDialog(); } @@ -114,7 +130,18 @@ dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd) { switch (cmd.action) { + case LFUN_EXTERNAL_EDIT: { + lyx::Assert(cmd.view()); + + InsetExternal::Params p; + InsetExternalMailer::string2params(cmd.argument, p); + editExternal(p, cmd.view()->buffer()); + return DISPATCHED_NOUPDATE; + } + case LFUN_INSET_MODIFY: { + lyx::Assert(cmd.view()); + InsetExternal::Params p; InsetExternalMailer::string2params(cmd.argument, p); setParams(p, cmd.view()->buffer()->filePath()); @@ -137,12 +164,6 @@ dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd) } -void InsetExternal::cache(BufferView * bv) const -{ - renderer_->view(bv); -} - - void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const { renderer_->metrics(mi, dim); @@ -193,14 +214,14 @@ void InsetExternal::setParams(Params const & p, string const & filepath) // Update the display using the new parameters. if (params_.filename.empty() || !filepath.empty()) renderer_->update(get_grfx_params(params_, filepath)); - string const msg = doSubstitution(0, params_.templ.guiName); + string const msg = doSubstitution(params_, 0, params_.templ.guiName); renderer_->setNoDisplayMessage(msg); } string const InsetExternal::editMessage() const { - return doSubstitution(0, params_.templ.guiName); + return doSubstitution(params_, 0, params_.templ.guiName); } @@ -311,7 +332,7 @@ void InsetExternal::read(Buffer const * buffer, LyXLex & lex) // Update the display using the new parameters. if (buffer) renderer_->update(get_grfx_params(params_, buffer->filePath())); - string const msg = doSubstitution(0, params_.templ.guiName); + string const msg = doSubstitution(params_, 0, params_.templ.guiName); renderer_->setNoDisplayMessage(msg); } @@ -331,7 +352,7 @@ int InsetExternal::write(string const & format, } updateExternal(format, buf, external_in_tmpdir); - string const str = doSubstitution(buf, cit->second.product); + string const str = doSubstitution(params_, buf, cit->second.product); os << str; return int(lyx::count(str.begin(), str.end(),'\n') + 1); } @@ -397,56 +418,6 @@ void InsetExternal::validate(LaTeXFeatures & features) const } -string const InsetExternal::doSubstitution(Buffer const * buffer, - string const & s) const -{ - string result; - string const basename = ChangeExtension(params_.filename, string()); - string filepath; - bool external_in_tmpdir = false; - if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) { - filepath = buffer->filePath(); - if (lyxrc.use_tempdir) - external_in_tmpdir = true; - } - if (tempname_.empty()) { - string const path = external_in_tmpdir ? buffer->tmppath : string(); - tempname_ = lyx::tempName(path, "lyxext"); - lyx::unlink(tempname_); - // must have an extension for the converter code to work correctly. - tempname_ += ".tmp"; - } - result = subst(s, "$$FName", params_.filename); - result = subst(result, "$$Basename", basename); - result = subst(result, "$$FPath", filepath); - result = subst(result, "$$Tempname", tempname_); - result = subst(result, "$$Sysdir", system_lyxdir); - - // Handle the $$Contents(filename) syntax - if (contains(result, "$$Contents(\"")) { - - string::size_type const pos = result.find("$$Contents(\""); - string::size_type const end = result.find("\")", pos); - string const file = result.substr(pos + 12, end - (pos + 12)); - string contents; - if (buffer) { - Path p(buffer->filePath()); - if (!IsFileReadable(file)) - Path p(buffer->tmppath); - if (IsFileReadable(file)) - contents = GetFileContents(file); - } else { - contents = GetFileContents(file); - } - result = subst(result, - ("$$Contents(\"" + file + "\")").c_str(), - contents); - } - - return result; -} - - void InsetExternal::updateExternal(string const & format, Buffer const * buf, bool external_in_tmpdir) const @@ -499,7 +470,8 @@ void InsetExternal::updateExternal(string const & format, return; } - string const to_file = doSubstitution(buf, outputFormat.updateResult); + string const to_file = doSubstitution(params_, buf, + outputFormat.updateResult); FileInfo fi(from_file); string abs_to_file = to_file; @@ -517,18 +489,57 @@ void InsetExternal::updateExternal(string const & format, } -void InsetExternal::editExternal() const +namespace { + +/// Substitute meta-variables in this string +string const doSubstitution(InsetExternal::Params const & params, + Buffer const * buffer, string const & s) { - ExternalTemplate const & et = params_.templ; - if (et.editCommand.empty()) - return; + string result; + string const basename = ChangeExtension(params.filename, string()); + string filepath; + + result = subst(s, "$$FName", params.filename); + result = subst(result, "$$Basename", basename); + result = subst(result, "$$FPath", filepath); + result = subst(result, "$$Tempname", params.tempname); + result = subst(result, "$$Sysdir", system_lyxdir); + + // Handle the $$Contents(filename) syntax + if (contains(result, "$$Contents(\"")) { - BufferView const * bv = renderer_->view(); - Buffer const * buffer = bv ? bv->buffer() : 0; + string::size_type const pos = result.find("$$Contents(\""); + string::size_type const end = result.find("\")", pos); + string const file = result.substr(pos + 12, end - (pos + 12)); + string contents; + if (buffer) { + Path p(buffer->filePath()); + if (!IsFileReadable(file)) + Path p(buffer->tmppath); + if (IsFileReadable(file)) + contents = GetFileContents(file); + } else { + contents = GetFileContents(file); + } + result = subst(result, + ("$$Contents(\"" + file + "\")").c_str(), + contents); + } + + return result; +} + + +void editExternal(InsetExternal::Params const & params, Buffer const * buffer) +{ if (!buffer) return; - string const command = doSubstitution(buffer, et.editCommand); + ExternalTemplate const & et = params.templ; + if (et.editCommand.empty()) + return; + + string const command = doSubstitution(params, buffer, et.editCommand); Path p(buffer->filePath()); Forkedcall call; @@ -539,6 +550,7 @@ void InsetExternal::editExternal() const call.startscript(Forkedcall::DontWait, command); } +} // namespace anon string const InsetExternalMailer::name_("external"); diff --git a/src/insets/insetexternal.h b/src/insets/insetexternal.h index 0fe922c222..83285347fe 100644 --- a/src/insets/insetexternal.h +++ b/src/insets/insetexternal.h @@ -29,8 +29,11 @@ public: /// hold parameters settable from the GUI struct Params { Params(); + ~Params(); /// the filename string filename; + /// The name of the tempfile used for manipulations. + string tempname; /// the current template used ExternalTemplate templ; /// how the inset is displayed by LyX @@ -46,10 +49,6 @@ public: virtual ~InsetExternal(); /// virtual dispatch_result localDispatch(FuncRequest const & cmd); - /** Would not be needed if editExternal were dispatched properly from - * the frontends rather than being invoked directly. - */ - virtual void cache(BufferView *) const; /// void metrics(MetricsInfo &, Dimension &) const; /// @@ -97,9 +96,6 @@ public: void updateExternal(string const &, Buffer const *, bool external_in_tmpdir) const; - /// edit file of this template - void editExternal() const; - /// return a copy of our current params Params const & params() const; @@ -117,15 +113,9 @@ private: int write(string const & format, Buffer const *, std::ostream &, bool external_in_tmpdir = false) const; - /// Substitute meta-variables in this string - string const doSubstitution(Buffer const *, string const & s) const; - /// the current params Params params_; - /// A temp filename - mutable string tempname_; - /// The thing that actually draws the image on LyX's screen. boost::scoped_ptr const renderer_; }; diff --git a/src/lfuns.h b/src/lfuns.h index 0ecea946a9..14dcb4be1a 100644 --- a/src/lfuns.h +++ b/src/lfuns.h @@ -335,6 +335,7 @@ enum kb_action { // 255 LFUN_PARAGRAPH_APPLY, LFUN_PARAGRAPH_UPDATE, + LFUN_EXTERNAL_EDIT, LFUN_LASTACTION // end of the table }; diff --git a/src/lyxfunc.C b/src/lyxfunc.C index 293e23f3c5..332f0eb267 100644 --- a/src/lyxfunc.C +++ b/src/lyxfunc.C @@ -44,6 +44,7 @@ #include "ParagraphParameters.h" #include "insets/insetcommand.h" +#include "insets/insetexternal.h" #include "insets/insettabular.h" #include "mathed/formulamacro.h" @@ -1588,6 +1589,12 @@ void LyXFunc::dispatch(FuncRequest const & ev, bool verbose) owner->getDialogs().toggleTooltips(); break; + case LFUN_EXTERNAL_EDIT: { + InsetExternal() + .localDispatch(FuncRequest(view(), action, argument)); + break; + } + default: // Then if it was none of the above // Trying the BufferView::pimpl dispatch: -- 2.39.2