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
24 #include "bufferlist.h"
26 #include "support/FileInfo.h"
27 #include "support/filetools.h"
28 #include "support/lyxmanip.h"
29 #include "support/lyxfunctional.h"
30 #include "lyx_gui_misc.h"
31 #include "lastfiles.h"
36 #include "bufferview_funcs.h"
37 #include "BufferView.h"
40 #include "vc-backend.h"
41 #include "TextCache.h"
50 extern BufferView * current_view;
53 // Class BufferStorage
56 void BufferStorage::release(Buffer * buf)
59 Container::iterator it = find(container.begin(), container.end(), buf);
60 if (it != container.end()) {
61 // Make sure that we don't store a LyXText in
62 // the textcache that points to the buffer
66 textcache.removeAllWithBuffer(tmp);
72 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
74 Buffer * tmpbuf = new Buffer(s, ronly);
75 tmpbuf->params.useClassDefaults();
76 lyxerr[Debug::INFO] << "Assigning to buffer "
77 << container.size() << endl;
78 container.push_back(tmpbuf);
87 BufferList::BufferList()
88 : state_(BufferList::OK)
92 bool BufferList::empty() const
94 return bstore.empty();
98 bool BufferList::QwriteAll()
100 bool askMoreConfirmation = false;
102 for (BufferStorage::iterator it = bstore.begin();
103 it != bstore.end(); ++it) {
104 if (!(*it)->isLyxClean()) {
106 if ((*it)->isUnnamed())
107 fname = OnlyFilename((*it)->fileName());
109 fname = MakeDisplayPath((*it)->fileName(), 50);
112 switch (AskConfirmation(_("Changes in document:"),
114 _("Save document?"))) {
116 if ((*it)->isUnnamed())
117 reask = !WriteAs(current_view, (*it));
119 reask = !MenuWrite(current_view, (*it));
123 // if we crash after this we could
124 // have no autosave file but I guess
125 // this is really inprobable (Jug)
126 if ((*it)->isUnnamed()) {
127 removeAutosaveFile((*it)->fileName());
129 askMoreConfirmation = true;
130 unsaved += MakeDisplayPath(fname, 50);
140 if (askMoreConfirmation &&
141 lyxrc.exit_confirmation &&
142 !AskQuestion(_("Some documents were not saved:"),
143 unsaved, _("Exit anyway?"))) {
151 void BufferList::closeAll()
153 state_ = BufferList::CLOSING;
154 // Since we are closing we can just as well delete all
155 // in the textcache this will also speed the closing/quiting up a bit.
158 while (!bstore.empty()) {
159 close(bstore.front());
161 state_ = BufferList::OK;
165 bool BufferList::close(Buffer * buf)
170 // Trace back why we need to use buf->getUser here.
171 // Perhaps slight rewrite is in order? (Lgb)
173 if (buf->getUser()) buf->getUser()->insetUnlock();
174 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
176 ProhibitInput(buf->getUser());
178 if (buf->isUnnamed())
179 fname = OnlyFilename(buf->fileName());
181 fname = MakeDisplayPath(buf->fileName(), 50);
184 switch (AskConfirmation(_("Changes in document:"),
186 _("Save document?"))){
188 if (buf->isUnnamed())
189 reask = !WriteAs(current_view, buf);
190 else if (buf->save()) {
191 lastfiles->newFile(buf->fileName());
195 AllowInput(buf->getUser());
200 if (buf->isUnnamed()) {
201 removeAutosaveFile(buf->fileName());
207 AllowInput(buf->getUser());
212 AllowInput(buf->getUser());
220 vector<string> const BufferList::getFileNames() const
223 std::copy(bstore.begin(), bstore.end(),
224 lyx::back_inserter_fun(nvec, &Buffer::fileName));
229 Buffer * BufferList::first()
231 if (bstore.empty()) return 0;
232 return bstore.front();
236 Buffer * BufferList::getBuffer(unsigned int choice)
238 if (choice >= bstore.size()) return 0;
239 return bstore[choice];
243 int BufferList::unlockInset(UpdatableInset * inset)
247 //if (!inset) return 1;
248 for (BufferStorage::iterator it = bstore.begin();
249 it != bstore.end(); ++it) {
251 && (*it)->getUser()->theLockingInset() == inset) {
252 (*it)->getUser()->insetUnlock();
260 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
262 for (BufferStorage::iterator it = bstore.begin();
263 it != bstore.end(); ++it) {
264 if (!(*it)->isDepClean(mastertmpdir)) {
265 string writefile = mastertmpdir;
267 writefile += (*it)->getLatexName();
268 (*it)->makeLaTeXFile(writefile, mastertmpdir,
270 (*it)->markDepClean(mastertmpdir);
276 void BufferList::emergencyWriteAll()
278 for_each(bstore.begin(), bstore.end(),
279 lyx::class_fun(*this, &BufferList::emergencyWrite));
283 void BufferList::emergencyWrite(Buffer * buf)
285 // assert(buf) // this is not good since C assert takes an int
286 // and a pointer is a long (JMarc)
287 assert(buf != 0); // use c assert to avoid a loop
290 // No need to save if the buffer has not changed.
291 if (buf->isLyxClean()) return;
293 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
294 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
295 : buf->fileName().c_str()) << endl;
297 // We try to save three places:
299 // 1) Same place as document. Unless it is an unnamed doc.
300 if (!buf->isUnnamed()) {
301 string s = buf->fileName();
303 lyxerr << " " << s << endl;
304 if (buf->writeFile(s, true)) {
306 lyxerr << _(" Save seems successful. Phew.") << endl;
309 lyxerr << _(" Save failed! Trying...") << endl;
313 // 2) In HOME directory.
314 string s = AddName(GetEnvPath("HOME"), buf->fileName());
316 lyxerr << " " << s << endl;
317 if (buf->writeFile(s, true)) {
319 lyxerr << _(" Save seems successful. Phew.") << endl;
323 lyxerr << _(" Save failed! Trying...") << endl;
325 // 3) In "/tmp" directory.
326 // MakeAbsPath to prepend the current
327 // drive letter on OS/2
328 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
330 lyxerr << " " << s << endl;
331 if (buf->writeFile(s, true)) {
333 lyxerr << _(" Save seems successful. Phew.") << endl;
336 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
341 Buffer * BufferList::readFile(string const & s, bool ronly)
343 Buffer * b = bstore.newBuffer(s, ronly);
346 string e = OnlyPath(s);
348 // File information about normal file
349 FileInfo fileInfo2(s);
351 // Check if emergency save file exists and is newer.
352 e += OnlyFilename(s) + ".emergency";
353 FileInfo fileInfoE(e);
355 bool use_emergency = false;
357 if (fileInfoE.exist() && fileInfo2.exist()) {
358 if (fileInfoE.getModificationTime()
359 > fileInfo2.getModificationTime()) {
360 if (AskQuestion(_("An emergency save of this document exists!"),
361 MakeDisplayPath(s, 50),
362 _("Try to load that instead?"))) {
364 // the file is not saved if we load the
367 use_emergency = true;
369 // Here, we should delete the emergency save
375 if (!use_emergency) {
376 // Now check if autosave file is newer.
378 a += OnlyFilename(s);
380 FileInfo fileInfoA(a);
381 if (fileInfoA.exist() && fileInfo2.exist()) {
382 if (fileInfoA.getModificationTime()
383 > fileInfo2.getModificationTime()) {
384 if (AskQuestion(_("Autosave file is newer."),
385 MakeDisplayPath(s, 50),
386 _("Load that one instead?"))) {
388 // the file is not saved if we load the
392 // Here, we should delete the autosave
398 // not sure if this is the correct place to begin LyXLex
401 if (b->readFile(lex))
410 bool BufferList::exists(string const & s) const
412 return find_if(bstore.begin(), bstore.end(),
413 lyx::compare_memfun(&Buffer::fileName, s))
418 bool BufferList::isLoaded(Buffer const * b) const
422 BufferStorage::const_iterator cit =
423 find(bstore.begin(), bstore.end(), b);
424 return cit != bstore.end();
428 Buffer * BufferList::getBuffer(string const & s)
430 BufferStorage::iterator it =
431 find_if(bstore.begin(), bstore.end(),
432 lyx::compare_memfun(&Buffer::fileName, s));
433 return it != bstore.end() ? (*it) : 0;
437 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
440 Buffer * b = bstore.newBuffer(name);
442 // use defaults.lyx as a default template if it exists.
444 tname = LibFileSearch("templates", "defaults.lyx");
446 if (!tname.empty() && IsLyXFilename(tname)) {
447 bool templateok = false;
451 if (b->readFile(lex)) {
456 WriteAlert(_("Error!"), _("Unable to open template"),
457 MakeDisplayPath(tname));
458 // no template, start with empty buffer
459 b->paragraph = new Paragraph;
461 } else { // start with empty buffer
462 b->paragraph = new Paragraph;
465 if (!lyxrc.new_ask_filename && !isNamed) {
467 b->setFileName(name);
470 b->setReadonly(false);
476 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
478 // make sure our path is absolute
479 string const s = MakeAbsPath(filename);
481 // file already open?
483 if (AskQuestion(_("Document is already open:"),
484 MakeDisplayPath(s, 50),
485 _("Do you want to reload that document?"))) {
486 // Reload is accomplished by closing and then loading
487 if (!close(getBuffer(s))) {
490 // Fall through to new load. (Asger)
492 // Here, we pretend that we just loaded the
499 switch (IsFileWriteable(s)) {
506 b->lyxvc.file_found_hook(s);
508 break; //fine- it's r/w
510 // Here we probably should run
511 if (LyXVC::file_not_found_hook(s)) {
512 // Ask if the file should be checked out for
513 // viewing/editing, if so: load it.
514 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
515 // How can we know _how_ to do the checkout?
516 // With the current VC support it has to be,
517 // a RCS file since CVS do not have special ,v files.
519 return loadLyXFile(filename, tolastfiles);
522 if (AskQuestion(_("Cannot open specified file:"),
523 MakeDisplayPath(s, 50),
524 _("Create new document with this name?")))
526 // Find a free buffer
527 b = newFile(s, string(), true);
532 if (b && tolastfiles)
533 lastfiles->newFile(b->fileName());