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