]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
remove unused code
[lyx.git] / src / buffer.C
index d51ac5c27a0760728518963cf0c89f1f6e116cd6..d45fb87f960450fecc75ddcd2b32b5000a282a75 100644 (file)
@@ -20,7 +20,7 @@
 
 #include "buffer.h"
 #include "bufferlist.h"
-#include "lyx_main.h"
+#include "counters.h"
 #include "LyXAction.h"
 #include "lyxrc.h"
 #include "lyxlex.h"
@@ -45,6 +45,7 @@
 #include "ParagraphParameters.h"
 #include "iterators.h"
 #include "lyxtextclasslist.h"
+#include "sgml.h"
 
 #include "mathed/formulamacro.h"
 #include "mathed/formula.h"
@@ -94,6 +95,9 @@
 #include "support/lyxmanip.h"
 #include "support/lyxalgo.h" // for lyx::count
 
+#include <boost/bind.hpp>
+#include <boost/tuple/tuple.hpp>
+
 #include <fstream>
 #include <iomanip>
 #include <map>
 #include <sys/types.h>
 #include <utime.h>
 
-
 #ifdef HAVE_LOCALE
 #include <locale>
 #endif
@@ -131,6 +134,7 @@ using std::max;
 using std::set;
 using std::stack;
 using std::list;
+using std::for_each;
 
 using lyx::pos_type;
 using lyx::textclass_type;
@@ -138,26 +142,20 @@ using lyx::textclass_type;
 // all these externs should eventually be removed.
 extern BufferList bufferlist;
 
-extern LyXAction lyxaction;
-
 namespace {
 
 const int LYX_FORMAT = 220;
 
 } // namespace anon
 
-extern int tex_code_break_column;
-
-
 Buffer::Buffer(string const & file, bool ronly)
-       : paragraph(0), niceFile(true), lyx_clean(true), bak_clean(true),
+       : niceFile(true), lyx_clean(true), bak_clean(true),
          unnamed(false), dep_clean(0), read_only(ronly),
-         filename_(file), users(0)
+         filename_(file), users(0), ctrs(new Counters)
 {
        lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
 //     filename = file;
        filepath_ = OnlyPath(file);
-//     paragraph = 0;
 //     lyx_clean = true;
 //     bak_clean = true;
 //     dep_clean = 0;
@@ -188,14 +186,7 @@ Buffer::~Buffer()
                DestroyBufferTmpDir(tmppath);
        }
 
-       Paragraph * par = paragraph;
-       Paragraph * tmppar;
-       while (par) {
-               tmppar = par->next();
-               delete par;
-               par = tmppar;
-       }
-       paragraph = 0;
+       paragraphs.clear();
 
        // Remove any previewed LaTeX snippets assocoated with this buffer.
        grfx::Previews::get().removeLoader(this);
@@ -252,7 +243,7 @@ void Buffer::setReadonly(bool flag)
        if (read_only != flag) {
                read_only = flag;
                updateTitles();
-               users->owner()->getDialogs()->updateBufferDependent(false);
+               users->owner()->getDialogs().updateBufferDependent(false);
        }
 }
 
