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
23 #include "bufferlist.h"
25 #include "minibuffer.h"
26 #include "support/FileInfo.h"
27 #include "support/filetools.h"
28 #include "lyx_gui_misc.h"
29 #include "lastfiles.h"
34 #include "bufferview_funcs.h"
37 #include "vc-backend.h"
38 #include "TextCache.h"
40 extern BufferView * current_view; // called too many times in this file...
47 // Class BufferStorage
50 void BufferStorage::release(Buffer * buf)
52 Container::iterator it = find(container.begin(), container.end(), buf);
53 if (it != container.end()) {
54 // Make sure that we don't store a LyXText in
55 // the textcache that points to the buffer
59 textcache.removeAllWithBuffer(tmp);
65 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
67 Buffer * tmpbuf = new Buffer(s, ronly);
68 tmpbuf->params.useClassDefaults();
69 lyxerr.debug() << "Assigning to buffer "
70 << container.size() << endl;
71 container.push_back(tmpbuf);
80 BufferList::BufferList()
81 : state_(BufferList::OK)
85 bool BufferList::empty() const
87 return bstore.empty();
91 extern void MenuWrite(Buffer *);
93 bool BufferList::QwriteAll()
95 bool askMoreConfirmation = false;
97 for(BufferStorage::iterator it = bstore.begin();
98 it != bstore.end(); ++it) {
99 if (!(*it)->isLyxClean()) {
100 switch(AskConfirmation(_("Changes in document:"),
101 MakeDisplayPath((*it)->fileName(),
103 _("Save document?"))) {
108 askMoreConfirmation = true;
109 unsaved += MakeDisplayPath((*it)->fileName(),
118 if (askMoreConfirmation &&
119 lyxrc.exit_confirmation &&
120 !AskQuestion(_("Some documents were not saved:"),
121 unsaved, _("Exit anyway?"))) {
129 void BufferList::closeAll()
131 state_ = BufferList::CLOSING;
132 // Since we are closing we can just as well delete all
133 // in the textcache this will also speed the closing/quiting up a bit.
136 while (!bstore.empty()) {
137 close(bstore.front());
139 state_ = BufferList::OK;
143 void BufferList::resize()
145 for(BufferStorage::iterator it = bstore.begin();
146 it != bstore.end(); ++it) {
152 bool BufferList::close(Buffer * buf)
154 if (buf->getUser()) buf->getUser()->insetUnlock();
156 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
157 ProhibitInput(buf->getUser());
158 switch(AskConfirmation(_("Changes in document:"),
159 MakeDisplayPath(buf->fileName(), 50),
160 _("Save document?"))){
163 lastfiles->newFile(buf->fileName());
165 AllowInput(buf->getUser());
170 AllowInput(buf->getUser());
173 AllowInput(buf->getUser());
181 vector<string> BufferList::getFileNames() const
184 for(BufferStorage::const_iterator cit = bstore.begin();
185 cit != bstore.end(); ++cit) {
186 nvec.push_back((*cit)->fileName());
192 Buffer * BufferList::first()
194 if (bstore.empty()) return 0;
195 return bstore.front();
199 Buffer * BufferList::getBuffer(int choice)
201 if (choice >= bstore.size()) return 0;
202 return bstore[choice];
206 int BufferList::unlockInset(UpdatableInset * inset)
208 if (!inset) return 1;
209 for(BufferStorage::iterator it = bstore.begin();
210 it != bstore.end(); ++it) {
212 && (*it)->getUser()->the_locking_inset == inset) {
213 (*it)->getUser()->insetUnlock();
221 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
223 for(BufferStorage::iterator it = bstore.begin();
224 it != bstore.end(); ++it) {
225 if (!(*it)->isDepClean(mastertmpdir)) {
226 string writefile = mastertmpdir;
228 writefile += (*it)->getLatexName();
229 (*it)->makeLaTeXFile(writefile, mastertmpdir,
231 (*it)->markDepClean(mastertmpdir);
237 void BufferList::emergencyWriteAll()
239 for (BufferStorage::iterator it = bstore.begin();
240 it != bstore.end(); ++it) {
241 if (!(*it)->isLyxClean()) {
244 lyxerr <<_("lyx: Attempting to save"
247 << _(" as...") << endl;
249 for (int i = 0; i < 3 && !madeit; ++i) {
252 // We try to save three places:
253 // 1) Same place as document.
254 // 2) In HOME directory.
255 // 3) In "/tmp" directory.
257 s = (*it)->fileName();
259 s = AddName(GetEnvPath("HOME"),
262 // MakeAbsPath to prepend the current
263 // drive letter on OS/2
264 s = AddName(MakeAbsPath("/tmp/"),
269 lyxerr << " " << i + 1 << ") " << s << endl;
271 if ((*it)->writeFile(s, true)) {
272 (*it)->markLyxClean();
273 lyxerr << _(" Save seems successful. "
277 lyxerr << _(" Save failed! Trying...")
280 lyxerr << _(" Save failed! Bummer. "
290 Buffer * BufferList::readFile(string const & s, bool ronly)
292 Buffer * b = bstore.newBuffer(s, ronly);
295 string e = OnlyPath(s);
297 // File information about normal file
298 FileInfo fileInfo2(s);
300 // Check if emergency save file exists and is newer.
301 e += OnlyFilename(s) + ".emergency";
302 FileInfo fileInfoE(e);
304 bool use_emergency = false;
306 if (fileInfoE.exist() && fileInfo2.exist()) {
307 if (fileInfoE.getModificationTime()
308 > fileInfo2.getModificationTime()) {
309 if (AskQuestion(_("An emergency save of this document exists!"),
310 MakeDisplayPath(s, 50),
311 _("Try to load that instead?"))) {
313 // the file is not saved if we load the
316 use_emergency = true;
318 // Here, we should delete the emergency save
324 if (!use_emergency) {
325 // Now check if autosave file is newer.
327 a += OnlyFilename(s);
329 FileInfo fileInfoA(a);
330 if (fileInfoA.exist() && fileInfo2.exist()) {
331 if (fileInfoA.getModificationTime()
332 > fileInfo2.getModificationTime()) {
333 if (AskQuestion(_("Autosave file is newer."),
334 MakeDisplayPath(s, 50),
335 _("Load that one instead?"))) {
337 // the file is not saved if we load the
341 // Here, we should delete the autosave
347 // not sure if this is the correct place to begin LyXLex
350 if (b->readFile(lex))
359 bool BufferList::exists(string const & s) const
361 for (BufferStorage::const_iterator cit = bstore.begin();
362 cit != bstore.end(); ++cit) {
363 if ((*cit)->fileName() == s)
370 bool BufferList::isLoaded(Buffer const * b) const
372 BufferStorage::const_iterator cit =
373 find(bstore.begin(), bstore.end(), b);
374 return cit != bstore.end();
378 Buffer * BufferList::getBuffer(string const & s)
380 for(BufferStorage::iterator it = bstore.begin();
381 it != bstore.end(); ++it) {
382 if ((*it)->fileName() == s)
389 Buffer * BufferList::newFile(string const & name, string tname)
392 Buffer * b = bstore.newBuffer(name);
394 // use defaults.lyx as a default template if it exists.
396 tname = LibFileSearch("templates", "defaults.lyx");
398 if (!tname.empty() && IsLyXFilename(tname)) {
399 bool templateok = false;
403 if (b->readFile(lex)) {
408 WriteAlert(_("Error!"), _("Unable to open template"),
409 MakeDisplayPath(tname));
410 // no template, start with empty buffer
411 b->paragraph = new LyXParagraph;
414 else { // start with empty buffer
415 b->paragraph = new LyXParagraph;
419 b->setReadonly(false);
425 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
427 // make sure our path is absolute
428 string s = MakeAbsPath(filename);
430 // file already open?
432 if (AskQuestion(_("Document is already open:"),
433 MakeDisplayPath(s, 50),
434 _("Do you want to reload that document?"))) {
435 // Reload is accomplished by closing and then loading
436 if (!close(getBuffer(s))) {
439 // Fall through to new load. (Asger)
441 // Here, we pretend that we just loaded the
448 switch (IsFileWriteable(s)) {
451 current_view->owner()->getMiniBuffer()->
452 Set(_("File `") + MakeDisplayPath(s, 50) +
453 _("' is read-only."));
460 b->lyxvc.file_found_hook(s);
462 break; //fine- it's r/w
464 // Here we probably should run
465 if (LyXVC::file_not_found_hook(s)) {
466 // Ask if the file should be checked out for
467 // viewing/editing, if so: load it.
468 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
469 // How can we know _how_ to do the checkout?
470 // With the current VC support it has to be,
471 // a RCS file since CVS do not have special ,v files.
473 return loadLyXFile(filename, tolastfiles);
476 if (AskQuestion(_("Cannot open specified file:"),
477 MakeDisplayPath(s, 50),
478 _("Create new document with this name?")))
480 // Find a free buffer
481 b = newFile(s, string());
486 if (b && tolastfiles)
487 lastfiles->newFile(b->fileName());