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?"))){
161 if (buf->save(lyxrc->make_backup)) {
162 lastfiles->newFile(buf->fileName());
180 vector<string> BufferList::getFileNames() const
183 for(BufferStorage::const_iterator cit = bstore.begin();
184 cit != bstore.end(); ++cit) {
185 nvec.push_back((*cit)->fileName());
191 Buffer * BufferList::first()
193 if (bstore.empty()) return 0;
194 return bstore.front();
198 Buffer * BufferList::getBuffer(int choice)
200 if (choice >= bstore.size()) return 0;
201 return bstore[choice];
205 int BufferList::unlockInset(UpdatableInset * inset)
207 if (!inset) return 1;
208 for(BufferStorage::iterator it = bstore.begin();
209 it != bstore.end(); ++it) {
211 && (*it)->getUser()->the_locking_inset == inset) {
212 (*it)->getUser()->insetUnlock();
220 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
222 for(BufferStorage::iterator it = bstore.begin();
223 it != bstore.end(); ++it) {
224 if (!(*it)->isDepClean(mastertmpdir)) {
225 string writefile = mastertmpdir;
227 writefile += ChangeExtension((*it)->fileName(),
229 (*it)->makeLaTeXFile(writefile, mastertmpdir,
231 (*it)->markDepClean(mastertmpdir);
237 void BufferList::emergencyWriteAll()
239 for (BufferStorage::iterator it = bstore.begin();
240 it != bstore.end(); ++it) {
241 if (!(*it)->isLyxClean()) {
244 lyxerr <<_("lyx: Attempting to save"
247 << _(" as...") << endl;
249 for (int i = 0; i < 3 && !madeit; ++i) {
252 // We try to save three places:
253 // 1) Same place as document.
254 // 2) In HOME directory.
255 // 3) In "/tmp" directory.
257 s = (*it)->fileName();
259 s = AddName(GetEnvPath("HOME"),
262 // MakeAbsPath to prepend the current
263 // drive letter on OS/2
264 s = AddName(MakeAbsPath("/tmp/"),
269 lyxerr << " " << i + 1 << ") " << s << endl;
271 if ((*it)->writeFile(s, true)) {
272 (*it)->markLyxClean();
273 lyxerr << _(" Save seems successful. "
277 lyxerr << _(" Save failed! Trying...")
280 lyxerr << _(" Save failed! Bummer. "
290 Buffer * BufferList::readFile(string const & s, bool ronly)
292 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
295 string e = OnlyPath(s);
297 // File information about normal file
298 FileInfo fileInfo2(s);
300 // Check if emergency save file exists and is newer.
301 e += OnlyFilename(s) + ".emergency";
302 FileInfo fileInfoE(e);
304 bool use_emergency = false;
306 if (fileInfoE.exist() && fileInfo2.exist()) {
307 if (fileInfoE.getModificationTime()
308 > fileInfo2.getModificationTime()) {
309 if (AskQuestion(_("An emergency save of this document exists!"),
310 MakeDisplayPath(s, 50),
311 _("Try to load that instead?"))) {
313 // the file is not saved if we load the
316 use_emergency = true;
318 // Here, we should delete the emergency save
324 if (!use_emergency) {
325 // Now check if autosave file is newer.
327 a += OnlyFilename(s);
329 FileInfo fileInfoA(a);
330 if (fileInfoA.exist() && fileInfo2.exist()) {
331 if (fileInfoA.getModificationTime()
332 > fileInfo2.getModificationTime()) {
333 if (AskQuestion(_("Autosave file is newer."),
334 MakeDisplayPath(s, 50),
335 _("Load that one instead?"))) {
337 // the file is not saved if we load the
341 // Here, we should delete the autosave
347 // not sure if this is the correct place to begin LyXLex
350 if (b->readFile(lex))
359 bool BufferList::exists(string const & s) const
361 for (BufferStorage::const_iterator cit = bstore.begin();
362 cit != bstore.end(); ++cit) {
363 if ((*cit)->fileName() == s)
370 bool BufferList::isLoaded(Buffer const * b) const
372 BufferStorage::const_iterator cit =
373 find(bstore.begin(), bstore.end(), b);
374 return cit != bstore.end();
378 Buffer * BufferList::getBuffer(string const & s)
380 for(BufferStorage::iterator it = bstore.begin();
381 it != bstore.end(); ++it) {
382 if ((*it)->fileName() == s)
389 Buffer * BufferList::newFile(string const & name, string tname)
392 Buffer * b = bstore.newBuffer(name, lyxrc);
394 // use defaults.lyx as a default template if it exists.
396 tname = LibFileSearch("templates", "defaults.lyx");
398 if (!tname.empty() && IsLyXFilename(tname)) {
399 bool templateok = false;
403 if (b->readFile(lex)) {
408 WriteAlert(_("Error!"), _("Unable to open template"),
409 MakeDisplayPath(tname));
410 // no template, start with empty buffer
411 b->paragraph = new LyXParagraph;
414 else { // start with empty buffer
415 b->paragraph = new LyXParagraph;
419 b->setReadonly(false);
425 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
427 // make sure our path is absolute
428 string s = MakeAbsPath(filename);
430 // Is this done too early?
432 if (IsSGMLFilename(s)) {
434 if (fi.exist() && fi.readable()) {
435 if (!RunLinuxDoc(-1, s)) {
436 s = ChangeExtension (s, ".lyx", false);
437 } else { // sgml2lyx failed
438 WriteAlert(_("Error!"),
439 _("Could not convert file"), s);
443 // just change the extension and it will be
444 // handled like a regular lyx file that does
446 s = ChangeExtension(s, ".lyx", false);
450 // file already open?
452 if (AskQuestion(_("Document is already open:"),
453 MakeDisplayPath(s, 50),
454 _("Do you want to reload that document?"))) {
455 // Reload is accomplished by closing and then loading
456 if (!close(getBuffer(s))) {
459 // Fall through to new load. (Asger)
461 // Here, we pretend that we just loaded the
468 switch (IsFileWriteable(s)) {
471 current_view->owner()->getMiniBuffer()->
472 Set(_("File `") + MakeDisplayPath(s, 50) +
473 _("' is read-only."));
480 b->lyxvc.file_found_hook(s);
482 break; //fine- it's r/w
484 // Here we probably should run
485 if (LyXVC::file_not_found_hook(s)) {
486 // Ask if the file should be checked out for
487 // viewing/editing, if so: load it.
488 if (AskQuestion(_("Do you want to retrive file under version control?"))) {
489 // How can we know _how_ to do the checkout?
490 // With the current VC support it has to be,
491 // a RCS file since CVS do not have special ,v files.
493 return loadLyXFile(filename, tolastfiles);
496 if (AskQuestion(_("Cannot open specified file:"),
497 MakeDisplayPath(s, 50),
498 _("Create new document with this name?")))
500 // Find a free buffer
501 b = newFile(s, string());
506 if (b && tolastfiles)
507 lastfiles->newFile(b->fileName());