]> git.lyx.org Git - features.git/blobdiff - src/buffer.C
some new (not extensive) changes, some fixes, will probably reverto to .la libs later...
[features.git] / src / buffer.C
index a587a127c245f948ce58ead5f3bee9d669fbffcc..e0953cf145bdde710a6fad6f78f0acb7232a5fe5 100644 (file)
@@ -43,6 +43,7 @@ using std::setw;
 #include "bufferlist.h"
 #include "lyx_main.h"
 #include "lyx_gui_misc.h"
+#include "LyXAction.h"
 #include "lyxrc.h"
 #include "lyxlex.h"
 #include "tex-strings.h"
@@ -50,6 +51,7 @@ using std::setw;
 #include "lyx_cb.h"
 #include "minibuffer.h"
 #include "lyxfont.h"
+#include "version.h"
 #include "mathed/formulamacro.h"
 #include "insets/lyxinset.h"
 #include "insets/inseterror.h"
@@ -99,9 +101,10 @@ using std::setw;
 
 // all these externs should eventually be removed.
 extern BufferList bufferlist;
-extern void SmallUpdate(signed char);
-extern unsigned char GetCurrentTextClass();
-extern void BeforeChange();
+
+extern void MenuExport(Buffer *, string const &);
+extern LyXAction lyxaction;
+
 
 static const float LYX_FORMAT = 2.15;
 
@@ -116,15 +119,12 @@ Buffer::Buffer(string const & file, LyXRC * lyxrc, bool ronly)
        filename = file;
        filepath = OnlyPath(file);
        paragraph = 0;
-       text = 0;
-       the_locking_inset = 0;
        lyx_clean = true;
        bak_clean = true;
        dvi_clean_orgd = false;  // Heinrich Bauer, 23/03/98
        dvi_clean_tmpd = false;  // Heinrich Bauer, 23/03/98
        dep_clean = 0;
        read_only = ronly;
-       inset_slept = false;
        users = 0;
        lyxvc.buffer(this);
        if (read_only || (lyxrc && lyxrc->use_tempdir)) {
@@ -156,7 +156,6 @@ Buffer::~Buffer()
                par = tmppar;
        }
        paragraph = 0;
-       delete text;
 }
 
 
@@ -192,7 +191,7 @@ void Buffer::resetAutosaveTimers()
 }
 
 
-void Buffer::setFileName(string const & newfile)
+void Buffer::fileName(string const & newfile)
 {
        filename = MakeAbsPath(newfile);
        filepath = OnlyPath(filename);
@@ -200,19 +199,9 @@ void Buffer::setFileName(string const & newfile)
        updateTitles();
 }
 
-void Buffer::InsetUnlock()
-{
-       if (the_locking_inset) {
-               if (!inset_slept) the_locking_inset->InsetUnlock();
-               the_locking_inset = 0;
-               text->FinishUndo();
-               inset_slept = false;
-       }
-}
-
 
 // Inserts a file into current document
-bool Buffer::insertLyXFile(string const & filen)
+bool BufferView::insertLyXFile(string const & filen)
        //
        // (c) CHT Software Service GmbH
        // Uwe C. Schroeder
@@ -223,46 +212,49 @@ bool Buffer::insertLyXFile(string const & filen)
 {
        if (filen.empty()) return false;
 
-       string filename = MakeAbsPath(filen);
+       string fname = MakeAbsPath(filen);
 
        // check if file exist
-       FileInfo fi(filename);
+       FileInfo fi(fname);
 
        if (!fi.readable()) {
                WriteAlert(_("Error!"),
                           _("Specified file is unreadable: "),
-                          MakeDisplayPath(filename, 50));
+                          MakeDisplayPath(fname, 50));
                return false;
        }
        
-       BeforeChange();
+       beforeChange();
 
-       FilePtr myfile(filename, FilePtr::read);
-       if (!myfile()) {
+       ifstream ifs(fname.c_str());
+       if (!ifs) {
                WriteAlert(_("Error!"),
                           _("Cannot open specified file: "),
-                          MakeDisplayPath(filename, 50));
+                          MakeDisplayPath(fname, 50));
                return false;
        }
        LyXLex lex(0, 0);
-       lex.setFile(myfile);
-       int c = fgetc(myfile());
-       ungetc(c, myfile);
+       lex.setStream(ifs);
+       char c; ifs.get(c);
+       ifs.putback(c);
 
        bool res = true;
 
        if (c == '#') {
                lyxerr.debug() << "Will insert file with header" << endl;
-               res = readFile(lex, text->cursor.par);
+               res = buffer()->readFile(lex, text->cursor.par);
        } else {
                lyxerr.debug() << "Will insert file without header" << endl;
-               res = readLyXformat2(lex, text->cursor.par);
+               res = buffer()->readLyXformat2(lex, text->cursor.par);
        }
+
        resize();
        return res;
 }
 
 
+// candidate for move to BufferView
+// (at least some parts in the beginning of the func)
 //
 // Uwe C. Schroeder
 // changed to be public and have one parameter
