From f19b6178a9505e4dfe17f1836096f4ce1040a200 Mon Sep 17 00:00:00 2001 From: Vincent van Ravesteijn Date: Sun, 22 Nov 2009 20:50:35 +0000 Subject: [PATCH] Fix bug #4360: Instant preview does not render formulas which contain math-macro from input file. Patch from Stefan. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@32157 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/graphics/PreviewLoader.cpp | 9 -- src/graphics/Previews.cpp | 2 +- src/insets/Inset.h | 3 +- src/insets/InsetExternal.cpp | 3 +- src/insets/InsetExternal.h | 2 +- src/insets/InsetInclude.cpp | 3 +- src/insets/InsetInclude.h | 2 +- src/insets/InsetTabular.cpp | 12 +- src/insets/InsetTabular.h | 3 +- src/insets/InsetText.cpp | 16 +- src/insets/InsetText.h | 2 +- src/mathed/InsetMathHull.cpp | 39 ++++- src/mathed/InsetMathHull.h | 5 +- src/mathed/MathMacroTemplate.cpp | 263 ++++++++++++++++--------------- src/mathed/MathMacroTemplate.h | 18 ++- 15 files changed, 219 insertions(+), 163 deletions(-) diff --git a/src/graphics/PreviewLoader.cpp b/src/graphics/PreviewLoader.cpp index 82893ed11c..453f36c91a 100644 --- a/src/graphics/PreviewLoader.cpp +++ b/src/graphics/PreviewLoader.cpp @@ -701,15 +701,6 @@ void PreviewLoader::Impl::dumpPreamble(odocstream & os) const << "\\def\\lyxlock{}\n" << "\n"; - // Loop over the insets in the buffer and dump all the math-macros. - Inset & inset = buffer_.inset(); - InsetIterator it = inset_iterator_begin(inset); - InsetIterator const end = inset_iterator_end(inset); - - for (; it != end; ++it) - if (it->lyxCode() == MATHMACRO_CODE) - it->latex(os, runparams); - // All equation labels appear as "(#)" + preview.sty's rendering of // the label name if (lyxrc.preview_hashed_labels) diff --git a/src/graphics/Previews.cpp b/src/graphics/Previews.cpp index b31292fc30..7c50618a9a 100644 --- a/src/graphics/Previews.cpp +++ b/src/graphics/Previews.cpp @@ -72,7 +72,7 @@ void Previews::generateBufferPreviews(Buffer const & buffer) const InsetIterator const end = inset_iterator_end(inset); for (; it != end; ++it) - it->addPreview(ploader); + it->addPreview(it, ploader); ploader.startLoading(); } diff --git a/src/insets/Inset.h b/src/insets/Inset.h index bb269f90e0..da160e447c 100644 --- a/src/insets/Inset.h +++ b/src/insets/Inset.h @@ -449,7 +449,8 @@ public: * Most insets have no interest in this capability, so the method * defaults to empty. */ - virtual void addPreview(graphics::PreviewLoader &) const {} + virtual void addPreview(DocIterator const & insetPos, + graphics::PreviewLoader &) const {} /** Classifies the unicode characters appearing in a math inset * depending on whether they are to be translated as latex diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp index d86a78d313..058b4be321 100644 --- a/src/insets/InsetExternal.cpp +++ b/src/insets/InsetExternal.cpp @@ -757,7 +757,8 @@ void InsetExternal::validate(LaTeXFeatures & features) const } -void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const +void InsetExternal::addPreview(DocIterator const & /*inset_pos*/, + graphics::PreviewLoader & ploader) const { RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview(); if (!ptr) diff --git a/src/insets/InsetExternal.h b/src/insets/InsetExternal.h index a04f64cb66..22c909b0e2 100644 --- a/src/insets/InsetExternal.h +++ b/src/insets/InsetExternal.h @@ -139,7 +139,7 @@ private: /// Update needed features for this inset. void validate(LaTeXFeatures & features) const; /// - void addPreview(graphics::PreviewLoader &) const; + void addPreview(DocIterator const &, graphics::PreviewLoader &) const; /// bool showInsetDialog(BufferView * bv) const; /// diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index 2c2b230ba6..efc80a21ac 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -949,7 +949,8 @@ void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset, } // namespace anon -void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const +void InsetInclude::addPreview(DocIterator const & /*inset_pos*/, + graphics::PreviewLoader & ploader) const { Buffer const & buffer = ploader.buffer(); if (!preview_wanted(params(), buffer)) diff --git a/src/insets/InsetInclude.h b/src/insets/InsetInclude.h index 11e5ac2389..208c79ee6a 100644 --- a/src/insets/InsetInclude.h +++ b/src/insets/InsetInclude.h @@ -84,7 +84,7 @@ public: /// void validate(LaTeXFeatures &) const; /// - void addPreview(graphics::PreviewLoader &) const; + void addPreview(DocIterator const &, graphics::PreviewLoader &) const; /// void addToToc(DocIterator const &); /// diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp index 9b0abc9690..e149c1d810 100644 --- a/src/insets/InsetTabular.cpp +++ b/src/insets/InsetTabular.cpp @@ -5289,13 +5289,19 @@ bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf, } -void InsetTabular::addPreview(PreviewLoader & loader) const +void InsetTabular::addPreview(DocIterator const & inset_pos, + PreviewLoader & loader) const { row_type const rows = tabular.row_info.size(); col_type const columns = tabular.column_info.size(); + DocIterator cell_pos = inset_pos; + + cell_pos.push_back(CursorSlice(*const_cast(this))); for (row_type i = 0; i < rows; ++i) { - for (col_type j = 0; j < columns; ++j) - tabular.cellInset(i, j)->addPreview(loader); + for (col_type j = 0; j < columns; ++j) { + cell_pos.top().idx() = index(i, j); + tabular.cellInset(i, j)->addPreview(cell_pos, loader); + } } } diff --git a/src/insets/InsetTabular.h b/src/insets/InsetTabular.h index c8a9b3940e..902a1e1f70 100644 --- a/src/insets/InsetTabular.h +++ b/src/insets/InsetTabular.h @@ -813,7 +813,8 @@ public: /// virtual bool forcePlainLayout(idx_type cell = 0) const; /// - void addPreview(graphics::PreviewLoader &) const; + void addPreview(DocIterator const & inset_pos, + graphics::PreviewLoader &) const; /// lock cell with given index void edit(Cursor & cur, bool front, EntryDirection entry_from); diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp index 7da34f699d..1d57c843a9 100644 --- a/src/insets/InsetText.cpp +++ b/src/insets/InsetText.cpp @@ -641,16 +641,24 @@ void InsetText::appendParagraphs(ParagraphList & plist) } -void InsetText::addPreview(PreviewLoader & loader) const +void InsetText::addPreview(DocIterator const & text_inset_pos, + PreviewLoader & loader) const { ParagraphList::const_iterator pit = paragraphs().begin(); ParagraphList::const_iterator pend = paragraphs().end(); + int pidx = 0; - for (; pit != pend; ++pit) { + DocIterator inset_pos = text_inset_pos; + inset_pos.push_back(CursorSlice(*const_cast(this))); + + for (; pit != pend; ++pit, ++pidx) { InsetList::const_iterator it = pit->insetList().begin(); InsetList::const_iterator end = pit->insetList().end(); - for (; it != end; ++it) - it->inset->addPreview(loader); + inset_pos.pit() = pidx; + for (; it != end; ++it) { + inset_pos.pos() = it->pos; + it->inset->addPreview(inset_pos, loader); + } } } diff --git a/src/insets/InsetText.h b/src/insets/InsetText.h index bf72bcf1f7..fb1fb0a6ba 100644 --- a/src/insets/InsetText.h +++ b/src/insets/InsetText.h @@ -137,7 +137,7 @@ public: void appendParagraphs(ParagraphList &); /// - void addPreview(graphics::PreviewLoader &) const; + void addPreview(DocIterator const &, graphics::PreviewLoader &) const; /// void edit(Cursor & cur, bool front, EntryDirection entry_from); diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index 0142885559..f54e1a7dfb 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -28,6 +28,7 @@ #include "FuncStatus.h" #include "LaTeXFeatures.h" #include "LyXRC.h" +#include "MacroTable.h" #include "sgml.h" #include "TextPainter.h" #include "TocBackend.h" @@ -456,22 +457,44 @@ void InsetMathHull::initUnicodeMath() const } -void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const +void InsetMathHull::addPreview(DocIterator const & insetPos, + graphics::PreviewLoader & /*ploader*/) const { if (RenderPreview::status() == LyXRC::PREVIEW_ON) { - docstring const snippet = latexString(*this); - preview_->addPreview(snippet, ploader); + reloadPreview(insetPos); } } -bool InsetMathHull::notifyCursorLeaves(Cursor const & /*old*/, Cursor & cur) +void InsetMathHull::reloadPreview(DocIterator const & pos) const +{ + Buffer const * buffer = pos.buffer(); + + // collect macros at this position + MacroNameSet macros; + buffer->listMacroNames(macros); + MacroNameSet::iterator it = macros.begin(); + MacroNameSet::iterator end = macros.end(); + odocstringstream macro_preamble; + for (; it != end; ++it) { + MacroData const * data = buffer->getMacro(*it, pos, true); + if (data) { + data->write(macro_preamble, true); + macro_preamble << endl; + } + } + + // start preview + docstring const snippet = macro_preamble.str() + latexString(*this); + LYXERR(Debug::MACROS, "Preview snippet: " << snippet); + preview_->addPreview(snippet, *buffer); + preview_->startLoading(*buffer); +} + +bool InsetMathHull::notifyCursorLeaves(Cursor const & old, Cursor & cur) { if (RenderPreview::status() == LyXRC::PREVIEW_ON) { - Buffer const * buffer = cur.buffer(); - docstring const snippet = latexString(*this); - preview_->addPreview(snippet, *buffer); - preview_->startLoading(*buffer); + reloadPreview(old); cur.updateFlags(Update::Force); } return false; diff --git a/src/mathed/InsetMathHull.h b/src/mathed/InsetMathHull.h index db4ea41770..001b2e3ee4 100644 --- a/src/mathed/InsetMathHull.h +++ b/src/mathed/InsetMathHull.h @@ -132,7 +132,10 @@ public: /// //bool insetAllowed(InsetCode code) const; /// - void addPreview(graphics::PreviewLoader &) const; + void addPreview(DocIterator const & insetPos, + graphics::PreviewLoader &) const; + /// Recreate the preview if preview is enabled. + void reloadPreview(DocIterator const & pos) const; /// void initUnicodeMath() const; diff --git a/src/mathed/MathMacroTemplate.cpp b/src/mathed/MathMacroTemplate.cpp index 8e809e3b21..2d74457040 100644 --- a/src/mathed/MathMacroTemplate.cpp +++ b/src/mathed/MathMacroTemplate.cpp @@ -17,6 +17,7 @@ #include "LaTeXFeatures.h" #include "InsetMathBrace.h" #include "InsetMathChar.h" +#include "InsetMathHull.h" #include "InsetMathSqrt.h" #include "MathMacro.h" #include "MathMacroArgument.h" @@ -33,10 +34,13 @@ #include "FuncRequest.h" #include "FuncStatus.h" #include "Lexer.h" +#include "LyXRC.h" #include "Undo.h" #include "frontends/Painter.h" +#include "insets/RenderPreview.h" + #include "support/lassert.h" #include "support/convert.h" #include "support/debug.h" @@ -619,20 +623,15 @@ void MathMacroTemplate::edit(Cursor & cur, bool front, EntryDirection entry_from bool MathMacroTemplate::notifyCursorLeaves(Cursor const & old, Cursor & cur) { - // find this in cursor old - Cursor insetCur = old; - int scriptSlice = insetCur.find(this); - LASSERT(scriptSlice != -1, /**/); - insetCur.cutOff(scriptSlice); - - commitEditChanges(insetCur); + commitEditChanges(cur, old); updateLook(); cur.updateFlags(Update::Force); return InsetMathNest::notifyCursorLeaves(old, cur); } -void MathMacroTemplate::removeArguments(Cursor & cur, int from, int to) +void MathMacroTemplate::removeArguments(Cursor & cur, + DocIterator const & /*insetPos*/, int from, int to) { for (DocIterator it = doc_iterator_begin(&buffer(), this); it; it.forwardChar()) { if (!it.nextInset()) @@ -716,68 +715,98 @@ void MathMacroTemplate::insertMissingArguments(int maxArg) } -void MathMacroTemplate::changeArity(Cursor & cur, int newNumArg) +void MathMacroTemplate::changeArity(Cursor & cur, + DocIterator const & insetPos, int newNumArg) { // remove parameter which do not appear anymore in the definition for (int i = numargs_; i > newNumArg; --i) - removeParameter(cur, numargs_ - 1, false); + removeParameter(cur, insetPos, numargs_ - 1, false); // add missing parameter for (int i = numargs_; i < newNumArg; ++i) - insertParameter(cur, numargs_, false, false); + insertParameter(cur, insetPos, numargs_, false, false); } -void MathMacroTemplate::commitEditChanges(Cursor & cur) +/// +class AddRemoveMacroInstanceFix { - int argsInDef = maxArgumentInDefinition(); - if (argsInDef != numargs_) { - cur.recordUndoFullDocument(); - changeArity(cur, argsInDef); +public: + /// + AddRemoveMacroInstanceFix(int n, bool insert) : n_(n), insert_(insert) {} + /// + void operator()(MathMacro * macro) { + if (macro->folded()) { + if (insert_) + macro->insertArgument(n_); + else + macro->removeArgument(n_); + } } - insertMissingArguments(argsInDef); -} +private: + /// + int n_; + /// + bool insert_; +}; -// FIXME: factorize those functions here with a functional style, maybe using Boost's function -// objects? -void fixMacroInstancesAddRemove(Cursor const & from, docstring const & name, int n, bool insert) { - Cursor dit = from; +/// +class OptionalsMacroInstanceFix +{ +public: + /// + OptionalsMacroInstanceFix(int optionals) : optionals_(optionals) {} + /// + void operator()(MathMacro * macro) { + macro->setOptionals(optionals_); + } - for (; dit; dit.forwardPos()) { - // only until a macro is redefined - if (dit.inset().lyxCode() == MATHMACRO_CODE) { - MathMacroTemplate const & macroTemplate - = static_cast(dit.inset()); - if (macroTemplate.name() == name) - break; - } +private: + /// + int optionals_; +}; - // in front of macro instance? - Inset * inset = dit.nextInset(); - if (!inset) - continue; - InsetMath * insetMath = inset->asInsetMath(); - if (!insetMath) - continue; - MathMacro * macro = insetMath->asMacro(); - if (macro && macro->name() == name && macro->folded()) { - // found macro instance - if (insert) - macro->insertArgument(n); - else - macro->removeArgument(n); - } - } -} +/// +class NullMacroInstanceFix +{ +public: + /// + void operator()(MathMacro * macro) {} +}; -void fixMacroInstancesOptional(Cursor const & from, docstring const & name, int optionals) { - Cursor dit = from; +template +void fixMacroInstances(Cursor & cur, DocIterator const & inset_pos, + docstring const & name, F & fix) +{ + // goto position behind macro template + DocIterator dit = inset_pos; + dit.pop_back(); + dit.top().forwardPos(); + // remember hull to trigger preview reload + DocIterator hull(dit.buffer()); + bool preview_reload_needed = false; + + // iterate over all positions until macro is redefined for (; dit; dit.forwardPos()) { + // left the outer hull? + if (!hull.empty() && dit.depth() == hull.depth()) { + // reload the preview if necessary + if (preview_reload_needed) { + InsetMathHull * inset_hull = + hull.nextInset()->asInsetMath()->asHullInset(); + LASSERT(inset_hull, /**/); + inset_hull->reloadPreview(hull); + cur.updateFlags(Update::Force); + preview_reload_needed = false; + } + hull.clear(); + } + // only until a macro is redefined if (dit.inset().lyxCode() == MATHMACRO_CODE) { MathMacroTemplate const & macroTemplate @@ -793,44 +822,42 @@ void fixMacroInstancesOptional(Cursor const & from, docstring const & name, int InsetMath * insetMath = inset->asInsetMath(); if (!insetMath) continue; + + // in front of outer hull? + InsetMathHull * inset_hull = insetMath->asHullInset(); + if (inset_hull && hull.empty()) { + // remember this for later preview reload + hull = dit; + } + MathMacro * macro = insetMath->asMacro(); if (macro && macro->name() == name && macro->folded()) { - // found macro instance - macro->setOptionals(optionals); + fix(macro); + if (RenderPreview::status() == LyXRC::PREVIEW_ON) + preview_reload_needed = true; } } } -template -void fixMacroInstancesFunctional(Cursor const & from, - docstring const & name, F & fix) { - Cursor dit = from; - - for (; dit; dit.forwardPos()) { - // only until a macro is redefined - if (dit.inset().lyxCode() == MATHMACRO_CODE) { - MathMacroTemplate const & macroTemplate - = static_cast(dit.inset()); - if (macroTemplate.name() == name) - break; - } - - // in front of macro instance? - Inset * inset = dit.nextInset(); - if (!inset) - continue; - InsetMath * insetMath = inset->asInsetMath(); - if (!insetMath) - continue; - MathMacro * macro = insetMath->asMacro(); - if (macro && macro->name() == name && macro->folded()) - F(macro); +void MathMacroTemplate::commitEditChanges(Cursor & cur, + DocIterator const & inset_pos) +{ + int args_in_def = maxArgumentInDefinition(); + if (args_in_def != numargs_) { + cur.recordUndoFullDocument(); + changeArity(cur, inset_pos, args_in_def); } + insertMissingArguments(args_in_def); + + // make sure the preview are up to date + NullMacroInstanceFix fix; + fixMacroInstances(cur, inset_pos, name(), fix); } -void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy, bool addarg) +void MathMacroTemplate::insertParameter(Cursor & cur, + DocIterator const & inset_pos, int pos, bool greedy, bool addarg) { if (pos <= numargs_ && pos >= optionals_ && numargs_ < 9) { ++numargs_; @@ -845,13 +872,9 @@ void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy, bool } if (!greedy) { - Cursor dit = cur; - dit.leaveInset(*this); - // TODO: this was dit.forwardPosNoDescend before. Check that this is the same - dit.top().forwardPos(); - // fix macro instances - fixMacroInstancesAddRemove(dit, name(), pos, true); + AddRemoveMacroInstanceFix fix(pos, true); + fixMacroInstances(cur, inset_pos, name(), fix); } } @@ -859,11 +882,12 @@ void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy, bool } -void MathMacroTemplate::removeParameter(Cursor & cur, int pos, bool greedy) +void MathMacroTemplate::removeParameter(Cursor & cur, + DocIterator const & inset_pos, int pos, bool greedy) { if (pos < numargs_ && pos >= 0) { --numargs_; - removeArguments(cur, pos, pos); + removeArguments(cur, inset_pos, pos, pos); shiftArguments(pos + 1, -1); // removed optional parameter? @@ -886,13 +910,8 @@ void MathMacroTemplate::removeParameter(Cursor & cur, int pos, bool greedy) if (!greedy) { // fix macro instances - //boost::function fix = _1->insertArgument(n); - //fixMacroInstancesFunctional(dit, name(), fix); - Cursor dit = cur; - dit.leaveInset(*this); - // TODO: this was dit.forwardPosNoDescend before. Check that this is the same - dit.top().forwardPos(); - fixMacroInstancesAddRemove(dit, name(), pos, false); + AddRemoveMacroInstanceFix fix(pos, false); + fixMacroInstances(cur, inset_pos, name(), fix); } } @@ -900,7 +919,9 @@ void MathMacroTemplate::removeParameter(Cursor & cur, int pos, bool greedy) } -void MathMacroTemplate::makeOptional(Cursor & cur) { +void MathMacroTemplate::makeOptional(Cursor & cur, + DocIterator const & inset_pos) +{ if (numargs_ > 0 && optionals_ < numargs_) { ++optionals_; cells_.insert(cells_.begin() + optIdx(optionals_ - 1), optionalValues_[optionals_ - 1]); @@ -910,18 +931,17 @@ void MathMacroTemplate::makeOptional(Cursor & cur) { ++cur[macroSlice].idx(); // fix macro instances - Cursor dit = cur; - dit.leaveInset(*this); - // TODO: this was dit.forwardPosNoDescend before. Check that this is the same - dit.top().forwardPos(); - fixMacroInstancesOptional(dit, name(), optionals_); + OptionalsMacroInstanceFix fix(optionals_); + fixMacroInstances(cur, inset_pos, name(), fix); } updateLook(); } -void MathMacroTemplate::makeNonOptional(Cursor & cur) { +void MathMacroTemplate::makeNonOptional(Cursor & cur, + DocIterator const & inset_pos) +{ if (numargs_ > 0 && optionals_ > 0) { --optionals_; @@ -942,11 +962,8 @@ void MathMacroTemplate::makeNonOptional(Cursor & cur) { } // fix macro instances - Cursor dit = cur; - dit.leaveInset(*this); - // TODO: this was dit.forwardPosNoDescend before. Check that this is the same - dit.top().forwardPos(); - fixMacroInstancesOptional(dit, name(), optionals_); + OptionalsMacroInstanceFix fix(optionals_); + fixMacroInstances(cur, inset_pos, name(), fix); } updateLook(); @@ -960,77 +977,77 @@ void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd) case LFUN_MATH_MACRO_ADD_PARAM: if (numargs_ < 9) { - commitEditChanges(cur); + commitEditChanges(cur, cur); cur.recordUndoFullDocument(); size_t pos = numargs_; if (arg.size() != 0) pos = (size_t)convert(arg) - 1; // it is checked for >=0 in getStatus - insertParameter(cur, pos); + insertParameter(cur, cur, pos); } break; case LFUN_MATH_MACRO_REMOVE_PARAM: if (numargs_ > 0) { - commitEditChanges(cur); + commitEditChanges(cur, cur); cur.recordUndoFullDocument(); size_t pos = numargs_ - 1; if (arg.size() != 0) pos = (size_t)convert(arg) - 1; // it is checked for >=0 in getStatus - removeParameter(cur, pos); + removeParameter(cur, cur, pos); } break; case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM: if (numargs_ < 9) { - commitEditChanges(cur); + commitEditChanges(cur, cur); cur.recordUndoFullDocument(); - insertParameter(cur, numargs_, true); + insertParameter(cur, cur, numargs_, true); } break; case LFUN_MATH_MACRO_REMOVE_GREEDY_PARAM: if (numargs_ > 0) { - commitEditChanges(cur); + commitEditChanges(cur, cur); cur.recordUndoFullDocument(); - removeParameter(cur, numargs_ - 1, true); + removeParameter(cur, cur, numargs_ - 1, true); } break; case LFUN_MATH_MACRO_MAKE_OPTIONAL: - commitEditChanges(cur); + commitEditChanges(cur, cur); cur.recordUndoFullDocument(); - makeOptional(cur); + makeOptional(cur, cur); break; case LFUN_MATH_MACRO_MAKE_NONOPTIONAL: - commitEditChanges(cur); + commitEditChanges(cur, cur); cur.recordUndoFullDocument(); - makeNonOptional(cur); + makeNonOptional(cur, cur); break; case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM: if (numargs_ < 9) { - commitEditChanges(cur); + commitEditChanges(cur, cur); cur.recordUndoFullDocument(); - insertParameter(cur, optionals_); - makeOptional(cur); + insertParameter(cur, cur, optionals_); + makeOptional(cur, cur); } break; case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM: if (optionals_ > 0) { - commitEditChanges(cur); + commitEditChanges(cur, cur); cur.recordUndoFullDocument(); - removeParameter(cur, optionals_ - 1); + removeParameter(cur, cur, optionals_ - 1); } break; case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM: if (numargs_ == optionals_) { - commitEditChanges(cur); + commitEditChanges(cur, cur); cur.recordUndoFullDocument(); - insertParameter(cur, 0, true); - makeOptional(cur); + insertParameter(cur, cur, 0, true); + makeOptional(cur, cur); } break; diff --git a/src/mathed/MathMacroTemplate.h b/src/mathed/MathMacroTemplate.h index f6ac948d87..4a496e0857 100644 --- a/src/mathed/MathMacroTemplate.h +++ b/src/mathed/MathMacroTemplate.h @@ -115,17 +115,20 @@ private: virtual Inset * clone() const; /// remove #n with from<=n<=to - void removeArguments(Cursor & cur, int from, int to); + void removeArguments(Cursor & cur, DocIterator const & inset_pos, + int from, int to); /// shift every #n with from<=n, i.e. #n -> #(n-by) void shiftArguments(size_t from, int by); /// - void insertParameter(Cursor & cur, int pos, bool greedy = false, bool addarg = true); + void insertParameter(Cursor & cur, DocIterator const & inset_pos, + int pos, bool greedy = false, bool addarg = true); /// - void removeParameter(Cursor & cur, int pos, bool greedy = false); + void removeParameter(Cursor & cur, DocIterator const & inset_pos, + int pos, bool greedy = false); /// - void makeOptional(Cursor & cur); + void makeOptional(Cursor & cur, DocIterator const & inset_pos); /// - void makeNonOptional(Cursor & cur); + void makeNonOptional(Cursor & cur, DocIterator const & inset_pos); /// idx_type defIdx() const { return optionals_ + 1; } /// index of default value cell of optional parameter (#1 -> n=0) @@ -139,9 +142,10 @@ private: /// add missing #n arguments up to \c maxArg void insertMissingArguments(int maxArg); /// change the arity - void changeArity(Cursor & cur, int newNumArg); + void changeArity(Cursor & cur, DocIterator const & inset_pos, + int newNumArg); /// find arguments in definition and adapt the arity accordingly - void commitEditChanges(Cursor & cur); + void commitEditChanges(Cursor & cur, DocIterator const & inset_pos); /// The representation of the macro template, with some holes to edit mutable MathData look_; /// -- 2.39.2