]> git.lyx.org Git - lyx.git/blob - src/bufferlist.C
e6c2c3fc34555ab122ed4f1f91d811b4cbd923c8
[lyx.git] / src / bufferlist.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Word Processor
5  *
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-1999 The LyX Team. 
8  *
9  *           This file is Copyright 1996-1999
10  *           Lars Gullik Bjønnes
11  *
12  * ======================================================
13  */
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include <config.h>
20 #include <sys/types.h>
21 #include <utime.h>
22 #include "bufferlist.h"
23 #include "lyx_main.h"
24 #include "minibuffer.h"
25 #include "support/FileInfo.h"
26 #include "support/filetools.h"
27 #include "lyx_gui_misc.h"
28 #include "lastfiles.h"
29 #include "debug.h"
30 #include "lyxrc.h"
31 #include "lyxscreen.h"
32 #include "lyxtext.h"
33 #include "lyx_cb.h"
34 #include "gettext.h"
35
36 extern BufferView * current_view;
37 extern MiniBuffer * minibuffer;
38 extern void SmallUpdate(signed char);
39 extern void BeforeChange();
40 extern int RunLinuxDoc(int, string const &);
41
42 //
43 // Class BufferStorage
44 //
45
46 #ifndef NEW_STORE
47 BufferStorage::BufferStorage()
48 {
49         // Initialize the buffer array
50         for (int i=NUMBER_OF_BUFFERS-1; i >=0; i--) {
51                 buffer[i] = 0;
52         }
53 }
54 #endif
55
56 #ifndef NEW_STORE
57 bool BufferStorage::isEmpty()
58 {
59         for (int i=NUMBER_OF_BUFFERS-1; i >=0; i--) {
60                 if (buffer[i]) return false;
61         }
62         return true;
63 }
64 #endif
65
66 void BufferStorage::release(Buffer * buf)
67 {
68 #ifdef NEW_STORE
69         for(Container::iterator it = container.begin();
70             it != container.end(); ++it) {
71                 if ((*it) == buf) {
72                         Buffer * tmpbuf = (*it);
73                         container.erase(it);
74                         delete tmpbuf;
75                         break;
76                 }
77         }
78 #else
79         int i = 0;
80         for (i = 0; i < NUMBER_OF_BUFFERS; i++)
81                 if (buffer[i] == buf) break;
82         Buffer * tmpbuf = buffer[i];
83         buffer[i] = 0;
84         delete tmpbuf;
85 #endif
86 }
87
88
89 Buffer * BufferStorage::newBuffer(string const & s,
90                                  LyXRC * lyxrc,
91                                  bool ronly)
92 {
93 #ifdef NEW_STORE
94         Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
95         tmpbuf->params.useClassDefaults();
96         lyxerr.debug() << "Assigning to buffer "
97                        << container.size() + 1 << endl;
98         container.push_back(tmpbuf);
99         return tmpbuf;
100 #else
101         int i=0;
102         while (i < NUMBER_OF_BUFFERS - 1
103                && buffer[i]) i++;
104         buffer[i] = new Buffer(s, lyxrc, ronly);
105         buffer[i]->params.useClassDefaults();
106         lyxerr.debug() << "Assigning to buffer " << i << endl;
107         return buffer[i];
108 #endif
109 }
110
111
112 #ifndef NEW_STORE
113 //
114 // Class BufferStrorage_Iter
115 //
116
117 Buffer * BufferStorage_Iter::operator() ()
118 {
119         int i = 0;
120         for (i = index; i < BufferStorage::NUMBER_OF_BUFFERS; i++) {
121                 if (cs->buffer[i]) {
122                         index = i + 1;
123                         return cs->buffer[i];
124                 }
125         }
126         return 0;       
127 }
128
129
130 Buffer * BufferStorage_Iter::operator[] (int a)
131 {
132         // a is >=1
133         if (a <= 0) return 0;
134         
135         int i = 0;
136         while (a--) {
137                 while(!cs->buffer[i++]);
138         }
139         if (i - 1 < BufferStorage::NUMBER_OF_BUFFERS)
140                 return cs->buffer[i - 1];
141         return 0;       
142 }
143 #endif
144
145
146 //
147 // Class BufferList
148 //
149 BufferList::BufferList()
150 {
151         _state = BufferList::OK;
152 }
153
154
155 bool BufferList::empty()
156 {
157         return bstore.empty();
158 }
159
160
161 extern void MenuWrite(Buffer *);
162
163 bool BufferList::QwriteAll()
164 {
165         bool askMoreConfirmation = false;
166         string unsaved;
167 #ifdef NEW_STORE
168         for(BufferStorage::iterator it = bstore.begin();
169             it != bstore.end(); ++it) {
170                 if (!(*it)->isLyxClean()) {
171                         switch(AskConfirmation(_("Changes in document:"),
172                                                MakeDisplayPath((*it)->filename,
173                                                                50),
174                                                _("Save document?"))) {
175                         case 1: // Yes
176                                 MenuWrite((*it));
177                                 break;
178                         case 2: // No
179                                 askMoreConfirmation = true;
180                                 unsaved += MakeDisplayPath((*it)->filename,50);
181                                 unsaved += "\n";
182                                 break;
183                         case 3: // Cancel
184                                 return false;
185                         }
186                 }
187         }
188 #else
189         BufferStorage_Iter biter(bstore);
190         Buffer * b = 0;
191         while ((b = biter())) {
192                 if (!b->isLyxClean()) {
193                         switch(AskConfirmation(_("Changes in document:"),
194                                                MakeDisplayPath(b->filename,50),
195                                                _("Save document?"))) {
196                         case 1: // Yes
197                                 MenuWrite(b);
198                                 break;
199                         case 2: // No
200                                 askMoreConfirmation = true;
201                                 unsaved += MakeDisplayPath(b->filename,50);
202                                 unsaved += "\n";
203                                 break;
204                         case 3: // Cancel
205                                 return false;
206                         }
207                 }
208         }
209 #endif
210         if (askMoreConfirmation &&
211             lyxrc->exit_confirmation &&
212             !AskQuestion(_("Some documents were not saved:"),
213                          unsaved, _("Exit anyway?"))) {
214                 return false;
215         }
216
217         return true;
218 }
219
220
221 // Should probably be moved to somewhere else: BufferView? LyXView?
222 bool BufferList::write(Buffer * buf, bool makeBackup)
223 {
224         minibuffer->Set(_("Saving document"),
225                         MakeDisplayPath(buf->filename),"...");
226
227         // We don't need autosaves in the immediate future. (Asger)
228         buf->resetAutosaveTimers();
229
230         // make a backup
231         if (makeBackup) {
232                 string s = buf->filename + '~';
233                 // Rename is the wrong way of making a backup,
234                 // this is the correct way.
235                 /* truss cp fil fil2:
236                    lstat("LyXVC3.lyx", 0xEFFFF898)                 Err#2 ENOENT
237                    stat("LyXVC.lyx", 0xEFFFF688)                   = 0
238                    open("LyXVC.lyx", O_RDONLY)                     = 3
239                    open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
240                    fstat(4, 0xEFFFF508)                            = 0
241                    fstat(3, 0xEFFFF508)                            = 0
242                    read(3, " # T h i s   f i l e   w".., 8192)     = 5579
243                    write(4, " # T h i s   f i l e   w".., 5579)    = 5579
244                    read(3, 0xEFFFD4A0, 8192)                       = 0
245                    close(4)                                        = 0
246                    close(3)                                        = 0
247                    chmod("LyXVC3.lyx", 0100644)                    = 0
248                    lseek(0, 0, SEEK_CUR)                           = 46440
249                    _exit(0)
250                  */
251
252                 // Should proabaly have some more error checking here.
253                 // Should be cleaned up in 0.13, at least a bit.
254                 // Doing it this way, also makes the inodes stay the same.
255                 // This is still not a very good solution, in particular we
256                 // might loose the owner of the backup.
257                 FileInfo finfo(buf->filename);
258                 if (finfo.exist()) {
259                         mode_t fmode = finfo.getMode();
260
261                         struct utimbuf *times =
262                                 (struct utimbuf*)new char[sizeof(struct utimbuf)];
263                         times->actime = finfo.getAccessTime();
264                         times->modtime = finfo.getModificationTime();
265                         long blksize = finfo.getBlockSize();
266                         lyxerr.debug() << "BlockSize: " << blksize << endl;
267                         FilePtr fin(buf->filename,FilePtr::read);
268                         FilePtr fout(s,FilePtr::truncate);
269                         if (fin() && fout()) {
270                                 char * cbuf = new char[blksize+1];
271                                 size_t c_read = 0;
272                                 size_t c_write = 0;
273                                 do {
274                                         c_read = fread(cbuf, 1, blksize, fin);
275                                         if (c_read != 0)
276                                                 c_write =
277                                                         fwrite(cbuf, 1, c_read, fout);
278                                 } while (c_read);
279                                 fin.close();
280                                 fout.close();
281                                 chmod(s.c_str(), fmode);
282                                 
283                                 if (utime(s.c_str(), times)) {
284                                         lyxerr << "utime error." << endl;
285                                 }
286                                 delete [] cbuf;
287                         } else {
288                                 lyxerr << "LyX was not able to make backupcopy. Beware." << endl;
289                         }
290                         delete[] times;
291                 }
292         }
293         
294         if (buf->writeFile(buf->filename,false)) {
295                 buf->markLyxClean();
296
297                 minibuffer->Set(_("Document saved as"),
298                                 MakeDisplayPath(buf->filename));
299
300                 // now delete the autosavefile
301                 string a = OnlyPath(buf->filename);
302                 a += '#';
303                 a += OnlyFilename(buf->filename);
304                 a += '#';
305                 FileInfo fileinfo(a);
306                 if (fileinfo.exist()) {
307                         if (remove(a.c_str()) != 0) {
308                                 WriteFSAlert(_("Could not delete "
309                                                "auto-save file!"), a);
310                         }
311                 }
312         } else {
313                 // Saving failed, so backup is not backup
314                 if (makeBackup) {
315                         string s = buf->filename + '~';
316                         rename(s.c_str(), buf->filename.c_str());
317                 }
318                 minibuffer->Set(_("Save failed!"));
319                 return false;
320         }
321
322         return true;
323 }
324
325
326 void BufferList::closeAll()
327 {
328         _state = BufferList::CLOSING;
329 #ifdef NEW_STORE
330         while (!bstore.empty()) {
331                 close(bstore.front());
332         }
333 #else
334         BufferStorage_Iter biter(bstore);
335         Buffer * b = 0;
336         while ((b = biter())) {
337                 close(b);
338         }
339 #endif
340         _state = BufferList::OK;
341 }
342
343
344 void BufferList::resize()
345 {
346 #ifdef NEW_STORE
347         for(BufferStorage::iterator it = bstore.begin();
348             it != bstore.end(); ++it) {
349                 (*it)->resize();
350         }
351 #else
352         BufferStorage_Iter biter(bstore);
353         Buffer * b = 0;
354         while ((b = biter())) {
355                 b->resize();
356         }
357 #endif
358 }
359
360
361 bool BufferList::close(Buffer * buf)
362 {
363         buf->InsetUnlock();
364         
365         if (buf->paragraph && !buf->isLyxClean() && !quitting) {
366                 ProhibitInput();
367                 switch(AskConfirmation(_("Changes in document:"),
368                               MakeDisplayPath(buf->filename,50),
369                                       _("Save document?"))){
370                 case 1: // Yes
371                         if (write(buf)) {
372                                 lastfiles->newFile(buf->filename);
373                         } else {
374                                 AllowInput();
375                                 return false;
376                         }
377                         break;
378                 case 3: // Cancel
379                         AllowInput();
380                         return false;
381                 }
382                 AllowInput();
383         }
384
385         bstore.release(buf);
386         return true;
387 }
388
389
390 void BufferList::makePup(int pup)
391         /* This should be changed to return a char const *const
392            in the same way as for lastfiles.[hC]
393            */
394 {
395         int ant = 0;
396 #ifdef NEW_STORE
397         for(BufferStorage::iterator it = bstore.begin();
398             it != bstore.end(); ++it) {
399                 string relbuf = MakeDisplayPath((*it)->filename, 30);
400                 fl_addtopup(pup, relbuf.c_str());
401                 ++ant;
402         }
403 #else
404         BufferStorage_Iter biter(bstore);
405         Buffer * b = 0;
406         while ((b = biter())) {
407                 string relbuf = MakeDisplayPath(b->filename, 30);
408                 fl_addtopup(pup, relbuf.c_str());
409                 ++ant;
410         }
411 #endif
412         if (ant == 0) fl_addtopup(pup,_("No Documents Open!%t"));
413 }
414
415
416 Buffer * BufferList::first()
417 {
418 #ifdef NEW_STORE
419         if (bstore.empty()) return 0;
420         return bstore.front();
421 #else
422         BufferStorage_Iter biter(bstore);
423         return biter();
424 #endif
425 }
426
427
428 Buffer * BufferList::getBuffer(int choice)
429 {
430 #ifdef NEW_STORE
431         if (choice >= bstore.size()) return 0;
432         return bstore[choice];
433 #else
434         BufferStorage_Iter biter(bstore);
435         Buffer * b = 0;
436         b = biter[choice];
437             
438         // Be careful, this could be 0.    
439         return b;
440 #endif
441 }
442
443
444 void BufferList::updateInset(Inset * inset, bool mark_dirty)
445 {
446 #ifdef NEW_STORE
447         for (BufferStorage::iterator it = bstore.begin();
448              it != bstore.end(); ++it) {
449                 if ((*it)->text && (*it)->text->UpdateInset(inset)) {
450                         if (mark_dirty)
451                                 (*it)->markDirty();
452                         break;
453                 }
454         }
455 #else
456         BufferStorage_Iter biter(bstore);
457         Buffer *b=0;
458         while ((b=biter())) {
459                 if (b->text && b->text->UpdateInset(inset)) {
460                         if (mark_dirty)
461                                 b->markDirty();
462                         break;
463                 }
464         }
465 #endif
466 }
467
468
469 int BufferList::unlockInset(UpdatableInset * inset)
470 {
471 #ifdef NEW_STORE
472         if (!inset) return 1;
473         for(BufferStorage::iterator it = bstore.begin();
474             it != bstore.end(); ++it) {
475                 if ((*it)->the_locking_inset == inset) {
476                         (*it)->InsetUnlock();
477                         return 0;
478                 }
479         }
480         return 1;
481 #else
482         if (!inset) return 1;
483         
484         BufferStorage_Iter biter(bstore);
485         Buffer *b=0;
486         while ((b=biter())) {
487                 if (b->the_locking_inset == inset) {
488                         b->InsetUnlock();
489                         return 0;
490                 }
491         }
492         return 1;
493 #endif
494 }
495
496
497 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
498 {
499 #ifdef NEW_STORE
500         for(BufferStorage::iterator it = bstore.begin();
501             it != bstore.end(); ++it) {
502                 if (!(*it)->isDepClean(mastertmpdir)) {
503                         string writefile = mastertmpdir;
504                         writefile += '/';
505                         writefile += ChangeExtension((*it)->getFileName(),
506                                                      ".tex", true);
507                         (*it)->makeLaTeXFile(writefile, mastertmpdir,
508                                              false, true);
509                         (*it)->markDepClean(mastertmpdir);
510                                                      
511                 }
512         }
513 #else
514         BufferStorage_Iter biter(bstore);
515         Buffer *b=0;
516         while ((b=biter())) {
517                 if (!b->isDepClean(mastertmpdir)) {
518                         string writefile = mastertmpdir;
519                         writefile += '/';
520                         writefile += ChangeExtension(b->getFileName(), ".tex", true);
521                         b->makeLaTeXFile(writefile,mastertmpdir,false,true);
522                         b->markDepClean(mastertmpdir);
523                 }
524         }
525 #endif
526 }
527
528
529 void BufferList::emergencyWriteAll()
530 {
531 #ifdef NEW_STORE
532         for (BufferStorage::iterator it = bstore.begin();
533              it != bstore.end(); ++it) {
534                 if (!(*it)->isLyxClean()) {
535                         bool madeit=false;
536                         
537                         lyxerr <<_("lyx: Attempting to save"
538                                       " document ")
539                                << (*it)->filename
540                                << _(" as...") << endl;
541                         
542                         for (int i = 0; i < 3 && !madeit; ++i) {
543                                 string s;
544                                 
545                                 // We try to save three places:
546                                 // 1) Same place as document.
547                                 // 2) In HOME directory.
548                                 // 3) In "/tmp" directory.
549                                 if (i == 0) {
550                                         s = (*it)->filename;
551                                 } else if (i == 1) {
552                                         s = AddName(GetEnvPath("HOME"),
553                                                     (*it)->filename);
554                                 } else { // MakeAbsPath to prepend the current drive letter on OS/2
555                                         s = AddName(MakeAbsPath("/tmp/"),
556                                                     (*it)->filename);
557                                 }
558                                 s += ".emergency";
559                                 
560                                 lyxerr << "  " << i + 1 << ") " << s << endl;
561                                 
562                                 if ((*it)->writeFile(s,true)) {
563                                         (*it)->markLyxClean();
564                                         lyxerr << _("  Save seems successful. "
565                                                     "Phew.") << endl;
566                                         madeit = true;
567                                 } else if (i != 2) {
568                                         lyxerr << _("  Save failed! Trying...")
569                                                << endl;
570                                 } else {
571                                         lyxerr << _("  Save failed! Bummer. Document is lost.") << endl;
572                                 }
573                         }
574                 }
575         }
576 #else
577         BufferStorage_Iter biter(bstore);
578         Buffer * b = 0;
579         while ((b = biter())) {
580                 if (!b->isLyxClean()) {
581                         bool madeit = false;
582                         
583                         lyxerr <<_("lyx: Attempting to save"
584                                       " document ")
585                                << b->filename
586                                << _(" as...") << endl;
587                         
588                         for (int i=0; i<3 && !madeit; i++) {
589                                 string s;
590                                 
591                                 // We try to save three places:
592                                 // 1) Same place as document.
593                                 // 2) In HOME directory.
594                                 // 3) In "/tmp" directory.
595                                 if (i == 0) {
596                                         s = b->filename;
597                                 } else if (i == 1) {
598                                         s = AddName(GetEnvPath("HOME"),
599                                                     b->filename);
600                                 } else { // MakeAbsPath to prepend the current drive letter on OS/2
601                                         s = AddName(MakeAbsPath("/tmp/"),
602                                                     b->filename);
603                                 }
604                                 s += ".emergency";
605                                 
606                                 lyxerr << "  " << i + 1 << ") " << s << endl;
607                                 
608                                 if (b->writeFile(s,true)) {
609                                         b->markLyxClean();
610                                         lyxerr << _("  Save seems successful. Phew.") << endl;
611                                         madeit = true;
612                                 } else if (i != 2) {
613                                         lyxerr << _("  Save failed! Trying...")
614                                                << endl;
615                                 } else {
616                                         lyxerr << _("  Save failed! Bummer. Document is lost.") << endl;
617                                 }
618                         }
619                 }
620         }
621 #endif
622 }
623
624
625 Buffer* BufferList::readFile(string const & s, bool ronly)
626 {
627         Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
628
629         string ts = s;
630         string e = OnlyPath(s);
631         string a = e;
632         // File information about normal file
633         FileInfo fileInfo2(s);
634
635         // Check if emergency save file exists and is newer.
636         e += OnlyFilename(s) + ".emergency";
637         FileInfo fileInfoE(e);
638
639         bool use_emergency = false;
640
641         if (fileInfoE.exist() && fileInfo2.exist()) {
642                 if (fileInfoE.getModificationTime()
643                     > fileInfo2.getModificationTime()) {
644                         if (AskQuestion(_("An emergency save of this document exists!"),
645                                         MakeDisplayPath(s,50),
646                                         _("Try to load that instead?"))) {
647                                 ts = e;
648                                 // the file is not saved if we load the
649                                 // emergency file.
650                                 b->markDirty();
651                                 use_emergency = true;
652                         } else {
653                                 // Here, we should delete the emergency save
654                                 unlink(e.c_str());
655                         }
656                 }
657         }
658
659         if (!use_emergency) {
660                 // Now check if autosave file is newer.
661                 a += '#';
662                 a += OnlyFilename(s);
663                 a += '#';
664                 FileInfo fileInfoA(a);
665                 if (fileInfoA.exist() && fileInfo2.exist()) {
666                         if (fileInfoA.getModificationTime()
667                             > fileInfo2.getModificationTime()) {
668                                 if (AskQuestion(_("Autosave file is newer."),
669                                                 MakeDisplayPath(s,50),
670                                                 _("Load that one instead?"))) {
671                                         ts = a;
672                                         // the file is not saved if we load the
673                                         // autosave file.
674                                         b->markDirty();
675                                 } else {
676                                         // Here, we should delete the autosave
677                                         unlink(a.c_str());
678                                 }
679                         }
680                 }
681         }
682         // not sure if this is the correct place to begin LyXLex
683         LyXLex lex(0, 0);
684         lex.setFile(ts);
685         if (b->readFile(lex))
686                 return b;
687         else {
688                 bstore.release(b);
689                 return 0;
690         }
691 }
692
693
694 bool BufferList::exists(string const & s)
695 {
696 #ifdef NEW_STORE
697         for (BufferStorage::iterator it = bstore.begin();
698              it != bstore.end(); ++it) {
699                 if ((*it)->filename == s)
700                         return true;
701         }
702         return false;
703 #else
704         BufferStorage_Iter biter(bstore);
705         Buffer * b = 0;
706         while ((b = biter())) {
707                 if (b->filename == s)
708                         return true;
709         }
710         return false;
711 #endif
712 }
713
714
715 Buffer * BufferList::getBuffer(string const & s)
716 {
717 #ifdef NEW_STORE
718         for(BufferStorage::iterator it = bstore.begin();
719             it != bstore.end(); ++it) {
720                 if ((*it)->filename == s)
721                         return (*it);
722         }
723         return 0;
724 #else
725         BufferStorage_Iter biter(bstore);
726         Buffer * b = 0;
727         while ((b = biter())) {
728                 if (b->filename == s)
729                         return b;
730         }
731         return 0;
732 #endif
733 }
734
735
736 Buffer * BufferList::newFile(string const & name, string tname)
737 {
738         /* get a free buffer */ 
739         Buffer * b = bstore.newBuffer(name, lyxrc);
740
741         // use defaults.lyx as a default template if it exists.
742         if (tname.empty()) {
743                 tname = LibFileSearch("templates", "defaults.lyx");
744         }
745         if (!tname.empty() && IsLyXFilename(tname)){
746                 bool templateok = false;
747                 LyXLex lex(0,0);
748                 lex.setFile(tname);
749                 if (lex.IsOK()) {
750                         if (b->readFile(lex)) {
751                                 templateok = true;
752                         }
753                 }
754                 if (!templateok) {
755                         WriteAlert(_("Error!"),_("Unable to open template"), 
756                                    MakeDisplayPath(tname));
757                         // no template, start with empty buffer
758                         b->paragraph = new LyXParagraph;
759 #ifdef NEW_TEXT
760                         b->paragraph->text.reserve(500);
761 #endif
762                 }
763         }
764         else {  // start with empty buffer
765                 b->paragraph = new LyXParagraph;
766 #ifdef NEW_TEXT
767                 b->paragraph->text.reserve(500);
768 #endif
769         }
770
771         b->markDirty();
772         b->setReadonly(false);
773         
774         return b;
775 }
776
777
778 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
779 {
780         // make sure our path is absolute
781         string s = MakeAbsPath(filename);
782
783         // Is this done too early?
784         // Is it LinuxDoc?
785         if (IsSGMLFilename(s)) {
786                 FileInfo fi(s);
787                 if (fi.exist() && fi.readable()) {
788                         if (!RunLinuxDoc(-1, s)) {
789                                 s = ChangeExtension (s, ".lyx", false);
790                         } else { // sgml2lyx failed
791                                 WriteAlert(_("Error!"),
792                                            _("Could not convert file"),s);
793                                 return 0;
794                         }
795                 } else {
796                         // just change the extension and it will be
797                         // handled like a regular lyx file that does
798                         // not exist.
799                         s = ChangeExtension(s, ".lyx", false);
800                 }
801         }
802         
803         // file already open?
804         if (exists(s)) {
805                 if (AskQuestion(_("Document is already open:"), 
806                                 MakeDisplayPath(s,50),
807                                 _("Do you want to reload that document?"))) {
808                         // Reload is accomplished by closing and then loading
809                         if (!close(getBuffer(s))) {
810                                 return 0;
811                         }
812                         // Fall through to new load. (Asger)
813                 } else {
814                         // Here, we pretend that we just loaded the 
815                         // open document
816                         return getBuffer(s);
817                 }
818         }
819         Buffer * b = 0;
820         bool ro = false;
821         switch (IsFileWriteable(s)) {
822         case 0:
823                 minibuffer->Set(_("File `")+MakeDisplayPath(s,50)+
824                                 _("' is read-only."));
825                 ro = true;
826                 // Fall through
827         case 1:
828                 b = readFile(s, ro);
829                 if (b) {
830                         b->lyxvc.file_found_hook(s);
831                 }
832                 break; //fine- it's r/w
833         case -1:
834                 // Here we probably should run
835                 if (LyXVC::file_not_found_hook(s)) {
836                         // Ask if the file should be checked out for
837                         // viewing/editing, if so: load it.
838                         lyxerr << "Do you want to checkout?" << endl;
839                 }
840                 if (AskQuestion(_("Cannot open specified file:"), 
841                                 MakeDisplayPath(s,50),
842                                 _("Create new document with this name?")))
843                 {
844                         // Find a free buffer
845                         b = newFile(s,string());
846                 }
847                 break;
848         }
849
850         if (b && tolastfiles)
851                 lastfiles->newFile(b->getFileName());
852
853         return b;
854 }