]> git.lyx.org Git - lyx.git/blobdiff - src/bufferlist.C
try pre2 again
[lyx.git] / src / bufferlist.C
index d05d1deda19ce35dd1b97a80d1602cef24c44909..3f4477636478f3fe8f20590692be00d5112c1ee7 100644 (file)
 #include <config.h>
 
 #include <algorithm>
+#include <functional>
 
 #include "bufferlist.h"
 #include "lyx_main.h"
 #include "minibuffer.h"
 #include "support/FileInfo.h"
 #include "support/filetools.h"
+#include "support/lyxmanip.h"
+#include "support/lyxfunctional.h"
 #include "lyx_gui_misc.h"
 #include "lastfiles.h"
 #include "debug.h"
 #include "vc-backend.h"
 #include "TextCache.h"
 
-extern BufferView * current_view; // called too many times in this file...
-
 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
@@ -66,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;
 }
@@ -88,14 +94,11 @@ bool BufferList::empty() const
 }
 
 
-extern bool MenuWrite(Buffer *);
-extern bool MenuWriteAs(Buffer *);
-
 bool BufferList::QwriteAll()
 {
         bool askMoreConfirmation = false;
         string unsaved;
-       for(BufferStorage::iterator it = bstore.begin();
+       for (BufferStorage::iterator it = bstore.begin();
            it != bstore.end(); ++it) {
                if (!(*it)->isLyxClean()) {
                        string fname;
@@ -104,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 = !MenuWriteAs(current_view, (*it));
                                        else {
-                                               reask = !MenuWrite((*it));
+                                               reask = !MenuWrite(current_view, (*it));
                                        }
                                        break;
                                case 2: // No
@@ -160,10 +163,7 @@ void BufferList::closeAll()
 
 void BufferList::resize()
 {
-       for(BufferStorage::iterator it = bstore.begin();
-           it != bstore.end(); ++it) {
-               (*it)->resize();
-       }
+       for_each(bstore.begin(), bstore.end(), mem_fun(&Buffer::resize));
 }
 
 
@@ -183,15 +183,16 @@ bool BufferList::close(Buffer * buf)
                else
                        fname = MakeDisplayPath(buf->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 (buf->isUnnamed())
-                                       reask = !MenuWriteAs(buf);
+                                       reask = !MenuWriteAs(current_view, buf);
                                else if (buf->save()) {
                                        lastfiles->newFile(buf->fileName());
+                                       reask = false;
                                } else {
                                        if (buf->getUser())
                                                AllowInput(buf->getUser());
@@ -219,13 +220,11 @@ bool BufferList::close(Buffer * buf)
 }
 
 
-vector<string> BufferList::getFileNames() const
+vector<string> const BufferList::getFileNames() const
 {
        vector<string> nvec;
-       for(BufferStorage::const_iterator cit = bstore.begin();
-           cit != bstore.end(); ++cit) {
-               nvec.push_back((*cit)->fileName());
-       }
+       std::copy(bstore.begin(), bstore.end(),
+                 back_inserter_fun(nvec, &Buffer::fileName));
        return nvec;
 }
 
@@ -237,7 +236,7 @@ Buffer * BufferList::first()
 }
 
 
-Buffer * BufferList::getBuffer(int choice)
+Buffer * BufferList::getBuffer(unsigned int choice)
 {
        if (choice >= bstore.size()) return 0;
        return bstore[choice];
@@ -247,10 +246,10 @@ Buffer * BufferList::getBuffer(int choice)
 int BufferList::unlockInset(UpdatableInset * inset)
 {
        if (!inset) return 1;
-       for(BufferStorage::iterator it = bstore.begin();
-           it != bstore.end(); ++it) {
+       for (BufferStorage::iterator it = bstore.begin();
+            it != bstore.end(); ++it) {
                if ((*it)->getUser()
-                   && (*it)->getUser()->the_locking_inset == inset) {
+                   && (*it)->getUser()->theLockingInset() == inset) {
                        (*it)->getUser()->insetUnlock();
                        return 0;
                }
@@ -261,8 +260,8 @@ int BufferList::unlockInset(UpdatableInset * inset)
 
 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
 {
-       for(BufferStorage::iterator it = bstore.begin();
-           it != bstore.end(); ++it) {
+       for (BufferStorage::iterator it = bstore.begin();
+            it != bstore.end(); ++it) {
                if (!(*it)->isDepClean(mastertmpdir)) {
                        string writefile = mastertmpdir;
                        writefile += '/';
@@ -277,62 +276,64 @@ void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
 
 void BufferList::emergencyWriteAll()
 {
-       for (BufferStorage::iterator it = bstore.begin();
-            it != bstore.end(); ++it) {
-               if (!(*it)->isLyxClean()) {
-                       bool madeit = false;
-                       
-                       lyxerr <<_("lyx: Attempting to save"
-                                  " document ");
-                       if ((*it)->isUnnamed())
-                           lyxerr << OnlyFilename((*it)->fileName());
-                       else
-                           lyxerr << (*it)->fileName();
-                       lyxerr << _(" as...") << endl;
-                       
-                       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 ((*it)->isUnnamed())
-                                               continue;
-                                       s = (*it)->fileName();
-                               } else if (i == 1) {
-                                       s = AddName(GetEnvPath("HOME"),
-                                                   (*it)->fileName());
-                               } else {
-                                       // MakeAbsPath to prepend the current
-                                       // drive letter on OS/2
-                                       s = AddName(MakeAbsPath("/tmp/"),
-                                                   (*it)->fileName());
-                               }
-                               s += ".emergency";
-                               
-                               lyxerr << "  " << i + 1 << ") " << s << endl;
-                               
-                               if ((*it)->writeFile(s, true)) {
-                                       (*it)->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;
-                               }
-                       }
+       for_each(bstore.begin(), bstore.end(),
+                class_fun(*this, &BufferList::emergencyWrite));
+}
+
+
+void BufferList::emergencyWrite(Buffer * buf) 
+{
+       // No need to save if the buffer has not changed.
+       if (buf->isLyxClean()) return;
+       
+       lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
+                     buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
+                     : buf->fileName().c_str()) << endl;
+       
+       // We try to save three places:
+
+       // 1) Same place as document. Unless it is an unnamed doc.
+       if (!buf->isUnnamed()) {
+               string s = buf->fileName();
+               s += ".emergency";
+               lyxerr << "  " << s << endl;
+               if (buf->writeFile(s, true)) {
+                       buf->markLyxClean();
+                       lyxerr << _("  Save seems successful. Phew.") << endl;
+                       return;
+               } else {
+                       lyxerr << _("  Save failed! Trying...") << endl;
                }
        }
+       
+       // 2) In HOME directory.
+       string s = AddName(GetEnvPath("HOME"), buf->fileName());
+       s += ".emergency";
+       lyxerr << " " << s << endl;
+       if (buf->writeFile(s, true)) {
+               buf->markLyxClean();
+               lyxerr << _("  Save seems successful. Phew.") << endl;
+               return;
+       }
+       
+       lyxerr << _("  Save failed! Trying...") << endl;
+       
+       // 3) In "/tmp" directory.
+       // MakeAbsPath to prepend the current
+       // drive letter on OS/2
+       s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
+       s += ".emergency";
+       lyxerr << " " << s << endl;
+       if (buf->writeFile(s, true)) {
+               buf->markLyxClean();
+               lyxerr << _("  Save seems successful. Phew.") << endl;
+               return;
+       }
+       lyxerr << _("  Save failed! Bummer. Document is lost.") << endl;
 }
 
 
+
 Buffer * BufferList::readFile(string const & s, bool ronly)
 {
        Buffer * b = bstore.newBuffer(s, ronly);
@@ -362,7 +363,7 @@ Buffer * BufferList::readFile(string const & s, bool ronly)
                                use_emergency = true;
                        } else {
                                // Here, we should delete the emergency save
-                               ::unlink(e.c_str());
+                               lyx::unlink(e);
                        }
                }
        }
@@ -385,7 +386,7 @@ Buffer * BufferList::readFile(string const & s, bool ronly)
                                        b->markDirty();
                                } else {
                                        // Here, we should delete the autosave
-                                       ::unlink(a.c_str());
+                                       lyx::unlink(a);
                                }
                        }
                }
@@ -404,12 +405,8 @@ Buffer * BufferList::readFile(string const & s, bool ronly)
 
 bool BufferList::exists(string const & s) const
 {
-       for (BufferStorage::const_iterator cit = bstore.begin();
-            cit != bstore.end(); ++cit) {
-               if ((*cit)->fileName() == s)
-                       return true;
-       }
-       return false;
+       return find_if(bstore.begin(), bstore.end(),
+                      compare_memfun(&Buffer::fileName, s)) != bstore.end();
 }
 
 
@@ -423,12 +420,10 @@ bool BufferList::isLoaded(Buffer const * b) const
 
 Buffer * BufferList::getBuffer(string const & s)
 {
-       for(BufferStorage::iterator it = bstore.begin();
-           it != bstore.end(); ++it) {
-               if ((*it)->fileName() == s)
-                       return (*it);
-       }
-       return 0;
+       BufferStorage::iterator it =
+               find_if(bstore.begin(), bstore.end(),
+                       compare_memfun(&Buffer::fileName, s));
+       return it != bstore.end() ? (*it) : 0;
 }
 
 
@@ -456,17 +451,15 @@ Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
                        // no template, start with empty buffer
                        b->paragraph = new LyXParagraph;
                }
-       }
-       else {  // start with empty buffer
+       } else {  // start with empty buffer
                b->paragraph = new LyXParagraph;
        }
 
-#warning Why mark a new document dirty? I deactivate this (Jug)
-       if (!lyxrc.new_ask_filename) {
-//             b->markDirty();
-               if (!isNamed)
-                       b->setUnnamed();
+       if (!lyxrc.new_ask_filename && !isNamed) {
+               b->setUnnamed();
+               b->setFileName(name);
        }
+
        b->setReadonly(false);
        
        return b;
@@ -476,7 +469,7 @@ Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
 {
        // make sure our path is absolute
-       string s = MakeAbsPath(filename);
+       string const s = MakeAbsPath(filename);
 
        // file already open?
        if (exists(s)) {