]> git.lyx.org Git - lyx.git/blobdiff - src/bufferlist.C
In for a penny, in for a pound. Consistent use of // -*- C++ -*-
[lyx.git] / src / bufferlist.C
index 08173d2c81ecc548923c4d69b6d3b38c8e184c7e..ecfcb4945aee23100197efc12214d222191c1b52 100644 (file)
@@ -4,9 +4,9 @@
  *           LyX, The Document Word Processor
  *
  *           Copyright 1995 Matthias Ettrich
- *           Copyright 1995-2000 The LyX Team. 
+ *           Copyright 1995-2001 The LyX Team. 
  *
- *           This file is Copyright 1996-2000
+ *           This file is Copyright 1996-2001
  *           Lars Gullik Bjønnes
  *
  * ====================================================== 
@@ -23,7 +23,6 @@
 
 #include "bufferlist.h"
 #include "lyx_main.h"
-#include "minibuffer.h"
 #include "support/FileInfo.h"
 #include "support/filetools.h"
 #include "support/lyxmanip.h"
 using std::vector;
 using std::find;
 using std::endl;
+using std::find_if;
+using std::for_each;
+using std::mem_fun;
+
+extern BufferView * current_view;
 
 //
 // Class BufferStorage
@@ -50,6 +54,7 @@ using std::endl;
 
 void BufferStorage::release(Buffer * buf)
 {
+       lyx::Assert(buf);
        Container::iterator it = find(container.begin(), container.end(), buf);
        if (it != container.end()) {
                // Make sure that we don't store a LyXText in
@@ -67,8 +72,8 @@ Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
 {
        Buffer * tmpbuf = new Buffer(s, ronly);
        tmpbuf->params.useClassDefaults();
-       lyxerr.debug() << "Assigning to buffer "
-                      << container.size() << endl;
+       lyxerr[Debug::INFO] << "Assigning to buffer "
+                           << container.size() << endl;
        container.push_back(tmpbuf);
        return tmpbuf;
 }
@@ -89,9 +94,6 @@ bool BufferList::empty() const
 }
 
 
-extern bool MenuWrite(Buffer *);
-extern bool MenuWriteAs(Buffer *);
-
 bool BufferList::QwriteAll()
 {
         bool askMoreConfirmation = false;
@@ -105,15 +107,15 @@ bool BufferList::QwriteAll()
                        else
                                fname = MakeDisplayPath((*it)->fileName(), 50);
                        bool reask = true;
-                       while(reask) {
-                               switch(AskConfirmation(_("Changes in document:"),
+                       while (reask) {
+                               switch (AskConfirmation(_("Changes in document:"),
                                                       fname,
                                                       _("Save document?"))) {
                                case 1: // Yes
                                        if ((*it)->isUnnamed())
-                                               reask = !MenuWriteAs((*it));
+                                               reask = !WriteAs(current_view, (*it));
                                        else {
-                                               reask = !MenuWrite((*it));
+                                               reask = !MenuWrite(current_view, (*it));
                                        }
                                        break;
                                case 2: // No
@@ -167,6 +169,8 @@ void BufferList::resize()
 
 bool BufferList::close(Buffer * buf)
 {
+       lyx::Assert(buf);
+       
        // CHECK
        // Trace back why we need to use buf->getUser here.
        // Perhaps slight rewrite is in order? (Lgb)
@@ -182,12 +186,12 @@ bool BufferList::close(Buffer * buf)
                        fname = MakeDisplayPath(buf->fileName(), 50);
                bool reask = true;
                while (reask) {
-                       switch(AskConfirmation(_("Changes in document:"),
+                       switch (AskConfirmation(_("Changes in document:"),
                                               fname,
                                               _("Save document?"))){
                        case 1: // Yes
                                if (buf->isUnnamed())
-                                       reask = !MenuWriteAs(buf);
+                                       reask = !WriteAs(current_view, buf);
                                else if (buf->save()) {
                                        lastfiles->newFile(buf->fileName());
                                        reask = false;
@@ -222,7 +226,7 @@ vector<string> const BufferList::getFileNames() const
 {
        vector<string> nvec;
        std::copy(bstore.begin(), bstore.end(),
-                 back_inserter_fun(nvec, &Buffer::fileName));
+                 lyx::back_inserter_fun(nvec, &Buffer::fileName));
        return nvec;
 }
 
@@ -243,7 +247,9 @@ Buffer * BufferList::getBuffer(unsigned int choice)
 
 int BufferList::unlockInset(UpdatableInset * inset)
 {
-       if (!inset) return 1;
+       lyx::Assert(inset);
+       
+       //if (!inset) return 1;
        for (BufferStorage::iterator it = bstore.begin();
             it != bstore.end(); ++it) {
                if ((*it)->getUser()
@@ -275,12 +281,17 @@ void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
 void BufferList::emergencyWriteAll()
 {
        for_each(bstore.begin(), bstore.end(),
-                class_fun(*this, &BufferList::emergencyWrite));
+                lyx::class_fun(*this, &BufferList::emergencyWrite));
 }
 
 
 void BufferList::emergencyWrite(Buffer * buf) 
 {
+       // assert(buf) // this is not good since C assert takes an int
+                      // and a pointer is a long (JMarc)
+       assert(buf != 0); // use c assert to avoid a loop
+
+       
        // No need to save if the buffer has not changed.
        if (buf->isLyxClean()) return;
        
@@ -288,50 +299,6 @@ void BufferList::emergencyWrite(Buffer * buf)
                      buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
                      : buf->fileName().c_str()) << endl;
        
-       //if (buf->isUnnamed())
-       //      lyxerr << OnlyFilename(buf->fileName());
-       //else
-       //      lyxerr << buf->fileName();
-       //lyxerr << _(" as...") << endl;
-       
-       // Let's unroll this loop (Lgb)
-#if 0
-       bool madeit = false;
-       
-       for (int i = 0; i < 3 && !madeit; ++i) {
-               string s;
-               
-               // We try to save three places:
-               // 1) Same place as document.
-               // 2) In HOME directory.
-               // 3) In "/tmp" directory.
-               if (i == 0) {
-                       if (buf->isUnnamed())
-                               continue;
-                       s = buf->fileName();
-               } else if (i == 1) {
-                       s = AddName(GetEnvPath("HOME"), buf->fileName());
-               } else {
-                       // MakeAbsPath to prepend the current
-                       // drive letter on OS/2
-                       s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
-               }
-               s += ".emergency";
-               
-               lyxerr << "  " << i + 1 << ") " << s << endl;
-               
-               if (buf->writeFile(s, true)) {
-                       buf->markLyxClean();
-                       lyxerr << _("  Save seems successful. Phew.") << endl;
-                       madeit = true;
-               } else if (i != 2) {
-                       lyxerr << _("  Save failed! Trying...") << endl;
-               } else {
-                       lyxerr << _("  Save failed! Bummer. Document is lost.")
-                              << endl;
-               }
-       }
-#else
        // We try to save three places:
 
        // 1) Same place as document. Unless it is an unnamed doc.
@@ -372,7 +339,6 @@ void BufferList::emergencyWrite(Buffer * buf)
                return;
        }
        lyxerr << _("  Save failed! Bummer. Document is lost.") << endl;
-#endif
 }
 
 
@@ -449,12 +415,15 @@ Buffer * BufferList::readFile(string const & s, bool ronly)
 bool BufferList::exists(string const & s) const
 {
        return find_if(bstore.begin(), bstore.end(),
-                      compare_memfun(&Buffer::fileName, s)) != bstore.end();
+                      lyx::compare_memfun(&Buffer::fileName, s))
+               != bstore.end();
 }
 
 
 bool BufferList::isLoaded(Buffer const * b) const
 {
+       lyx::Assert(b);
+       
        BufferStorage::const_iterator cit =
                find(bstore.begin(), bstore.end(), b);
        return cit != bstore.end();
@@ -465,7 +434,7 @@ Buffer * BufferList::getBuffer(string const & s)
 {
        BufferStorage::iterator it =
                find_if(bstore.begin(), bstore.end(),
-                       compare_memfun(&Buffer::fileName, s));
+                       lyx::compare_memfun(&Buffer::fileName, s));
        return it != bstore.end() ? (*it) : 0;
 }
 
@@ -498,10 +467,11 @@ Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
                b->paragraph = new LyXParagraph;
        }
 
-       if (!lyxrc.new_ask_filename) {
-               if (!isNamed)
-                       b->setUnnamed();
+       if (!lyxrc.new_ask_filename && !isNamed) {
+               b->setUnnamed();
+               b->setFileName(name);
        }
+
        b->setReadonly(false);
        
        return b;
@@ -533,11 +503,6 @@ Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
        bool ro = false;
        switch (IsFileWriteable(s)) {
        case 0:
-#if 0
-               current_view->owner()->getMiniBuffer()->
-                       Set(_("File `") + MakeDisplayPath(s, 50) +
-                           _("' is read-only."));
-#endif
                ro = true;
                // Fall through
        case 1: