1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Word Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1998 The LyX Team.
9 * This file is Copyleft (C) 1996-1998
12 *======================================================
16 #pragma implementation
20 #include <sys/types.h>
22 #include "bufferlist.h"
24 #include "minibuffer.h"
26 #include "filetools.h"
27 #include "lyx_gui_misc.h"
28 #include "lastfiles.h"
31 #include "lyxscreen.h"
36 // $Id: bufferlist.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $
38 #if !defined(lint) && !defined(WITH_WARNINGS)
39 static char vcid[] = "$Id: bufferlist.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $";
42 extern BufferView *current_view;
43 extern MiniBuffer *minibuffer;
44 extern void SmallUpdate(signed char);
45 extern void BeforeChange();
46 extern int RunLinuxDoc(int, LString const &);
49 // Class BufferStorage
52 BufferStorage::BufferStorage()
54 // Initialize the buffer array
55 for (int i=NUMBER_OF_BUFFERS-1; i >=0; i--) {
61 bool BufferStorage::isEmpty()
63 for (int i=NUMBER_OF_BUFFERS-1; i >=0; i--) {
64 if (buffer[i]) return false;
70 void BufferStorage::release(Buffer* buf)
73 for (i=0; i<NUMBER_OF_BUFFERS; i++)
74 if (buffer[i] == buf) break;
75 Buffer *tmpbuf = buffer[i];
81 Buffer* BufferStorage::newBuffer(LString const &s,
86 while (i < NUMBER_OF_BUFFERS - 1
88 buffer[i] = new Buffer(s, lyxrc, ronly);
89 buffer[i]->params.useClassDefaults();
90 lyxerr.debug(LString("Assigning to buffer ") + i, Error::ANY);
96 // Class BufferStrorage_Iter
99 Buffer* BufferStorage_Iter::operator() ()
102 for (i=index; i < BufferStorage::NUMBER_OF_BUFFERS; i++) {
105 return cs->buffer[i];
112 Buffer* BufferStorage_Iter::operator[] (int a)
119 while(!cs->buffer[i++]);
121 if (i-1 < BufferStorage::NUMBER_OF_BUFFERS)
122 return cs->buffer[i-1];
130 BufferList::BufferList()
132 _state = BufferList::OK;
136 BufferList::~BufferList()
138 // I am sure something should be done here too.
142 bool BufferList::isEmpty()
144 return bstore.isEmpty();
147 extern void MenuWrite(Buffer*);
149 bool BufferList::QwriteAll()
151 bool askMoreConfirmation = false;
153 BufferStorage_Iter biter(bstore);
155 while ((b=biter())) {
156 if (!b->isLyxClean()) {
157 switch(AskConfirmation(_("Changes in document:"),
158 MakeDisplayPath(b->filename,50),
159 _("Save document?"))) {
164 askMoreConfirmation = true;
165 unsaved += MakeDisplayPath(b->filename,50);
173 if (askMoreConfirmation &&
174 lyxrc->exit_confirmation &&
175 !AskQuestion(_("Some documents were not saved:"),
176 unsaved, _("Exit anyway?"))) {
184 // Should probably be moved to somewhere else: BufferView? LyXView?
185 bool BufferList::write(Buffer *buf, bool makeBackup)
187 minibuffer->Set(_("Saving document"),
188 MakeDisplayPath(buf->filename),"...");
190 // We don't need autosaves in the immediate future. (Asger)
191 buf->resetAutosaveTimers();
195 LString s = buf->filename + '~';
196 // Rename is the wrong way of making a backup,
197 // this is the correct way.
198 /* truss cp fil fil2:
199 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
200 stat("LyXVC.lyx", 0xEFFFF688) = 0
201 open("LyXVC.lyx", O_RDONLY) = 3
202 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
203 fstat(4, 0xEFFFF508) = 0
204 fstat(3, 0xEFFFF508) = 0
205 read(3, " # T h i s f i l e w".., 8192) = 5579
206 write(4, " # T h i s f i l e w".., 5579) = 5579
207 read(3, 0xEFFFD4A0, 8192) = 0
210 chmod("LyXVC3.lyx", 0100644) = 0
211 lseek(0, 0, SEEK_CUR) = 46440
215 // Should proabaly have some more error checking here.
216 // Should be cleaned up in 0.13, at least a bit.
217 // Doing it this way, also makes the inodes stay the same.
218 // This is still not a very good solution, in particular we
219 // might loose the owner of the backup.
220 FileInfo finfo(buf->filename);
222 mode_t fmode = finfo.getMode();
224 struct utimbuf *times =
225 (struct utimbuf*)new char[sizeof(struct utimbuf)];
226 times->actime = finfo.getAccessTime();
227 times->modtime = finfo.getModificationTime();
228 long blksize = finfo.getBlockSize();
229 lyxerr.debug(LString("BlockSize: ") + int(blksize));
230 FilePtr fin(buf->filename,FilePtr::read);
231 FilePtr fout(s,FilePtr::truncate);
232 if (fin() && fout()) {
233 char * cbuf = new char[blksize+1];
237 c_read = fread(cbuf, 1, blksize, fin);
240 fwrite(cbuf, 1, c_read, fout);
244 chmod(s.c_str(), fmode);
246 if (utime(s.c_str(), times)) {
247 lyxerr.print("utime error.");
251 lyxerr.print("LyX was not able to make backupcopy. Beware.");
257 if (buf->writeFile(buf->filename,false)) {
260 minibuffer->Set(_("Document saved as"),
261 MakeDisplayPath(buf->filename));
263 // now delete the autosavefile
264 LString a = OnlyPath(buf->filename);
266 a += OnlyFilename(buf->filename);
268 FileInfo fileinfo(a);
269 if (fileinfo.exist()) {
270 if (remove(a.c_str()) != 0) {
271 WriteFSAlert(_("Could not delete "
272 "auto-save file!"), a);
276 // Saving failed, so backup is not backup
278 LString s = buf->filename + '~';
279 rename(s.c_str(), buf->filename.c_str());
281 minibuffer->Set(_("Save failed!"));
289 void BufferList::closeAll()
291 _state = BufferList::CLOSING;
293 BufferStorage_Iter biter(bstore);
295 while ((b=biter())) {
298 _state = BufferList::OK;
302 void BufferList::resize()
304 BufferStorage_Iter biter(bstore);
306 while ((b=biter())) {
312 bool BufferList::close(Buffer *buf)
316 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
318 switch(AskConfirmation(_("Changes in document:"),
319 MakeDisplayPath(buf->filename,50),
320 _("Save document?"))){
323 lastfiles->newFile(buf->filename);
341 void BufferList::makePup(int pup)
342 /* This should be changed to return a char const *const
343 in the same way as for lastfiles.[hC]
347 BufferStorage_Iter biter(bstore);
349 while ((b=biter())) {
350 LString relbuf = MakeDisplayPath(b->filename,30);
351 fl_addtopup(pup, relbuf.c_str());
354 if (ant == 0) fl_addtopup(pup,_("No Documents Open!%t"));
358 Buffer* BufferList::first()
360 BufferStorage_Iter biter(bstore);
365 Buffer* BufferList::getBuffer(int choice)
367 BufferStorage_Iter biter(bstore);
371 // Be careful, this could be 0.
376 void BufferList::updateInset(Inset *inset, bool mark_dirty)
378 BufferStorage_Iter biter(bstore);
380 while ((b=biter())) {
381 if (b->text && b->text->UpdateInset(inset)) {
390 int BufferList::unlockInset(UpdatableInset *inset)
392 if (!inset) return 1;
394 BufferStorage_Iter biter(bstore);
396 while ((b=biter())) {
397 if (b->the_locking_inset == inset) {
406 void BufferList::updateIncludedTeXfiles(LString const & mastertmpdir)
408 BufferStorage_Iter biter(bstore);
410 while ((b=biter())) {
411 if (!b->isDepClean(mastertmpdir)) {
412 LString writefile = mastertmpdir;
414 writefile += ChangeExtension(b->getFileName(), ".tex", true);
415 b->makeLaTeXFile(writefile,mastertmpdir,false,true);
416 b->markDepClean(mastertmpdir);
422 void BufferList::emergencyWriteAll()
424 BufferStorage_Iter biter(bstore);
426 while ((b=biter())) {
427 if (!b->isLyxClean()) {
430 lyxerr.print(_("lyx: Attempting to save"
435 for (int i=0; i<3 && !madeit; i++) {
438 // We try to save three places:
439 // 1) Same place as document.
440 // 2) In HOME directory.
441 // 3) In "/tmp" directory.
445 s = AddName(getEnvPath("HOME"),
447 } else { // MakeAbsPath to prepend the current drive letter on OS/2
448 s = AddName(MakeAbsPath("/tmp/"),
453 lyxerr.print(LString(" ") + (i+1) + ") " + s);
455 if (b->writeFile(s,true)) {
457 lyxerr.print(_(" Save seems successful. Phew."));
460 lyxerr.print(_(" Save failed! Trying..."));
462 lyxerr.print(_(" Save failed! Bummer. Document is lost."));
470 Buffer* BufferList::readFile(LString const & s, bool ronly)
472 Buffer *b = bstore.newBuffer(s, lyxrc, ronly);
475 LString e = OnlyPath(s);
477 // File information about normal file
478 FileInfo fileInfo2(s);
480 // Check if emergency save file exists and is newer.
481 e += OnlyFilename(s) + ".emergency";
482 FileInfo fileInfoE(e);
484 bool use_emergency = false;
486 if (fileInfoE.exist() && fileInfo2.exist()) {
487 if (fileInfoE.getModificationTime()
488 > fileInfo2.getModificationTime()) {
489 if (AskQuestion(_("An emergency save of this document exists!"),
490 MakeDisplayPath(s,50),
491 _("Try to load that instead?"))) {
493 // the file is not saved if we load the
496 use_emergency = true;
498 // Here, we should delete the emergency save
504 if (!use_emergency) {
505 // Now check if autosave file is newer.
507 a += OnlyFilename(s);
509 FileInfo fileInfoA(a);
510 if (fileInfoA.exist() && fileInfo2.exist()) {
511 if (fileInfoA.getModificationTime()
512 > fileInfo2.getModificationTime()) {
513 if (AskQuestion(_("Autosave file is newer."),
514 MakeDisplayPath(s,50),
515 _("Load that one instead?"))) {
517 // the file is not saved if we load the
521 // Here, we should delete the autosave
527 // not sure if this is the correct place to begin LyXLex
530 if (b->readFile(lex))
539 bool BufferList::exists(LString const & s)
541 BufferStorage_Iter biter(bstore);
543 while ((b=biter())) {
544 if (b->filename == s)
551 Buffer* BufferList::getBuffer(LString const &s)
553 BufferStorage_Iter biter(bstore);
555 while ((b=biter())) {
563 Buffer* BufferList::newFile(LString const & name, LString tname)
565 /* get a free buffer */
566 Buffer *b = bstore.newBuffer(name, lyxrc);
568 // use defaults.lyx as a default template if it exists.
570 tname = LibFileSearch("templates", "defaults.lyx");
572 if (!tname.empty() && IsLyXFilename(tname)){
573 bool templateok = false;
577 if (b->readFile(lex)) {
582 WriteAlert(_("Error!"),_("Unable to open template"),
583 MakeDisplayPath(tname));
584 // no template, start with empty buffer
585 b->paragraph = new LyXParagraph();
588 else { // start with empty buffer
589 b->paragraph = new LyXParagraph();
593 b->setReadonly(false);
599 Buffer* BufferList::loadLyXFile(LString const & filename, bool tolastfiles)
601 // make sure our path is absolute
602 LString s = MakeAbsPath(filename);
604 // Is this done too early?
606 if (IsSGMLFilename(s)) {
608 if (fi.exist() && fi.readable()) {
609 if (!RunLinuxDoc(-1, s)) {
610 s = ChangeExtension (s, ".lyx", false);
611 } else { // sgml2lyx failed
612 WriteAlert(_("Error!"),
613 _("Could not convert file"),s);
617 // just change the extension and it will be
618 // handled like a regular lyx file that does
620 s = ChangeExtension(s, ".lyx", false);
624 // file already open?
626 if (AskQuestion(_("Document is already open:"),
627 MakeDisplayPath(s,50),
628 _("Do you want to reload that document?"))) {
629 // Reload is accomplished by closing and then loading
630 if (!close(getBuffer(s))) {
633 // Fall through to new load. (Asger)
635 // Here, we pretend that we just loaded the
642 switch (IsFileWriteable(s)) {
644 minibuffer->Set(_("File `")+MakeDisplayPath(s,50)+
645 _("' is read-only."));
651 b->lyxvc.file_found_hook(s);
653 break; //fine- it's r/w
655 // Here we probably should run
656 if (LyXVC::file_not_found_hook(s)) {
657 // Ask if the file should be checked out for
658 // viewing/editing, if so: load it.
659 lyxerr.print("Do you want to checkout?");
661 if (AskQuestion(_("Cannot open specified file:"),
662 MakeDisplayPath(s,50),
663 _("Create new document with this name?")))
665 // Find a free buffer
666 b = newFile(s,LString());
671 if (b && tolastfiles)
672 lastfiles->newFile(b->getFileName());