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