]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.cpp
3f64b909f2ce827b9b0262131f1c430fe9ac4417
[lyx.git] / src / buffer_funcs.cpp
1 /**
2  * \file buffer_funcs.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Alfredo Braunstein
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  */
12
13 #include <config.h>
14
15 #include "buffer_funcs.h"
16 #include "Buffer.h"
17 #include "BufferList.h"
18 #include "BufferParams.h"
19 #include "DocIterator.h"
20 #include "Counters.h"
21 #include "ErrorList.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "InsetList.h"
25 #include "Language.h"
26 #include "LaTeX.h"
27 #include "Layout.h"
28 #include "LyX.h"
29 #include "LyXVC.h"
30 #include "TextClass.h"
31 #include "Paragraph.h"
32 #include "ParagraphList.h"
33 #include "ParagraphParameters.h"
34 #include "ParIterator.h"
35 #include "TexRow.h"
36 #include "Text.h"
37 #include "TocBackend.h"
38
39 #include "frontends/alert.h"
40
41 #include "insets/InsetBibitem.h"
42 #include "insets/InsetInclude.h"
43
44 #include "support/lassert.h"
45 #include "support/convert.h"
46 #include "support/debug.h"
47 #include "support/filetools.h"
48 #include "support/gettext.h"
49 #include "support/lstrings.h"
50 #include "support/textutils.h"
51
52 using namespace std;
53 using namespace lyx::support;
54
55 namespace lyx {
56
57 namespace Alert = frontend::Alert;
58
59
60 Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
61 {
62         // File already open?
63         Buffer * checkBuffer = theBufferList().getBuffer(filename);
64         if (checkBuffer) {
65                 // Sometimes (when setting the master buffer from a child)
66                 // we accept a dirty buffer right away (otherwise we'd get
67                 // an infinite loop (bug 5514).
68                 // We also accept a dirty buffer when the document has not
69                 // yet been saved to disk.
70                 if (checkBuffer->isClean() || acceptDirty || !filename.exists())
71                         return checkBuffer;
72                 docstring const file = makeDisplayPath(filename.absFileName(), 20);
73                 docstring const text = bformat(_(
74                                 "The document %1$s is already loaded and has unsaved changes.\n"
75                                 "Do you want to abandon your changes and reload the version on disk?"), file);
76                 int res = Alert::prompt(_("Reload saved document?"),
77                                         text, 2, 2,  _("Yes, &Reload"), _("No, &Keep Changes"), _("&Cancel"));
78                 switch (res) {
79                         case 0: {
80                                 // reload the document
81                                 if (checkBuffer->reload() != Buffer::ReadSuccess)
82                                         return 0;
83                                 return checkBuffer;
84                         }
85                         case 1:
86                                 // keep changes
87                                 return checkBuffer;
88                         case 2:
89                                 // cancel
90                                 return 0;
91                 }
92         }
93
94         bool const exists = filename.exists();
95         bool const tryVC = exists ? false : LyXVC::fileInVC(filename);
96         if (exists || tryVC) {
97                 if (exists && !filename.isReadableFile()) {
98                         docstring text = bformat(_("The file %1$s exists but is not "
99                                 "readable by the current user."),
100                                 from_utf8(filename.absFileName()));
101                         Alert::error(_("File not readable!"), text);
102                         return 0;
103                 }
104                 Buffer * b = theBufferList().newBuffer(filename.absFileName());
105                 if (!b) {
106                         // Buffer creation is not possible.
107                         return 0;
108                 }
109                 if (b->loadLyXFile() != Buffer::ReadSuccess) {
110                         // do not save an emergency file when releasing the buffer
111                         b->markClean();
112                         theBufferList().release(b);
113                         return 0;
114                 }
115                 return b;
116         }
117
118         docstring text = bformat(_("The document %1$s does not yet "
119                 "exist.\n\nDo you want to create a new document?"),
120                 from_utf8(filename.absFileName()));
121         if (!Alert::prompt(_("Create new document?"),
122                         text, 0, 1, _("&Create"), _("Cancel")))
123                 return newFile(filename.absFileName(), string(), true);
124
125         return 0;
126 }
127
128
129 // FIXME newFile() should probably be a member method of Application...
130 Buffer * newFile(string const & filename, string const & templatename,
131                  bool is_named)
132 {
133         // get a free buffer
134         Buffer * b = theBufferList().newBuffer(filename);
135         if (!b)
136                 // Buffer creation is not possible.
137                 return 0;
138
139         FileName tname;
140         // use defaults.lyx as a default template if it exists.
141         if (templatename.empty())
142                 tname = libFileSearch("templates", "defaults.lyx");
143         else
144                 tname = makeAbsPath(templatename);
145
146         if (!tname.empty()) {
147                 if (b->loadThisLyXFile(tname) != Buffer::ReadSuccess) {
148                         docstring const file = makeDisplayPath(tname.absFileName(), 50);
149                         docstring const text  = bformat(
150                                 _("The specified document template\n%1$s\ncould not be read."),
151                                 file);
152                         Alert::error(_("Could not read template"), text);
153                         theBufferList().release(b);
154                         return 0;
155                 }
156         }
157
158         if (is_named)
159                 // in this case, the user chose the filename, so we
160                 // assume that she really does want this file.
161                 b->markDirty();
162         else
163                 b->setUnnamed();
164
165         b->setReadonly(false);
166         b->setFullyLoaded(true);
167
168         return b;
169 }
170
171
172 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
173                                                 string const & templatename)
174 {
175         // FIXME THREAD
176         static map<string, int> file_number;
177
178         FileName filename;
179
180         do {
181                 filename.set(path, 
182                         prefix + convert<string>(++file_number[prefix]) + ".lyx");
183         }
184         while (theBufferList().exists(filename) || filename.isReadableFile());
185                 
186         return newFile(filename.absFileName(), templatename, false);
187 }
188
189
190 Buffer * loadIfNeeded(FileName const & fname)
191 {
192         Buffer * buffer = theBufferList().getBuffer(fname);
193         if (!buffer) {
194                 if (!fname.exists() && !LyXVC::fileInVC(fname))
195                         return 0;
196
197                 buffer = theBufferList().newBuffer(fname.absFileName());
198                 if (!buffer)
199                         // Buffer creation is not possible.
200                         return 0;
201
202                 if (buffer->loadLyXFile() != Buffer::ReadSuccess) {
203                         //close the buffer we just opened
204                         theBufferList().release(buffer);
205                         return 0;
206                 }
207         }
208         return buffer;
209 }
210
211
212 } // namespace lyx