]> git.lyx.org Git - features.git/blobdiff - src/buffer.C
clear()->erase() ; lots of using directives for cxx
[features.git] / src / buffer.C
index 1826584815c5e583fdfc91b1279ff6f7344ce88f..c589a1fa4c2b7a6cfa709504bbb4d23edb65bb96 100644 (file)
 
 #include <fstream>
 #include <iomanip>
-using std::ofstream;
-using std::ifstream;
-using std::fstream;
-using std::ios;
-using std::setw;
-
 
 #include <cstdlib>
 #include <unistd.h>
@@ -50,7 +44,7 @@ using std::setw;
 #include "lyxlex.h"
 #include "tex-strings.h"
 #include "layout.h"
-#include "lyx_cb.h"
+#include "bufferview_funcs.h"
 #include "minibuffer.h"
 #include "lyxfont.h"
 #include "version.h"
@@ -77,6 +71,7 @@ using std::setw;
 #include "insets/insetert.h"
 #include "insets/insetgraphics.h"
 #include "insets/insetfoot.h"
+#include "insets/insettabular.h"
 #include "support/filetools.h"
 #include "support/path.h"
 #include "LaTeX.h"
@@ -92,15 +87,14 @@ using std::setw;
 #include "gettext.h"
 #include "language.h"
 
-// Uncomment this line to enable a workaround for the weird behaviour
-// of the cursor between a displayed inset and last character
-// at the upper line. (Alejandro 20.9.96)
-// #define BREAK_BEFORE_INSET
-
-/* Lars, when changing this file sometime in the future to a list,
- * please check out the figinsets-sideeffects described at the
- * beginning of figinset.C    Matthias (04.07.1996)
- */
+using std::ostream;
+using std::ofstream;
+using std::ifstream;
+using std::fstream;
+using std::ios;
+using std::setw;
+using std::endl;
+using std::pair;
 
 
 // all these externs should eventually be removed.
@@ -114,8 +108,6 @@ static const float LYX_FORMAT = 2.16;
 
 extern int tex_code_break_column;
 
-extern void FreeUpdateTimer();
-
 
 Buffer::Buffer(string const & file, bool ronly)
 {
@@ -133,7 +125,7 @@ Buffer::Buffer(string const & file, bool ronly)
        lyxvc.buffer(this);
        if (read_only || (lyxrc.use_tempdir)) {
                tmppath = CreateBufferTmpDir();
-       } else tmppath.clear();
+       } else tmppath.erase();
 }
 
 
@@ -163,10 +155,30 @@ Buffer::~Buffer()
 }
 
 
+string Buffer::getLatexName(bool no_path) const
+{
+       return ChangeExtension(MakeLatexName(filename), 
+                              ".tex", no_path); 
+}
+
+
+void Buffer::setReadonly(bool flag)
+{
+       if (read_only != flag) {
+               read_only = flag; 
+               updateTitles();
+               updateAllVisibleBufferRelatedPopups();
+       }
+       if (read_only) {
+               WarnReadonly(filename);
+       }
+}
+
+
 bool Buffer::saveParamsAsDefaults()
 {
        string fname = AddName(AddPath(user_lyxdir, "templates/"),
-                               "defaults.lyx");
+                              "defaults.lyx");
        Buffer defaults = Buffer(fname);
        
        // Use the current buffer's parameters as default
@@ -223,7 +235,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
        bool the_end_read = false;
 
        LyXParagraph * return_par = 0;
-       LyXFont font(LyXFont::ALL_INHERIT,params.language_info);
+       LyXFont font(LyXFont::ALL_INHERIT, params.language_info);
        if (format < 2.16 && params.language == "hebrew")
                font.setLanguage(default_language);
 
@@ -278,27 +290,25 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
 }
 
 
-bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
-                                       LyXParagraph *& return_par,
-                                       const string & token, int & pos,
-                                       char & depth, LyXFont & font,
-                                       LyXParagraph::footnote_flag & footnoteflag,
-                                       LyXParagraph::footnote_kind & footnotekind)
+bool
+Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
+                                  LyXParagraph *& return_par,
+                                  string const & token, int & pos,
+                                  char & depth, LyXFont & font,
+                                  LyXParagraph::footnote_flag & footnoteflag,
+                                  LyXParagraph::footnote_kind & footnotekind)
 {
-       Inset * inset = 0;
-       int tmpret, tmpret2;
-       string tmptok;
        bool the_end_read = false;
-       
+
        if (token[0] != '\\') {
-               int n = token.length();
-               for (int i = 0; i < n; ++i) {
-                       par->InsertChar(pos, token[i]);
+               for (string::const_iterator cit = token.begin();
+                    cit != token.end(); ++cit) {
+                       par->InsertChar(pos, (*cit));
                        par->SetFont(pos, font);
                        ++pos;
                }
        } else if (token == "\\i") {
-               inset = new InsetLatexAccent;
+               Inset * inset = new InsetLatexAccent;
                inset->Read(lex);
                par->InsertChar(pos, LyXParagraph::META_INSET); 
                par->InsertInset(pos, inset);
@@ -320,7 +330,7 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                if (pp.first) {
                        par->layout = pp.second;
                } else { // layout not found
-                               // use default layout "Standard" (0)
+                       // use default layout "Standard" (0)
                        par->layout = 0;
                }
                // Test whether the layout is obsolete.
@@ -334,7 +344,7 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                par->footnoteflag = footnoteflag;
                par->footnotekind = footnotekind;
                par->depth = depth;
-               font = LyXFont(LyXFont::ALL_INHERIT,params.language_info);
+               font = LyXFont(LyXFont::ALL_INHERIT, params.language_info);
                if (format < 2.16 && params.language == "hebrew")
                        font.setLanguage(default_language);
        } else if (token == "\\end_float") {
@@ -349,11 +359,11 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                pos = 0;
                lex.EatLine();
                par->layout = LYX_DUMMY_LAYOUT;
-               font = LyXFont(LyXFont::ALL_INHERIT,params.language_info);
+               font = LyXFont(LyXFont::ALL_INHERIT, params.language_info);
                if (format < 2.16 && params.language == "hebrew")
                        font.setLanguage(default_language);
        } else if (token == "\\begin_float") {
-               tmpret = lex.FindToken(string_footnotekinds);
+               int tmpret = lex.FindToken(string_footnotekinds);
                if (tmpret == -1) ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) 
                        footnotekind = static_cast<LyXParagraph::footnote_kind>(tmpret); // bad
@@ -427,7 +437,7 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
        } else if (token == "\\start_of_appendix") {
                par->start_of_appendix = true;
        } else if (token == "\\paragraph_separation") {
-               tmpret = lex.FindToken(string_paragraph_separation);
+               int tmpret = lex.FindToken(string_paragraph_separation);
                if (tmpret == -1) ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) 
                        params.paragraph_separation =
@@ -435,21 +445,13 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
        } else if (token == "\\defskip") {
                lex.nextToken();
                params.defskip = VSpace(lex.GetString());
-#if 0
-       } else if (token == "\\no_isolatin1") { // obsolete
-               lex.nextToken();
-       } else if (token == "\\no_babel") { // obsolete
-               lex.nextToken();
-       } else if (token == "\\no_epsfig") { // obsolete
-               lex.nextToken();
-#endif
        } 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);
        } else if (token == "\\quotes_language") {
-               tmpret = lex.FindToken(string_quotes_language);
+               int tmpret = lex.FindToken(string_quotes_language);
                if (tmpret == -1) ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) {
                        InsetQuotes::quote_language tmpl = 
@@ -487,13 +489,13 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                        break;
                }
        } else if (token == "\\papersize") {
-               tmpret = lex.FindToken(string_papersize);
+               int tmpret = lex.FindToken(string_papersize);
                if (tmpret == -1)
                        ++tmpret;
                else
                        params.papersize2 = tmpret;
        } else if (token == "\\paperpackage") {
-               tmpret = lex.FindToken(string_paperpackages);
+               int tmpret = lex.FindToken(string_paperpackages);
                if (tmpret == -1) {
                        ++tmpret;
                        params.paperpackage = BufferParams::PACKAGE_NONE;
@@ -506,7 +508,7 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                lex.nextToken();
                params.use_amsmath = lex.GetInteger();
        } else if (token == "\\paperorientation") {
-               tmpret = lex.FindToken(string_orientation);
+               int tmpret = lex.FindToken(string_orientation);
                if (tmpret == -1) ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) 
                        params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
@@ -602,34 +604,63 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
        } else if (token == "\\tocdepth") {
                lex.nextToken();
                params.tocdepth = lex.GetInteger();
+#if 0
        } else if (token == "\\baselinestretch") { // obsolete
                lex.nextToken(); // should not be used directly
                // anymore.
                // Will probably keep a kind of support just for
                // compability.
                params.spacing.set(Spacing::Other, lex.GetFloat());
+#endif
        } else if (token == "\\spacing") {
+               lex.next();
+               string tmp = strip(lex.GetString());
+               Spacing::Space tmp_space = Spacing::Default;
+               float tmp_val = 0.0;
+               if (tmp == "single") {
+                       tmp_space = Spacing::Single;
+               } else if (tmp == "onehalf") {
+                       tmp_space = Spacing::Onehalf;
+               } else if (tmp == "double") {
+                       tmp_space = Spacing::Double;
+               } else if (tmp == "other") {
+                       lex.next();
+                       tmp_space = Spacing::Other;
+                       tmp_val = lex.GetFloat();
+               } else {
+                       lex.printError("Unknown spacing token: '$$Token'");
+               }
+               // Small hack so that files written with klyx will be
+               // parsed correctly.
+               if (return_par) {
+                       par->spacing.set(tmp_space, tmp_val);
+               } else {
+                       params.spacing.set(tmp_space, tmp_val);
+               }
+       } else if (token == "\\paragraph_spacing") {
                lex.next();
                string tmp = strip(lex.GetString());
                if (tmp == "single") {
-                       params.spacing.set(Spacing::Single);
+                       par->spacing.set(Spacing::Single);
                } else if (tmp == "onehalf") {
-                       params.spacing.set(Spacing::Onehalf);
+                       par->spacing.set(Spacing::Onehalf);
                } else if (tmp == "double") {
-                       params.spacing.set(Spacing::Double);
+                       par->spacing.set(Spacing::Double);
                } else if (tmp == "other") {
                        lex.next();
-                       params.spacing.set(Spacing::Other,
-                                          lex.GetFloat());
+                       par->spacing.set(Spacing::Other,
+                                        lex.GetFloat());
                } else {
                        lex.printError("Unknown spacing token: '$$Token'");
                }
        } else if (token == "\\float_placement") {
                lex.nextToken();
                params.float_placement = lex.GetString();
+#if 0
        } else if (token == "\\cursor") { // obsolete
                // this is obsolete, so we just skip it.
                lex.nextToken();
+#endif
        } else if (token == "\\family") { 
                lex.next();
                font.setLyXFamily(lex.GetString());
@@ -665,20 +696,6 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                        font.setLanguage(params.language_info);
                        lex.printError("Unknown language `$$Token'");
                }
