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(BufferView *, int, string const &);
40 extern BufferView * current_view; // called too many times in this file...
45 // Class BufferStorage
48 void BufferStorage::release(Buffer * buf)
50 Container::iterator it = find(container.begin(), container.end(), buf);
51 if (it != container.end()) {
52 // Make sure that we don't store a LyXText in
53 // the textcache that points to the buffer
57 textcache.removeAllWithBuffer(tmp);
63 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
65 Buffer * tmpbuf = new Buffer(s, ronly);
66 tmpbuf->params.useClassDefaults();
67 lyxerr.debug() << "Assigning to buffer "
68 << container.size() << endl;
69 container.push_back(tmpbuf);
78 BufferList::BufferList()
79 : state_(BufferList::OK)
83 bool BufferList::empty() const
85 return bstore.empty();
89 extern void MenuWrite(Buffer *);
91 bool BufferList::QwriteAll()
93 bool askMoreConfirmation = false;
95 for(BufferStorage::iterator it = bstore.begin();
96 it != bstore.end(); ++it) {
97 if (!(*it)->isLyxClean()) {
98 switch(AskConfirmation(_("Changes in document:"),
99 MakeDisplayPath((*it)->fileName(),
101 _("Save document?"))) {
106 askMoreConfirmation = true;
107 unsaved += MakeDisplayPath((*it)->fileName(),
116 if (askMoreConfirmation &&
117 lyxrc.exit_confirmation &&
118 !AskQuestion(_("Some documents were not saved:"),
119 unsaved, _("Exit anyway?"))) {
127 void BufferList::closeAll()
129 state_ = BufferList::CLOSING;
130 // Since we are closing we can just as well delete all
131 // in the textcache this will also speed the closing/quiting up a bit.
134 while (!bstore.empty()) {
135 close(bstore.front());
137 state_ = BufferList::OK;
141 void BufferList::resize()
143 for(BufferStorage::iterator it = bstore.begin();
144 it != bstore.end(); ++it) {
150 bool BufferList::close(Buffer * buf)
152 if (buf->getUser()) buf->getUser()->insetUnlock();
154 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
156 switch(AskConfirmation(_("Changes in document:"),
157 MakeDisplayPath(buf->fileName(), 50),
158 _("Save document?"))){
160 if (buf->save(lyxrc.make_backup)) {
161 lastfiles->newFile(buf->fileName());
179 vector<string> BufferList::getFileNames() const
182 for(BufferStorage::const_iterator cit = bstore.begin();
183 cit != bstore.end(); ++cit) {
184 nvec.push_back((*cit)->fileName());
190 Buffer * BufferList::first()
192 if (bstore.empty()) return 0;
193 return bstore.front();
197 Buffer * BufferList::getBuffer(int choice)
199 if (choice >= bstore.size()) return 0;
200 return bstore[choice];
204 int BufferList::unlockInset(UpdatableInset * inset)
206 if (!inset) return 1;
207 for(BufferStorage::iterator it = bstore.begin();
208 it != bstore.end(); ++it) {
210 && (*it)->getUser()->the_locking_inset == inset) {
211 (*it)->getUser()->insetUnlock();
219 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
221 for(BufferStorage::iterator it = bstore.begin();
222 it != bstore.end(); ++it) {
223 if (!(*it)->isDepClean(mastertmpdir)) {
224 string writefile = mastertmpdir;
226 writefile += ChangeExtension((*it)->fileName(),
228 (*it)->makeLaTeXFile(writefile, mastertmpdir,
230 (*it)->markDepClean(mastertmpdir);
236 void BufferList::emergencyWriteAll()
238 for (BufferStorage::iterator it = bstore.begin();
239 it != bstore.end(); ++it) {
240 if (!(*it)->isLyxClean()) {
243 lyxerr <<_("lyx: Attempting to save"
246 << _(" as...") << endl;
248 for (int i = 0; i < 3 && !madeit; ++i) {
251 // We try to save three places:
252 // 1) Same place as document.
253 // 2) In HOME directory.
254 // 3) In "/tmp" directory.
256 s = (*it)->fileName();
258 s = AddName(GetEnvPath("HOME"),
261 // MakeAbsPath to prepend the current
262 // drive letter on OS/2
263 s = AddName(MakeAbsPath("/tmp/"),
268 lyxerr << " " << i + 1 << ") " << s << endl;
270 if ((*it)->writeFile(s, true)) {
271 (*it)->markLyxClean();
272 lyxerr << _(" Save seems successful. "
276 lyxerr << _(" Save failed! Trying...")
279 lyxerr << _(" Save failed! Bummer. "
289 Buffer * BufferList::readFile(string const & s, bool ronly)
291 Buffer * b = bstore.newBuffer(s, ronly);
294 string e = OnlyPath(s);
296 // File information about normal file
297 FileInfo fileInfo2(s);
299 // Check if emergency save file exists and is newer.
300 e += OnlyFilename(s) + ".emergency";
301 FileInfo fileInfoE(e);
303 bool use_emergency = false;
305 if (fileInfoE.exist() && fileInfo2.exist()) {
306 if (fileInfoE.getModificationTime()
307 > fileInfo2.getModificationTime()) {
308 if (AskQuestion(_("An emergency save of this document exists!"),
309 MakeDisplayPath(s, 50),
310 _("Try to load that instead?"))) {
312 // the file is not saved if we load the
315 use_emergency = true;
317 // Here, we should delete the emergency save
323 if (!use_emergency) {
324 // Now check if autosave file is newer.
326 a += OnlyFilename(s);
328 FileInfo fileInfoA(a);
329 if (fileInfoA.exist() && fileInfo2.exist()) {
330 if (fileInfoA.getModificationTime()
331 > fileInfo2.getModificationTime()) {
332 if (AskQuestion(_("Autosave file is newer."),
333 MakeDisplayPath(s, 50),
334 _("Load that one instead?"))) {
336 // the file is not saved if we load the
340 // Here, we should delete the autosave
346 // not sure if this is the correct place to begin LyXLex
349 if (b->readFile(lex))
358 bool BufferList::exists(string const & s) const
360 for (BufferStorage::const_iterator cit = bstore.begin();
361 cit != bstore.end(); ++cit) {
362 if ((*cit)->fileName() == s)
369 bool BufferList::isLoaded(Buffer const * b) const
371 BufferStorage::const_iterator cit =
372 find(bstore.begin(), bstore.end(), b);
373 return cit != bstore.end();
377 Buffer * BufferList::getBuffer(string const & s)
379 for(BufferStorage::iterator it = bstore.begin();
380 it != bstore.end(); ++it) {
381 if ((*it)->fileName() == s)
388 Buffer * BufferList::newFile(string const & name, string tname)
391 Buffer * b = bstore.newBuffer(name);
393 // use defaults.lyx as a default template if it exists.
395 tname = LibFileSearch("templates", "defaults.lyx");
397 if (!tname.empty() && IsLyXFilename(tname)) {
398 bool templateok = false;
402 if (b->readFile(lex)) {
407 WriteAlert(_("Error!"), _("Unable to open template"),
408 MakeDisplayPath(tname));
409 // no template, start with empty buffer
410 b->paragraph = new LyXParagraph;
413 else { // start with empty buffer
414 b->paragraph = new LyXParagraph;
418 b->setReadonly(false);
424 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
426 // make sure our path is absolute
427 string s = MakeAbsPath(filename);
429 // Is this done too early?
431 if (IsSGMLFilename(s)) {
433 if (fi.exist() && fi.readable()) {
434 if (!RunLinuxDoc(current_view, -1, s)) {
435 s = ChangeExtension (s, ".lyx", false);
436 } else { // sgml2lyx failed
437 WriteAlert(_("Error!"),
438 _("Could not convert file"), s);
442 // just change the extension and it will be
443 // handled like a regular lyx file that does
445 s = ChangeExtension(s, ".lyx", false);
449 // file already open?
451 if (AskQuestion(_("Document is already open:"),
452 MakeDisplayPath(s, 50),
453 _("Do you want to reload that document?"))) {
454 // Reload is accomplished by closing and then loading
455 if (!close(getBuffer(s))) {
458 // Fall through to new load. (Asger)
460 // Here, we pretend that we just loaded the
467 switch (IsFileWriteable(s)) {
470 current_view->owner()->getMiniBuffer()->
471 Set(_("File `") + MakeDisplayPath(s, 50) +
472 _("' is read-only."));
479 b->lyxvc.file_found_hook(s);
481 break; //fine- it's r/w
483 // Here we probably should run
484 if (LyXVC::file_not_found_hook(s)) {
485 // Ask if the file should be checked out for
486 // viewing/editing, if so: load it.
487 if (AskQuestion(_("Do you want to retrive file under version control?"))) {
488 // How can we know _how_ to do the checkout?
489 // With the current VC support it has to be,
490 // a RCS file since CVS do not have special ,v files.
492 return loadLyXFile(filename, tolastfiles);
495 if (AskQuestion(_("Cannot open specified file:"),
496 MakeDisplayPath(s, 50),
497 _("Create new document with this name?")))
499 // Find a free buffer
500 b = newFile(s, string());
505 if (b && tolastfiles)
506 lastfiles->newFile(b->fileName());