]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.cpp
Natbib authoryear uses (Ref1; Ref2) by default.
[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                 if (!Alert::prompt(_("Reload saved document?"),
77                           text, 0, 1,  _("&Reload"), _("&Keep Changes"))) {
78                         // reload the document
79                         if (checkBuffer->reload() != Buffer::ReadSuccess)
80                                 return 0;
81                 }
82                 return checkBuffer;
83         }
84
85         bool const exists = filename.exists();
86         bool const tryVC = exists ? false : LyXVC::fileInVC(filename);
87         if (exists || tryVC) {
88                 if (exists && !filename.isReadableFile()) {
89                         docstring text = bformat(_("The file %1$s exists but is not "
90                                 "readable by the current user."),
91                                 from_utf8(filename.absFileName()));
92                         Alert::error(_("File not readable!"), text);
93                         return 0;
94                 }
95                 Buffer * b = theBufferList().newBuffer(filename.absFileName());
96                 if (!b) {
97                         // Buffer creation is not possible.
98                         return 0;
99                 }
100                 if (b->loadLyXFile() != Buffer::ReadSuccess) {
101                         // do not save an emergency file when releasing the buffer
102                         b->markClean();
103                         theBufferList().release(b);
104                         return 0;
105                 }
106                 return b;
107         }
108
109         docstring text = bformat(_("The document %1$s does not yet "
110                 "exist.\n\nDo you want to create a new document?"),
111                 from_utf8(filename.absFileName()));
112         if (!Alert::prompt(_("Create new document?"),
113                         text, 0, 1, _("&Create"), _("Cancel")))
114                 return newFile(filename.absFileName(), string(), true);
115
116         return 0;
117 }
118
119
120 // FIXME newFile() should probably be a member method of Application...
121 Buffer * newFile(string const & filename, string const & templatename,
122                  bool is_named)
123 {
124         // get a free buffer
125         Buffer * b = theBufferList().newBuffer(filename);
126         if (!b)
127                 // Buffer creation is not possible.
128                 return 0;
129
130         FileName tname;
131         // use defaults.lyx as a default template if it exists.
132         if (templatename.empty())
133                 tname = libFileSearch("templates", "defaults.lyx");
134         else
135                 tname = makeAbsPath(templatename);
136
137         if (!tname.empty()) {
138                 if (b->loadThisLyXFile(tname) != Buffer::ReadSuccess) {
139                         docstring const file = makeDisplayPath(tname.absFileName(), 50);
140                         docstring const text  = bformat(
141                                 _("The specified document template\n%1$s\ncould not be read."),
142                                 file);
143                         Alert::error(_("Could not read template"), text);
144                         theBufferList().release(b);
145                         return 0;
146                 }
147         }
148
149         if (is_named)
150                 // in this case, the user chose the filename, so we
151                 // assume that she really does want this file.
152                 b->markDirty();
153         else
154                 b->setUnnamed();
155
156         b->setReadonly(false);
157         b->setFullyLoaded(true);
158
159         return b;
160 }
161
162
163 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
164                                                 string const & templatename)
165 {
166         static map<string, int> file_number;
167
168         FileName filename;
169
170         do {
171                 filename.set(path, 
172                         prefix + convert<string>(++file_number[prefix]) + ".lyx");
173         }
174         while (theBufferList().exists(filename) || filename.isReadableFile());
175                 
176         return newFile(filename.absFileName(), templatename, false);
177 }
178
179
180 Buffer * loadIfNeeded(FileName const & fname)
181 {
182         Buffer * buffer = theBufferList().getBuffer(fname);
183         if (!buffer) {
184                 if (!fname.exists() && !LyXVC::fileInVC(fname))
185                         return 0;
186
187                 buffer = theBufferList().newBuffer(fname.absFileName());
188                 if (!buffer)
189                         // Buffer creation is not possible.
190                         return 0;
191
192                 if (buffer->loadLyXFile() != Buffer::ReadSuccess) {
193                         //close the buffer we just opened
194                         theBufferList().release(buffer);
195                         return 0;
196                 }
197         }
198         return buffer;
199 }
200
201
202 } // namespace lyx