]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacroTemplate.cpp
Fix bug 5802 (http://bugzilla.lyx.org/show_bug.cgi?id=5802)
[lyx.git] / src / mathed / MathMacroTemplate.cpp
index e889deb226b2f4ba30bd3f95f1afd9b671501b14..6765f01a99783b8980d014da7c4de850785ee52d 100644 (file)
@@ -3,7 +3,8 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author André Pönitz
+ * \author André Pönitz
+ * \author Stefan Schimanski
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -629,8 +630,9 @@ bool MathMacroTemplate::notifyCursorLeaves(Cursor const & old, Cursor & cur)
 }
 
 
-void MathMacroTemplate::removeArguments(Cursor & cur, int from, int to) {
-       for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
+void MathMacroTemplate::removeArguments(Cursor & cur, int from, int to)
+{
+       for (DocIterator it = doc_iterator_begin(&buffer(), this); it; it.forwardChar()) {
                if (!it.nextInset())
                        continue;
                if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
@@ -650,8 +652,9 @@ void MathMacroTemplate::removeArguments(Cursor & cur, int from, int to) {
 }
 
 
-void MathMacroTemplate::shiftArguments(size_t from, int by) {
-       for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
+void MathMacroTemplate::shiftArguments(size_t from, int by)
+{
+       for (DocIterator it = doc_iterator_begin(&buffer(), this); it; it.forwardChar()) {
                if (!it.nextInset())
                        continue;
                if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
@@ -668,8 +671,7 @@ void MathMacroTemplate::shiftArguments(size_t from, int by) {
 int MathMacroTemplate::maxArgumentInDefinition() const
 {
        int maxArg = 0;
-       MathMacroTemplate * nonConst = const_cast<MathMacroTemplate *>(this);
-       DocIterator it = doc_iterator_begin(*nonConst);
+       DocIterator it = doc_iterator_begin(&buffer(), this);
        it.idx() = defIdx();
        for (; it; it.forwardChar()) {
                if (!it.nextInset())
@@ -689,7 +691,7 @@ void MathMacroTemplate::insertMissingArguments(int maxArg)
        idx_type idx = cell(displayIdx()).empty() ? defIdx() : displayIdx();
 
        // search for #n macros arguments
-       DocIterator it = doc_iterator_begin(*this);
+       DocIterator it = doc_iterator_begin(&buffer(), this);
        it.idx() = idx;
        for (; it && it[0].idx() == idx; it.forwardChar()) {
                if (!it.nextInset())
@@ -1136,12 +1138,13 @@ void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) cons
                if (optionals_ > 0) {
                        // macros with optionals use the xargs package, e.g.:
                        // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
+                       // \long is implicit by xargs
                        if (redefinition_ && !overwriteRedefinition)
                                os << "\\renewcommandx";
                        else
                                os << "\\newcommandx";
 
-                       os << "\\" << name().c_str()
+                       os << "\\" << name()
                           << "[" << numargs_ << "]"
                           << "[usedefault, addprefix=\\global";
                        for (int i = 0; i < optionals_; ++i) {
@@ -1156,9 +1159,11 @@ void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) cons
                        }
                        os << "]";
                } else {
-                       // macros without optionals use standard _global_ \def macros:
-                       // \global\def\foo#1#2{#1,#2}
-                       os << "\\global\\def\\" << name().c_str();
+                       // Macros without optionals use standard _global_ \def macros:
+                       //   \global\def\long\foo#1#2{#1,#2}
+                       // We use the \long prefix as this is the equivalent to \newcommand.
+                       // We cannot use \newcommand directly because \global does not work with it.
+                       os << "\\global\\long\\def\\" << name();
                        docstring param = from_ascii("#0");
                        for (int i = 1; i <= numargs_; ++i) { 
                                param[1] = '0' + i;
@@ -1168,12 +1173,12 @@ void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) cons
        } else {
                // in LyX output we use some pseudo syntax which is implementation
                // independent, e.g.
-               // \newcommand{\foo}[2][default}{#1,#2}
+               // \newcommand{\foo}[2][default]{#1,#2}
                if (redefinition_ && !overwriteRedefinition)
                        os << "\\renewcommand";
                else
                        os << "\\newcommand";
-               os << "{\\" << name().c_str() << '}';
+               os << "{\\" << name() << '}';
                if (numargs_ > 0)
                        os << '[' << numargs_ << ']';