]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
Fix crash when running lyx -dbg insets -e ...
[lyx.git] / src / buffer.C
index 8a0d18603b43a8daa650960b2745cd220b70ab84..53893e39d3924b5aff3e3f9972401d983a921a77 100644 (file)
 #include "BufferView.h"
 #include "ParagraphParameters.h"
 
-using std::stringstream;
 using std::ostream;
 using std::ofstream;
 using std::ifstream;
@@ -127,7 +126,7 @@ extern LyXAction lyxaction;
 
 namespace {
 
-const int LYX_FORMAT = 218;
+const int LYX_FORMAT = 220;
 
 } // namespace anon
 
@@ -278,6 +277,29 @@ void Buffer::setFileName(string const & newfile)
 }
 
 
+// We'll remove this later. (Lgb)
+namespace {
+
+string last_inset_read;
+
+struct ErtComp 
+{
+       ErtComp() : active(false), in_tabular(false) {
+       }
+       string contents;
+       bool active;
+       bool in_tabular;
+};
+
+std::stack<ErtComp> ert_stack;
+ErtComp ert_comp;
+
+
+} // anon
+
+
+int unknown_layouts;
+
 // candidate for move to BufferView
 // (at least some parts in the beginning of the func)
 //
@@ -288,84 +310,103 @@ void Buffer::setFileName(string const & newfile)
 // Returns false if "\the_end" is not read for formats >= 2.13. (Asger)
 bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
 {
+       unknown_layouts = 0;
+#ifdef NO_LATEX
+       ert_comp.contents.erase();
+       ert_comp.active = false;
+       ert_comp.in_tabular = false;
+#endif
+       
        int pos = 0;
        Paragraph::depth_type depth = 0; 
        bool the_end_read = false;
 
-       Paragraph * return_par = 0;
+       Paragraph * first_par = 0;
        LyXFont font(LyXFont::ALL_INHERIT, params.language);
        if (file_format < 216 && params.language->lang() == "hebrew")
                font.setLanguage(default_language);
-       // If we are inserting, we cheat and get a token in advance
-       bool has_token = false;
-       string pretoken;
 
        if (!par) {
                par = new Paragraph;
        } else {
                // We are inserting into an existing document
                users->text->breakParagraph(users);
-               return_par = users->text->firstParagraph();
+               first_par = users->text->firstParagraph();
                pos = 0;
                markDirty();
                // We don't want to adopt the parameters from the
                // document we insert, so we skip until the text begins:
                while (lex.IsOK()) {
                        lex.nextToken();
-                       pretoken = lex.GetString();
+                       string const pretoken = lex.GetString();
                        if (pretoken == "\\layout") {
-                               has_token = true;
+                               lex.pushToken(pretoken);
                                break;
                        }
                }
        }
 
        while (lex.IsOK()) {
-               if (has_token) {
-                       has_token = false;
-               } else {
-                       lex.nextToken();
-                       pretoken = lex.GetString();
-               }
+               lex.nextToken();
+               string const token = lex.GetString();
+
+               if (token.empty()) continue;
 
-               if (pretoken.empty()) continue;
+               lyxerr[Debug::PARSER] << "Handling token: `"
+                                     << token << "'" << endl;
                
                the_end_read =
-                       parseSingleLyXformat2Token(lex, par, return_par,
-                                                  pretoken, pos, depth,
-                                                  font
-                               );
+                       parseSingleLyXformat2Token(lex, par, first_par,
+                                                  token, pos, depth,
+                                                  font);
        }
    
-       if (!return_par)
-               return_par = par;
+       if (!first_par)
+               first_par = par;
 
-       paragraph = return_par;
-       
-       return the_end_read;
-}
+       paragraph = first_par;
 
+       if (unknown_layouts > 0) {
+               string s = _("Couldn't set the layout for ");
+               if (unknown_layouts == 1) {
+                       s += _("one paragraph");
+               } else {
+                       s += tostr(unknown_layouts);
+                       s += _(" paragraphs");
+               }
+               WriteAlert(_("Textclass Loading Error!"), s,
+                          _("When reading " + fileName()));
+       }       
 
-// We'll remove this later. (Lgb)
-namespace {
+       return the_end_read;
+}
 
