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)
155 // Trace back why we need to use buf->getUser here.
156 // Perhaps slight rewrite is in order? (Lgb)
158 if (buf->getUser()) buf->getUser()->insetUnlock();
159 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
160 if (buf->getUser()) ProhibitInput(buf->getUser());
161 switch(AskConfirmation(_("Changes in document:"),
162 MakeDisplayPath(buf->fileName(), 50),
163 _("Save document?"))){
166 lastfiles->newFile(buf->fileName());
168 if (buf->getUser()) AllowInput(buf->getUser());
173 if (buf->getUser()) AllowInput(buf->getUser());
176 if (buf->getUser()) AllowInput(buf->getUser());
184 vector<string> BufferList::getFileNames() const
187 for(BufferStorage::const_iterator cit = bstore.begin();
188 cit != bstore.end(); ++cit) {
189 nvec.push_back((*cit)->fileName());
195 Buffer * BufferList::first()
197 if (bstore.empty()) return 0;
198 return bstore.front();
202 Buffer * BufferList::getBuffer(int choice)
204 if (choice >= bstore.size()) return 0;
205 return bstore[choice];
209 int BufferList::unlockInset(UpdatableInset * inset)
211 if (!inset) return 1;
212 for(BufferStorage::iterator it = bstore.begin();
213 it != bstore.end(); ++it) {
215 && (*it)->getUser()->the_locking_inset == inset) {
216 (*it)->getUser()->insetUnlock();
224 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
226 for(BufferStorage::iterator it = bstore.begin();
227 it != bstore.end(); ++it) {
228 if (!(*it)->isDepClean(mastertmpdir)) {
229 string writefile = mastertmpdir;
231 writefile += (*it)->getLatexName();
232 (*it)->makeLaTeXFile(writefile, mastertmpdir,
234 (*it)->markDepClean(mastertmpdir);
240 void BufferList::emergencyWriteAll()
242 for (BufferStorage::iterator it = bstore.begin();
243 it != bstore.end(); ++it) {
244 if (!(*it)->isLyxClean()) {
247 lyxerr <<_("lyx: Attempting to save"
250 << _(" as...") << endl;
252 for (int i = 0; i < 3 && !madeit; ++i) {
255 // We try to save three places:
256 // 1) Same place as document.
257 // 2) In HOME directory.
258 // 3) In "/tmp" directory.
260 s = (*it)->fileName();
262 s = AddName(GetEnvPath("HOME"),
265 // MakeAbsPath to prepend the current
266 // drive letter on OS/2
267 s = AddName(MakeAbsPath("/tmp/"),
272 lyxerr << " " << i + 1 << ") " << s << endl;
274 if ((*it)->writeFile(s, true)) {
275 (*it)->markLyxClean();
276 lyxerr << _(" Save seems successful. "
280 lyxerr << _(" Save failed! Trying...")
283 lyxerr << _(" Save failed! Bummer. "
293 Buffer * BufferList::readFile(string const & s, bool ronly)
295 Buffer * b = bstore.newBuffer(s, ronly);
298 string e = OnlyPath(s);
300 // File information about normal file
301 FileInfo fileInfo2(s);
303 // Check if emergency save file exists and is newer.
304 e += OnlyFilename(s) + ".emergency";
305 FileInfo fileInfoE(e);
307 bool use_emergency = false;
309 if (fileInfoE.exist() && fileInfo2.exist()) {
310 if (fileInfoE.getModificationTime()
311 > fileInfo2.getModificationTime()) {
312 if (AskQuestion(_("An emergency save of this document exists!"),
313 MakeDisplayPath(s, 50),
314 _("Try to load that instead?"))) {
316 // the file is not saved if we load the
319 use_emergency = true;
321 // Here, we should delete the emergency save
327 if (!use_emergency) {
328 // Now check if autosave file is newer.
330 a += OnlyFilename(s);
332 FileInfo fileInfoA(a);
333 if (fileInfoA.exist() && fileInfo2.exist()) {
334 if (fileInfoA.getModificationTime()
335 > fileInfo2.getModificationTime()) {
336 if (AskQuestion(_("Autosave file is newer."),
337 MakeDisplayPath(s, 50),
338 _("Load that one instead?"))) {
340 // the file is not saved if we load the
344 // Here, we should delete the autosave
350 // not sure if this is the correct place to begin LyXLex
353 if (b->readFile(lex))
362 bool BufferList::exists(string const & s) const
364 for (BufferStorage::const_iterator cit = bstore.begin();
365 cit != bstore.end(); ++cit) {
366 if ((*cit)->fileName() == s)
373 bool BufferList::isLoaded(Buffer const * b) const
375 BufferStorage::const_iterator cit =
376 find(bstore.begin(), bstore.end(), b);
377 return cit != bstore.end();
381 Buffer * BufferList::getBuffer(string const & s)
383 for(BufferStorage::iterator it = bstore.begin();
384 it != bstore.end(); ++it) {
385 if ((*it)->fileName() == s)
392 Buffer * BufferList::newFile(string const & name, string tname)
395 Buffer * b = bstore.newBuffer(name);
397 // use defaults.lyx as a default template if it exists.
399 tname = LibFileSearch("templates", "defaults.lyx");
401 if (!tname.empty() && IsLyXFilename(tname)) {
402 bool templateok = false;
406 if (b->readFile(lex)) {
411 WriteAlert(_("Error!"), _("Unable to open template"),
412 MakeDisplayPath(tname));
413 // no template, start with empty buffer
414 b->paragraph = new LyXParagraph;
417 else { // start with empty buffer
418 b->paragraph = new LyXParagraph;
422 b->setReadonly(false);
428 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
430 // make sure our path is absolute
431 string s = MakeAbsPath(filename);
433 // file already open?
435 if (AskQuestion(_("Document is already open:"),
436 MakeDisplayPath(s, 50),
437 _("Do you want to reload that document?"))) {
438 // Reload is accomplished by closing and then loading
439 if (!close(getBuffer(s))) {
442 // Fall through to new load. (Asger)
444 // Here, we pretend that we just loaded the
451 switch (IsFileWriteable(s)) {
454 current_view->owner()->getMiniBuffer()->
455 Set(_("File `") + MakeDisplayPath(s, 50) +
456 _("' is read-only."));
463 b->lyxvc.file_found_hook(s);
465 break; //fine- it's r/w
467 // Here we probably should run
468 if (LyXVC::file_not_found_hook(s)) {
469 // Ask if the file should be checked out for
470 // viewing/editing, if so: load it.
471 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
472 // How can we know _how_ to do the checkout?
473 // With the current VC support it has to be,
474 // a RCS file since CVS do not have special ,v files.
476 return loadLyXFile(filename, tolastfiles);
479 if (AskQuestion(_("Cannot open specified file:"),
480 MakeDisplayPath(s, 50),
481 _("Create new document with this name?")))
483 // Find a free buffer
484 b = newFile(s, string());
489 if (b && tolastfiles)
490 lastfiles->newFile(b->fileName());