]> git.lyx.org Git - lyx.git/blobdiff - src/bufferlist.C
Collapse all those LFUN_XYZ_APPLY to a single LFUN_INSET_APPLY.
[lyx.git] / src / bufferlist.C
index fc642d8c1d441fa9539fef25ccb07e10105e2882..a68a8a9adf09c7fa01441aa520b553549a0b4b53 100644 (file)
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "bufferlist.h"
 #include "lyx_main.h"
 #include "lastfiles.h"
@@ -31,7 +27,7 @@
 #include "frontends/LyXView.h"
 #include "vc-backend.h"
 #include "TextCache.h"
-#include "lyxtextclasslist.h"
+#include "lyxlex.h"
 
 #include "frontends/Alert.h"
 
@@ -41,6 +37,9 @@
 #include "support/lyxfunctional.h"
 #include "support/LAssert.h"
 
+#include <boost/bind.hpp>
+#include "BoostFormat.h"
+
 #include <cassert>
 #include <algorithm>
 #include <functional>
@@ -55,43 +54,8 @@ using std::mem_fun;
 
 extern BufferView * current_view;
 
-//
-// Class BufferStorage
-//
-
-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
-               // the textcache that points to the buffer
-               // we just deleted.
-               Buffer * tmp = (*it);
-               container.erase(it);
-               textcache.removeAllWithBuffer(tmp);
-               delete tmp;
-       }
-}
-
-
-Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
-{
-       Buffer * tmpbuf = new Buffer(s, ronly);
-       tmpbuf->params.useClassDefaults();
-       lyxerr[Debug::INFO] << "Assigning to buffer "
-                           << container.size() << endl;
-       container.push_back(tmpbuf);
-       return tmpbuf;
-}
-
-
-//
-// Class BufferList
-//
 
 BufferList::BufferList()
-       : state_(BufferList::OK)
 {}
 
 
@@ -141,7 +105,7 @@ bool BufferList::qwriteAll()
        BufferStorage::iterator it = bstore.begin();
        BufferStorage::iterator end = bstore.end();
        for (; it != end; ++it) {
-               if (!(*it)->isLyxClean()) {
+               if (!(*it)->isClean()) {
                        string fname;
                        if ((*it)->isUnnamed())
                                fname = OnlyFilename((*it)->fileName());
@@ -152,18 +116,39 @@ bool BufferList::qwriteAll()
                }
        }
 
-       if (!unsaved.empty() && lyxrc.exit_confirmation) {
-               return Alert::askQuestion(_("Some documents were not saved:"),
-                                         unsaved, _("Exit anyway?"));
-       }
-
        return true;
 }
 
 
+void BufferList::release(Buffer * buf)
+{
+       lyx::Assert(buf);
+       BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
+       if (it != bstore.end()) {
+               // Make sure that we don't store a LyXText in
+               // the textcache that points to the buffer
+               // we just deleted.
+               Buffer * tmp = (*it);
+               bstore.erase(it);
+               textcache.removeAllWithBuffer(tmp);
+               delete tmp;
+       }
+}
+
+
+Buffer * BufferList::newBuffer(string const & s, bool ronly)
+{
+       Buffer * tmpbuf = new Buffer(s, ronly);
+       tmpbuf->params.useClassDefaults();
+       lyxerr[Debug::INFO] << "Assigning to buffer "
+                           << bstore.size() << endl;
+       bstore.push_back(tmpbuf);
+       return tmpbuf;
+}
+
 void BufferList::closeAll()
 {
-       state_ = BufferList::CLOSING;
        // Since we are closing we can just as well delete all
        // in the textcache this will also speed the closing/quiting up a bit.
        textcache.clear();
@@ -171,7 +156,6 @@ void BufferList::closeAll()
        while (!bstore.empty()) {
                close(bstore.front());
        }
-       state_ = BufferList::OK;
 }
 
 
@@ -179,16 +163,7 @@ 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)
-
-       if (buf->getUser())
-               buf->getUser()->insetUnlock();
-
-       if (buf->paragraph && !buf->isLyxClean() && !quitting) {
-               if (buf->getUser())
-                       buf->getUser()->owner()->prohibitInput();
+       if (!buf->paragraphs.empty() && !buf->isClean() && !quitting) {
                string fname;
                if (buf->isUnnamed())
                        fname = OnlyFilename(buf->fileName());
@@ -206,8 +181,6 @@ bool BufferList::close(Buffer * buf)
                                        lastfiles->newFile(buf->fileName());
                                        reask = false;
                                } else {
-                                       if (buf->getUser())
-                                               buf->getUser()->owner()->allowInput();
                                        return false;
                                }
                                break;
@@ -218,16 +191,12 @@ bool BufferList::close(Buffer * buf)
                                reask = false;
                                break;
                        case 3: // Cancel
-                               if (buf->getUser())
-                                       buf->getUser()->owner()->allowInput();
                                return false;
                        }
                }
-               if (buf->getUser())
-                       buf->getUser()->owner()->allowInput();
        }
 
-       bstore.release(buf);
+       release(buf);
        return true;
 }
 
@@ -257,23 +226,6 @@ Buffer * BufferList::getBuffer(unsigned int choice)
 }
 
 