-       } else if (token == "\\direction") { // obsolete
-               if (format >= 2.16)
-                       lex.printError("Command \\direction is obsolete");
-               lex.next();
-               string tok = lex.GetString();
-               if (tok == "rtl")
-                       font.setLanguage(&languages["hebrew"]);
-               else if (tok == "default")
-                       if (params.language == "hebrew")
-                               font.setLanguage(default_language);
-                       else
-                               font.setLanguage(params.language_info);
-               else
-                       lex.printError("Unknown direction `$$Token'");
        } else if (token == "\\emph") {
                lex.next();
                font.setEmph(font.setLyXMisc(lex.GetString()));
@@ -702,20 +719,20 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                lex.next();
                font.setLyXColor(lex.GetString());
        } else if (token == "\\align") {
-               tmpret = lex.FindToken(string_align);
+               int tmpret = lex.FindToken(string_align);
                if (tmpret == -1) ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
-                       tmpret2 = 1;
+                       int tmpret2 = 1;
                        for (; tmpret > 0; --tmpret)
                                tmpret2 = tmpret2 * 2;
                        par->align = LyXAlignment(tmpret2);
                }
        } else if (token == "\\added_space_top") {
                lex.nextToken();
-               par->added_space_top = lex.GetString();
+               par->added_space_top = VSpace(lex.GetString());
        } else if (token == "\\added_space_bottom") {
                lex.nextToken();
-               par->added_space_bottom = lex.GetString();
+               par->added_space_bottom = VSpace(lex.GetString());
        } else if (token == "\\pextra_type") {
                lex.nextToken();
                par->pextra_type = lex.GetInteger();
@@ -737,88 +754,98 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
        } else if (token == "\\labelwidthstring") {
                lex.EatLine();
                par->labelwidthstring = lex.GetString();
-               /* do not delete this token, it is still needed! */ 
+               // do not delete this token, it is still needed!
        } else if (token == "\\end_inset") {
-               /* simple ignore this. The insets do not have
-                *  to read this */
-               // but insets should read it, it is a part of
-               //the inset isn't it? Lgb.
+               // Simply ignore this. The insets do not have
+               // to read this.
+               // But insets should read it, it is a part of
+               // the inset isn't it? Lgb.
        } else if (token == "\\begin_inset") {
+               // Should be moved out into its own function/method. (Lgb)
                lex.next();
-               tmptok = lex.GetString();
-               /* test the different insets */ 
+               string tmptok = lex.GetString();
+               // test the different insets
                if (tmptok == "Quotes") {
-                       inset = new InsetQuotes;
+                       Inset * inset = new InsetQuotes;
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
+#if 0
                } else if (tmptok == "\\i") {
-                       inset = new InsetLatexAccent;
+                       Inset * inset = new InsetLatexAccent;
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
+#endif
                } else if (tmptok == "FormulaMacro") {
-                       inset = new InsetFormulaMacro;
+                       Inset * inset = new InsetFormulaMacro;
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
                } else if (tmptok == "Formula") {
-                       inset = new InsetFormula;
+                       Inset * inset = new InsetFormula;
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
                } else if (tmptok == "Figure") {
-                       inset = new InsetFig(100, 100, this);
+                       Inset * inset = new InsetFig(100, 100, this);
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
                } else if (tmptok == "Info") {
-                       inset = new InsetInfo;
+                       Inset * inset = new InsetInfo;
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
                } else if (tmptok == "Include") {
-                       inset = new InsetInclude(string(), this);
+                       Inset * inset = new InsetInclude(string(), this);
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
                } else if (tmptok == "ERT") {
-                       inset = new InsetERT(this);
+                       Inset * inset = new InsetERT(this);
+                       inset->Read(lex);
+                       par->InsertChar(pos, LyXParagraph::META_INSET);
+                       par->InsertInset(pos, inset);
+                       par->SetFont(pos, font);
+                       ++pos;
+               } else if (tmptok == "Tabular") {
+                       Inset * inset = new InsetTabular(this);
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
                } else if (tmptok == "Text") {
-                       inset = new InsetText(this);
+                       Inset * inset = new InsetText(this);
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
                } else if (tmptok == "Foot") {
-                       inset = new InsetFoot(this);
+                       Inset * inset = new InsetFoot(this);
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
                } else if (tmptok == "GRAPHICS") {
-                       inset = new InsetGraphics;
+                       Inset * inset = new InsetGraphics;
                                //inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
@@ -826,6 +853,7 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                } else if (tmptok == "LatexCommand") {
                        InsetCommand inscmd;
                        inscmd.Read(lex);
+                       Inset * inset = 0;
                        if (inscmd.getCmdName() == "cite") {
                                inset = new InsetCitation(inscmd.getContents(), inscmd.getOptions());
                        } else if (inscmd.getCmdName() == "bibitem") {
@@ -847,7 +875,8 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                                if (!inscmd.getOptions().empty() || !inscmd.getContents().empty()) {
                                        inset = new InsetRef(inscmd, this);
                                }
-#warning Verify that this else clause is still needed. (Lgb)
+                               // CHECK if this else clause
+                               //is still needed. (Lgb)
 #if 0
                                // This condition comes from a
                                // temporary solution to the latexdel
@@ -924,28 +953,33 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                                ++pos;
                        }
                }
+#if 0
        } else if (token == "\\InsetQuotes") {
-               inset = new InsetQuotes;
+               lyxerr << "InsetQuotes" << endl;
+               Inset * inset = new InsetQuotes;
                inset->Read(lex);
                par->InsertChar(pos, LyXParagraph::META_INSET); 
                par->InsertInset(pos, inset);
                par->SetFont(pos, font);
                ++pos;
+#endif
+#if 0
        } else if (token == "\\InsetFormula") {
-               inset = new InsetFormula;
+               lyxerr << "InsetFormula" << endl;
+               Inset * inset = new InsetFormula;
                inset->Read(lex);
                par->InsertChar(pos, LyXParagraph::META_INSET); 
                par->InsertInset(pos, inset);
                par->SetFont(pos, font);
                ++pos;
+#endif
        } else if (token == "\\SpecialChar") {
                LyXLayout const & layout =
                        textclasslist.Style(params.textclass, 
                                            par->GetLayout());
 
                // Insets don't make sense in a free-spacing context! ---Kayvan
-               if (layout.free_spacing)
-               {
+               if (layout.free_spacing) {
                        if (lex.IsOK()) {
                                string next_token;
                                lex.next();
@@ -960,36 +994,30 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                                        lex.printError("Token `$$Token' "
                                                       "is in free space "
                                                       "paragraph layout!");
-                                       pos--;
+                                       --pos;
                                }
                        }
-               }
-               else
-               {
-                       inset = new InsetSpecialChar;
+               } else {
+                       Inset * inset = new InsetSpecialChar;
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET); 
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                }
                ++pos;
