]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.cpp
A couple comments and some very minor cleanup.
[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                 if (checkBuffer->isClean() || acceptDirty)
68                         return checkBuffer;
69                 docstring const file = makeDisplayPath(filename.absFilename(), 20);
70                 docstring text = bformat(_(
71                                 "The document %1$s is already loaded and has unsaved changes.\n"
72                                 "Do you want to abandon your changes and reload the version on disk?"), file);
73                 if (Alert::prompt(_("Reload saved document?"),
74                                 text, 0, 1,  _("&Reload"), _("&Keep Changes")))
75                         return checkBuffer;
76
77                 // FIXME: should be LFUN_REVERT
78                 theBufferList().release(checkBuffer);
79                 // Load it again.
80                 return checkAndLoadLyXFile(filename);
81         }
82
83         if (filename.exists()) {
84                 if (!filename.isReadableFile()) {
85                         docstring text = bformat(_("The file %1$s exists but is not "
86                                 "readable by the current user."),
87                                 from_utf8(filename.absFilename()));
88                         Alert::error(_("File not readable!"), text);
89                         return 0;
90                 }
91                 Buffer * b = theBufferList().newBuffer(filename.absFilename());
92                 if (!b) {
93                         // Buffer creation is not possible.
94                         return 0;
95                 }
96                 if (!b->loadLyXFile(filename)) {
97                         theBufferList().release(b);
98                         return 0;
99                 }
100                 return b;
101         }
102
103         docstring text = bformat(_("The document %1$s does not yet "
104                 "exist.\n\nDo you want to create a new document?"),
105                 from_utf8(filename.absFilename()));
106         if (!Alert::prompt(_("Create new document?"),
107                         text, 0, 1, _("&Create"), _("Cancel")))
108                 return newFile(filename.absFilename(), string(), true);
109
110         return 0;
111 }
112
113
114 // FIXME newFile() should probably be a member method of Application...
115 Buffer * newFile(string const & filename, string const & templatename,
116                  bool const isNamed)
117 {
118         // get a free buffer
119         Buffer * b = theBufferList().newBuffer(filename);
120         if (!b)
121                 // Buffer creation is not possible.
122                 return 0;
123
124         FileName tname;
125         // use defaults.lyx as a default template if it exists.
126         if (templatename.empty())
127                 tname = libFileSearch("templates", "defaults.lyx");
128         else
129                 tname = makeAbsPath(templatename);
130
131         if (!tname.empty()) {
132                 if (!b->readFile(tname)) {
133                         docstring const file = makeDisplayPath(tname.absFilename(), 50);
134                         docstring const text  = bformat(
135                                 _("The specified document template\n%1$s\ncould not be read."),
136                                 file);
137                         Alert::error(_("Could not read template"), text);
138                         theBufferList().release(b);
139                         return 0;
140                 }
141         }
142
143         if (!isNamed) {
144                 b->setUnnamed();
145                 b->setFileName(filename);
146         }
147
148         b->setReadonly(false);
149         b->setFullyLoaded(true);
150
151         return b;
152 }
153
154
155 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
156                                                 string const & templatename)
157 {
158         static map<string, int> file_number;
159
160         FileName filename;
161
162         do {
163                 filename.set(path, 
164                         prefix + convert<string>(++file_number[prefix]) + ".lyx");
165         }
166         while (theBufferList().exists(filename) || filename.isReadableFile());
167                 
168         return newFile(filename.absFilename(), "", false);
169 }
170
171
172 /* 
173  * FIXME : merge with countChars. The structures of the two functions
174  * are similar but, unfortunately, they seem to have a different
175  * notion of what to count. Since nobody ever complained about that,
176  * this proves (again) that any number beats no number ! (JMarc)
177  */
178 int countWords(DocIterator const & from, DocIterator const & to)
179 {
180         int count = 0;
181         bool inword = false;
182         for (DocIterator dit = from ; dit != to ; ) {
183                 if (!dit.inTexted()) {
184                         dit.forwardPos();
185                         continue;
186                 }
187                 
188                 Paragraph const & par = dit.paragraph();
189                 pos_type const pos = dit.pos();
190                 
191                 // Copied and adapted from isWordSeparator() in Paragraph
192                 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
193                         Inset const * ins = par.getInset(pos);
194                         if (ins && !ins->producesOutput()) {
195                                 //skip this inset
196                                 ++dit.top().pos();
197                                 continue;
198                         }
199                         if (par.isWordSeparator(pos)) 
200                                 inword = false;
201                         else if (!inword) {
202                                 ++count;
203                                 inword = true;
204                         }
205                 }
206                 dit.forwardPos();
207         }
208
209         return count;
210 }
211
212
213 int countChars(DocIterator const & from, DocIterator const & to, 
214                bool with_blanks)
215 {
216         int chars = 0;
217         int blanks = 0;
218         for (DocIterator dit = from ; dit != to ; ) {
219                 if (!dit.inTexted()) {
220                         dit.forwardPos();
221                         continue;
222                 }
223                 
224                 Paragraph const & par = dit.paragraph();
225                 pos_type const pos = dit.pos();
226                 
227                 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
228                         if (Inset const * ins = par.getInset(pos)) {
229                                 if (!ins->producesOutput()) {
230                                         //skip this inset
231                                         ++dit.top().pos();
232                                         continue;
233                                 }
234                                 if (ins->isLetter())
235                                         ++chars;
236                                 else if (with_blanks && ins->isSpace())
237                                         ++blanks;
238                         } else {
239                                 char_type const c = par.getChar(pos);
240                                 if (isPrintableNonspace(c))
241                                         ++chars;
242                                 else if (isSpace(c) && with_blanks)
243                                         ++blanks;
244                         }
245                 }
246                 dit.forwardPos();
247         }
248
249         return chars + blanks;
250 }
251
252
253 Buffer * loadIfNeeded(FileName const & fname)
254 {
255         Buffer * buffer = theBufferList().getBuffer(fname);
256         if (!buffer) {
257                 if (!fname.exists())
258                         return 0;
259
260                 buffer = theBufferList().newBuffer(fname.absFilename());
261                 if (!buffer)
262                         // Buffer creation is not possible.
263                         return 0;
264
265                 if (!buffer->loadLyXFile(fname)) {
266                         //close the buffer we just opened
267                         theBufferList().release(buffer);
268                         return 0;
269                 }
270         }
271         return buffer;
272 }
273
274
275 } // namespace lyx