]> git.lyx.org Git - lyx.git/blob - src/bufferlist.C
some using changes small changes in lyxfont and some other things, read the Changelog
[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-2000 The LyX Team. 
8  *
9  *           This file is Copyright 1996-2000
10  *           Lars Gullik Bjønnes
11  *
12  * ====================================================== 
13  */
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include <config.h>
20
21 #include <algorithm>
22
23 #include "bufferlist.h"
24 #include "lyx_main.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"
30 #include "debug.h"
31 #include "lyxrc.h"
32 #include "lyxtext.h"
33 #include "lyx_cb.h"
34 #include "gettext.h"
35 #include "LyXView.h"
36 #include "vc-backend.h"
37 #include "TextCache.h"
38
39 extern BufferView * current_view; // called too many times in this file...
40
41 using std::find;
42 using std::endl;
43
44 //
45 // Class BufferStorage
46 //
47
48 void BufferStorage::release(Buffer * buf)
49 {
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
54                 // we just deleted.
55                 Buffer * tmp = (*it);
56                 container.erase(it);
57                 textcache.removeAllWithBuffer(tmp);
58                 delete tmp;
59         }
60 }
61
62
63 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
64 {
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);
70         return tmpbuf;
71 }
72
73
74 //
75 // Class BufferList
76 //
77
78 BufferList::BufferList()
79         : state_(BufferList::OK)
80 {}
81
82
83 bool BufferList::empty() const
84 {
85         return bstore.empty();
86 }
87
88
89 extern void MenuWrite(Buffer *);
90
91 bool BufferList::QwriteAll()
92 {
93         bool askMoreConfirmation = false;
94         string unsaved;
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(),
100                                                                50),
101                                                _("Save document?"))) {
102                         case 1: // Yes
103                                 MenuWrite((*it));
104                                 break;
105                         case 2: // No
106                                 askMoreConfirmation = true;
107                                 unsaved += MakeDisplayPath((*it)->fileName(),
108                                                            50);
109                                 unsaved += "\n";
110                                 break;
111                         case 3: // Cancel
112                                 return false;
113                         }
114                 }
115         }
116         if (askMoreConfirmation &&
117             lyxrc.exit_confirmation &&
118             !AskQuestion(_("Some documents were not saved:"),
119                          unsaved, _("Exit anyway?"))) {
120                 return false;
121         }
122
123         return true;
124 }
125
126
127 void BufferList::closeAll()
128 {
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.
132         textcache.clear();
133         
134         while (!bstore.empty()) {
135                 close(bstore.front());
136         }
137         state_ = BufferList::OK;
138 }
139
140
141 void BufferList::resize()
142 {
143         for(BufferStorage::iterator it = bstore.begin();
144             it != bstore.end(); ++it) {
145                 (*it)->resize();
146         }
147 }
148
149
150 bool BufferList::close(Buffer * buf)
151 {
152         if (buf->getUser()) buf->getUser()->insetUnlock();
153         
154         if (buf->paragraph && !buf->isLyxClean() && !quitting) {
155                 ProhibitInput();
156                 switch(AskConfirmation(_("Changes in document:"),
157                                        MakeDisplayPath(buf->fileName(), 50),
158                                        _("Save document?"))){
159                 case 1: // Yes
160                         if (buf->save()) {
161                                 lastfiles->newFile(buf->fileName());
162                         } else {
163                                 AllowInput();
164                                 return false;
165                         }
166                         break;
167                 case 3: // Cancel
168                         AllowInput();
169                         return false;
170                 }
171                 AllowInput();
172         }
173
174         bstore.release(buf);
175         return true;
176 }
177
178
179 vector<string> BufferList::getFileNames() const
180 {
181         vector<string> nvec;
182         for(BufferStorage::const_iterator cit = bstore.begin();
183             cit != bstore.end(); ++cit) {
184                 nvec.push_back((*cit)->fileName());
185         }
186         return nvec;
187 }
188
189
190 Buffer * BufferList::first()
191 {
192         if (bstore.empty()) return 0;
193         return bstore.front();
194 }
195
196
197 Buffer * BufferList::getBuffer(int choice)
198 {
199         if (choice >= bstore.size()) return 0;
200         return bstore[choice];
201 }
202
203
204 int BufferList::unlockInset(UpdatableInset * inset)
205 {
206         if (!inset) return 1;
207         for(BufferStorage::iterator it = bstore.begin();
208             it != bstore.end(); ++it) {
209                 if ((*it)->getUser()
210                     && (*it)->getUser()->the_locking_inset == inset) {
211                         (*it)->getUser()->insetUnlock();
212                         return 0;
213                 }
214         }
215         return 1;
216 }
217
218
219 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
220 {
221         for(BufferStorage::iterator it = bstore.begin();
222             it != bstore.end(); ++it) {
223                 if (!(*it)->isDepClean(mastertmpdir)) {
224                         string writefile = mastertmpdir;
225                         writefile += '/';
226                         writefile += ChangeExtension((*it)->fileName(),
227                                                      ".tex", true);
228                         (*it)->makeLaTeXFile(writefile, mastertmpdir,
229                                              false, true);
230                         (*it)->markDepClean(mastertmpdir);
231                 }
232         }
233 }
234
235
236 void BufferList::emergencyWriteAll()
237 {
238         for (BufferStorage::iterator it = bstore.begin();
239              it != bstore.end(); ++it) {
240                 if (!(*it)->isLyxClean()) {
241                         bool madeit = false;
242                         
243                         lyxerr <<_("lyx: Attempting to save"
244                                    " document ")
245                                << (*it)->fileName()
246                                << _(" as...") << endl;
247                         
248                         for (int i = 0; i < 3 && !madeit; ++i) {
249                                 string s;
250                                 
251                                 // We try to save three places:
252                                 // 1) Same place as document.
253                                 // 2) In HOME directory.
254                                 // 3) In "/tmp" directory.
255                                 if (i == 0) {
256                                         s = (*it)->fileName();
257                                 } else if (i == 1) {
258                                         s = AddName(GetEnvPath("HOME"),
259                                                     (*it)->fileName());
260                                 } else {
261                                         // MakeAbsPath to prepend the current
262                                         // drive letter on OS/2
263                                         s = AddName(MakeAbsPath("/tmp/"),
264                                                     (*it)->fileName());
265                                 }
266                                 s += ".emergency";
267                                 
268                                 lyxerr << "  " << i + 1 << ") " << s << endl;
269                                 
270                                 if ((*it)->writeFile(s, true)) {
271                                         (*it)->markLyxClean();
272                                         lyxerr << _("  Save seems successful. "
273                                                     "Phew.") << endl;
274                                         madeit = true;
275                                 } else if (i != 2) {
276                                         lyxerr << _("  Save failed! Trying...")
277                                                << endl;
278                                 } else {
279                                         lyxerr << _("  Save failed! Bummer. "
280                                                     "Document is lost.")
281                                                << endl;
282                                 }
283                         }
284                 }
285         }
286 }
287
288
289 Buffer * BufferList::readFile(string const & s, bool ronly)
290 {
291         Buffer * b = bstore.newBuffer(s, ronly);
292
293         string ts = s;
294         string e = OnlyPath(s);
295         string a = e;
296         // File information about normal file
297         FileInfo fileInfo2(s);
298
299         // Check if emergency save file exists and is newer.
300         e += OnlyFilename(s) + ".emergency";
301         FileInfo fileInfoE(e);
302
303         bool use_emergency = false;
304
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?"))) {
311                                 ts = e;
312                                 // the file is not saved if we load the
313                                 // emergency file.
314                                 b->markDirty();
315                                 use_emergency = true;
316                         } else {
317                                 // Here, we should delete the emergency save
318                                 ::unlink(e.c_str());
319                         }
320                 }
321         }
322
323         if (!use_emergency) {
324                 // Now check if autosave file is newer.
325                 a += '#';
326                 a += OnlyFilename(s);
327                 a += '#';
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?"))) {
335                                         ts = a;
336                                         // the file is not saved if we load the
337                                         // autosave file.
338                                         b->markDirty();
339                                 } else {
340                                         // Here, we should delete the autosave
341                                         ::unlink(a.c_str());
342                                 }
343                         }
344                 }
345         }
346         // not sure if this is the correct place to begin LyXLex
347         LyXLex lex(0, 0);
348         lex.setFile(ts);
349         if (b->readFile(lex))
350                 return b;
351         else {
352                 bstore.release(b);
353                 return 0;
354         }
355 }
356
357
358 bool BufferList::exists(string const & s) const
359 {
360         for (BufferStorage::const_iterator cit = bstore.begin();
361              cit != bstore.end(); ++cit) {
362                 if ((*cit)->fileName() == s)
363                         return true;
364         }
365         return false;
366 }
367
368
369 bool BufferList::isLoaded(Buffer const * b) const
370 {
371         BufferStorage::const_iterator cit =
372                 find(bstore.begin(), bstore.end(), b);
373         return cit != bstore.end();
374 }
375
376
377 Buffer * BufferList::getBuffer(string const & s)
378 {
379         for(BufferStorage::iterator it = bstore.begin();
380             it != bstore.end(); ++it) {
381                 if ((*it)->fileName() == s)
382                         return (*it);
383         }
384         return 0;
385 }
386
387
388 Buffer * BufferList::newFile(string const & name, string tname)
389 {
390         // get a free buffer
391         Buffer * b = bstore.newBuffer(name);
392
393         // use defaults.lyx as a default template if it exists.
394         if (tname.empty()) {
395                 tname = LibFileSearch("templates", "defaults.lyx");
396         }
397         if (!tname.empty() && IsLyXFilename(tname)) {
398                 bool templateok = false;
399                 LyXLex lex(0, 0);
400                 lex.setFile(tname);
401                 if (lex.IsOK()) {
402                         if (b->readFile(lex)) {
403                                 templateok = true;
404                         }
405                 }
406                 if (!templateok) {
407                         WriteAlert(_("Error!"), _("Unable to open template"), 
408                                    MakeDisplayPath(tname));
409                         // no template, start with empty buffer
410                         b->paragraph = new LyXParagraph;
411                 }
412         }
413         else {  // start with empty buffer
414                 b->paragraph = new LyXParagraph;
415         }
416
417         b->markDirty();
418         b->setReadonly(false);
419         
420         return b;
421 }
422
423
424 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
425 {
426         // make sure our path is absolute
427         string s = MakeAbsPath(filename);
428
429         // file already open?
430         if (exists(s)) {
431                 if (AskQuestion(_("Document is already open:"), 
432                                 MakeDisplayPath(s, 50),
433                                 _("Do you want to reload that document?"))) {
434                         // Reload is accomplished by closing and then loading
435                         if (!close(getBuffer(s))) {
436                                 return 0;
437                         }
438                         // Fall through to new load. (Asger)
439                 } else {
440                         // Here, we pretend that we just loaded the 
441                         // open document
442                         return getBuffer(s);
443                 }
444         }
445         Buffer * b = 0;
446         bool ro = false;
447         switch (IsFileWriteable(s)) {
448         case 0:
449 #if 0
450                 current_view->owner()->getMiniBuffer()->
451                         Set(_("File `") + MakeDisplayPath(s, 50) +
452                             _("' is read-only."));
453 #endif
454                 ro = true;
455                 // Fall through
456         case 1:
457                 b = readFile(s, ro);
458                 if (b) {
459                         b->lyxvc.file_found_hook(s);
460                 }
461                 break; //fine- it's r/w
462         case -1:
463                 // Here we probably should run
464                 if (LyXVC::file_not_found_hook(s)) {
465                         // Ask if the file should be checked out for
466                         // viewing/editing, if so: load it.
467                         if (AskQuestion(_("Do you want to retrive file under version control?"))) {
468                                 // How can we know _how_ to do the checkout?
469                                 // With the current VC support it has to be,
470                                 // a RCS file since CVS do not have special ,v files.
471                                 RCS::retrive(s);
472                                 return loadLyXFile(filename, tolastfiles);
473                         }
474                 }
475                 if (AskQuestion(_("Cannot open specified file:"), 
476                                 MakeDisplayPath(s, 50),
477                                 _("Create new document with this name?")))
478                         {
479                                 // Find a free buffer
480                                 b = newFile(s, string());
481                         }
482                 break;
483         }
484
485         if (b && tolastfiles)
486                 lastfiles->newFile(b->fileName());
487
488         return b;
489 }