@@ -346,7 +337,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
 
        if (!par) {
                par = new Paragraph;
-               par->layout(textclasslist[params.textclass].defaultLayout());
+               par->layout(params.getLyXTextClass().defaultLayout());
        } else {
                // We are inserting into an existing document
                users->text->breakParagraph(users);
@@ -383,7 +374,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
        if (!first_par)
                first_par = par;
 
-       paragraph = first_par;
+       paragraphs.set(first_par);
 
        if (unknown_layouts > 0) {
                string s = _("Couldn't set the layout for ");
@@ -447,7 +438,7 @@ void Buffer::insertErtContents(Paragraph * par, int & pos, bool set_inactive)
                                last = last->next();
                        // create the new paragraph after the last one
                        Paragraph * par = new Paragraph(last);
-                       par->layout(textclasslist[params.textclass].defaultLayoutName());
+                       par->layout(params.getLyXTextClass().defaultLayoutName());
                        par->setInsetOwner(last->inInset());
                        // set the contents
                        LyXFont font(LyXFont::ALL_INHERIT, params.language);
@@ -542,13 +533,13 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                lex.eatLine();
                string layoutname = lex.getString();
 
-               LyXTextClass const & tclass = textclasslist[params.textclass];
+               LyXTextClass const & tclass = params.getLyXTextClass();
 
                if (layoutname.empty()) {
                        layoutname = tclass.defaultLayoutName();
                }
 #ifndef NO_COMPABILITY
-               if (compare_no_case(layoutname, "latex") == 0) {
+               if (compare_ascii_no_case(layoutname, "latex") == 0) {
                        ert_comp.active = true;
                        ert_comp.fromlayout = true;
                        ert_comp.font = font;
@@ -569,7 +560,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 #ifdef USE_CAPTION
                // The is the compability reading of layout caption.
                // It can be removed in LyX version 1.3.0. (Lgb)
-               if (compare_no_case(layoutname, "caption") == 0) {
+               if (compare_ascii_no_case(layoutname, "caption") == 0) {
                        // We expect that the par we are now working on is
                        // really inside a InsetText inside a InsetFloat.
                        // We also know that captions can only be
@@ -626,14 +617,14 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                                first_par = par;
                        else {
                                par = new Paragraph(par);
-                               par->layout(textclasslist[params.textclass].defaultLayout());
+                               par->layout(params.getLyXTextClass().defaultLayout());
                        }
                        pos = 0;
-                       par->layout(textclasslist[params.textclass][layoutname]);
+                       par->layout(params.getLyXTextClass()[layoutname]);
                        // Test whether the layout is obsolete.
                        LyXLayout_ptr const & layout = par->layout();
                        if (!layout->obsoleted_by().empty())
-                               par->layout(textclasslist[params.textclass][layout->obsoleted_by()]);
+                               par->layout(params.getLyXTextClass()[layout->obsoleted_by()]);
                        par->params().depth(depth);
 #ifndef NO_COMPABILITY
                } else {
@@ -642,7 +633,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        par->layout(layoutname);
                        // Test whether the layout is obsolete.
                        LyXLayout_ptr const & layout =
-                               textclasslist[params.textclass][par->layout()];
+                               params.getLyXTextClass()[par->layout()];
                        if (!layout->obsoleted_by().empty())
                                par->layout(layout->obsoleted_by());
                        par->params().depth(depth);
@@ -803,34 +794,29 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        old_float << "collapsed true\n";
                } else if (tmptok == "fig") {
                        inset = new InsetFloat(params, "figure");
-                       old_float << "placement htbp\n"
-                                 << "wide false\n"
+                       old_float << "wide false\n"
                                  << "collapsed false\n";
                } else if (tmptok == "tab") {
                        inset = new InsetFloat(params, "table");
-                       old_float << "placement htbp\n"
-                                 << "wide false\n"
+                       old_float << "wide false\n"
                                  << "collapsed false\n";
                } else if (tmptok == "alg") {
                        inset = new InsetFloat(params, "algorithm");
-                       old_float << "placement htbp\n"
-                                 << "wide false\n"
+                       old_float << "wide false\n"
                                  << "collapsed false\n";
                } else if (tmptok == "wide-fig") {
                        inset = new InsetFloat(params, "figure");
                        //InsetFloat * tmp = new InsetFloat("figure");
                        //tmp->wide(true);
                        //inset = tmp;
-                       old_float << "placement htbp\n"
-                                 << "wide true\n"
+                       old_float << "wide true\n"
                                  << "collapsed false\n";
                } else if (tmptok == "wide-tab") {
                        inset = new InsetFloat(params, "table");
                        //InsetFloat * tmp = new InsetFloat("table");
                        //tmp->wide(true);
                        //inset = tmp;
-                       old_float << "placement htbp\n"
-                                 << "wide true\n"
+                       old_float << "wide true\n"
                                  << "collapsed false\n";
                }
 
@@ -902,7 +888,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                                string(_("LyX will not be able to produce output correctly.")));
                        params.textclass = 0;
                }
-               if (!textclasslist[params.textclass].load()) {
+               if (!params.getLyXTextClass().load()) {
                        // if the textclass wasn't loaded properly
                        // we need to either substitute another
                        // or stop loading the file.
@@ -910,7 +896,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        // stop loading... ideas??  ARRae980418
                        Alert::alert(_("Textclass Loading Error!"),
                                   string(_("Can't load textclass ")) +
-                                  textclasslist[params.textclass].name(),
+                                  params.getLyXTextClass().name(),
                                   _("-- substituting default"));
                        params.textclass = 0;
                }
@@ -953,9 +939,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                int tmpret = lex.findToken(string_paragraph_separation);
                if (tmpret == -1)
                        ++tmpret;
-               if (tmpret != LYX_LAYOUT_DEFAULT)
-                       params.paragraph_separation =
-                               static_cast<BufferParams::PARSEP>(tmpret);
+               params.paragraph_separation =
+                       static_cast<BufferParams::PARSEP>(tmpret);
        } else if (token == "\\defskip") {
                lex.nextToken();
                params.defskip = VSpace(lex.getString());
@@ -970,31 +955,29 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                int tmpret = lex.findToken(string_quotes_language);
                if (tmpret == -1)
                        ++tmpret;
-               if (tmpret != LYX_LAYOUT_DEFAULT) {
-                       InsetQuotes::quote_language tmpl =
-                               InsetQuotes::EnglishQ;
-                       switch (tmpret) {
-                       case 0:
-                               tmpl = InsetQuotes::EnglishQ;
-                               break;
-                       case 1:
-                               tmpl = InsetQuotes::SwedishQ;
-                               break;
-                       case 2:
-                               tmpl = InsetQuotes::GermanQ;
-                               break;
-                       case 3:
-                               tmpl = InsetQuotes::PolishQ;
-                               break;
-                       case 4:
-                               tmpl = InsetQuotes::FrenchQ;
-                               break;
-                       case 5:
-                               tmpl = InsetQuotes::DanishQ;
-                               break;
-                       }
-                       params.quotes_language = tmpl;
+               InsetQuotes::quote_language tmpl =
+                       InsetQuotes::EnglishQ;
+               switch (tmpret) {
+               case 0:
+                       tmpl = InsetQuotes::EnglishQ;
+                       break;
+               case 1:
+                       tmpl = InsetQuotes::SwedishQ;
+                       break;
+               case 2:
+                       tmpl = InsetQuotes::GermanQ;
+                       break;
+               case 3:
+                       tmpl = InsetQuotes::PolishQ;
+                       break;
+               case 4:
+                       tmpl = InsetQuotes::FrenchQ;
+                       break;
+               case 5:
+                       tmpl = InsetQuotes::DanishQ;
+                       break;
                }
+               params.quotes_language = tmpl;
        } else if (token == "\\quotes_times") {
                lex.nextToken();
                switch (lex.getInteger()) {
@@ -1034,8 +1017,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                int tmpret = lex.findToken(string_orientation);
                if (tmpret == -1)
                        ++tmpret;
-               if (tmpret != LYX_LAYOUT_DEFAULT)
-                       params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
+               params.orientation =
+                       static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
        } else if (token == "\\paperwidth") {
                lex.next();
                params.paperwidth = lex.getString();
@@ -1065,7 +1048,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                params.footskip = lex.getString();
        } else if (token == "\\paperfontsize") {
                lex.nextToken();
-               params.fontsize = strip(lex.getString());
+               params.fontsize = rtrim(lex.getString());
        } else if (token == "\\papercolumns") {
                lex.nextToken();
                params.columns = lex.getInteger();
@@ -1078,7 +1061,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                }
        } else if (token == "\\paperpagestyle") {
                lex.nextToken();
-               params.pagestyle = strip(lex.getString());
+               params.pagestyle = rtrim(lex.getString());
        } else if (token == "\\bullet") {
                lex.nextToken();
                int const index = lex.getInteger();
@@ -1133,7 +1116,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                params.tocdepth = lex.getInteger();
        } else if (token == "\\spacing") {
                lex.next();
-               string const tmp = strip(lex.getString());
+               string const tmp = rtrim(lex.getString());
                Spacing::Space tmp_space = Spacing::Default;
                float tmp_val = 0.0;
                if (tmp == "single") {
@@ -1158,7 +1141,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                }
        } else if (token == "\\paragraph_spacing") {
                lex.next();
-               string const tmp = strip(lex.getString());
+               string const tmp = rtrim(lex.getString());
                if (tmp == "single") {
                        par->params().spacing(Spacing(Spacing::Single));
                } else if (tmp == "onehalf") {
@@ -1178,11 +1161,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
        } else if (token == "\\align") {
                int tmpret = lex.findToken(string_align);
                if (tmpret == -1) ++tmpret;
-               if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
-                       int const tmpret2 = int(pow(2.0, tmpret));
-                       //lyxerr << "Tmpret2 = " << tmpret2 << endl;
-                       par->params().align(LyXAlignment(tmpret2));
-               }
+               int const tmpret2 = int(pow(2.0, tmpret));
+               par->params().align(LyXAlignment(tmpret2));
        } else if (token == "\\added_space_top") {
                lex.nextToken();
                VSpace value = VSpace(lex.getString());
@@ -1367,7 +1347,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                                if (par->params().pextraStartMinipage() &&
                                    !par->params().pextraHfill()) {
                                        Paragraph * p = new Paragraph;
-                                       p->layout(textclasslist[params.textclass].defaultLayoutName());
+                                       p->layout(params.getLyXTextClass().defaultLayoutName());
 
                                        p->previous(parBeforeMinipage);
                                        parBeforeMinipage->next(p);
@@ -1442,7 +1422,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        // a sequence of minipages
                        // in its own paragraph.
                        Paragraph * p = new Paragraph;
-                       p->layout(textclasslist[params.textclass].defaultLayoutName());
+                       p->layout(params.getLyXTextClass().defaultLayoutName());
                        p->previous(par->previous());
                        p->next(0);
                        p->params().depth(depth);
@@ -1514,7 +1494,7 @@ void Buffer::insertStringAsLines(Paragraph *& par, pos_type & pos,
        for(string::const_iterator cit = str.begin();
            cit != str.end(); ++cit) {
                if (*cit == '\n') {
-                       if (autobreakrows && (par->size() || layout->keepempty)) {
+                       if (autobreakrows && (!par->empty() || layout->keepempty)) {
                                par->breakParagraph(params, pos,
                                                    layout->isEnvironment());
                                par = par->next();
@@ -1581,7 +1561,7 @@ void Buffer::readInset(LyXLex & lex, Paragraph *& par,
 
                // This strange command allows LyX to recognize "natbib" style
                // citations: citet, citep, Citet etc.
-               if (compare_no_case(cmdName, "cite", 4) == 0) {
+               if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
                        inset = new InsetCitation(inscmd);
                } else if (cmdName == "bibitem") {
                        lex.printError("Wrong place for bibitem");
@@ -1713,10 +1693,19 @@ bool Buffer::readFile(LyXLex & lex, Paragraph * par)
                                                     "Use LyX 0.10.x to read this!"));
                                        return false;
                                } else if (file_format < 220) {
-                                       Alert::alert(_("ERROR!"),
-                                                    _("Old LyX file format found. "
-                                                      "Use LyX 1.2.x to read this!"));
-                                       return false;
+                                       string const command = "lyx2lyx "
+                                               + QuoteName(filename_);
+                                       cmd_ret const ret = RunCommand(command);
+                                       if (ret.first) {
+                                               Alert::alert(_("ERROR!"),
+                                                    _("An error occured while "
+                                                      "running the conversion script."));
+                                               return false;
+                                       }
+                                       istringstream is(ret.second);
+                                       LyXLex tmplex(0, 0);
+                                       tmplex.setStream(is);
+                                       return readFile(tmplex);
                                }
                        }
                        bool the_end = readLyXformat2(lex, par);
@@ -1805,8 +1794,8 @@ bool Buffer::save() const
                }
        }
 
-       if (writeFile(fileName(), false)) {
-               markLyxClean();
+       if (writeFile(fileName())) {
+               markClean();
                removeAutosaveFile(fileName());
        } else {
                // Saving failed, so backup is not backup
@@ -1819,46 +1808,19 @@ bool Buffer::save() const
 }
 
 
-// Returns false if unsuccesful
-bool Buffer::writeFile(string const & fname, bool flag) const
+bool Buffer::writeFile(string const & fname) const
 {
-       // if flag is false writeFile will not create any GUI
-       // warnings, only cerr.
-       // Needed for autosave in background or panic save (Matthias 120496)
-
        if (read_only && (fname == fileName())) {
-               // Here we should come with a question if we should
-               // perform the write anyway.
-               if (flag)
-                       lyxerr << _("Error! Document is read-only: ")
-                              << fname << endl;
-               else
-                       Alert::alert(_("Error! Document is read-only: "),
-                                  fname);
                return false;
        }
 
        FileInfo finfo(fname);
        if (finfo.exist() && !finfo.writable()) {
-               // Here we should come with a question if we should
-               // try to do the save anyway. (i.e. do a chmod first)
-               if (flag)
-                       lyxerr << _("Error! Cannot write file: ")
-                              << fname << endl;
-               else
-                       Alert::err_alert(_("Error! Cannot write file: "),
-                                    fname);
                return false;
        }
 
        ofstream ofs(fname.c_str());
        if (!ofs) {
-               if (flag)
-                       lyxerr << _("Error! Cannot open file: ")
-                              << fname << endl;
-               else
-                       Alert::err_alert(_("Error! Cannot open file: "),
-                                    fname);
                return false;
        }
 
@@ -1881,7 +1843,7 @@ bool Buffer::writeFile(string const & fname, bool flag) const
 
        // this will write out all the paragraphs
        // using recursive descent.
-       paragraph->writeFile(this, ofs, params, depth);
+       paragraphs.begin()->writeFile(this, ofs, params, depth);
 
        // Write marker that shows file is complete
        ofs << "\n\\the_end" << endl;
@@ -1958,22 +1920,22 @@ string const Buffer::asciiParagraph(Paragraph const * par,
        if (compare_no_case(tmp, "itemize") == 0) {
                ltype = 1;
                ltype_depth = depth + 1;
-       } else if (compare_no_case(tmp, "enumerate") == 0) {
+       } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
                ltype = 2;
                ltype_depth = depth + 1;
-       } else if (contains(lowercase(tmp), "ection")) {
+       } else if (contains(ascii_lowercase(tmp), "ection")) {
                ltype = 3;
                ltype_depth = depth + 1;
-       } else if (contains(lowercase(tmp), "aragraph")) {
+       } else if (contains(ascii_lowercase(tmp), "aragraph")) {
                ltype = 4;
                ltype_depth = depth + 1;
-       } else if (compare_no_case(tmp, "description") == 0) {
+       } else if (compare_ascii_no_case(tmp, "description") == 0) {
                ltype = 5;
                ltype_depth = depth + 1;
-       } else if (compare_no_case(tmp, "abstract") == 0) {
+       } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
                ltype = 6;
                ltype_depth = 0;
-       } else if (compare_no_case(tmp, "bibliography") == 0) {
+       } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
                ltype = 7;
                ltype_depth = 0;
        } else {
@@ -2156,10 +2118,11 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
 
 void Buffer::writeFileAscii(ostream & ofs, int linelen)
 {
-       Paragraph * par = paragraph;
-       while (par) {
-               ofs << asciiParagraph(par, linelen, par->previous() == 0);
-               par = par->next();
+       ParagraphList::iterator beg = paragraphs.begin();
+       ParagraphList::iterator end = paragraphs.end();
+       ParagraphList::iterator it = beg;
+       for (; it != end; ++it) {
+               ofs << asciiParagraph(&*it, linelen, it == beg);
        }
        ofs << "\n";
 }
@@ -2195,8 +2158,6 @@ void Buffer::makeLaTeXFile(ostream & os,
 {
        niceFile = nice; // this will be used by Insetincludes.
 
-       tex_code_break_column = lyxrc.ascii_linelen;
-
        // validate the buffer.
        lyxerr[Debug::LATEX] << "  Validating buffer..." << endl;
        LaTeXFeatures features(params);
@@ -2206,7 +2167,7 @@ void Buffer::makeLaTeXFile(ostream & os,
        texrow.reset();
        // The starting paragraph of the coming rows is the
        // first paragraph of the document. (Asger)
-       texrow.start(paragraph, 0);
+       texrow.start(&*(paragraphs.begin()), 0);
 
        if (!only_body && nice) {
                os << "%% " << lyx_docversion << " created this file.  "
@@ -2221,9 +2182,7 @@ void Buffer::makeLaTeXFile(ostream & os,
        // usual is \batchmode and has a
        // special input@path to allow the including of figures
        // with either \input or \includegraphics (what figinsets do).
-       // batchmode is not set if there is a tex_code_break_column.
-       // In this case somebody is interested in the generated LaTeX,
-       // so this is OK. input@path is set when the actual parameter
+       // input@path is set when the actual parameter
        // original_path is set. This is done for usual tex-file, but not
        // for nice-latex-file. (Matthias 250696)
        if (!only_body) {
@@ -2247,7 +2206,7 @@ void Buffer::makeLaTeXFile(ostream & os,
 
                os << "\\documentclass";
 
-               LyXTextClass const & tclass = textclasslist[params.textclass];
+               LyXTextClass const & tclass = params.getLyXTextClass();
 
                ostringstream options; // the document class options.
 
@@ -2331,7 +2290,7 @@ void Buffer::makeLaTeXFile(ostream & os,
 
                string strOptions(options.str().c_str());
                if (!strOptions.empty()) {
-                       strOptions = strip(strOptions, ',');
+                       strOptions = rtrim(strOptions, ",");
                        os << '[' << strOptions << ']';
                }
 
@@ -2635,7 +2594,7 @@ void Buffer::makeLaTeXFile(ostream & os,
                texrow.newline();
        }
 
-       latexParagraphs(os, paragraph, 0, texrow);
+       latexParagraphs(os, &*(paragraphs.begin()), 0, texrow);
 
        // add this just in case after all the paragraphs
        os << endl;
@@ -2661,12 +2620,6 @@ void Buffer::makeLaTeXFile(ostream & os,
        // Just to be sure. (Asger)
        texrow.newline();
 
-       // tex_code_break_column's value is used to decide
-       // if we are in batchmode or not (within mathed_write()
-       // in math_write.C) so we must set it to a non-zero
-       // value when we leave otherwise we save incorrect .lyx files.
-       tex_code_break_column = lyxrc.ascii_linelen;
-
        lyxerr[Debug::INFO] << "Finished making latex file." << endl;
        lyxerr[Debug::INFO] << "Row count was " << texrow.rows()-1 << "." << endl;
 
@@ -2730,31 +2683,31 @@ void Buffer::latexParagraphs(ostream & ofs, Paragraph * par,
 
 bool Buffer::isLatex() const
 {
-       return textclasslist[params.textclass].outputType() == LATEX;
+       return params.getLyXTextClass().outputType() == LATEX;
 }
 
 
 bool Buffer::isLinuxDoc() const
 {
-       return textclasslist[params.textclass].outputType() == LINUXDOC;
+       return params.getLyXTextClass().outputType() == LINUXDOC;
 }
 
 
 bool Buffer::isLiterate() const
 {
-       return textclasslist[params.textclass].outputType() == LITERATE;
+       return params.getLyXTextClass().outputType() == LITERATE;
 }
 
 
 bool Buffer::isDocBook() const
 {
-       return textclasslist[params.textclass].outputType() == DOCBOOK;
+       return params.getLyXTextClass().outputType() == DOCBOOK;
 }
 
 
 bool Buffer::isSGML() const
 {
-       LyXTextClass const & tclass = textclasslist[params.textclass];
+       LyXTextClass const & tclass = params.getLyXTextClass();
 
        return tclass.outputType() == LINUXDOC ||
               tclass.outputType() == DOCBOOK;
@@ -2810,7 +2763,7 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
 
        texrow.reset();
 
-       LyXTextClass const & tclass = textclasslist[params.textclass];
+       LyXTextClass const & tclass = params.getLyXTextClass();
 
        string top_element = tclass.latexname();
 
@@ -2843,7 +2796,7 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
            << " -->\n";
 
        Paragraph::depth_type depth = 0; // paragraph depth
-       Paragraph * par = paragraph;
+       Paragraph * par = &*(paragraphs.begin());
        string item_name;
        vector<string> environment_stack(5);
 
@@ -3191,10 +3144,10 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
                                os << c;
                        ++char_line_count;
                } else {
-                       string sgml_string;
-                       if (par->sgmlConvertChar(c, sgml_string)
-                           && !style->free_spacing && !par->isFreeSpacing())
-                       {
+                       bool ws;
+                       string str;
+                       boost::tie(ws, str) = sgml::escapeChar(c);
+                       if (ws && !style->free_spacing && !par->isFreeSpacing()) {
                                // in freespacing mode, spaces are
                                // non-breaking characters
                                if (desc_on) {// if char is ' ' then...
@@ -3208,8 +3161,8 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
                                        os << c;
                                }
                        } else {
-                               os << sgml_string;
-                               char_line_count += sgml_string.length();
+                               os << str;
+                               char_line_count += str.length();
                        }
                }
                font_old = font;
@@ -3258,7 +3211,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                return;
        }
 
-       Paragraph * par = paragraph;
+       Paragraph * par = &*(paragraphs.begin());
 
        niceFile = nice; // this will be used by Insetincludes.
 
@@ -3267,7 +3220,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
 
        texrow.reset();
 
-       LyXTextClass const & tclass = textclasslist[params.textclass];
+       LyXTextClass const & tclass = params.getLyXTextClass();
        string top_element = tclass.latexname();
 
        if (!only_body) {
@@ -3581,13 +3534,14 @@ void Buffer::simpleDocBookOnePar(ostream & os,
                        }
                } else {
                        char c = par->getChar(i);
-                       string sgml_string;
-                       par->sgmlConvertChar(c, sgml_string);
+                       bool ws;
+                       string str;
+                       boost::tie(ws, str) = sgml::escapeChar(c);
 
                        if (style->pass_thru) {
                                os << c;
                        } else if (style->free_spacing || par->isFreeSpacing() || c != ' ') {
-                                       os << sgml_string;
+                                       os << str;
                        } else if (desc_on ==1) {
                                ++char_line_count;
                                os << "\n</term><listitem><para>";
@@ -3670,25 +3624,14 @@ int Buffer::runChktex()
 
 void Buffer::validate(LaTeXFeatures & features) const
 {
-       Paragraph * par = paragraph;
-       LyXTextClass const & tclass = textclasslist[params.textclass];
+       LyXTextClass const & tclass = params.getLyXTextClass();
 
        // AMS Style is at document level
        if (params.use_amsmath || tclass.provides(LyXTextClass::amsmath))
                features.require("amsmath");
 
-       while (par) {
-               // We don't use "lyxerr.debug" because of speed. (Asger)
-               if (lyxerr.debugging(Debug::LATEX))
-                       lyxerr << "Paragraph: " <<  par << endl;
-
-               // Now just follow the list of paragraphs and run
-               // validate on each of them.
-               par->validate(features);
-
-               // and then the next paragraph
-               par = par->next();
-       }
+       for_each(paragraphs.begin(), paragraphs.end(),
+                boost::bind(&Paragraph::validate, _1, boost::ref(features)));
 
        // the bullet shapes are buffer level not paragraph level
        // so they are tested here
@@ -3741,20 +3684,20 @@ string const Buffer::getIncludeonlyList(char delim)
 }
 
 
-vector<string> const Buffer::getLabelList()
+vector<string> const Buffer::getLabelList() const
 {
        /// if this is a child document and the parent is already loaded
        /// Use the parent's list instead  [ale990407]
        if (!params.parentname.empty()
            && bufferlist.exists(params.parentname)) {
-               Buffer * tmp = bufferlist.getBuffer(params.parentname);
+               Buffer const * tmp = bufferlist.getBuffer(params.parentname);
                if (tmp)
                        return tmp->getLabelList();
        }
 
        vector<string> label_list;
-       for (inset_iterator it = inset_iterator_begin();
-            it != inset_iterator_end(); ++it) {
+       for (inset_iterator it = inset_const_iterator_begin();
+            it != inset_const_iterator_end(); ++it) {
                vector<string> const l = (*it)->getLabelList();
                label_list.insert(label_list.end(), l.begin(), l.end());
        }
@@ -3762,69 +3705,6 @@ vector<string> const Buffer::getLabelList()
 }
 
 
-Buffer::Lists const Buffer::getLists() const
-{
-       Lists l;
-       Paragraph * par = paragraph;
-
-       LyXTextClass const & textclass = textclasslist[params.textclass];
-       bool found = textclass.hasLayout("Caption");
-       string const layout("Caption");
-
-       while (par) {
-#warning bogus type (Lgb)
-               char const labeltype = par->layout()->labeltype;
-
-               if (labeltype >= LABEL_COUNTER_CHAPTER
-                   && labeltype <= LABEL_COUNTER_CHAPTER + params.tocdepth) {
-                               // insert this into the table of contents
-                       SingleList & item = l["TOC"];
-                       int depth = max(0,
-                                       labeltype -
-                                       textclass.maxcounter());
-                       item.push_back(TocItem(par, depth, par->asString(this, true)));
-               }
-               // For each paragrph, traverse its insets and look for
-               // FLOAT_CODE
-
-               if (found) {
-                       Paragraph::inset_iterator it =
-                               par->inset_iterator_begin();
-                       Paragraph::inset_iterator end =
-                               par->inset_iterator_end();
-
-                       for (; it != end; ++it) {
-                               if ((*it)->lyxCode() == Inset::FLOAT_CODE) {
-                                       InsetFloat * il =
-                                               static_cast<InsetFloat*>(*it);
-
-                                       string const type = il->type();
-
-                                       // Now find the caption in the float...
-                                       // We now tranverse the paragraphs of
-                                       // the inset...
-                                       Paragraph * tmp = il->inset.paragraph();
-                                       while (tmp) {
-                                               if (tmp->layout()->name() == layout) {
-                                                       SingleList & item = l[type];
-                                                       string const str =
-                                                               tostr(item.size()+1) + ". " + tmp->asString(this, false);
-                                                       item.push_back(TocItem(tmp, 0 , str));
-                                               }
-                                               tmp = tmp->next();
-                                       }
-                               }
-                       }
-               } else {
-                       lyxerr << "Caption not found" << endl;
-               }
-
-               par = par->next();
-       }
-       return l;
-}
-
-
 // This is also a buffer property (ale)
 vector<pair<string, string> > const Buffer::getBibkeyList() const
 {
@@ -3838,17 +3718,17 @@ vector<pair<string, string> > const Buffer::getBibkeyList() const
        }
 
        vector<StringPair> keys;
-       Paragraph * par = paragraph;
-       while (par) {
-               if (par->bibkey) {
-                       string const key = par->bibkey->getContents();
-                       string const opt = par->bibkey->getOptions();
-                       string const ref = par->asString(this, false);
+       ParagraphList::iterator pit = paragraphs.begin();
+       ParagraphList::iterator pend = paragraphs.end();
+       for (; pit != pend; ++pit) {
+               if (pit->bibkey) {
+                       string const key = pit->bibkey->getContents();
+                       string const opt = pit->bibkey->getOptions();
+                       string const ref = pit->asString(this, false);
                        string const info = opt + "TheBibliographyRef" + ref;
 
                        keys.push_back(StringPair(key, info));
                }
-               par = par->next();
        }
 
        // Might be either using bibtex or a child has bibliography
@@ -3909,8 +3789,8 @@ bool Buffer::dispatch(string const & command, bool * result)
 {
        // Split command string into command and argument
        string cmd;
-       string line = frontStrip(command);
-       string const arg = strip(frontStrip(split(line, cmd, ' ')));
+       string line = ltrim(command);
+       string const arg = trim(split(line, cmd, ' '));
 
        return dispatch(lyxaction.LookupFunc(cmd), arg, result);
 }
@@ -3938,10 +3818,8 @@ bool Buffer::dispatch(int action, string const & argument, bool * result)
 void Buffer::resizeInsets(BufferView * bv)
 {
        /// then remove all LyXText in text-insets
-       Paragraph * par = paragraph;
-       for (; par; par = par->next()) {
-           par->resizeInsetsLyXText(bv);
-       }
+       for_each(paragraphs.begin(), paragraphs.end(),
+                boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
 }
 
 
@@ -3973,11 +3851,17 @@ bool Buffer::isMultiLingual()
 }
 
 
+Counters & Buffer::counters() const
+{
+       return *ctrs.get();
+}
+
+
 Buffer::inset_iterator::inset_iterator(Paragraph * paragraph, pos_type pos)
        : par(paragraph)
 {
-       it = par->InsetIterator(pos);
-       if (it == par->inset_iterator_end()) {
+       it = par->insetlist.insetIterator(pos);
+       if (it == par->insetlist.end()) {
                par = par->next();
                setParagraph();
        }
@@ -3987,8 +3871,8 @@ Buffer::inset_iterator::inset_iterator(Paragraph * paragraph, pos_type pos)
 void Buffer::inset_iterator::setParagraph()
 {
        while (par) {
-               it = par->inset_iterator_begin();
-               if (it != par->inset_iterator_end())
+               it = par->insetlist.begin();
+               if (it != par->insetlist.end())
                        return;
                par = par->next();
        }
@@ -4014,17 +3898,19 @@ Inset * Buffer::getInsetFromID(int id_arg) const
 
 Paragraph * Buffer::getParFromID(int id) const
 {
-       if (id < 0) return 0;
-       Paragraph * par = paragraph;
-       while (par) {
-               if (par->id() == id) {
-                       return par;
+       if (id < 0)
+               return 0;
+
+       ParagraphList::iterator it = paragraphs.begin();
+       ParagraphList::iterator end = paragraphs.end();
+       for (; it != end; ++it) {
+               if (it->id() == id) {
+                       return &*it;
                }
-               Paragraph * tmp = par->getParFromID(id);
+               Paragraph * tmp = it->getParFromID(id);
                if (tmp) {
                        return tmp;
                }
-               par = par->next();
        }
        return 0;
 }
@@ -4032,7 +3918,7 @@ Paragraph * Buffer::getParFromID(int id) const
 
 ParIterator Buffer::par_iterator_begin()
 {
-       return ParIterator(paragraph);
+       return ParIterator(&*(paragraphs.begin()));
 }