]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
cosmetic fix
[lyx.git] / src / buffer.C
index 2b724aa8a2ff163450e7bb1ae4bb17d18e07dfc9..b7afa7c11a69504c38f2178cd4b89e22159cfea6 100644 (file)
@@ -11,6 +11,7 @@
 #include <config.h>
 
 #include "buffer.h"
+#include "buffer_funcs.h"
 #include "bufferlist.h"
 #include "LyXAction.h"
 #include "lyxrc.h"
@@ -31,7 +32,6 @@
 #include "errorlist.h"
 #include "Lsstream.h"
 #include "format.h"
-#include "BufferView.h"
 #include "ParagraphParameters.h"
 #include "iterators.h"
 #include "lyxtextclasslist.h"
@@ -55,6 +55,7 @@
 
 #include "graphics/Previews.h"
 
+#include "support/LAssert.h"
 #include "support/textutils.h"
 #include "support/filetools.h"
 #include "support/path.h"
@@ -85,6 +86,8 @@
 #include <locale>
 #endif
 
+using namespace lyx::support;
+
 #ifndef CXX_GLOBAL_CSTD
 using std::pow;
 #endif
@@ -114,12 +117,26 @@ namespace {
 
 const int LYX_FORMAT = 224;
 
+bool openFileWrite(ofstream & ofs, string const & fname)
+{
+       ofs.open(fname.c_str());
+       if (!ofs) {
+               string const file = MakeDisplayPath(fname, 50);
+               string text = bformat(_("Could not open the specified "
+                                       "document\n%1$s."), file);
+               Alert::error(_("Could not open file"), text);
+               return false;
+       }
+       return true;
+}
+
+
 } // namespace anon
 
 Buffer::Buffer(string const & file, bool ronly)
        : niceFile(true), lyx_clean(true), bak_clean(true),
          unnamed(false), read_only(ronly),
-         filename_(file), users(0)
+         filename_(file)
 {
        lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
        filepath_ = OnlyPath(file);
@@ -141,10 +158,7 @@ Buffer::~Buffer()
        // here the buffer should take care that it is
        // saved properly, before it goes into the void.
 
-       // make sure that views using this buffer
-       // forgets it.
-       if (users)
-               users->buffer(0);
+       closing();
 
        if (!tmppath.empty() && destroyDir(tmppath) != 0) {
                Alert::warning(_("Could not remove temporary directory"),
@@ -154,7 +168,7 @@ Buffer::~Buffer()
        paragraphs.clear();
 
        // Remove any previewed LaTeX snippets assocoated with this buffer.
-       grfx::Previews::get().removeLoader(this);
+       lyx::graphics::Previews::get().removeLoader(this);
 }
 
 
@@ -204,9 +218,7 @@ void Buffer::setReadonly(bool flag)
 {
        if (read_only != flag) {
                read_only = flag;
-               updateTitles();
-               if (users)
-                       users->owner()->getDialogs().updateBufferDependent(false);
+               readonly(flag);
        }
 }
 
@@ -217,24 +229,6 @@ AuthorList & Buffer::authors()
 }
 
 
-/// Update window titles of all users
-// Should work on a list
-void Buffer::updateTitles() const
-{
-       if (users)
-               users->owner()->updateWindowTitle();
-}
-
-
-/// Reset autosave timer of all users
-// Should work on a list
-void Buffer::resetAutosaveTimers() const
-{
-       if (users)
-               users->owner()->resetAutosaveTimer();
-}
-
-
 void Buffer::setFileName(string const & newfile)
 {
        filename_ = MakeAbsPath(newfile);
@@ -273,6 +267,7 @@ int Buffer::readHeader(LyXLex & lex)
                lyxerr[Debug::PARSER] << "Handling header token: `"
                                      << token << '\'' << endl;
 
+
                string unknown = params.readToken(lex, token);
                if (!unknown.empty()) {
                        if (unknown[0] != '\\') {
@@ -280,11 +275,11 @@ int Buffer::readHeader(LyXLex & lex)
                        } else {
                                ++unknown_tokens;
                                string const s = bformat(_("Unknown token: "
-                                                          "%1$s %2$s\n"), 
-                                                        token, 
+                                                          "%1$s %2$s\n"),
+                                                        token,
                                                         lex.getString());
-                               parseError(ErrorItem(_("Header error"), s, 
-                                                    -1, 0, 0));
+                               error(ErrorItem(_("Header error"), s,
+                                               -1, 0, 0));
                        }
                }
        }
