1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Word Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * This file is Copyright 1996-2001
12 * ======================================================
16 #pragma implementation
25 #include "bufferlist.h"
27 #include "support/FileInfo.h"
28 #include "support/filetools.h"
29 #include "support/lyxmanip.h"
30 #include "support/lyxfunctional.h"
31 #include "lyx_gui_misc.h"
32 #include "lastfiles.h"
37 #include "bufferview_funcs.h"
38 #include "BufferView.h"
41 #include "vc-backend.h"
42 #include "TextCache.h"
51 extern BufferView * current_view;
54 // Class BufferStorage
57 void BufferStorage::release(Buffer * buf)
60 Container::iterator it = find(container.begin(), container.end(), buf);
61 if (it != container.end()) {
62 // Make sure that we don't store a LyXText in
63 // the textcache that points to the buffer
67 textcache.removeAllWithBuffer(tmp);
73 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
75 Buffer * tmpbuf = new Buffer(s, ronly);
76 tmpbuf->params.useClassDefaults();
77 lyxerr[Debug::INFO] << "Assigning to buffer "
78 << container.size() << endl;
79 container.push_back(tmpbuf);
88 BufferList::BufferList()
89 : state_(BufferList::OK)
93 bool BufferList::empty() const
95 return bstore.empty();
99 bool BufferList::qwriteAll()
101 bool askMoreConfirmation = false;
103 for (BufferStorage::iterator it = bstore.begin();
104 it != bstore.end(); ++it) {
105 if (!(*it)->isLyxClean()) {
107 if ((*it)->isUnnamed())
108 fname = OnlyFilename((*it)->fileName());
110 fname = MakeDisplayPath((*it)->fileName(), 50);
113 switch (AskConfirmation(_("Changes in document:"),
115 _("Save document?"))) {
117 if ((*it)->isUnnamed())
118 reask = !WriteAs(current_view, (*it));
120 reask = !MenuWrite(current_view, (*it));
124 // if we crash after this we could
125 // have no autosave file but I guess
126 // this is really inprobable (Jug)
127 if ((*it)->isUnnamed()) {
128 removeAutosaveFile((*it)->fileName());
130 askMoreConfirmation = true;
131 unsaved += MakeDisplayPath(fname, 50);
141 if (askMoreConfirmation &&
142 lyxrc.exit_confirmation &&
143 !AskQuestion(_("Some documents were not saved:"),
144 unsaved, _("Exit anyway?"))) {
152 void BufferList::closeAll()
154 state_ = BufferList::CLOSING;
155 // Since we are closing we can just as well delete all
156 // in the textcache this will also speed the closing/quiting up a bit.
159 while (!bstore.empty()) {
160 close(bstore.front());
162 state_ = BufferList::OK;
166 bool BufferList::close(Buffer * buf)
171 // Trace back why we need to use buf->getUser here.
172 // Perhaps slight rewrite is in order? (Lgb)
174 if (buf->getUser()) buf->getUser()->insetUnlock();
175 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
177 ProhibitInput(buf->getUser());
179 if (buf->isUnnamed())
180 fname = OnlyFilename(buf->fileName());
182 fname = MakeDisplayPath(buf->fileName(), 50);
185 switch (AskConfirmation(_("Changes in document:"),
187 _("Save document?"))){
189 if (buf->isUnnamed())
190 reask = !WriteAs(current_view, buf);
191 else if (buf->save()) {
192 lastfiles->newFile(buf->fileName());
196 AllowInput(buf->getUser());
201 if (buf->isUnnamed()) {
202 removeAutosaveFile(buf->fileName());
208 AllowInput(buf->getUser());
213 AllowInput(buf->getUser());
221 vector<string> const BufferList::getFileNames() const
224 std::copy(bstore.begin(), bstore.end(),
225 lyx::back_inserter_fun(nvec, &Buffer::fileName));
230 Buffer * BufferList::first()
232 if (bstore.empty()) return 0;
233 return bstore.front();
237 Buffer * BufferList::getBuffer(unsigned int choice)
239 if (choice >= bstore.size()) return 0;
240 return bstore[choice];
244 int BufferList::unlockInset(UpdatableInset * inset)
248 //if (!inset) return 1;
249 for (BufferStorage::iterator it = bstore.begin();
250 it != bstore.end(); ++it) {
252 && (*it)->getUser()->theLockingInset() == inset) {
253 (*it)->getUser()->insetUnlock();
261 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
263 for (BufferStorage::iterator it = bstore.begin();
264 it != bstore.end(); ++it) {
265 if (!(*it)->isDepClean(mastertmpdir)) {
266 string writefile = mastertmpdir;
268 writefile += (*it)->getLatexName();
269 (*it)->makeLaTeXFile(writefile, mastertmpdir,
271 (*it)->markDepClean(mastertmpdir);
277 void BufferList::emergencyWriteAll()
279 for_each(bstore.begin(), bstore.end(),
280 lyx::class_fun(*this, &BufferList::emergencyWrite));
284 void BufferList::emergencyWrite(Buffer * buf)
286 // assert(buf) // this is not good since C assert takes an int
287 // and a pointer is a long (JMarc)
288 assert(buf != 0); // use c assert to avoid a loop
291 // No need to save if the buffer has not changed.
292 if (buf->isLyxClean()) return;
294 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
295 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
296 : buf->fileName().c_str()) << endl;
298 // We try to save three places:
300 // 1) Same place as document. Unless it is an unnamed doc.
301 if (!buf->isUnnamed()) {
302 string s = buf->fileName();
304 lyxerr << " " << s << endl;
305 if (buf->writeFile(s, true)) {
307 lyxerr << _(" Save seems successful. Phew.") << endl;
310 lyxerr << _(" Save failed! Trying...") << endl;
314 // 2) In HOME directory.
315 string s = AddName(GetEnvPath("HOME"), buf->fileName());
317 lyxerr << " " << s << endl;
318 if (buf->writeFile(s, true)) {
320 lyxerr << _(" Save seems successful. Phew.") << endl;
324 lyxerr << _(" Save failed! Trying...") << endl;
326 // 3) In "/tmp" directory.
327 // MakeAbsPath to prepend the current
328 // drive letter on OS/2
329 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
331 lyxerr << " " << s << endl;
332 if (buf->writeFile(s, true)) {
334 lyxerr << _(" Save seems successful. Phew.") << endl;
337 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
342 Buffer * BufferList::readFile(string const & s, bool ronly)
344 Buffer * b = bstore.newBuffer(s, ronly);
347 string e = OnlyPath(s);
349 // File information about normal file
350 FileInfo fileInfo2(s);
352 // Check if emergency save file exists and is newer.
353 e += OnlyFilename(s) + ".emergency";
354 FileInfo fileInfoE(e);
356 bool use_emergency = false;
358 if (fileInfoE.exist() && fileInfo2.exist()) {
359 if (fileInfoE.getModificationTime()
360 > fileInfo2.getModificationTime()) {
361 if (AskQuestion(_("An emergency save of this document exists!"),
362 MakeDisplayPath(s, 50),
363 _("Try to load that instead?"))) {
365 // the file is not saved if we load the
368 use_emergency = true;
370 // Here, we should delete the emergency save
376 if (!use_emergency) {
377 // Now check if autosave file is newer.
379 a += OnlyFilename(s);
381 FileInfo fileInfoA(a);
382 if (fileInfoA.exist() && fileInfo2.exist()) {
383 if (fileInfoA.getModificationTime()
384 > fileInfo2.getModificationTime()) {
385 if (AskQuestion(_("Autosave file is newer."),
386 MakeDisplayPath(s, 50),
387 _("Load that one instead?"))) {
389 // the file is not saved if we load the
393 // Here, we should delete the autosave
399 // not sure if this is the correct place to begin LyXLex
402 if (b->readFile(lex))
411 bool BufferList::exists(string const & s) const
413 return find_if(bstore.begin(), bstore.end(),
414 lyx::compare_memfun(&Buffer::fileName, s))
419 bool BufferList::isLoaded(Buffer const * b) const
423 BufferStorage::const_iterator cit =
424 find(bstore.begin(), bstore.end(), b);
425 return cit != bstore.end();
429 Buffer * BufferList::getBuffer(string const & s)
431 BufferStorage::iterator it =
432 find_if(bstore.begin(), bstore.end(),
433 lyx::compare_memfun(&Buffer::fileName, s));
434 return it != bstore.end() ? (*it) : 0;
438 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
441 Buffer * b = bstore.newBuffer(name);
443 // use defaults.lyx as a default template if it exists.
445 tname = LibFileSearch("templates", "defaults.lyx");
447 if (!tname.empty()) {
448 bool templateok = false;
452 if (b->readFile(lex)) {
457 WriteAlert(_("Error!"), _("Unable to open template"),
458 MakeDisplayPath(tname));
459 // no template, start with empty buffer
460 b->paragraph = new Paragraph;
462 } else { // start with empty buffer
463 b->paragraph = new Paragraph;
466 if (!lyxrc.new_ask_filename && !isNamed) {
468 b->setFileName(name);
471 b->setReadonly(false);
477 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
479 // make sure our path is absolute
480 string const s = MakeAbsPath(filename);
482 // file already open?
484 if (AskQuestion(_("Document is already open:"),
485 MakeDisplayPath(s, 50),
486 _("Do you want to reload that document?"))) {
487 // Reload is accomplished by closing and then loading
488 if (!close(getBuffer(s))) {
491 // Fall through to new load. (Asger)
493 // Here, we pretend that we just loaded the
500 switch (IsFileWriteable(s)) {
507 b->lyxvc.file_found_hook(s);
509 break; //fine- it's r/w
511 // Here we probably should run
512 if (LyXVC::file_not_found_hook(s)) {
513 // Ask if the file should be checked out for
514 // viewing/editing, if so: load it.
515 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
516 // How can we know _how_ to do the checkout?
517 // With the current VC support it has to be,
518 // a RCS file since CVS do not have special ,v files.
520 return loadLyXFile(filename, tolastfiles);
523 if (AskQuestion(_("Cannot open specified file:"),
524 MakeDisplayPath(s, 50),
525 _("Create new document with this name?")))
527 // Find a free buffer
528 b = newFile(s, string(), true);
533 if (b && tolastfiles)
534 lastfiles->newFile(b->fileName());