]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.cpp
Correction: The inset name is citation, not cite
[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 "TextClass.h"
30 #include "Paragraph.h"
31 #include "ParagraphList.h"
32 #include "ParagraphParameters.h"
33 #include "ParIterator.h"
34 #include "TexRow.h"
35 #include "Text.h"
36 #include "TocBackend.h"
37
38 #include "frontends/alert.h"
39
40 #include "insets/InsetBibitem.h"
41 #include "insets/InsetInclude.h"
42
43 #include "support/lassert.h"
44 #include "support/convert.h"
45 #include "support/debug.h"
46 #include "support/filetools.h"
47 #include "support/gettext.h"
48 #include "support/lstrings.h"
49 #include "support/textutils.h"
50
51 using namespace std;
52 using namespace lyx::support;
53
54 namespace lyx {
55
56 namespace Alert = frontend::Alert;
57
58
59 Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
60 {
61         // File already open?
62         Buffer * checkBuffer = theBufferList().getBuffer(filename);
63         if (checkBuffer) {
64                 // Sometimes (when setting the master buffer from a child)
65                 // we accept a dirty buffer right away (otherwise we'd get
66                 // an infinite loop (bug 5514).
67                 // We also accept a dirty buffer when the document has not
68                 // yet been saved to disk.
69                 if (checkBuffer->isClean() || acceptDirty || !filename.exists())
70                         return checkBuffer;
71                 docstring const file = makeDisplayPath(filename.absFileName(), 20);
72                 docstring const text = bformat(_(
73                                 "The document %1$s is already loaded and has unsaved changes.\n"
74                                 "Do you want to abandon your changes and reload the version on disk?"), file);
75                 if (!Alert::prompt(_("Reload saved document?"),
76                           text, 0, 1,  _("&Reload"), _("&Keep Changes"))) {
77                         // reload the document
78                         if (checkBuffer->reload() != Buffer::ReadSuccess)
79                                 return 0;
80                 }
81                 return checkBuffer;
82         }
83
84         if (filename.exists()) {
85                 if (!filename.isReadableFile()) {
86                         docstring text = bformat(_("The file %1$s exists but is not "
87                                 "readable by the current user."),
88                                 from_utf8(filename.absFileName()));
89                         Alert::error(_("File not readable!"), text);
90                         return 0;
91                 }
92                 Buffer * b = theBufferList().newBuffer(filename.absFileName());
93                 if (!b) {
94                         // Buffer creation is not possible.
95                         return 0;
96                 }
97                 if (b->loadLyXFile() != Buffer::ReadSuccess) {
98                         // do not save an emergency file when releasing the buffer
99                         b->markClean();
100                         theBufferList().release(b);
101                         return 0;
102                 }
103                 return b;
104         }
105
106         docstring text = bformat(_("The document %1$s does not yet "
107                 "exist.\n\nDo you want to create a new document?"),
108                 from_utf8(filename.absFileName()));
109         if (!Alert::prompt(_("Create new document?"),
110                         text, 0, 1, _("&Create"), _("Cancel")))
111                 return newFile(filename.absFileName(), string(), true);
112
113         return 0;
114 }
115
116
117 // FIXME newFile() should probably be a member method of Application...
118 Buffer * newFile(string const & filename, string const & templatename,
119                  bool is_named)
120 {
121         // get a free buffer
122         Buffer * b = theBufferList().newBuffer(filename);
123         if (!b)
124                 // Buffer creation is not possible.
125                 return 0;
126
127         FileName tname;
128         // use defaults.lyx as a default template if it exists.
129         if (templatename.empty())
130                 tname = libFileSearch("templates", "defaults.lyx");
131         else
132                 tname = makeAbsPath(templatename);
133
134         if (!tname.empty()) {
135                 if (b->loadThisLyXFile(tname) != Buffer::ReadSuccess) {
136                         docstring const file = makeDisplayPath(tname.absFileName(), 50);
137                         docstring const text  = bformat(
138                                 _("The specified document template\n%1$s\ncould not be read."),
139                                 file);
140                         Alert::error(_("Could not read template"), text);
141                         theBufferList().release(b);
142                         return 0;
143                 }
144         }
145
146         if (is_named)
147                 // in this case, the user chose the filename, so we
148                 // assume that she really does want this file.
149                 b->markDirty();
150         else
151                 b->setUnnamed();
152
153         b->setReadonly(false);
154         b->setFullyLoaded(true);
155
156         return b;
157 }
158
159
160 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
161                                                 string const & templatename)
162 {
163         static map<string, int> file_number;
164
165         FileName filename;
166
167         do {
168                 filename.set(path, 
169                         prefix + convert<string>(++file_number[prefix]) + ".lyx");
170         }
171         while (theBufferList().exists(filename) || filename.isReadableFile());
172                 
173         return newFile(filename.absFileName(), templatename, false);
174 }
175
176
177 /* 
178  * FIXME : merge with countChars. The structures of the two functions
179  * are similar but, unfortunately, they seem to have a different
180  * notion of what to count. Since nobody ever complained about that,
181  * this proves (again) that any number beats no number ! (JMarc)
182  * We have two use cases:
183  * 1. Count the words of the given range for document statistics
184  * - ignore inset content without output. (skipNoOutput == true)
185  * 2. Count the words to present a progress bar for the spell checker
186  * - has to count whole content. (skipNoOutput == false)
187  */
188 int countWords(DocIterator const & from, DocIterator const & to,
189                            bool skipNoOutput)
190 {
191         int count = 0;
192         bool inword = false;
193         
194         for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
195                 if (!dit.inTexted()) {
196                         dit.forwardPos();
197                         continue;
198                 }
199                 
200                 Paragraph const & par = dit.paragraph();
201                 pos_type const pos = dit.pos();
202
203                 // Copied and adapted from isWordSeparator() in Paragraph
204                 if (pos == dit.lastpos()) {
205                         inword = false;
206                 } else if (!par.isDeleted(pos)) {
207                         Inset const * ins = par.getInset(pos);
208                         if (ins && skipNoOutput && !ins->producesOutput()) {
209                                 // skip this inset
210                                 ++dit.top().pos();
211                                 // stop if end of range was skipped
212                                 if (!to.atEnd() && dit >= to)
213                                         break;
214                                 continue;
215                         }
216                         if (par.isWordSeparator(pos)) 
217                                 inword = false;
218                         else if (!inword) {
219                                 ++count;
220                                 inword = true;
221                         }
222                 }
223                 dit.forwardPos();
224         }
225
226         return count;
227 }
228
229
230 int countChars(DocIterator const & from, DocIterator const & to, 
231                bool with_blanks)
232 {
233         int chars = 0;
234         int blanks = 0;
235         for (DocIterator dit = from ; dit != to ; ) {
236                 if (!dit.inTexted()) {
237                         dit.forwardPos();
238                         continue;
239                 }
240                 
241                 Paragraph const & par = dit.paragraph();
242                 pos_type const pos = dit.pos();
243                 
244                 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
245                         if (Inset const * ins = par.getInset(pos)) {
246                                 if (!ins->producesOutput()) {
247                                         //skip this inset
248                                         ++dit.top().pos();
249                                         continue;
250                                 }
251                                 if (ins->isLetter())
252                                         ++chars;
253                                 else if (with_blanks && ins->isSpace())
254                                         ++blanks;
255                         } else {
256                                 char_type const c = par.getChar(pos);
257                                 if (isPrintableNonspace(c))
258                                         ++chars;
259                                 else if (isSpace(c) && with_blanks)
260                                         ++blanks;
261                         }
262                 }
263                 dit.forwardPos();
264         }
265
266         return chars + blanks;
267 }
268
269
270 Buffer * loadIfNeeded(FileName const & fname)
271 {
272         Buffer * buffer = theBufferList().getBuffer(fname);
273         if (!buffer) {
274                 if (!fname.exists())
275                         return 0;
276
277                 buffer = theBufferList().newBuffer(fname.absFileName());
278                 if (!buffer)
279                         // Buffer creation is not possible.
280                         return 0;
281
282                 if (buffer->loadLyXFile() != Buffer::ReadSuccess) {
283                         //close the buffer we just opened
284                         theBufferList().release(buffer);
285                         return 0;
286                 }
287         }
288         return buffer;
289 }
290
291
292 } // namespace lyx