]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
More fixes to the autocollapsing of paragraphs.
[lyx.git] / src / buffer.C
index ac4a50196ad8d47796a400ab1617842418568eb0..7ae0873b3715c7cc0c0819677febdeb8b9b85a50 100644 (file)
 
 #include <config.h>
 
-#include <fstream>
-#include <iomanip>
-#include <map>
-#include <stack>
-#include <list>
-
-#include <cstdlib>
-#include <cmath>
-#include <unistd.h>
-#include <sys/types.h>
-#include <utime.h>
-
-#include <algorithm>
-
-#ifdef HAVE_LOCALE
-#include <locale>
-#endif
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 #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 "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 <unistd.h>
+#include <sys/types.h>
+#include <utime.h>
+
+
+#ifdef HAVE_LOCALE
+#include <locale>
+#endif
+
 
 using std::ostream;
 using std::ofstream;
@@ -123,6 +128,10 @@ using std::set;
 using std::stack;
 using std::list;
 
+using lyx::pos_type;
+using lyx::layout_type;
+using lyx::textclass_type;
+
 // all these externs should eventually be removed.
 extern BufferList bufferlist;
 
@@ -184,7 +193,7 @@ Buffer::~Buffer()
 
 string const Buffer::getLatexName(bool no_path) const
 {
-       string name = ChangeExtension(MakeLatexName(filename), ".tex");
+       string const name = ChangeExtension(MakeLatexName(filename), ".tex");
        if (no_path)
                return OnlyFilename(name);
        else
@@ -241,7 +250,8 @@ void Buffer::setReadonly(bool flag)
 // Should work on a list
 void Buffer::updateTitles() const
 {
-       if (users) users->owner()->updateWindowTitle();
+       if (users)
+               users->owner()->updateWindowTitle();
 }
 
 
@@ -249,7 +259,8 @@ void Buffer::updateTitles() const
 // Should work on a list
 void Buffer::resetAutosaveTimers() const
 {
-       if (users) users->owner()->resetAutosaveTimer();
+       if (users)
+               users->owner()->resetAutosaveTimer();
 }
 
 
@@ -384,12 +395,14 @@ bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
 
 #ifndef NO_COMPABILITY
 void Buffer::insertErtContents(Paragraph * par, int & pos,
-                              LyXFont const & font, bool set_inactive) 
+                       LyXFont const & f, bool set_inactive) 
 {
        if (!ert_comp.contents.empty()) {
-               lyxerr[Debug::INSETS] << "ERT contents:\n"
-                      << ert_comp.contents << endl;
+               lyxerr[Debug::INSETS] << "ERT contents:\n'"
+                                     << ert_comp.contents << "'" << endl;
                Inset * inset = new InsetERT(ert_comp.contents, true);
+               LyXFont font;
+               font.setLanguage(f.language());
                par->insertInset(pos++, inset, font);
                ert_comp.contents.erase();
        }
@@ -445,11 +458,10 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                // Do the insetert.
                insertErtContents(par, pos, font);
 #endif
-                lex.eatLine();
-                string const layoutname = lex.getString();
-                pair<bool, LyXTextClass::LayoutList::size_type> pp
-                        = textclasslist.NumberOfLayout(params.textclass,
-                                                       layoutname);
+               lex.eatLine();
+               string const layoutname = lex.getString();
+               pair<bool, layout_type> pp
+                 = textclasslist.NumberOfLayout(params.textclass, layoutname);
 
 #ifndef NO_COMPABILITY
                if (compare_no_case(layoutname, "latex") == 0) {
@@ -530,9 +542,9 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                                        + 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 =
@@ -660,7 +672,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                params.readPreamble(lex);
        } else if (token == "\\textclass") {
                lex.eatLine();
-               pair<bool, LyXTextClassList::size_type> pp = 
+               pair<bool, textclass_type> pp = 
                        textclasslist.NumberOfClass(lex.getString());
                if (pp.first) {
                        params.textclass = pp.second;
@@ -1063,7 +1075,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                                            par->getLayout());
 
                // Insets don't make sense in a free-spacing context! ---Kayvan
-               if (layout.free_spacing) {
+               if (layout.free_spacing || par->isFreeSpacing()) {
                        if (lex.isOK()) {
                                lex.next();
                                string next_token = lex.getString();
@@ -1121,7 +1133,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        textclasslist.Style(params.textclass, 
                                            par->getLayout());
 
-               if (layout.free_spacing) {
+               if (layout.free_spacing || par->isFreeSpacing()) {
                        par->insertChar(pos, ' ', font);
                } else {
                        Inset * inset = new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
@@ -1170,9 +1182,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
                }
@@ -1338,7 +1349,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 }
 
 // needed to insert the selection
-void Buffer::insertStringAsLines(Paragraph *& par, Paragraph::pos_type & pos,
+void Buffer::insertStringAsLines(Paragraph *& par, pos_type & pos,
                                  LyXFont const & fn,string const & str) const
 {
        LyXLayout const & layout = textclasslist.Style(params.textclass, 
@@ -1362,19 +1373,19 @@ void Buffer::insertStringAsLines(Paragraph *& par, Paragraph::pos_type & pos,
                        }
                        // do not insert consecutive spaces if !free_spacing
                } else if ((*cit == ' ' || *cit == '\t') &&
-                          space_inserted && !layout.free_spacing)
+                          space_inserted && !layout.free_spacing &&
+                                  !par->isFreeSpacing())
                {
                        continue;
                } else if (*cit == '\t') {
-                       if (!layout.free_spacing) {
+                       if (!layout.free_spacing && !par->isFreeSpacing()) {
                                // tabs are like spaces here
                                par->insertChar(pos, ' ', font);
                                ++pos;
                                space_inserted = true;
                        } else {
-                               const Paragraph::pos_type nb = 8 - pos % 8;
-                               for (Paragraph::pos_type a = 0; 
-                                    a < nb ; ++a) {
+                               const pos_type nb = 8 - pos % 8;
+                               for (pos_type a = 0; a < nb ; ++a) {
                                        par->insertChar(pos, ' ', font);
                                        ++pos;
                                }
@@ -1812,7 +1823,7 @@ string const Buffer::asciiParagraph(Paragraph const * par,
                lyxerr << "Should this ever happen?" << endl;
        }
 
-       for (Paragraph::pos_type i = 0; i < par->size(); ++i) {
+       for (pos_type i = 0; i < par->size(); ++i) {
                if (!i && !noparbreak) {
                        if (linelen > 0)
                                buffer << "\n\n";
@@ -2437,6 +2448,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;
 }
 
 
@@ -2775,7 +2787,7 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
 {
        LyXLayout const & style = textclasslist.Style(params.textclass,
                                                      par->getLayout());
-        string::size_type char_line_count = 5;     // Heuristic choice ;-) 
+       string::size_type char_line_count = 5;     // Heuristic choice ;-) 
 
        // gets paragraph main font
        LyXFont font_old;
@@ -2795,7 +2807,7 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
 
        stack<PAR_TAG> tag_state;
        // parsing main loop
-       for (Paragraph::pos_type i = 0; i < par->size(); ++i) {
+       for (pos_type i = 0; i < par->size(); ++i) {
 
                PAR_TAG tag_close = NONE;
                list < PAR_TAG > tag_open;
@@ -2887,7 +2899,7 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
                }
 
                list < PAR_TAG > temp;
-               while(!tag_state.empty() && tag_close ) {
+               while (!tag_state.empty() && tag_close ) {
                        PAR_TAG k =  tag_state.top();
                        tag_state.pop();
                        os << "</" << tag_name(k) << ">";
@@ -2926,7 +2938,8 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
                } else {
                        string sgml_string;
                        if (par->sgmlConvertChar(c, sgml_string)
-                           && !style.free_spacing) { 
+                           && !style.free_spacing && !par->isFreeSpacing())
+                       { 
                                // in freespacing mode, spaces are
                                // non-breaking characters
                                if (desc_on) {// if char is ' ' then...
@@ -2967,8 +2980,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));
 }
 
 
@@ -3122,6 +3135,8 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
 
                        // treat label as a special case for
                        // more WYSIWYM handling.
+                       // This is a hack while paragraphs can't have
+                       // attributes, like id in this case.
                        if (par->isInset(0)) {
                                Inset * inset = par->getInset(0);
                                Inset::Code lyx_code = inset->lyxCode();
@@ -3288,8 +3303,7 @@ void Buffer::simpleDocBookOnePar(ostream & os,
        //      os << string(depth,' ');
 
        // parsing main loop
-       for (Paragraph::pos_type i = 0;
-            i < par->size(); ++i) {
+       for (pos_type i = 0; i < par->size(); ++i) {
                LyXFont font = par->getFont(params, i);
 
                // handle <emphasis> tag
@@ -3303,18 +3317,20 @@ void Buffer::simpleDocBookOnePar(ostream & os,
                        }
                }
       
-               char c = par->getChar(i);
 
-               if (c == Paragraph::META_INSET) {
+               if ( par->isInset(i) ) {
                        Inset * inset = par->getInset(i);
-                       inset->docbook(this, os);
+                       // don't print the inset in position 0 if desc_on == 3 (label)
+                       if ( i || desc_on != 3)
+                               inset->docbook(this, os);
                } else {
+                       char c = par->getChar(i);
                        string sgml_string;
                        par->sgmlConvertChar(c, sgml_string);
 
                        if (style.pass_thru) {
                                os << c;
-                       } else if(style.free_spacing || c != ' ') {
+                       } else if(style.free_spacing || par->isFreeSpacing() || c != ' ') {
                                        os << sgml_string;
                        } else if (desc_on ==1) {
                                ++char_line_count;
@@ -3492,16 +3508,16 @@ Buffer::Lists const Buffer::getLists() const
        Paragraph * par = paragraph;
 
 #if 1
-       std::pair<bool, LyXTextClassList::size_type> const tmp =
+       std::pair<bool, textclass_type> const tmp =
                textclasslist.NumberOfLayout(params.textclass, "Caption");
        bool const found = tmp.first;
-       LyXTextClassList::size_type const cap = tmp.second;
+       textclass_type const cap = tmp.second;
        
 #else
        // This is the prefered way to to this, but boost::tie can break
        // some compilers
        bool found;
-       LyXTextClassList::size_type cap;
+       textclass_type cap;
        boost::tie(found, cap) = textclasslist
                .NumberOfLayout(params.textclass, "Caption");
 #endif
@@ -3698,8 +3714,7 @@ bool Buffer::isMultiLingual()
 }
 
 
-Buffer::inset_iterator::inset_iterator(Paragraph * paragraph,
-                                      Paragraph::pos_type pos)
+Buffer::inset_iterator::inset_iterator(Paragraph * paragraph, pos_type pos)
        : par(paragraph)
 {
        it = par->InsetIterator(pos);