]> git.lyx.org Git - lyx.git/blobdiff - src/BufferList.cpp
Release children of a master if the children don't have a default master. It was...
[lyx.git] / src / BufferList.cpp
index 7026cd8ea55c7a9beb177fe91183ce3bd69d18cf..b47057a5ec4b0ead88db31887570da0b098e82d9 100644 (file)
@@ -3,7 +3,7 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Lars Gullik Bjønnes
+ * \author Lars Gullik Bjønnes
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -24,8 +24,9 @@
 
 #include "support/ExceptionMessage.h"
 #include "support/debug.h"
-#include "support/filetools.h"
 #include "support/FileName.h"
+#include "support/FileNameList.h"
+#include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
 #include "support/Package.h"
@@ -50,6 +51,15 @@ BufferList::BufferList()
 {}
 
 
+BufferList::~BufferList()
+{
+       BufferStorage::iterator it = binternal.begin();
+       BufferStorage::iterator end = binternal.end();
+       for (; it != end; ++it)
+               delete (*it);
+}
+
+
 bool BufferList::empty() const
 {
        return bstore.empty();
@@ -109,8 +119,12 @@ Buffer * BufferList::newBuffer(string const & s, bool const ronly)
                }
        }
        tmpbuf->params().useClassDefaults();
-       LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
-       bstore.push_back(tmpbuf.get());
+       if (tmpbuf->fileName().extension() == "internal") {
+               binternal.push_back(tmpbuf.get());
+       } else {
+               LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
+               bstore.push_back(tmpbuf.get());
+       }
        return tmpbuf.release();
 }
 
@@ -122,12 +136,13 @@ void BufferList::closeAll()
 }
 
 
-vector<string> const BufferList::getFileNames() const
+FileNameList const & BufferList::fileNames() const
 {
-       vector<string> nvec;
+       static FileNameList nvec;
+       nvec.clear();
        transform(bstore.begin(), bstore.end(),
                  back_inserter(nvec),
-                 boost::bind(&Buffer::absFileName, _1));
+                 boost::bind(&Buffer::fileName, _1));
        return nvec;
 }
 
@@ -166,10 +181,8 @@ Buffer * BufferList::next(Buffer const * buf) const
                                                bstore.end(), buf);
        LASSERT(it != bstore.end(), /**/);
        ++it;
-       if (it == bstore.end())
-               return bstore.front();
-       else
-               return *it;
+       Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
+       return nextbuf;
 }
 
 
@@ -182,10 +195,9 @@ Buffer * BufferList::previous(Buffer const * buf) const
        BufferStorage::const_iterator it = find(bstore.begin(),
                                                bstore.end(), buf);
        LASSERT(it != bstore.end(), /**/);
-       if (it == bstore.begin())
-               return bstore.back();
-       else
-               return *(it - 1);
+
+       Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
+       return previousbuf;
 }
 
 
@@ -207,102 +219,46 @@ void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
 
 void BufferList::emergencyWriteAll()
 {
-       for_each(bstore.begin(), bstore.end(),
-                bind(&BufferList::emergencyWrite, this, _1));
-}
-
-
-docstring BufferList::emergencyWrite(Buffer * buf)
-{
-       // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
-       // compare with 0 to avoid pointer/interger comparison
-       // ::assert(buf != 0);
-       if (!buf)
-               return _("No file open!");
-
-       // No need to save if the buffer has not changed.
-       if (buf->isClean())
-               return docstring();
-
-       string const doc = buf->isUnnamed()
-               ? onlyFilename(buf->absFileName()) : buf->absFileName();
-
-       docstring user_message = bformat(
-               _("LyX: Attempting to save document %1$s\n"), from_utf8(doc));
-
-       // We try to save three places:
-       // 1) Same place as document. Unless it is an unnamed doc.
-       if (!buf->isUnnamed()) {
-               string s = buf->absFileName();
-               s += ".emergency";
-               lyxerr << "  " << s << endl;
-               if (buf->writeFile(FileName(s))) {
-                       buf->markClean();
-                       user_message += _("  Save seems successful. Phew.\n");
-                       return user_message;
-               } else {
-                       user_message += _("  Save failed! Trying...\n");
-               }
-       }
-
-       // 2) In HOME directory.
-       string s = addName(package().home_dir().absFilename(), buf->absFileName());
-       s += ".emergency";
-       lyxerr << ' ' << s << endl;
-       if (buf->writeFile(FileName(s))) {
-               buf->markClean();
-               user_message += _("  Save seems successful. Phew.\n");
-               return user_message;
-       }
-
-       user_message += _("  Save failed! Trying...\n");
-
-       // 3) In "/tmp" directory.
-       // MakeAbsPath to prepend the current
-       // drive letter on OS/2
-       s = addName(package().temp_dir().absFilename(), buf->absFileName());
-       s += ".emergency";
-       lyxerr << ' ' << s << endl;
-       if (buf->writeFile(FileName(s))) {
-               buf->markClean();
-               user_message += _("  Save seems successful. Phew.\n");
-               return user_message;
-       }
-
-       user_message += _("  Save failed! Bummer. Document is lost.");
-       return user_message;
+       BufferStorage::const_iterator it = bstore.begin();
+       BufferStorage::const_iterator const en = bstore.end();
+       for (; it != en; ++it)
+                (*it)->emergencyWrite();
 }
 
 
 bool BufferList::exists(FileName const & fname) const
 {
-       //FIXME: use Buffer::fileName()!
-       string const s = fname.absFilename();
-       return find_if(bstore.begin(), bstore.end(),
-                      bind(equal_to<string>(),
-                           bind(&Buffer::absFileName, _1),
-                           s))
-               != bstore.end();
+       return getBuffer(fname) != 0;
 }
 
 
 bool BufferList::isLoaded(Buffer const * b) const
 {
-       LASSERT(b, /**/);
        BufferStorage::const_iterator cit =
                find(bstore.begin(), bstore.end(), b);
        return cit != bstore.end();
 }
 
 
