]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
John's Layout Tabular UI improvements and Martins fixes to clearing the
[lyx.git] / src / buffer.C
index b2c50a9d700b4e102365531708bbd63baa2493f3..bd51584ec751c2446b4bfe17389e2df1e4733c60 100644 (file)
 #include "bufferview_funcs.h"
 #include "lyxfont.h"
 #include "version.h"
+#include "LaTeX.h"
+#include "Chktex.h"
+#include "LyXView.h"
+#include "debug.h"
+#include "LaTeXFeatures.h"
+#include "lyxtext.h"
+#include "gettext.h"
+#include "language.h"
+#include "encoding.h"
+#include "exporter.h"
+#include "Lsstream.h"
+#include "converter.h"
+#include "BufferView.h"
+#include "ParagraphParameters.h"
+#include "iterators.h"
+#include "lyxtextclasslist.h"
+
 #include "mathed/formulamacro.h"
 #include "mathed/formula.h"
+
 #include "insets/inset.h"
 #include "insets/inseterror.h"
 #include "insets/insetlabel.h"
 #endif
 #include "insets/insetcaption.h"
 #include "insets/insetfloatlist.h"
+
+#include "frontends/Dialogs.h"
+#include "frontends/Alert.h"
+
 #include "support/textutils.h"
 #include "support/filetools.h"
 #include "support/path.h"
 #include "support/os.h"
-#include "LaTeX.h"
-#include "Chktex.h"
-#include "LyXView.h"
-#include "debug.h"
-#include "LaTeXFeatures.h"
 #include "support/syscall.h"
 #include "support/lyxlib.h"
 #include "support/FileInfo.h"
 #include "support/lyxmanip.h"
-#include "lyxtext.h"
-#include "gettext.h"
-#include "language.h"
-#include "frontends/Dialogs.h"
-#include "frontends/Alert.h"
-#include "encoding.h"
-#include "exporter.h"
-#include "Lsstream.h"
-#include "converter.h"
-#include "BufferView.h"
-#include "ParagraphParameters.h"
-#include "iterators.h"
 
 #include <fstream>
 #include <iomanip>
 #include <map>
 #include <stack>
 #include <list>
+#include <algorithm>
 
 #include <cstdlib>
 #include <cmath>
 #include <sys/types.h>
 #include <utime.h>
 
-#include <algorithm>
 
 #ifdef HAVE_LOCALE
 #include <locale>
@@ -456,9 +461,14 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 #endif
                lex.eatLine();
                string const layoutname = lex.getString();
+               //lyxerr << "Looking for layout '"
+               // << layoutname << "'!" << endl;
+               
                pair<bool, layout_type> pp
                  = textclasslist.NumberOfLayout(params.textclass, layoutname);
 
+               //lyxerr << "Result: " << pp.first << "/" << pp.second << endl;
+               
 #ifndef NO_COMPABILITY
                if (compare_no_case(layoutname, "latex") == 0) {
                        ert_comp.active = true;
@@ -532,15 +542,18 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        } else {
                                // layout not found
                                // use default layout "Standard" (0)
+                               //lyxerr << "Layout '" << layoutname
+                               //       << "' was not found!" << endl;
+                               
                                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);
-                               par->setFont(0, LyXFont(LyXFont::ALL_INHERIT,
-                                                       params.language));
+                               par->insertInset(0, new_inset
+                                                LyXFont(LyXFont::ALL_INHERIT,
+                                                        params.language));
                        }
                        // Test whether the layout is obsolete.
                        LyXLayout const & layout =
