]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
layout as string
[lyx.git] / src / buffer.C
index 7ae0873b3715c7cc0c0819677febdeb8b9b85a50..331c336463b27a1f5c9c5f04851d2deaad42d4b3 100644 (file)
@@ -21,7 +21,6 @@
 #include "buffer.h"
 #include "bufferlist.h"
 #include "lyx_main.h"
-#include "lyx_gui_misc.h"
 #include "LyXAction.h"
 #include "lyxrc.h"
 #include "lyxlex.h"
@@ -45,6 +44,7 @@
 #include "BufferView.h"
 #include "ParagraphParameters.h"
 #include "iterators.h"
+#include "lyxtextclasslist.h"
 
 #include "mathed/formulamacro.h"
 #include "mathed/formula.h"
@@ -65,7 +65,6 @@
 #include "insets/insettoc.h"
 #include "insets/insetparent.h"
 #include "insets/insetspecialchar.h"
-#include "insets/figinset.h"
 #include "insets/insettext.h"
 #include "insets/insetert.h"
 #include "insets/insetgraphics.h"
 #include "support/filetools.h"
 #include "support/path.h"
 #include "support/os.h"
-#include "support/syscall.h"
 #include "support/lyxlib.h"
 #include "support/FileInfo.h"
 #include "support/lyxmanip.h"
+#include "support/lyxalgo.h" // for lyx::count
 
 #include <fstream>
 #include <iomanip>
@@ -129,7 +128,6 @@ using std::stack;
 using std::list;
 
 using lyx::pos_type;
-using lyx::layout_type;
 using lyx::textclass_type;
 
 // all these externs should eventually be removed.
@@ -147,21 +145,26 @@ extern int tex_code_break_column;
 
 
 Buffer::Buffer(string const & file, bool ronly)
+       : paragraph(0), lyx_clean(true), bak_clean(true),
+         unnamed(false), dep_clean(0), read_only(ronly),
+         filename_(file), users(0)
 {
        lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
-       filename = file;
-       filepath = OnlyPath(file);
-       paragraph = 0;
-       lyx_clean = true;
-       bak_clean = true;
-       dep_clean = 0;
-       read_only = ronly;
-       unnamed = false;
-       users = 0;
+//     filename = file;
+       filepath_ = OnlyPath(file);
+//     paragraph = 0;
+//     lyx_clean = true;
+//     bak_clean = true;
+//     dep_clean = 0;
+//     read_only = ronly;
+//     unnamed = false;
+//     users = 0;
        lyxvc.buffer(this);
-       if (read_only || (lyxrc.use_tempdir)) {
+       if (read_only || lyxrc.use_tempdir) {
                tmppath = CreateBufferTmpDir();
-       } else tmppath.erase();
+       } else {
+               tmppath.erase();
+       }
 }
 
 
@@ -193,7 +196,7 @@ Buffer::~Buffer()
 
 string const Buffer::getLatexName(bool no_path) const
 {
-       string const name = ChangeExtension(MakeLatexName(filename), ".tex");
+       string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
        if (no_path)
                return OnlyFilename(name);
        else
@@ -266,9 +269,9 @@ void Buffer::resetAutosaveTimers() const
 
 void Buffer::setFileName(string const & newfile)
 {
-       filename = MakeAbsPath(newfile);
-       filepath = OnlyPath(filename);
-       setReadonly(IsFileWriteable(filename) == 0);
+       filename_ = MakeAbsPath(newfile);
+       filepath_ = OnlyPath(filename_);
+       setReadonly(IsFileWriteable(filename_) == 0);
        updateTitles();
 }
 
@@ -286,13 +289,16 @@ struct ErtComp
        string contents;
        bool active;
        bool in_tabular;
+       LyXFont font;
 };
 
 std::stack<ErtComp> ert_stack;
 ErtComp ert_comp;
 #endif
 
+#ifdef WITH_WARNINGS
 #warning And _why_ is this here? (Lgb)
+#endif
 int unknown_layouts;
 int unknown_tokens;
 
