]> git.lyx.org Git - lyx.git/blobdiff - src/lyxtextclass.C
Fix deleting of paragraphs after undo (fix #236).
[lyx.git] / src / lyxtextclass.C
index cefc00a93b7540468a8708a3692662fb3a9d4099..7f6a668ff464adfee5de6161a4246f86ccfaf8d6 100644 (file)
@@ -172,23 +172,32 @@ bool LyXTextClass::Read(string const & filename, bool merge)
 
                case TC_DEFAULTSTYLE:
                        if (lexrc.next()) {
-                               string const name = subst(lowercase(lexrc.getString()), '_', ' ');
+                               string const name = subst(lexrc.getString(),
+                                                         '_', ' ');
                                defaultlayout_ = name;
                        }
                        break;
                        
                case TC_STYLE:
                        if (lexrc.next()) {
-                               string const name = subst(lowercase(lexrc.getString()),
+                               string const name = subst(lexrc.getString(),
                                                    '_', ' ');
                                if (hasLayout(name)) {
-                                       LyXLayout & lay = operator[](name);
+                                       LyXLayout & lay =
+                                               const_cast<LyXLayout &>(operator[](name));
                                        error = do_readStyle(lexrc, lay);
                                } else {
                                        LyXLayout lay;
                                        lay.setName(name);
                                        if (!(error = do_readStyle(lexrc, lay)))
                                                layoutlist.push_back(lay);
+                                       if (defaultlayout_.empty()) {
+                                               // We do not have a default
+                                               // layout yet, so we choose
+                                               // the first layout we
+                                               // encounter.
+                                               defaultlayout_ = name;
+                                       }
                                }
                        }
                        else {
@@ -199,7 +208,7 @@ bool LyXTextClass::Read(string const & filename, bool merge)
 
                case TC_NOSTYLE:
                        if (lexrc.next()) {
-                               string const style = subst(lowercase(lexrc.getString()),
+                               string const style = subst(lexrc.getString(),
                                                     '_', ' ');
                                if (!delete_layout(style))
                                        lyxerr << "Cannot delete style `" << style << "'" << endl;
@@ -487,7 +496,7 @@ string const & LyXTextClass::rightmargin() const
 
 bool LyXTextClass::hasLayout(string const & n) const
 {
-       string const name = (n.empty() ? defaultLayoutName() : lowercase(n));
+       string const name = (n.empty() ? defaultLayoutName() : n);
        
        return find_if(layoutlist.begin(), layoutlist.end(),
                       lyx::compare_memfun(&LyXLayout::name, name))
@@ -497,42 +506,26 @@ bool LyXTextClass::hasLayout(string const & n) const
 
 LyXLayout const & LyXTextClass::operator[](string const & n) const
 {
+       lyx::Assert(!n.empty());
+       
        if (n.empty())
                lyxerr << "Operator[] called with empty n" << endl;
        
-       string const name = (n.empty() ? defaultLayoutName() : lowercase(n));
-       
-       LayoutList::const_iterator cit =
-               find_if(layoutlist.begin(),
-                       layoutlist.end(),
-                       lyx::compare_memfun(&LyXLayout::name, name));
-
-       if (cit == layoutlist.end()) {
-               lyxerr << "We failed to find the layout '" << name
-                      << "' in the layout list. You MUST investigate!"
-                      << endl;
-               
-               // we require the name to exist
-               lyx::Assert(false);
-       }
-
-       return *cit;
-}
+       string const name = (n.empty() ? defaultLayoutName() : n);
 
+       static string lastLayoutName;
+       static LayoutList::difference_type lastLayoutIndex;
 
-LyXLayout & LyXTextClass::operator[](string const & n)
-{
-       if (n.empty())
-               lyxerr << "Operator[] called with empty n" << endl;
+       if (name == lastLayoutName)
+               return layoutlist[lastLayoutIndex];
 
-       string const name = (n.empty() ? defaultLayoutName() : lowercase(n));
        
-       LayoutList::iterator it =
+       LayoutList::const_iterator cit =
                find_if(layoutlist.begin(),
                        layoutlist.end(),
                        lyx::compare_memfun(&LyXLayout::name, name));
 
-       if (it == layoutlist.end()) {
+       if (cit == layoutlist.end()) {
                lyxerr << "We failed to find the layout '" << name
                       << "' in the layout list. You MUST investigate!"
                       << endl;
@@ -540,15 +533,16 @@ LyXLayout & LyXTextClass::operator[](string const & n)
                // we require the name to exist
                lyx::Assert(false);
        }
+
+       lastLayoutName = name;
+       lastLayoutIndex = std::distance(layoutlist.begin(), cit);
        
-       return *it;
+       return *cit;
 }
 
 
-bool LyXTextClass::delete_layout(string const & n)
+bool LyXTextClass::delete_layout(string const & name)
 {
-       string const name = lowercase(n);
-
        if (name == defaultLayoutName())
                return false;
        
@@ -598,12 +592,6 @@ LyXLayout const & LyXTextClass::defaultLayout() const
 }
 
 
-LyXLayout & LyXTextClass::defaultLayout()
-{
-       return operator[](defaultLayoutName());
-}
-
-
 string const & LyXTextClass::name() const
 {
        return name_;