@@ -724,21 +737,25 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                par->params().startOfAppendix(true);
        } else if (token == "\\paragraph_separation") {
                int tmpret = lex.findToken(string_paragraph_separation);
-               if (tmpret == -1) ++tmpret;
+               if (tmpret == -1)
+                       ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) 
                        params.paragraph_separation =
                                static_cast<BufferParams::PARSEP>(tmpret);
        } else if (token == "\\defskip") {
                lex.nextToken();
                params.defskip = VSpace(lex.getString());
+#ifndef NO_COMPABILITY
        } else if (token == "\\epsfig") { // obsolete
                // Indeed it is obsolete, but we HAVE to be backwards
                // compatible until 0.14, because otherwise all figures
                // in existing documents are irretrivably lost. (Asger)
                params.readGraphicsDriver(lex);
+#endif
        } else if (token == "\\quotes_language") {
                int tmpret = lex.findToken(string_quotes_language);
-               if (tmpret == -1) ++tmpret;
+               if (tmpret == -1)
+                       ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) {
                        InsetQuotes::quote_language tmpl = 
                                InsetQuotes::EnglishQ;
@@ -801,7 +818,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                params.use_numerical_citations = lex.getInteger();
        } else if (token == "\\paperorientation") {
                int tmpret = lex.findToken(string_orientation);
-               if (tmpret == -1) ++tmpret;
+               if (tmpret == -1)
+                       ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) 
                        params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
        } else if (token == "\\paperwidth") {
@@ -1178,9 +1196,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        + " " + lex.text()  + "\n";
 
                InsetError * new_inset = new InsetError(s);
-               par->insertInset(pos, new_inset);
-               par->setFont(pos, LyXFont(LyXFont::ALL_INHERIT,
-                                         params.language));
+               par->insertInset(pos, new_inset, LyXFont(LyXFont::ALL_INHERIT,
+                                                        params.language));
 
 #ifndef NO_COMPABILITY
                }
@@ -1191,7 +1208,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 #ifndef NO_PEXTRA_REALLY
        // I wonder if we could use this blanket fix for all the
        // checkminipage cases...
-       if (par && par->size()) {
+       // don't forget about ert paragraphs and compatibility read for'em
+       if (par && (par->size() || !ert_comp.contents.empty())) {
                // It is possible that this will check to often,
                // but that should not be an correctness issue.
                // Only a speed issue.
@@ -1205,6 +1223,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
        //
        // BEGIN pextra_minipage compability
        // This should be removed in 1.3.x (Lgb)
+       // I don't think we should remove this so fast (Jug)
        
        // This compability code is not perfect. In a couple
        // of rand cases it fails. When the minipage par is
@@ -1256,7 +1275,16 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                            lyxerr << "WP:" << mini->width() << endl;
                            mini->width(tostr(par->params().pextraWidthp())+"%");
                        }
+                       Paragraph * op = mini->firstParagraph();
                        mini->inset.paragraph(par);
+                       //
+                       // and free the old ones!
+                       //
+                       while(op) {
+                               Paragraph * pp = op->next();
+                               delete op;
+                               op = pp;
+                       }
                        // Insert the minipage last in the
                        // previous paragraph.
                        if (par->params().pextraHfill()) {
@@ -1293,6 +1321,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        tmp = tmp->next();
                }
                depth = parBeforeMinipage->params().depth();
+               // and set this depth on the par as it has not been set already
+               par->params().depth(depth);
                minipar = parBeforeMinipage = 0;
        } else if (!minipar &&
                   (par->params().pextraType() == Paragraph::PEXTRA_MINIPAGE))
