1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Word Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * This file is Copyright 1996-2000
12 * ======================================================
16 #pragma implementation
24 #include "bufferlist.h"
26 #include "minibuffer.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"
40 #include "vc-backend.h"
41 #include "TextCache.h"
50 extern BufferView * current_view;
53 // Class BufferStorage
56 void BufferStorage::release(Buffer * buf)
58 Container::iterator it = find(container.begin(), container.end(), buf);
59 if (it != container.end()) {
60 // Make sure that we don't store a LyXText in
61 // the textcache that points to the buffer
65 textcache.removeAllWithBuffer(tmp);
71 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
73 Buffer * tmpbuf = new Buffer(s, ronly);
74 tmpbuf->params.useClassDefaults();
75 lyxerr.debug() << "Assigning to buffer "
76 << container.size() << endl;
77 container.push_back(tmpbuf);
86 BufferList::BufferList()
87 : state_(BufferList::OK)
91 bool BufferList::empty() const
93 return bstore.empty();
97 bool BufferList::QwriteAll()
99 bool askMoreConfirmation = false;
101 for (BufferStorage::iterator it = bstore.begin();
102 it != bstore.end(); ++it) {
103 if (!(*it)->isLyxClean()) {
105 if ((*it)->isUnnamed())
106 fname = OnlyFilename((*it)->fileName());
108 fname = MakeDisplayPath((*it)->fileName(), 50);
111 switch (AskConfirmation(_("Changes in document:"),
113 _("Save document?"))) {
115 if ((*it)->isUnnamed())
116 reask = !MenuWriteAs(current_view, (*it));
118 reask = !MenuWrite(current_view, (*it));
122 // if we crash after this we could
123 // have no autosave file but I guess
124 // this is really inprobable (Jug)
125 if ((*it)->isUnnamed()) {
126 removeAutosaveFile((*it)->fileName());
128 askMoreConfirmation = true;
129 unsaved += MakeDisplayPath(fname, 50);
139 if (askMoreConfirmation &&
140 lyxrc.exit_confirmation &&
141 !AskQuestion(_("Some documents were not saved:"),
142 unsaved, _("Exit anyway?"))) {
150 void BufferList::closeAll()
152 state_ = BufferList::CLOSING;
153 // Since we are closing we can just as well delete all
154 // in the textcache this will also speed the closing/quiting up a bit.
157 while (!bstore.empty()) {
158 close(bstore.front());
160 state_ = BufferList::OK;
164 void BufferList::resize()
166 for_each(bstore.begin(), bstore.end(), mem_fun(&Buffer::resize));
170 bool BufferList::close(Buffer * buf)
173 // Trace back why we need to use buf->getUser here.
174 // Perhaps slight rewrite is in order? (Lgb)
176 if (buf->getUser()) buf->getUser()->insetUnlock();
177 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
179 ProhibitInput(buf->getUser());
181 if (buf->isUnnamed())
182 fname = OnlyFilename(buf->fileName());
184 fname = MakeDisplayPath(buf->fileName(), 50);
187 switch (AskConfirmation(_("Changes in document:"),
189 _("Save document?"))){
191 if (buf->isUnnamed())
192 reask = !MenuWriteAs(current_view, buf);
193 else if (buf->save()) {
194 lastfiles->newFile(buf->fileName());
198 AllowInput(buf->getUser());
203 if (buf->isUnnamed()) {
204 removeAutosaveFile(buf->fileName());
210 AllowInput(buf->getUser());
215 AllowInput(buf->getUser());
223 vector<string> const BufferList::getFileNames() const
226 std::copy(bstore.begin(), bstore.end(),
227 back_inserter_fun(nvec, &Buffer::fileName));
232 Buffer * BufferList::first()
234 if (bstore.empty()) return 0;
235 return bstore.front();
239 Buffer * BufferList::getBuffer(unsigned int choice)
241 if (choice >= bstore.size()) return 0;
242 return bstore[choice];
246 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 class_fun(*this, &BufferList::emergencyWrite));
284 void BufferList::emergencyWrite(Buffer * buf)
286 // No need to save if the buffer has not changed.
287 if (buf->isLyxClean()) return;
289 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
290 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
291 : buf->fileName().c_str()) << endl;
293 // We try to save three places:
295 // 1) Same place as document. Unless it is an unnamed doc.
296 if (!buf->isUnnamed()) {
297 string s = buf->fileName();
299 lyxerr << " " << s << endl;
300 if (buf->writeFile(s, true)) {
302 lyxerr << _(" Save seems successful. Phew.") << endl;
305 lyxerr << _(" Save failed! Trying...") << endl;
309 // 2) In HOME directory.
310 string s = AddName(GetEnvPath("HOME"), buf->fileName());
312 lyxerr << " " << s << endl;
313 if (buf->writeFile(s, true)) {
315 lyxerr << _(" Save seems successful. Phew.") << endl;
319 lyxerr << _(" Save failed! Trying...") << endl;
321 // 3) In "/tmp" directory.
322 // MakeAbsPath to prepend the current
323 // drive letter on OS/2
324 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
326 lyxerr << " " << s << endl;
327 if (buf->writeFile(s, true)) {
329 lyxerr << _(" Save seems successful. Phew.") << endl;
332 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
337 Buffer * BufferList::readFile(string const & s, bool ronly)
339 Buffer * b = bstore.newBuffer(s, ronly);
342 string e = OnlyPath(s);
344 // File information about normal file
345 FileInfo fileInfo2(s);
347 // Check if emergency save file exists and is newer.
348 e += OnlyFilename(s) + ".emergency";
349 FileInfo fileInfoE(e);
351 bool use_emergency = false;
353 if (fileInfoE.exist() && fileInfo2.exist()) {
354 if (fileInfoE.getModificationTime()
355 > fileInfo2.getModificationTime()) {
356 if (AskQuestion(_("An emergency save of this document exists!"),
357 MakeDisplayPath(s, 50),
358 _("Try to load that instead?"))) {
360 // the file is not saved if we load the
363 use_emergency = true;
365 // Here, we should delete the emergency save
371 if (!use_emergency) {
372 // Now check if autosave file is newer.
374 a += OnlyFilename(s);
376 FileInfo fileInfoA(a);
377 if (fileInfoA.exist() && fileInfo2.exist()) {
378 if (fileInfoA.getModificationTime()
379 > fileInfo2.getModificationTime()) {
380 if (AskQuestion(_("Autosave file is newer."),
381 MakeDisplayPath(s, 50),
382 _("Load that one instead?"))) {
384 // the file is not saved if we load the
388 // Here, we should delete the autosave
394 // not sure if this is the correct place to begin LyXLex
397 if (b->readFile(lex))
406 bool BufferList::exists(string const & s) const
408 return find_if(bstore.begin(), bstore.end(),
409 compare_memfun(&Buffer::fileName, s)) != bstore.end();
413 bool BufferList::isLoaded(Buffer const * b) const
415 BufferStorage::const_iterator cit =
416 find(bstore.begin(), bstore.end(), b);
417 return cit != bstore.end();
421 Buffer * BufferList::getBuffer(string const & s)
423 BufferStorage::iterator it =
424 find_if(bstore.begin(), bstore.end(),
425 compare_memfun(&Buffer::fileName, s));
426 return it != bstore.end() ? (*it) : 0;
430 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
433 Buffer * b = bstore.newBuffer(name);
435 // use defaults.lyx as a default template if it exists.
437 tname = LibFileSearch("templates", "defaults.lyx");
439 if (!tname.empty() && IsLyXFilename(tname)) {
440 bool templateok = false;
444 if (b->readFile(lex)) {
449 WriteAlert(_("Error!"), _("Unable to open template"),
450 MakeDisplayPath(tname));
451 // no template, start with empty buffer
452 b->paragraph = new LyXParagraph;
454 } else { // start with empty buffer
455 b->paragraph = new LyXParagraph;
458 if (!lyxrc.new_ask_filename && !isNamed) {
460 b->setFileName(name);
463 b->setReadonly(false);
469 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
471 // make sure our path is absolute
472 string const s = MakeAbsPath(filename);
474 // file already open?
476 if (AskQuestion(_("Document is already open:"),
477 MakeDisplayPath(s, 50),
478 _("Do you want to reload that document?"))) {
479 // Reload is accomplished by closing and then loading
480 if (!close(getBuffer(s))) {
483 // Fall through to new load. (Asger)
485 // Here, we pretend that we just loaded the
492 switch (IsFileWriteable(s)) {
495 current_view->owner()->getMiniBuffer()->
496 Set(_("File `") + MakeDisplayPath(s, 50) +
497 _("' is read-only."));
504 b->lyxvc.file_found_hook(s);
506 break; //fine- it's r/w
508 // Here we probably should run
509 if (LyXVC::file_not_found_hook(s)) {
510 // Ask if the file should be checked out for
511 // viewing/editing, if so: load it.
512 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
513 // How can we know _how_ to do the checkout?
514 // With the current VC support it has to be,
515 // a RCS file since CVS do not have special ,v files.
517 return loadLyXFile(filename, tolastfiles);
520 if (AskQuestion(_("Cannot open specified file:"),
521 MakeDisplayPath(s, 50),
522 _("Create new document with this name?")))
524 // Find a free buffer
525 b = newFile(s, string(), true);
530 if (b && tolastfiles)
531 lastfiles->newFile(b->fileName());