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 * ======================================================
18 #pragma implementation
21 #include "bufferlist.h"
23 #include "lastfiles.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
31 #include "frontends/LyXView.h"
32 #include "vc-backend.h"
33 #include "TextCache.h"
34 #include "lyxtextclasslist.h"
36 #include "frontends/Alert.h"
38 #include "support/FileInfo.h"
39 #include "support/filetools.h"
40 #include "support/lyxmanip.h"
41 #include "support/lyxfunctional.h"
42 #include "support/LAssert.h"
56 extern BufferView * current_view;
59 // Class BufferStorage
62 void BufferStorage::release(Buffer * buf)
65 Container::iterator it = find(container.begin(), container.end(), buf);
66 if (it != container.end()) {
67 // Make sure that we don't store a LyXText in
68 // the textcache that points to the buffer
72 textcache.removeAllWithBuffer(tmp);
78 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
80 Buffer * tmpbuf = new Buffer(s, ronly);
81 tmpbuf->params.useClassDefaults();
82 lyxerr[Debug::INFO] << "Assigning to buffer "
83 << container.size() << endl;
84 container.push_back(tmpbuf);
93 BufferList::BufferList()
94 : state_(BufferList::OK)
98 bool BufferList::empty() const
100 return bstore.empty();
104 bool BufferList::qwriteOne(Buffer * buf, string const & fname,
105 string & unsaved_list)
109 switch (Alert::askConfirmation(_("Changes in document:"),
111 _("Save document?"))) {
113 // FIXME: WriteAs can be asynch !
114 if (buf->isUnnamed())
115 reask = !WriteAs(current_view, buf);
117 reask = !MenuWrite(current_view, buf);
121 // if we crash after this we could
122 // have no autosave file but I guess
123 // this is really inprobable (Jug)
124 if (buf->isUnnamed()) {
125 removeAutosaveFile(buf->fileName());
128 unsaved_list += MakeDisplayPath(fname, 50) + "\n";
138 bool BufferList::qwriteAll()
141 BufferStorage::iterator it = bstore.begin();
142 BufferStorage::iterator end = bstore.end();
143 for (; it != end; ++it) {
144 if (!(*it)->isLyxClean()) {
146 if ((*it)->isUnnamed())
147 fname = OnlyFilename((*it)->fileName());
149 fname = MakeDisplayPath((*it)->fileName(), 50);
150 if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
155 if (!unsaved.empty() && lyxrc.exit_confirmation) {
156 return Alert::askQuestion(_("Some documents were not saved:"),
157 unsaved, _("Exit anyway?"));
164 void BufferList::closeAll()
166 state_ = BufferList::CLOSING;
167 // Since we are closing we can just as well delete all
168 // in the textcache this will also speed the closing/quiting up a bit.
171 while (!bstore.empty()) {
172 close(bstore.front());
174 state_ = BufferList::OK;
178 bool BufferList::close(Buffer * buf)
183 // Trace back why we need to use buf->getUser here.
184 // Perhaps slight rewrite is in order? (Lgb)
187 buf->getUser()->insetUnlock();
189 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
191 buf->getUser()->owner()->prohibitInput();
193 if (buf->isUnnamed())
194 fname = OnlyFilename(buf->fileName());
196 fname = MakeDisplayPath(buf->fileName(), 50);
199 switch (Alert::askConfirmation(_("Changes in document:"),
201 _("Save document?"))) {
203 if (buf->isUnnamed())
204 reask = !WriteAs(current_view, buf);
205 else if (buf->save()) {
206 lastfiles->newFile(buf->fileName());
210 buf->getUser()->owner()->allowInput();
215 if (buf->isUnnamed()) {
216 removeAutosaveFile(buf->fileName());
222 buf->getUser()->owner()->allowInput();
227 buf->getUser()->owner()->allowInput();
235 vector<string> const BufferList::getFileNames() const
238 std::copy(bstore.begin(), bstore.end(),
239 lyx::back_inserter_fun(nvec, &Buffer::fileName));
244 Buffer * BufferList::first()
248 return bstore.front();
252 Buffer * BufferList::getBuffer(unsigned int choice)
254 if (choice >= bstore.size())
256 return bstore[choice];
260 int BufferList::unlockInset(UpdatableInset * inset)
264 BufferStorage::iterator it = bstore.begin();
265 BufferStorage::iterator end = bstore.end();
266 for (; it != end; ++it) {
268 && (*it)->getUser()->theLockingInset() == inset) {
269 (*it)->getUser()->insetUnlock();
277 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
279 BufferStorage::iterator it = bstore.begin();
280 BufferStorage::iterator end = bstore.end();
281 for (; it != end; ++it) {
282 if (!(*it)->isDepClean(mastertmpdir)) {
283 string writefile = mastertmpdir;
285 writefile += (*it)->getLatexName();
286 (*it)->makeLaTeXFile(writefile, mastertmpdir,
288 (*it)->markDepClean(mastertmpdir);
294 void BufferList::emergencyWriteAll()
296 for_each(bstore.begin(), bstore.end(),
297 lyx::void_class_fun(*this, &BufferList::emergencyWrite));
301 void BufferList::emergencyWrite(Buffer * buf)
303 // assert(buf) // this is not good since C assert takes an int
304 // and a pointer is a long (JMarc)
305 assert(buf != 0); // use c assert to avoid a loop
308 // No need to save if the buffer has not changed.
309 if (buf->isLyxClean())
312 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
313 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
314 : buf->fileName().c_str()) << endl;
316 // We try to save three places:
318 // 1) Same place as document. Unless it is an unnamed doc.
319 if (!buf->isUnnamed()) {
320 string s = buf->fileName();
322 lyxerr << " " << s << endl;
323 if (buf->writeFile(s, true)) {
325 lyxerr << _(" Save seems successful. Phew.") << endl;
328 lyxerr << _(" Save failed! Trying...") << endl;
332 // 2) In HOME directory.
333 string s = AddName(GetEnvPath("HOME"), buf->fileName());
335 lyxerr << " " << s << endl;
336 if (buf->writeFile(s, true)) {
338 lyxerr << _(" Save seems successful. Phew.") << endl;
342 lyxerr << _(" Save failed! Trying...") << endl;
344 // 3) In "/tmp" directory.
345 // MakeAbsPath to prepend the current
346 // drive letter on OS/2
347 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
349 lyxerr << " " << s << endl;
350 if (buf->writeFile(s, true)) {
352 lyxerr << _(" Save seems successful. Phew.") << endl;
355 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
360 Buffer * BufferList::readFile(string const & s, bool ronly)
363 string e = OnlyPath(s);
365 // File information about normal file
366 FileInfo fileInfo2(s);
368 if (!fileInfo2.exist()) {
369 Alert::alert(_("Error!"), _("Cannot open file"),
374 Buffer * b = bstore.newBuffer(s, ronly);
376 // Check if emergency save file exists and is newer.
377 e += OnlyFilename(s) + ".emergency";
378 FileInfo fileInfoE(e);
380 bool use_emergency = false;
382 if (fileInfoE.exist() && fileInfo2.exist()) {
383 if (fileInfoE.getModificationTime()
384 > fileInfo2.getModificationTime()) {
385 if (Alert::askQuestion(_("An emergency save of this document exists!"),
386 MakeDisplayPath(s, 50),
387 _("Try to load that instead?"))) {
389 // the file is not saved if we load the
392 use_emergency = true;
394 // Here, we should delete the emergency save
400 if (!use_emergency) {
401 // Now check if autosave file is newer.
403 a += OnlyFilename(s);
405 FileInfo fileInfoA(a);
406 if (fileInfoA.exist() && fileInfo2.exist()) {
407 if (fileInfoA.getModificationTime()
408 > fileInfo2.getModificationTime()) {
409 if (Alert::askQuestion(_("Autosave file is newer."),
410 MakeDisplayPath(s, 50),
411 _("Load that one instead?"))) {
413 // the file is not saved if we load the
417 // Here, we should delete the autosave
423 // not sure if this is the correct place to begin LyXLex
426 if (b->readFile(lex))
435 bool BufferList::exists(string const & s) const
437 return find_if(bstore.begin(), bstore.end(),
438 lyx::compare_memfun(&Buffer::fileName, s))
443 bool BufferList::isLoaded(Buffer const * b) const
447 BufferStorage::const_iterator cit =
448 find(bstore.begin(), bstore.end(), b);
449 return cit != bstore.end();
453 Buffer * BufferList::getBuffer(string const & s)
455 BufferStorage::iterator it =
456 find_if(bstore.begin(), bstore.end(),
457 lyx::compare_memfun(&Buffer::fileName, s));
458 return it != bstore.end() ? (*it) : 0;
462 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
465 Buffer * b = bstore.newBuffer(name);
467 // use defaults.lyx as a default template if it exists.
469 tname = LibFileSearch("templates", "defaults.lyx");
471 if (!tname.empty()) {
472 bool templateok = false;
476 if (b->readFile(lex)) {
481 Alert::alert(_("Error!"), _("Unable to open template"),
482 MakeDisplayPath(tname));
483 // no template, start with empty buffer
484 b->paragraph = new Paragraph;
485 b->paragraph->layout(textclasslist[b->params.textclass].defaultLayoutName());
487 } else { // start with empty buffer
488 b->paragraph = new Paragraph;
489 b->paragraph->layout(textclasslist[b->params.textclass].defaultLayoutName());
492 if (!lyxrc.new_ask_filename && !isNamed) {
494 b->setFileName(name);
497 b->setReadonly(false);
503 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
505 // get absolute path of file and add ".lyx" to the filename if
507 string s = FileSearch(string(), filename, "lyx");
512 // file already open?
514 if (Alert::askQuestion(_("Document is already open:"),
515 MakeDisplayPath(s, 50),
516 _("Do you want to reload that document?"))) {
517 // Reload is accomplished by closing and then loading
518 if (!close(getBuffer(s))) {
521 // Fall through to new load. (Asger)
523 // Here, we pretend that we just loaded the
530 switch (IsFileWriteable(s)) {
537 b->lyxvc.file_found_hook(s);
539 break; //fine- it's r/w
541 // Here we probably should run
542 if (LyXVC::file_not_found_hook(s)) {
543 // Ask if the file should be checked out for
544 // viewing/editing, if so: load it.
545 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
546 // How can we know _how_ to do the checkout?
547 // With the current VC support it has to be,
548 // a RCS file since CVS do not have special ,v files.
550 return loadLyXFile(filename, tolastfiles);
553 if (Alert::askQuestion(_("Cannot open specified file:"),
554 MakeDisplayPath(s, 50),
555 _("Create new document with this name?")))
557 // Find a free buffer
558 b = newFile(s, string(), true);
563 if (b && tolastfiles)
564 lastfiles->newFile(b->fileName());