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 "frontends/Alert.h"
26 #include "bufferlist.h"
28 #include "support/FileInfo.h"
29 #include "support/filetools.h"
30 #include "support/lyxmanip.h"
31 #include "support/lyxfunctional.h"
32 #include "support/LAssert.h"
33 #include "lyx_gui_misc.h"
34 #include "lastfiles.h"
39 #include "bufferview_funcs.h"
40 #include "BufferView.h"
43 #include "vc-backend.h"
44 #include "TextCache.h"
53 extern BufferView * current_view;
56 // Class BufferStorage
59 void BufferStorage::release(Buffer * buf)
62 Container::iterator it = find(container.begin(), container.end(), buf);
63 if (it != container.end()) {
64 // Make sure that we don't store a LyXText in
65 // the textcache that points to the buffer
69 textcache.removeAllWithBuffer(tmp);
75 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
77 Buffer * tmpbuf = new Buffer(s, ronly);
78 tmpbuf->params.useClassDefaults();
79 lyxerr[Debug::INFO] << "Assigning to buffer "
80 << container.size() << endl;
81 container.push_back(tmpbuf);
90 BufferList::BufferList()
91 : state_(BufferList::OK)
95 bool BufferList::empty() const
97 return bstore.empty();
101 bool BufferList::qwriteOne(Buffer * buf, string const & fname, string & unsaved_list)
105 switch (Alert::askConfirmation(_("Changes in document:"),
107 _("Save document?"))) {
109 // FIXME: WriteAs can be asynch !
110 if (buf->isUnnamed())
111 reask = !WriteAs(current_view, buf);
113 reask = !MenuWrite(current_view, buf);
117 // if we crash after this we could
118 // have no autosave file but I guess
119 // this is really inprobable (Jug)
120 if (buf->isUnnamed()) {
121 removeAutosaveFile(buf->fileName());
124 unsaved_list += MakeDisplayPath(fname, 50) + "\n";
134 bool BufferList::qwriteAll()
137 for (BufferStorage::iterator it = bstore.begin();
138 it != bstore.end(); ++it)
140 if (!(*it)->isLyxClean()) {
142 if ((*it)->isUnnamed())
143 fname = OnlyFilename((*it)->fileName());
145 fname = MakeDisplayPath((*it)->fileName(), 50);
146 if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
151 if (!unsaved.empty() && lyxrc.exit_confirmation) {
152 return Alert::askQuestion(_("Some documents were not saved:"),
153 unsaved, _("Exit anyway?"));
160 void BufferList::closeAll()
162 state_ = BufferList::CLOSING;
163 // Since we are closing we can just as well delete all
164 // in the textcache this will also speed the closing/quiting up a bit.
167 while (!bstore.empty()) {
168 close(bstore.front());
170 state_ = BufferList::OK;
174 bool BufferList::close(Buffer * buf)
179 // Trace back why we need to use buf->getUser here.
180 // Perhaps slight rewrite is in order? (Lgb)
182 if (buf->getUser()) buf->getUser()->insetUnlock();
183 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
185 buf->getUser()->owner()->prohibitInput();
187 if (buf->isUnnamed())
188 fname = OnlyFilename(buf->fileName());
190 fname = MakeDisplayPath(buf->fileName(), 50);
193 switch (Alert::askConfirmation(_("Changes in document:"),
195 _("Save document?"))){
197 if (buf->isUnnamed())
198 reask = !WriteAs(current_view, buf);
199 else if (buf->save()) {
200 lastfiles->newFile(buf->fileName());
204 buf->getUser()->owner()->allowInput();
209 if (buf->isUnnamed()) {
210 removeAutosaveFile(buf->fileName());
216 buf->getUser()->owner()->allowInput();
221 buf->getUser()->owner()->allowInput();
229 vector<string> const BufferList::getFileNames() const
232 std::copy(bstore.begin(), bstore.end(),
233 lyx::back_inserter_fun(nvec, &Buffer::fileName));
238 Buffer * BufferList::first()
240 if (bstore.empty()) return 0;
241 return bstore.front();
245 Buffer * BufferList::getBuffer(unsigned int choice)
247 if (choice >= bstore.size()) return 0;
248 return bstore[choice];
252 int BufferList::unlockInset(UpdatableInset * inset)
256 //if (!inset) return 1;
257 for (BufferStorage::iterator it = bstore.begin();
258 it != bstore.end(); ++it) {
260 && (*it)->getUser()->theLockingInset() == inset) {
261 (*it)->getUser()->insetUnlock();
269 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
271 for (BufferStorage::iterator it = bstore.begin();
272 it != bstore.end(); ++it) {
273 if (!(*it)->isDepClean(mastertmpdir)) {
274 string writefile = mastertmpdir;
276 writefile += (*it)->getLatexName();
277 (*it)->makeLaTeXFile(writefile, mastertmpdir,
279 (*it)->markDepClean(mastertmpdir);
285 void BufferList::emergencyWriteAll()
287 for_each(bstore.begin(), bstore.end(),
288 lyx::class_fun(*this, &BufferList::emergencyWrite));
292 void BufferList::emergencyWrite(Buffer * buf)
294 // assert(buf) // this is not good since C assert takes an int
295 // and a pointer is a long (JMarc)
296 assert(buf != 0); // use c assert to avoid a loop
299 // No need to save if the buffer has not changed.
300 if (buf->isLyxClean()) return;
302 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
303 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
304 : buf->fileName().c_str()) << endl;
306 // We try to save three places:
308 // 1) Same place as document. Unless it is an unnamed doc.
309 if (!buf->isUnnamed()) {
310 string s = buf->fileName();
312 lyxerr << " " << s << endl;
313 if (buf->writeFile(s, true)) {
315 lyxerr << _(" Save seems successful. Phew.") << endl;
318 lyxerr << _(" Save failed! Trying...") << endl;
322 // 2) In HOME directory.
323 string s = AddName(GetEnvPath("HOME"), buf->fileName());
325 lyxerr << " " << s << endl;
326 if (buf->writeFile(s, true)) {
328 lyxerr << _(" Save seems successful. Phew.") << endl;
332 lyxerr << _(" Save failed! Trying...") << endl;
334 // 3) In "/tmp" directory.
335 // MakeAbsPath to prepend the current
336 // drive letter on OS/2
337 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
339 lyxerr << " " << s << endl;
340 if (buf->writeFile(s, true)) {
342 lyxerr << _(" Save seems successful. Phew.") << endl;
345 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
350 Buffer * BufferList::readFile(string const & s, bool ronly)
352 Buffer * b = bstore.newBuffer(s, ronly);
355 string e = OnlyPath(s);
357 // File information about normal file
358 FileInfo fileInfo2(s);
360 if (!fileInfo2.exist()) {
361 Alert::alert(_("Error!"), _("Cannot open file"),
366 // Check if emergency save file exists and is newer.
367 e += OnlyFilename(s) + ".emergency";
368 FileInfo fileInfoE(e);
370 bool use_emergency = false;
372 if (fileInfoE.exist() && fileInfo2.exist()) {
373 if (fileInfoE.getModificationTime()
374 > fileInfo2.getModificationTime()) {
375 if (Alert::askQuestion(_("An emergency save of this document exists!"),
376 MakeDisplayPath(s, 50),
377 _("Try to load that instead?"))) {
379 // the file is not saved if we load the
382 use_emergency = true;
384 // Here, we should delete the emergency save
390 if (!use_emergency) {
391 // Now check if autosave file is newer.
393 a += OnlyFilename(s);
395 FileInfo fileInfoA(a);
396 if (fileInfoA.exist() && fileInfo2.exist()) {
397 if (fileInfoA.getModificationTime()
398 > fileInfo2.getModificationTime()) {
399 if (Alert::askQuestion(_("Autosave file is newer."),
400 MakeDisplayPath(s, 50),
401 _("Load that one instead?"))) {
403 // the file is not saved if we load the
407 // Here, we should delete the autosave
413 // not sure if this is the correct place to begin LyXLex
416 if (b->readFile(lex))
425 bool BufferList::exists(string const & s) const
427 return find_if(bstore.begin(), bstore.end(),
428 lyx::compare_memfun(&Buffer::fileName, s))
433 bool BufferList::isLoaded(Buffer const * b) const
437 BufferStorage::const_iterator cit =
438 find(bstore.begin(), bstore.end(), b);
439 return cit != bstore.end();
443 Buffer * BufferList::getBuffer(string const & s)
445 BufferStorage::iterator it =
446 find_if(bstore.begin(), bstore.end(),
447 lyx::compare_memfun(&Buffer::fileName, s));
448 return it != bstore.end() ? (*it) : 0;
452 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
455 Buffer * b = bstore.newBuffer(name);
457 // use defaults.lyx as a default template if it exists.
459 tname = LibFileSearch("templates", "defaults.lyx");
461 if (!tname.empty()) {
462 bool templateok = false;
466 if (b->readFile(lex)) {
471 Alert::alert(_("Error!"), _("Unable to open template"),
472 MakeDisplayPath(tname));
473 // no template, start with empty buffer
474 b->paragraph = new Paragraph;
476 } else { // start with empty buffer
477 b->paragraph = new Paragraph;
480 if (!lyxrc.new_ask_filename && !isNamed) {
482 b->setFileName(name);
485 b->setReadonly(false);
491 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
493 // get absolute path of file and add ".lyx" to the filename if
495 string s = FileSearch(string(), filename, "lyx");
500 // file already open?
502 if (Alert::askQuestion(_("Document is already open:"),
503 MakeDisplayPath(s, 50),
504 _("Do you want to reload that document?"))) {
505 // Reload is accomplished by closing and then loading
506 if (!close(getBuffer(s))) {
509 // Fall through to new load. (Asger)
511 // Here, we pretend that we just loaded the
518 switch (IsFileWriteable(s)) {
525 b->lyxvc.file_found_hook(s);
527 break; //fine- it's r/w
529 // Here we probably should run
530 if (LyXVC::file_not_found_hook(s)) {
531 // Ask if the file should be checked out for
532 // viewing/editing, if so: load it.
533 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
534 // How can we know _how_ to do the checkout?
535 // With the current VC support it has to be,
536 // a RCS file since CVS do not have special ,v files.
538 return loadLyXFile(filename, tolastfiles);
541 if (Alert::askQuestion(_("Cannot open specified file:"),
542 MakeDisplayPath(s, 50),
543 _("Create new document with this name?")))
545 // Find a free buffer
546 b = newFile(s, string(), true);
551 if (b && tolastfiles)
552 lastfiles->newFile(b->fileName());