@@ -330,7 +336,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
        } else {
                // We are inserting into an existing document
                users->text->breakParagraph(users);
-               first_par = users->text->firstParagraph();
+               first_par = users->text->ownerParagraph();
                pos = 0;
                markDirty();
                // We don't want to adopt the parameters from the
@@ -394,16 +400,13 @@ bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
 
 
 #ifndef NO_COMPABILITY
-void Buffer::insertErtContents(Paragraph * par, int & pos,
-                       LyXFont const & f, bool set_inactive) 
+void Buffer::insertErtContents(Paragraph * par, int & pos, bool set_inactive) 
 {
        if (!ert_comp.contents.empty()) {
                lyxerr[Debug::INSETS] << "ERT contents:\n'"
                                      << ert_comp.contents << "'" << endl;
-               Inset * inset = new InsetERT(ert_comp.contents, true);
-               LyXFont font;
-               font.setLanguage(f.language());
-               par->insertInset(pos++, inset, font);
+               Inset * inset = new InsetERT(params.language, ert_comp.contents, true);
+               par->insertInset(pos++, inset, ert_comp.font);
                ert_comp.contents.erase();
        }
        if (set_inactive) {
@@ -456,16 +459,19 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 #ifndef NO_COMPABILITY
                ert_comp.in_tabular = false;
                // Do the insetert.
-               insertErtContents(par, pos, font);
+               insertErtContents(par, pos);
 #endif
                lex.eatLine();
                string const layoutname = lex.getString();
-               pair<bool, layout_type> pp
-                 = textclasslist.NumberOfLayout(params.textclass, layoutname);
+               //lyxerr << "Looking for layout '"
+               // << layoutname << "'!" << endl;
 
+               //lyxerr << "Result: " << pp.first << "/" << pp.second << endl;
+               
 #ifndef NO_COMPABILITY
                if (compare_no_case(layoutname, "latex") == 0) {
                        ert_comp.active = true;
+                       ert_comp.font = font;
                }
 #endif
 #ifdef USE_CAPTION
@@ -527,34 +533,12 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                                par = new Paragraph(par);
                        }
                        pos = 0;
-                       if (pp.first) {
-                               par->layout = pp.second;
-#ifndef NO_COMPABILITY
-                       } else if (ert_comp.active) {
-                               par->layout = 0;
-#endif
-                       } else {
-                               // layout not found
-                               // use default layout "Standard" (0)
-                               par->layout = 0;
-                               ++unknown_layouts;
-                               string const s = _("Layout had to be changed from\n")
-                                       + layoutname + _(" to ")
-                                       + textclasslist.NameOfLayout(params.textclass, par->layout);
-                               InsetError * new_inset = new InsetError(s);
-                               par->insertInset(0, new_inset, 
-                                                LyXFont(LyXFont::ALL_INHERIT,
-                                                        params.language));
-                       }
+                       par->layout(layoutname);
                        // Test whether the layout is obsolete.
                        LyXLayout const & layout =
-                               textclasslist.Style(params.textclass,
-                                                   par->layout);
+                               textclasslist[params.textclass][par->layout()];
                        if (!layout.obsoleted_by().empty())
-                               par->layout = textclasslist
-                                       .NumberOfLayout(params.textclass,
-                                                       layout.obsoleted_by())
-                                       .second;
+                               par->layout(layout.obsoleted_by());
                        par->params().depth(depth);
                        font = LyXFont(LyXFont::ALL_INHERIT, params.language);
                        if (file_format < 216
@@ -566,8 +550,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 
 #ifndef NO_COMPABILITY
        } else if (token == "\\begin_float") {
-               insertErtContents(par, pos, font);
-               //insertErtContents(par, pos, font, false);
+               insertErtContents(par, pos);
+               //insertErtContents(par, pos, false);
                //ert_stack.push(ert_comp);
                //ert_comp = ErtComp();
                
@@ -657,7 +641,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                inset->read(this, nylex);
                par->insertInset(pos, inset, font);
                ++pos;
-               insertErtContents(par, pos, font);
+               insertErtContents(par, pos);
 #endif
        } else if (token == "\\begin_deeper") {
                ++depth;
@@ -683,7 +667,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                                string(_("LyX will not be able to produce output correctly.")));
                        params.textclass = 0;
                }
-               if (!textclasslist.Load(params.textclass)) {
+               if (!textclasslist[params.textclass].load()) {
                        // if the textclass wasn't loaded properly
                        // we need to either substitute another
                        // or stop loading the file.
@@ -691,7 +675,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        // stop loading... ideas??  ARRae980418
                        Alert::alert(_("Textclass Loading Error!"),
                                   string(_("Can't load textclass ")) +
-                                  textclasslist.NameOfClass(params.textclass),
+                                  textclasslist[params.textclass].name(),
                                   _("-- substituting default"));
                        params.textclass = 0;
                }
@@ -728,21 +712,25 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                par->params().startOfAppendix(true);
        } else if (token == "\\paragraph_separation") {
                int tmpret = lex.findToken(string_paragraph_separation);
-               if (tmpret == -1) ++tmpret;
+               if (tmpret == -1)
+                       ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) 
                        params.paragraph_separation =
                                static_cast<BufferParams::PARSEP>(tmpret);
        } else if (token == "\\defskip") {
                lex.nextToken();
                params.defskip = VSpace(lex.getString());
+#ifndef NO_COMPABILITY
        } else if (token == "\\epsfig") { // obsolete
                // Indeed it is obsolete, but we HAVE to be backwards
                // compatible until 0.14, because otherwise all figures
                // in existing documents are irretrivably lost. (Asger)
                params.readGraphicsDriver(lex);
+#endif
        } else if (token == "\\quotes_language") {
                int tmpret = lex.findToken(string_quotes_language);
-               if (tmpret == -1) ++tmpret;
+               if (tmpret == -1)
+                       ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) {
                        InsetQuotes::quote_language tmpl = 
                                InsetQuotes::EnglishQ;
@@ -805,7 +793,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                params.use_numerical_citations = lex.getInteger();
        } else if (token == "\\paperorientation") {
                int tmpret = lex.findToken(string_orientation);
-               if (tmpret == -1) ++tmpret;
+               if (tmpret == -1)
+                       ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) 
                        params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
        } else if (token == "\\paperwidth") {
@@ -877,6 +866,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                                //how can I put it back?
                }
        } else if (token == "\\bulletLaTeX") {
+               // The bullet class should be able to read this.
                lex.nextToken();
                int const index = lex.getInteger();
                lex.next();
@@ -893,6 +883,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        lex.next();
                        temp_str = lex.getString();
                }
+
                params.user_defined_bullets[index].setText(sum_str);
                params.temp_bullets[index].setText(sum_str);
        } else if (token == "\\secnumdepth") {
@@ -963,12 +954,13 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                string const tok = lex.getString();
                if (tok == "no_latex") {
                        // Do the insetert.
-                       insertErtContents(par, pos, font);
+                       insertErtContents(par, pos);
                } else if (tok == "latex") {
                        ert_comp.active = true;
+                       ert_comp.font = font;
                } else if (tok == "default") {
                        // Do the insetert.
-                       insertErtContents(par, pos, font);
+                       insertErtContents(par, pos);
                } else {
                        lex.printError("Unknown LaTeX font flag "
                                       "`$$Token'");
@@ -1019,10 +1011,16 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                }
        } else if (token == "\\added_space_top") {
                lex.nextToken();
-               par->params().spaceTop(VSpace(lex.getString()));
+               VSpace value = VSpace(lex.getString());
+               if ((value.length().len().value() != 0) ||
+                   (value.kind() != VSpace::LENGTH))
+                   par->params().spaceTop(value);
        } else if (token == "\\added_space_bottom") {
                lex.nextToken();
-               par->params().spaceBottom(VSpace(lex.getString()));
+               VSpace value = VSpace(lex.getString());
+               if ((value.length().len().value() != 0) ||
+                   (value.kind() != VSpace::LENGTH))
+                   par->params().spaceBottom(value);
 #ifndef NO_COMPABILITY
 #ifndef NO_PEXTRA_REALLY
        } else if (token == "\\pextra_type") {
@@ -1059,7 +1057,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                // the inset isn't it? Lgb.
        } else if (token == "\\begin_inset") {
 #ifndef NO_COMPABILITY
-               insertErtContents(par, pos, font, false);
+               insertErtContents(par, pos, false);
                ert_stack.push(ert_comp);
                ert_comp = ErtComp();
 #endif
@@ -1067,12 +1065,11 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 #ifndef NO_COMPABILITY
                ert_comp = ert_stack.top();
                ert_stack.pop();
-               insertErtContents(par, pos, font);
+               insertErtContents(par, pos);
 #endif
        } else if (token == "\\SpecialChar") {
                LyXLayout const & layout =
-                       textclasslist.Style(params.textclass, 
-                                           par->getLayout());
+                       textclasslist[params.textclass][par->layout()];
 
                // Insets don't make sense in a free-spacing context! ---Kayvan
                if (layout.free_spacing || par->isFreeSpacing()) {
@@ -1105,7 +1102,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        // Since we cannot know it this is only a regular
                        // newline or a tabular cell delimter we have to
                        // handle the ERT here.
-                       insertErtContents(par, pos, font, false);
+                       insertErtContents(par, pos, false);
 
                        par->insertChar(pos, Paragraph::META_NEWLINE, font);
                        ++pos;
@@ -1130,8 +1127,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                // Remove it later some time...introduced with fileformat
                // 2.16. (Lgb)
                LyXLayout const & layout =
-                       textclasslist.Style(params.textclass, 
-                                           par->getLayout());
+                       textclasslist[params.textclass][par->layout()];
 
                if (layout.free_spacing || par->isFreeSpacing()) {
                        par->insertChar(pos, ' ', font);
@@ -1161,7 +1157,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 #ifndef NO_COMPABILITY
                // If we still have some ert active here we have to insert
                // it so we don't loose it. (Lgb)
-               insertErtContents(par, pos, font);
+               insertErtContents(par, pos);
 #endif
                the_end_read = true;
 #ifndef NO_COMPABILITY
@@ -1194,7 +1190,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 #ifndef NO_PEXTRA_REALLY
        // I wonder if we could use this blanket fix for all the
        // checkminipage cases...
-       if (par && par->size()) {
+       // don't forget about ert paragraphs and compatibility read for'em
+       if (par && (par->size() || !ert_comp.contents.empty())) {
                // It is possible that this will check to often,
                // but that should not be an correctness issue.
                // Only a speed issue.
@@ -1208,6 +1205,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
        //
        // BEGIN pextra_minipage compability
        // This should be removed in 1.3.x (Lgb)
+       // I don't think we should remove this so fast (Jug)
        
        // This compability code is not perfect. In a couple
        // of rand cases it fails. When the minipage par is
@@ -1220,8 +1218,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
        if (checkminipage && (call_depth == 1)) {
        checkminipage = false;
        if (minipar && (minipar != par) &&
-           (par->params().pextraType()==Paragraph::PEXTRA_MINIPAGE))
-       {
+           (par->params().pextraType() == Paragraph::PEXTRA_MINIPAGE)) {
                lyxerr << "minipages in a row" << endl;
                if (par->params().pextraStartMinipage()) {
                        lyxerr << "start new minipage" << endl;
@@ -1242,10 +1239,10 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        // create a new paragraph to insert the
                        // minipages in the following case
                        if (par->params().pextraStartMinipage() &&
-                           !par->params().pextraHfill())
-                       {
+                           !par->params().pextraHfill()) {
                                Paragraph * p = new Paragraph;
-                               p->layout = 0;
+                               p->layout(textclasslist[params.textclass].defaultLayoutName());
+                               
                                p->previous(parBeforeMinipage);
                                parBeforeMinipage->next(p);
                                p->next(0);
@@ -1254,12 +1251,21 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        }
                        InsetMinipage * mini = new InsetMinipage;
                        mini->pos(static_cast<InsetMinipage::Position>(par->params().pextraAlignment()));
-                       mini->width(par->params().pextraWidth());
+                       mini->pageWidth(LyXLength(par->params().pextraWidth()));
                        if (!par->params().pextraWidthp().empty()) {
-                           lyxerr << "WP:" << mini->width() << endl;
-                           mini->width(tostr(par->params().pextraWidthp())+"%");
+                           lyxerr << "WP:" << mini->pageWidth().asString() << endl;
+                           mini->pageWidth(LyXLength((par->params().pextraWidthp())+"%"));
                        }
+                       Paragraph * op = mini->firstParagraph();
                        mini->inset.paragraph(par);
+                       //
+                       // and free the old ones!
+                       //
+                       while(op) {
+                               Paragraph * pp = op->next();
+                               delete op;
+                               op = pp;
+                       }
                        // Insert the minipage last in the
                        // previous paragraph.
                        if (par->params().pextraHfill()) {
@@ -1296,6 +1302,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        tmp = tmp->next();
                }
                depth = parBeforeMinipage->params().depth();
+               // and set this depth on the par as it has not been set already
+               par->params().depth(depth);
                minipar = parBeforeMinipage = 0;
        } else if (!minipar &&
                   (par->params().pextraType() == Paragraph::PEXTRA_MINIPAGE))
@@ -1308,7 +1316,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                // a sequence of minipages
                // in its own paragraph.
                Paragraph * p = new Paragraph;
-               p->layout = 0;
+               p->layout(textclasslist[params.textclass].defaultLayoutName());
                p->previous(par->previous());
                p->next(0);
                p->params().depth(depth);
@@ -1324,10 +1332,10 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 
                InsetMinipage * mini = new InsetMinipage;
                mini->pos(static_cast<InsetMinipage::Position>(minipar->params().pextraAlignment()));
-               mini->width(minipar->params().pextraWidth());
+               mini->pageWidth(LyXLength(minipar->params().pextraWidth()));
                if (!par->params().pextraWidthp().empty()) {
-                   lyxerr << "WP:" << mini->width() << endl;
-                   mini->width(tostr(par->params().pextraWidthp())+"%");
+                   lyxerr << "WP:" << mini->pageWidth().asString() << endl;
+                   mini->pageWidth(LyXLength((par->params().pextraWidthp())+"%"));
                }
                mini->inset.paragraph(minipar);
                        
@@ -1352,11 +1360,11 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 void Buffer::insertStringAsLines(Paragraph *& par, pos_type & pos,
                                  LyXFont const & fn,string const & str) const
 {
-       LyXLayout const & layout = textclasslist.Style(params.textclass, 
-                                                      par->getLayout());
+       LyXLayout const & layout =
+               textclasslist[params.textclass][par->layout()];
        LyXFont font = fn;
        
-       (void)par->checkInsertChar(font);
+       par->checkInsertChar(font);
        // insert the string, don't insert doublespace
        bool space_inserted = true;
        for(string::const_iterator cit = str.begin(); 
@@ -1478,8 +1486,8 @@ void Buffer::readInset(LyXLex & lex, Paragraph *& par,
                } else if (tmptok == "Formula") {
                        inset = new InsetFormula;
                } else if (tmptok == "Figure") { // Backward compatibility
-                       inset = new InsetFig(100, 100, *this);
-                       //inset = new InsetGraphics;
+//                     inset = new InsetFig(100, 100, *this);
+                       inset = new InsetGraphics;
                } else if (tmptok == "Graphics") {
                        inset = new InsetGraphics;
                } else if (tmptok == "Info") {// backwards compatibility
@@ -1490,7 +1498,7 @@ void Buffer::readInset(LyXLex & lex, Paragraph *& par,
                } else if (tmptok == "Note") {
                        inset = new InsetNote;
                } else if (tmptok == "Include") {
-                       InsetCommandParams p( "Include" );
+                       InsetCommandParams p("Include");
                        inset = new InsetInclude(p, *this);
                } else if (tmptok == "ERT") {
                        inset = new InsetERT;
@@ -1667,7 +1675,7 @@ bool Buffer::writeFile(string const & fname, bool flag) const
        // warnings, only cerr.
        // Needed for autosave in background or panic save (Matthias 120496)
 
-       if (read_only && (fname == filename)) {
+       if (read_only && (fname == fileName())) {
                // Here we should come with a question if we should
                // perform the write anyway.
                if (flag)
@@ -1755,178 +1763,219 @@ bool Buffer::writeFile(string const & fname, bool flag) const
 
 
 string const Buffer::asciiParagraph(Paragraph const * par,
-                                   unsigned int linelen) const
+                                    unsigned int linelen,
+                                    bool noparbreak) const
 {
        ostringstream buffer;
+       ostringstream word;
        Paragraph::depth_type depth = 0;
        int ltype = 0;
        Paragraph::depth_type ltype_depth = 0;
        string::size_type currlinelen = 0;
        bool ref_printed = false;
-
-       int noparbreak = 0;
-       int islatex = 0;
-       if (!par->previous()) {
-               // begins or ends a deeper area ?
-               if (depth != par->params().depth()) {
-                       if (par->params().depth() > depth) {
-                               while (par->params().depth() > depth) {
-                                       ++depth;
-                               }
-                       } else {
-                               while (par->params().depth() < depth) {
-                                       --depth;
-                               }
+//     if (!par->previous()) {
+#if 0
+       // begins or ends a deeper area ?
+       if (depth != par->params().depth()) {
+               if (par->params().depth() > depth) {
+                       while (par->params().depth() > depth) {
+                               ++depth;
                        }
-               }
-               
-               // First write the layout
-               string const tmp = textclasslist.NameOfLayout(params.textclass, par->layout);
-               if (tmp == "Itemize") {
-                       ltype = 1;
-                       ltype_depth = depth + 1;
-               } else if (tmp == "Enumerate") {
-                       ltype = 2;
-                       ltype_depth = depth + 1;
-               } else if (contains(tmp, "ection")) {
-                       ltype = 3;
-                       ltype_depth = depth + 1;
-               } else if (contains(tmp, "aragraph")) {
-                       ltype = 4;
-                       ltype_depth = depth + 1;
-               } else if (tmp == "Description") {
-                       ltype = 5;
-                       ltype_depth = depth + 1;
-               } else if (tmp == "Abstract") {
-                       ltype = 6;
-                       ltype_depth = 0;
-               } else if (tmp == "Bibliography") {
-                       ltype = 7;
-                       ltype_depth = 0;
                } else {
-                       ltype = 0;
-                       ltype_depth = 0;
+                       while (par->params().depth() < depth) {
+                               --depth;
+                       }
                }
+       }
+#else
+       depth = par->params().depth();
+#endif
+               
+       // First write the layout
+       string const & tmp = lowercase(par->layout());
+       if (tmp == "itemize") {
+               ltype = 1;
+               ltype_depth = depth + 1;
+       } else if (tmp == "enumerate") {
+               ltype = 2;
+               ltype_depth = depth + 1;
+       } else if (contains(tmp, "ection")) {
+               ltype = 3;
+               ltype_depth = depth + 1;
+       } else if (contains(tmp, "aragraph")) {
+               ltype = 4;
+               ltype_depth = depth + 1;
+       } else if (tmp == "description") {
+               ltype = 5;
+               ltype_depth = depth + 1;
+       } else if (tmp == "abstract") {
+               ltype = 6;
+               ltype_depth = 0;
+       } else if (tmp == "bibliography") {
+               ltype = 7;
+               ltype_depth = 0;
+       } else {
+               ltype = 0;
+               ltype_depth = 0;
+       }
                
-               /* maybe some vertical spaces */ 
+       /* maybe some vertical spaces */ 
                
-               /* the labelwidthstring used in lists */ 
+       /* the labelwidthstring used in lists */ 
                
-               /* some lines? */ 
+       /* some lines? */ 
                
-               /* some pagebreaks? */ 
+       /* some pagebreaks? */ 
                
-               /* noindent ? */ 
+       /* noindent ? */ 
                
-               /* what about the alignment */ 
-       } else {
-               lyxerr << "Should this ever happen?" << endl;
-       }
-
-       for (pos_type i = 0; i < par->size(); ++i) {
-               if (!i && !noparbreak) {
+       /* what about the alignment */ 
+//     } else {
+//             lyxerr << "Should this ever happen?" << endl;
+//     }
+
+       // linelen <= 0 is special and means we don't have pargraph breaks
+       if (!noparbreak) {
+               if (linelen > 0)
+                       buffer << "\n\n";
+               for (Paragraph::depth_type j = 0; j < depth; ++j)
+                       buffer << "  ";
+               currlinelen = depth * 2;
+               //--
+               // we should probably change to the paragraph language in the
+               // gettext here (if possible) so that strings are outputted in
+               // the correct language! (20012712 Jug)
+               //--    
+               switch (ltype) {
+               case 0: // Standard
+               case 4: // (Sub)Paragraph
+               case 5: // Description
+                       break;
+               case 6: // Abstract
                        if (linelen > 0)
-                               buffer << "\n\n";
-                       for (Paragraph::depth_type j = 0; j < depth; ++j)
-                               buffer << "  ";
-                       currlinelen = depth * 2;
-                       switch (ltype) {
-                       case 0: // Standard
-                       case 4: // (Sub)Paragraph
-                       case 5: // Description
-                               break;
-                       case 6: // Abstract
+                               buffer << _("Abstract") << "\n\n";
+                       else
+                               buffer << _("Abstract: ");
+                       break;
+               case 7: // Bibliography
+                       if (!ref_printed) {
                                if (linelen > 0)
-                                       buffer << "Abstract\n\n";
+                                       buffer << _("References") << "\n\n";
                                else
-                                       buffer << "Abstract: ";
-                               break;
-                       case 7: // Bibliography
-                               if (!ref_printed) {
-                                       if (linelen > 0)
-                                               buffer << "References\n\n";
-                                       else
-                                               buffer << "References: ";
-                                       ref_printed = true;
-                               }
-                               break;
-                       default:
-                               buffer << par->params().labelString() << " ";
-                               break;
+                                       buffer << _("References: ");
+                               ref_printed = true;
                        }
-                       if (ltype_depth > depth) {
-                               for (Paragraph::depth_type j = ltype_depth - 1; 
-                                    j > depth; --j)
-                                       buffer << "  ";
-                               currlinelen += (ltype_depth-depth)*2;
+                       break;
+               default:
+                       buffer << par->params().labelString() << " ";
+                       break;
+               }
+       }
+       string s = buffer.str();
+       if (s.rfind('\n') != string::npos) {
+               string dummy;
+               s = rsplit(buffer.str().c_str(), dummy, '\n');
+       }
+       currlinelen = s.length();
+       if (!currlinelen) {
+               for (Paragraph::depth_type j = 0; j < depth; ++j)
+                       buffer << "  ";
+               currlinelen = depth * 2;
+               if (ltype_depth > depth) {
+                       for (Paragraph::depth_type j = ltype_depth;
+                                j > depth; --j)
+                       {
+                               buffer << "  ";
                        }
+                       currlinelen += (ltype_depth-depth)*2;
                }
-               
+       }
+       // this is to change the linebreak to do it by word a bit more intelligent
+       // hopefully! (only in the case where we have a max linelenght!) (Jug)
+       for (pos_type i = 0; i < par->size(); ++i) {
                char c = par->getUChar(params, i);
-               if (islatex)
-                       continue;
                switch (c) {
                case Paragraph::META_INSET:
                {
                        Inset const * inset = par->getInset(i);
                        if (inset) {
-                               if (!inset->ascii(this, buffer)) {
+                               if (linelen > 0) {
+                                       buffer << word.str();
+                                       word.str("");
+                               }
+                               if (inset->ascii(this, buffer, linelen)) {
+                                       // to be sure it breaks paragraph
+                                       currlinelen += linelen;
+                               }
+#if 0
+                               else {
                                        string dummy;
                                        string const s =
                                                rsplit(buffer.str().c_str(),
                                                       dummy, '\n');
-                                       currlinelen += s.length();
-                               } else {
-                                       // to be sure it breaks paragraph
-                                       currlinelen += linelen;
+                                       currlinelen = s.length();
                                }
+#endif
                        }
                }
                break;
                
                case Paragraph::META_NEWLINE:
                        if (linelen > 0) {
-                               buffer << "\n";
+                               buffer << word.str() << "\n";
+                               word.str("");
                                for (Paragraph::depth_type j = 0; 
                                     j < depth; ++j)
                                        buffer << "  ";
-                       }
-                       currlinelen = depth * 2;
-                       if (ltype_depth > depth) {
-                               for (Paragraph::depth_type j = ltype_depth;
-                                    j > depth; --j)
-                                       buffer << "  ";
-                               currlinelen += (ltype_depth - depth) * 2;
+                               currlinelen = depth * 2;
+                               if (ltype_depth > depth) {
+                                       for (Paragraph::depth_type j = ltype_depth;
+                                                j > depth; --j)
+                                               buffer << "  ";
+                                       currlinelen += (ltype_depth - depth) * 2;
+                               }
                        }
                        break;
                        
-               case Paragraph::META_HFILL: 
-                       buffer << "\t";
+               case Paragraph::META_HFILL:
+                       buffer << word.str() << "\t";
+                       currlinelen += word.str().length() + 1;
+                       word.str("");
                        break;
 
                default:
-                       if ((linelen > 0) && (currlinelen > (linelen - 10)) &&
-                           (c == ' ') && ((i + 2) < par->size()))
-                       {
-                               buffer << "\n";
-                               for (Paragraph::depth_type j = 0; 
-                                    j < depth; ++j)
-                                       buffer << "  ";
-                               currlinelen = depth * 2;
-                               if (ltype_depth > depth) {
-                                       for (Paragraph::depth_type j = ltype_depth;
-                                           j > depth; --j)
+                       if (c == ' ') {
+                               buffer << word.str() << ' ';
+                               currlinelen += word.str().length() + 1;
+                               word.str("");
+                       } else {
+                               if (c != '\0') {
+                                       word << c;
+                               } else {
+                                       lyxerr[Debug::INFO] <<
+                                               "writeAsciiFile: NULL char in structure." << endl;
+                               }
+                               if ((linelen > 0) &&
+                                       (currlinelen+word.str().length()) > linelen)
+                               {
+                                       buffer << "\n";
+                                       for (Paragraph::depth_type j = 0; j < depth; ++j)
                                                buffer << "  ";
-                                       currlinelen += (ltype_depth-depth)*2;
+                                       currlinelen = depth * 2;
+                                       if (ltype_depth > depth) {
+                                               for (Paragraph::depth_type j = ltype_depth;
+                                                        j > depth; --j)
+                                               {
+                                                       buffer << "  ";
+                                               }
+                                               currlinelen += (ltype_depth-depth)*2;
+                                       }
                                }
-                       } else if (c != '\0') {
-                               buffer << c;
-                               ++currlinelen;
-                       } else
-                               lyxerr[Debug::INFO] << "writeAsciiFile: NULL char in structure." << endl;
+                       }
                        break;
                }
        }
+       buffer << word.str();
        return buffer.str().c_str();
 }
 
@@ -1946,14 +1995,16 @@ void Buffer::writeFileAscii(ostream & ofs, int linelen)
 {
        Paragraph * par = paragraph;
        while (par) {
-               ofs << asciiParagraph(par, linelen);
+               ofs << asciiParagraph(par, linelen, par->previous() == 0);
                par = par->next();
        }
        ofs << "\n";
 }
 
+
 bool use_babel;
 
+
 void Buffer::makeLaTeXFile(string const & fname, 
                           string const & original_path,
                           bool nice, bool only_body)
@@ -1965,7 +2016,7 @@ void Buffer::makeLaTeXFile(string const & fname,
        tex_code_break_column = lyxrc.ascii_linelen;
 
         LyXTextClass const & tclass =
-               textclasslist.TextClass(params.textclass);
+               textclasslist[params.textclass];
 
        ofstream ofs(fname.c_str());
        if (!ofs) {
@@ -1975,7 +2026,7 @@ void Buffer::makeLaTeXFile(string const & fname,
        
        // validate the buffer.
        lyxerr[Debug::LATEX] << "  Validating buffer..." << endl;
-       LaTeXFeatures features(params, tclass.numLayouts());
+       LaTeXFeatures features(params, tclass.size());
        validate(features);
        lyxerr[Debug::LATEX] << "  Buffer validation done." << endl;
        
@@ -2003,7 +2054,7 @@ void Buffer::makeLaTeXFile(string const & fname,
        // original_path is set. This is done for usual tex-file, but not
        // for nice-latex-file. (Matthias 250696)
        if (!only_body) {
-               if (!nice){
+               if (!nice) {
                        // code for usual, NOT nice-latex-file
                        ofs << "\\batchmode\n"; // changed
                        // from \nonstopmode
@@ -2102,13 +2153,13 @@ void Buffer::makeLaTeXFile(string const & fname,
                }
 
                string strOptions(options.str().c_str());
-               if (!strOptions.empty()){
+               if (!strOptions.empty()) {
                        strOptions = strip(strOptions, ',');
                        ofs << '[' << strOptions << ']';
                }
                
                ofs << '{'
-                   << textclasslist.LatexnameOfClass(params.textclass)
+                   << textclasslist[params.textclass].latexname()
                    << "}\n";
                texrow.newline();
                // end of \documentclass defs
@@ -2254,12 +2305,6 @@ void Buffer::makeLaTeXFile(string const & fname,
                        texrow.newline();
                }
 
-               if (features.isRequired("amsstyle")
-                           && !tclass.provides(LyXTextClass::amsmath)) {
-                       ofs << "\\usepackage{amsmath}\n";
-                       texrow.newline();
-               }
-               
                if (tokenPos(tclass.opt_pagestyle(),
                             '|', params.pagestyle) >= 0) {
                        if (params.pagestyle == "fancy") {
@@ -2378,7 +2423,9 @@ void Buffer::makeLaTeXFile(string const & fname,
                if (!bullets_def.empty())
                  preamble += bullets_def + "}\n\n";
 
-               for (int j = countChar(preamble, '\n'); j-- ;) {
+               int const nlines =
+                       int(lyx::count(preamble.begin(), preamble.end(), '\n'));
+               for (int j = 0; j != nlines; ++j) {
                        texrow.newline();
                }
 
@@ -2456,33 +2503,41 @@ void Buffer::makeLaTeXFile(string const & fname,
 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
 //
 void Buffer::latexParagraphs(ostream & ofs, Paragraph * par,
-                            Paragraph * endpar, TexRow & texrow) const
+                             Paragraph * endpar, TexRow & texrow) const
 {
        bool was_title = false;
        bool already_title = false;
 
        // if only_body
        while (par != endpar) {
-               LyXLayout const & layout =
-                       textclasslist.Style(params.textclass,
-                                           par->layout);
-           
-               if (layout.intitle) {
-                       if (already_title) {
-                               lyxerr <<"Error in latexParagraphs: You"
-                                       " should not mix title layouts"
-                                       " with normal ones." << endl;
-                       } else
-                               was_title = true;
-               } else if (was_title && !already_title) {
-                       ofs << "\\maketitle\n";
-                       texrow.newline();
-                       already_title = true;
-                       was_title = false;                  
-               }
-               
-               if (layout.isEnvironment()) {
-                       par = par->TeXEnvironment(this, params, ofs, texrow);
+               Inset * in = par->inInset();
+               // well we have to check if we are in an inset with unlimited
+               // lenght (all in one row) if that is true then we don't allow
+               // any special options in the paragraph and also we don't allow
+               // any environment other then "Standard" to be valid!
+               if ((in == 0) || !in->forceDefaultParagraphs(in)) {
+                       LyXLayout const & layout =
+                               textclasslist[params.textclass][par->layout()];
+                       
+                       if (layout.intitle) {
+                               if (already_title) {
+                                       lyxerr <<"Error in latexParagraphs: You"
+                                               " should not mix title layouts"
+                                               " with normal ones." << endl;
+                               } else
+                                       was_title = true;
+                       } else if (was_title && !already_title) {
+                               ofs << "\\maketitle\n";
+                               texrow.newline();
+                               already_title = true;
+                               was_title = false;                  
+                       }
+                       
+                       if (layout.isEnvironment()) {
+                               par = par->TeXEnvironment(this, params, ofs, texrow);
+                       } else {
+                               par = par->TeXOnePar(this, params, ofs, texrow, false);
+                       }
                } else {
                        par = par->TeXOnePar(this, params, ofs, texrow, false);
                }
@@ -2497,32 +2552,34 @@ void Buffer::latexParagraphs(ostream & ofs, Paragraph * par,
 
 bool Buffer::isLatex() const
 {
-       return textclasslist.TextClass(params.textclass).outputType() == LATEX;
+       return textclasslist[params.textclass].outputType() == LATEX;
 }
 
 
 bool Buffer::isLinuxDoc() const
 {
-       return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC;
+       return textclasslist[params.textclass].outputType() == LINUXDOC;
 }
 
 
 bool Buffer::isLiterate() const
 {
-       return textclasslist.TextClass(params.textclass).outputType() == LITERATE;
+       return textclasslist[params.textclass].outputType() == LITERATE;
 }
 
 
 bool Buffer::isDocBook() const
 {
-       return textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
+       return textclasslist[params.textclass].outputType() == DOCBOOK;
 }
 
 
 bool Buffer::isSGML() const
 {
-       return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC ||
-              textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
+       LyXTextClass const & tclass = textclasslist[params.textclass];
+       
+       return tclass.outputType() == LINUXDOC ||
+              tclass.outputType() == DOCBOOK;
 }
 
 
@@ -2555,15 +2612,15 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
 
        niceFile = nice; // this will be used by included files.
 
-        LyXTextClass const & tclass =
-               textclasslist.TextClass(params.textclass);
+        LyXTextClass const & tclass = textclasslist[params.textclass];
 
-       LaTeXFeatures features(params, tclass.numLayouts());
+       LaTeXFeatures features(params, tclass.size());
+       
        validate(features);
 
        texrow.reset();
 
-       string top_element = textclasslist.LatexnameOfClass(params.textclass);
+       string top_element = textclasslist[params.textclass].latexname();
 
        if (!body_only) {
                ofs << "<!doctype linuxdoc system";
@@ -2598,14 +2655,13 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
 
        while (par) {
                LyXLayout const & style =
-                       textclasslist.Style(params.textclass,
-                                           par->layout);
+                       textclasslist[params.textclass][par->layout()];
 
                // treat <toc> as a special case for compatibility with old code
                if (par->isInset(0)) {
                        Inset * inset = par->getInset(0);
                        Inset::Code lyx_code = inset->lyxCode();
-                       if (lyx_code == Inset::TOC_CODE){
+                       if (lyx_code == Inset::TOC_CODE) {
                                string const temp = "toc";
                                sgmlOpenTag(ofs, depth, temp);
 
@@ -2641,7 +2697,7 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
                                          _("Error : Wrong depth for"
                                            " LatexType Command.\n"));
 
-                       if (!environment_stack[depth].empty()){
+                       if (!environment_stack[depth].empty()) {
                                sgmlCloseTag(ofs, depth,
                                             environment_stack[depth]);
                                ofs << "</p>";
@@ -2774,7 +2830,7 @@ void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
 inline
 void reset(PAR_TAG & p1, PAR_TAG const & p2)
 {
-       p1 = static_cast<PAR_TAG>( p1 & ~p2);
+       p1 = static_cast<PAR_TAG>(p1 & ~p2);
 }
 
 } // namespace anon
@@ -2785,8 +2841,8 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
                                  Paragraph * par, 
                                  Paragraph::depth_type /*depth*/)
 {
-       LyXLayout const & style = textclasslist.Style(params.textclass,
-                                                     par->getLayout());
+       LyXLayout const & style =
+               textclasslist[params.textclass][par->layout()];
        string::size_type char_line_count = 5;     // Heuristic choice ;-) 
 
        // gets paragraph main font
@@ -2899,7 +2955,7 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
                }
 
                list < PAR_TAG > temp;
-               while (!tag_state.empty() && tag_close ) {
+               while (!tag_state.empty() && tag_close) {
                        PAR_TAG k =  tag_state.top();
                        tag_state.pop();
                        os << "</" << tag_name(k) << ">";
@@ -2998,14 +3054,14 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
        niceFile = nice; // this will be used by Insetincludes.
 
         LyXTextClass const & tclass =
-               textclasslist.TextClass(params.textclass);
+               textclasslist[params.textclass];
 
-       LaTeXFeatures features(params, tclass.numLayouts());
+       LaTeXFeatures features(params, tclass.size());
        validate(features);
    
        texrow.reset();
 
-       string top_element = textclasslist.LatexnameOfClass(params.textclass);
+       string top_element = textclasslist[params.textclass].latexname();
 
        if (!only_body) {
                ofs << "<!DOCTYPE " << top_element
@@ -3055,8 +3111,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                int desc_on = 0; // description mode
 
                LyXLayout const & style =
-                       textclasslist.Style(params.textclass,
-                                           par->layout);
+                       textclasslist[params.textclass][par->layout()];
 
                // environment tag closing
                for (; depth > par->params().depth(); --depth) {
@@ -3140,7 +3195,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                        if (par->isInset(0)) {
                                Inset * inset = par->getInset(0);
                                Inset::Code lyx_code = inset->lyxCode();
-                               if (lyx_code == Inset::LABEL_CODE){
+                               if (lyx_code == Inset::LABEL_CODE) {
                                        command_name += " id=\"";
                                        command_name += (static_cast<InsetCommand *>(inset))->getContents();
                                        command_name += "\"";
@@ -3164,7 +3219,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                        }
 
                        if (environment_stack[depth] != style.latexname()) {
-                               if(environment_stack.size() == depth + 1) {
+                               if (environment_stack.size() == depth + 1) {
                                        environment_stack.push_back("!-- --");
                                        environment_inner.push_back("!-- --");
                                }
@@ -3187,7 +3242,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                        
                        if (style.latextype == LATEX_ENVIRONMENT) {
                                if (!style.latexparam().empty()) {
-                                       if(style.latexparam() == "CDATA")
+                                       if (style.latexparam() == "CDATA")
                                                ofs << "<![CDATA[";
                                        else
                                                sgmlOpenTag(ofs, depth + command_depth,
@@ -3237,7 +3292,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                        break;
                case LATEX_ENVIRONMENT:
                        if (!style.latexparam().empty()) {
-                               if(style.latexparam() == "CDATA")
+                               if (style.latexparam() == "CDATA")
                                        ofs << "]]>";
                                else
                                        sgmlCloseTag(ofs, depth + command_depth,
@@ -3293,8 +3348,8 @@ void Buffer::simpleDocBookOnePar(ostream & os,
 {
        bool emph_flag = false;
 
-       LyXLayout const & style = textclasslist.Style(params.textclass,
-                                                     par->getLayout());
+       LyXLayout const & style =
+               textclasslist[params.textclass][par->layout()];
 
        LyXFont font_old = style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
 
@@ -3311,18 +3366,23 @@ void Buffer::simpleDocBookOnePar(ostream & os,
                        if (font.emph() == LyXFont::ON) {
                                os << "<emphasis>";
                                emph_flag = true;
-                       }else if(i) {
+                       } else if (i) {
                                os << "</emphasis>";
                                emph_flag = false;
                        }
                }
       
 
-               if ( par->isInset(i) ) {
+               if (par->isInset(i)) {
                        Inset * inset = par->getInset(i);
                        // don't print the inset in position 0 if desc_on == 3 (label)
-                       if ( i || desc_on != 3)
+                       if ( i || desc_on != 3) {
+                               if(style.latexparam() == "CDATA")
+                                       os << "]]>";
                                inset->docbook(this, os);
+                               if(style.latexparam() == "CDATA")
+                                       os << "<![CDATA[";
+                       }
                } else {
                        char c = par->getChar(i);
                        string sgml_string;
@@ -3330,7 +3390,7 @@ void Buffer::simpleDocBookOnePar(ostream & os,
 
                        if (style.pass_thru) {
                                os << c;
-                       } else if(style.free_spacing || par->isFreeSpacing() || c != ' ') {
+                       } else if (style.free_spacing || par->isFreeSpacing() || c != ' ') {
                                        os << sgml_string;
                        } else if (desc_on ==1) {
                                ++char_line_count;
@@ -3352,7 +3412,7 @@ void Buffer::simpleDocBookOnePar(ostream & os,
                // <term> not closed...
                os << "</term>";
        }
-       if(style.free_spacing) os << '\n';
+       if (style.free_spacing) os << '\n';
 }
 
 
@@ -3367,7 +3427,7 @@ int Buffer::runChktex()
 
        // get LaTeX-Filename
        string const name = getLatexName();
-       string path = OnlyPath(filename);
+       string path = filePath();
 
        string const org_path = path;
        if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
@@ -3384,7 +3444,7 @@ int Buffer::runChktex()
        makeLaTeXFile(name, org_path, false);
 
        TeXErrors terr;
-       Chktex chktex(lyxrc.chktex_command, name, filepath);
+       Chktex chktex(lyxrc.chktex_command, name, filePath());
        int res = chktex.run(terr); // run chktex
 
        if (res == -1) {
@@ -3397,7 +3457,7 @@ int Buffer::runChktex()
 
        // if we removed error insets before we ran chktex or if we inserted
        // error insets after we ran chktex, this must be run:
-       if (removedErrorInsets || res){
+       if (removedErrorInsets || res) {
                users->redraw();
                users->fitCursor();
        }
@@ -3410,12 +3470,11 @@ int Buffer::runChktex()
 void Buffer::validate(LaTeXFeatures & features) const
 {
        Paragraph * par = paragraph;
-        LyXTextClass const & tclass = 
-               textclasslist.TextClass(params.textclass);
+        LyXTextClass const & tclass = textclasslist[params.textclass];
     
         // AMS Style is at document level
         if (params.use_amsmath || tclass.provides(LyXTextClass::amsmath))
-               features.require("amsstyle");
+               features.require("amsmath");
     
        while (par) {
                // We don't use "lyxerr.debug" because of speed. (Asger)
@@ -3507,25 +3566,12 @@ Buffer::Lists const Buffer::getLists() const
        Lists l;
        Paragraph * par = paragraph;
 
-#if 1
-       std::pair<bool, textclass_type> const tmp =
-               textclasslist.NumberOfLayout(params.textclass, "Caption");
-       bool const found = tmp.first;
-       textclass_type const cap = tmp.second;
-       
-#else
-       // This is the prefered way to to this, but boost::tie can break
-       // some compilers
-       bool found;
-       textclass_type cap;
-       boost::tie(found, cap) = textclasslist
-               .NumberOfLayout(params.textclass, "Caption");
-#endif
+       LyXTextClass const & textclass = textclasslist[params.textclass];
+       bool found = textclass.hasLayout("caption");
+       string const layout("caption");
 
        while (par) {
-               char const labeltype =
-                       textclasslist.Style(params.textclass, 
-                                           par->getLayout()).labeltype;
+               char const labeltype = textclass[par->layout()].labeltype;
                
                if (labeltype >= LABEL_COUNTER_CHAPTER
                    && labeltype <= LABEL_COUNTER_CHAPTER + params.tocdepth) {
@@ -3533,7 +3579,7 @@ Buffer::Lists const Buffer::getLists() const
                        SingleList & item = l["TOC"];
                        int depth = max(0,
                                        labeltype - 
-                                       textclasslist.TextClass(params.textclass).maxcounter());
+                                       textclass.maxcounter());
                        item.push_back(TocItem(par, depth, par->asString(this, true)));
                }
                // For each paragrph, traverse its insets and look for
@@ -3557,7 +3603,7 @@ Buffer::Lists const Buffer::getLists() const
                                        // the inset...
                                        Paragraph * tmp = il->inset.paragraph();
                                        while (tmp) {
-                                               if (tmp->layout == cap) {
+                                               if (tmp->layout() == layout) {
                                                        SingleList & item = l[type];
                                                        string const str =
                                                                tostr(item.size()+1) + ". " + tmp->asString(this, false);