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
24 #include <sys/types.h>
27 #include "bufferlist.h"
29 #include "minibuffer.h"
30 #include "support/FileInfo.h"
31 #include "support/filetools.h"
32 #include "lyx_gui_misc.h"
33 #include "lastfiles.h"
40 #include "vc-backend.h"
41 #include "TextCache.h"
43 extern BufferView * current_view;
44 extern int RunLinuxDoc(int, string const &);
52 // Class BufferStorage
55 void BufferStorage::release(Buffer * buf)
57 Container::iterator it = find(container.begin(), container.end(), buf);
58 if (it != container.end()) {
59 // Make sure that we don't store a LyXText in
60 // the textcache that points to the buffer
64 textcache.removeAllWithBuffer(tmp);
70 Buffer * BufferStorage::newBuffer(string const & s,
74 Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
75 tmpbuf->params.useClassDefaults();
76 lyxerr.debug() << "Assigning to buffer "
77 << container.size() << endl;
78 container.push_back(tmpbuf);
87 BufferList::BufferList()
88 : state_(BufferList::OK)
92 bool BufferList::empty() const
94 return bstore.empty();
98 extern void MenuWrite(Buffer *);
100 bool BufferList::QwriteAll()
102 bool askMoreConfirmation = false;
104 for(BufferStorage::iterator it = bstore.begin();
105 it != bstore.end(); ++it) {
106 if (!(*it)->isLyxClean()) {
107 switch(AskConfirmation(_("Changes in document:"),
108 MakeDisplayPath((*it)->fileName(),
110 _("Save document?"))) {
115 askMoreConfirmation = true;
116 unsaved += MakeDisplayPath((*it)->fileName(),
125 if (askMoreConfirmation &&
126 lyxrc->exit_confirmation &&
127 !AskQuestion(_("Some documents were not saved:"),
128 unsaved, _("Exit anyway?"))) {
136 // Should probably be moved to somewhere else: BufferView? LyXView?
137 bool BufferList::write(Buffer * buf, bool makeBackup)
143 ->Set(_("Saving document"),
144 MakeDisplayPath(buf->fileName()), "...");
146 // We don't need autosaves in the immediate future. (Asger)
147 buf->resetAutosaveTimers();
151 string s = buf->fileName() + '~';
152 // Rename is the wrong way of making a backup,
153 // this is the correct way.
154 /* truss cp fil fil2:
155 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
156 stat("LyXVC.lyx", 0xEFFFF688) = 0
157 open("LyXVC.lyx", O_RDONLY) = 3
158 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
159 fstat(4, 0xEFFFF508) = 0
160 fstat(3, 0xEFFFF508) = 0
161 read(3, " # T h i s f i l e w".., 8192) = 5579
162 write(4, " # T h i s f i l e w".., 5579) = 5579
163 read(3, 0xEFFFD4A0, 8192) = 0
166 chmod("LyXVC3.lyx", 0100644) = 0
167 lseek(0, 0, SEEK_CUR) = 46440
171 // Should proabaly have some more error checking here.
172 // Should be cleaned up in 0.13, at least a bit.
173 // Doing it this way, also makes the inodes stay the same.
174 // This is still not a very good solution, in particular we
175 // might loose the owner of the backup.
176 FileInfo finfo(buf->fileName());
178 mode_t fmode = finfo.getMode();
179 struct utimbuf * times = new struct utimbuf;
181 times->actime = finfo.getAccessTime();
182 times->modtime = finfo.getModificationTime();
183 ifstream ifs(buf->fileName().c_str());
184 ofstream ofs(s.c_str(), ios::out|ios::trunc);
189 ::chmod(s.c_str(), fmode);
191 if (::utime(s.c_str(), times)) {
192 lyxerr << "utime error." << endl;
195 lyxerr << "LyX was not able to make "
196 "backupcopy. Beware." << endl;
202 if (buf->writeFile(buf->fileName(), false)) {
205 current_view->owner()->getMiniBuffer()->
206 Set(_("Document saved as"),
207 MakeDisplayPath(buf->fileName()));
209 // now delete the autosavefile
210 string a = OnlyPath(buf->fileName());
212 a += OnlyFilename(buf->fileName());
214 FileInfo fileinfo(a);
215 if (fileinfo.exist()) {
216 if (::remove(a.c_str()) != 0) {
217 WriteFSAlert(_("Could not delete "
218 "auto-save file!"), a);
222 // Saving failed, so backup is not backup
224 string s = buf->fileName() + '~';
225 ::rename(s.c_str(), buf->fileName().c_str());
227 current_view->owner()->getMiniBuffer()->Set(_("Save failed!"));
235 void BufferList::closeAll()
237 state_ = BufferList::CLOSING;
238 // Since we are closing we can just as well delete all
239 // in the textcache this will also speed the closing/quiting up a bit.
242 while (!bstore.empty()) {
243 close(bstore.front());
245 state_ = BufferList::OK;
249 void BufferList::resize()
251 for(BufferStorage::iterator it = bstore.begin();
252 it != bstore.end(); ++it) {
258 bool BufferList::close(Buffer * buf)
260 if (buf->getUser()) buf->getUser()->insetUnlock();
262 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
264 switch(AskConfirmation(_("Changes in document:"),
265 MakeDisplayPath(buf->fileName(), 50),
266 _("Save document?"))){
268 if (write(buf, lyxrc->make_backup)) {
269 lastfiles->newFile(buf->fileName());
287 vector<string> BufferList::getFileNames() const
290 for(BufferStorage::const_iterator cit = bstore.begin();
291 cit != bstore.end(); ++cit) {
292 nvec.push_back((*cit)->fileName());
298 Buffer * BufferList::first()
300 if (bstore.empty()) return 0;
301 return bstore.front();
305 Buffer * BufferList::getBuffer(int choice)
307 if (choice >= bstore.size()) return 0;
308 return bstore[choice];
312 void BufferList::updateInset(Inset * inset, bool mark_dirty)
314 for (BufferStorage::iterator it = bstore.begin();
315 it != bstore.end(); ++it) {
317 && (*it)->getUser()->text->UpdateInset(inset)) {
326 int BufferList::unlockInset(UpdatableInset * inset)
328 if (!inset) return 1;
329 for(BufferStorage::iterator it = bstore.begin();
330 it != bstore.end(); ++it) {
332 && (*it)->getUser()->the_locking_inset == inset) {
333 (*it)->getUser()->insetUnlock();
341 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
343 for(BufferStorage::iterator it = bstore.begin();
344 it != bstore.end(); ++it) {
345 if (!(*it)->isDepClean(mastertmpdir)) {
346 string writefile = mastertmpdir;
348 writefile += ChangeExtension((*it)->fileName(),
350 (*it)->makeLaTeXFile(writefile, mastertmpdir,
352 (*it)->markDepClean(mastertmpdir);
358 void BufferList::emergencyWriteAll()
360 for (BufferStorage::iterator it = bstore.begin();
361 it != bstore.end(); ++it) {
362 if (!(*it)->isLyxClean()) {
365 lyxerr <<_("lyx: Attempting to save"
368 << _(" as...") << endl;
370 for (int i = 0; i < 3 && !madeit; ++i) {
373 // We try to save three places:
374 // 1) Same place as document.
375 // 2) In HOME directory.
376 // 3) In "/tmp" directory.
378 s = (*it)->fileName();
380 s = AddName(GetEnvPath("HOME"),
383 // MakeAbsPath to prepend the current
384 // drive letter on OS/2
385 s = AddName(MakeAbsPath("/tmp/"),
390 lyxerr << " " << i + 1 << ") " << s << endl;
392 if ((*it)->writeFile(s, true)) {
393 (*it)->markLyxClean();
394 lyxerr << _(" Save seems successful. "
398 lyxerr << _(" Save failed! Trying...")
401 lyxerr << _(" Save failed! Bummer. "
411 Buffer * BufferList::readFile(string const & s, bool ronly)
413 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
416 string e = OnlyPath(s);
418 // File information about normal file
419 FileInfo fileInfo2(s);
421 // Check if emergency save file exists and is newer.
422 e += OnlyFilename(s) + ".emergency";
423 FileInfo fileInfoE(e);
425 bool use_emergency = false;
427 if (fileInfoE.exist() && fileInfo2.exist()) {
428 if (fileInfoE.getModificationTime()
429 > fileInfo2.getModificationTime()) {
430 if (AskQuestion(_("An emergency save of this document exists!"),
431 MakeDisplayPath(s, 50),
432 _("Try to load that instead?"))) {
434 // the file is not saved if we load the
437 use_emergency = true;
439 // Here, we should delete the emergency save
445 if (!use_emergency) {
446 // Now check if autosave file is newer.
448 a += OnlyFilename(s);
450 FileInfo fileInfoA(a);
451 if (fileInfoA.exist() && fileInfo2.exist()) {
452 if (fileInfoA.getModificationTime()
453 > fileInfo2.getModificationTime()) {
454 if (AskQuestion(_("Autosave file is newer."),
455 MakeDisplayPath(s, 50),
456 _("Load that one instead?"))) {
458 // the file is not saved if we load the
462 // Here, we should delete the autosave
468 // not sure if this is the correct place to begin LyXLex
471 if (b->readFile(lex))
480 bool BufferList::exists(string const & s) const
482 for (BufferStorage::const_iterator cit = bstore.begin();
483 cit != bstore.end(); ++cit) {
484 if ((*cit)->fileName() == s)
491 bool BufferList::isLoaded(Buffer const * b) const
493 BufferStorage::const_iterator cit =
494 find(bstore.begin(), bstore.end(), b);
495 return cit != bstore.end();
499 Buffer * BufferList::getBuffer(string const & s)
501 for(BufferStorage::iterator it = bstore.begin();
502 it != bstore.end(); ++it) {
503 if ((*it)->fileName() == s)
510 Buffer * BufferList::newFile(string const & name, string tname)
513 Buffer * b = bstore.newBuffer(name, lyxrc);
515 // use defaults.lyx as a default template if it exists.
517 tname = LibFileSearch("templates", "defaults.lyx");
519 if (!tname.empty() && IsLyXFilename(tname)) {
520 bool templateok = false;
524 if (b->readFile(lex)) {
529 WriteAlert(_("Error!"), _("Unable to open template"),
530 MakeDisplayPath(tname));
531 // no template, start with empty buffer
532 b->paragraph = new LyXParagraph;
535 else { // start with empty buffer
536 b->paragraph = new LyXParagraph;
540 b->setReadonly(false);
546 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
548 // make sure our path is absolute
549 string s = MakeAbsPath(filename);
551 // Is this done too early?
553 if (IsSGMLFilename(s)) {
555 if (fi.exist() && fi.readable()) {
556 if (!RunLinuxDoc(-1, s)) {
557 s = ChangeExtension (s, ".lyx", false);
558 } else { // sgml2lyx failed
559 WriteAlert(_("Error!"),
560 _("Could not convert file"), s);
564 // just change the extension and it will be
565 // handled like a regular lyx file that does
567 s = ChangeExtension(s, ".lyx", false);
571 // file already open?
573 if (AskQuestion(_("Document is already open:"),
574 MakeDisplayPath(s, 50),
575 _("Do you want to reload that document?"))) {
576 // Reload is accomplished by closing and then loading
577 if (!close(getBuffer(s))) {
580 // Fall through to new load. (Asger)
582 // Here, we pretend that we just loaded the
589 switch (IsFileWriteable(s)) {
591 current_view->owner()->getMiniBuffer()->
592 Set(_("File `") + MakeDisplayPath(s, 50) +
593 _("' is read-only."));
599 b->lyxvc.file_found_hook(s);
601 break; //fine- it's r/w
603 // Here we probably should run
604 if (LyXVC::file_not_found_hook(s)) {
605 // Ask if the file should be checked out for
606 // viewing/editing, if so: load it.
607 if (AskQuestion(_("Do you want to retrive file under version control?"))) {
608 // How can we know _how_ to do the checkout?
609 // With the current VC support it has to be,
610 // a RCS file since CVS do not have special ,v files.
612 return loadLyXFile(filename, tolastfiles);
615 if (AskQuestion(_("Cannot open specified file:"),
616 MakeDisplayPath(s, 50),
617 _("Create new document with this name?")))
619 // Find a free buffer
620 b = newFile(s, string());
625 if (b && tolastfiles)
626 lastfiles->newFile(b->fileName());