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