]> git.lyx.org Git - lyx.git/blobdiff - src/bufferlist.C
Fix working of the spellchecker dialog with ispell when there are no
[lyx.git] / src / bufferlist.C
index 99fcd4dd45370d9258f30b3f90e9e55a4c989e60..c5af8316686d4dea579960ac7a608988cf70db86 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
  *
  * ====================================================== 
 
 #include <config.h>
 
+#include <assert.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 "support/LAssert.h"
 #include "lyx_gui_misc.h"
 #include "lastfiles.h"
 #include "debug.h"
@@ -35,6 +36,7 @@
 #include "lyxtext.h"
 #include "lyx_cb.h"
 #include "bufferview_funcs.h"
+#include "BufferView.h"
 #include "gettext.h"
 #include "LyXView.h"
 #include "vc-backend.h"
@@ -47,12 +49,15 @@ using std::find_if;
 using std::for_each;
 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
@@ -70,8 +75,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;
 }
@@ -92,10 +97,7 @@ bool BufferList::empty() const
 }
 
 
-extern bool MenuWrite(Buffer *);
-extern bool MenuWriteAs(Buffer *);
-
-bool BufferList::QwriteAll()
+bool BufferList::qwriteAll()
 {
         bool askMoreConfirmation = false;
         string unsaved;
@@ -108,15 +110,15 @@ bool BufferList::QwriteAll()
                        else
                                fname = MakeDisplayPath((*it)->fileName(), 50);
                        bool reask = true;
-                       while(reask) {
+                       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
@@ -162,14 +164,10 @@ void BufferList::closeAll()
 }
 
 
-void BufferList::resize()
-{
-       for_each(bstore.begin(), bstore.end(), mem_fun(&Buffer::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)
@@ -177,7 +175,7 @@ bool BufferList::close(Buffer * buf)
         if (buf->getUser()) buf->getUser()->insetUnlock();
        if (buf->paragraph && !buf->isLyxClean() && !quitting) {
                if (buf->getUser())
-                       ProhibitInput(buf->getUser());
+                       buf->getUser()->owner()->prohibitInput();
                string fname;
                if (buf->isUnnamed())
                        fname = OnlyFilename(buf->fileName());
@@ -190,13 +188,13 @@ bool BufferList::close(Buffer * buf)
                                               _("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;
                                } else {
                                        if (buf->getUser())
-                                               AllowInput(buf->getUser());
+                                               buf->getUser()->owner()->allowInput();
                                        return false;
                                }
                                break;
@@ -208,12 +206,12 @@ bool BufferList::close(Buffer * buf)
                                break;
                        case 3: // Cancel
                                if (buf->getUser())
-                                       AllowInput(buf->getUser());
+                                       buf->getUser()->owner()->allowInput();
                                return false;
                        }
                }
                if (buf->getUser())
-                       AllowInput(buf->getUser());
+                       buf->getUser()->owner()->allowInput();
        }
 
        bstore.release(buf);
@@ -225,7 +223,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;
 }
 
@@ -246,7 +244,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()
@@ -278,12 +278,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;
        
@@ -345,6 +350,12 @@ Buffer * BufferList::readFile(string const & s, bool ronly)
        // File information about normal file
        FileInfo fileInfo2(s);
 
+       if (!fileInfo2.exist()) {
+               WriteAlert(_("Error!"), _("Cannot open file"), 
+                       MakeDisplayPath(s));
+               return 0;
+       }
        // Check if emergency save file exists and is newer.
        e += OnlyFilename(s) + ".emergency";
        FileInfo fileInfoE(e);
@@ -407,12 +418,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();
@@ -423,7 +437,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;
 }
 
@@ -437,11 +451,11 @@ Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
        if (tname.empty()) {
                tname = LibFileSearch("templates", "defaults.lyx");
        }
-       if (!tname.empty() && IsLyXFilename(tname)) {
+       if (!tname.empty()) {
                bool templateok = false;
                LyXLex lex(0, 0);
                lex.setFile(tname);
-               if (lex.IsOK()) {
+               if (lex.isOK()) {
                        if (b->readFile(lex)) {
                                templateok = true;
                        }
@@ -450,10 +464,10 @@ Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
                        WriteAlert(_("Error!"), _("Unable to open template"), 
                                   MakeDisplayPath(tname));
                        // no template, start with empty buffer
-                       b->paragraph = new LyXParagraph;
+                       b->paragraph = new Paragraph;
                }
        } else {  // start with empty buffer
-               b->paragraph = new LyXParagraph;
+               b->paragraph = new Paragraph;
        }
 
        if (!lyxrc.new_ask_filename && !isNamed) {
@@ -469,8 +483,12 @@ 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 const s = MakeAbsPath(filename);
+       // get absolute path of file and add ".lyx" to the filename if
+       // necessary
+       string s = FileSearch(string(), filename, "lyx");
+       if (s.empty()) {
+               s = filename;
+       }
 
        // file already open?
        if (exists(s)) {
@@ -492,11 +510,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: