]> git.lyx.org Git - features.git/commitdiff
Support for removing "default" modules.
authorRichard Heck <rgheck@comcast.net>
Sun, 27 Jul 2008 17:46:27 +0000 (17:46 +0000)
committerRichard Heck <rgheck@comcast.net>
Sun, 27 Jul 2008 17:46:27 +0000 (17:46 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25917 a592a061-630c-0410-9148-cb99ea01b6c8

src/Buffer.cpp
src/BufferParams.cpp
src/BufferParams.h
src/frontends/qt4/GuiDocument.cpp

index 44e7b62a4c5c9b85137a9f894cba195cec1137a1..69462370447b2f01b75a9acf04d7a5c8b57a878a 100644 (file)
@@ -115,7 +115,7 @@ namespace os = support::os;
 
 namespace {
 
-int const LYX_FORMAT = 338; //Uwe: support for polytonic Greek
+int const LYX_FORMAT = 339; //rgh: removed modules
 
 typedef map<string, bool> DepClean;
 typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
@@ -471,6 +471,7 @@ int Buffer::readHeader(Lexer & lex)
        params().fontsCJK.erase();
        params().listings_params.clear();
        params().clearLayoutModules();
+       params().clearRemovedModules();
        params().pdfoptions().clear();
        
        for (int i = 0; i < 4; ++i) {
index 2117e8059c006818cbb39c86ea63bdc17261f2fc..95744c6669d896f8b034adda6f1797548cda2fd6 100644 (file)
@@ -487,6 +487,8 @@ string BufferParams::readToken(Lexer & lex, string const & token,
                readLocalLayout(lex);
        } else if (token == "\\begin_modules") {
                readModules(lex);
+       } else if (token == "\\begin_removed_modules") {
+               readRemovedModules(lex);
        } else if (token == "\\options") {
                lex.eatLine();
                options = lex.getString();
@@ -692,15 +694,26 @@ void BufferParams::writeFile(ostream & os) const
                os << "\\master " << master << '\n';
        }
        
-       //the modules
+       // removed modules
+       if (!removedModules_.empty()) {
+               os << "\\begin_removed_modules" << '\n';
+               set<string>::const_iterator it = removedModules_.begin();
+               set<string>::const_iterator en = removedModules_.end();
+               for (; it != en; it++)
+                       os << *it << '\n';
+               os << "\\end_removed_modules" << '\n';
+       }
+
+       // the modules
        if (!layoutModules_.empty()) {
                os << "\\begin_modules" << '\n';
                LayoutModuleList::const_iterator it = layoutModules_.begin();
-               for (; it != layoutModules_.end(); it++)
+               LayoutModuleList::const_iterator en = layoutModules_.end();
+               for (; it != en; it++)
                        os << *it << '\n';
                os << "\\end_modules" << '\n';
        }
-
+       
        // local layout information
        if (!local_layout.empty()) {
                // remove '\n' from the end 
@@ -1445,33 +1458,44 @@ bool BufferParams::setBaseClass(string const & classname)
        set<string>::const_iterator men = mods.end();
        for (; mit != men; mit++) {
                string const & modName = *mit;
-               LayoutModuleList::const_iterator const fit = 
-                               find(layoutModules_.begin(), layoutModules_.end(), modName);
-               if (fit == layoutModules_.end()) {
-                       // We need to make sure there's no module chosen that excludes this one
-                       LayoutModuleList::const_iterator lit = layoutModules_.begin();
-                       LayoutModuleList::const_iterator len = layoutModules_.end();
-                       bool foundit = false;
-                       // so iterate over the selected modules...
-                       for (; lit != len; lit++) {
-                               LyXModule * lm = moduleList[*lit];
-                               if (!lm)
-                                       continue;
-                               vector<string> const & exc = lm->getExcludedModules();
-                               // ...and see if one of them excludes us.
-                               if (find(exc.begin(), exc.end(), modName) != exc.end()) {
-                                       foundit = true;
-                                       LYXERR(Debug::TCLASS, "Default module `" << modName << 
-                                                       "' not added because excluded by loaded module `" << 
-                                                       *lit << "'.");
-                                       break;
-                               }
-                       }
-                       if (!foundit) {
-                               LYXERR(Debug::TCLASS, "Default module `" << modName << "' added.");
-                               layoutModules_.push_back(modName);
+               // see if we're already in use
+               if (find(layoutModules_.begin(), layoutModules_.end(), modName) != 
+                   layoutModules_.end()) {
+                       LYXERR(Debug::TCLASS, "Default module `" << modName << 
+                                       "' not added because already used.");
+                       continue;
+               }
+               // make sure the user hasn't removed it
+               if (find(removedModules_.begin(), removedModules_.end(), modName) != 
+                   removedModules_.end()) {
+                       LYXERR(Debug::TCLASS, "Default module `" << modName << 
+                                       "' not added because removed by user.");
+                       continue;
+               }
+               // Now we want to check the list of selected modules to see if any of them
+               // exclude this one.
+               bool foundit = false;
+               // so iterate over the selected modules...
+               LayoutModuleList::const_iterator lit = layoutModules_.begin();
+               LayoutModuleList::const_iterator len = layoutModules_.end();
+               for (; lit != len; lit++) {
+                       LyXModule * lm = moduleList[*lit];
+                       if (!lm)
+                               continue;
+                       vector<string> const & exc = lm->getExcludedModules();
+                       // ...and see if this one excludes us.
+                       if (find(exc.begin(), exc.end(), modName) != exc.end()) {
+                               foundit = true;
+                               LYXERR(Debug::TCLASS, "Default module `" << modName << 
+                                               "' not added because excluded by loaded module `" << 
+                                               *lit << "'.");
+                               break;
                        }
                }
+               if (!foundit) {
+                       LYXERR(Debug::TCLASS, "Default module `" << modName << "' added.");
+                       layoutModules_.push_back(modName);
+               }
        }
        return true;
 }
@@ -1541,13 +1565,6 @@ void BufferParams::makeDocumentClass()
 }
 
 
-vector<string> const & BufferParams::getModules() const 
-{
-       return layoutModules_;
-}
-
-
-
 bool BufferParams::addLayoutModule(string const & modName) 
 {
        LayoutModuleList::const_iterator it = layoutModules_.begin();
@@ -1560,12 +1577,6 @@ bool BufferParams::addLayoutModule(string const & modName)
 }
 
 
-void BufferParams::clearLayoutModules() 
-{
-       layoutModules_.clear();
-}
-
-
 Font const BufferParams::getFont() const
 {
        FontInfo f = documentClass().defaultfont();
@@ -1693,6 +1704,37 @@ void BufferParams::readModules(Lexer & lex)
 }
 
 
+void BufferParams::readRemovedModules(Lexer & lex)
+{
+       if (!lex.eatLine()) {
+               lyxerr << "Error (BufferParams::readRemovedModules):"
+                               "Unexpected end of input." << endl;
+               return;
+       }
+       while (true) {
+               string mod = lex.getString();
+               if (mod == "\\end_removed_modules")
+                       break;
+               removedModules_.insert(mod);
+               lex.eatLine();
+       }
+       // now we want to remove any removed modules that were previously 
+       // added. normally, that will be because default modules were added in 
+       // setBaseClass(), which gets called when \textclass is read at the 
+       // start of the read.
+       set<string>::const_iterator rit = removedModules_.begin();
+       set<string>::const_iterator const ren = removedModules_.end();
+       for (; rit != ren; rit++) {
+               LayoutModuleList::iterator const mit = layoutModules_.begin();
+               LayoutModuleList::iterator const men = layoutModules_.end();
+               LayoutModuleList::iterator found = find(mit, men, *rit);
+               if (found == men)
+                       continue;
+               layoutModules_.erase(found);
+       }
+}
+
+
 string BufferParams::paperSizeName(PapersizePurpose purpose) const
 {
        char real_papersize = papersize;
index fe85a3dab86da15f46170427b48490ac393efc02..fbc5a7b92515ad63ba17637de7d8a4f76ca4c6d2 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "support/copied_ptr.h"
 
+#include <set>
 #include <vector>
 
 namespace lyx {
@@ -51,6 +52,8 @@ class VSpace;
  */
 class BufferParams {
 public:
+       ///
+       typedef std::vector<std::string> LayoutModuleList;
        ///
        enum ParagraphSeparation {
                ///
@@ -124,7 +127,11 @@ public:
        /// but it seems to be needed by CutAndPaste::putClipboard().
        void setDocumentClass(DocumentClass const * const);
        /// List of modules in use
-       std::vector<std::string> const & getModules() const;
+       LayoutModuleList const & getModules() const { return layoutModules_; }
+       /// List of default modules the user has removed
+       std::set<std::string> const & getRemovedModules() const 
+                       { return removedModules_; }
+       ///
        /// Add a module to the list of modules in use.
        /// Returns true if module was successfully added.
        /// The makeClass variable signals whether to call makeDocumentClass. This
@@ -132,8 +139,13 @@ public:
        /// the BufferParams do not represent the parameters for an actual buffer
        /// (as in GuiDocument).
        bool addLayoutModule(std::string const & modName);
+       ///
+       void addRemovedModule(std::string const & modName) 
+                       { removedModules_.insert(modName); }
        /// Clear the list
-       void clearLayoutModules();
+       void clearLayoutModules() { layoutModules_.clear(); }
+       /// Clear the removed module list
+       void clearRemovedModules() { removedModules_.clear(); }
 
        /// returns the main font for the buffer (document)
        Font const getFont() const;
@@ -327,15 +339,18 @@ private:
        void readBulletsLaTeX(Lexer &);
        ///
        void readModules(Lexer &);
+       ///
+       void readRemovedModules(Lexer &);
 
        /// for use with natbib
        CiteEngine cite_engine_;
        ///
        DocumentClass * doc_class_;
-       ///
-       typedef std::vector<std::string> LayoutModuleList;
        /// 
        LayoutModuleList layoutModules_;
+       /// this is for modules that are required by the document class but that
+       /// the user has chosen not to use
+       std::set<std::string> removedModules_;
 
        /** Use the Pimpl idiom to hide those member variables that would otherwise
         *  drag in other header files.
index c5be6ef4e5fdf3516114001065517c80627f9096..2a55c7f46bb51e39c1f56927b30cfe919ca1df70 100644 (file)
@@ -1565,6 +1565,27 @@ void GuiDocument::apply(BufferParams & params)
        vector<string> selModList;
        for (int i = 0; i < srows; ++i)
                params.addLayoutModule(modules_sel_model_.getIDString(i));
+       // update the list of removed modules
+       params.clearRemovedModules();
+       set<string> const & reqmods = params.baseClass()->defaultModules();
+       set<string>::const_iterator rit = reqmods.begin();
+       set<string>::const_iterator ren = reqmods.end();
+       // check each of the required modules
+       for (; rit != ren; rit++) {
+               vector<string>::const_iterator mit = params.getModules().begin();
+               vector<string>::const_iterator men = params.getModules().end();
+               bool found = false;
+               for (; mit != men; mit++) {
+                       if (*rit == *mit) {
+                               found = true;
+                               break;
+                       }
+               }
+               if (!found) {
+                       // the module isn't present so must have been removed by the user
+                       params.addRemovedModule(*rit);
+               }
+       }
 
        if (mathsModule->amsautoCB->isChecked()) {
                params.use_amsmath = BufferParams::package_auto;