]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
Set layout informations on newly created paragraphs and don't resize InsetTexts
[lyx.git] / src / buffer.C
index ad3ac81b4540a5c2fc9eaee7f21996699707096e..1afe9c8d78cca04a004ed8fb34e7d1c301ad3eb3 100644 (file)
@@ -145,7 +145,7 @@ extern int tex_code_break_column;
 
 
 Buffer::Buffer(string const & file, bool ronly)
-       : paragraph(0), lyx_clean(true), bak_clean(true),
+       : paragraph(0), niceFile(true), lyx_clean(true), bak_clean(true),
          unnamed(false), dep_clean(0), read_only(ronly),
          filename_(file), users(0)
 {
@@ -333,6 +333,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
 
        if (!par) {
                par = new Paragraph;
+               par->layout(textclasslist[params.textclass].defaultLayoutName());
        } else {
                // We are inserting into an existing document
                users->text->breakParagraph(users);
@@ -433,7 +434,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
        static int call_depth;
        ++call_depth;
        bool checkminipage = false;
-       static Paragraph * minipar;
+       static Paragraph * minipar = 0;
        static Paragraph * parBeforeMinipage;
 #endif
 #endif
@@ -476,6 +477,9 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        lyxerr << "Layout '" << layoutname << "' does not"
                               << " exist in textclass '" << tclass.name()
                               << "'." << endl;
+                       lyxerr << "Trying to use default layout instead."
+                              << endl;
+                       layoutname = tclass.defaultLayoutName();
                }
                
 #ifndef NO_COMPABILITY
@@ -541,6 +545,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                                first_par = par;
                        else {
                                par = new Paragraph(par);
+                               par->layout(textclasslist[params.textclass].defaultLayoutName());
                        }
                        pos = 0;
                        par->layout(layoutname);
@@ -1172,6 +1177,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                the_end_read = true;
 #ifndef NO_COMPABILITY
 #ifndef NO_PEXTRA_REALLY
+               if (minipar == par)
+                       par = 0;
                minipar = parBeforeMinipage = 0;
 #endif
 #endif
@@ -1228,7 +1235,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
        if (checkminipage && (call_depth == 1)) {
        checkminipage = false;
        if (minipar && (minipar != par) &&
-           (par->params().pextraType() == Paragraph::PEXTRA_MINIPAGE)) {
+           (par->params().pextraType() == Paragraph::PEXTRA_MINIPAGE))
+       {
                lyxerr << "minipages in a row" << endl;
                if (par->params().pextraStartMinipage()) {
                        lyxerr << "start new minipage" << endl;
@@ -1264,7 +1272,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        mini->pageWidth(LyXLength(par->params().pextraWidth()));
                        if (!par->params().pextraWidthp().empty()) {
                            lyxerr << "WP:" << mini->pageWidth().asString() << endl;
-                           mini->pageWidth(LyXLength((par->params().pextraWidthp())+"%"));
+                           mini->pageWidth(LyXLength((par->params().pextraWidthp())+"p%"));
                        }
                        Paragraph * op = mini->firstParagraph();
                        mini->inset.paragraph(par);
@@ -1345,9 +1353,19 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                mini->pageWidth(LyXLength(minipar->params().pextraWidth()));
                if (!par->params().pextraWidthp().empty()) {
                    lyxerr << "WP:" << mini->pageWidth().asString() << endl;
-                   mini->pageWidth(LyXLength((par->params().pextraWidthp())+"%"));
+                   mini->pageWidth(LyXLength((par->params().pextraWidthp())+"p%"));
                }
+
+               Paragraph * op = mini->firstParagraph();
                mini->inset.paragraph(minipar);
+               //
+               // and free the old ones!
+               //
+               while(op) {
+                       Paragraph * pp = op->next();
+                       delete op;
+                       op = pp;
+               }
                        
                // Insert the minipage last in the
                // previous paragraph.
@@ -1772,16 +1790,27 @@ bool Buffer::writeFile(string const & fname, bool flag) const
 }
 
 
+namespace {
+
+pair<int, string> const addDepth(int depth, int ldepth)
+{
+       int d = depth * 2;
+       if (ldepth > depth)
+               d += (ldepth - depth) * 2;
+       return make_pair(d, string(d, ' '));
+}
+
+}
+
+
 string const Buffer::asciiParagraph(Paragraph const * par,
                                     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;
 //     if (!par->previous()) {
 #if 0
@@ -1845,12 +1874,16 @@ string const Buffer::asciiParagraph(Paragraph const * par,
 //     }
 
        // linelen <= 0 is special and means we don't have pargraph breaks
+
+       string::size_type currlinelen = 0;
+
        if (!noparbreak) {
                if (linelen > 0)
                        buffer << "\n\n";
-               for (Paragraph::depth_type j = 0; j < depth; ++j)
-                       buffer << "  ";
-               currlinelen = depth * 2;
+
+               buffer << string(depth * 2, ' ');
+               currlinelen += depth * 2;
+
                //--
                // we should probably change to the paragraph language in the
                // gettext here (if possible) so that strings are outputted in
@@ -1862,46 +1895,52 @@ string const Buffer::asciiParagraph(Paragraph const * par,
                case 5: // Description
                        break;
                case 6: // Abstract
-                       if (linelen > 0)
+                       if (linelen > 0) {
                                buffer << _("Abstract") << "\n\n";
-                       else
-                               buffer << _("Abstract: ");
+                               currlinelen = 0;
+                       } else {
+                               string const abst = _("Abstract: ");
+                               buffer << abst;
+                               currlinelen += abst.length();
+                       }
                        break;
                case 7: // Bibliography
                        if (!ref_printed) {
-                               if (linelen > 0)
+                               if (linelen > 0) {
                                        buffer << _("References") << "\n\n";
-                               else
-                                       buffer << _("References: ");
+                                       currlinelen = 0;
+                               } else {
+                                       string const refs = _("References: ");
+                                       buffer << refs;
+                                       currlinelen += refs.length();
+                               }
+                               
                                ref_printed = true;
                        }
                        break;
                default:
-                       buffer << par->params().labelString() << " ";
-                       break;
+               {
+                       string const parlab = par->params().labelString();
+                       buffer << parlab << " ";
+                       currlinelen += parlab.length() + 1;
+               }
+               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;
-               }
+               pair<int, string> p = addDepth(depth, ltype_depth);
+               buffer << p.second;
+               currlinelen += p.first;
        }
-       // 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)
+       
+       // 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)
+
+       string word;
+       
        for (pos_type i = 0; i < par->size(); ++i) {
                char c = par->getUChar(params, i);
                switch (c) {
@@ -1910,82 +1949,73 @@ string const Buffer::asciiParagraph(Paragraph const * par,
                        Inset const * inset = par->getInset(i);
                        if (inset) {
                                if (linelen > 0) {
-                                       buffer << word.str();
-                                       word.str("");
+                                       buffer << word;
+                                       currlinelen += word.length();
+                                       word.erase();
                                }
                                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();
-                               }
-#endif
                        }
                }
                break;
                
                case Paragraph::META_NEWLINE:
                        if (linelen > 0) {
-                               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;
-                               }
+                               buffer << word << "\n";
+                               word.erase();
+
+                               pair<int, string> p = addDepth(depth,
+                                                              ltype_depth);
+                               buffer << p.second;
+                               currlinelen = p.first;
                        }
                        break;
                        
                case Paragraph::META_HFILL:
-                       buffer << word.str() << "\t";
-                       currlinelen += word.str().length() + 1;
-                       word.str("");
+                       buffer << word << "\t";
+                       currlinelen += word.length() + 1;
+                       word.erase();
                        break;
 
                default:
                        if (c == ' ') {
-                               buffer << word.str() << ' ';
-                               currlinelen += word.str().length() + 1;
-                               word.str("");
+                               if (linelen > 0 &&
+                                   currlinelen + word.length() > linelen - 10) {
+                                       buffer << "\n";
+                                       pair<int, string> p =
+                                               addDepth(depth, ltype_depth);
+                                       buffer << p.second;
+                                       currlinelen = p.first;
+                               }
+
+                               buffer << word << ' ';
+                               currlinelen += word.length() + 1;
+                               word.erase();
+
                        } else {
                                if (c != '\0') {
-                                       word << c;
+                                       word.push_back(c);
                                } else {
                                        lyxerr[Debug::INFO] <<
                                                "writeAsciiFile: NULL char in structure." << endl;
                                }
                                if ((linelen > 0) &&
-                                       (currlinelen+word.str().length()) > linelen)
+                                       (currlinelen + word.length()) > linelen)
                                {
                                        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)
-                                               {
-                                                       buffer << "  ";
-                                               }
-                                               currlinelen += (ltype_depth-depth)*2;
-                                       }
+
+                                       pair<int, string> p =
+                                               addDepth(depth, ltype_depth);
+                                       buffer << p.second;
+                                       currlinelen = p.first;
                                }
                        }
                        break;
                }
        }
