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"
36 #include "lyxscreen.h"
41 #include "vc-backend.h"
43 extern BufferView * current_view;
44 extern int RunLinuxDoc(int, string const &);
51 // Class BufferStorage
54 void BufferStorage::release(Buffer * buf)
56 Container::iterator it = find(container.begin(), container.end(), buf);
57 if (it != container.end()) {
65 Buffer * BufferStorage::newBuffer(string const & s,
69 Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
70 tmpbuf->params.useClassDefaults();
71 lyxerr.debug() << "Assigning to buffer "
72 << container.size() << endl;
73 container.push_back(tmpbuf);
82 BufferList::BufferList()
83 : state_(BufferList::OK)
87 bool BufferList::empty() const
89 return bstore.empty();
93 extern void MenuWrite(Buffer *);
95 bool BufferList::QwriteAll()
97 bool askMoreConfirmation = false;
99 for(BufferStorage::iterator it = bstore.begin();
100 it != bstore.end(); ++it) {
101 if (!(*it)->isLyxClean()) {
102 switch(AskConfirmation(_("Changes in document:"),
103 MakeDisplayPath((*it)->fileName(),
105 _("Save document?"))) {
110 askMoreConfirmation = true;
111 unsaved += MakeDisplayPath((*it)->fileName(),
120 if (askMoreConfirmation &&
121 lyxrc->exit_confirmation &&
122 !AskQuestion(_("Some documents were not saved:"),
123 unsaved, _("Exit anyway?"))) {
131 // Should probably be moved to somewhere else: BufferView? LyXView?
132 bool BufferList::write(Buffer * buf, bool makeBackup)
138 ->Set(_("Saving document"),
139 MakeDisplayPath(buf->fileName()), "...");
141 // We don't need autosaves in the immediate future. (Asger)
142 buf->resetAutosaveTimers();
146 string s = buf->fileName() + '~';
147 // Rename is the wrong way of making a backup,
148 // this is the correct way.
149 /* truss cp fil fil2:
150 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
151 stat("LyXVC.lyx", 0xEFFFF688) = 0
152 open("LyXVC.lyx", O_RDONLY) = 3
153 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
154 fstat(4, 0xEFFFF508) = 0
155 fstat(3, 0xEFFFF508) = 0
156 read(3, " # T h i s f i l e w".., 8192) = 5579
157 write(4, " # T h i s f i l e w".., 5579) = 5579
158 read(3, 0xEFFFD4A0, 8192) = 0
161 chmod("LyXVC3.lyx", 0100644) = 0
162 lseek(0, 0, SEEK_CUR) = 46440
166 // Should proabaly have some more error checking here.
167 // Should be cleaned up in 0.13, at least a bit.
168 // Doing it this way, also makes the inodes stay the same.
169 // This is still not a very good solution, in particular we
170 // might loose the owner of the backup.
171 FileInfo finfo(buf->fileName());
173 mode_t fmode = finfo.getMode();
174 struct utimbuf * times = new struct utimbuf;
176 times->actime = finfo.getAccessTime();
177 times->modtime = finfo.getModificationTime();
178 ifstream ifs(buf->fileName().c_str());
179 ofstream ofs(s.c_str(), ios::out|ios::trunc);
191 chmod(s.c_str(), fmode);
193 if (utime(s.c_str(), times)) {
194 lyxerr << "utime error." << endl;
197 lyxerr << "LyX was not able to make "
198 "backupcopy. Beware." << endl;
204 if (buf->writeFile(buf->fileName(), false)) {
207 current_view->owner()->getMiniBuffer()->
208 Set(_("Document saved as"),
209 MakeDisplayPath(buf->fileName()));
211 // now delete the autosavefile
212 string a = OnlyPath(buf->fileName());
214 a += OnlyFilename(buf->fileName());
216 FileInfo fileinfo(a);
217 if (fileinfo.exist()) {
218 if (remove(a.c_str()) != 0) {
219 WriteFSAlert(_("Could not delete "
220 "auto-save file!"), a);
224 // Saving failed, so backup is not backup
226 string s = buf->fileName() + '~';
227 rename(s.c_str(), buf->fileName().c_str());
229 current_view->owner()->getMiniBuffer()->Set(_("Save failed!"));
237 void BufferList::closeAll()
239 state_ = BufferList::CLOSING;
240 while (!bstore.empty()) {
241 close(bstore.front());
243 state_ = BufferList::OK;
247 void BufferList::resize()
249 for(BufferStorage::iterator it = bstore.begin();
250 it != bstore.end(); ++it) {
256 bool BufferList::close(Buffer * buf)
258 if (buf->getUser()) buf->getUser()->insetUnlock();
260 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
262 switch(AskConfirmation(_("Changes in document:"),
263 MakeDisplayPath(buf->fileName(), 50),
264 _("Save document?"))){
266 if (write(buf, lyxrc->make_backup)) {
267 lastfiles->newFile(buf->fileName());
285 vector<string> BufferList::getFileNames() const
288 for(BufferStorage::const_iterator cit = bstore.begin();
289 cit != bstore.end(); ++cit) {
290 nvec.push_back((*cit)->fileName());
296 Buffer * BufferList::first()
298 if (bstore.empty()) return 0;
299 return bstore.front();
303 Buffer * BufferList::getBuffer(int choice)
305 if (choice >= bstore.size()) return 0;
306 return bstore[choice];
310 void BufferList::updateInset(Inset * inset, bool mark_dirty)
312 for (BufferStorage::iterator it = bstore.begin();
313 it != bstore.end(); ++it) {
315 && (*it)->getUser()->text->UpdateInset(inset)) {
324 int BufferList::unlockInset(UpdatableInset * inset)
326 if (!inset) return 1;
327 for(BufferStorage::iterator it = bstore.begin();
328 it != bstore.end(); ++it) {
330 && (*it)->getUser()->the_locking_inset == inset) {
331 (*it)->getUser()->insetUnlock();
339 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
341 for(BufferStorage::iterator it = bstore.begin();
342 it != bstore.end(); ++it) {
343 if (!(*it)->isDepClean(mastertmpdir)) {
344 string writefile = mastertmpdir;
346 writefile += ChangeExtension((*it)->fileName(),
348 (*it)->makeLaTeXFile(writefile, mastertmpdir,
350 (*it)->markDepClean(mastertmpdir);
356 void BufferList::emergencyWriteAll()
358 for (BufferStorage::iterator it = bstore.begin();
359 it != bstore.end(); ++it) {
360 if (!(*it)->isLyxClean()) {
363 lyxerr <<_("lyx: Attempting to save"
366 << _(" as...") << endl;
368 for (int i = 0; i < 3 && !madeit; ++i) {
371 // We try to save three places:
372 // 1) Same place as document.
373 // 2) In HOME directory.
374 // 3) In "/tmp" directory.
376 s = (*it)->fileName();
378 s = AddName(GetEnvPath("HOME"),
381 // MakeAbsPath to prepend the current
382 // drive letter on OS/2
383 s = AddName(MakeAbsPath("/tmp/"),
388 lyxerr << " " << i + 1 << ") " << s << endl;
390 if ((*it)->writeFile(s, true)) {
391 (*it)->markLyxClean();
392 lyxerr << _(" Save seems successful. "
396 lyxerr << _(" Save failed! Trying...")
399 lyxerr << _(" Save failed! Bummer. "
409 Buffer * BufferList::readFile(string const & s, bool ronly)
411 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
414 string e = OnlyPath(s);
416 // File information about normal file
417 FileInfo fileInfo2(s);
419 // Check if emergency save file exists and is newer.
420 e += OnlyFilename(s) + ".emergency";
421 FileInfo fileInfoE(e);
423 bool use_emergency = false;
425 if (fileInfoE.exist() && fileInfo2.exist()) {
426 if (fileInfoE.getModificationTime()
427 > fileInfo2.getModificationTime()) {
428 if (AskQuestion(_("An emergency save of this document exists!"),
429 MakeDisplayPath(s, 50),
430 _("Try to load that instead?"))) {
432 // the file is not saved if we load the
435 use_emergency = true;
437 // Here, we should delete the emergency save
443 if (!use_emergency) {
444 // Now check if autosave file is newer.
446 a += OnlyFilename(s);
448 FileInfo fileInfoA(a);
449 if (fileInfoA.exist() && fileInfo2.exist()) {
450 if (fileInfoA.getModificationTime()
451 > fileInfo2.getModificationTime()) {
452 if (AskQuestion(_("Autosave file is newer."),
453 MakeDisplayPath(s, 50),
454 _("Load that one instead?"))) {
456 // the file is not saved if we load the
460 // Here, we should delete the autosave
466 // not sure if this is the correct place to begin LyXLex
469 if (b->readFile(lex))
478 bool BufferList::exists(string const & s) const
480 for (BufferStorage::const_iterator cit = bstore.begin();
481 cit != bstore.end(); ++cit) {
482 if ((*cit)->fileName() == s)
489 bool BufferList::isLoaded(Buffer const * b) const
491 BufferStorage::const_iterator cit =
492 find(bstore.begin(), bstore.end(), b);
493 return cit != bstore.end();
497 Buffer * BufferList::getBuffer(string const & s)
499 for(BufferStorage::iterator it = bstore.begin();
500 it != bstore.end(); ++it) {
501 if ((*it)->fileName() == s)
508 Buffer * BufferList::newFile(string const & name, string tname)
510 /* get a free buffer */
511 Buffer * b = bstore.newBuffer(name, lyxrc);
513 // use defaults.lyx as a default template if it exists.
515 tname = LibFileSearch("templates", "defaults.lyx");
517 if (!tname.empty() && IsLyXFilename(tname)){
518 bool templateok = false;
522 if (b->readFile(lex)) {
527 WriteAlert(_("Error!"), _("Unable to open template"),
528 MakeDisplayPath(tname));
529 // no template, start with empty buffer
530 b->paragraph = new LyXParagraph;
533 else { // start with empty buffer
534 b->paragraph = new LyXParagraph;
538 b->setReadonly(false);
544 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
546 // make sure our path is absolute
547 string s = MakeAbsPath(filename);
549 // Is this done too early?
551 if (IsSGMLFilename(s)) {
553 if (fi.exist() && fi.readable()) {
554 if (!RunLinuxDoc(-1, s)) {
555 s = ChangeExtension (s, ".lyx", false);
556 } else { // sgml2lyx failed
557 WriteAlert(_("Error!"),
558 _("Could not convert file"), s);
562 // just change the extension and it will be
563 // handled like a regular lyx file that does
565 s = ChangeExtension(s, ".lyx", false);
569 // file already open?
571 if (AskQuestion(_("Document is already open:"),
572 MakeDisplayPath(s, 50),
573 _("Do you want to reload that document?"))) {
574 // Reload is accomplished by closing and then loading
575 if (!close(getBuffer(s))) {
578 // Fall through to new load. (Asger)
580 // Here, we pretend that we just loaded the
587 switch (IsFileWriteable(s)) {
589 current_view->owner()->getMiniBuffer()->
590 Set(_("File `")+MakeDisplayPath(s, 50)+
591 _("' is read-only."));
597 b->lyxvc.file_found_hook(s);
599 break; //fine- it's r/w
601 // Here we probably should run
602 if (LyXVC::file_not_found_hook(s)) {
603 // Ask if the file should be checked out for
604 // viewing/editing, if so: load it.
605 if (AskQuestion(_("Do you want to retrive file under version control?"))) {
606 // How can we know _how_ to do the checkout?
607 // With the current VC support it has to be,
608 // a RCS file since CVS do not have special ,v files.
610 return loadLyXFile(filename, tolastfiles);
613 if (AskQuestion(_("Cannot open specified file:"),
614 MakeDisplayPath(s, 50),
615 _("Create new document with this name?")))
617 // Find a free buffer
618 b = newFile(s, string());
623 if (b && tolastfiles)
624 lastfiles->newFile(b->fileName());