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 bool 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));
115 reask = !MenuWrite((*it));
119 // if we crash after this we could
120 // have no autosave file but I guess
121 // this is really inprobable (Jug)
122 if ((*it)->isUnnamed()) {
123 removeAutosaveFile((*it)->fileName());
125 askMoreConfirmation = true;
126 unsaved += MakeDisplayPath(fname, 50);
136 if (askMoreConfirmation &&
137 lyxrc.exit_confirmation &&
138 !AskQuestion(_("Some documents were not saved:"),
139 unsaved, _("Exit anyway?"))) {
147 void BufferList::closeAll()
149 state_ = BufferList::CLOSING;
150 // Since we are closing we can just as well delete all
151 // in the textcache this will also speed the closing/quiting up a bit.
154 while (!bstore.empty()) {
155 close(bstore.front());
157 state_ = BufferList::OK;
161 void BufferList::resize()
163 for(BufferStorage::iterator it = bstore.begin();
164 it != bstore.end(); ++it) {
170 bool BufferList::close(Buffer * buf)
173 // Trace back why we need to use buf->getUser here.
174 // Perhaps slight rewrite is in order? (Lgb)
176 if (buf->getUser()) buf->getUser()->insetUnlock();
177 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
179 ProhibitInput(buf->getUser());
181 if (buf->isUnnamed())
182 fname = OnlyFilename(buf->fileName());
184 fname = MakeDisplayPath(buf->fileName(), 50);
187 switch(AskConfirmation(_("Changes in document:"),
189 _("Save document?"))){
191 if (buf->isUnnamed())
192 reask = !MenuWriteAs(buf);
193 else if (buf->save()) {
194 lastfiles->newFile(buf->fileName());
198 AllowInput(buf->getUser());
203 if (buf->isUnnamed()) {
204 removeAutosaveFile(buf->fileName());
210 AllowInput(buf->getUser());
215 AllowInput(buf->getUser());
223 vector<string> const BufferList::getFileNames() const
226 for(BufferStorage::const_iterator cit = bstore.begin();
227 cit != bstore.end(); ++cit) {
228 nvec.push_back((*cit)->fileName());
234 Buffer * BufferList::first()
236 if (bstore.empty()) return 0;
237 return bstore.front();
241 Buffer * BufferList::getBuffer(int choice)
243 if (choice >= bstore.size()) return 0;
244 return bstore[choice];
248 int BufferList::unlockInset(UpdatableInset * inset)
250 if (!inset) return 1;
251 for(BufferStorage::iterator it = bstore.begin();
252 it != bstore.end(); ++it) {
254 && (*it)->getUser()->the_locking_inset == inset) {
255 (*it)->getUser()->insetUnlock();
263 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
265 for(BufferStorage::iterator it = bstore.begin();
266 it != bstore.end(); ++it) {
267 if (!(*it)->isDepClean(mastertmpdir)) {
268 string writefile = mastertmpdir;
270 writefile += (*it)->getLatexName();
271 (*it)->makeLaTeXFile(writefile, mastertmpdir,
273 (*it)->markDepClean(mastertmpdir);
279 void BufferList::emergencyWriteAll()
281 for (BufferStorage::iterator it = bstore.begin();
282 it != bstore.end(); ++it) {
283 if (!(*it)->isLyxClean()) {
286 lyxerr <<_("lyx: Attempting to save"
288 if ((*it)->isUnnamed())
289 lyxerr << OnlyFilename((*it)->fileName());
291 lyxerr << (*it)->fileName();
292 lyxerr << _(" as...") << endl;
294 for (int i = 0; i < 3 && !madeit; ++i) {
297 // We try to save three places:
298 // 1) Same place as document.
299 // 2) In HOME directory.
300 // 3) In "/tmp" directory.
302 if ((*it)->isUnnamed())
304 s = (*it)->fileName();
306 s = AddName(GetEnvPath("HOME"),
309 // MakeAbsPath to prepend the current
310 // drive letter on OS/2
311 s = AddName(MakeAbsPath("/tmp/"),
316 lyxerr << " " << i + 1 << ") " << s << endl;
318 if ((*it)->writeFile(s, true)) {
319 (*it)->markLyxClean();
320 lyxerr << _(" Save seems successful. "
324 lyxerr << _(" Save failed! Trying...")
327 lyxerr << _(" Save failed! Bummer. "
337 Buffer * BufferList::readFile(string const & s, bool ronly)
339 Buffer * b = bstore.newBuffer(s, ronly);
342 string e = OnlyPath(s);
344 // File information about normal file
345 FileInfo fileInfo2(s);
347 // Check if emergency save file exists and is newer.
348 e += OnlyFilename(s) + ".emergency";
349 FileInfo fileInfoE(e);
351 bool use_emergency = false;
353 if (fileInfoE.exist() && fileInfo2.exist()) {
354 if (fileInfoE.getModificationTime()
355 > fileInfo2.getModificationTime()) {
356 if (AskQuestion(_("An emergency save of this document exists!"),
357 MakeDisplayPath(s, 50),
358 _("Try to load that instead?"))) {
360 // the file is not saved if we load the
363 use_emergency = true;
365 // Here, we should delete the emergency save
371 if (!use_emergency) {
372 // Now check if autosave file is newer.
374 a += OnlyFilename(s);
376 FileInfo fileInfoA(a);
377 if (fileInfoA.exist() && fileInfo2.exist()) {
378 if (fileInfoA.getModificationTime()
379 > fileInfo2.getModificationTime()) {
380 if (AskQuestion(_("Autosave file is newer."),
381 MakeDisplayPath(s, 50),
382 _("Load that one instead?"))) {
384 // the file is not saved if we load the
388 // Here, we should delete the autosave
394 // not sure if this is the correct place to begin LyXLex
397 if (b->readFile(lex))
406 bool BufferList::exists(string const & s) const
408 for (BufferStorage::const_iterator cit = bstore.begin();
409 cit != bstore.end(); ++cit) {
410 if ((*cit)->fileName() == s)
417 bool BufferList::isLoaded(Buffer const * b) const
419 BufferStorage::const_iterator cit =
420 find(bstore.begin(), bstore.end(), b);
421 return cit != bstore.end();
425 Buffer * BufferList::getBuffer(string const & s)
427 for(BufferStorage::iterator it = bstore.begin();
428 it != bstore.end(); ++it) {
429 if ((*it)->fileName() == s)
436 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
439 Buffer * b = bstore.newBuffer(name);
441 // use defaults.lyx as a default template if it exists.
443 tname = LibFileSearch("templates", "defaults.lyx");
445 if (!tname.empty() && IsLyXFilename(tname)) {
446 bool templateok = false;
450 if (b->readFile(lex)) {
455 WriteAlert(_("Error!"), _("Unable to open template"),
456 MakeDisplayPath(tname));
457 // no template, start with empty buffer
458 b->paragraph = new LyXParagraph;
460 } else { // start with empty buffer
461 b->paragraph = new LyXParagraph;
464 if (!lyxrc.new_ask_filename) {
468 b->setReadonly(false);
474 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
476 // make sure our path is absolute
477 string const s = MakeAbsPath(filename);
479 // file already open?
481 if (AskQuestion(_("Document is already open:"),
482 MakeDisplayPath(s, 50),
483 _("Do you want to reload that document?"))) {
484 // Reload is accomplished by closing and then loading
485 if (!close(getBuffer(s))) {
488 // Fall through to new load. (Asger)
490 // Here, we pretend that we just loaded the
497 switch (IsFileWriteable(s)) {
500 current_view->owner()->getMiniBuffer()->
501 Set(_("File `") + MakeDisplayPath(s, 50) +
502 _("' is read-only."));
509 b->lyxvc.file_found_hook(s);
511 break; //fine- it's r/w
513 // Here we probably should run
514 if (LyXVC::file_not_found_hook(s)) {
515 // Ask if the file should be checked out for
516 // viewing/editing, if so: load it.
517 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
518 // How can we know _how_ to do the checkout?
519 // With the current VC support it has to be,
520 // a RCS file since CVS do not have special ,v files.
522 return loadLyXFile(filename, tolastfiles);
525 if (AskQuestion(_("Cannot open specified file:"),
526 MakeDisplayPath(s, 50),
527 _("Create new document with this name?")))
529 // Find a free buffer
530 b = newFile(s, string(), true);
535 if (b && tolastfiles)
536 lastfiles->newFile(b->fileName());