@@ -290,8 +282,8 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
        if(!par) {
                par = new LyXParagraph;
        } else {
-               text->BreakParagraph();
-               return_par = text->FirstParagraph();
+               users->text->BreakParagraph();
+               return_par = users->text->FirstParagraph();
                pos = 0;
                markDirty();
                // We don't want to adopt the parameters from the
@@ -321,10 +313,10 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                        continue;
                else if (token[0] != '\\') {
                        int n = token.length();
-                       for (int i = 0; i < n; i++) {
+                       for (int i = 0; i < n; ++i) {
                                par->InsertChar(pos, token[i]);
                                par->SetFont(pos, font);
-                               pos++;
+                               ++pos;
                        }
                } else if (token == "\\i") {
                        inset = new InsetLatexAccent;
@@ -379,28 +371,28 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                        font = LyXFont(LyXFont::ALL_INHERIT);
                } else if (token == "\\begin_float") {
                        tmpret = lex.FindToken(string_footnotekinds);
-                       if (tmpret == -1) tmpret++;
+                       if (tmpret == -1) ++tmpret;
                        if (tmpret != LYX_LAYOUT_DEFAULT) 
-                               footnotekind = (LyXParagraph::footnote_kind)tmpret; // bad
+                               footnotekind = static_cast<LyXParagraph::footnote_kind>(tmpret); // bad
                        if (footnotekind == LyXParagraph::FOOTNOTE
                            || footnotekind == LyXParagraph::MARGIN)
                                footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
                        else 
                                footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
                } else if (token == "\\begin_deeper") {
-                       depth++;
+                       ++depth;
                } else if (token == "\\end_deeper") {
                        if (!depth) {
                                lex.printError("\\end_deeper: "
                                               "depth is already null");
                        }
                        else
-                               depth--;
+                               --depth;
                } else if (token == "\\begin_preamble") {
                        params.readPreamble(lex);
                } else if (token == "\\textclass") {
                        lex.EatLine();
-                       pair<bool, LyXTextClassList::ClassList::size_type> pp = 
+                       pair<bool, LyXTextClassList::size_type> pp = 
                                textclasslist.NumberOfClass(lex.GetString());
                        if (pp.first) {
                                params.textclass = pp.second;
@@ -424,7 +416,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                        lex.EatLine();
                        params.options = lex.GetString();
                } else if (token == "\\language") {
-                       params.readLanguage(lex);           
+                       params.readLanguage(lex);    
                } else if (token == "\\fontencoding") {
                        lex.EatLine();
                } else if (token == "\\inputencoding") {
@@ -460,11 +452,11 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                } else if (token == "\\defskip") {
                        lex.nextToken();
                        params.defskip = VSpace(lex.GetString());
-               } else if (token == "\\no_isolatin1") {
+               } else if (token == "\\no_isolatin1") { // obsolete
                        lex.nextToken();
-               } else if (token == "\\no_babel") {
+               } else if (token == "\\no_babel") { // obsolete
                        lex.nextToken();
-               } else if (token == "\\no_epsfig") {
+               } else if (token == "\\no_epsfig") { // obsolete
                        lex.nextToken();
                } else if (token == "\\epsfig") { // obsolete
                        // Indeed it is obsolete, but we HAVE to be backwards
@@ -632,7 +624,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                } else if (token == "\\tocdepth") {
                        lex.nextToken();
                        params.tocdepth = lex.GetInteger();
-               } else if (token == "\\baselinestretch") { // now obsolete
+               } else if (token == "\\baselinestretch") { // obsolete
                        lex.nextToken(); // should not be used directly
                        // anymore.
                        // Will probably keep a kind of support just for
@@ -657,7 +649,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                } else if (token == "\\float_placement") {
                        lex.nextToken();
                        params.float_placement = lex.GetString();
-               } else if (token == "\\cursor") {
+               } else if (token == "\\cursor") { // obsolete
                        // this is obsolete, so we just skip it.
                        lex.nextToken();
                } else if (token == "\\family") { 
@@ -710,13 +702,13 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                } else if (token == "\\align") {
                        tmpret = lex.FindToken(string_align);
                        if (tmpret == -1) tmpret++;
-                       if (tmpret != LYX_LAYOUT_DEFAULT) {
+                       if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
                                tmpret2 = 1;
-                               for (; tmpret>0; tmpret--)
+                               for (; tmpret > 0; --tmpret)
                                        tmpret2 = tmpret2 * 2;
                                par->align = LyXAlignment(tmpret2);
                        }
-               } else if (token == "\\added_space_top"){
+               } else if (token == "\\added_space_top") {
                        lex.nextToken();
                        par->added_space_top = lex.GetString();
                } else if (token == "\\added_space_bottom") {
@@ -890,6 +882,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                                par->InsertInset(pos, inset);
                                par->SetFont(pos, font);
                                pos++;
+#if 0
                        } else if (tmptok == "Label") {
                                // Kept for compability. Remove in 0.13.
                                if (lex.EatLine()) {
@@ -902,6 +895,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                                        par->SetFont(pos, font);
                                        pos++;
                                }
+#endif
                        } else if (tmptok == "Info") {
                                inset = new InsetInfo;
                                inset->Read(lex);
@@ -940,9 +934,11 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                                        if (!inscmd.getOptions().empty() || !inscmd.getContents().empty()) {
                                                inset = new InsetRef(inscmd, this);
                                        }
-                                       /* This condition comes from a temporary solution
-                                          to the latexdel ref inset that was transformed to an empty ref
-                                          inset plus the body surronded by latexdel insets */
+                                       // This condition comes from a
+                                       // temporary solution to the latexdel
+                                       // ref inset that was transformed to
+                                       // an empty ref inset plus the body
+                                       // surronded by latexdel insets
                                        else {
                                                string cont, opt, tmptmptok, cmdname;
                                                lex.next();
@@ -1003,7 +999,10 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                                        inset = new InsetPrintIndex(this);
                                } else if (inscmd.getCmdName() == "lyxparent") {
                                        inset = new InsetParent(inscmd.getContents(), this);
-                               } else 
+                               }
+#if 0
+                               // Do we need this at all now?
+                               else 
                                        // The following three are only for compatibility
                                        if (inscmd.getCmdName() == "-") {
                                                inset = new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
@@ -1013,12 +1012,13 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                                                inset = new InsetSpecialChar(InsetSpecialChar::LDOTS);
                                        } else
                                                inset = inscmd.Clone();
+#endif
                               
                                if (inset) {
                                        par->InsertChar(pos, LyXParagraph::META_INSET);
                                        par->InsertInset(pos, inset);
                                        par->SetFont(pos, font);
-                                       pos++;
+                                       ++pos;
                                }
                        }
                } else if (token == "\\InsetQuotes") {
@@ -1027,7 +1027,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                        par->InsertChar(pos, LyXParagraph::META_INSET); 
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
-                       pos++;
+                       ++pos;
 #if 0
                } else if (token == "\\InsetLatex") {
                        inset = new InsetLatex;
@@ -1035,7 +1035,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                        par->InsertChar(pos, LyXParagraph::META_INSET); 
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
-                       pos++;
+                       ++pos;
                } else if (token == "\\InsetLatexDel") {
                        lex.printError(_("Warning: Ignoring Old Inset"));
 #endif
@@ -1045,35 +1045,35 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                        par->InsertChar(pos, LyXParagraph::META_INSET); 
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
-                       pos++;
+                       ++pos;
                } else if (token == "\\SpecialChar") {
                        inset = new InsetSpecialChar;
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET); 
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
-                       pos++;
+                       ++pos;
                } else if (token == "\\Figure") {
                        inset = new InsetFig(100, 100, this);
                        inset->Read(lex);
                        par->InsertChar(pos, LyXParagraph::META_INSET); 
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
-                       pos++;
+                       ++pos;
                } else if (token == "\\newline") {
                        par->InsertChar(pos, LyXParagraph::META_NEWLINE);
                        par->SetFont(pos, font);
-                       pos++;
+                       ++pos;
                } else if (token == "\\LyXTable") {
                        par->table = new LyXTable(lex);
                } else if (token == "\\hfill") {
                        par->InsertChar(pos, LyXParagraph::META_HFILL);
                        par->SetFont(pos, font);
-                       pos++;
+                       ++pos;
                } else if (token == "\\protected_separator") {
                        par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
                        par->SetFont(pos, font);
-                       pos++;
+                       ++pos;
                } else if (token == "\\bibitem") {  // ale970302
                        if (!par->bibkey)
                                par->bibkey = new InsetBibKey;
@@ -1081,7 +1081,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                }else if (token == "\\backslash") {
                        par->InsertChar(pos, '\\');
                        par->SetFont(pos, font);
-                       pos++;
+                       ++pos;
                }else if (token == "\\the_end") {
                        the_end_read = true;
                } else {
@@ -1089,10 +1089,10 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
                        lex.printError("Unknown token `$$Token'. "
                                       "Inserting as text.");
                        int n = token.length();
-                       for (int i = 0; i < n; i++) {
+                       for (int i = 0; i < n; ++i) {
                                par->InsertChar(pos, token[i]);
                                par->SetFont(pos, font);
-                               pos++;
+                               ++pos;
                        }
                }
        }
@@ -1164,54 +1164,52 @@ bool Buffer::readFile(LyXLex & lex, LyXParagraph * par)
                    
 
 // Returns false if unsuccesful
-bool Buffer::writeFile(string const & filename, bool flag)
+bool Buffer::writeFile(string const & fname, bool flag)
 {
        // 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 && (filename == this->filename)) {
+       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: ")
-                              << filename << endl;
+                              << fname << endl;
                else
                        WriteAlert(_("Error! Document is read-only: "),
-                                  filename);
+                                  fname);
                return false;
        }
 
-       FileInfo finfo(filename);
+       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: ")
-                              << filename << endl;
+                              << fname << endl;
                else
                        WriteFSAlert(_("Error! Cannot write file: "),
-                                    filename);
+                                    fname);
                return false;
        }
 
-       ofstream ofs(filename.c_str());
+       ofstream ofs(fname.c_str());
        if (!ofs) {
                if (flag)
                        lyxerr << _("Error! Cannot open file: ")
-                              << filename << endl;
+                              << fname << endl;
                else
-                       WriteFSAlert(_("Error! Canno open file:"),
-                                    filename);
+                       WriteFSAlert(_("Error! Cannot open file: "),
+                                    fname);
                return false;
        }
        // The top of the file should not be written by params.
-       // collect some very important information
-       string userName(getUserName()) ;
 
        // write out a comment in the top of the file
-       ofs << "#LyX 1.1 Copyright 1995-1999 Matthias Ettrich"
-               " and the LyX Team\n";
+       ofs << '#' << LYX_DOCVERSION 
+           << " created this file. For more info see http://www.lyx.org/\n";
        ofs.setf(ios::showpoint|ios::fixed);
        ofs.precision(2);
        ofs << "\\lyxformat " << setw(4) <<  LYX_FORMAT << "\n";
@@ -1234,7 +1232,7 @@ bool Buffer::writeFile(string const & filename, bool flag)
 }
 
 