-string last_inset_read;
 
-} // anon
+void Buffer::insertErtContents(Paragraph * par, int & pos,
+                              LyXFont const & font, bool set_inactive) 
+{
+       if (!ert_comp.contents.empty()) {
+               lyxerr[Debug::INSETS] << "ERT contents:\n"
+                      << ert_comp.contents << endl;
+               Inset * inset = new InsetERT(ert_comp.contents);
+               par->insertInset(pos++, inset, font);
+               ert_comp.contents.erase();
+       }
+       if (set_inactive) {
+               ert_comp.active = false;
+       }
+}
 
 
 bool
 Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
-                                  Paragraph *& return_par,
+                                  Paragraph *& first_par,
                                   string const & token, int & pos,
                                   Paragraph::depth_type & depth, 
                                   LyXFont & font
        )
 {
        bool the_end_read = false;
-#ifdef NO_LATEX
-       static string inset_ert_contents;
-#endif
 #ifndef NO_PEXTRA_REALLY
        // This is super temporary but is needed to get the compability
        // mode for minipages work correctly together with new tabulars.
@@ -377,12 +418,20 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 #endif
        
        if (token[0] != '\\') {
+#ifdef NO_LATEX
+               if (ert_comp.active) {
+                       ert_comp.contents += token;
+               } else {
+#endif
                for (string::const_iterator cit = token.begin();
                     cit != token.end(); ++cit) {
                        par->insertChar(pos, (*cit), font);
                        ++pos;
                }
                checkminipage = true;
+#ifdef NO_LATEX
+               }
+#endif
        } else if (token == "\\i") {
                Inset * inset = new InsetLatexAccent;
                inset->read(this, lex);
@@ -390,13 +439,9 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                ++pos;
        } else if (token == "\\layout") {
 #ifdef NO_LATEX
+               ert_comp.in_tabular = false;
                // Do the insetert.
-               if (!inset_ert_contents.empty()) {
-                       Inset * inset = new InsetERT(inset_ert_contents);
-                       par->insertInset(pos, inset, font);
-                       ++pos;
-                       inset_ert_contents.erase();
-               }
+               insertErtContents(par, pos, font);
 #endif
                 lex.EatLine();
                 string const layoutname = lex.GetString();
@@ -404,6 +449,11 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                         = textclasslist.NumberOfLayout(params.textclass,
                                                        layoutname);
 
+#ifdef NO_LATEX
+               if (compare_no_case(layoutname, "latex") == 0) {
+                       ert_comp.active = true;
+               }
+#endif
 #ifdef USE_CAPTION
                // The is the compability reading of layout caption.
                // It can be removed in LyX version 1.3.0. (Lgb)
@@ -457,8 +507,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        ++pos;
                } else {
 #endif
-                       if (!return_par)
-                               return_par = par;
+                       if (!first_par)
+                               first_par = par;
                        else {
                                par = new Paragraph(par);
                        }
@@ -469,6 +519,12 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                                // layout not found
                                // use default layout "Standard" (0)
                                par->layout = 0;
+                               ++unknown_layouts;
+                               string const s = _("Layout had to be changed from\n")
+                                       + layoutname + _(" to ")
+                                       + textclasslist.NameOfLayout(params.textclass, par->layout);
+                               InsetError * new_inset = new InsetError(s);
+                               par->insertInset(0, new_inset);
                        }
                        // Test whether the layout is obsolete.
                        LyXLayout const & layout =
@@ -824,7 +880,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                }
                // Small hack so that files written with klyx will be
                // parsed correctly.