@@ -306,6 +301,7 @@ bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
        bool the_end_read = false;
 
        if (paragraphs.empty()) {
+               readHeader(lex);
                if (!params.getLyXTextClass().load()) {
                        string theclass = params.getLyXTextClass().name();
                        Alert::error(_("Can't load document class"), bformat(
@@ -314,9 +310,6 @@ bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
                        params.textclass = 0;
                }
        } else {
-               // We are inserting into an existing document
-               users->text->breakParagraph(paragraphs);
-
                // We don't want to adopt the parameters from the
                // document we insert, so read them into a temporary buffer
                // and then discard it
@@ -540,7 +533,7 @@ bool Buffer::readFile(LyXLex & lex, string const & filename,
                        // This code is reached if lyx2lyx failed (for
                        // some reason) to change the file format of
                        // the file.
-                       lyx::Assert(false);
+                       Assert(false);
                        return false;
                }
        }
@@ -624,7 +617,7 @@ bool Buffer::save() const
        } else {
                // Saving failed, so backup is not backup
                if (lyxrc.make_backup) {
-                       lyx::rename(s, fileName());
+                       rename(s, fileName());
                }
                return false;
        }
@@ -644,9 +637,8 @@ bool Buffer::writeFile(string const & fname) const
        }
 
        ofstream ofs(fname.c_str());
-       if (!ofs) {
+       if (!ofs)
                return false;
-       }
 
 #ifdef HAVE_LOCALE
        // Use the standard "C" locale for file output.
