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"
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)->isClean()) {
146 if ((*it)->isUnnamed())
147 fname = OnlyFilename((*it)->fileName());
149 fname = MakeDisplayPath((*it)->fileName(), 50);
150 if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
159 void BufferList::closeAll()
161 state_ = BufferList::CLOSING;
162 // Since we are closing we can just as well delete all
163 // in the textcache this will also speed the closing/quiting up a bit.
166 while (!bstore.empty()) {
167 close(bstore.front());
169 state_ = BufferList::OK;
173 bool BufferList::close(Buffer * buf)
178 // Trace back why we need to use buf->getUser here.
179 // Perhaps slight rewrite is in order? (Lgb)
182 buf->getUser()->insetUnlock();
184 if (!buf->paragraphs.empty() && !buf->isClean() && !quitting) {
186 buf->getUser()->owner()->prohibitInput();
188 if (buf->isUnnamed())
189 fname = OnlyFilename(buf->fileName());
191 fname = MakeDisplayPath(buf->fileName(), 50);
194 switch (Alert::askConfirmation(_("Changes in document:"),
196 _("Save document?"))) {
198 if (buf->isUnnamed())
199 reask = !WriteAs(current_view, buf);
200 else if (buf->save()) {
201 lastfiles->newFile(buf->fileName());
205 buf->getUser()->owner()->allowInput();
210 if (buf->isUnnamed()) {
211 removeAutosaveFile(buf->fileName());
217 buf->getUser()->owner()->allowInput();
222 buf->getUser()->owner()->allowInput();
230 vector<string> const BufferList::getFileNames() const
233 std::copy(bstore.begin(), bstore.end(),
234 lyx::back_inserter_fun(nvec, &Buffer::fileName));
239 Buffer * BufferList::first()
243 return bstore.front();
247 Buffer * BufferList::getBuffer(unsigned int choice)
249 if (choice >= bstore.size())
251 return bstore[choice];
255 int BufferList::unlockInset(UpdatableInset * inset)
259 BufferStorage::iterator it = bstore.begin();
260 BufferStorage::iterator end = bstore.end();
261 for (; it != end; ++it) {
263 && (*it)->getUser()->theLockingInset() == inset) {
264 (*it)->getUser()->insetUnlock();
272 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
274 BufferStorage::iterator it = bstore.begin();
275 BufferStorage::iterator end = bstore.end();
276 for (; it != end; ++it) {
277 if (!(*it)->isDepClean(mastertmpdir)) {
278 string writefile = mastertmpdir;
280 writefile += (*it)->getLatexName();
281 (*it)->makeLaTeXFile(writefile, mastertmpdir,
283 (*it)->markDepClean(mastertmpdir);
289 void BufferList::emergencyWriteAll()
291 for_each(bstore.begin(), bstore.end(),
292 lyx::void_class_fun(*this, &BufferList::emergencyWrite));
296 void BufferList::emergencyWrite(Buffer * buf)
298 // assert(buf) // this is not good since C assert takes an int
299 // and a pointer is a long (JMarc)
300 assert(buf != 0); // use c assert to avoid a loop
303 // No need to save if the buffer has not changed.
307 lyxerr << _("lyx: Attempting to save document ")
308 << (buf->isUnnamed() ? OnlyFilename(buf->fileName())
310 << _(" as...") << endl;
312 // We try to save three places:
314 // 1) Same place as document. Unless it is an unnamed doc.
315 if (!buf->isUnnamed()) {
316 string s = buf->fileName();
318 lyxerr << " " << s << endl;
319 if (buf->writeFile(s)) {
321 lyxerr << _(" Save seems successful. Phew.") << endl;
324 lyxerr << _(" Save failed! Trying...") << endl;
328 // 2) In HOME directory.
329 string s = AddName(GetEnvPath("HOME"), buf->fileName());
331 lyxerr << " " << s << endl;
332 if (buf->writeFile(s)) {
334 lyxerr << _(" Save seems successful. Phew.") << endl;
338 lyxerr << _(" Save failed! Trying...") << endl;
340 // 3) In "/tmp" directory.
341 // MakeAbsPath to prepend the current
342 // drive letter on OS/2
343 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
345 lyxerr << " " << s << endl;
346 if (buf->writeFile(s)) {
348 lyxerr << _(" Save seems successful. Phew.") << endl;
351 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
356 Buffer * BufferList::readFile(string const & s, bool ronly)
359 string e = OnlyPath(s);
361 // File information about normal file
362 FileInfo fileInfo2(s);
364 if (!fileInfo2.exist()) {
365 Alert::alert(_("Error!"), _("Cannot open file"),
370 Buffer * b = bstore.newBuffer(s, ronly);
372 // Check if emergency save file exists and is newer.
373 e += OnlyFilename(s) + ".emergency";
374 FileInfo fileInfoE(e);
376 bool use_emergency = false;
378 if (fileInfoE.exist() && fileInfo2.exist()) {
379 if (fileInfoE.getModificationTime()
380 > fileInfo2.getModificationTime()) {
381 if (Alert::askQuestion(_("An emergency save of this document exists!"),
382 MakeDisplayPath(s, 50),
383 _("Try to load that instead?"))) {
385 // the file is not saved if we load the
388 use_emergency = true;
390 // Here, we should delete the emergency save
396 if (!use_emergency) {
397 // Now check if autosave file is newer.
399 a += OnlyFilename(s);
401 FileInfo fileInfoA(a);
402 if (fileInfoA.exist() && fileInfo2.exist()) {
403 if (fileInfoA.getModificationTime()
404 > fileInfo2.getModificationTime()) {
405 if (Alert::askQuestion(_("Autosave file is newer."),
406 MakeDisplayPath(s, 50),
407 _("Load that one instead?"))) {
409 // the file is not saved if we load the
413 // Here, we should delete the autosave
419 // not sure if this is the correct place to begin LyXLex
422 if (b->readFile(lex))
431 bool BufferList::exists(string const & s) const
433 return find_if(bstore.begin(), bstore.end(),
434 lyx::compare_memfun(&Buffer::fileName, s))
439 bool BufferList::isLoaded(Buffer const * b) const
443 BufferStorage::const_iterator cit =
444 find(bstore.begin(), bstore.end(), b);
445 return cit != bstore.end();
449 Buffer * BufferList::getBuffer(string const & s)
451 BufferStorage::iterator it =
452 find_if(bstore.begin(), bstore.end(),
453 lyx::compare_memfun(&Buffer::fileName, s));
454 return it != bstore.end() ? (*it) : 0;
458 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
461 Buffer * b = bstore.newBuffer(name);
463 // use defaults.lyx as a default template if it exists.
465 tname = LibFileSearch("templates", "defaults.lyx");
467 if (!tname.empty()) {
468 bool templateok = false;
472 if (b->readFile(lex)) {
477 Alert::alert(_("Error!"), _("Unable to open template"),
478 MakeDisplayPath(tname));
479 // no template, start with empty buffer
480 b->paragraphs.set(new Paragraph);
481 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
483 } else { // start with empty buffer
484 b->paragraphs.set(new Paragraph);
485 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
490 b->setFileName(name);
493 b->setReadonly(false);
499 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
501 // get absolute path of file and add ".lyx" to the filename if
503 string s = FileSearch(string(), filename, "lyx");
508 // file already open?
510 if (Alert::askQuestion(_("Document is already open:"),
511 MakeDisplayPath(s, 50),
512 _("Do you want to reload that document?"))) {
513 // Reload is accomplished by closing and then loading
514 if (!close(getBuffer(s))) {
517 // Fall through to new load. (Asger)
519 // Here, we pretend that we just loaded the
526 switch (IsFileWriteable(s)) {
533 b->lyxvc.file_found_hook(s);
535 break; //fine- it's r/w
537 // Here we probably should run
538 if (LyXVC::file_not_found_hook(s)) {
539 // Ask if the file should be checked out for
540 // viewing/editing, if so: load it.
541 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
542 // How can we know _how_ to do the checkout?
543 // With the current VC support it has to be,
544 // a RCS file since CVS do not have special ,v files.
546 return loadLyXFile(filename, tolastfiles);
549 if (Alert::askQuestion(_("Cannot open specified file:"),
550 MakeDisplayPath(s, 50),
551 _("Create new document with this name?")))
553 // Find a free buffer
554 b = newFile(s, string(), true);
559 if (b && tolastfiles)
560 lastfiles->newFile(b->fileName());