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 += ChangeExtension((*it)->fileName(),
230 (*it)->makeLaTeXFile(writefile, mastertmpdir,
232 (*it)->markDepClean(mastertmpdir);
238 void BufferList::emergencyWriteAll()
240 for (BufferStorage::iterator it = bstore.begin();
241 it != bstore.end(); ++it) {
242 if (!(*it)->isLyxClean()) {
245 lyxerr <<_("lyx: Attempting to save"
248 << _(" as...") << endl;
250 for (int i = 0; i < 3 && !madeit; ++i) {
253 // We try to save three places:
254 // 1) Same place as document.
255 // 2) In HOME directory.
256 // 3) In "/tmp" directory.
258 s = (*it)->fileName();
260 s = AddName(GetEnvPath("HOME"),
263 // MakeAbsPath to prepend the current
264 // drive letter on OS/2
265 s = AddName(MakeAbsPath("/tmp/"),
270 lyxerr << " " << i + 1 << ") " << s << endl;
272 if ((*it)->writeFile(s, true)) {
273 (*it)->markLyxClean();
274 lyxerr << _(" Save seems successful. "
278 lyxerr << _(" Save failed! Trying...")
281 lyxerr << _(" Save failed! Bummer. "
291 Buffer * BufferList::readFile(string const & s, bool ronly)
293 Buffer * b = bstore.newBuffer(s, ronly);
296 string e = OnlyPath(s);
298 // File information about normal file
299 FileInfo fileInfo2(s);
301 // Check if emergency save file exists and is newer.
302 e += OnlyFilename(s) + ".emergency";
303 FileInfo fileInfoE(e);
305 bool use_emergency = false;
307 if (fileInfoE.exist() && fileInfo2.exist()) {
308 if (fileInfoE.getModificationTime()
309 > fileInfo2.getModificationTime()) {
310 if (AskQuestion(_("An emergency save of this document exists!"),
311 MakeDisplayPath(s, 50),
312 _("Try to load that instead?"))) {
314 // the file is not saved if we load the
317 use_emergency = true;
319 // Here, we should delete the emergency save
325 if (!use_emergency) {
326 // Now check if autosave file is newer.
328 a += OnlyFilename(s);
330 FileInfo fileInfoA(a);
331 if (fileInfoA.exist() && fileInfo2.exist()) {
332 if (fileInfoA.getModificationTime()
333 > fileInfo2.getModificationTime()) {
334 if (AskQuestion(_("Autosave file is newer."),
335 MakeDisplayPath(s, 50),
336 _("Load that one instead?"))) {
338 // the file is not saved if we load the
342 // Here, we should delete the autosave
348 // not sure if this is the correct place to begin LyXLex
351 if (b->readFile(lex))
360 bool BufferList::exists(string const & s) const
362 for (BufferStorage::const_iterator cit = bstore.begin();
363 cit != bstore.end(); ++cit) {
364 if ((*cit)->fileName() == s)
371 bool BufferList::isLoaded(Buffer const * b) const
373 BufferStorage::const_iterator cit =
374 find(bstore.begin(), bstore.end(), b);
375 return cit != bstore.end();
379 Buffer * BufferList::getBuffer(string const & s)
381 for(BufferStorage::iterator it = bstore.begin();
382 it != bstore.end(); ++it) {
383 if ((*it)->fileName() == s)
390 Buffer * BufferList::newFile(string const & name, string tname)
393 Buffer * b = bstore.newBuffer(name);
395 // use defaults.lyx as a default template if it exists.
397 tname = LibFileSearch("templates", "defaults.lyx");
399 if (!tname.empty() && IsLyXFilename(tname)) {
400 bool templateok = false;
404 if (b->readFile(lex)) {
409 WriteAlert(_("Error!"), _("Unable to open template"),
410 MakeDisplayPath(tname));
411 // no template, start with empty buffer
412 b->paragraph = new LyXParagraph;
415 else { // start with empty buffer
416 b->paragraph = new LyXParagraph;
420 b->setReadonly(false);
426 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
428 // make sure our path is absolute
429 string s = MakeAbsPath(filename);
431 // file already open?
433 if (AskQuestion(_("Document is already open:"),
434 MakeDisplayPath(s, 50),
435 _("Do you want to reload that document?"))) {
436 // Reload is accomplished by closing and then loading
437 if (!close(getBuffer(s))) {
440 // Fall through to new load. (Asger)
442 // Here, we pretend that we just loaded the
449 switch (IsFileWriteable(s)) {
452 current_view->owner()->getMiniBuffer()->
453 Set(_("File `") + MakeDisplayPath(s, 50) +
454 _("' is read-only."));
461 b->lyxvc.file_found_hook(s);
463 break; //fine- it's r/w
465 // Here we probably should run
466 if (LyXVC::file_not_found_hook(s)) {
467 // Ask if the file should be checked out for
468 // viewing/editing, if so: load it.
469 if (AskQuestion(_("Do you want to retrive file under version control?"))) {
470 // How can we know _how_ to do the checkout?
471 // With the current VC support it has to be,
472 // a RCS file since CVS do not have special ,v files.
474 return loadLyXFile(filename, tolastfiles);
477 if (AskQuestion(_("Cannot open specified file:"),
478 MakeDisplayPath(s, 50),
479 _("Create new document with this name?")))
481 // Find a free buffer
482 b = newFile(s, string());
487 if (b && tolastfiles)
488 lastfiles->newFile(b->fileName());