@@ -840,7 +832,7 @@ string const Buffer::asciiParagraph(Paragraph const & par,
                switch (c) {
                case Paragraph::META_INSET:
                {
-                       Inset const * inset = par.getInset(i);
+                       InsetOld const * inset = par.getInset(i);
                        if (inset) {
                                if (linelen > 0) {
                                        buffer << word;
@@ -897,13 +889,9 @@ string const Buffer::asciiParagraph(Paragraph const & par,
 
 void Buffer::writeFileAscii(string const & fname, int linelen)
 {
-       ofstream ofs(fname.c_str());
-       if (!ofs) {
-               string const file = MakeDisplayPath(fname, 50);
-               string text = bformat(_("Could not save the document\n%1$s."), file);
-               Alert::error(_("Could not save document"), text);
+       ofstream ofs;
+       if (!::openFileWrite(ofs, fname))
                return;
-       }
        writeFileAscii(ofs, linelen);
 }
 
@@ -920,25 +908,19 @@ void Buffer::writeFileAscii(ostream & os, int linelen)
 }
 
 
-
 void Buffer::makeLaTeXFile(string const & fname,
                           string const & original_path,
                           LatexRunParams const & runparams,
-                          bool only_body, bool only_preamble)
+                          bool output_preamble, bool output_body)
 {
        lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
 
-       ofstream ofs(fname.c_str());
-       if (!ofs) {
-               string const file = MakeDisplayPath(fname, 50);
-               string text = bformat(_("Could not open the specified document\n%1$s."),
-                       file);
-               Alert::error(_("Could not open file"), text);
+       ofstream ofs;
+       if (!::openFileWrite(ofs, fname))
                return;
-       }
 
        makeLaTeXFile(ofs, original_path,
-                     runparams, only_body, only_preamble);
+                     runparams, output_preamble, output_body);
 
        ofs.close();
        if (ofs.fail()) {
@@ -950,7 +932,7 @@ void Buffer::makeLaTeXFile(string const & fname,
 void Buffer::makeLaTeXFile(ostream & os,
                           string const & original_path,
                           LatexRunParams const & runparams_in,
-                          bool only_body, bool only_preamble)
+                          bool output_preamble, bool output_body)
 {
        LatexRunParams runparams = runparams_in;
        niceFile = runparams.nice; // this will be used by Insetincludes.
@@ -966,7 +948,7 @@ void Buffer::makeLaTeXFile(ostream & os,
        // first paragraph of the document. (Asger)
        texrow.start(paragraphs.begin()->id(), 0);
 
-       if (!only_body && runparams.nice) {
+       if (output_preamble && runparams.nice) {
                os << "%% " << lyx_docversion << " created this file.  "
                        "For more info, see http://www.lyx.org/.\n"
                        "%% Do not edit unless you really know what "
@@ -982,7 +964,7 @@ void Buffer::makeLaTeXFile(ostream & os,
        // input@path is set when the actual parameter
        // original_path is set. This is done for usual tex-file, but not
        // for nice-latex-file. (Matthias 250696)
-       if (!only_body) {
+       if (output_preamble) {
                if (!runparams.nice) {
                        // code for usual, NOT nice-latex-file
                        os << "\\batchmode\n"; // changed
@@ -1004,13 +986,13 @@ void Buffer::makeLaTeXFile(ostream & os,
                // Write the preamble
                runparams.use_babel = params.writeLaTeX(os, features, texrow);
 
-               if (only_preamble)
+               if (!output_body)
                        return;
 
                // make the body.
                os << "\\begin{document}\n";
                texrow.newline();
-       } // only_body
+       } // output_preamble
        lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
 
        if (!lyxrc.language_auto_begin) {
@@ -1033,7 +1015,7 @@ void Buffer::makeLaTeXFile(ostream & os,
                texrow.newline();
        }
 
-       if (!only_body) {
+       if (output_preamble) {
                os << "\\end{document}\n";
                texrow.newline();
 
@@ -1090,15 +1072,9 @@ bool Buffer::isSGML() const
 
 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
 {
-       ofstream ofs(fname.c_str());
-
-       if (!ofs) {
-               string const file = MakeDisplayPath(fname, 50);
-               string text = bformat(_("Could not save the specified document\n%1$s.\n"),
-                       file);
-               Alert::error(_("Could not save document"), text);
+       ofstream ofs;
+       if (!::openFileWrite(ofs, fname))
                return;
-       }
 
        niceFile = nice; // this will be used by included files.
 
@@ -1116,7 +1092,7 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
                ofs << "<!doctype linuxdoc system";
 
                string preamble = params.preamble;
-               const string name = nice ? ChangeExtension(filename_, ".sgml")
+               string const name = nice ? ChangeExtension(filename_, ".sgml")
                         : fname;
                preamble += features.getIncludedFiles(name);
                preamble += features.getLyXSGMLEntities();
@@ -1144,17 +1120,15 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
        string item_name;
        vector<string> environment_stack(5);
 
-       users->resetErrorList();
-
        ParagraphList::iterator pit = paragraphs.begin();
        ParagraphList::iterator pend = paragraphs.end();
        for (; pit != pend; ++pit) {
                LyXLayout_ptr const & style = pit->layout();
                // treat <toc> as a special case for compatibility with old code
                if (pit->isInset(0)) {
-                       Inset * inset = pit->getInset(0);
-                       Inset::Code lyx_code = inset->lyxCode();
-                       if (lyx_code == Inset::TOC_CODE) {
+                       InsetOld * inset = pit->getInset(0);
+                       InsetOld::Code lyx_code = inset->lyxCode();
+                       if (lyx_code == InsetOld::TOC_CODE) {
                                string const temp = "toc";
                                sgml::openTag(ofs, depth, false, temp);
                                continue;
@@ -1184,7 +1158,7 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
 
                case LATEX_COMMAND:
                        if (depth != 0)
-                               parseError(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
+                               error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
 
                        if (!environment_stack[depth].empty()) {
                                sgml::closeTag(ofs, depth, false, environment_stack[depth]);
@@ -1274,8 +1248,6 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
 
        // we want this to be true outside previews (for insetexternal)
        niceFile = true;
-
-       users->showErrorList(_("LinuxDoc"));
 }
 
 
@@ -1478,7 +1450,7 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
                char c = par->getChar(i);
 
                if (c == Paragraph::META_INSET) {
-                       Inset * inset = par->getInset(i);
+                       InsetOld * inset = par->getInset(i);
                        inset->linuxdoc(this, os);
                        font_old = font;
                        continue;
@@ -1530,14 +1502,9 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
 
 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
 {
-       ofstream ofs(fname.c_str());
-       if (!ofs) {
-               string const file = MakeDisplayPath(fname, 50);
-               string text = bformat(_("Could not save the specified document\n%1$s.\n"),
-                       file);
-               Alert::error(_("Could not save document"), text);
+       ofstream ofs;
+       if (!::openFileWrite(ofs, fname))
                return;
-       }
 
        niceFile = nice; // this will be used by Insetincludes.
 
@@ -1554,7 +1521,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                    << "  PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\"";
 
                string preamble = params.preamble;
-               const string name = nice ? ChangeExtension(filename_, ".sgml")
+               string const name = nice ? ChangeExtension(filename_, ".sgml")
                         : fname;
                preamble += features.getIncludedFiles(name);
                preamble += features.getLyXSGMLEntities();
@@ -1592,8 +1559,6 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
        string item_name;
        string command_name;
 
-       users->resetErrorList();
-
        ParagraphList::iterator par = paragraphs.begin();
        ParagraphList::iterator pend = paragraphs.end();
 
@@ -1643,14 +1608,14 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
 
                case LATEX_COMMAND:
                        if (depth != 0)
-                               parseError(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
+                               error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
 
                        command_name = style->latexname();
 
                        sgmlparam = style->latexparam();
                        c_params = split(sgmlparam, c_depth,'|');
 
-                       cmd_depth = lyx::atoi(c_depth);
+                       cmd_depth = atoi(c_depth);
 
                        if (command_flag) {
                                if (cmd_depth < command_base) {
@@ -1682,9 +1647,9 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                        // 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();
-                               if (lyx_code == Inset::LABEL_CODE) {
+                               InsetOld * inset = par->getInset(0);
+                               InsetOld::Code lyx_code = inset->lyxCode();
+                               if (lyx_code == InsetOld::LABEL_CODE) {
                                        command_name += " id=\"";
                                        command_name += (static_cast<InsetCommand *>(inset))->getContents();
                                        command_name += '"';
@@ -1809,7 +1774,6 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
 
        // we want this to be true outside previews (for insetexternal)
        niceFile = true;
-       users->showErrorList(_("DocBook"));
 }
 
 
@@ -1852,7 +1816,7 @@ void Buffer::simpleDocBookOnePar(ostream & os,
 
 
                if (par->isInset(i)) {
-                       Inset * inset = par->getInset(i);
+                       InsetOld * inset = par->getInset(i);
                        // don't print the inset in position 0 if desc_on == 3 (label)
                        if (i || desc_on != 3) {
                                if (style->latexparam() == "CDATA")
@@ -1904,9 +1868,7 @@ void Buffer::simpleDocBookOnePar(ostream & os,
 // Other flags: -wall -v0 -x
 int Buffer::runChktex()
 {
-       if (!users->text) return 0;
-
-       users->owner()->busy(true);
+       busy(true);
 
        // get LaTeX-Filename
        string const name = getLatexName();
@@ -1918,7 +1880,7 @@ int Buffer::runChktex()
        }
 
        Path p(path); // path to LaTeX file
-       users->owner()->message(_("Running chktex..."));
+       message(_("Running chktex..."));
 
        // Generate the LaTeX file if neccessary
        LatexRunParams runparams;
@@ -1935,12 +1897,10 @@ int Buffer::runChktex()
                             _("Could not run chktex successfully."));
        } else if (res > 0) {
                // Insert all errors as errors boxes
-               ErrorList el (*this, terr);
-               users->setErrorList(el);
-               users->showErrorList(_("ChkTeX"));
+               bufferErrors(*this, terr);
        }
 
-       users->owner()->busy(false);
+       busy(false);
 
        return res;
 }
@@ -2014,7 +1974,7 @@ void Buffer::getLabelList(std::vector<string> & list) const
 
 
 // This is also a buffer property (ale)
-void Buffer::fillWithBibKeys(vector<pair<string, string> > & keys) const
+void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const
 {
        /// if this is a child document and the parent is already loaded
        /// use the parent's list instead  [ale990412]
@@ -2028,11 +1988,11 @@ void Buffer::fillWithBibKeys(vector<pair<string, string> > & keys) const
 
        for (inset_iterator it = inset_const_iterator_begin();
                it != inset_const_iterator_end(); ++it) {
-               if (it->lyxCode() == Inset::BIBTEX_CODE)
+               if (it->lyxCode() == InsetOld::BIBTEX_CODE)
                        static_cast<InsetBibtex &>(*it).fillWithBibKeys(this, keys);
-               else if (it->lyxCode() == Inset::INCLUDE_CODE)
+               else if (it->lyxCode() == InsetOld::INCLUDE_CODE)
                        static_cast<InsetInclude &>(*it).fillWithBibKeys(keys);
-               else if (it->lyxCode() == Inset::BIBITEM_CODE) {
+               else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
                        InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
                        string const key = bib.getContents();
                        string const opt = bib.getOptions();
@@ -2089,22 +2049,6 @@ bool Buffer::dispatch(int action, string const & argument, bool * result)
 }
 
 
-void Buffer::resizeInsets(BufferView * bv)
-{
-       /// then remove all LyXText in text-insets
-       for_each(paragraphs.begin(), paragraphs.end(),
-                boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
-}
-
-
-void Buffer::redraw()
-{
-#warning repaint needed here, or do you mean update() ?
-       users->repaint();
-       users->fitCursor();
-}
-
-
 void Buffer::changeLanguage(Language const * from, Language const * to)
 {
        lyxerr << "Changing Language!" << endl;
@@ -2146,14 +2090,14 @@ void Buffer::inset_iterator::setParagraph()
 }
 
 
-Inset * Buffer::getInsetFromID(int id_arg) const
+InsetOld * Buffer::getInsetFromID(int id_arg) const
 {
        for (inset_iterator it = inset_const_iterator_begin();
                 it != inset_const_iterator_end(); ++it)
        {
                if (it->id() == id_arg)
                        return &(*it);
-               Inset * in = it->getInsetFromID(id_arg);
+               InsetOld * in = it->getInsetFromID(id_arg);
                if (in)
                        return in;
        }
@@ -2224,19 +2168,6 @@ ParConstIterator Buffer::par_iterator_end() const
 }
 
 
-
-void Buffer::addUser(BufferView * u)
-{
-       users = u;
-}
-
-
-void Buffer::delUser(BufferView *)
-{
-       users = 0;
-}
-
-
 Language const * Buffer::getLanguage() const
 {
        return params.language;
@@ -2330,12 +2261,6 @@ bool Buffer::isReadonly() const
 }
 
 
-BufferView * Buffer::getUser() const
-{
-       return users;
-}
-
-
 void Buffer::setParentName(string const & name)
 {
        params.parentname = name;