]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.cpp
Disable CheckTeX while buffer is processed
[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/mutex.h"
51 #include "support/textutils.h"
52
53 using namespace std;
54 using namespace lyx::support;
55
56 namespace lyx {
57
58 namespace Alert = frontend::Alert;
59
60
61 Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
62 {
63         // File already open?
64         Buffer * checkBuffer = theBufferList().getBuffer(filename);
65         if (checkBuffer) {
66                 // Sometimes (when setting the master buffer from a child)
67                 // we accept a dirty buffer right away (otherwise we'd get
68                 // an infinite loop (bug 5514).
69                 // We also accept a dirty buffer when the document has not
70                 // yet been saved to disk.
71                 if (checkBuffer->isClean() || acceptDirty || !filename.exists())
72                         return checkBuffer;
73                 docstring const file = makeDisplayPath(filename.absFileName(), 20);
74                 docstring const text = bformat(_(
75                                 "The document %1$s is already loaded and has unsaved changes.\n"
76                                 "Do you want to abandon your changes and reload the version on disk?"), file);
77                 int res = Alert::prompt(_("Reload saved document?"),
78                                         text, 2, 2,  _("Yes, &Reload"), _("No, &Keep Changes"), _("&Cancel"));
79                 switch (res) {
80                         case 0: {
81                                 // reload the document
82                                 if (checkBuffer->reload() != Buffer::ReadSuccess)
83                                         return 0;
84                                 return checkBuffer;
85                         }
86                         case 1:
87                                 // keep changes
88                                 return checkBuffer;
89                         case 2:
90                                 // cancel
91                                 return 0;
92                 }
93         }
94
95         bool const exists = filename.exists();
96         bool const tryVC = exists ? false : LyXVC::fileInVC(filename);
97         if (exists || tryVC) {
98                 if (exists) {
99                         if (!filename.isReadableFile()) {
100                                 docstring text = bformat(_("The file %1$s exists but is not "
101                                         "readable by the current user."),
102                                         from_utf8(filename.absFileName()));
103                                 Alert::error(_("File not readable!"), text);
104                                 return 0;
105                         }
106                         if (filename.extension() == "lyx" && filename.isFileEmpty()) {
107                                 // Makes it possible to open an empty (0 bytes) .lyx file
108                                 return newFile(filename.absFileName(), "", true);
109                         }
110                 }
111                 Buffer * b = theBufferList().newBuffer(filename.absFileName());
112                 if (!b) {
113                         // Buffer creation is not possible.
114                         return 0;
115                 }
116                 if (b->loadLyXFile() != Buffer::ReadSuccess) {
117                         // do not save an emergency file when releasing the buffer
118                         b->markClean();
119                         theBufferList().release(b);
120                         return 0;
121                 }
122                 return b;
123         }
124
125         docstring text = bformat(_("The document %1$s does not yet "
126                 "exist.\n\nDo you want to create a new document?"),
127                 from_utf8(filename.absFileName()));
128         if (!Alert::prompt(_("Create new document?"),
129                         text, 0, 1, _("&Create"), _("Cancel")))
130                 return newFile(filename.absFileName(), string(), true);
131
132         return 0;
133 }
134
135
136 // FIXME newFile() should probably be a member method of Application...
137 Buffer * newFile(string const & filename, string const & templatename,
138                  bool is_named)
139 {
140         // get a free buffer
141         Buffer * b = theBufferList().newBuffer(filename);
142         if (!b)
143                 // Buffer creation is not possible.
144                 return 0;
145
146         FileName tname;
147         // use defaults.lyx as a default template if it exists.
148         if (templatename.empty())
149                 tname = libFileSearch("templates", "defaults.lyx");
150         else
151                 tname = makeAbsPath(templatename);
152
153         if (!tname.empty()) {
154                 if (b->loadThisLyXFile(tname) != Buffer::ReadSuccess) {
155                         docstring const file = makeDisplayPath(tname.absFileName(), 50);
156                         docstring const text  = bformat(
157                                 _("The specified document template\n%1$s\ncould not be read."),
158                                 file);
159                         Alert::error(_("Could not read template"), text);
160                         theBufferList().release(b);
161                         return 0;
162                 }
163         }
164
165         if (is_named)
166                 // in this case, the user chose the filename, so we
167                 // assume that she really does want this file.
168                 b->markDirty();
169         else
170                 b->setUnnamed();
171
172         b->setReadonly(false);
173         b->setFullyLoaded(true);
174
175         return b;
176 }
177
178
179 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
180                                                 string const & templatename)
181 {
182         static map<string, int> file_number;
183         static Mutex mutex;
184
185         Mutex::Locker locker(&mutex);
186         FileName filename;
187
188         do {
189                 filename.set(path,
190                         prefix + convert<string>(++file_number[prefix]) + ".lyx");
191         }
192         while (theBufferList().exists(filename) || filename.isReadableFile());
193
194         return newFile(filename.absFileName(), templatename, false);
195 }
196
197
198 Buffer * loadIfNeeded(FileName const & fname)
199 {
200         Buffer * buffer = theBufferList().getBuffer(fname);
201         if (!buffer) {
202                 if (!fname.exists() && !LyXVC::fileInVC(fname))
203                         return 0;
204
205                 buffer = theBufferList().newBuffer(fname.absFileName());
206                 if (!buffer)
207                         // Buffer creation is not possible.
208                         return 0;
209
210                 if (buffer->loadLyXFile() != Buffer::ReadSuccess) {
211                         //close the buffer we just opened
212                         theBufferList().release(buffer);
213                         return 0;
214                 }
215         }
216         return buffer;
217 }
218
219
220 } // namespace lyx