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)
57 Container::iterator it = find(container.begin(), container.end(), buf);
58 if (it != container.end()) {
59 // Make sure that we don't store a LyXText in
60 // the textcache that points to the buffer
64 textcache.removeAllWithBuffer(tmp);
70 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
72 Buffer * tmpbuf = new Buffer(s, ronly);
73 tmpbuf->params.useClassDefaults();
74 lyxerr[Debug::INFO] << "Assigning to buffer "
75 << container.size() << endl;
76 container.push_back(tmpbuf);
85 BufferList::BufferList()
86 : state_(BufferList::OK)
90 bool BufferList::empty() const
92 return bstore.empty();
96 bool BufferList::QwriteAll()
98 bool askMoreConfirmation = false;
100 for (BufferStorage::iterator it = bstore.begin();
101 it != bstore.end(); ++it) {
102 if (!(*it)->isLyxClean()) {
104 if ((*it)->isUnnamed())
105 fname = OnlyFilename((*it)->fileName());
107 fname = MakeDisplayPath((*it)->fileName(), 50);
110 switch (AskConfirmation(_("Changes in document:"),
112 _("Save document?"))) {
114 if ((*it)->isUnnamed())
115 reask = !WriteAs(current_view, (*it));
117 reask = !MenuWrite(current_view, (*it));
121 // if we crash after this we could
122 // have no autosave file but I guess
123 // this is really inprobable (Jug)
124 if ((*it)->isUnnamed()) {
125 removeAutosaveFile((*it)->fileName());
127 askMoreConfirmation = true;
128 unsaved += MakeDisplayPath(fname, 50);
138 if (askMoreConfirmation &&
139 lyxrc.exit_confirmation &&
140 !AskQuestion(_("Some documents were not saved:"),
141 unsaved, _("Exit anyway?"))) {
149 void BufferList::closeAll()
151 state_ = BufferList::CLOSING;
152 // Since we are closing we can just as well delete all
153 // in the textcache this will also speed the closing/quiting up a bit.
156 while (!bstore.empty()) {
157 close(bstore.front());
159 state_ = BufferList::OK;
163 void BufferList::resize()
165 for_each(bstore.begin(), bstore.end(), mem_fun(&Buffer::resize));
169 bool BufferList::close(Buffer * buf)
172 // Trace back why we need to use buf->getUser here.
173 // Perhaps slight rewrite is in order? (Lgb)
175 if (buf->getUser()) buf->getUser()->insetUnlock();
176 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
178 ProhibitInput(buf->getUser());
180 if (buf->isUnnamed())
181 fname = OnlyFilename(buf->fileName());
183 fname = MakeDisplayPath(buf->fileName(), 50);
186 switch (AskConfirmation(_("Changes in document:"),
188 _("Save document?"))){
190 if (buf->isUnnamed())
191 reask = !WriteAs(current_view, buf);
192 else if (buf->save()) {
193 lastfiles->newFile(buf->fileName());
197 AllowInput(buf->getUser());
202 if (buf->isUnnamed()) {
203 removeAutosaveFile(buf->fileName());
209 AllowInput(buf->getUser());
214 AllowInput(buf->getUser());
222 vector<string> const BufferList::getFileNames() const
225 std::copy(bstore.begin(), bstore.end(),
226 back_inserter_fun(nvec, &Buffer::fileName));
231 Buffer * BufferList::first()
233 if (bstore.empty()) return 0;
234 return bstore.front();
238 Buffer * BufferList::getBuffer(unsigned int choice)
240 if (choice >= bstore.size()) return 0;
241 return bstore[choice];
245 int BufferList::unlockInset(UpdatableInset * inset)
247 if (!inset) return 1;
248 for (BufferStorage::iterator it = bstore.begin();
249 it != bstore.end(); ++it) {
251 && (*it)->getUser()->theLockingInset() == inset) {
252 (*it)->getUser()->insetUnlock();
260 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
262 for (BufferStorage::iterator it = bstore.begin();
263 it != bstore.end(); ++it) {
264 if (!(*it)->isDepClean(mastertmpdir)) {
265 string writefile = mastertmpdir;
267 writefile += (*it)->getLatexName();
268 (*it)->makeLaTeXFile(writefile, mastertmpdir,
270 (*it)->markDepClean(mastertmpdir);
276 void BufferList::emergencyWriteAll()
278 for_each(bstore.begin(), bstore.end(),
279 class_fun(*this, &BufferList::emergencyWrite));
283 void BufferList::emergencyWrite(Buffer * buf)
285 // No need to save if the buffer has not changed.
286 if (buf->isLyxClean()) return;
288 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
289 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
290 : buf->fileName().c_str()) << endl;
292 // We try to save three places:
294 // 1) Same place as document. Unless it is an unnamed doc.
295 if (!buf->isUnnamed()) {
296 string s = buf->fileName();
298 lyxerr << " " << s << endl;
299 if (buf->writeFile(s, true)) {
301 lyxerr << _(" Save seems successful. Phew.") << endl;
304 lyxerr << _(" Save failed! Trying...") << endl;
308 // 2) In HOME directory.
309 string s = AddName(GetEnvPath("HOME"), buf->fileName());
311 lyxerr << " " << s << endl;
312 if (buf->writeFile(s, true)) {
314 lyxerr << _(" Save seems successful. Phew.") << endl;
318 lyxerr << _(" Save failed! Trying...") << endl;
320 // 3) In "/tmp" directory.
321 // MakeAbsPath to prepend the current
322 // drive letter on OS/2
323 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
325 lyxerr << " " << s << endl;
326 if (buf->writeFile(s, true)) {
328 lyxerr << _(" Save seems successful. Phew.") << endl;
331 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
336 Buffer * BufferList::readFile(string const & s, bool ronly)
338 Buffer * b = bstore.newBuffer(s, ronly);
341 string e = OnlyPath(s);
343 // File information about normal file
344 FileInfo fileInfo2(s);
346 // Check if emergency save file exists and is newer.
347 e += OnlyFilename(s) + ".emergency";
348 FileInfo fileInfoE(e);
350 bool use_emergency = false;
352 if (fileInfoE.exist() && fileInfo2.exist()) {
353 if (fileInfoE.getModificationTime()
354 > fileInfo2.getModificationTime()) {
355 if (AskQuestion(_("An emergency save of this document exists!"),
356 MakeDisplayPath(s, 50),
357 _("Try to load that instead?"))) {
359 // the file is not saved if we load the
362 use_emergency = true;
364 // Here, we should delete the emergency save
370 if (!use_emergency) {
371 // Now check if autosave file is newer.
373 a += OnlyFilename(s);
375 FileInfo fileInfoA(a);
376 if (fileInfoA.exist() && fileInfo2.exist()) {
377 if (fileInfoA.getModificationTime()
378 > fileInfo2.getModificationTime()) {
379 if (AskQuestion(_("Autosave file is newer."),
380 MakeDisplayPath(s, 50),
381 _("Load that one instead?"))) {
383 // the file is not saved if we load the
387 // Here, we should delete the autosave
393 // not sure if this is the correct place to begin LyXLex
396 if (b->readFile(lex))
405 bool BufferList::exists(string const & s) const
407 return find_if(bstore.begin(), bstore.end(),
408 compare_memfun(&Buffer::fileName, s)) != bstore.end();
412 bool BufferList::isLoaded(Buffer const * b) const
414 BufferStorage::const_iterator cit =
415 find(bstore.begin(), bstore.end(), b);
416 return cit != bstore.end();
420 Buffer * BufferList::getBuffer(string const & s)
422 BufferStorage::iterator it =
423 find_if(bstore.begin(), bstore.end(),
424 compare_memfun(&Buffer::fileName, s));
425 return it != bstore.end() ? (*it) : 0;
429 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
432 Buffer * b = bstore.newBuffer(name);
434 // use defaults.lyx as a default template if it exists.
436 tname = LibFileSearch("templates", "defaults.lyx");
438 if (!tname.empty() && IsLyXFilename(tname)) {
439 bool templateok = false;
443 if (b->readFile(lex)) {
448 WriteAlert(_("Error!"), _("Unable to open template"),
449 MakeDisplayPath(tname));
450 // no template, start with empty buffer
451 b->paragraph = new LyXParagraph;
453 } else { // start with empty buffer
454 b->paragraph = new LyXParagraph;
457 if (!lyxrc.new_ask_filename && !isNamed) {
459 b->setFileName(name);
462 b->setReadonly(false);
468 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
470 // make sure our path is absolute
471 string const s = MakeAbsPath(filename);
473 // file already open?
475 if (AskQuestion(_("Document is already open:"),
476 MakeDisplayPath(s, 50),
477 _("Do you want to reload that document?"))) {
478 // Reload is accomplished by closing and then loading
479 if (!close(getBuffer(s))) {
482 // Fall through to new load. (Asger)
484 // Here, we pretend that we just loaded the
491 switch (IsFileWriteable(s)) {
498 b->lyxvc.file_found_hook(s);
500 break; //fine- it's r/w
502 // Here we probably should run
503 if (LyXVC::file_not_found_hook(s)) {
504 // Ask if the file should be checked out for
505 // viewing/editing, if so: load it.
506 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
507 // How can we know _how_ to do the checkout?
508 // With the current VC support it has to be,
509 // a RCS file since CVS do not have special ,v files.
511 return loadLyXFile(filename, tolastfiles);
514 if (AskQuestion(_("Cannot open specified file:"),
515 MakeDisplayPath(s, 50),
516 _("Create new document with this name?")))
518 // Find a free buffer
519 b = newFile(s, string(), true);
524 if (b && tolastfiles)
525 lastfiles->newFile(b->fileName());