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