-int BufferList::unlockInset(UpdatableInset * inset)
-{
-       lyx::Assert(inset);
-
-       BufferStorage::iterator it = bstore.begin();
-       BufferStorage::iterator end = bstore.end();
-       for (; it != end; ++it) {
-               if ((*it)->getUser()
-                   && (*it)->getUser()->theLockingInset() == inset) {
-                       (*it)->getUser()->insetUnlock();
-                       return 0;
-               }
-       }
-       return 1;
-}
-
-
 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
 {
        BufferStorage::iterator it = bstore.begin();
@@ -294,7 +246,7 @@ void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
 void BufferList::emergencyWriteAll()
 {
        for_each(bstore.begin(), bstore.end(),
-                lyx::void_class_fun(*this, &BufferList::emergencyWrite));
+                boost::bind(&BufferList::emergencyWrite, this, _1));
 }
 
 
@@ -306,13 +258,19 @@ void BufferList::emergencyWrite(Buffer * buf)
 
 
        // No need to save if the buffer has not changed.
-       if (buf->isLyxClean())
+       if (buf->isClean())
                return;
 
-       lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
-                     buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
-                     : buf->fileName().c_str()) << endl;
+       string const doc = buf->isUnnamed()
+               ? OnlyFilename(buf->fileName()) : buf->fileName();
 
+#if USE_BOOST_FORMAT
+       lyxerr << boost::format(_("LyX: Attempting to save document %1$s"))
+               % doc
+              << endl;
+#else
+       lyxerr << _("LyX: Attempting to save document ") << doc << endl;
+#endif
        // We try to save three places:
 
        // 1) Same place as document. Unless it is an unnamed doc.
@@ -320,8 +278,8 @@ void BufferList::emergencyWrite(Buffer * buf)
                string s = buf->fileName();
                s += ".emergency";
                lyxerr << "  " << s << endl;
-               if (buf->writeFile(s, true)) {
-                       buf->markLyxClean();
+               if (buf->writeFile(s)) {
+                       buf->markClean();
                        lyxerr << _("  Save seems successful. Phew.") << endl;
                        return;
                } else {
@@ -332,9 +290,9 @@ void BufferList::emergencyWrite(Buffer * buf)
        // 2) In HOME directory.
        string s = AddName(GetEnvPath("HOME"), buf->fileName());
        s += ".emergency";
-       lyxerr << " " << s << endl;
-       if (buf->writeFile(s, true)) {
-               buf->markLyxClean();
+       lyxerr << ' ' << s << endl;
+       if (buf->writeFile(s)) {
+               buf->markClean();
                lyxerr << _("  Save seems successful. Phew.") << endl;
                return;
        }
@@ -346,9 +304,9 @@ void BufferList::emergencyWrite(Buffer * buf)
        // drive letter on OS/2
        s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
        s += ".emergency";
-       lyxerr << " " << s << endl;
-       if (buf->writeFile(s, true)) {
-               buf->markLyxClean();
+       lyxerr << ' ' << s << endl;
+       if (buf->writeFile(s)) {
+               buf->markClean();
                lyxerr << _("  Save seems successful. Phew.") << endl;
                return;
        }
@@ -371,7 +329,7 @@ Buffer * BufferList::readFile(string const & s, bool ronly)
                return 0;
        }
 
-       Buffer * b = bstore.newBuffer(s, ronly);
+       Buffer * b = newBuffer(s, ronly);
 
        // Check if emergency save file exists and is newer.
        e += OnlyFilename(s) + ".emergency";
@@ -423,10 +381,10 @@ Buffer * BufferList::readFile(string const & s, bool ronly)
        // not sure if this is the correct place to begin LyXLex
        LyXLex lex(0, 0);
        lex.setFile(ts);
-       if (b->readFile(lex))
+       if (b->readFile(lex, ts))
                return b;
        else {
-               bstore.release(b);
+               release(b);
                return 0;
        }
 }
@@ -462,7 +420,7 @@ Buffer * BufferList::getBuffer(string const & s)
 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
 {
        // get a free buffer
-       Buffer * b = bstore.newBuffer(name);
+       Buffer * b = newBuffer(name);
 
        // use defaults.lyx as a default template if it exists.
        if (tname.empty()) {
@@ -473,7 +431,7 @@ Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
                LyXLex lex(0, 0);
                lex.setFile(tname);
                if (lex.isOK()) {
-                       if (b->readFile(lex)) {
+                       if (b->readFile(lex, tname)) {
                                templateok = true;
                        }
                }
@@ -481,15 +439,15 @@ Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
                        Alert::alert(_("Error!"), _("Unable to open template"),
                                   MakeDisplayPath(tname));
                        // no template, start with empty buffer
-                       b->paragraph = new Paragraph;
-                       b->paragraph->layout(textclasslist[b->params.textclass].defaultLayoutName());
+                       b->paragraphs.set(new Paragraph);
+                       b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
                }
        } else {  // start with empty buffer
-               b->paragraph = new Paragraph;
-                       b->paragraph->layout(textclasslist[b->params.textclass].defaultLayoutName());
+               b->paragraphs.set(new Paragraph);
+               b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
        }
 
-       if (!lyxrc.new_ask_filename && !isNamed) {
+       if (!isNamed) {
                b->setUnnamed();
                b->setFileName(name);
        }
@@ -565,3 +523,13 @@ Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
 
        return b;
 }
+
+
+void BufferList::setCurrentAuthor(string const & name, string const & email)
+{
+       BufferStorage::iterator it = bstore.begin();
+       BufferStorage::iterator end = bstore.end();
+       for (; it != end; ++it) {
+               (*it)->authors().record(0, Author(name, email));
+       }
+}