-void Buffer::writeFileAscii(string const & filename, int linelen) 
+void Buffer::writeFileAscii(string const & fname, int linelen) 
 {
        LyXFont font1, font2;
        Inset * inset;
@@ -1247,9 +1245,9 @@ void Buffer::writeFileAscii(string const & filename, int linelen)
        long fpos = 0;
        bool ref_printed = false;
 
-       ofstream ofs(filename.c_str());
+       ofstream ofs(fname.c_str());
        if (!ofs) {
-               WriteFSAlert(_("Error: Cannot write file:"), filename);
+               WriteFSAlert(_("Error: Cannot write file:"), fname);
                return;
        }
 
@@ -1401,22 +1399,24 @@ void Buffer::writeFileAscii(string const & filename, int linelen)
                                        break;
                                }
                                if (ltype_depth > depth) {
-                                       for(j = ltype_depth-1; j>depth; j--)
+                                       for(j = ltype_depth - 1; j > depth; --j)
                                                ofs << "  ";
                                        currlinelen += (ltype_depth-depth)*2;
                                }
                                if (par->table) {
-                                       for(j = 0;j<cells;j++) {
+                                       for(j = 0; j < cells; ++j) {
                                                ofs << '+';
-                                               for(h = 0; h < (clen[j]+1); h++)
+                                               for(h = 0; h < (clen[j] + 1);
+                                                   ++h)
                                                        ofs << '-';
                                        }
                                        ofs << "+\n";
-                                       for(j = 0; j<depth; j++)
+                                       for(j = 0; j < depth; ++j)
                                                ofs << "  ";
-                                       currlinelen = depth*2;
+                                       currlinelen = depth * 2;
                                        if (ltype_depth > depth) {
-                                               for(j = ltype_depth; j>depth; j--)
+                                               for(j = ltype_depth;
+                                                   j > depth; --j)
                                                        ofs << "  ";
                                                currlinelen += (ltype_depth-depth)*2;
                                        }
@@ -1451,32 +1451,34 @@ void Buffer::writeFileAscii(string const & filename, int linelen)
                                                for(j = actpos; j < clen[cell-1];j++)
                                                        ofs << ' ';
                                                ofs << " |\n";
-                                               for(j = 0; j<depth; j++)
+                                               for(j = 0; j < depth; ++j)
                                                        ofs << "  ";
                                                currlinelen = depth*2;
                                                if (ltype_depth > depth) {
-                                                       for(j = ltype_depth; j>depth; j--)
+                                                       for(j = ltype_depth; j > depth; --j)
                                                                ofs << "  ";
-                                                       currlinelen += (ltype_depth-depth)*2;
+                                                       currlinelen += (ltype_depth-depth) * 2;
                                                }
-                                               for(j = 0;j<cells;j++) {
+                                               for(j = 0; j < cells; ++j) {
                                                        ofs << '+';
-                                                       for(h = 0; h < (clen[j]+1); ++h)
+                                                       for(h = 0; h < (clen[j] + 1); ++h)
                                                                ofs << '-';
                                                }
                                                ofs << "+\n";
-                                               for(j = 0; j<depth; j++)
+                                               for(j = 0; j < depth; ++j)
                                                        ofs << "  ";
-                                               currlinelen = depth*2;
+                                               currlinelen = depth * 2;
                                                if (ltype_depth > depth) {
-                                                       for(j = ltype_depth; j>depth; j--)
+                                                       for(j = ltype_depth;
+                                                           j > depth; --j)
                                                                ofs << "  ";
                                                        currlinelen += (ltype_depth-depth)*2;
                                                }
                                                ofs << "| ";
                                                cell = 1;
                                        } else {
-                                               for(j = actpos; j<clen[cell-1]; j++)
+                                               for(j = actpos;
+                                                   j < clen[cell - 1]; ++j)
                                                        ofs << ' ';
                                                ofs << " | ";
                                                ++cell;
@@ -1485,13 +1487,14 @@ void Buffer::writeFileAscii(string const & filename, int linelen)
                                        currlinelen = actpos = 0;
                                } else {
                                        ofs << "\n";
-                                       for(j = 0; j<depth; j++)
+                                       for(j = 0; j < depth; ++j)
                                                ofs << "  ";
                                        currlinelen = depth * 2;
                                        if (ltype_depth > depth) {
-                                               for(j = ltype_depth; j>depth; j--)
+                                               for(j = ltype_depth;
+                                                   j > depth; --j)
                                                        ofs << "  ";
-                                               currlinelen += (ltype_depth-depth)*2;
+                                               currlinelen += (ltype_depth - depth) * 2;
                                        }
                                }
                                break;
@@ -1529,15 +1532,15 @@ void Buffer::writeFileAscii(string const & filename, int linelen)
                        for(j = actpos; j < clen[cell - 1]; ++j)
                                ofs << ' ';
                        ofs << " |\n";
-                       for(j = 0; j<depth; j++)
+                       for(j = 0; j < depth; ++j)
                                ofs << "  ";
                        currlinelen = depth * 2;
                        if (ltype_depth > depth) {
                                for(j = ltype_depth; j > depth; --j)
                                        ofs << "  ";
-                               currlinelen += (ltype_depth-depth)*2;
+                               currlinelen += (ltype_depth - depth) * 2;
                        }
-                       for(j = 0;j<cells; ++j) {
+                       for(j = 0; j < cells; ++j) {
                                ofs << '+';
                                for(h = 0; h < (clen[j] + 1); ++h)
                                        ofs << '-';
@@ -1552,12 +1555,15 @@ void Buffer::writeFileAscii(string const & filename, int linelen)
 }
 
 
-void Buffer::makeLaTeXFile(string const & filename, 
+void Buffer::makeLaTeXFile(string const & fname, 
                           string const & original_path,
                           bool nice, bool only_body)
 {
        lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
-       params.textclass = GetCurrentTextClass();
+       
+       // How the **** can this be needed?
+       //params.textclass = current_view->buffer()->params.textclass;
+       
        niceFile = nice; // this will be used by Insetincludes.
 
        tex_code_break_column = lyxrc->ascii_linelen;
@@ -1565,9 +1571,9 @@ void Buffer::makeLaTeXFile(string const & filename,
         LyXTextClass const & tclass =
                textclasslist.TextClass(params.textclass);
 
-       ofstream ofs(filename.c_str());
+       ofstream ofs(fname.c_str());
        if (!ofs) {
-               WriteFSAlert(_("Error: Cannot open file:"), filename);
+               WriteFSAlert(_("Error: Cannot open file: "), fname);
                return;
        }
        
@@ -1586,18 +1592,15 @@ void Buffer::makeLaTeXFile(string const & filename,
        string LFile;
        
        if (!only_body && nice) {
-               LFile += "%% This LaTeX-file was created by <";
-               LFile += userName + "> " + date();
-               LFile += "%% LyX 1.0 (C) 1995-1999 by Matthias Ettrich and the LyX Team\n";
-               LFile += "\n%% Do not edit this file unless you know what you are doing.\n";
-               texrow.newline();
-               texrow.newline();
+               LFile += "%% " LYX_DOCVERSION " created this file.  "
+                        "For more info, see http://www.lyx.org/.\n";
+               LFile += "%% Do not edit unless you really know what you are doing.\n";
                texrow.newline();
                texrow.newline();
        }
        lyxerr.debug() << "lyx header finished" << endl;
        // There are a few differences between nice LaTeX and usual files:
-       // usual is \batchmode, uses \listfiles and has a 
+       // 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.
@@ -1611,9 +1614,6 @@ void Buffer::makeLaTeXFile(string const & filename,
                        LFile += "\\batchmode\n"; // changed
                        // from \nonstopmode
                        texrow.newline();
-                       // We don't need listfiles anymore
-                       //LFile += "\\listfiles\n";
-                       //texrow.newline();
                }
                if (!original_path.empty()) {
                        LFile += "\\makeatletter\n";
@@ -1676,7 +1676,8 @@ void Buffer::makeLaTeXFile(string const & filename,
                                options += "onecolumn,";
                }
 
-               if (!params.use_geometry && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
+               if (!params.use_geometry 
+                   && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
                        options += "landscape,";
                
                // language should be a parameter to \documentclass
@@ -2106,27 +2107,18 @@ bool Buffer::isSGML() const
 void Buffer::sgmlOpenTag(ostream & os, int depth,
                         string const & latexname) const
 {
-       static char * space[] = {" ","  ", "   ", "    ", "     ", "      ",
-                                "       ",
-                                "        ", "         ", "          ",
-                                "          "};
-
-       os << space[depth] << "<" << latexname << ">\n";
+       os << string(depth, ' ') << "<" << latexname << ">\n";
 }
 
 
 void Buffer::sgmlCloseTag(ostream & os, int depth,
                          string const & latexname) const
 {
-       static char * space[] = {" ", "  ", "   ", "    ", "     ",
-                                "      ", "       ", "        ",
-                                "         ", "          ", "          "};
-
-       os << space[depth] << "</" << latexname << ">\n";
+       os << string(depth, ' ') << "</" << latexname << ">\n";
 }
 
 
-void Buffer::makeLinuxDocFile(string const & filename, int column)
+void Buffer::makeLinuxDocFile(string const & fname, int column)
 {
        LyXParagraph * par = paragraph;
 
@@ -2136,10 +2128,10 @@ void Buffer::makeLinuxDocFile(string const & filename, int column)
 
        int depth = 0;              /* paragraph depth */
 
-       ofstream ofs(filename.c_str());
+       ofstream ofs(fname.c_str());
 
        if (!ofs) {
-               WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), filename);
+               WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
                return;
        }
    
@@ -2156,7 +2148,7 @@ void Buffer::makeLinuxDocFile(string const & filename, int column)
 
         string userName(getUserName());
        ofs << "<!-- LinuxDoc file was created by LyX 1.0 (C) 1995-1999 "
-           << "by <" << userName << "> " << (char*)date() << " -->\n";
+           << "by <" << userName << "> " << date() << " -->\n";
 
        if(params.options.empty())
                sgmlOpenTag(ofs, 0, top_element);
@@ -2170,7 +2162,7 @@ void Buffer::makeLinuxDocFile(string const & filename, int column)
        while (par) {
                int desc_on = 0;            /* description mode*/
                LyXLayout const & style =
-                       textclasslist.Style(GetCurrentTextClass(),
+                       textclasslist.Style(users->buffer()->params.textclass,
                                            par->layout);
                par->AutoDeleteInsets();
 
@@ -2188,13 +2180,13 @@ void Buffer::makeLinuxDocFile(string const & filename, int column)
                        }
                }
 
-               /* environment tag closing */
+               // environment tag closing
                for( ; depth > par->depth; --depth) {
                        sgmlCloseTag(ofs, depth, environment_stack[depth]);
                        environment_stack[depth].clear();
                }
 
-               /* write opening SGML tags */
+               // write opening SGML tags
                switch(style.latextype) {
                case LATEX_PARAGRAPH:
                        if(depth == par->depth 
@@ -2435,18 +2427,16 @@ void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
 void Buffer::push_tag(ostream & os, char const * tag,
                      int & pos, char stack[5][3])
 {
-       int j;
-
        /* pop all previous tags */
-       for (j = pos; j >= 0; --j)
+       for (int j = pos; j >= 0; --j)
                os << "</" << stack[j] << ">";
 
        /* add new tag */
        sprintf(stack[++pos], "%s", tag);
 
        /* push all tags */
-       for (j = 0; j <= pos; ++j)
-               os << "<" << stack[j] << ">";
+       for (int i = 0; i <= pos; ++i)
+               os << "<" << stack[i] << ">";
 }
 
 
@@ -2454,19 +2444,17 @@ void Buffer::push_tag(ostream & os, char const * tag,
 void Buffer::pop_tag(ostream & os, char const * tag,
                     int & pos, char stack[5][3])
 {
-       int j;
-
        // pop all tags till specified one
-       for (j = pos; (j >= 0) && (strcmp(stack[j], tag)); --j)
+       for (int j = pos; (j >= 0) && (strcmp(stack[j], tag)); --j)
                os << "</" << stack[j] << ">";
 
        // closes the tag
        os << "</" << tag << ">";
 
        // push all tags, but the specified one
-       for (j = j + 1; j <= pos; ++j) {
-               os << "<" << stack[j] << ">";
-               strcpy(stack[j-1], stack[j]);
+       for (int i = 0; i <= pos; ++i) {
+               os << "<" << stack[i] << ">";
+               strcpy(stack[i - 1], stack[i]);
        }
        --pos;
 }
@@ -2489,7 +2477,7 @@ void linux_doc_line_break(ostream & os, unsigned int & colcount,
 
 
 void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
-                                 int desc_on, int const depth)
+                                 int desc_on, int const /*depth*/)
 {
        LyXFont font1, font2;
        char c;
@@ -2709,7 +2697,7 @@ void Buffer::LinuxDocError(LyXParagraph * par, int pos,
 
 enum { MAX_NEST_LEVEL = 25};
 
-void Buffer::makeDocBookFile(string const & filename, int column)
+void Buffer::makeDocBookFile(string const & fname, int column)
 {
        LyXParagraph * par = paragraph;
 
@@ -2727,13 +2715,12 @@ void Buffer::makeDocBookFile(string const & filename, int column)
 
        tex_code_break_column = column; 
 
-       ofstream ofs(filename.c_str());
+       ofstream ofs(fname.c_str());
        if (!ofs) {
-               WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), filename);
+               WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
                return;
        }
    
-       //ResetTexRow();
        texrow.reset();
 
        ofs << "<!doctype " << top_element
@@ -2744,9 +2731,8 @@ void Buffer::makeDocBookFile(string const & filename, int column)
        else
                ofs << "\n [ " << params.preamble << " \n]>\n\n";
 
-        string userName(getUserName());
-       ofs << "<!-- DocBook file was created by LyX 1.0 (C) 1995-1999\n"
-           << "by <" << userName << "> " << (char*)date() << " -->\n";
+       ofs << "<!-- DocBook file was created by " << LYX_DOCVERSION 
+           << "\n  See http://www.lyx.org/ for more information -->\n";
 
        if(params.options.empty())
                sgmlOpenTag(ofs, 0, top_element);
@@ -2759,7 +2745,7 @@ void Buffer::makeDocBookFile(string const & filename, int column)
 
        while (par) {
                int desc_on= 0;            /* description mode*/
-               LyXLayout const & style = textclasslist.Style(GetCurrentTextClass(),
+               LyXLayout const & style = textclasslist.Style(users->buffer()->params.textclass,
                                                   par->layout);
                par->AutoDeleteInsets();
 
@@ -3062,7 +3048,7 @@ void Buffer::SimpleDocBookOnePar(string & file, string & extra,
                        //         desc_on == 4
                        //
                        if(desc_on!= 3 || i!= 0) {
-                               if(tmp_out[0] == '@') {
+                               if(!tmp_out.empty() && tmp_out[0] == '@') {
                                        if(desc_on == 4)
                                                extra += frontStrip(tmp_out, '@');
                                        else
@@ -3120,9 +3106,9 @@ void Buffer::SimpleDocBookOnePar(string & file, string & extra,
 }
 
 
-bool Buffer::removeAutoInsets()
+bool BufferView::removeAutoInsets()
 {
-       LyXParagraph *par = paragraph;
+       LyXParagraph * par = buffer()->paragraph;
 
        LyXCursor cursor = text->cursor;
        LyXCursor tmpcursor = cursor;
@@ -3134,16 +3120,17 @@ bool Buffer::removeAutoInsets()
                if (par->AutoDeleteInsets()){
                        a = true;
                        if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
-                               /* this is possible now, since SetCursor takes
-                                  care about footnotes */
+                               // this is possible now, since SetCursor takes
+                               // care about footnotes
                                text->SetCursorIntern(par, 0);
-                               text->RedoParagraphs(text->cursor, text->cursor.par->Next());
+                               text->RedoParagraphs(text->cursor,
+                                                    text->cursor.par->Next());
                                text->FullRebreak();
                        }
                }
                par = par->next;
        }
-       /* avoid forbidden cursor positions caused by error removing */ 
+       // avoid forbidden cursor positions caused by error removing
        if (cursor.pos > cursor.par->Last())
                cursor.pos = cursor.par->Last();
        text->SetCursorIntern(cursor.par, cursor.pos);
@@ -3154,7 +3141,7 @@ bool Buffer::removeAutoInsets()
 
 int Buffer::runLaTeX()
 {
-       if (!text) return 0;
+       if (!users->text) return 0;
 
        ProhibitInput();
 
@@ -3172,7 +3159,7 @@ int Buffer::runLaTeX()
        users->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));   
 
        // Remove all error insets
-       bool a = removeAutoInsets();
+       bool a = users->removeAutoInsets();
 
        // Always generate the LaTeX file
        makeLaTeXFile(name, org_path, false);
@@ -3191,7 +3178,7 @@ int Buffer::runLaTeX()
        } else if ((res & LaTeX::ERRORS)) {
                users->owner()->getMiniBuffer()->Set(_("Done"));
                // Insert all errors as errors boxes
-               insertErrors(terr);
+               users->insertErrors(terr);
                
                // Dvi should also be kept dirty if the latex run
                // ends up with errors. However it should be possible
@@ -3217,7 +3204,7 @@ int Buffer::runLaTeX()
 
 int Buffer::runLiterate()
 {
-       if (!text) return 0;
+       if (!users->text) return 0;
 
        ProhibitInput();
 
@@ -3238,7 +3225,7 @@ int Buffer::runLiterate()
        users->owner()->getMiniBuffer()->Set(_("Running Literate..."));   
 
        // Remove all error insets
-       bool a = removeAutoInsets();
+       bool a = users->removeAutoInsets();
 
        // generate the Literate file if necessary
        if (!isDviClean() || a) {
@@ -3260,7 +3247,7 @@ int Buffer::runLiterate()
        } else if ((res & Literate::ERRORS)) {
                users->owner()->getMiniBuffer()->Set(_("Done"));
                // Insert all errors as errors boxes
-               insertErrors(terr);
+               users->insertErrors(terr);
                
                // Dvi should also be kept dirty if the latex run
                // ends up with errors. However it should be possible
@@ -3286,7 +3273,7 @@ int Buffer::runLiterate()
 
 int Buffer::buildProgram()
 {
-        if (!text) return 0;
+        if (!users->text) return 0;
  
         ProhibitInput();
  
@@ -3307,7 +3294,7 @@ int Buffer::buildProgram()
         users->owner()->getMiniBuffer()->Set(_("Building Program..."));   
  
         // Remove all error insets
-        bool a = removeAutoInsets();
+        bool a = users->removeAutoInsets();
  
         // generate the LaTeX file if necessary
         if (!isNwClean() || a) {
@@ -3329,7 +3316,7 @@ int Buffer::buildProgram()
         } else if ((res & Literate::ERRORS)) {
                 users->owner()->getMiniBuffer()->Set(_("Done"));
                 // Insert all errors as errors boxes
-                insertErrors(terr);
+               users->insertErrors(terr);
                 
                 // Literate files should also be kept dirty if the literate 
                 // command run ends up with errors.
@@ -3357,7 +3344,7 @@ int Buffer::buildProgram()
 // Other flags: -wall -v0 -x
 int Buffer::runChktex()
 {
-       if (!text) return 0;
+       if (!users->text) return 0;
 
        ProhibitInput();
 
@@ -3374,7 +3361,7 @@ int Buffer::runChktex()
        users->owner()->getMiniBuffer()->Set(_("Running chktex..."));
 
        // Remove all error insets
-       bool a = removeAutoInsets();
+       bool a = users->removeAutoInsets();
 
        // Generate the LaTeX file if neccessary
        if (!isDviClean() || a) {
@@ -3391,7 +3378,7 @@ int Buffer::runChktex()
                           _("Could not run with file:"), name);
        } else if (res > 0) {
                // Insert all errors as errors boxes
-               insertErrors(terr);
+               users->insertErrors(terr);
        }
 
        // if we removed error insets before we ran chktex or if we inserted
@@ -3407,17 +3394,14 @@ int Buffer::runChktex()
 }
 
 
-extern void AllFloats(char, char);
-
-
-void Buffer::insertErrors(TeXErrors & terr)
+void BufferView::insertErrors(TeXErrors & terr)
 {
        // Save the cursor position
        LyXCursor cursor = text->cursor;
 
        // This is drastic, but it's the only fix, I could find. (Asger)
-       AllFloats(1, 0);
-       AllFloats(1, 1);
+       allFloats(1, 0);
+       allFloats(1, 1);
 
        for (TeXErrors::Errors::const_iterator cit = terr.begin();
             cit != terr.end();
@@ -3431,9 +3415,9 @@ void Buffer::insertErrors(TeXErrors & terr)
                int tmpid = -1; 
                int tmppos = -1;
 
-               texrow.getIdFromRow(errorrow, tmpid, tmppos);
+               buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
 
-               LyXParagraph* texrowpar;
+               LyXParagraph * texrowpar = 0;
 
                if (tmpid == -1) {
                        texrowpar = text->FirstParagraph();
@@ -3446,7 +3430,6 @@ void Buffer::insertErrors(TeXErrors & terr)
                        continue;
 
                InsetError * new_inset = new InsetError(msgtxt);
-
                text->SetCursorIntern(texrowpar, tmppos);
                text->InsertInset(new_inset);
                text->FullRebreak();
@@ -3456,14 +3439,14 @@ void Buffer::insertErrors(TeXErrors & terr)
 }
 
 
-void Buffer::setCursorFromRow (int row)
+void BufferView::setCursorFromRow(int row)
 {
        int tmpid = -1; 
        int tmppos = -1;
 
-       texrow.getIdFromRow(row, tmpid, tmppos);
+       buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
 
-       LyXParagraph* texrowpar;
+       LyXParagraph * texrowpar;
 
        if (tmpid == -1) {
                texrowpar = text->FirstParagraph();
@@ -3622,35 +3605,6 @@ void Buffer::markDviDirty()
 }
 
 
-void Buffer::update(signed char f)
-{
-       if (!users) return;
-       
-       users->owner()->updateLayoutChoice();
-
-       if (!text->selection && f > -3)
-               text->sel_cursor = text->cursor;
-       
-       FreeUpdateTimer();
-       text->FullRebreak();
-       users->update();
-
-       if (f != 3 && f != -3) {
-               users->fitCursor();
-               users->updateScrollbar();
-       }
-
-       if (f == 1 || f == -1) {
-               if (isLyxClean()) {
-                       markDirty();
-                       users->owner()->getMiniBuffer()->setTimer(4);
-               } else {
-                       markDirty();
-               }
-       }
-}
-
-
 void Buffer::validate(LaTeXFeatures & features)
 {
        LyXParagraph * par = paragraph;
@@ -3762,7 +3716,8 @@ void Buffer::setOldPaperStuff()
 }
 #endif
 
-void Buffer::insertInset(Inset * inset, string const & lout,
+
+void BufferView::insertInset(Inset * inset, string const & lout,
                         bool no_table)
 {
        // check for table/list in tables
@@ -3776,7 +3731,7 @@ void Buffer::insertInset(Inset * inset, string const & lout,
        text->SetCursorParUndo();
        text->FreezeUndo();
        
-       BeforeChange();
+       beforeChange();
        if (!lout.empty()) {
                update(-2);
                text->BreakParagraph();
@@ -3789,7 +3744,7 @@ void Buffer::insertInset(Inset * inset, string const & lout,
                        update(-1);
                }
 
-               int lay = textclasslist.NumberOfLayout(params.textclass,
+               int lay = textclasslist.NumberOfLayout(buffer()->params.textclass,
                                                       lout).second;
                if (lay == -1) // layout not found
                        // use default layout "Standard" (0)
@@ -3816,9 +3771,9 @@ void Buffer::insertInset(Inset * inset, string const & lout,
 
 
 // Open and lock an updatable inset
-void Buffer::open_new_inset(UpdatableInset * new_inset)
+void BufferView::open_new_inset(UpdatableInset * new_inset)
 {
-       BeforeChange();
+       beforeChange();
        text->FinishUndo();
        insertInset(new_inset);
        text->CursorLeft();
@@ -3827,7 +3782,7 @@ void Buffer::open_new_inset(UpdatableInset * new_inset)
 }
 
 
-/* This function should be in Buffer because it's a buffer's property (ale) */
+// This function should be in Buffer because it's a buffer's property (ale)
 string Buffer::getIncludeonlyList(char delim)
 {
        string lst;
@@ -3856,18 +3811,18 @@ string Buffer::getIncludeonlyList(char delim)
 }
 
 
-/* This is also a buffer property (ale) */ 
+// This is also a buffer property (ale)
 string Buffer::getReferenceList(char delim)
 {
        /// 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 * tmp = bufferlist.getBuffer(params.parentname);
                if (tmp)
                  return tmp->getReferenceList(delim);
        }
 
-       LyXParagraph *par = paragraph;
+       LyXParagraph * par = paragraph;
        LyXParagraph::size_type pos;
        Inset * inset;
         string lst;
@@ -3888,13 +3843,13 @@ string Buffer::getReferenceList(char delim)
 }
 
 
-/* This is also a buffer property (ale) */ 
+// This is also a buffer property (ale)
 string Buffer::getBibkeyList(char delim)
 {
        /// if this is a child document and the parent is already loaded
        /// Use the parent's list instead  [ale990412]
         if (!params.parentname.empty() && bufferlist.exists(params.parentname)) {
-               Buffer *tmp = bufferlist.getBuffer(params.parentname);
+               Buffer * tmp = bufferlist.getBuffer(params.parentname);
                if (tmp)
                        return tmp->getBibkeyList(delim);
        }
@@ -3922,9 +3877,9 @@ string Buffer::getBibkeyList(char delim)
                                if (inset-> LyxCode() == Inset::BIBTEX_CODE) {
                                        if (!bibkeys.empty())
                                                bibkeys += delim;
-                                       bibkeys += static_cast<InsetBibtex*>(inset)->getKeys();
+                                       bibkeys += static_cast<InsetBibtex*>(inset)->getKeys(delim);
                                } else if (inset-> LyxCode() == Inset::INCLUDE_CODE) {
-                                       string bk = static_cast<InsetInclude*>(inset)->getKeys();
+                                       string bk = static_cast<InsetInclude*>(inset)->getKeys(delim);
                                        if (!bk.empty()) {
                                                if (!bibkeys.empty())
                                                        bibkeys += delim;
@@ -3947,10 +3902,10 @@ string Buffer::getBibkeyList(char delim)
 // think how this will work in a multiwindo/buffer environment, all the
 // cursors in all the views showing this buffer will move. (Lgb)
 // OK, then no cursor action should be allowed in buffer. (ale)
-bool Buffer::gotoLabel(string const & label)
+bool BufferView::gotoLabel(string const & label)
 
 {
-        LyXParagraph * par = paragraph;
+        LyXParagraph * par = buffer()->paragraph;
         LyXParagraph::size_type pos;
         Inset * inset;
         while (par) {
@@ -3958,7 +3913,7 @@ bool Buffer::gotoLabel(string const & label)
                 while ((inset = par->ReturnNextInsetPointer(pos))){     
                         for (int i = 0; i < inset->GetNumberOfLabels(); i++) {
                                if (label == inset->getLabel(i)) {
-                                       BeforeChange();
+                                       beforeChange();
                                        text->SetCursor(par, pos);
                                        text->sel_cursor = text->cursor;
                                        update(0);
@@ -4004,3 +3959,28 @@ void Buffer::markDepClean(string const & name)
                }
        }
 }
+
+
+void 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());
+}
+
+
+void Buffer::Dispatch(int action, string const & argument)
+{
+       switch (action) {
+               case LFUN_EXPORT: 
+                       MenuExport(this, argument);
+                       break;
+
+               default:
+                       lyxerr << "A truly unknown func!" << endl;
+               break;
+
+       } // end of switch
+}