-Buffer * BufferList::getBuffer(string const & s)
+namespace {
+struct equivalent_to : public binary_function<FileName, FileName, bool>
 {
-       BufferStorage::iterator it =
-               find_if(bstore.begin(), bstore.end(),
-                       bind(equal_to<string>(),
-                            bind(&Buffer::absFileName, _1),
-                            s));
+       bool operator()(FileName const & x, FileName const & y) const
+       { return equivalent(x, y); }
+};
+}
 
+
+Buffer * BufferList::getBuffer(support::FileName const & fname) const
+{
+       // 1) cheap test, using string comparison of file names
+       BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
+               bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
+       if (it != bstore.end())
+                       return *it;
+       // 2) possibly expensive test, using equivalence test of file names
+       it = find_if(bstore.begin(), bstore.end(),
+               bind(equivalent_to(), bind(&Buffer::fileName, _1), fname));
        return it != bstore.end() ? (*it) : 0;
 }
 
@@ -311,9 +267,26 @@ Buffer * BufferList::getBufferFromTmp(string const & s)
 {
        BufferStorage::iterator it = bstore.begin();
        BufferStorage::iterator end = bstore.end();
-       for (; it < end; ++it)
-               if (prefixIs(s, (*it)->temppath()))
-                       return *it;
+       for (; it < end; ++it) {
+               if (prefixIs(s, (*it)->temppath())) {
+                       // check whether the filename matches the master
+                       string const master_name = changeExtension(onlyFilename(
+                                               (*it)->absFileName()), ".tex");
+                       if (suffixIs(s, master_name))
+                               return *it;
+                       // if not, try with the children
+                       vector<Buffer *> clist = (*it)->getChildren();
+                       vector<Buffer *>::const_iterator cit = clist.begin();
+                       vector<Buffer *>::const_iterator cend = clist.end();
+                       for (; cit < cend; ++cit) {
+                               string const mangled_child_name = DocFileName(
+                                       changeExtension((*cit)->absFileName(),
+                                               ".tex")).mangledFilename();
+                               if (suffixIs(s, mangled_child_name))
+                                       return *cit;
+                       }
+               }
+       }
        return 0;
 }
 
@@ -327,11 +300,11 @@ void BufferList::setCurrentAuthor(docstring const & name, docstring const & emai
 }
 
 
-int BufferList::bufferNum(string const & name) const
+int BufferList::bufferNum(FileName const & fname) const
 {
-       vector<string> buffers = getFileNames();
-       vector<string>::const_iterator cit =
-               find(buffers.begin(), buffers.end(), name);
+       FileNameList const & buffers = fileNames();
+       FileNameList::const_iterator cit =
+               find(buffers.begin(), buffers.end(), fname);
        if (cit == buffers.end())
                return 0;
        return int(cit - buffers.begin());
@@ -345,7 +318,8 @@ bool BufferList::releaseChild(Buffer * parent, Buffer * child)
        LASSERT(parent->isChild(child), return false);
 
        // Child document has a different parent, don't close it.
-       if (child->parent() != parent)
+       Buffer const * parent_ = child->parent();
+       if (parent_ && parent_ != parent)
                return false;
 
        BufferStorage::iterator it = bstore.begin();