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 *);
92 extern bool MenuWriteAs(Buffer *);
94 bool BufferList::QwriteAll()
96 bool askMoreConfirmation = false;
98 for(BufferStorage::iterator it = bstore.begin();
99 it != bstore.end(); ++it) {
100 if (!(*it)->isLyxClean()) {
102 if ((*it)->isUnnamed())
103 fname = OnlyFilename((*it)->fileName());
105 fname = MakeDisplayPath((*it)->fileName(), 50);
108 switch(AskConfirmation(_("Changes in document:"),
110 _("Save document?"))) {
112 if ((*it)->isUnnamed())
113 reask = !MenuWriteAs((*it));
120 askMoreConfirmation = true;
121 unsaved += MakeDisplayPath(fname, 50);
131 if (askMoreConfirmation &&
132 lyxrc.exit_confirmation &&
133 !AskQuestion(_("Some documents were not saved:"),
134 unsaved, _("Exit anyway?"))) {
142 void BufferList::closeAll()
144 state_ = BufferList::CLOSING;
145 // Since we are closing we can just as well delete all
146 // in the textcache this will also speed the closing/quiting up a bit.
149 while (!bstore.empty()) {
150 close(bstore.front());
152 state_ = BufferList::OK;
156 void BufferList::resize()
158 for(BufferStorage::iterator it = bstore.begin();
159 it != bstore.end(); ++it) {
165 bool BufferList::close(Buffer * buf)
168 // Trace back why we need to use buf->getUser here.
169 // Perhaps slight rewrite is in order? (Lgb)
171 if (buf->getUser()) buf->getUser()->insetUnlock();
172 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
174 ProhibitInput(buf->getUser());
176 if (buf->isUnnamed())
177 fname = OnlyFilename(buf->fileName());
179 fname = MakeDisplayPath(buf->fileName(), 50);
182 switch(AskConfirmation(_("Changes in document:"),
184 _("Save document?"))){
186 if (buf->isUnnamed())
187 reask = !MenuWriteAs(buf);
188 else if (buf->save()) {
189 lastfiles->newFile(buf->fileName());
192 AllowInput(buf->getUser());
201 AllowInput(buf->getUser());
206 AllowInput(buf->getUser());
214 vector<string> BufferList::getFileNames() const
217 for(BufferStorage::const_iterator cit = bstore.begin();
218 cit != bstore.end(); ++cit) {
219 nvec.push_back((*cit)->fileName());
225 Buffer * BufferList::first()
227 if (bstore.empty()) return 0;
228 return bstore.front();
232 Buffer * BufferList::getBuffer(int choice)
234 if (choice >= bstore.size()) return 0;
235 return bstore[choice];
239 int BufferList::unlockInset(UpdatableInset * inset)
241 if (!inset) return 1;
242 for(BufferStorage::iterator it = bstore.begin();
243 it != bstore.end(); ++it) {
245 && (*it)->getUser()->the_locking_inset == inset) {
246 (*it)->getUser()->insetUnlock();
254 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
256 for(BufferStorage::iterator it = bstore.begin();
257 it != bstore.end(); ++it) {
258 if (!(*it)->isDepClean(mastertmpdir)) {
259 string writefile = mastertmpdir;
261 writefile += (*it)->getLatexName();
262 (*it)->makeLaTeXFile(writefile, mastertmpdir,
264 (*it)->markDepClean(mastertmpdir);
270 void BufferList::emergencyWriteAll()
272 for (BufferStorage::iterator it = bstore.begin();
273 it != bstore.end(); ++it) {
274 if (!(*it)->isLyxClean()) {
277 lyxerr <<_("lyx: Attempting to save"
279 if ((*it)->isUnnamed())
280 lyxerr << OnlyFilename((*it)->fileName());
282 lyxerr << (*it)->fileName();
283 lyxerr << _(" as...") << endl;
285 for (int i = 0; i < 3 && !madeit; ++i) {
288 // We try to save three places:
289 // 1) Same place as document.
290 // 2) In HOME directory.
291 // 3) In "/tmp" directory.
293 if ((*it)->isUnnamed())
295 s = (*it)->fileName();
297 s = AddName(GetEnvPath("HOME"),
300 // MakeAbsPath to prepend the current
301 // drive letter on OS/2
302 s = AddName(MakeAbsPath("/tmp/"),
307 lyxerr << " " << i + 1 << ") " << s << endl;
309 if ((*it)->writeFile(s, true)) {
310 (*it)->markLyxClean();
311 lyxerr << _(" Save seems successful. "
315 lyxerr << _(" Save failed! Trying...")
318 lyxerr << _(" Save failed! Bummer. "
328 Buffer * BufferList::readFile(string const & s, bool ronly)
330 Buffer * b = bstore.newBuffer(s, ronly);
333 string e = OnlyPath(s);
335 // File information about normal file
336 FileInfo fileInfo2(s);
338 // Check if emergency save file exists and is newer.
339 e += OnlyFilename(s) + ".emergency";
340 FileInfo fileInfoE(e);
342 bool use_emergency = false;
344 if (fileInfoE.exist() && fileInfo2.exist()) {
345 if (fileInfoE.getModificationTime()
346 > fileInfo2.getModificationTime()) {
347 if (AskQuestion(_("An emergency save of this document exists!"),
348 MakeDisplayPath(s, 50),
349 _("Try to load that instead?"))) {
351 // the file is not saved if we load the
354 use_emergency = true;
356 // Here, we should delete the emergency save
362 if (!use_emergency) {
363 // Now check if autosave file is newer.
365 a += OnlyFilename(s);
367 FileInfo fileInfoA(a);
368 if (fileInfoA.exist() && fileInfo2.exist()) {
369 if (fileInfoA.getModificationTime()
370 > fileInfo2.getModificationTime()) {
371 if (AskQuestion(_("Autosave file is newer."),
372 MakeDisplayPath(s, 50),
373 _("Load that one instead?"))) {
375 // the file is not saved if we load the
379 // Here, we should delete the autosave
385 // not sure if this is the correct place to begin LyXLex
388 if (b->readFile(lex))
397 bool BufferList::exists(string const & s) const
399 for (BufferStorage::const_iterator cit = bstore.begin();
400 cit != bstore.end(); ++cit) {
401 if ((*cit)->fileName() == s)
408 bool BufferList::isLoaded(Buffer const * b) const
410 BufferStorage::const_iterator cit =
411 find(bstore.begin(), bstore.end(), b);
412 return cit != bstore.end();
416 Buffer * BufferList::getBuffer(string const & s)
418 for(BufferStorage::iterator it = bstore.begin();
419 it != bstore.end(); ++it) {
420 if ((*it)->fileName() == s)
427 Buffer * BufferList::newFile(string const & name, string tname)
430 Buffer * b = bstore.newBuffer(name);
432 // use defaults.lyx as a default template if it exists.
434 tname = LibFileSearch("templates", "defaults.lyx");
436 if (!tname.empty() && IsLyXFilename(tname)) {
437 bool templateok = false;
441 if (b->readFile(lex)) {
446 WriteAlert(_("Error!"), _("Unable to open template"),
447 MakeDisplayPath(tname));
448 // no template, start with empty buffer
449 b->paragraph = new LyXParagraph;
452 else { // start with empty buffer
453 b->paragraph = new LyXParagraph;
456 #ifdef NEW_WITH_FILENAME
459 #warning Why mark a new document dirty? I deactivate this for unnamed docs! (Jug)
462 b->setReadonly(false);
468 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
470 // make sure our path is absolute
471 string 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)) {
494 current_view->owner()->getMiniBuffer()->
495 Set(_("File `") + MakeDisplayPath(s, 50) +
496 _("' is read-only."));
503 b->lyxvc.file_found_hook(s);
505 break; //fine- it's r/w
507 // Here we probably should run
508 if (LyXVC::file_not_found_hook(s)) {
509 // Ask if the file should be checked out for
510 // viewing/editing, if so: load it.
511 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
512 // How can we know _how_ to do the checkout?
513 // With the current VC support it has to be,
514 // a RCS file since CVS do not have special ,v files.
516 return loadLyXFile(filename, tolastfiles);
519 if (AskQuestion(_("Cannot open specified file:"),
520 MakeDisplayPath(s, 50),
521 _("Create new document with this name?")))
523 // Find a free buffer
524 b = newFile(s, string());
529 if (b && tolastfiles)
530 lastfiles->newFile(b->fileName());