]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
More fixes to the autocollapsing of paragraphs.
[lyx.git] / src / buffer.C
index 437ae91b6d5967c8c7affe4b452d3c4f74be466a..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 "lyx_gui_misc.h"      // WarnReadonly()
-#include "frontends/Dialogs.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
@@ -234,9 +243,6 @@ void Buffer::setReadonly(bool flag)
                updateTitles();
                users->owner()->getDialogs()->updateBufferDependent(false);
        }
-       if (read_only) {
-               WarnReadonly(filename);
-       }
 }
 
 
@@ -244,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();
 }
 
 
@@ -252,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();
 }
 
 
@@ -365,7 +373,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
                        s += tostr(unknown_layouts);
                        s += _(" paragraphs");
                }
-               WriteAlert(_("Textclass Loading Error!"), s,
+               Alert::alert(_("Textclass Loading Error!"), s,
                           _("When reading " + fileName()));
        }
 
@@ -377,7 +385,7 @@ bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
                        s += tostr(unknown_tokens);
                        s += _(" unknown tokens");
                }
-               WriteAlert(_("Textclass Loading Error!"), s,
+               Alert::alert(_("Textclass Loading Error!"), s,
                           _("When reading " + fileName()));
        }
 
@@ -387,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();
        }
@@ -448,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) {
@@ -533,7 +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->insertInset(0, new_inset, 
+                                                LyXFont(LyXFont::ALL_INHERIT,
+                                                        params.language));
                        }
                        // Test whether the layout is obsolete.
                        LyXLayout const & layout =
@@ -661,24 +672,24 @@ 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;
                } else {
-                       WriteAlert(string(_("Textclass error")), 
+                       Alert::alert(string(_("Textclass error")), 
                                string(_("The document uses an unknown textclass \"")) + 
                                lex.getString() + string("\"."),
                                string(_("LyX will not be able to produce output correctly.")));
                        params.textclass = 0;
                }
                if (!textclasslist.Load(params.textclass)) {
-                               // if the textclass wasn't loaded properly
-                               // we need to either substitute another
-                               // or stop loading the file.
-                               // I can substitute but I don't see how I can
-                               // stop loading... ideas??  ARRae980418
-                       WriteAlert(_("Textclass Loading Error!"),
+                       // if the textclass wasn't loaded properly
+                       // we need to either substitute another
+                       // or stop loading the file.
+                       // I can substitute but I don't see how I can
+                       // stop loading... ideas??  ARRae980418
+                       Alert::alert(_("Textclass Loading Error!"),
                                   string(_("Can't load textclass ")) +
                                   textclasslist.NameOfClass(params.textclass),
                                   _("-- substituting default"));
@@ -1064,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();
@@ -1122,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);
@@ -1171,7 +1182,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
                        + " " + lex.text()  + "\n";
 
                InsetError * new_inset = new InsetError(s);
-               par->insertInset(pos, new_inset);
+               par->insertInset(pos, new_inset, LyXFont(LyXFont::ALL_INHERIT,
+                                                        params.language));
 
 #ifndef NO_COMPABILITY
                }
@@ -1337,7 +1349,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
 }
 
 // needed to insert the selection