@@ -1752,178 +1782,217 @@ bool Buffer::writeFile(string const & fname, bool flag) const
 
 
 string const Buffer::asciiParagraph(Paragraph const * par,
-                                   unsigned int linelen) const
+                                    unsigned int linelen,
+                                    bool noparbreak) const
 {
        ostringstream buffer;
+       ostringstream word;
        Paragraph::depth_type depth = 0;
        int ltype = 0;
        Paragraph::depth_type ltype_depth = 0;
        string::size_type currlinelen = 0;
        bool ref_printed = false;
-
-       int noparbreak = 0;
-       int islatex = 0;
-       if (!par->previous()) {
-               // begins or ends a deeper area ?
-               if (depth != par->params().depth()) {
-                       if (par->params().depth() > depth) {
-                               while (par->params().depth() > depth) {
-                                       ++depth;
-                               }
-                       } else {
-                               while (par->params().depth() < depth) {
-                                       --depth;
-                               }
+//     if (!par->previous()) {
+#if 0
+       // begins or ends a deeper area ?
+       if (depth != par->params().depth()) {
+               if (par->params().depth() > depth) {
+                       while (par->params().depth() > depth) {
+                               ++depth;
                        }
-               }
-               
-               // First write the layout
-               string const tmp = textclasslist.NameOfLayout(params.textclass, par->layout);
-               if (tmp == "Itemize") {
-                       ltype = 1;
-                       ltype_depth = depth + 1;
-               } else if (tmp == "Enumerate") {
-                       ltype = 2;
-                       ltype_depth = depth + 1;
-               } else if (contains(tmp, "ection")) {
-                       ltype = 3;
-                       ltype_depth = depth + 1;
-               } else if (contains(tmp, "aragraph")) {
-                       ltype = 4;
-                       ltype_depth = depth + 1;
-               } else if (tmp == "Description") {
-                       ltype = 5;
-                       ltype_depth = depth + 1;
-               } else if (tmp == "Abstract") {
-                       ltype = 6;
-                       ltype_depth = 0;
-               } else if (tmp == "Bibliography") {
-                       ltype = 7;
-                       ltype_depth = 0;
                } else {
-                       ltype = 0;
-                       ltype_depth = 0;
+                       while (par->params().depth() < depth) {
+                               --depth;
+                       }
                }
+       }
+#else
+       depth = par->params().depth();
+#endif
                
-               /* maybe some vertical spaces */ 
+       // First write the layout
+       string const tmp = textclasslist.NameOfLayout(params.textclass, par->layout);
+       if (tmp == "Itemize") {
+               ltype = 1;
+               ltype_depth = depth + 1;
+       } else if (tmp == "Enumerate") {
+               ltype = 2;
+               ltype_depth = depth + 1;
+       } else if (contains(tmp, "ection")) {
+               ltype = 3;
+               ltype_depth = depth + 1;
+       } else if (contains(tmp, "aragraph")) {
+               ltype = 4;
+               ltype_depth = depth + 1;
+       } else if (tmp == "Description") {
+               ltype = 5;
+               ltype_depth = depth + 1;
+       } else if (tmp == "Abstract") {
+               ltype = 6;
+               ltype_depth = 0;
+       } else if (tmp == "Bibliography") {
+               ltype = 7;
+               ltype_depth = 0;
+       } else {
+               ltype = 0;
+               ltype_depth = 0;
+       }
                
-               /* the labelwidthstring used in lists */ 
+       /* maybe some vertical spaces */ 
                
-               /* some lines? */ 
+       /* the labelwidthstring used in lists */ 
                
-               /* some pagebreaks? */ 
+       /* some lines? */ 
                
-               /* noindent ? */ 
+       /* some pagebreaks? */ 
                
-               /* what about the alignment */ 
-       } else {
-               lyxerr << "Should this ever happen?" << endl;
-       }
-
-       for (pos_type i = 0; i < par->size(); ++i) {
-               if (!i && !noparbreak) {
+       /* noindent ? */ 
+               
+       /* what about the alignment */ 
+//     } else {
+//             lyxerr << "Should this ever happen?" << endl;
+//     }
+
+       // linelen <= 0 is special and means we don't have pargraph breaks
+       if (!noparbreak) {
+               if (linelen > 0)
+                       buffer << "\n\n";
+               for (Paragraph::depth_type j = 0; j < depth; ++j)
+                       buffer << "  ";
+               currlinelen = depth * 2;
+               //--
+               // we should probably change to the paragraph language in the
+               // gettext here (if possible) so that strings are outputted in
+               // the correct language! (20012712 Jug)
+               //--    
+               switch (ltype) {
+               case 0: // Standard
+               case 4: // (Sub)Paragraph
+               case 5: // Description
+                       break;
+               case 6: // Abstract
                        if (linelen > 0)
-                               buffer << "\n\n";
-                       for (Paragraph::depth_type j = 0; j < depth; ++j)
-                               buffer << "  ";
-                       currlinelen = depth * 2;
-                       switch (ltype) {
-                       case 0: // Standard
-                       case 4: // (Sub)Paragraph
-                       case 5: // Description
-                               break;
-                       case 6: // Abstract
+                               buffer << _("Abstract") << "\n\n";
+                       else
+                               buffer << _("Abstract: ");
+                       break;
+               case 7: // Bibliography
+                       if (!ref_printed) {
                                if (linelen > 0)
-                                       buffer << "Abstract\n\n";
+                                       buffer << _("References") << "\n\n";
                                else
-                                       buffer << "Abstract: ";
-                               break;
-                       case 7: // Bibliography
-                               if (!ref_printed) {
-                                       if (linelen > 0)
-                                               buffer << "References\n\n";
-                                       else
-                                               buffer << "References: ";
-                                       ref_printed = true;
-                               }
-                               break;
-                       default:
-                               buffer << par->params().labelString() << " ";
-                               break;
+                                       buffer << _("References: ");
+                               ref_printed = true;
                        }
-                       if (ltype_depth > depth) {
-                               for (Paragraph::depth_type j = ltype_depth - 1; 
-                                    j > depth; --j)
-                                       buffer << "  ";
-                               currlinelen += (ltype_depth-depth)*2;
+                       break;
+               default:
+                       buffer << par->params().labelString() << " ";
+                       break;
+               }
+       }
+       string s = buffer.str();
+       if (s.rfind('\n') != string::npos) {
+               string dummy;
+               s = rsplit(buffer.str().c_str(), dummy, '\n');
+       }
+       currlinelen = s.length();
+       if (!currlinelen) {
+               for (Paragraph::depth_type j = 0; j < depth; ++j)
+                       buffer << "  ";
+               currlinelen = depth * 2;
+               if (ltype_depth > depth) {
+                       for (Paragraph::depth_type j = ltype_depth;
+                                j > depth; --j)
+                       {
+                               buffer << "  ";
                        }
+                       currlinelen += (ltype_depth-depth)*2;
                }
-               
+       }
+       // this is to change the linebreak to do it by word a bit more intelligent
+       // hopefully! (only in the case where we have a max linelenght!) (Jug)
+       for (pos_type i = 0; i < par->size(); ++i) {
                char c = par->getUChar(params, i);
-               if (islatex)
-                       continue;
                switch (c) {
                case Paragraph::META_INSET:
                {
                        Inset const * inset = par->getInset(i);
                        if (inset) {
-                               if (!inset->ascii(this, buffer)) {
+                               if (linelen > 0)
+                                       buffer << word.str();
+                               if (inset->ascii(this, buffer, linelen)) {
+                                       // to be sure it breaks paragraph
+                                       currlinelen += linelen;
+                               }
+#if 0
+                               else {
                                        string dummy;
                                        string const s =
                                                rsplit(buffer.str().c_str(),
                                                       dummy, '\n');
-                                       currlinelen += s.length();
-                               } else {
-                                       // to be sure it breaks paragraph
-                                       currlinelen += linelen;
+                                       currlinelen = s.length();
                                }
+#endif
                        }
                }
                break;
                
                case Paragraph::META_NEWLINE:
                        if (linelen > 0) {
-                               buffer << "\n";
+                               buffer << word.str() << "\n";
+                               word.str("");
                                for (Paragraph::depth_type j = 0; 
                                     j < depth; ++j)
                                        buffer << "  ";
-                       }
-                       currlinelen = depth * 2;
-                       if (ltype_depth > depth) {
-                               for (Paragraph::depth_type j = ltype_depth;
-                                    j > depth; --j)
-                                       buffer << "  ";
-                               currlinelen += (ltype_depth - depth) * 2;
+                               currlinelen = depth * 2;
+                               if (ltype_depth > depth) {
+                                       for (Paragraph::depth_type j = ltype_depth;
+                                                j > depth; --j)
+                                               buffer << "  ";
+                                       currlinelen += (ltype_depth - depth) * 2;
+                               }
                        }
                        break;
                        
-               case Paragraph::META_HFILL: 
-                       buffer << "\t";
+               case Paragraph::META_HFILL:
+                       buffer << word.str() << "\t";
+                       currlinelen += word.str().length() + 1;
+                       word.str("");
                        break;
 
                default:
-                       if ((linelen > 0) && (currlinelen > (linelen - 10)) &&
-                           (c == ' ') && ((i + 2) < par->size()))
-                       {
-                               buffer << "\n";
-                               for (Paragraph::depth_type j = 0; 
-                                    j < depth; ++j)
-                                       buffer << "  ";
-                               currlinelen = depth * 2;
-                               if (ltype_depth > depth) {
-                                       for (Paragraph::depth_type j = ltype_depth;
-                                           j > depth; --j)
+                       if (c == ' ') {
+                               buffer << word.str() << ' ';
+                               currlinelen += word.str().length() + 1;
+                               word.str("");
+                       } else {
+                               if (c != '\0') {
+                                       word << c;
+                               } else {
+                                       lyxerr[Debug::INFO] <<
+                                               "writeAsciiFile: NULL char in structure." << endl;
+                               }
+                               if ((linelen > 0) &&
+                                       (currlinelen+word.str().length()) > linelen)
+                               {
+                                       buffer << "\n";
+                                       for (Paragraph::depth_type j = 0; j < depth; ++j)
                                                buffer << "  ";
-                                       currlinelen += (ltype_depth-depth)*2;
+                                       currlinelen = depth * 2;
+                                       if (ltype_depth > depth) {
+                                               for (Paragraph::depth_type j = ltype_depth;
+                                                        j > depth; --j)
+                                               {
+                                                       buffer << "  ";
+                                               }
+                                               currlinelen += (ltype_depth-depth)*2;
+                                       }
                                }
-                       } else if (c != '\0') {
-                               buffer << c;
-                               ++currlinelen;
-                       } else
-                               lyxerr[Debug::INFO] << "writeAsciiFile: NULL char in structure." << endl;
+                       }
                        break;
                }
        }
+       buffer << word.str();
        return buffer.str().c_str();
 }
 
@@ -1943,14 +2012,16 @@ void Buffer::writeFileAscii(ostream & ofs, int linelen)
 {
        Paragraph * par = paragraph;
        while (par) {
-               ofs << asciiParagraph(par, linelen);
+               ofs << asciiParagraph(par, linelen, par->previous() == 0);
                par = par->next();
        }
        ofs << "\n";
 }
 
+
 bool use_babel;
 
+
 void Buffer::makeLaTeXFile(string const & fname, 
                           string const & original_path,
                           bool nice, bool only_body)
@@ -2445,6 +2516,7 @@ void Buffer::makeLaTeXFile(string const & fname,
        }
        
        lyxerr[Debug::INFO] << "Finished making latex file." << endl;
+       lyxerr[Debug::INFO] << "Row count was " << texrow.rows()-1 << "." << endl;
 }
 
 
@@ -2976,8 +3048,8 @@ void Buffer::sgmlError(Paragraph * par, int pos,
 {
        // insert an error marker in text
        InsetError * new_inset = new InsetError(message);
-       par->insertInset(pos, new_inset);
-       par->setFont(pos, LyXFont(LyXFont::ALL_INHERIT, params.language));
+       par->insertInset(pos, new_inset, LyXFont(LyXFont::ALL_INHERIT, 
+                                                params.language));
 }