]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
Move #includes out of header files.
[lyx.git] / src / buffer.C
index d242264a1f2a8156b4613febfdeee4c22dde0df5..39528ce6b2052257d4a9b3cc64d6666b015ee8d2 100644 (file)
@@ -5,7 +5,7 @@
  *
  * \author Lars Gullik Bjønnes
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
@@ -168,8 +168,8 @@ Buffer::~Buffer()
 
        paragraphs.clear();
 
-       // Remove any previewed LaTeX snippets assocoated with this buffer.
-       lyx::graphics::Previews::get().removeLoader(this);
+       // Remove any previewed LaTeX snippets associated with this buffer.
+       lyx::graphics::Previews::get().removeLoader(*this);
 }
 
 
@@ -246,7 +246,7 @@ void unknownClass(string const & unknown)
 {
        Alert::warning(_("Unknown document class"),
                bformat(_("Using the default document class, because the "
-                       " class %1$s is unknown."), unknown));
+                       "class %1$s is unknown."), unknown));
 }
 
 } // anon
@@ -295,7 +295,7 @@ int Buffer::readHeader(LyXLex & lex)
 // changed to be public and have one parameter
 // if par = 0 normal behavior
 // else insert behavior
-// Returns false if "\the_end" is not read (Asger)
+// Returns false if "\end_document" is not read (Asger)
 bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
 {
        Paragraph::depth_type depth = 0;
@@ -329,7 +329,7 @@ bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
                lyxerr[Debug::PARSER] << "Handling token: `"
                                      << token << '\'' << endl;
 
-               if (token == "\\the_end") {
+               if (token == "\\end_document") {
                        the_end_read = true;
                        continue;
                }
@@ -348,7 +348,7 @@ int Buffer::readParagraph(LyXLex & lex, string const & token,
        static Change current_change;
        int unknown = 0;
 
-       if (token == "\\layout") {
+       if (token == "\\begin_layout") {
                lex.pushToken(token);
 
                Paragraph par;
@@ -440,9 +440,15 @@ void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
 }
 
 
-bool Buffer::readFile(LyXLex & lex, string const & filename)
+bool Buffer::readFile(string const & filename)
 {
-       bool ret = readFile(lex, filename, paragraphs.begin());
+       // Check if the file is compressed.
+       string const format = getExtFromContents(filename);
+       if (format == "gzip" || format == "zip" || format == "compress") {
+               params.compressed = true;
+       }
+
+       bool ret = readFile(filename, paragraphs.begin());
 
        // After we have read a file, we must ensure that the buffer
        // language is set and used in the gui.
@@ -453,13 +459,23 @@ bool Buffer::readFile(LyXLex & lex, string const & filename)
 }
 
 
-// FIXME: all the below Alerts should give the filename..
+bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
+{
+       LyXLex lex(0, 0);
+       lex.setFile(filename);
+
+       return readFile(lex, filename, pit);
+}
+
+
 bool Buffer::readFile(LyXLex & lex, string const & filename,
                      ParagraphList::iterator pit)
 {
+       Assert(!filename.empty());
+
        if (!lex.isOK()) {
                Alert::error(_("Document could not be read"),
-                       _("The specified document could not be read."));
+                            bformat(_("%1$s could not be read."), filename));
                return false;
        }
 
@@ -468,14 +484,17 @@ bool Buffer::readFile(LyXLex & lex, string const & filename,
 
        if (!lex.isOK()) {
                Alert::error(_("Document could not be read"),
-                       _("The specified document could not be read."));
+                            bformat(_("%1$s could not be read."), filename));
                return false;
        }
 
        // the first token _must_ be...
        if (token != "\\lyxformat") {
+               lyxerr << "Token: " << token << endl;
+
                Alert::error(_("Document format failure"),
-                       _("The specified document is not a LyX document."));
+                            bformat(_("%1$s is not a LyX document."),
+                                      filename));
                return false;
        }
 
@@ -487,64 +506,58 @@ bool Buffer::readFile(LyXLex & lex, string const & filename,
        //lyxerr << "           dot found at " << dot << endl;
        if (dot != string::npos)
                        tmp_format.erase(dot, 1);
-       file_format = strToInt(tmp_format);
+       int file_format = strToInt(tmp_format);
        //lyxerr << "format: " << file_format << endl;
-       if (file_format == LYX_FORMAT) {
-               // current format
-       } else if (file_format > LYX_FORMAT) {
+
+       if (file_format > LYX_FORMAT) {
                Alert::warning(_("Document format failure"),
-                       _("This document was created with a newer version of "
-                       "LyX. This is likely to cause problems."));
+                              bformat(_("%1$s was created with a newer"
+                                        " version of LyX. This is likely to"
+                                        " cause problems."),
+                                        filename));
        } else if (file_format < LYX_FORMAT) {
-               // old formats
-               if (file_format < 200) {
-                       Alert::error(_("Document format failure"),
-                               _("This LyX document is too old to be read "
-                               "by this version of LyX. Try LyX 0.10."));
+               string const tmpfile = tempName();
+               string command = LibFileSearch("lyx2lyx", "lyx2lyx");
+               if (command.empty()) {
+                       Alert::error(_("Conversion script not found"),
+                                    bformat(_("%1$s is from an earlier"
+                                              " version of LyX, but the"
+                                              " conversion script lyx2lyx"
+                                              " could not be found."),
+                                              filename));
                        return false;
-               } else if (!filename.empty()) {
-                       string command =
-                               LibFileSearch("lyx2lyx", "lyx2lyx");
-                       if (command.empty()) {
-                               Alert::error(_("Conversion script not found"),
-                                       _("The document is from an earlier version "
-                                         "of LyX, but the conversion script lyx2lyx "
-                                         "could not be found."));
-                               return false;
-                       }
-                       command += " -t"
-                               +tostr(LYX_FORMAT) + ' '
-                               + QuoteName(filename);
-                       lyxerr[Debug::INFO] << "Running '"
-                                           << command << '\''
-                                           << endl;
-                       cmd_ret const ret = RunCommand(command);
-                       if (ret.first) {
-                               Alert::error(_("Conversion script failed"),
-                                       _("The document is from an earlier version "
-                                         "of LyX, but the lyx2lyx script failed "
-                                         "to convert it."));
-                               return false;
-                       }
-                       istringstream is(STRCONV(ret.second));
-                       LyXLex tmplex(0, 0);
-                       tmplex.setStream(is);
-                       return readFile(tmplex, string(), pit);
-               } else {
-                       // This code is reached if lyx2lyx failed (for
-                       // some reason) to change the file format of
-                       // the file.
-                       Assert(false);
+               }
+               command += " -t"
+                       + tostr(LYX_FORMAT)
+                       + " -o " + tmpfile + ' '
+                       + QuoteName(filename);
+               lyxerr[Debug::INFO] << "Running '"
+                                   << command << '\''
+                                   << endl;
+               cmd_ret const ret = RunCommand(command);
+               if (ret.first != 0) {
+                       Alert::error(_("Conversion script failed"),
+                                    bformat(_("%1$s is from an earlier version"
+                                             " of LyX, but the lyx2lyx script"
+                                             " failed to convert it."),
+                                             filename));
                        return false;
+               } else {
+                       bool ret = readFile(tmpfile, pit);
+                       // Do stuff with tmpfile name and buffer name here.
+                       return ret;
                }
+
        }
+
        bool the_end = readBody(lex, pit);
        params.setPaperStuff();
 
        if (!the_end) {
                Alert::error(_("Document format failure"),
-                       _("The document ended unexpectedly, which means "
-                         "that it is probably corrupted."));
+                            bformat(_("%1$s ended unexpectedly, which means"
+                                      " that it is probably corrupted."),
+                                      filename));
        }
        return true;
 }
@@ -637,23 +650,25 @@ bool Buffer::writeFile(string const & fname) const
                return false;
        }
 
-       bool const compressed = (fname.substr(fname.size() - 3, 3) == ".gz");
+       bool retval;
 
-       if (compressed) {
+       if (params.compressed) {
                gz::ogzstream ofs(fname.c_str());
 
                if (!ofs)
                        return false;
 
-               return do_writeFile(ofs);
+               retval = do_writeFile(ofs);
 
-       }
+       } else {
+               ofstream ofs(fname.c_str());
+               if (!ofs)
+                       return false;
 
-       ofstream ofs(fname.c_str());
-       if (!ofs)
-               return false;
+               retval = do_writeFile(ofs);
+       }
 
-       return do_writeFile(ofs);
+       return retval;
 }
 
 
@@ -668,7 +683,7 @@ bool Buffer::do_writeFile(ostream & ofs) const
        // The top of the file should not be written by params.
 
        // write out a comment in the top of the file
-       ofs << '#' << lyx_docversion
+       ofs << "#LyX " << lyx_version
            << " created this file. For more info see http://www.lyx.org/\n"
            << "\\lyxformat " << LYX_FORMAT << "\n";
 
@@ -684,10 +699,10 @@ bool Buffer::do_writeFile(ostream & ofs) const
        ParagraphList::const_iterator pit = paragraphs.begin();
        ParagraphList::const_iterator pend = paragraphs.end();
        for (; pit != pend; ++pit)
-               pit->write(this, ofs, params, depth);
+               pit->write(*this, ofs, params, depth);
 
        // Write marker that shows file is complete
-       ofs << "\n\\the_end" << endl;
+       ofs << "\n\\end_document" << endl;
 
        // Shouldn't really be needed....
        //ofs.close();
@@ -860,7 +875,7 @@ string const Buffer::asciiParagraph(Paragraph const & par,
                                        currlinelen += word.length();
                                        word.erase();
                                }
-                               if (inset->ascii(this, buffer, linelen)) {
+                               if (inset->ascii(*this, buffer, linelen)) {
                                        // to be sure it breaks paragraph
                                        currlinelen += linelen;
                                }
@@ -970,7 +985,7 @@ void Buffer::makeLaTeXFile(ostream & os,
        texrow.start(paragraphs.begin()->id(), 0);
 
        if (output_preamble && runparams.nice) {
-               os << "%% " << lyx_docversion << " created this file.  "
+               os << "%% LyX " << lyx_version << " created this file.  "
                        "For more info, see http://www.lyx.org/.\n"
                        "%% Do not edit unless you really know what "
                        "you are doing.\n";
@@ -1023,7 +1038,7 @@ void Buffer::makeLaTeXFile(ostream & os,
                texrow.newline();
        }
 
-       latexParagraphs(this, paragraphs, os, texrow, runparams);
+       latexParagraphs(*this, paragraphs, os, texrow, runparams);
 
        // add this just in case after all the paragraphs
        os << endl;
@@ -1133,7 +1148,7 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
                }
        }
 
-       ofs << "<!-- "  << lyx_docversion
+       ofs << "<!-- LyX "  << lyx_version
            << " created this file. For more info see http://www.lyx.org/"
            << " -->\n";
 
@@ -1472,7 +1487,7 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
 
                if (c == Paragraph::META_INSET) {
                        InsetOld * inset = par->getInset(i);
-                       inset->linuxdoc(this, os);
+                       inset->linuxdoc(*this, os);
                        font_old = font;
                        continue;
                }
@@ -1564,7 +1579,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
        }
        sgml::openTag(ofs, 0, false, top);
 
-       ofs << "<!-- DocBook file was created by " << lyx_docversion
+       ofs << "<!-- DocBook file was created by LyX " << lyx_version
            << "\n  See http://www.lyx.org/ for more information -->\n";
 
        vector<string> environment_stack(10);
@@ -1842,7 +1857,7 @@ void Buffer::simpleDocBookOnePar(ostream & os,
                        if (i || desc_on != 3) {
                                if (style->latexparam() == "CDATA")
                                        os << "]]>";
-                               inset->docbook(this, os, false);
+                               inset->docbook(*this, os, false);
                                if (style->latexparam() == "CDATA")
                                        os << "<![CDATA[";
                        }
@@ -2010,7 +2025,7 @@ void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) con
        for (inset_iterator it = inset_const_iterator_begin();
                it != inset_const_iterator_end(); ++it) {
                if (it->lyxCode() == InsetOld::BIBTEX_CODE)
-                       static_cast<InsetBibtex &>(*it).fillWithBibKeys(this, keys);
+                       static_cast<InsetBibtex &>(*it).fillWithBibKeys(*this, keys);
                else if (it->lyxCode() == InsetOld::INCLUDE_CODE)
                        static_cast<InsetInclude &>(*it).fillWithBibKeys(keys);
                else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
@@ -2177,6 +2192,7 @@ ParIterator Buffer::par_iterator_end()
        return ParIterator(paragraphs.end(), paragraphs);
 }
 
+
 ParConstIterator Buffer::par_iterator_begin() const
 {
        return ParConstIterator(const_cast<ParagraphList&>(paragraphs).begin(), paragraphs);