-void Buffer::insertStringAsLines(Paragraph *& par, Paragraph::size_type & pos,
+void Buffer::insertStringAsLines(Paragraph *& par, pos_type & pos,
                                  LyXFont const & fn,string const & str) const
 {
        LyXLayout const & layout = textclasslist.Style(params.textclass, 
@@ -1361,19 +1373,19 @@ void Buffer::insertStringAsLines(Paragraph *& par, Paragraph::size_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::size_type nb = 8 - pos % 8;
-                               for (Paragraph::size_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;
                                }
@@ -1537,14 +1549,14 @@ bool Buffer::readFile(LyXLex & lex, Paragraph * par)
                                // current format
                        } else if (file_format > LYX_FORMAT) {
                                // future format
-                               WriteAlert(_("Warning!"),
+                               Alert::alert(_("Warning!"),
                                           _("LyX file format is newer that what"),
                                           _("is supported in this LyX version. Expect some problems."));
                                
                        } else if (file_format < LYX_FORMAT) {
                                // old formats
                                if (file_format < 200) {
-                                       WriteAlert(_("ERROR!"),
+                                       Alert::alert(_("ERROR!"),
                                                   _("Old LyX file format found. "
                                                     "Use LyX 0.10.x to read this!"));
                                        return false;
@@ -1556,16 +1568,17 @@ bool Buffer::readFile(LyXLex & lex, Paragraph * par)
                        if (file_format < 213)
                                the_end = true;
 
-                       if (!the_end)
-                               WriteAlert(_("Warning!"),
+                       if (!the_end) {
+                               Alert::alert(_("Warning!"),
                                           _("Reading of document is not complete"),
                                           _("Maybe the document is truncated"));
+                       }
                        return true;
                } else { // "\\lyxformat" not found
-                       WriteAlert(_("ERROR!"), _("Not a LyX file!"));
+                       Alert::alert(_("ERROR!"), _("Not a LyX file!"));
                }
        } else
-               WriteAlert(_("ERROR!"), _("Unable to read file!"));
+               Alert::alert(_("ERROR!"), _("Unable to read file!"));
        return false;
 }
                    
@@ -1661,7 +1674,7 @@ bool Buffer::writeFile(string const & fname, bool flag) const
                        lyxerr << _("Error! Document is read-only: ")
                               << fname << endl;
                else
-                       WriteAlert(_("Error! Document is read-only: "),
+                       Alert::alert(_("Error! Document is read-only: "),
                                   fname);
                return false;
        }
@@ -1674,7 +1687,7 @@ bool Buffer::writeFile(string const & fname, bool flag) const
                        lyxerr << _("Error! Cannot write file: ")
                               << fname << endl;
                else
-                       WriteFSAlert(_("Error! Cannot write file: "),
+                       Alert::err_alert(_("Error! Cannot write file: "),
                                     fname);
                return false;
        }
@@ -1685,7 +1698,7 @@ bool Buffer::writeFile(string const & fname, bool flag) const
                        lyxerr << _("Error! Cannot open file: ")
                               << fname << endl;
                else
-                       WriteFSAlert(_("Error! Cannot open file: "),
+                       Alert::err_alert(_("Error! Cannot open file: "),
                                     fname);
                return false;
        }
@@ -1810,7 +1823,7 @@ string const Buffer::asciiParagraph(Paragraph const * par,
                lyxerr << "Should this ever happen?" << endl;
        }
 
-       for (Paragraph::size_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";
@@ -1922,7 +1935,7 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
 {
        ofstream ofs(fname.c_str());
        if (!ofs) {
-               WriteFSAlert(_("Error: Cannot write file:"), fname);
+               Alert::err_alert(_("Error: Cannot write file:"), fname);
                return;
        }
        writeFileAscii(ofs, linelen);
@@ -1956,7 +1969,7 @@ void Buffer::makeLaTeXFile(string const & fname,
 
        ofstream ofs(fname.c_str());
        if (!ofs) {
-               WriteFSAlert(_("Error: Cannot open file: "), fname);
+               Alert::err_alert(_("Error: Cannot open file: "), fname);
                return;
        }
        
@@ -2158,8 +2171,6 @@ void Buffer::makeLaTeXFile(string const & fname,
                        ofs << "\\usepackage{geometry}\n";
                        texrow.newline();
                        ofs << "\\geometry{verbose";
-                       if (params.sides == LyXTextClass::TwoSides)
-                               ofs << ",twoside";
                        if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
                                ofs << ",landscape";
                        switch (params.papersize2) {
@@ -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;
 }
 
 
@@ -2537,7 +2549,7 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
        ofstream ofs(fname.c_str());
 
        if (!ofs) {
-               WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
+               Alert::alert(_("LYX_ERROR:"), _("Cannot write file"), fname);
                return;
        }
 
@@ -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::size_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,7 +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->insertInset(pos, new_inset, LyXFont(LyXFont::ALL_INHERIT, 
+                                                params.language));
 }
 
 
@@ -2975,7 +2989,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
 {
        ofstream ofs(fname.c_str());
        if (!ofs) {
-               WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
+               Alert::alert(_("LYX_ERROR:"), _("Cannot write file"), fname);
                return;
        }
 
@@ -3121,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();
@@ -3287,8 +3303,7 @@ void Buffer::simpleDocBookOnePar(ostream & os,
        //      os << string(depth,' ');
 
        // parsing main loop
-       for (Paragraph::size_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
@@ -3302,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;
@@ -3371,7 +3388,7 @@ int Buffer::runChktex()
        int res = chktex.run(terr); // run chktex
 
        if (res == -1) {
-               WriteAlert(_("chktex did not work!"),
+               Alert::alert(_("chktex did not work!"),
                           _("Could not run with file:"), name);
        } else if (res > 0) {
                // Insert all errors as errors boxes
@@ -3491,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
@@ -3697,8 +3714,7 @@ bool Buffer::isMultiLingual()
 }
 
 
-Buffer::inset_iterator::inset_iterator(Paragraph * paragraph,
-                                      Paragraph::size_type pos)
+Buffer::inset_iterator::inset_iterator(Paragraph * paragraph, pos_type pos)
        : par(paragraph)
 {
        it = par->InsetIterator(pos);