/* This file is part of
- * ======================================================
+ * ======================================================
*
* LyX, The Document Word Processor
*
* This file is Copyright 1996-1999
* Lars Gullik Bjønnes
*
- *======================================================
+ * ======================================================
*/
#ifdef __GNUG__
#include "support/filetools.h"
#include "lyx_gui_misc.h"
#include "lastfiles.h"
-#include "error.h"
+#include "debug.h"
#include "lyxrc.h"
#include "lyxscreen.h"
#include "lyxtext.h"
#include "lyx_cb.h"
#include "gettext.h"
-extern BufferView *current_view;
-extern MiniBuffer *minibuffer;
+extern BufferView * current_view;
+extern MiniBuffer * minibuffer;
extern void SmallUpdate(signed char);
extern void BeforeChange();
extern int RunLinuxDoc(int, string const &);
// Class BufferStorage
//
+#ifndef NEW_STORE
BufferStorage::BufferStorage()
{
// Initialize the buffer array
- for (int i=NUMBER_OF_BUFFERS-1; i >=0; i--) {
+ for (int i = NUMBER_OF_BUFFERS-1; i >= 0; i--) {
buffer[i] = 0;
- }
+ }
}
+#endif
-
+#ifndef NEW_STORE
bool BufferStorage::isEmpty()
{
- for (int i=NUMBER_OF_BUFFERS-1; i >=0; i--) {
+ for (int i = NUMBER_OF_BUFFERS-1; i >= 0; i--) {
if (buffer[i]) return false;
}
return true;
}
+#endif
-
-void BufferStorage::release(Buffer* buf)
+void BufferStorage::release(Buffer * buf)
{
- int i=0;
- for (i=0; i<NUMBER_OF_BUFFERS; i++)
+#ifdef NEW_STORE
+ for(Container::iterator it = container.begin();
+ it != container.end(); ++it) {
+ if ((*it) == buf) {
+ Buffer * tmpbuf = (*it);
+ container.erase(it);
+ delete tmpbuf;
+ break;
+ }
+ }
+#else
+ int i = 0;
+ for (i = 0; i < NUMBER_OF_BUFFERS; i++)
if (buffer[i] == buf) break;
- Buffer *tmpbuf = buffer[i];
+ Buffer * tmpbuf = buffer[i];
buffer[i] = 0;
delete tmpbuf;
+#endif
}
-Buffer* BufferStorage::newBuffer(string const &s,
- LyXRC *lyxrc,
+Buffer * BufferStorage::newBuffer(string const & s,
+ LyXRC * lyxrc,
bool ronly)
{
- int i=0;
+#ifdef NEW_STORE
+ Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
+ tmpbuf->params.useClassDefaults();
+ lyxerr.debug() << "Assigning to buffer "
+ << container.size() + 1 << endl;
+ container.push_back(tmpbuf);
+ return tmpbuf;
+#else
+ int i = 0;
while (i < NUMBER_OF_BUFFERS - 1
&& buffer[i]) i++;
buffer[i] = new Buffer(s, lyxrc, ronly);
buffer[i]->params.useClassDefaults();
- lyxerr.debug(string("Assigning to buffer ") + tostr(i), Error::ANY);
+ lyxerr.debug() << "Assigning to buffer " << i << endl;
return buffer[i];
+#endif
}
+#ifndef NEW_STORE
//
// Class BufferStrorage_Iter
//
-Buffer* BufferStorage_Iter::operator() ()
+Buffer * BufferStorage_Iter::operator() ()
{
- int i=0;
- for (i=index; i < BufferStorage::NUMBER_OF_BUFFERS; i++) {
+ int i = 0;
+ for (i = index; i < BufferStorage::NUMBER_OF_BUFFERS; i++) {
if (cs->buffer[i]) {
- index = i+1;
+ index = i + 1;
return cs->buffer[i];
}
}
}
-Buffer* BufferStorage_Iter::operator[] (int a)
+Buffer * BufferStorage_Iter::operator[] (int a)
{
- // a is >=1
- if (a<=0) return 0;
+ // a is >= 1
+ if (a <= 0) return 0;
- int i=0;
+ int i = 0;
while (a--) {
while(!cs->buffer[i++]);
}
- if (i-1 < BufferStorage::NUMBER_OF_BUFFERS)
- return cs->buffer[i-1];
+ if (i - 1 < BufferStorage::NUMBER_OF_BUFFERS)
+ return cs->buffer[i - 1];
return 0;
}
+#endif
//
}
-BufferList::~BufferList()
+bool BufferList::empty()
{
- // I am sure something should be done here too.
+ return bstore.empty();
}
-bool BufferList::isEmpty()
-{
- return bstore.isEmpty();
-}
-
-extern void MenuWrite(Buffer*);
+extern void MenuWrite(Buffer *);
bool BufferList::QwriteAll()
{
bool askMoreConfirmation = false;
string unsaved;
+#ifdef NEW_STORE
+ for(BufferStorage::iterator it = bstore.begin();
+ it != bstore.end(); ++it) {
+ if (!(*it)->isLyxClean()) {
+ switch(AskConfirmation(_("Changes in document:"),
+ MakeDisplayPath((*it)->filename,
+ 50),
+ _("Save document?"))) {
+ case 1: // Yes
+ MenuWrite((*it));
+ break;
+ case 2: // No
+ askMoreConfirmation = true;
+ unsaved += MakeDisplayPath((*it)->filename, 50);
+ unsaved += "\n";
+ break;
+ case 3: // Cancel
+ return false;
+ }
+ }
+ }
+#else
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
- while ((b=biter())) {
+ Buffer * b = 0;
+ while ((b = biter())) {
if (!b->isLyxClean()) {
switch(AskConfirmation(_("Changes in document:"),
- MakeDisplayPath(b->filename,50),
+ MakeDisplayPath(b->filename, 50),
_("Save document?"))) {
case 1: // Yes
MenuWrite(b);
break;
case 2: // No
askMoreConfirmation = true;
- unsaved += MakeDisplayPath(b->filename,50);
+ unsaved += MakeDisplayPath(b->filename, 50);
unsaved += "\n";
break;
case 3: // Cancel
}
}
}
+#endif
if (askMoreConfirmation &&
lyxrc->exit_confirmation &&
!AskQuestion(_("Some documents were not saved:"),
// Should probably be moved to somewhere else: BufferView? LyXView?
-bool BufferList::write(Buffer *buf, bool makeBackup)
+bool BufferList::write(Buffer * buf, bool makeBackup)
{
minibuffer->Set(_("Saving document"),
- MakeDisplayPath(buf->filename),"...");
+ MakeDisplayPath(buf->filename), "...");
// We don't need autosaves in the immediate future. (Asger)
buf->resetAutosaveTimers();
if (finfo.exist()) {
mode_t fmode = finfo.getMode();
- struct utimbuf *times =
+ struct utimbuf *times =
(struct utimbuf*)new char[sizeof(struct utimbuf)];
times->actime = finfo.getAccessTime();
times->modtime = finfo.getModificationTime();
long blksize = finfo.getBlockSize();
- lyxerr.debug(string("BlockSize: ") + tostr(blksize));
- FilePtr fin(buf->filename,FilePtr::read);
- FilePtr fout(s,FilePtr::truncate);
+ lyxerr.debug() << "BlockSize: " << blksize << endl;
+ FilePtr fin(buf->filename, FilePtr::read);
+ FilePtr fout(s, FilePtr::truncate);
if (fin() && fout()) {
char * cbuf = new char[blksize+1];
size_t c_read = 0;
do {
c_read = fread(cbuf, 1, blksize, fin);
if (c_read != 0)
- c_write =
+ c_write =
fwrite(cbuf, 1, c_read, fout);
} while (c_read);
fin.close();
chmod(s.c_str(), fmode);
if (utime(s.c_str(), times)) {
- lyxerr.print("utime error.");
+ lyxerr << "utime error." << endl;
}
delete [] cbuf;
} else {
- lyxerr.print("LyX was not able to make backupcopy. Beware.");
+ lyxerr << "LyX was not able to make backupcopy. Beware." << endl;
}
delete[] times;
}
}
- if (buf->writeFile(buf->filename,false)) {
+ if (buf->writeFile(buf->filename, false)) {
buf->markLyxClean();
minibuffer->Set(_("Document saved as"),
void BufferList::closeAll()
{
_state = BufferList::CLOSING;
-
+#ifdef NEW_STORE
+ while (!bstore.empty()) {
+ close(bstore.front());
+ }
+#else
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
- while ((b=biter())) {
+ Buffer * b = 0;
+ while ((b = biter())) {
close(b);
}
+#endif
_state = BufferList::OK;
}
void BufferList::resize()
{
+#ifdef NEW_STORE
+ for(BufferStorage::iterator it = bstore.begin();
+ it != bstore.end(); ++it) {
+ (*it)->resize();
+ }
+#else
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
- while ((b=biter())) {
+ Buffer * b = 0;
+ while ((b = biter())) {
b->resize();
}
+#endif
}
-bool BufferList::close(Buffer *buf)
+bool BufferList::close(Buffer * buf)
{
buf->InsetUnlock();
if (buf->paragraph && !buf->isLyxClean() && !quitting) {
ProhibitInput();
switch(AskConfirmation(_("Changes in document:"),
- MakeDisplayPath(buf->filename,50),
+ MakeDisplayPath(buf->filename, 50),
_("Save document?"))){
case 1: // Yes
if (write(buf)) {
in the same way as for lastfiles.[hC]
*/
{
- int ant=0;
+ int ant = 0;
+#ifdef NEW_STORE
+ for(BufferStorage::iterator it = bstore.begin();
+ it != bstore.end(); ++it) {
+ string relbuf = MakeDisplayPath((*it)->filename, 30);
+ fl_addtopup(pup, relbuf.c_str());
+ ++ant;
+ }
+#else
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
- while ((b=biter())) {
- string relbuf = MakeDisplayPath(b->filename,30);
+ Buffer * b = 0;
+ while ((b = biter())) {
+ string relbuf = MakeDisplayPath(b->filename, 30);
fl_addtopup(pup, relbuf.c_str());
- ant++;
+ ++ant;
}
- if (ant == 0) fl_addtopup(pup,_("No Documents Open!%t"));
+#endif
+ if (ant == 0) fl_addtopup(pup, _("No Documents Open!%t"));
}
-Buffer* BufferList::first()
+Buffer * BufferList::first()
{
+#ifdef NEW_STORE
+ if (bstore.empty()) return 0;
+ return bstore.front();
+#else
BufferStorage_Iter biter(bstore);
return biter();
+#endif
}
-Buffer* BufferList::getBuffer(int choice)
+Buffer * BufferList::getBuffer(int choice)
{
+#ifdef NEW_STORE
+ if (choice >= bstore.size()) return 0;
+ return bstore[choice];
+#else
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
+ Buffer * b = 0;
b = biter[choice];
// Be careful, this could be 0.
return b;
+#endif
}
-void BufferList::updateInset(Inset *inset, bool mark_dirty)
+void BufferList::updateInset(Inset * inset, bool mark_dirty)
{
+#ifdef NEW_STORE
+ for (BufferStorage::iterator it = bstore.begin();
+ it != bstore.end(); ++it) {
+ if ((*it)->text && (*it)->text->UpdateInset(inset)) {
+ if (mark_dirty)
+ (*it)->markDirty();
+ break;
+ }
+ }
+#else
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
- while ((b=biter())) {
+ Buffer *b = 0;
+ while ((b = biter())) {
if (b->text && b->text->UpdateInset(inset)) {
if (mark_dirty)
b->markDirty();
break;
}
}
+#endif
}
-int BufferList::unlockInset(UpdatableInset *inset)
+int BufferList::unlockInset(UpdatableInset * inset)
{
+#ifdef NEW_STORE
+ if (!inset) return 1;
+ for(BufferStorage::iterator it = bstore.begin();
+ it != bstore.end(); ++it) {
+ if ((*it)->the_locking_inset == inset) {
+ (*it)->InsetUnlock();
+ return 0;
+ }
+ }
+ return 1;
+#else
if (!inset) return 1;
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
- while ((b=biter())) {
+ Buffer *b = 0;
+ while ((b = biter())) {
if (b->the_locking_inset == inset) {
b->InsetUnlock();
return 0;
}
}
return 1;
+#endif
}
void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
{
+#ifdef NEW_STORE
+ for(BufferStorage::iterator it = bstore.begin();
+ it != bstore.end(); ++it) {
+ if (!(*it)->isDepClean(mastertmpdir)) {
+ string writefile = mastertmpdir;
+ writefile += '/';
+ writefile += ChangeExtension((*it)->getFileName(),
+ ".tex", true);
+ (*it)->makeLaTeXFile(writefile, mastertmpdir,
+ false, true);
+ (*it)->markDepClean(mastertmpdir);
+
+ }
+ }
+#else
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
- while ((b=biter())) {
+ Buffer *b = 0;
+ while ((b = biter())) {
if (!b->isDepClean(mastertmpdir)) {
string writefile = mastertmpdir;
writefile += '/';
writefile += ChangeExtension(b->getFileName(), ".tex", true);
- b->makeLaTeXFile(writefile,mastertmpdir,false,true);
+ b->makeLaTeXFile(writefile, mastertmpdir, false, true);
b->markDepClean(mastertmpdir);
}
}
+#endif
}
void BufferList::emergencyWriteAll()
{
+#ifdef NEW_STORE
+ for (BufferStorage::iterator it = bstore.begin();
+ it != bstore.end(); ++it) {
+ if (!(*it)->isLyxClean()) {
+ bool madeit = false;
+
+ lyxerr <<_("lyx: Attempting to save"
+ " document ")
+ << (*it)->filename
+ << _(" 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) {
+ 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;
+ }
+ }
+ }
+ }
+#else
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
- while ((b=biter())) {
+ Buffer * b = 0;
+ while ((b = biter())) {
if (!b->isLyxClean()) {
- bool madeit=false;
+ bool madeit = false;
- lyxerr.print(_("lyx: Attempting to save"
+ lyxerr <<_("lyx: Attempting to save"
" document ")
- + b->filename
- + _(" as..."));
+ << b->filename
+ << _(" as...") << endl;
- for (int i=0; i<3 && !madeit; i++) {
+ 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 (i == 0) {
s = b->filename;
- } else if (i==1) {
+ } else if (i == 1) {
s = AddName(GetEnvPath("HOME"),
b->filename);
} else { // MakeAbsPath to prepend the current drive letter on OS/2
}
s += ".emergency";
- lyxerr.print(string(" ") + tostr(i+1) + ") " + s);
+ lyxerr << " " << i + 1 << ") " << s << endl;
- if (b->writeFile(s,true)) {
+ if (b->writeFile(s, true)) {
b->markLyxClean();
- lyxerr.print(_(" Save seems successful. Phew."));
+ lyxerr << _(" Save seems successful. Phew.") << endl;
madeit = true;
} else if (i != 2) {
- lyxerr.print(_(" Save failed! Trying..."));
+ lyxerr << _(" Save failed! Trying...")
+ << endl;
} else {
- lyxerr.print(_(" Save failed! Bummer. Document is lost."));
+ lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
}
}
}
}
+#endif
}
Buffer* BufferList::readFile(string const & s, bool ronly)
{
- Buffer *b = bstore.newBuffer(s, lyxrc, ronly);
+ Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
string ts = s;
string e = OnlyPath(s);
if (fileInfoE.getModificationTime()
> fileInfo2.getModificationTime()) {
if (AskQuestion(_("An emergency save of this document exists!"),
- MakeDisplayPath(s,50),
+ MakeDisplayPath(s, 50),
_("Try to load that instead?"))) {
ts = e;
// the file is not saved if we load the
if (fileInfoA.getModificationTime()
> fileInfo2.getModificationTime()) {
if (AskQuestion(_("Autosave file is newer."),
- MakeDisplayPath(s,50),
+ MakeDisplayPath(s, 50),
_("Load that one instead?"))) {
ts = a;
// the file is not saved if we load the
bool BufferList::exists(string const & s)
{
+#ifdef NEW_STORE
+ for (BufferStorage::iterator it = bstore.begin();
+ it != bstore.end(); ++it) {
+ if ((*it)->filename == s)
+ return true;
+ }
+ return false;
+#else
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
- while ((b=biter())) {
+ Buffer * b = 0;
+ while ((b = biter())) {
if (b->filename == s)
return true;
}
return false;
+#endif
}
-Buffer* BufferList::getBuffer(string const &s)
+Buffer * BufferList::getBuffer(string const & s)
{
+#ifdef NEW_STORE
+ for(BufferStorage::iterator it = bstore.begin();
+ it != bstore.end(); ++it) {
+ if ((*it)->filename == s)
+ return (*it);
+ }
+ return 0;
+#else
BufferStorage_Iter biter(bstore);
- Buffer *b=0;
- while ((b=biter())) {
- if (b->filename ==s)
+ Buffer * b = 0;
+ while ((b = biter())) {
+ if (b->filename == s)
return b;
}
return 0;
+#endif
}
-Buffer* BufferList::newFile(string const & name, string tname)
+Buffer * BufferList::newFile(string const & name, string tname)
{
/* get a free buffer */
- Buffer *b = bstore.newBuffer(name, lyxrc);
+ Buffer * b = bstore.newBuffer(name, lyxrc);
// use defaults.lyx as a default template if it exists.
if (tname.empty()) {
}
if (!tname.empty() && IsLyXFilename(tname)){
bool templateok = false;
- LyXLex lex(0,0);
+ LyXLex lex(0, 0);
lex.setFile(tname);
if (lex.IsOK()) {
if (b->readFile(lex)) {
}
}
if (!templateok) {
- WriteAlert(_("Error!"),_("Unable to open template"),
+ WriteAlert(_("Error!"), _("Unable to open template"),
MakeDisplayPath(tname));
// no template, start with empty buffer
- b->paragraph = new LyXParagraph();
+ b->paragraph = new LyXParagraph;
}
}
else { // start with empty buffer
- b->paragraph = new LyXParagraph();
+ b->paragraph = new LyXParagraph;
}
b->markDirty();
}
-Buffer* BufferList::loadLyXFile(string const & filename, bool tolastfiles)
+Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
{
// make sure our path is absolute
string s = MakeAbsPath(filename);
s = ChangeExtension (s, ".lyx", false);
} else { // sgml2lyx failed
WriteAlert(_("Error!"),
- _("Could not convert file"),s);
+ _("Could not convert file"), s);
return 0;
}
} else {
// file already open?
if (exists(s)) {
if (AskQuestion(_("Document is already open:"),
- MakeDisplayPath(s,50),
+ MakeDisplayPath(s, 50),
_("Do you want to reload that document?"))) {
// Reload is accomplished by closing and then loading
if (!close(getBuffer(s))) {
return getBuffer(s);
}
}
- Buffer *b=0;
+ Buffer * b = 0;
bool ro = false;
switch (IsFileWriteable(s)) {
case 0:
- minibuffer->Set(_("File `")+MakeDisplayPath(s,50)+
+ minibuffer->Set(_("File `")+MakeDisplayPath(s, 50)+
_("' is read-only."));
ro = true;
// Fall through
case 1:
- b=readFile(s, ro);
+ b = readFile(s, ro);
if (b) {
b->lyxvc.file_found_hook(s);
}
if (LyXVC::file_not_found_hook(s)) {
// Ask if the file should be checked out for
// viewing/editing, if so: load it.
- lyxerr.print("Do you want to checkout?");
+ lyxerr << "Do you want to checkout?" << endl;
}
if (AskQuestion(_("Cannot open specified file:"),
- MakeDisplayPath(s,50),
+ MakeDisplayPath(s, 50),
_("Create new document with this name?")))
{
// Find a free buffer
- b = newFile(s,string());
+ b = newFile(s, string());
}
break;
}