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());
197 AllowInput(buf->getUser());
202 if (buf->isUnnamed()) {
203 removeAutosaveFile(buf->fileName());
209 AllowInput(buf->getUser());
214 AllowInput(buf->getUser());
222 vector<string> const BufferList::getFileNames() const
225 for(BufferStorage::const_iterator cit = bstore.begin();
226 cit != bstore.end(); ++cit) {
227 nvec.push_back((*cit)->fileName());
233 Buffer * BufferList::first()
235 if (bstore.empty()) return 0;
236 return bstore.front();
240 Buffer * BufferList::getBuffer(int choice)
242 if (choice >= bstore.size()) return 0;
243 return bstore[choice];
247 int BufferList::unlockInset(UpdatableInset * inset)
249 if (!inset) return 1;
250 for(BufferStorage::iterator it = bstore.begin();
251 it != bstore.end(); ++it) {
253 && (*it)->getUser()->the_locking_inset == inset) {
254 (*it)->getUser()->insetUnlock();
262 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
264 for(BufferStorage::iterator it = bstore.begin();
265 it != bstore.end(); ++it) {
266 if (!(*it)->isDepClean(mastertmpdir)) {
267 string writefile = mastertmpdir;
269 writefile += (*it)->getLatexName();
270 (*it)->makeLaTeXFile(writefile, mastertmpdir,
272 (*it)->markDepClean(mastertmpdir);
278 void BufferList::emergencyWriteAll()
280 for (BufferStorage::iterator it = bstore.begin();
281 it != bstore.end(); ++it) {
282 if (!(*it)->isLyxClean()) {
285 lyxerr <<_("lyx: Attempting to save"
287 if ((*it)->isUnnamed())
288 lyxerr << OnlyFilename((*it)->fileName());
290 lyxerr << (*it)->fileName();
291 lyxerr << _(" as...") << endl;
293 for (int i = 0; i < 3 && !madeit; ++i) {
296 // We try to save three places:
297 // 1) Same place as document.
298 // 2) In HOME directory.
299 // 3) In "/tmp" directory.
301 if ((*it)->isUnnamed())
303 s = (*it)->fileName();
305 s = AddName(GetEnvPath("HOME"),
308 // MakeAbsPath to prepend the current
309 // drive letter on OS/2
310 s = AddName(MakeAbsPath("/tmp/"),
315 lyxerr << " " << i + 1 << ") " << s << endl;
317 if ((*it)->writeFile(s, true)) {
318 (*it)->markLyxClean();
319 lyxerr << _(" Save seems successful. "
323 lyxerr << _(" Save failed! Trying...")
326 lyxerr << _(" Save failed! Bummer. "
336 Buffer * BufferList::readFile(string const & s, bool ronly)
338 Buffer * b = bstore.newBuffer(s, ronly);
341 string e = OnlyPath(s);
343 // File information about normal file
344 FileInfo fileInfo2(s);
346 // Check if emergency save file exists and is newer.
347 e += OnlyFilename(s) + ".emergency";
348 FileInfo fileInfoE(e);
350 bool use_emergency = false;
352 if (fileInfoE.exist() && fileInfo2.exist()) {
353 if (fileInfoE.getModificationTime()
354 > fileInfo2.getModificationTime()) {
355 if (AskQuestion(_("An emergency save of this document exists!"),
356 MakeDisplayPath(s, 50),
357 _("Try to load that instead?"))) {
359 // the file is not saved if we load the
362 use_emergency = true;
364 // Here, we should delete the emergency save
370 if (!use_emergency) {
371 // Now check if autosave file is newer.
373 a += OnlyFilename(s);
375 FileInfo fileInfoA(a);
376 if (fileInfoA.exist() && fileInfo2.exist()) {
377 if (fileInfoA.getModificationTime()
378 > fileInfo2.getModificationTime()) {
379 if (AskQuestion(_("Autosave file is newer."),
380 MakeDisplayPath(s, 50),
381 _("Load that one instead?"))) {
383 // the file is not saved if we load the
387 // Here, we should delete the autosave
393 // not sure if this is the correct place to begin LyXLex
396 if (b->readFile(lex))
405 bool BufferList::exists(string const & s) const
407 for (BufferStorage::const_iterator cit = bstore.begin();
408 cit != bstore.end(); ++cit) {
409 if ((*cit)->fileName() == s)
416 bool BufferList::isLoaded(Buffer const * b) const
418 BufferStorage::const_iterator cit =
419 find(bstore.begin(), bstore.end(), b);
420 return cit != bstore.end();
424 Buffer * BufferList::getBuffer(string const & s)
426 for(BufferStorage::iterator it = bstore.begin();
427 it != bstore.end(); ++it) {
428 if ((*it)->fileName() == s)
435 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
438 Buffer * b = bstore.newBuffer(name);
440 // use defaults.lyx as a default template if it exists.
442 tname = LibFileSearch("templates", "defaults.lyx");
444 if (!tname.empty() && IsLyXFilename(tname)) {
445 bool templateok = false;
449 if (b->readFile(lex)) {
454 WriteAlert(_("Error!"), _("Unable to open template"),
455 MakeDisplayPath(tname));
456 // no template, start with empty buffer
457 b->paragraph = new LyXParagraph;
459 } else { // start with empty buffer
460 b->paragraph = new LyXParagraph;
463 if (!lyxrc.new_ask_filename) {
467 b->setReadonly(false);
473 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
475 // make sure our path is absolute
476 string const s = MakeAbsPath(filename);
478 // file already open?
480 if (AskQuestion(_("Document is already open:"),
481 MakeDisplayPath(s, 50),
482 _("Do you want to reload that document?"))) {
483 // Reload is accomplished by closing and then loading
484 if (!close(getBuffer(s))) {
487 // Fall through to new load. (Asger)
489 // Here, we pretend that we just loaded the
496 switch (IsFileWriteable(s)) {
499 current_view->owner()->getMiniBuffer()->
500 Set(_("File `") + MakeDisplayPath(s, 50) +
501 _("' is read-only."));
508 b->lyxvc.file_found_hook(s);
510 break; //fine- it's r/w
512 // Here we probably should run
513 if (LyXVC::file_not_found_hook(s)) {
514 // Ask if the file should be checked out for
515 // viewing/editing, if so: load it.
516 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
517 // How can we know _how_ to do the checkout?
518 // With the current VC support it has to be,
519 // a RCS file since CVS do not have special ,v files.
521 return loadLyXFile(filename, tolastfiles);
524 if (AskQuestion(_("Cannot open specified file:"),
525 MakeDisplayPath(s, 50),
526 _("Create new document with this name?")))
528 // Find a free buffer
529 b = newFile(s, string(), true);
534 if (b && tolastfiles)
535 lastfiles->newFile(b->fileName());