]> git.lyx.org Git - features.git/commitdiff
* make macros global. This is conceptionally much easier for the user,
authorStefan Schimanski <sts@lyx.org>
Mon, 10 Mar 2008 12:49:02 +0000 (12:49 +0000)
committerStefan Schimanski <sts@lyx.org>
Mon, 10 Mar 2008 12:49:02 +0000 (12:49 +0000)
and moreover simplifies the code a lot.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23616 a592a061-630c-0410-9148-cb99ea01b6c8

13 files changed:
src/Buffer.cpp
src/Buffer.h
src/LaTeXFeatures.cpp
src/insets/InsetBox.cpp
src/insets/InsetBox.h
src/insets/InsetCollapsable.cpp
src/insets/InsetCollapsable.h
src/insets/InsetFloat.h
src/insets/InsetFootlike.h
src/insets/InsetListings.h
src/insets/InsetText.h
src/insets/InsetWrap.h
src/mathed/MathMacroTemplate.cpp

index b3d1091affed5754c3e3b2e11b3ff4ba0421e979..a5df4bd9f5f6d1a99645076273dcede11e7eb080 100644 (file)
@@ -1813,25 +1813,14 @@ MacroData const * Buffer::getMacro(docstring const & name,
 }
 
 
-void Buffer::updateEnvironmentMacros(DocIterator & it, pit_type lastpit,
-       DocIterator & scope) const
+void Buffer::updateMacros(DocIterator & it, DocIterator & scope) const
 {
-       Paragraph & par = it.paragraph();
-       depth_type depth = par.params().depth();
-       Length const & leftIndent = par.params().leftIndent();
+       pit_type lastpit = it.lastpit();
 
        // look for macros in each paragraph
        while (it.pit() <= lastpit) {
                Paragraph & par = it.paragraph();
 
-               // increased depth?
-               if ((par.params().depth() > depth
-                       || par.params().leftIndent() != leftIndent)
-                   && par.layout().isEnvironment()) {
-                       updateBlockMacros(it, scope);
-                       continue;
-               }
-
                // iterate over the insets of the current paragraph
                InsetList const & insets = par.insetList();
                InsetList::const_iterator iit = insets.begin();
@@ -1852,7 +1841,7 @@ void Buffer::updateEnvironmentMacros(DocIterator & it, pit_type lastpit,
 
                                // collect macros in inset
                                it.push_back(CursorSlice(*iit->inset));
-                               updateInsetMacros(it, newScope ? insetScope : scope);
+                               updateMacros(it, newScope ? insetScope : scope);
                                it.pop_back();
                                continue;
                        }
@@ -1909,48 +1898,6 @@ void Buffer::updateEnvironmentMacros(DocIterator & it, pit_type lastpit,
 }
 
 
-void Buffer::updateBlockMacros(DocIterator & it, DocIterator & scope) const
-{
-       Paragraph & par = it.paragraph();
-               
-       // set scope for macros in this paragraph:
-       // * either the "old" outer scope
-       // * or the scope ending after the environment
-       if (par.layout().isEnvironment()) {
-               // find end of environment block,
-               DocIterator envEnd = it;
-               pit_type n = it.lastpit() + 1;
-               depth_type depth = par.params().depth();
-               Length const & length = par.params().leftIndent();
-               // looping through the paragraph, basically until
-               // the layout changes or the depth gets smaller.
-               // (the logic of output_latex.cpp's TeXEnvironment)
-               do {
-                       envEnd.pit()++;
-                       if (envEnd.pit() == n)
-                               break;
-               } while (par.layout() == envEnd.paragraph().layout()
-                        || depth < envEnd.paragraph().params().depth()
-                        || length != envEnd.paragraph().params().leftIndent());               
-               
-               // collect macros from environment block
-               updateEnvironmentMacros(it, envEnd.pit() - 1, envEnd);
-       } else {
-               // collect macros from paragraph
-               updateEnvironmentMacros(it, it.pit(), scope);
-       }
-}
-
-
-void Buffer::updateInsetMacros(DocIterator & it, DocIterator & scope) const
-{
-       // look for macros in each paragraph
-       pit_type n = it.lastpit() + 1;
-       while (it.pit() < n)
-               updateBlockMacros(it, scope);       
-}
-
-
 void Buffer::updateMacros() const
 {
        if (d->macro_lock)
@@ -1970,7 +1917,7 @@ void Buffer::updateMacros() const
        DocIterator it = par_iterator_begin();
        DocIterator outerScope = it;
        outerScope.pit() = outerScope.lastpit() + 2;
-       updateInsetMacros(it, outerScope);
+       updateMacros(it, outerScope);
 }
 
 
index 33d019b3de1746d886ec1d16ef227b9181261f8d..eb9b581bf71f0b37273c5a0faafd99a547e3063b 100644 (file)
@@ -462,25 +462,11 @@ private:
        /// search for macro in local (buffer) table or in children
        MacroData const * getBufferMacro(docstring const & name,
                                         DocIterator const & pos) const;
-       /** Update macro table in the whole text inset
-           \param it at the start of the text inset)
-       */
-       void updateInsetMacros(DocIterator & it, 
-                              DocIterator & scope) const;
-       /** Update macro table for paragraphs until \c lastpit
+       /** Update macro table starting with position of it
            \param it in some text inset
-           \param lastpit last processed paragraph
        */
-       void updateEnvironmentMacros(DocIterator & it, 
-                                    pit_type lastpit, 
+       void updateMacros(DocIterator & it,
                                     DocIterator & scope) const;
-       /** Update macro table for one paragraph block with 
-           same layout and depth, until \c lastpit
-           \param it in some text inset
-           \param lastpit last processed paragraph
-       */
-       void updateBlockMacros(DocIterator & it, 
-                              DocIterator & scope) const;
 
        /// 
        bool readFileHelper(support::FileName const & s);
index 0540c937b391f643735109c9c2cd44b2b06ede11..a54dc842d708a058db60d7541aaddddd4e2fa7c5 100644 (file)
@@ -548,7 +548,7 @@ char const * simplefeatures[] = {
        "ifthen",
        "amsthm",
        "listings",
-       "bm"
+       "bm",
        "xargs"
 };
 
index b81ac332f25bba197ca0805123bd3558ca353752..ec99e94b59ab7c24f94bd60513a549c72482d568 100644 (file)
@@ -233,13 +233,6 @@ bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-bool InsetBox::isMacroScope() const
-{
-       BoxType btype = boxtranslator().find(params_.type);
-       return btype != Frameless || params_.inner_box;
-}
-
-
 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
 {
        BoxType btype = boxtranslator().find(params_.type);
index 29fa21e263b833463abcdeca47bf2a875ef5e8ad..f9ab46c67deb1bedfceb4889053164e319ab5be3 100644 (file)
@@ -103,8 +103,6 @@ private:
        ///
        bool noFontChange() const { return true; }
        ///
-       bool isMacroScope() const;
-       ///
        int latex(odocstream &, OutputParams const &) const;
        ///
        int plaintext(odocstream &, OutputParams const &) const;
index f9f8f3637606f07766ee960d7dd9f0f1826c2e55..75021227dbe0a11d790cc61c7feb6d052691468e 100644 (file)
@@ -816,22 +816,6 @@ InsetLayout::InsetDecoration InsetCollapsable::decoration() const
 }
 
 
-bool InsetCollapsable::isMacroScope() const
-{
-       // layout_ == 0 leads to no latex output, so ignore 
-       // the macros outside
-       if (!layout_)
-               return true;
-
-       // see InsetCollapsable::latex(...) below. In these case
-       // an environment is opened there
-       if (!layout_->latexname().empty())
-               return true;
-
-       return false;
-}
-
-
 int InsetCollapsable::latex(odocstream & os,
                          OutputParams const & runparams) const
 {
index 99679d315b6c73b5a65da0b29b6ac9f38bde2f09..2aa4f1b951eb15c5f311b5f31a053317fd6510d2 100644 (file)
@@ -78,8 +78,6 @@ public:
        /// can we go further down on mouse click?
        bool descendable() const;
        ///
-       bool isMacroScope() const;
-       ///
        void setLabel(docstring const & l);
        ///
        virtual void setButtonLabel() {}
index e068554408c4f46bd289012b97a3cae7dbf9aab1..1313484f62bd8a4c37f5f30991b04459f011dd6c 100644 (file)
@@ -61,8 +61,6 @@ public:
        ///
        InsetCode lyxCode() const { return FLOAT_CODE; }
        ///
-       bool isMacroScope() const { return true; }
-       ///
        int latex(odocstream &, OutputParams const &) const;
        ///
        int plaintext(odocstream &, OutputParams const &) const;
index 07f949ce8d511ee102cfbebcc8b59548bf3e0f72..20477a0099b2225a1d914cca2ab34325bccf3fd9 100644 (file)
@@ -32,8 +32,6 @@ private:
        ///
        void write(std::ostream & os) const;
        ///
-       bool isMacroScope() const { return true; }
-       ///
        bool insetAllowed(InsetCode) const;
        /** returns true if, when outputing LaTeX, font changes should
            be closed before generating this inset. This is needed for
index 85120aeb681c5f64779e7ed0776efb612ed42798..736d1822fb4bac6c8ce5883313df0224c5df26b8 100644 (file)
@@ -45,8 +45,6 @@ public:
        ///
        docstring editMessage() const;
        ///
-       bool isMacroScope() const { return true; }
-       ///
        int latex(odocstream &, OutputParams const &) const;
        ///
        void validate(LaTeXFeatures &) const;
index 2d183c11064db2d6898f1ba374c5897b6359a78d..5549b24a95588df423c2392c76afd1ea17112399 100644 (file)
@@ -131,7 +131,7 @@ public:
        /// should paragraph indendation be ommitted in any case?
        bool neverIndent() const;
        ///
-       virtual bool isMacroScope() const { return true; }
+       virtual bool isMacroScope() const { return false; }
        ///
        virtual bool allowMultiPar() const { return true; }
 
index aa47bec13025f6022ca076abb3fdd278118cb45d..4bdafb65555f6b95d3eae9c6f660408fcabf2988 100644 (file)
@@ -60,8 +60,6 @@ private:
        ///
        InsetCode lyxCode() const { return WRAP_CODE; }
        ///
-       bool isMacroScope() const { return true; }
-       ///
        int latex(odocstream &, OutputParams const &) const;
        ///
        int plaintext(odocstream &, OutputParams const &) const;
index 46612210e27f423f527b98be77df73a80fbad20d..a8bbba4283c5cba1652320fc74f00c58a359218f 100644 (file)
@@ -1133,40 +1133,51 @@ void MathMacroTemplate::write(WriteStream & os) const
 
 void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
 {
-       bool xargs = false;
-       // newcommand or renewcommand
-       if (os.latex() && optionals_ > 1) {
-               if (redefinition_ && !overwriteRedefinition)
-                       os << "\\renewcommandx";
-               else
-                       os << "\\newcommandx";
-               xargs = true;
-               //os << "\\newcommandx";
+       if (os.latex()) {
+               if (optionals_ > 0) {
+                       // macros with optionals use the xargs package, e.g.:
+                       // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
+                       if (redefinition_ && !overwriteRedefinition)
+                               os << "\\renewcommandx";
+                       else
+                               os << "\\newcommandx";
+
+                       os << "\\" << name().c_str()
+                          << "[" << numargs_ << "]"
+                          << "[usedefault, addprefix=\\global";
+                       for (int i = 0; i < optionals_; ++i) {
+                               docstring optValue = asString(cell(optIdx(i)));
+                               if (optValue.find(']') != docstring::npos
+                                   || optValue.find('=') != docstring::npos)
+                                       os << ", " << i + 1 << "="
+                                       << "{" << cell(optIdx(i)) << "}";
+                               else
+                                       os << ", " << i + 1 << "="
+                                       << cell(optIdx(i));
+                       }
+                       os << "]";
+               } else {
+                       // macros without optionals use standard _global_ \def macros:
+                       // \global\def\foo#1#2{#1,#2}
+                       os << "\\global\\def\\" << name().c_str();
+                       docstring param = from_ascii("#0");
+                       for (int i = 1; i <= numargs_; ++i) { 
+                               param[1] = '0' + i;
+                               os << param;
+                       }
+               }
        } else {
+               // in LyX output we use some pseudo syntax which is implementation
+               // independent, e.g.
+               // \newcommand{\foo}[2][default}{#1,#2}
                if (redefinition_ && !overwriteRedefinition)
                        os << "\\renewcommand";
                else
                        os << "\\newcommand";
-       }
-       os << "{\\" << name().c_str() << '}';
-       if (numargs_ > 0)
-               os << '[' << numargs_ << ']';
+               os << "{\\" << name().c_str() << '}';
+               if (numargs_ > 0)
+                       os << '[' << numargs_ << ']';
 
-       // optional values
-       if (xargs) {
-               os << "[usedefault";
-               for (int i = 0; i < optionals_; ++i) {
-                       docstring optValue = asString(cell(optIdx(i)));
-                       if (optValue.find(']') != docstring::npos
-                           || optValue.find('=') != docstring::npos)
-                               os << ", " << i + 1 << "="
-                                  << "{" << cell(optIdx(i)) << "}";
-                       else
-                               os << ", " << i + 1 << "="
-                                  << cell(optIdx(i));
-               }               
-               os << "]";
-       } else {
                for (int i = 0; i < optionals_; ++i) {
                        docstring optValue = asString(cell(optIdx(i)));
                        if (optValue.find(']') != docstring::npos)
@@ -1176,7 +1187,6 @@ void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) cons
                }
        }
 
-       
        os << "{" << cell(defIdx()) << "}";
 
        if (os.latex()) {
@@ -1260,7 +1270,7 @@ bool MathMacroTemplate::fixNameAndCheckIfValid()
        
 void MathMacroTemplate::validate(LaTeXFeatures & features) const
 {
-       if (optionals_ > 1) {
+       if (optionals_ > 0) {
                //features.require("newlyxcommand");
                features.require("xargs");
        }