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 "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"
39 #include "vc-backend.h"
40 #include "TextCache.h"
49 extern BufferView * current_view;
52 // Class BufferStorage
55 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::INFO] << "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 = !WriteAs(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)
175 // Trace back why we need to use buf->getUser here.
176 // Perhaps slight rewrite is in order? (Lgb)
178 if (buf->getUser()) buf->getUser()->insetUnlock();
179 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
181 ProhibitInput(buf->getUser());
183 if (buf->isUnnamed())
184 fname = OnlyFilename(buf->fileName());
186 fname = MakeDisplayPath(buf->fileName(), 50);
189 switch (AskConfirmation(_("Changes in document:"),
191 _("Save document?"))){
193 if (buf->isUnnamed())
194 reask = !WriteAs(current_view, buf);
195 else if (buf->save()) {
196 lastfiles->newFile(buf->fileName());
200 AllowInput(buf->getUser());
205 if (buf->isUnnamed()) {
206 removeAutosaveFile(buf->fileName());
212 AllowInput(buf->getUser());
217 AllowInput(buf->getUser());
225 vector<string> const BufferList::getFileNames() const
228 std::copy(bstore.begin(), bstore.end(),
229 lyx::back_inserter_fun(nvec, &Buffer::fileName));
234 Buffer * BufferList::first()
236 if (bstore.empty()) return 0;
237 return bstore.front();
241 Buffer * BufferList::getBuffer(unsigned int choice)
243 if (choice >= bstore.size()) return 0;
244 return bstore[choice];
248 int BufferList::unlockInset(UpdatableInset * inset)
252 //if (!inset) return 1;
253 for (BufferStorage::iterator it = bstore.begin();
254 it != bstore.end(); ++it) {
256 && (*it)->getUser()->theLockingInset() == inset) {
257 (*it)->getUser()->insetUnlock();
265 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
267 for (BufferStorage::iterator it = bstore.begin();
268 it != bstore.end(); ++it) {
269 if (!(*it)->isDepClean(mastertmpdir)) {
270 string writefile = mastertmpdir;
272 writefile += (*it)->getLatexName();
273 (*it)->makeLaTeXFile(writefile, mastertmpdir,
275 (*it)->markDepClean(mastertmpdir);
281 void BufferList::emergencyWriteAll()
283 for_each(bstore.begin(), bstore.end(),
284 lyx::class_fun(*this, &BufferList::emergencyWrite));
288 void BufferList::emergencyWrite(Buffer * buf)
290 assert(buf); // use c assert to avoid a loop
292 // No need to save if the buffer has not changed.
293 if (buf->isLyxClean()) return;
295 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
296 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
297 : buf->fileName().c_str()) << endl;
299 // We try to save three places:
301 // 1) Same place as document. Unless it is an unnamed doc.
302 if (!buf->isUnnamed()) {
303 string s = buf->fileName();
305 lyxerr << " " << s << endl;
306 if (buf->writeFile(s, true)) {
308 lyxerr << _(" Save seems successful. Phew.") << endl;
311 lyxerr << _(" Save failed! Trying...") << endl;
315 // 2) In HOME directory.
316 string s = AddName(GetEnvPath("HOME"), buf->fileName());
318 lyxerr << " " << s << endl;
319 if (buf->writeFile(s, true)) {
321 lyxerr << _(" Save seems successful. Phew.") << endl;
325 lyxerr << _(" Save failed! Trying...") << endl;
327 // 3) In "/tmp" directory.
328 // MakeAbsPath to prepend the current
329 // drive letter on OS/2
330 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
332 lyxerr << " " << s << endl;
333 if (buf->writeFile(s, true)) {
335 lyxerr << _(" Save seems successful. Phew.") << endl;
338 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
343 Buffer * BufferList::readFile(string const & s, bool ronly)
345 Buffer * b = bstore.newBuffer(s, ronly);
348 string e = OnlyPath(s);
350 // File information about normal file
351 FileInfo fileInfo2(s);
353 // Check if emergency save file exists and is newer.
354 e += OnlyFilename(s) + ".emergency";
355 FileInfo fileInfoE(e);
357 bool use_emergency = false;
359 if (fileInfoE.exist() && fileInfo2.exist()) {
360 if (fileInfoE.getModificationTime()
361 > fileInfo2.getModificationTime()) {
362 if (AskQuestion(_("An emergency save of this document exists!"),
363 MakeDisplayPath(s, 50),
364 _("Try to load that instead?"))) {
366 // the file is not saved if we load the
369 use_emergency = true;
371 // Here, we should delete the emergency save
377 if (!use_emergency) {
378 // Now check if autosave file is newer.
380 a += OnlyFilename(s);
382 FileInfo fileInfoA(a);
383 if (fileInfoA.exist() && fileInfo2.exist()) {
384 if (fileInfoA.getModificationTime()
385 > fileInfo2.getModificationTime()) {
386 if (AskQuestion(_("Autosave file is newer."),
387 MakeDisplayPath(s, 50),
388 _("Load that one instead?"))) {
390 // the file is not saved if we load the
394 // Here, we should delete the autosave
400 // not sure if this is the correct place to begin LyXLex
403 if (b->readFile(lex))
412 bool BufferList::exists(string const & s) const
414 return find_if(bstore.begin(), bstore.end(),
415 lyx::compare_memfun(&Buffer::fileName, s))
420 bool BufferList::isLoaded(Buffer const * b) const
424 BufferStorage::const_iterator cit =
425 find(bstore.begin(), bstore.end(), b);
426 return cit != bstore.end();
430 Buffer * BufferList::getBuffer(string const & s)
432 BufferStorage::iterator it =
433 find_if(bstore.begin(), bstore.end(),
434 lyx::compare_memfun(&Buffer::fileName, s));
435 return it != bstore.end() ? (*it) : 0;
439 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
442 Buffer * b = bstore.newBuffer(name);
444 // use defaults.lyx as a default template if it exists.
446 tname = LibFileSearch("templates", "defaults.lyx");
448 if (!tname.empty() && IsLyXFilename(tname)) {
449 bool templateok = false;
453 if (b->readFile(lex)) {
458 WriteAlert(_("Error!"), _("Unable to open template"),
459 MakeDisplayPath(tname));
460 // no template, start with empty buffer
461 b->paragraph = new LyXParagraph;
463 } else { // start with empty buffer
464 b->paragraph = new LyXParagraph;
467 if (!lyxrc.new_ask_filename && !isNamed) {
469 b->setFileName(name);
472 b->setReadonly(false);
478 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
480 // make sure our path is absolute
481 string const s = MakeAbsPath(filename);
483 // file already open?
485 if (AskQuestion(_("Document is already open:"),
486 MakeDisplayPath(s, 50),
487 _("Do you want to reload that document?"))) {
488 // Reload is accomplished by closing and then loading
489 if (!close(getBuffer(s))) {
492 // Fall through to new load. (Asger)
494 // Here, we pretend that we just loaded the
501 switch (IsFileWriteable(s)) {
508 b->lyxvc.file_found_hook(s);
510 break; //fine- it's r/w
512 // Here we probably should run
513 if (LyXVC::file_not_found_hook(s)) {
514 // Ask if the file should be checked out for
515 // viewing/editing, if so: load it.
516 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
517 // How can we know _how_ to do the checkout?
518 // With the current VC support it has to be,
519 // a RCS file since CVS do not have special ,v files.
521 return loadLyXFile(filename, tolastfiles);
524 if (AskQuestion(_("Cannot open specified file:"),
525 MakeDisplayPath(s, 50),
526 _("Create new document with this name?")))
528 // Find a free buffer
529 b = newFile(s, string(), true);
534 if (b && tolastfiles)
535 lastfiles->newFile(b->fileName());