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"
36 #include "vc-backend.h"
37 #include "TextCache.h"
39 extern int RunLinuxDoc(int, string const &);
44 // Class BufferStorage
47 void BufferStorage::release(Buffer * buf)
49 Container::iterator it = find(container.begin(), container.end(), buf);
50 if (it != container.end()) {
51 // Make sure that we don't store a LyXText in
52 // the textcache that points to the buffer
56 textcache.removeAllWithBuffer(tmp);
62 Buffer * BufferStorage::newBuffer(string const & s,
66 Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
67 tmpbuf->params.useClassDefaults();
68 lyxerr.debug() << "Assigning to buffer "
69 << container.size() << endl;
70 container.push_back(tmpbuf);
79 BufferList::BufferList()
80 : state_(BufferList::OK)
84 bool BufferList::empty() const
86 return bstore.empty();
90 extern void MenuWrite(Buffer *);
92 bool BufferList::QwriteAll()
94 bool askMoreConfirmation = false;
96 for(BufferStorage::iterator it = bstore.begin();
97 it != bstore.end(); ++it) {
98 if (!(*it)->isLyxClean()) {
99 switch(AskConfirmation(_("Changes in document:"),
100 MakeDisplayPath((*it)->fileName(),
102 _("Save document?"))) {
107 askMoreConfirmation = true;
108 unsaved += MakeDisplayPath((*it)->fileName(),
117 if (askMoreConfirmation &&
118 lyxrc->exit_confirmation &&
119 !AskQuestion(_("Some documents were not saved:"),
120 unsaved, _("Exit anyway?"))) {
128 void BufferList::closeAll()
130 state_ = BufferList::CLOSING;
131 // Since we are closing we can just as well delete all
132 // in the textcache this will also speed the closing/quiting up a bit.
135 while (!bstore.empty()) {
136 close(bstore.front());
138 state_ = BufferList::OK;
142 void BufferList::resize()
144 for(BufferStorage::iterator it = bstore.begin();
145 it != bstore.end(); ++it) {
151 bool BufferList::close(Buffer * buf)
153 if (buf->getUser()) buf->getUser()->insetUnlock();
155 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
157 switch(AskConfirmation(_("Changes in document:"),
158 MakeDisplayPath(buf->fileName(), 50),
159 _("Save document?"))){
162 if (write(buf, lyxrc->make_backup)) {
163 lastfiles->newFile(buf->fileName());
169 if (buf->save(lyxrc->make_backup)) {
170 lastfiles->newFile(buf->fileName());
189 vector<string> BufferList::getFileNames() const
192 for(BufferStorage::const_iterator cit = bstore.begin();
193 cit != bstore.end(); ++cit) {
194 nvec.push_back((*cit)->fileName());
200 Buffer * BufferList::first()
202 if (bstore.empty()) return 0;
203 return bstore.front();
207 Buffer * BufferList::getBuffer(int choice)
209 if (choice >= bstore.size()) return 0;
210 return bstore[choice];
214 void BufferList::updateInset(Inset * inset, bool mark_dirty)
216 for (BufferStorage::iterator it = bstore.begin();
217 it != bstore.end(); ++it) {
219 && (*it)->getUser()->text->UpdateInset(inset)) {
228 int BufferList::unlockInset(UpdatableInset * inset)
230 if (!inset) return 1;
231 for(BufferStorage::iterator it = bstore.begin();
232 it != bstore.end(); ++it) {
234 && (*it)->getUser()->the_locking_inset == inset) {
235 (*it)->getUser()->insetUnlock();
243 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
245 for(BufferStorage::iterator it = bstore.begin();
246 it != bstore.end(); ++it) {
247 if (!(*it)->isDepClean(mastertmpdir)) {
248 string writefile = mastertmpdir;
250 writefile += ChangeExtension((*it)->fileName(),
252 (*it)->makeLaTeXFile(writefile, mastertmpdir,
254 (*it)->markDepClean(mastertmpdir);
260 void BufferList::emergencyWriteAll()
262 for (BufferStorage::iterator it = bstore.begin();
263 it != bstore.end(); ++it) {
264 if (!(*it)->isLyxClean()) {
267 lyxerr <<_("lyx: Attempting to save"
270 << _(" as...") << endl;
272 for (int i = 0; i < 3 && !madeit; ++i) {
275 // We try to save three places:
276 // 1) Same place as document.
277 // 2) In HOME directory.
278 // 3) In "/tmp" directory.
280 s = (*it)->fileName();
282 s = AddName(GetEnvPath("HOME"),
285 // MakeAbsPath to prepend the current
286 // drive letter on OS/2
287 s = AddName(MakeAbsPath("/tmp/"),
292 lyxerr << " " << i + 1 << ") " << s << endl;
294 if ((*it)->writeFile(s, true)) {
295 (*it)->markLyxClean();
296 lyxerr << _(" Save seems successful. "
300 lyxerr << _(" Save failed! Trying...")
303 lyxerr << _(" Save failed! Bummer. "
313 Buffer * BufferList::readFile(string const & s, bool ronly)
315 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
318 string e = OnlyPath(s);
320 // File information about normal file
321 FileInfo fileInfo2(s);
323 // Check if emergency save file exists and is newer.
324 e += OnlyFilename(s) + ".emergency";
325 FileInfo fileInfoE(e);
327 bool use_emergency = false;
329 if (fileInfoE.exist() && fileInfo2.exist()) {
330 if (fileInfoE.getModificationTime()
331 > fileInfo2.getModificationTime()) {
332 if (AskQuestion(_("An emergency save of this document exists!"),
333 MakeDisplayPath(s, 50),
334 _("Try to load that instead?"))) {
336 // the file is not saved if we load the
339 use_emergency = true;
341 // Here, we should delete the emergency save
347 if (!use_emergency) {
348 // Now check if autosave file is newer.
350 a += OnlyFilename(s);
352 FileInfo fileInfoA(a);
353 if (fileInfoA.exist() && fileInfo2.exist()) {
354 if (fileInfoA.getModificationTime()
355 > fileInfo2.getModificationTime()) {
356 if (AskQuestion(_("Autosave file is newer."),
357 MakeDisplayPath(s, 50),
358 _("Load that one instead?"))) {
360 // the file is not saved if we load the
364 // Here, we should delete the autosave
370 // not sure if this is the correct place to begin LyXLex
373 if (b->readFile(lex))
382 bool BufferList::exists(string const & s) const
384 for (BufferStorage::const_iterator cit = bstore.begin();
385 cit != bstore.end(); ++cit) {
386 if ((*cit)->fileName() == s)
393 bool BufferList::isLoaded(Buffer const * b) const
395 BufferStorage::const_iterator cit =
396 find(bstore.begin(), bstore.end(), b);
397 return cit != bstore.end();
401 Buffer * BufferList::getBuffer(string const & s)
403 for(BufferStorage::iterator it = bstore.begin();
404 it != bstore.end(); ++it) {
405 if ((*it)->fileName() == s)
412 Buffer * BufferList::newFile(string const & name, string tname)
415 Buffer * b = bstore.newBuffer(name, lyxrc);
417 // use defaults.lyx as a default template if it exists.
419 tname = LibFileSearch("templates", "defaults.lyx");
421 if (!tname.empty() && IsLyXFilename(tname)) {
422 bool templateok = false;
426 if (b->readFile(lex)) {
431 WriteAlert(_("Error!"), _("Unable to open template"),
432 MakeDisplayPath(tname));
433 // no template, start with empty buffer
434 b->paragraph = new LyXParagraph;
437 else { // start with empty buffer
438 b->paragraph = new LyXParagraph;
442 b->setReadonly(false);
448 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
450 // make sure our path is absolute
451 string s = MakeAbsPath(filename);
453 // Is this done too early?
455 if (IsSGMLFilename(s)) {
457 if (fi.exist() && fi.readable()) {
458 if (!RunLinuxDoc(-1, s)) {
459 s = ChangeExtension (s, ".lyx", false);
460 } else { // sgml2lyx failed
461 WriteAlert(_("Error!"),
462 _("Could not convert file"), s);
466 // just change the extension and it will be
467 // handled like a regular lyx file that does
469 s = ChangeExtension(s, ".lyx", false);
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 retrive 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());