+#if 0
        } else if (token == "\\Figure") {
-               inset = new InsetFig(100, 100, this);
+               lyxerr << "Figure" << endl;
+               Inset * inset = new InsetFig(100, 100, this);
                inset->Read(lex);
                par->InsertChar(pos, LyXParagraph::META_INSET); 
                par->InsertInset(pos, inset);
                par->SetFont(pos, font);
                ++pos;
-       } else if (token == "\\newline") { // soon obsolete
-#if 1
+#endif
+       } else if (token == "\\newline") {
                par->InsertChar(pos, LyXParagraph::META_NEWLINE);
                par->SetFont(pos, font);
-#else
-               inset = new InsetSpecialChar(InsetSpecialChar::NEWLINE);
-               par->InsertChar(pos, LyXParagraph::META_INSET);
-               par->InsertInset(pos, inset);
-               par->SetFont(pos, font);
-#endif
                ++pos;
        } else if (token == "\\LyXTable") {
                par->table = new LyXTable(lex);
@@ -998,7 +1026,9 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                par->SetFont(pos, font);
                ++pos;
        } else if (token == "\\protected_separator") { // obsolete
-#if 1
+               // This is a backward compability thingie. (Lgb)
+               // Remove it later some time...introduced with fileformat
+               // 2.16. (Lgb)
                LyXLayout const & layout =
                        textclasslist.Style(params.textclass, 
                                            par->GetLayout());
@@ -1007,15 +1037,11 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                        par->InsertChar(pos, ' ');
                        par->SetFont(pos, font);
                } else {
-                       inset = new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
+                       Inset * inset = new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
                        par->InsertChar(pos, LyXParagraph::META_INSET);
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                }
-#else
-               par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
-               par->SetFont(pos, font);
-#endif
                ++pos;
        } else if (token == "\\bibitem") {  // ale970302
                if (!par->bibkey)
@@ -1031,9 +1057,9 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                // This should be insurance for the future: (Asger)
                lex.printError("Unknown token `$$Token'. "
                               "Inserting as text.");
-               int n = token.length();
-               for (int i = 0; i < n; ++i) {
-                       par->InsertChar(pos, token[i]);
+               for(string::const_iterator cit = token.begin();
+                   cit != token.end(); ++cit) {
+                       par->InsertChar(pos, (*cit));
                        par->SetFont(pos, font);
                        ++pos;
                }
@@ -1093,14 +1119,19 @@ bool Buffer::readFile(LyXLex & lex, LyXParagraph * par)
 
 
 // Should probably be moved to somewhere else: BufferView? LyXView?
-bool Buffer::save(bool makeBackup) const
+bool Buffer::save() const
 {
        // We don't need autosaves in the immediate future. (Asger)
        resetAutosaveTimers();
 
        // make a backup
-       if (makeBackup) {
-               string s = fileName() + '~';
+       string s;
+       if (lyxrc.make_backup) {
+               s = fileName() + '~';
+               if (!lyxrc.backupdir_path.empty())
+                       s = AddName(lyxrc.backupdir_path,
+                                   subst(CleanupPath(s),'/','!'));
+
                // Rename is the wrong way of making a backup,
                // this is the correct way.
                /* truss cp fil fil2:
@@ -1128,10 +1159,10 @@ bool Buffer::save(bool makeBackup) const
                FileInfo finfo(fileName());
                if (finfo.exist()) {
                        mode_t fmode = finfo.getMode();
-                       struct utimbuf * times = new struct utimbuf;
+                       struct utimbuf times = {
+                               finfo.getAccessTime(),
+                               finfo.getModificationTime() };
 
-                       times->actime = finfo.getAccessTime();
-                       times->modtime = finfo.getModificationTime();
                        ifstream ifs(fileName().c_str());
                        ofstream ofs(s.c_str(), ios::out|ios::trunc);
                        if (ifs && ofs) {
@@ -1140,14 +1171,13 @@ bool Buffer::save(bool makeBackup) const
                                ofs.close();
                                ::chmod(s.c_str(), fmode);
                                
-                               if (::utime(s.c_str(), times)) {
+                               if (::utime(s.c_str(), &times)) {
                                        lyxerr << "utime error." << endl;
                                }
                        } else {
                                lyxerr << "LyX was not able to make "
                                        "backupcopy. Beware." << endl;
                        }
-                       delete times;
                }
        }
        
@@ -1168,8 +1198,7 @@ bool Buffer::save(bool makeBackup) const
                }
        } else {
                // Saving failed, so backup is not backup
-               if (makeBackup) {
-                       string s = fileName() + '~';
+               if (lyxrc.make_backup) {
                        ::rename(s.c_str(), fileName().c_str());
                }
                return false;
@@ -1341,18 +1370,17 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
                        /* noindent ? */ 
         
                        /* what about the alignment */ 
-               }
-               else {
+               } else {
                        /* dummy layout, that means a footnote ended */ 
                        footnoteflag = LyXParagraph::NO_FOOTNOTE;
                        ofs << ") ";
                        noparbreak = 1;
                }
       
-               LyXLayout const & layout =
-                       textclasslist.Style(params.textclass, 
-                                           par->GetLayout());
-               bool free_spc = layout.free_spacing;
+               //LyXLayout const & layout =
+               //      textclasslist.Style(params.textclass, 
+               //                          par->GetLayout()); // unused
+               //bool free_spc = layout.free_spacing; //unused
 
                /* It might be a table */ 
                if (par->table){
@@ -1374,29 +1402,18 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
                                c = par->GetChar(i);
                                if (c == LyXParagraph::META_INSET) {
                                        if ((inset = par->GetInset(i))) {
-#if 1
 #ifdef HAVE_SSTREAM
-                                               ostringstream ost;
-                                               inset->Latex(ost, -1, free_spc);
+                                               std::ostringstream ost;
+                                               inset->Ascii(ost);
                                                h += ost.str().length();
 #else
                                                ostrstream ost;
-                                               inset->Latex(ost, -1, free_spc);
+                                               inset->Ascii(ost);
                                                ost << '\0';
                                                char * tmp = ost.str();
                                                string tstr(tmp);
                                                h += tstr.length();
                                                delete [] tmp;
-#endif
-#else
-                                               ofstream fs(fname1.c_str());
-                                               if (!fs) {
-                                                       WriteFSAlert(_("Error: Cannot open temporary file:"), fname1);
-                                                       return;
-                                               }
-                                               inset->Latex(fs, -1, free_spc);
-                                               h += fs.tellp() - 1;
-                                               ::remove(fname1.c_str());
 #endif
                                        }
                                } else if (c == LyXParagraph::META_NEWLINE) {
@@ -1411,7 +1428,7 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
                                clen[j] = h;
                }
       
-               font1 = LyXFont(LyXFont::ALL_INHERIT,params.language_info);
+               font1 = LyXFont(LyXFont::ALL_INHERIT, params.language_info);
                 actcell = 0;
                for (i = 0, actpos = 1; i < par->size(); ++i, ++actpos) {
                        if (!i && !footnoteflag && !noparbreak){
@@ -1468,8 +1485,7 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
                                        islatex = 0;
                                else
                                        islatex = 1;
-                       }
-                       else {
+                       } else {
                                islatex = 0;
                        }
                        c = par->GetChar(i);
@@ -1479,7 +1495,7 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
                        case LyXParagraph::META_INSET:
                                if ((inset = par->GetInset(i))) {
                                        fpos = ofs.tellp();
-                                       inset->Latex(ofs, -1, free_spc);
+                                       inset->Ascii(ofs);
                                        currlinelen += (ofs.tellp() - fpos);
                                        actpos += (ofs.tellp() - fpos) - 1;
                                }
@@ -1540,11 +1556,6 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
                        case LyXParagraph::META_HFILL: 
                                ofs << "\t";
                                break;
-#if 0
-                       case LyXParagraph::META_PROTECTED_SEPARATOR:
-                               ofs << " ";
-                               break;
-#endif
                        case '\\':
                                ofs << "\\";
                                break;
@@ -1617,7 +1628,7 @@ void Buffer::makeLaTeXFile(string const & fname,
        
        // validate the buffer.
        lyxerr[Debug::LATEX] << "  Validating buffer..." << endl;
-       LaTeXFeatures features(tclass.numLayouts());
+       LaTeXFeatures features(params, tclass.numLayouts());
        validate(features);
        lyxerr[Debug::LATEX] << "  Buffer validation done." << endl;
        
@@ -1732,7 +1743,7 @@ void Buffer::makeLaTeXFile(string const & fname,
                                options += (*cit)->lang + ",";
                        }
                        if (default_language != params.language_info)
-                               options += default_language ->lang + ',';
+                               options += default_language->lang + ',';
                        options += params.language_info->lang + ',';
                }
 
@@ -1769,7 +1780,7 @@ void Buffer::makeLaTeXFile(string const & fname,
                        texrow.newline();
                }
                
-               /* at the very beginning the text parameters */
+               // At the very beginning the text parameters.
                if (params.paperpackage != BufferParams::PACKAGE_NONE) {
                        switch (params.paperpackage) {
                        case BufferParams::PACKAGE_A4:
@@ -1937,13 +1948,13 @@ void Buffer::makeLaTeXFile(string const & fname,
                string preamble, tmppreamble;
 
                // The optional packages;
-               preamble = features.getPackages(params);
+               preamble = features.getPackages();
 
                // this might be useful...
                preamble += "\n\\makeatletter\n\n";
 
                // Some macros LyX will need
-               tmppreamble = features.getMacros(params);
+               tmppreamble = features.getMacros();
 
                if (!tmppreamble.empty()) {
                        preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
@@ -1952,7 +1963,7 @@ void Buffer::makeLaTeXFile(string const & fname,
                }
 
                // the text class specific preamble 
-               tmppreamble = features.getTClassPreamble(params);
+               tmppreamble = features.getTClassPreamble();
                if (!tmppreamble.empty()) {
                        preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
                                "Textclass specific LaTeX commands.\n"
@@ -2022,20 +2033,64 @@ void Buffer::makeLaTeXFile(string const & fname,
                texrow.newline();
        }
        
+       latexParagraphs(ofs, paragraph, 0, texrow);
+
+       // add this just in case after all the paragraphs
+       ofs << endl;
+       texrow.newline();
+
+       if (!lyxrc.language_auto_end && params.language != "default") {
+               ofs << subst(lyxrc.language_command_end, "$$lang",
+                            params.language)
+                   << endl;
+               texrow.newline();
+       }
+
+       if (!only_body) {
+               ofs << "\\end{document}\n";
+               texrow.newline();
+       
+               lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
+       } else {
+               lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
+                                    << endl;
+       }
+
+       // 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;
+
+       ofs.close();
+       if (ofs.fail()) {
+               lyxerr << "File was not closed properly." << endl;
+       }
+       
+       lyxerr.debug() << "Finished making latex file." << endl;
+}
+
+//
+// LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
+//
+void Buffer::latexParagraphs(ostream & ofs, LyXParagraph *par,
+                            LyXParagraph *endpar, TexRow & texrow)
+{
        bool was_title = false;
        bool already_title = false;
 #ifdef HAVE_SSTREAM
-       ostringstream ftnote;
+       std::ostringstream ftnote;
 #else
        char * tmpholder = 0;
 #endif
        TexRow ft_texrow;
        int ftcount = 0;
 
-       LyXParagraph * par = paragraph;
-
        // if only_body
-       while (par) {
+       while (par != endpar) {
 #ifndef HAVE_SSTREAM
                ostrstream ftnote;
                if (tmpholder) {
@@ -2045,7 +2100,7 @@ void Buffer::makeLaTeXFile(string const & fname,
                }
 #endif
                if (par->IsDummy())
-                       lyxerr[Debug::LATEX] << "Error in MakeLateXFile."
+                       lyxerr[Debug::LATEX] << "Error in latexParagraphs."
                                             << endl;
                LyXLayout const & layout =
                        textclasslist.Style(params.textclass,
@@ -2053,7 +2108,7 @@ void Buffer::makeLaTeXFile(string const & fname,
            
                if (layout.intitle) {
                        if (already_title) {
-                               lyxerr <<"Error in MakeLatexFile: You"
+                               lyxerr <<"Error in latexParagraphs: You"
                                        " should not mix title layouts"
                                        " with normal ones." << endl;
                        } else
@@ -2111,42 +2166,8 @@ void Buffer::makeLaTeXFile(string const & fname,
                ofs << "\\maketitle\n";
                texrow.newline();
        }
-
-       if (!lyxrc.language_auto_end && params.language != "default") {
-               ofs << subst(lyxrc.language_command_end, "$$lang",
-                            params.language)
-                   << endl;
-               texrow.newline();
-       }
-
-       if (!only_body) {
-               ofs << "\\end{document}\n";
-               texrow.newline();
-       
-               lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
-       } else {
-               lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
-                                    << endl;
-       }
-
-       // 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;
-
-       ofs.close();
-       if (ofs.fail()) {
-               lyxerr << "File was not closed properly." << endl;
-       }
-       
-       lyxerr.debug() << "Finished making latex file." << endl;
 }
 
-
 bool Buffer::isLatex() const
 {
        return textclasslist.TextClass(params.textclass).outputType() == LATEX;
@@ -2200,7 +2221,7 @@ void Buffer::makeLinuxDocFile(string const & fname, int column)
        string environment_stack[10];
         string item_name;
 
-       int depth = 0;              /* paragraph depth */
+       int depth = 0; // paragraph depth
 
        ofstream ofs(fname.c_str());
 
@@ -2234,13 +2255,13 @@ void Buffer::makeLinuxDocFile(string const & fname, int column)
        }
 
        while (par) {
-               int desc_on = 0;            /* description mode*/
+               int desc_on = 0; // description mode
                LyXLayout const & style =
                        textclasslist.Style(users->buffer()->params.textclass,
                                            par->layout);
                par->AutoDeleteInsets();
 
-               /* treat <toc> as a special case for compatibility with old code */
+               // treat <toc> as a special case for compatibility with old code
                if (par->GetChar(0) == LyXParagraph::META_INSET) {
                        Inset * inset = par->GetInset(0);
                        char  lyx_code = inset->LyxCode();
@@ -2257,7 +2278,7 @@ void Buffer::makeLinuxDocFile(string const & fname, int column)
                // environment tag closing
                for( ; depth > par->depth; --depth) {
                        sgmlCloseTag(ofs, depth, environment_stack[depth]);
-                       environment_stack[depth].clear();
+                       environment_stack[depth].erase();
                }
 
                // write opening SGML tags
@@ -2266,7 +2287,7 @@ void Buffer::makeLinuxDocFile(string const & fname, int column)
                        if(depth == par->depth 
                           && !environment_stack[depth].empty()) {
                                sgmlCloseTag(ofs, depth, environment_stack[depth]);
-                               environment_stack[depth].clear();
+                               environment_stack[depth].erase();
                                if(depth) 
                                        --depth;
                                else
@@ -2277,7 +2298,9 @@ void Buffer::makeLinuxDocFile(string const & fname, int column)
 
                case LATEX_COMMAND:
                        if (depth!= 0)
-                               LinuxDocError(par, 0, _("Error : Wrong depth for LatexType Command.\n"));
+                               LinuxDocError(par, 0,
+                                             _("Error : Wrong depth for"
+                                               " LatexType Command.\n"));
 
                        if (!environment_stack[depth].empty()){
                                sgmlCloseTag(ofs, depth,
@@ -2285,7 +2308,7 @@ void Buffer::makeLinuxDocFile(string const & fname, int column)
                                ofs << "</p>";
                        }
 
-                       environment_stack[depth].clear();
+                       environment_stack[depth].erase();
                        sgmlOpenTag(ofs, depth, style.latexname());
                        break;
 
@@ -2297,11 +2320,11 @@ void Buffer::makeLinuxDocFile(string const & fname, int column)
 
                                sgmlCloseTag(ofs, depth,
                                             environment_stack[depth]);
-                               environment_stack[depth].clear();
+                               environment_stack[depth].erase();
                        }
                        if (depth < par->depth) {
                               depth = par->depth;
-                              environment_stack[depth].clear();
+                              environment_stack[depth].erase();
                        }
                        if (environment_stack[depth] != style.latexname()) {
                                if(depth == 0) {
@@ -2309,7 +2332,8 @@ void Buffer::makeLinuxDocFile(string const & fname, int column)
                                        sgmlOpenTag(ofs, depth, temp);
                                }
                                environment_stack[depth] = style.latexname();
-                               sgmlOpenTag(ofs, depth, environment_stack[depth]);
+                               sgmlOpenTag(ofs, depth,
+                                           environment_stack[depth]);
                        }
                        if(style.latextype == LATEX_ENVIRONMENT) break;
 
@@ -2336,7 +2360,7 @@ void Buffer::makeLinuxDocFile(string const & fname, int column)
                while(par && par->IsDummy());
 
                ofs << "\n";
-               /* write closing SGML tags */
+               // write closing SGML tags
                switch(style.latextype) {
                case LATEX_COMMAND:
                case LATEX_ENVIRONMENT:
@@ -2348,7 +2372,7 @@ void Buffer::makeLinuxDocFile(string const & fname, int column)
                }
        }
    
-       /* Close open tags */
+       // Close open tags
        for(; depth > 0; --depth)
                sgmlCloseTag(ofs, depth, environment_stack[depth]);
 
@@ -2424,20 +2448,20 @@ void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
                        if(inner_span) {
                                if(!tmp_par.empty()) {
                                        os << tmp_par;
-                                       tmp_par.clear();
+                                       tmp_par.erase();
                                        sgmlCloseTag(os, depth + 1, inner_tag);
                                        sgmlOpenTag(os, depth + 1, inner_tag);
                                }
-                       }
-                       else
+                       } else {
                                os << "\n";
+                       }
                } else {
                        os << tmp_par;
                        if(!inner_tag.empty()) sgmlCloseTag(os, depth + 1,
                                                            inner_tag);
                        if(!extra_par.empty()) os << extra_par;
                        if(!tag.empty()) sgmlCloseTag(os, depth, tag);
-                       extra_par.clear();
+                       extra_par.erase();
 
                        switch (par->footnotekind) {
                        case LyXParagraph::FOOTNOTE:
@@ -2472,7 +2496,7 @@ void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
                        if ((present == TAB_LIKE) || (present == FIG_LIKE)) {
                                DocBookHandleCaption(os, inner_tag, depth,
                                                     desc_on, par);
-                               inner_tag.clear();
+                               inner_tag.erase();
                        } else {
                                sgmlOpenTag(os, depth + 1, inner_tag);
                        }
@@ -2482,7 +2506,7 @@ void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
                    .NumberOfLayout(params.textclass,
                                    "Caption").second) {
 #ifdef HAVE_SSTREAM
-                       ostringstream ost;
+                       std::ostringstream ost;
 #else
                        ostrstream ost;
 #endif
@@ -2509,22 +2533,23 @@ void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
 }
 
 
-/* push a tag in a style stack */
+// push a tag in a style stack
 void Buffer::push_tag(ostream & os, char const * tag,
                      int & pos, char stack[5][3])
 {
-       /* pop all previous tags */
+       // pop all previous tags
        for (int j = pos; j >= 0; --j)
                os << "</" << stack[j] << ">";
 
-       /* add new tag */
+       // add new tag
        sprintf(stack[++pos], "%s", tag);
 
-       /* push all tags */
+       // push all tags
        for (int i = 0; i <= pos; ++i)
                os << "<" << stack[i] << ">";
 }
 
+
 void Buffer::pop_tag(ostream & os, char const * tag,
                      int & pos, char stack[5][3])
 {
@@ -2546,7 +2571,7 @@ void Buffer::pop_tag(ostream & os, char const * tag,
 }
 
 
-/* handle internal paragraph parsing -- layout already processed */
+// Handle internal paragraph parsing -- layout already processed.
 
 // checks, if newcol chars should be put into this line
 // writes newline, if necessary.
@@ -2578,7 +2603,8 @@ void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
        char shape_type  = 0;               // shape font flag 
        bool is_em = false;                 // emphasis (italic) font flag 
 
-       int stack_num = -1;          // style stack position 
+       int stack_num = -1;          // style stack position
+       // Can this be rewritten to use a std::stack, please. (Lgb)
        char stack[5][3];            // style stack 
         unsigned int char_line_count = 5;     // Heuristic choice ;-) 
 
@@ -2587,18 +2613,18 @@ void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
        else
                main_body = par->BeginningOfMainBody();
 
-       /* gets paragraph main font */
+       // gets paragraph main font
        if (main_body > 0)
                font1 = style.labelfont;
        else
                font1 = style.font;
 
   
-       /* parsing main loop */
+       // parsing main loop
        for (LyXParagraph::size_type i = 0;
             i < par->size(); ++i) {
 
-               /* handle quote tag */
+               // handle quote tag
                if (i == main_body && !par->IsDummy()) {
                        if (main_body > 0)
                                font1 = style.font;
@@ -2640,7 +2666,7 @@ void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
                        }
                }
 
-               /* handle bold face */
+               // handle bold face
                if (font1.series() != font2.series()) {
                        if (font2.series() == LyXFont::BOLD_SERIES) {
                                push_tag(os, "bf", stack_num, stack);
@@ -2652,7 +2678,7 @@ void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
                        }
                }
 
-               /* handle italic and slanted fonts */
+               // handle italic and slanted fonts
                if (font1.shape() != font2.shape()) {
                        switch(shape_type) {
                        case 0:
@@ -2686,7 +2712,7 @@ void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
                                }
                        }
                }
-               /* handle <em> tag */
+               // handle <em> tag
                if (font1.emph() != font2.emph()) {
                        if (font2.emph() == LyXFont::ON) {
                                push_tag(os, "em", stack_num, stack);
@@ -2733,12 +2759,12 @@ void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
                font1 = font2;
        }
 
-       /* needed if there is an optional argument but no contents */
+       // needed if there is an optional argument but no contents
        if (main_body > 0 && main_body == par->size()) {
                font1 = style.font;
        }
 
-       /* pop all defined Styles */
+       // pop all defined Styles
        for (j = stack_num; j >= 0; --j) {
                linux_doc_line_break(os, 
                                     char_line_count, 
@@ -2746,27 +2772,27 @@ void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
                os << "</" << stack[j] << ">";
        }
 
-       /* resets description flag correctly */
+       // resets description flag correctly
        switch(desc_on){
        case 1:
-               /* <tag> not closed... */
+               // <tag> not closed...
                linux_doc_line_break(os, char_line_count, 6);
                os << "</tag>";
                break;
        case 2:
-               /* fprintf(file, "</p>");*/
+               // fprintf(file, "</p>");
                break;
        }
 }
 
 
-/* print an error message */
+// Print an error message.
 void Buffer::LinuxDocError(LyXParagraph * par, int pos,
                           char const * message) 
 {
        InsetError * new_inset;
 
-       /* insert an error marker in text */
+       // insert an error marker in text
        new_inset = new InsetError(message);
        par->InsertChar(pos, LyXParagraph::META_INSET);
        par->InsertInset(pos, new_inset);
@@ -2785,8 +2811,10 @@ void Buffer::makeDocBookFile(string const & fname, int column)
        LyXParagraph * par = paragraph;
 
        string top_element= textclasslist.LatexnameOfClass(params.textclass);
+       // Please use a real stack.
        string environment_stack[MAX_NEST_LEVEL];
        string environment_inner[MAX_NEST_LEVEL];
+       // Please use a real stack.
        string command_stack[MAX_NEST_LEVEL];
        bool command_flag= false;
        int command_depth= 0, command_base= 0, cmd_depth= 0;
@@ -2794,7 +2822,7 @@ void Buffer::makeDocBookFile(string const & fname, int column)
         string item_name, command_name;
        string c_depth, c_params, tmps;
 
-       int depth= 0;              /* paragraph depth */
+       int depth = 0; // paragraph depth
 
        tex_code_break_column = column; 
 
@@ -2827,13 +2855,13 @@ void Buffer::makeDocBookFile(string const & fname, int column)
        }
 
        while (par) {
-               int desc_on = 0;            /* description mode*/
+               int desc_on = 0; // description mode
                LyXLayout const & style =
                        textclasslist.Style(users->buffer()->params.textclass,
                                            par->layout);
                par->AutoDeleteInsets();
 
-               /* environment tag closing */
+               // environment tag closing
                for( ; depth > par->depth; --depth) {
                        if(environment_inner[depth] != "!-- --") {
                                item_name= "listitem";
@@ -2845,8 +2873,8 @@ void Buffer::makeDocBookFile(string const & fname, int column)
                        }
                        sgmlCloseTag(ofs, depth + command_depth,
                                     environment_stack[depth]);
-                       environment_stack[depth].clear();
-                       environment_inner[depth].clear();
+                       environment_stack[depth].erase();
+                       environment_inner[depth].erase();
                }
 
                if(depth == par->depth
@@ -2865,8 +2893,8 @@ void Buffer::makeDocBookFile(string const & fname, int column)
                        sgmlCloseTag(ofs, depth + command_depth,
                                     environment_stack[depth]);
                        
-                       environment_stack[depth].clear();
-                       environment_inner[depth].clear();
+                       environment_stack[depth].erase();
+                       environment_inner[depth].erase();
                 }
 
                // Write opening SGML tags.
@@ -2937,7 +2965,7 @@ void Buffer::makeDocBookFile(string const & fname, int column)
                case LATEX_ITEM_ENVIRONMENT:
                        if (depth < par->depth) {
                                depth = par->depth;
-                               environment_stack[depth].clear();
+                               environment_stack[depth].erase();
                        }
 
                        if (environment_stack[depth] != style.latexname()) {
@@ -3003,7 +3031,7 @@ void Buffer::makeDocBookFile(string const & fname, int column)
                while(par && par->IsDummy());
 
                string end_tag;
-               /* write closing SGML tags */
+               // write closing SGML tags
                switch(style.latextype) {
                case LATEX_COMMAND:
                        end_tag = "title";
@@ -3081,10 +3109,6 @@ void Buffer::SimpleDocBookOnePar(ostream & os, string & extra,
                main_body = par->BeginningOfMainBody();
 
        // gets paragraph main font
-       //if (main_body > 0)
-       //      font1 = style.labelfont;
-       //else
-       //      font1 = style.font;
        LyXFont font1 = main_body > 0 ? style.labelfont : style.font;
        
        int char_line_count = depth;
@@ -3113,7 +3137,7 @@ void Buffer::SimpleDocBookOnePar(ostream & os, string & extra,
                if (c == LyXParagraph::META_INSET) {
                        Inset * inset = par->GetInset(i);
 #ifdef HAVE_SSTREAM
-                       ostringstream ost;
+                       std::ostringstream ost;
                        inset->DocBook(ost);
                        string tmp_out = ost.str().c_str();
 #else
@@ -3149,8 +3173,7 @@ void Buffer::SimpleDocBookOnePar(ostream & os, string & extra,
                        if (c != '\0')
                                os << c;
                        ++char_line_count;
-               }
-               else {
+               } else {
                        string sgml_string;
                        if (par->linuxDocConvertChar(c, sgml_string)
                            && !style.free_spacing) { // in freespacing
@@ -3161,19 +3184,17 @@ void Buffer::SimpleDocBookOnePar(ostream & os, string & extra,
                                        ++char_line_count;
                                        os << "\n</term><listitem><para>";
                                        desc_on = 2;
-                               }
-                               else  {
+                               } else {
                                        os << c;
                                }
-                       }
-                       else {
+                       } else {
                                os << sgml_string;
                        }
                }
                font1 = font2;
        }
 
-       /* needed if there is an optional argument but no contents */
+       // needed if there is an optional argument but no contents
        if (main_body > 0 && main_body == par->size()) {
                font1 = style.font;
        }
@@ -3181,10 +3202,10 @@ void Buffer::SimpleDocBookOnePar(ostream & os, string & extra,
                os << "</emphasis>";
        }
        
-       /* resets description flag correctly */
+       // resets description flag correctly
        switch(desc_on){
        case 1:
-               /* <term> not closed... */
+               // <term> not closed...
                os << "</term>";
                break;
        }
@@ -3196,7 +3217,7 @@ int Buffer::runLaTeX()
 {
        if (!users->text) return 0;
 
-       ProhibitInput();
+       ProhibitInput(users);
 
        // get LaTeX-Filename
        string name = getLatexName();
@@ -3249,9 +3270,9 @@ int Buffer::runLaTeX()
         if (a || (res & LaTeX::ERRORS)){
                 users->redraw();
                 users->fitCursor();
-                users->updateScrollbar();
+                //users->updateScrollbar();
         }
-        AllowInput();
+        AllowInput(users);
  
         return latex.getNumErrors();
 }
@@ -3261,7 +3282,7 @@ int Buffer::runLiterate()
 {
        if (!users->text) return 0;
 
-       ProhibitInput();
+       ProhibitInput(users);
 
        // get LaTeX-Filename
        string name = getLatexName();
@@ -3320,9 +3341,9 @@ int Buffer::runLiterate()
         if (a || (res & Literate::ERRORS)){
                 users->redraw();
                 users->fitCursor();
-                users->updateScrollbar();
+                //users->updateScrollbar();
         }
-        AllowInput();
+        AllowInput(users);
  
         return literate.getNumErrors();
 }
@@ -3332,7 +3353,7 @@ int Buffer::buildProgram()
 {
         if (!users->text) return 0;
  
-        ProhibitInput();
+        ProhibitInput(users);
  
         // get LaTeX-Filename
         string name = getLatexName();
@@ -3385,14 +3406,15 @@ int Buffer::buildProgram()
                 markNwClean();
         }
  
-        // if we removed error insets before we ran Literate/Build or if we inserted
-        // error insets after we ran Literate/Build this must be run:
+        // if we removed error insets before we ran Literate/Build or
+       // if we inserted error insets after we ran Literate/Build this
+       // must be run:
        if (a || (res & Literate::ERRORS)){
                users->redraw();
                users->fitCursor();
-               users->updateScrollbar();
+               //users->updateScrollbar();
        }
-       AllowInput();
+       AllowInput(users);
 
        return literate.getNumErrors();
 }
@@ -3405,7 +3427,7 @@ int Buffer::runChktex()
 {
        if (!users->text) return 0;
 
-       ProhibitInput();
+       ProhibitInput(users);
 
        // get LaTeX-Filename
        string name = getLatexName();
@@ -3445,9 +3467,9 @@ int Buffer::runChktex()
        if (a || res){
                users->redraw();
                users->fitCursor();
-               users->updateScrollbar();
+               //users->updateScrollbar();
        }
-       AllowInput();
+       AllowInput(users);
 
        return res;
 }
@@ -3488,10 +3510,9 @@ void Buffer::RoffAsciiTable(ostream & os, LyXParagraph * par)
                switch (c) {
                case LyXParagraph::META_INSET:
                        if ((inset = par->GetInset(i))) {
-#if 1
 #ifdef HAVE_SSTREAM
                                stringstresm ss(ios::in | ios::out);
-                               inset->Latex(ss, -1);
+                               inset->Ascii(ss);
                                ss.seekp(0);
                                ss.get(c);
                                while (!ss) {
@@ -3503,7 +3524,7 @@ void Buffer::RoffAsciiTable(ostream & os, LyXParagraph * par)
                                }
 #else
                                strstream ss;
-                               inset->Latex(ss, -1);
+                               inset->Ascii(ss);
                                ss.seekp(0);
                                ss.get(c);
                                while (!ss) {
@@ -3514,27 +3535,6 @@ void Buffer::RoffAsciiTable(ostream & os, LyXParagraph * par)
                                        ss.get(c);
                                }
                                delete [] ss.str();
-#endif
-#else
-                               fstream fs(fname2.c_str(), ios::in|ios::out);
-                               if (!fs) {
-                                       WriteAlert(_("LYX_ERROR:"),
-                                                  _("Cannot open temporary file:"), fname2);
-                                       ofs.close();
-                                       remove(fname1.c_str());
-                                       return;
-                               }
-                               inset->Latex(fs, -1);
-                               fs.seekp(0);
-                               fs.get(c);
-                               while(!fs) {
-                                       if (c == '\\')
-                                               ofs << "\\\\";
-                                       else
-                                               ofs << c;
-                                       fs >> c;
-                               }
-                               fs.close();
 #endif
                        }
                        break;
@@ -3546,10 +3546,6 @@ void Buffer::RoffAsciiTable(ostream & os, LyXParagraph * par)
                        break;
                case LyXParagraph::META_HFILL: 
                        break;
-#if 0
-               case LyXParagraph::META_PROTECTED_SEPARATOR:
-                       break;
-#endif
                case '\\': 
                        ofs << "\\\\";
                        break;
@@ -3684,7 +3680,7 @@ void Buffer::validate(LaTeXFeatures & features) const
        }
        
        if (lyxerr.debugging(Debug::LATEX)) {
-               features.showStruct(params);
+               features.showStruct();
        }
 }
 
@@ -3865,26 +3861,50 @@ void Buffer::markDepClean(string const & name)
 }
 
 
-void Buffer::Dispatch(string const & command)
+bool Buffer::Dispatch(string const & command)
 {
        // Split command string into command and argument
        string cmd, line = frontStrip(command);
        string arg = strip(frontStrip(split(line, cmd, ' ')));
 
-       Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
+       return Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
 }
 
 
-void Buffer::Dispatch(int action, string const & argument)
+bool Buffer::Dispatch(int action, string const & argument)
 {
+       bool dispatched = true;
        switch (action) {
                case LFUN_EXPORT: 
                        MenuExport(this, argument);
                        break;
 
                default:
-                       lyxerr << "A truly unknown func!" << endl;
-               break;
+                       dispatched = false;
+       }
+       return dispatched;
+}
+
 
-       } // end of switch
+void Buffer::ChangeLanguage(Language const * from, Language const * to)
+{
+
+       LyXParagraph * par = paragraph;
+       while (par) {
+               par->ChangeLanguage(from, to);
+               par = par->next;
+       }
+}
+
+
+bool Buffer::isMultiLingual()
+{
+
+       LyXParagraph * par = paragraph;
+       while (par) {
+               if (par->isMultiLingual())
+                       return true;
+               par = par->next;
+       }
+       return false;
 }