-       buffer << word.str();
+       buffer << word;
        return buffer.str().c_str();
 }
 
@@ -2021,16 +2051,16 @@ void Buffer::makeLaTeXFile(string const & fname,
 {
        lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
        
-       niceFile = nice; // this will be used by Insetincludes.
-
-       tex_code_break_column = lyxrc.ascii_linelen;
-
        ofstream ofs(fname.c_str());
        if (!ofs) {
                Alert::err_alert(_("Error: Cannot open file: "), fname);
                return;
        }
        
+       niceFile = nice; // this will be used by Insetincludes.
+
+       tex_code_break_column = lyxrc.ascii_linelen;
+
        // validate the buffer.
        lyxerr[Debug::LATEX] << "  Validating buffer..." << endl;
        LaTeXFeatures features(params);
@@ -2068,9 +2098,11 @@ void Buffer::makeLaTeXFile(string const & fname,
                        texrow.newline();
                }
                if (!original_path.empty()) {
+                       string inputpath = os::external_path(original_path);
+                       subst(inputpath, "~", "\\string~");
                        ofs << "\\makeatletter\n"
                            << "\\def\\input@path{{"
-                           << os::external_path(original_path) << "/}}\n"
+                           << inputpath << "/}}\n"
                            << "\\makeatother\n";
                        texrow.newline();
                        texrow.newline();
@@ -2503,6 +2535,9 @@ void Buffer::makeLaTeXFile(string const & fname,
        
        lyxerr[Debug::INFO] << "Finished making latex file." << endl;
        lyxerr[Debug::INFO] << "Row count was " << texrow.rows()-1 << "." << endl;
+
+       // we want this to be true outside previews (for insetexternal)
+       niceFile = true;
 }
 
 
@@ -2786,6 +2821,9 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
 
        ofs.close();
        // How to check for successful close
+
+       // we want this to be true outside previews (for insetexternal)
+       niceFile = true;
 }
 
 
@@ -3346,6 +3384,9 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
 
        ofs.close();
        // How to check for successful close
+
+       // we want this to be true outside previews (for insetexternal)
+       niceFile = true;
 }
 
 
@@ -3417,7 +3458,7 @@ void Buffer::simpleDocBookOnePar(ostream & os,
        // resets description flag correctly
        if (desc_on == 1) {
                // <term> not closed...
-               os << "</term>";
+               os << "</term>\n<listitem><para>&nbsp;</para>";
        }
        if (style.free_spacing) os << '\n';
 }
@@ -3574,8 +3615,8 @@ Buffer::Lists const Buffer::getLists() const
        Paragraph * par = paragraph;
 
        LyXTextClass const & textclass = textclasslist[params.textclass];
-       bool found = textclass.hasLayout("caption");
-       string const layout("caption");
+       bool found = textclass.hasLayout("Caption");
+       string const layout("Caption");
 
        while (par) {
                char const labeltype = textclass[par->layout()].labeltype;
@@ -3621,7 +3662,7 @@ Buffer::Lists const Buffer::getLists() const
                                }
                        }
                } else {
-                       lyxerr << "caption not found" << endl;
+                       lyxerr << "Caption not found" << endl;
                }
                
                par = par->next();