]> git.lyx.org Git - features.git/commitdiff
* \newlyxcommand support
authorStefan Schimanski <sts@lyx.org>
Sun, 23 Dec 2007 16:31:37 +0000 (16:31 +0000)
committerStefan Schimanski <sts@lyx.org>
Sun, 23 Dec 2007 16:31:37 +0000 (16:31 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22280 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/MathMacro.cpp
src/mathed/MathMacroTemplate.cpp

index 8741dfc705658875bbb8235791cbb272d4052c55..37dc8aa0301e53f26a306df9f8c784216b89d4cb 100644 (file)
@@ -552,39 +552,28 @@ void MathMacro::write(WriteStream & os) const
        
        os << "\\" << name();
        bool first = true;
-       idx_type i = 0;
        
-       // Use macroBackup_ instead of macro_ here, because
-       // this is outside the metrics/draw calls, hence the macro_
-       // variable can point to a MacroData which was freed already.
-       vector<docstring> const & defaults = macroBackup_.defaults();
+       // Optional arguments:
+       // First find last non-empty optional argument
+       idx_type emptyOptFrom = 0;
+       idx_type i = 0;
+       for (; i < cells_.size() && i < optionals_; ++i) {
+               if (!cell(i).empty())
+                       emptyOptFrom = i + 1;
+       }
        
-       // Optional argument
-       if (os.latex()) {
-               // Print first optional in LaTeX semantics
-               if (i < optionals_) {
-                       // the first real optional, the others are non-optional in latex
-                       if (!cell(i).empty()) {
-                               first = false;
-                               os << "[" << cell(0) << "]";
-                       }
-                       
-                       ++i;
-               }
-       } else {
-               // In lyx mode print all in any case
-               for (; i < cells_.size() && i < optionals_; ++i) {
-                       first = false;
-                               os << "[" << cell(i) << "]";
-               }
+       // print out optionals
+       for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) {
+               first = false;
+               os << "[" << cell(i) << "]";
        }
-
+       
+       // skip the tailing empty optionals
+       i = optionals_;
+       
        // Print remaining macros 
-       // (also the further optional parameters in LaTeX mode!)
        for (; i < cells_.size(); ++i) {
-               if (cell(i).empty() && i < optionals_)
-                       os << "{" << defaults[i] << "}";
-               else if (cell(i).size() == 1 
+               if (cell(i).size() == 1 
                         && cell(i)[0].nucleus()->asCharInset()) {
                        if (first)
                                os << " ";
index c892d346733bf0740eb3b7bd7e9e6f1c05ae0ff9..231633b9475b799bd5a8bd632a3d814d67655f13 100644 (file)
@@ -970,40 +970,26 @@ void MathMacroTemplate::write(WriteStream & os) const
 
 void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
 {
-       if (type_ == MacroTypeDef) {
-               os << "\\def\\" << name().c_str();
-               for (int i = 1; i <= numargs_; ++i)
-                       os << '#' << i;
-       } else {
-               // newcommand or renewcommand
-               if (redefinition_ && !overwriteRedefinition)
+       // newcommand or renewcommand
+       if (os.latex() && optionals_ > 1)
+               os << "\\newlyxcommand";
+       else {
+               if (redefinition_)
                        os << "\\renewcommand";
                else
                        os << "\\newcommand";
-               os << "{\\" << name().c_str() << '}';
-               if (numargs_ > 0)
-                       os << '[' << numargs_ << ']';
-
-               // optional values
-               if (os.latex()) {
-                       // in latex only one optional possible, simulate the others
-                       if (optionals_ >= 1) {
-                               docstring optValue = asString(cell(optIdx(0)));
-                               if (optValue.find(']') != docstring::npos)
-                                       os << "[{" << cell(optIdx(0)) << "}]";
-                               else
-                                       os << "[" << cell(optIdx(0)) << "]";
-                       }
-               } else {
-                       // in lyx we handle all optionals as real optionals
-                       for (int i = 0; i < optionals_; ++i) {
-                               docstring optValue = asString(cell(optIdx(i)));
-                               if (optValue.find(']') != docstring::npos)
-                                       os << "[{" << cell(optIdx(i)) << "}]";
-                               else
-                                       os << "[" << cell(optIdx(i)) << "]";
-                       }
-               }
+       }
+       os << "{\\" << name().c_str() << '}';
+       if (numargs_ > 0)
+               os << '[' << numargs_ << ']';
+
+       // optional values
+       for (int i = 0; i < optionals_; ++i) {
+               docstring optValue = asString(cell(optIdx(i)));
+               if (optValue.find(']') != docstring::npos)
+                       os << "[{" << cell(optIdx(i)) << "}]";
+               else
+                       os << "[" << cell(optIdx(i)) << "]";
        }
 
        os << "{" << cell(defIdx()) << "}";
@@ -1016,7 +1002,6 @@ void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) cons
                if (!cell(displayIdx()).empty())
                        os << "\n{" << cell(displayIdx()) << '}';
        }
-}
 
 
 int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,