]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
Forgot to add this files.
[lyx.git] / src / buffer.C
index e33f44ae519058fd523bc28c8ea04adafd012c28..7e5395cabc66013c2a5a150d4b2cdef5c53fb3b3 100644 (file)
@@ -56,9 +56,6 @@
 #include "insets/insetindex.h" 
 #include "insets/insetinclude.h"
 #include "insets/insettoc.h"
-#include "insets/insetlof.h"
-#include "insets/insetlot.h"
-#include "insets/insetloa.h"
 #include "insets/insetparent.h"
 #include "insets/insetspecialchar.h"
 #include "insets/figinset.h"
@@ -128,6 +125,7 @@ Buffer::Buffer(string const & file, bool ronly)
        bak_clean = true;
        dep_clean = 0;
        read_only = ronly;
+       unnamed = false;
        users = 0;
        lyxvc.buffer(this);
        if (read_only || (lyxrc.use_tempdir)) {
@@ -235,8 +233,6 @@ void Buffer::fileName(string const & newfile)
 // if par = 0 normal behavior
 // else insert behavior
 // Returns false if "\the_end" is not read for formats >= 2.13. (Asger)
-#define USE_PARSE_FUNCTION 1
-//#define USE_TABULAR_INSETS 1
 bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
 {
        string tmptok;
@@ -308,6 +304,10 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
 }
 
 
+// We'll remove this later. (Lgb)
+static string last_inset_read;
+
+
 bool
 Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                                   LyXParagraph *& return_par,
@@ -320,8 +320,6 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
        )
 {
        bool the_end_read = false;
-       // We'll remove this later. (Lgb)
-       static string last_inset_read;
        
        if (token[0] != '\\') {
                for (string::const_iterator cit = token.begin();
@@ -404,6 +402,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                //lyxerr << "old float: " << tmptok << endl;
                
                Inset * inset = 0;
+               string old_float;
                
                if (tmptok == "footnote") {
                        inset = new InsetFoot;
@@ -411,34 +410,56 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                        inset = new InsetMarginal;
                } else if (tmptok == "fig") {
                        inset = new InsetFloat("figure");
-                       //inset = new InsetFigure;
+                       old_float += "placement htbp\n";
                } else if (tmptok == "tab") {
                        inset = new InsetFloat("table");
-                       //inset = new InsetTable;
+                       old_float += "placement htbp\n";
                } else if (tmptok == "alg") {
                        inset = new InsetFloat("algorithm");
-                       //inset = new InsetAlgorithm;
+                       old_float += "placement htbp\n";
                } else if (tmptok == "wide-fig") {
                        InsetFloat * tmp = new InsetFloat("figure");
                        tmp->wide(true);
                        inset = tmp;
-                       //inset = new InsetFigure(true);
+                       old_float += "placement htbp\n";
                } else if (tmptok == "wide-tab") {
                        InsetFloat * tmp = new InsetFloat("table");
                        tmp->wide(true);
                        inset = tmp;
-                       //inset = new InsetTable(true);
+                       old_float += "placement htbp\n";
                }
 
                if (!inset) return false; // no end read yet
                
-               string old_float = "\ncollapsed true\n";
+               old_float += "collapsed true\n";
+
+               // Here we need to check for \end_deeper and handle that
+               // before we do the footnote parsing.
+               // This _is_ a hack! (Lgb)
+               while(true) {
+                       lex.next();
+                       string tmp = lex.GetString();
+                       if (tmp == "\\end_deeper") {
+                               lyxerr << "\\end_deeper caught!" << endl;
+                               if (!depth) {
+                                       lex.printError("\\end_deeper: "
+                                                      "depth is already null");
+                               } else
+                                       --depth;
+                               
+                       } else {
+                               old_float += tmp;
+                               old_float += ' ';
+                               break;
+                       }
+               }
+               
                old_float += lex.getLongString("\\end_float");
                old_float += "\n\\end_inset\n";
-               lyxerr << "float body: " << old_float << endl;
+               //lyxerr << "float body: " << old_float << endl;
 
 #ifdef HAVE_SSTREAM
-               istringstream istr(old_float.c_str());
+               istringstream istr(old_float);
 #else
                istrstream istr(old_float.c_str());
 #endif
@@ -468,7 +489,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                if (pp.first) {
                        params.textclass = pp.second;
                } else {
-                       lex.printError("Unknown textclass `$$Token'");
+                 lex.printError("Unknown textclass `$$Token'");
                        params.textclass = 0;
                }
                if (!textclasslist.Load(params.textclass)) {
@@ -829,149 +850,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                // 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();
-               string tmptok = lex.GetString();
-               last_inset_read = tmptok;
-               // test the different insets
-               if (tmptok == "Quotes") {
-                       Inset * inset = new InsetQuotes;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "External") {
-                       Inset * inset = new InsetExternal;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "FormulaMacro") {
-                       Inset * inset = new InsetFormulaMacro;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Formula") {
-                       Inset * inset = new InsetFormula;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Figure") {
-                       Inset * inset = new InsetFig(100, 100, this);
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Info") {
-                       Inset * inset = new InsetInfo;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Include") {
-                       Inset * inset = new InsetInclude(string(), this);
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "ERT") {
-                       Inset * inset = new InsetERT;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Tabular") {
-                       Inset * inset = new InsetTabular(this);
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Text") {
-                       Inset * inset = new InsetText;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Foot") {
-                       Inset * inset = new InsetFoot;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Marginal") {
-                       Inset * inset = new InsetMarginal;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Minipage") {
-                       Inset * inset = new InsetMinipage;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Float") {
-                       lex.next();
-                       string tmptok = lex.GetString();
-                       Inset * inset = new InsetFloat(tmptok);
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "List") {
-                       Inset * inset = new InsetList;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Theorem") {
-                       Inset * inset = new InsetList;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "Caption") {
-                       Inset * inset = new InsetCaption;
-                       inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-                       ++pos;
-               } else if (tmptok == "GRAPHICS") {
-                       Inset * inset = new InsetGraphics;
-                               //inset->Read(this, lex);
-                       par->InsertInset(pos, inset, font);
-               } else if (tmptok == "LatexCommand") {
-                       InsetCommand inscmd;
-                       inscmd.Read(this, lex);
-                       Inset * inset = 0;
-                       if (inscmd.getCmdName() == "cite") {
-                               inset = new InsetCitation(inscmd.getContents(), inscmd.getOptions());
-                       } else if (inscmd.getCmdName() == "bibitem") {
-                               lex.printError("Wrong place for bibitem");
-                               inset = inscmd.Clone();
-                       } else if (inscmd.getCmdName() == "BibTeX") {
-                               inset = new InsetBibtex(inscmd.getContents(), inscmd.getOptions(), this);
-                       } else if (inscmd.getCmdName() == "index") {
-                               inset = new InsetIndex(inscmd.getContents());
-                       } else if (inscmd.getCmdName() == "include") {
-                               inset = new InsetInclude(inscmd.getContents(), this);
-                       } else if (inscmd.getCmdName() == "label") {
-                               inset = new InsetLabel(inscmd.getCommand());
-                       } else if (inscmd.getCmdName() == "url"
-                                  || inscmd.getCmdName() == "htmlurl") {
-                               inset = new InsetUrl(inscmd.getCommand());
-                       } else if (inscmd.getCmdName() == "ref"
-                                  || inscmd.getCmdName() == "pageref"
-                                  || inscmd.getCmdName() == "vref"
-                                  || inscmd.getCmdName() == "vpageref"
-                                  || inscmd.getCmdName() == "prettyref") {
-                               if (!inscmd.getOptions().empty() || !inscmd.getContents().empty()) {
-                                       inset = new InsetRef(inscmd, this);
-                               }
-                       } else if (inscmd.getCmdName() == "tableofcontents") {
-                               inset = new InsetTOC(this);
-                       } else if (inscmd.getCmdName() == "listoffigures") {
-                               inset = new InsetLOF(this);
-                       } else if (inscmd.getCmdName() == "listofalgorithms") {
-                               inset = new InsetLOA(this);
-                       } else if (inscmd.getCmdName() == "listoftables") {
-                               inset = new InsetLOT(this);
-                       } else if (inscmd.getCmdName() == "printindex") {
-                               inset = new InsetPrintIndex(this);
-                       } else if (inscmd.getCmdName() == "lyxparent") {
-                               inset = new InsetParent(inscmd.getContents(), this);
-                       }
-                              
-                       if (inset) {
-                               par->InsertInset(pos, inset, font);
-                               ++pos;
-                       }
-               }
+               readInset(lex, par, pos, font);
        } else if (token == "\\SpecialChar") {
                LyXLayout const & layout =
                        textclasslist.Style(params.textclass, 
@@ -1004,7 +883,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                par->InsertChar(pos, LyXParagraph::META_NEWLINE, font);
                ++pos;
        } else if (token == "\\LyXTable") {
-#ifdef USE_TABULAR_INSETS
+#ifdef NEW_TABULAR
                Inset * inset = new InsetTabular(this);
                inset->Read(this, lex);
                par->InsertInset(pos, inset, font);
@@ -1031,20 +910,23 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                }
                ++pos;
        } else if (token == "\\bibitem") {  // ale970302
-               if (!par->bibkey)
-                       par->bibkey = new InsetBibKey;
+               if (!par->bibkey) {
+                       InsetCommandParams p( "bibitem" );
+                       par->bibkey = new InsetBibKey(p);
+               }
                par->bibkey->Read(this, lex);                   
-       }else if (token == "\\backslash") {
+       } else if (token == "\\backslash") {
                par->InsertChar(pos, '\\', font);
                ++pos;
-       }else if (token == "\\the_end") {
+       } else if (token == "\\the_end") {
                the_end_read = true;
        } else {
                // This should be insurance for the future: (Asger)
                lex.printError("Unknown token `$$Token'. "
                               "Inserting as text.");
-               for(string::const_iterator cit = token.begin();
-                   cit != token.end(); ++cit) {
+               string::const_iterator cit = token.begin();
+               string::const_iterator end = token.end();
+               for(; cit != end; ++cit) {
                        par->InsertChar(pos, (*cit), font);
                        ++pos;
                }
@@ -1052,13 +934,166 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
        return the_end_read;
 }
 
-bool Buffer::readFile(LyXLex & lex, LyXParagraph * par)
+
+void Buffer::readInset(LyXLex & lex, LyXParagraph *& par,
+                      int & pos, LyXFont & font)
 {
-       string token;
+       // consistency check
+       if (lex.GetString() != "\\begin_inset") {
+               lyxerr << "Buffer::readInset: Consistency check failed."
+                      << endl;
+       }
+       
+       lex.next();
+       string tmptok = lex.GetString();
+       last_inset_read = tmptok;
+       // test the different insets
+       if (tmptok == "Quotes") {
+               Inset * inset = new InsetQuotes;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "External") {
+               Inset * inset = new InsetExternal;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "FormulaMacro") {
+               Inset * inset = new InsetFormulaMacro;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Formula") {
+               Inset * inset = new InsetFormula;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Figure") {
+               Inset * inset = new InsetFig(100, 100, this);
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Info") {
+               Inset * inset = new InsetInfo;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Include") {
+               InsetCommandParams p( "Include" );
+               Inset * inset = new InsetInclude(p, this);
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "ERT") {
+               Inset * inset = new InsetERT;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Tabular") {
+               Inset * inset = new InsetTabular(this);
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Text") {
+               Inset * inset = new InsetText;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Foot") {
+               Inset * inset = new InsetFoot;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Marginal") {
+               Inset * inset = new InsetMarginal;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Minipage") {
+               Inset * inset = new InsetMinipage;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Float") {
+               lex.next();
+               string tmptok = lex.GetString();
+               Inset * inset = new InsetFloat(tmptok);
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "List") {
+               Inset * inset = new InsetList;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Theorem") {
+               Inset * inset = new InsetList;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "Caption") {
+               Inset * inset = new InsetCaption;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "GRAPHICS") {
+               Inset * inset = new InsetGraphics;
+               inset->Read(this, lex);
+               par->InsertInset(pos, inset, font);
+               ++pos;
+       } else if (tmptok == "LatexCommand") {
+               InsetCommandParams inscmd;
+               inscmd.Read(lex);
+               Inset * inset = 0;
+               if (inscmd.getCmdName() == "cite") {
+                       inset = new InsetCitation(inscmd);
+               } else if (inscmd.getCmdName() == "bibitem") {
+                       lex.printError("Wrong place for bibitem");
+                       inset = new InsetBibKey(inscmd);
+               } else if (inscmd.getCmdName() == "BibTeX") {
+                       inset = new InsetBibtex(inscmd, this);
+               } else if (inscmd.getCmdName() == "index") {
+                       inset = new InsetIndex(inscmd);
+               } else if (inscmd.getCmdName() == "include") {
+                       inset = new InsetInclude(inscmd, this);
+               } else if (inscmd.getCmdName() == "label") {
+                       inset = new InsetLabel(inscmd);
+               } else if (inscmd.getCmdName() == "url"
+                          || inscmd.getCmdName() == "htmlurl") {
+                       inset = new InsetUrl(inscmd);
+               } else if (inscmd.getCmdName() == "ref"
+                          || inscmd.getCmdName() == "pageref"
+                          || inscmd.getCmdName() == "vref"
+                          || inscmd.getCmdName() == "vpageref"
+                          || inscmd.getCmdName() == "prettyref") {
+                       if (!inscmd.getOptions().empty()
+                           || !inscmd.getContents().empty()) {
+                               inset = new InsetRef(inscmd, this);
+                       }
+               } else if (inscmd.getCmdName() == "tableofcontents"
+                          || inscmd.getCmdName() == "listofalgorithms"
+                          || inscmd.getCmdName() == "listoffigures"
+                          || inscmd.getCmdName() == "listoftables") {
+                       inset = new InsetTOC(inscmd);
+               } else if (inscmd.getCmdName() == "printindex") {
+                       inset = new InsetPrintIndex(inscmd);
+               } else if (inscmd.getCmdName() == "lyxparent") {
+                       inset = new InsetParent(inscmd, this);
+               }
+               
+               if (inset) {
+                       par->InsertInset(pos, inset, font);
+                       ++pos;
+               }
+       }
+}
 
+
+bool Buffer::readFile(LyXLex & lex, LyXParagraph * par)
+{
        if (lex.IsOK()) {
                lex.next();
-               token = lex.GetString();
+               string token(lex.GetString());
                if (token == "\\lyxformat") { // the first token _must_ be...
                        lex.next();
                        format = lex.GetFloat();
@@ -1268,9 +1303,18 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
        char c, footnoteflag = 0, depth = 0;
        string tmp;
        LyXParagraph::size_type i;
-       int j, h, ltype = 0, ltype_depth = 0,
-               * clen = 0, actcell = 0, actpos = 0, cell = 0, cells = 0,
-               currlinelen = 0;
+       int j;
+       int ltype = 0;
+       int ltype_depth = 0;
+       int actcell = 0;
+       int actpos = 0;
+#ifndef NEW_TABULAR
+       int h;
+       int * clen = 0;
+       int cell = 0;
+       int cells = 0;
+#endif
+       int currlinelen = 0;
        long fpos = 0;
        bool ref_printed = false;
 
@@ -2145,8 +2189,11 @@ void Buffer::latexParagraphs(ostream & ofs, LyXParagraph * par,
                ftcount = -1;
                if (layout.isEnvironment()
                     || par->pextra_type != LyXParagraph::PEXTRA_NONE) {
-                       par = par->TeXEnvironment(this, params, ofs, texrow,
-                                                 ftnote, ft_texrow, ftcount);
+                       par = par->TeXEnvironment(this, params, ofs, texrow
+#ifndef NEW_INSETS
+                                                 ,ftnote, ft_texrow, ftcount
+#endif
+                               );
                } else {
                        par = par->TeXOnePar(this, params, ofs, texrow, false
 #ifndef NEW_INSETS
@@ -3366,7 +3413,7 @@ int Buffer::runLiterate()
        users->owner()->getMiniBuffer()->Set(_("Running Literate..."));   
 
        // Remove all error insets
-       bool a = users->removeAutoInsets();
+       bool removedErrorInsets = users->removeAutoInsets();
 
        // generate the Literate file if necessary
        makeLaTeXFile(lit_name, org_path, false);
@@ -3399,7 +3446,7 @@ int Buffer::runLiterate()
 
        // if we removed error insets before we ran LaTeX or if we inserted
        // error insets after we ran LaTeX this must be run:
-        if (a || (res & Literate::ERRORS)){
+        if (removedErrorInsets || (res & Literate::ERRORS)){
                 users->redraw();
                 users->fitCursor();
                 //users->updateScrollbar();
@@ -3433,10 +3480,10 @@ int Buffer::buildProgram()
         users->owner()->getMiniBuffer()->Set(_("Building Program..."));   
  
         // Remove all error insets
-        bool a = users->removeAutoInsets();
+        bool removedErrorInsets = users->removeAutoInsets();
  
         // generate the LaTeX file if necessary
-        if (!isNwClean() || a) {
+        if (!isNwClean() || removedErrorInsets) {
                 makeLaTeXFile(lit_name, org_path, false);
                 markNwDirty();
         }
@@ -3470,7 +3517,7 @@ int Buffer::buildProgram()
         // 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)){
+       if (removedErrorInsets || (res & Literate::ERRORS)){
                users->redraw();
                users->fitCursor();
                //users->updateScrollbar();