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));
118 askMoreConfirmation = true;
119 unsaved += MakeDisplayPath(fname, 50);
129 if (askMoreConfirmation &&
130 lyxrc.exit_confirmation &&
131 !AskQuestion(_("Some documents were not saved:"),
132 unsaved, _("Exit anyway?"))) {
140 void BufferList::closeAll()
142 state_ = BufferList::CLOSING;
143 // Since we are closing we can just as well delete all
144 // in the textcache this will also speed the closing/quiting up a bit.
147 while (!bstore.empty()) {
148 close(bstore.front());
150 state_ = BufferList::OK;
154 void BufferList::resize()
156 for(BufferStorage::iterator it = bstore.begin();
157 it != bstore.end(); ++it) {
163 bool BufferList::close(Buffer * buf)
166 // Trace back why we need to use buf->getUser here.
167 // Perhaps slight rewrite is in order? (Lgb)
169 if (buf->getUser()) buf->getUser()->insetUnlock();
170 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
172 ProhibitInput(buf->getUser());
174 if (buf->isUnnamed())
175 fname = OnlyFilename(buf->fileName());
177 fname = MakeDisplayPath(buf->fileName(), 50);
180 switch(AskConfirmation(_("Changes in document:"),
182 _("Save document?"))){
184 if (buf->isUnnamed())
185 reask = !MenuWriteAs(buf);
186 else if (buf->save()) {
187 lastfiles->newFile(buf->fileName());
190 AllowInput(buf->getUser());
199 AllowInput(buf->getUser());
204 AllowInput(buf->getUser());
212 vector<string> BufferList::getFileNames() const
215 for(BufferStorage::const_iterator cit = bstore.begin();
216 cit != bstore.end(); ++cit) {
217 nvec.push_back((*cit)->fileName());
223 Buffer * BufferList::first()
225 if (bstore.empty()) return 0;
226 return bstore.front();
230 Buffer * BufferList::getBuffer(int choice)
232 if (choice >= bstore.size()) return 0;
233 return bstore[choice];
237 int BufferList::unlockInset(UpdatableInset * inset)
239 if (!inset) return 1;
240 for(BufferStorage::iterator it = bstore.begin();
241 it != bstore.end(); ++it) {
243 && (*it)->getUser()->the_locking_inset == inset) {
244 (*it)->getUser()->insetUnlock();
252 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
254 for(BufferStorage::iterator it = bstore.begin();
255 it != bstore.end(); ++it) {
256 if (!(*it)->isDepClean(mastertmpdir)) {
257 string writefile = mastertmpdir;
259 writefile += (*it)->getLatexName();
260 (*it)->makeLaTeXFile(writefile, mastertmpdir,
262 (*it)->markDepClean(mastertmpdir);
268 void BufferList::emergencyWriteAll()
270 for (BufferStorage::iterator it = bstore.begin();
271 it != bstore.end(); ++it) {
272 if (!(*it)->isLyxClean()) {
275 lyxerr <<_("lyx: Attempting to save"
277 if ((*it)->isUnnamed())
278 lyxerr << OnlyFilename((*it)->fileName());
280 lyxerr << (*it)->fileName();
281 lyxerr << _(" as...") << endl;
283 for (int i = 0; i < 3 && !madeit; ++i) {
286 // We try to save three places:
287 // 1) Same place as document.
288 // 2) In HOME directory.
289 // 3) In "/tmp" directory.
291 if ((*it)->isUnnamed())
293 s = (*it)->fileName();
295 s = AddName(GetEnvPath("HOME"),
298 // MakeAbsPath to prepend the current
299 // drive letter on OS/2
300 s = AddName(MakeAbsPath("/tmp/"),
305 lyxerr << " " << i + 1 << ") " << s << endl;
307 if ((*it)->writeFile(s, true)) {
308 (*it)->markLyxClean();
309 lyxerr << _(" Save seems successful. "
313 lyxerr << _(" Save failed! Trying...")
316 lyxerr << _(" Save failed! Bummer. "
326 Buffer * BufferList::readFile(string const & s, bool ronly)
328 Buffer * b = bstore.newBuffer(s, ronly);
331 string e = OnlyPath(s);
333 // File information about normal file
334 FileInfo fileInfo2(s);
336 // Check if emergency save file exists and is newer.
337 e += OnlyFilename(s) + ".emergency";
338 FileInfo fileInfoE(e);
340 bool use_emergency = false;
342 if (fileInfoE.exist() && fileInfo2.exist()) {
343 if (fileInfoE.getModificationTime()
344 > fileInfo2.getModificationTime()) {
345 if (AskQuestion(_("An emergency save of this document exists!"),
346 MakeDisplayPath(s, 50),
347 _("Try to load that instead?"))) {
349 // the file is not saved if we load the
352 use_emergency = true;
354 // Here, we should delete the emergency save
360 if (!use_emergency) {
361 // Now check if autosave file is newer.
363 a += OnlyFilename(s);
365 FileInfo fileInfoA(a);
366 if (fileInfoA.exist() && fileInfo2.exist()) {
367 if (fileInfoA.getModificationTime()
368 > fileInfo2.getModificationTime()) {
369 if (AskQuestion(_("Autosave file is newer."),
370 MakeDisplayPath(s, 50),
371 _("Load that one instead?"))) {
373 // the file is not saved if we load the
377 // Here, we should delete the autosave
383 // not sure if this is the correct place to begin LyXLex
386 if (b->readFile(lex))
395 bool BufferList::exists(string const & s) const
397 for (BufferStorage::const_iterator cit = bstore.begin();
398 cit != bstore.end(); ++cit) {
399 if ((*cit)->fileName() == s)
406 bool BufferList::isLoaded(Buffer const * b) const
408 BufferStorage::const_iterator cit =
409 find(bstore.begin(), bstore.end(), b);
410 return cit != bstore.end();
414 Buffer * BufferList::getBuffer(string const & s)
416 for(BufferStorage::iterator it = bstore.begin();
417 it != bstore.end(); ++it) {
418 if ((*it)->fileName() == s)
425 Buffer * BufferList::newFile(string const & name, string tname)
428 Buffer * b = bstore.newBuffer(name);
430 // use defaults.lyx as a default template if it exists.
432 tname = LibFileSearch("templates", "defaults.lyx");
434 if (!tname.empty() && IsLyXFilename(tname)) {
435 bool templateok = false;
439 if (b->readFile(lex)) {
444 WriteAlert(_("Error!"), _("Unable to open template"),
445 MakeDisplayPath(tname));
446 // no template, start with empty buffer
447 b->paragraph = new LyXParagraph;
450 else { // start with empty buffer
451 b->paragraph = new LyXParagraph;
454 #ifdef NEW_WITH_FILENAME
457 #warning Why mark a new document dirty? I deactivate this for unnamed docs! (Jug)
460 b->setReadonly(false);
466 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
468 // make sure our path is absolute
469 string s = MakeAbsPath(filename);
471 // file already open?
473 if (AskQuestion(_("Document is already open:"),
474 MakeDisplayPath(s, 50),
475 _("Do you want to reload that document?"))) {
476 // Reload is accomplished by closing and then loading
477 if (!close(getBuffer(s))) {
480 // Fall through to new load. (Asger)
482 // Here, we pretend that we just loaded the
489 switch (IsFileWriteable(s)) {
492 current_view->owner()->getMiniBuffer()->
493 Set(_("File `") + MakeDisplayPath(s, 50) +
494 _("' is read-only."));
501 b->lyxvc.file_found_hook(s);
503 break; //fine- it's r/w
505 // Here we probably should run
506 if (LyXVC::file_not_found_hook(s)) {
507 // Ask if the file should be checked out for
508 // viewing/editing, if so: load it.
509 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
510 // How can we know _how_ to do the checkout?
511 // With the current VC support it has to be,
512 // a RCS file since CVS do not have special ,v files.
514 return loadLyXFile(filename, tolastfiles);
517 if (AskQuestion(_("Cannot open specified file:"),
518 MakeDisplayPath(s, 50),
519 _("Create new document with this name?")))
521 // Find a free buffer
522 b = newFile(s, string());
527 if (b && tolastfiles)
528 lastfiles->newFile(b->fileName());