-               if (return_par) {
+               if (first_par) {
                        par->params().spacing(Spacing(tmp_space, tmp_val));
                } else {
                        params.spacing.set(tmp_space, tmp_val);
@@ -882,11 +938,13 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                lex.next();
                string const tok = lex.GetString();
                if (tok == "no_latex") {
-                       ; // nothing
+                       // Do the insetert.
+                       insertErtContents(par, pos, font);
                } else if (tok == "latex") {
-                       ; // nothing
+                       ert_comp.active = true;
                } else if (tok == "default") {
-                       ; // nothing
+                       // Do the insetert.
+                       insertErtContents(par, pos, font);
                } else {
                        lex.printError("Unknown LaTeX font flag "
                                       "`$$Token'");
@@ -974,7 +1032,16 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                // But insets should read it, it is a part of
                // the inset isn't it? Lgb.
        } else if (token == "\\begin_inset") {
+#ifdef NO_LATEX
+               insertErtContents(par, pos, font, false);
+               ert_stack.push(ert_comp);
+               ert_comp = ErtComp();
+#endif
                readInset(lex, par, pos, font);
+#ifdef NO_LATEX
+               ert_comp = ert_stack.top();
+               ert_stack.pop();
+#endif
        } else if (token == "\\SpecialChar") {
                LyXLayout const & layout =
                        textclasslist.Style(params.textclass, 
@@ -1004,9 +1071,27 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                }
                ++pos;
        } else if (token == "\\newline") {
+#ifdef NO_LATEX
+
+               if (!ert_comp.in_tabular && ert_comp.active) {
+                       ert_comp.contents += char(Paragraph::META_NEWLINE);
+               } else {
+                       // Since we cannot know it this is only a regular
+                       // newline or a tabular cell delimter we have to
+                       // handle the ERT here.
+                       insertErtContents(par, pos, font, false);
+
+                       par->insertChar(pos, Paragraph::META_NEWLINE, font);
+                       ++pos;
+               }
+#else
                par->insertChar(pos, Paragraph::META_NEWLINE, font);
                ++pos;
+#endif
        } else if (token == "\\LyXTable") {
+#ifdef NO_LATEX
+               ert_comp.in_tabular = true;
+#endif
                Inset * inset = new InsetTabular(*this);
                inset->read(this, lex);
                par->insertInset(pos, inset, font);
@@ -1039,12 +1124,30 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                }
                par->bibkey->read(this, lex);                   
        } else if (token == "\\backslash") {
+#ifdef NO_LATEX
+               if (ert_comp.active) {
+                       ert_comp.contents += "\\";
+               } else {
+#endif
                par->insertChar(pos, '\\', font);
                ++pos;
+#ifdef NO_LATEX
+               }
+#endif
        } else if (token == "\\the_end") {
+#ifdef NO_LATEX
+               // If we still have some ert active here we have to insert
+               // it so we don't loose it. (Lgb)
+               insertErtContents(par, pos, font);
+#endif
                the_end_read = true;
                minipar = parBeforeMinipage = 0;
        } else {
+#ifdef NO_LATEX
+               if (ert_comp.active) {
+                       ert_comp.contents += token;
+               } else {
+#endif
                // This should be insurance for the future: (Asger)
                lex.printError("Unknown token `$$Token'. "
                               "Inserting as text.");
@@ -1054,6 +1157,9 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        par->insertChar(pos, (*cit), font);
                        ++pos;
                }
+#ifdef NO_LATEX
+               }
+#endif
        }
 #ifndef NO_PEXTRA_REALLY
        // now check if we have a minipage paragraph as at this
@@ -1174,8 +1280,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                par->previous(0);
                parBeforeMinipage = p;
                minipar = par;
-               if (!return_par || (return_par == par))
-                       return_par = p;
+               if (!first_par || (first_par == par))
+                       first_par = p;
 
                InsetMinipage * mini = new InsetMinipage;
                mini->pos(static_cast<InsetMinipage::Position>(minipar->params().pextraAlignment()));
@@ -3157,7 +3263,7 @@ void Buffer::simpleDocBookOnePar(ostream & os, string & extra,
 
                if (c == Paragraph::META_INSET) {
                        Inset * inset = par->getInset(i);
-                       std::ostringstream ost;
+                       ostringstream ost;
                        inset->docBook(this, ost);
                        string tmp_out = ost.str().c_str();
 
@@ -3638,3 +3744,21 @@ Inset * Buffer::getInsetFromID(int id_arg) const
        }
        return 0;
 }
+
+
+Paragraph * Buffer::getParFromID(int id) const
+{
+       if (id < 0) return 0;
+       Paragraph * par = paragraph;
+       while (par) {
+               if (par->id() == id) {
+                       return par;
+               }
+               Paragraph * tmp = par->getParFromID(id);
+               if (tmp) {
+                       return tmp;
+               }
+               par = par->next();
+       }
+       return 0;
+}