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 "minibuffer.h"
27 #include "support/FileInfo.h"
28 #include "support/filetools.h"
29 #include "support/lyxmanip.h"
30 #include "support/lyxfunctional.h"
31 #include "lyx_gui_misc.h"
32 #include "lastfiles.h"
37 #include "bufferview_funcs.h"
40 #include "vc-backend.h"
41 #include "TextCache.h"
51 // Class BufferStorage
54 void BufferStorage::release(Buffer * buf)
56 Container::iterator it = find(container.begin(), container.end(), buf);
57 if (it != container.end()) {
58 // Make sure that we don't store a LyXText in
59 // the textcache that points to the buffer
63 textcache.removeAllWithBuffer(tmp);
69 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
71 Buffer * tmpbuf = new Buffer(s, ronly);
72 tmpbuf->params.useClassDefaults();
73 lyxerr.debug() << "Assigning to buffer "
74 << container.size() << endl;
75 container.push_back(tmpbuf);
84 BufferList::BufferList()
85 : state_(BufferList::OK)
89 bool BufferList::empty() const
91 return bstore.empty();
95 extern bool MenuWrite(Buffer *);
96 extern bool MenuWriteAs(Buffer *);
98 bool BufferList::QwriteAll()
100 bool askMoreConfirmation = false;
102 for (BufferStorage::iterator it = bstore.begin();
103 it != bstore.end(); ++it) {
104 if (!(*it)->isLyxClean()) {
106 if ((*it)->isUnnamed())
107 fname = OnlyFilename((*it)->fileName());
109 fname = MakeDisplayPath((*it)->fileName(), 50);
112 switch (AskConfirmation(_("Changes in document:"),
114 _("Save document?"))) {
116 if ((*it)->isUnnamed())
117 reask = !MenuWriteAs((*it));
119 reask = !MenuWrite((*it));
123 // if we crash after this we could
124 // have no autosave file but I guess
125 // this is really inprobable (Jug)
126 if ((*it)->isUnnamed()) {
127 removeAutosaveFile((*it)->fileName());
129 askMoreConfirmation = true;
130 unsaved += MakeDisplayPath(fname, 50);
140 if (askMoreConfirmation &&
141 lyxrc.exit_confirmation &&
142 !AskQuestion(_("Some documents were not saved:"),
143 unsaved, _("Exit anyway?"))) {
151 void BufferList::closeAll()
153 state_ = BufferList::CLOSING;
154 // Since we are closing we can just as well delete all
155 // in the textcache this will also speed the closing/quiting up a bit.
158 while (!bstore.empty()) {
159 close(bstore.front());
161 state_ = BufferList::OK;
165 void BufferList::resize()
167 for_each(bstore.begin(), bstore.end(), mem_fun(&Buffer::resize));
171 bool BufferList::close(Buffer * buf)
174 // Trace back why we need to use buf->getUser here.
175 // Perhaps slight rewrite is in order? (Lgb)
177 if (buf->getUser()) buf->getUser()->insetUnlock();
178 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
180 ProhibitInput(buf->getUser());
182 if (buf->isUnnamed())
183 fname = OnlyFilename(buf->fileName());
185 fname = MakeDisplayPath(buf->fileName(), 50);
188 switch (AskConfirmation(_("Changes in document:"),
190 _("Save document?"))){
192 if (buf->isUnnamed())
193 reask = !MenuWriteAs(buf);
194 else if (buf->save()) {
195 lastfiles->newFile(buf->fileName());
199 AllowInput(buf->getUser());
204 if (buf->isUnnamed()) {
205 removeAutosaveFile(buf->fileName());
211 AllowInput(buf->getUser());
216 AllowInput(buf->getUser());
224 vector<string> const BufferList::getFileNames() const
227 std::copy(bstore.begin(), bstore.end(),
228 back_inserter_fun(nvec, &Buffer::fileName));
233 Buffer * BufferList::first()
235 if (bstore.empty()) return 0;
236 return bstore.front();
240 Buffer * BufferList::getBuffer(unsigned int choice)
242 if (choice >= bstore.size()) return 0;
243 return bstore[choice];
247 int BufferList::unlockInset(UpdatableInset * inset)
249 if (!inset) return 1;
250 for (BufferStorage::iterator it = bstore.begin();
251 it != bstore.end(); ++it) {
253 && (*it)->getUser()->theLockingInset() == inset) {
254 (*it)->getUser()->insetUnlock();
262 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
264 for (BufferStorage::iterator it = bstore.begin();
265 it != bstore.end(); ++it) {
266 if (!(*it)->isDepClean(mastertmpdir)) {
267 string writefile = mastertmpdir;
269 writefile += (*it)->getLatexName();
270 (*it)->makeLaTeXFile(writefile, mastertmpdir,
272 (*it)->markDepClean(mastertmpdir);
278 void BufferList::emergencyWriteAll()
280 for_each(bstore.begin(), bstore.end(),
281 class_fun(*this, &BufferList::emergencyWrite));
285 void BufferList::emergencyWrite(Buffer * buf)
287 // No need to save if the buffer has not changed.
288 if (buf->isLyxClean()) return;
290 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
291 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
292 : buf->fileName().c_str()) << endl;
294 // We try to save three places:
296 // 1) Same place as document. Unless it is an unnamed doc.
297 if (!buf->isUnnamed()) {
298 string s = buf->fileName();
300 lyxerr << " " << s << endl;
301 if (buf->writeFile(s, true)) {
303 lyxerr << _(" Save seems successful. Phew.") << endl;
306 lyxerr << _(" Save failed! Trying...") << endl;
310 // 2) In HOME directory.
311 string s = AddName(GetEnvPath("HOME"), buf->fileName());
313 lyxerr << " " << s << endl;
314 if (buf->writeFile(s, true)) {
316 lyxerr << _(" Save seems successful. Phew.") << endl;
320 lyxerr << _(" Save failed! Trying...") << endl;
322 // 3) In "/tmp" directory.
323 // MakeAbsPath to prepend the current
324 // drive letter on OS/2
325 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
327 lyxerr << " " << s << endl;
328 if (buf->writeFile(s, true)) {
330 lyxerr << _(" Save seems successful. Phew.") << endl;
333 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
338 Buffer * BufferList::readFile(string const & s, bool ronly)
340 Buffer * b = bstore.newBuffer(s, ronly);
343 string e = OnlyPath(s);
345 // File information about normal file
346 FileInfo fileInfo2(s);
348 // Check if emergency save file exists and is newer.
349 e += OnlyFilename(s) + ".emergency";
350 FileInfo fileInfoE(e);
352 bool use_emergency = false;
354 if (fileInfoE.exist() && fileInfo2.exist()) {
355 if (fileInfoE.getModificationTime()
356 > fileInfo2.getModificationTime()) {
357 if (AskQuestion(_("An emergency save of this document exists!"),
358 MakeDisplayPath(s, 50),
359 _("Try to load that instead?"))) {
361 // the file is not saved if we load the
364 use_emergency = true;
366 // Here, we should delete the emergency save
372 if (!use_emergency) {
373 // Now check if autosave file is newer.
375 a += OnlyFilename(s);
377 FileInfo fileInfoA(a);
378 if (fileInfoA.exist() && fileInfo2.exist()) {
379 if (fileInfoA.getModificationTime()
380 > fileInfo2.getModificationTime()) {
381 if (AskQuestion(_("Autosave file is newer."),
382 MakeDisplayPath(s, 50),
383 _("Load that one instead?"))) {
385 // the file is not saved if we load the
389 // Here, we should delete the autosave
395 // not sure if this is the correct place to begin LyXLex
398 if (b->readFile(lex))
407 bool BufferList::exists(string const & s) const
409 return find_if(bstore.begin(), bstore.end(),
410 compare_memfun(&Buffer::fileName, s)) != bstore.end();
414 bool BufferList::isLoaded(Buffer const * b) const
416 BufferStorage::const_iterator cit =
417 find(bstore.begin(), bstore.end(), b);
418 return cit != bstore.end();
422 Buffer * BufferList::getBuffer(string const & s)
424 BufferStorage::iterator it =
425 find_if(bstore.begin(), bstore.end(),
426 compare_memfun(&Buffer::fileName, s));
427 return it != bstore.end() ? (*it) : 0;
431 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
434 Buffer * b = bstore.newBuffer(name);
436 // use defaults.lyx as a default template if it exists.
438 tname = LibFileSearch("templates", "defaults.lyx");
440 if (!tname.empty() && IsLyXFilename(tname)) {
441 bool templateok = false;
445 if (b->readFile(lex)) {
450 WriteAlert(_("Error!"), _("Unable to open template"),
451 MakeDisplayPath(tname));
452 // no template, start with empty buffer
453 b->paragraph = new LyXParagraph;
455 } else { // start with empty buffer
456 b->paragraph = new LyXParagraph;
459 if (!lyxrc.new_ask_filename && !isNamed) {
461 b->setFileName(name);
464 b->setReadonly(false);
470 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
472 // make sure our path is absolute
473 string const s = MakeAbsPath(filename);
475 // file already open?
477 if (AskQuestion(_("Document is already open:"),
478 MakeDisplayPath(s, 50),
479 _("Do you want to reload that document?"))) {
480 // Reload is accomplished by closing and then loading
481 if (!close(getBuffer(s))) {
484 // Fall through to new load. (Asger)
486 // Here, we pretend that we just loaded the
493 switch (IsFileWriteable(s)) {
496 current_view->owner()->getMiniBuffer()->
497 Set(_("File `") + MakeDisplayPath(s, 50) +
498 _("' is read-only."));
505 b->lyxvc.file_found_hook(s);
507 break; //fine- it's r/w
509 // Here we probably should run
510 if (LyXVC::file_not_found_hook(s)) {
511 // Ask if the file should be checked out for
512 // viewing/editing, if so: load it.
513 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
514 // How can we know _how_ to do the checkout?
515 // With the current VC support it has to be,
516 // a RCS file since CVS do not have special ,v files.
518 return loadLyXFile(filename, tolastfiles);
521 if (AskQuestion(_("Cannot open specified file:"),
522 MakeDisplayPath(s, 50),
523 _("Create new document with this name?")))
525 // Find a free buffer
526 b = newFile(s, string(), true);
531 if (b && tolastfiles)
532 lastfiles->newFile(b->fileName());