]> git.lyx.org Git - features.git/blob - src/buffer_funcs.cpp
move more stuff from buffer_func to Buffer
[features.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 "paragraph_funcs.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)
61 {
62         // File already open?
63         Buffer * checkBuffer = theBufferList().getBuffer(filename);
64         if (checkBuffer) {
65                 if (checkBuffer->isClean())
66                         return checkBuffer;
67                 docstring const file = makeDisplayPath(filename.absFilename(), 20);
68                 docstring text = bformat(_(
69                                 "The document %1$s is already loaded and has unsaved changes.\n"
70                                 "Do you want to abandon your changes and reload the version on disk?"), file);
71                 if (Alert::prompt(_("Reload saved document?"),
72                                 text, 0, 1,  _("&Reload"), _("&Keep Changes")))
73                         return checkBuffer;
74
75                 // FIXME: should be LFUN_REVERT
76                 theBufferList().release(checkBuffer);
77                 // Load it again.
78                 return checkAndLoadLyXFile(filename);
79         }
80
81         if (filename.exists()) {
82                 if (!filename.isReadableFile()) {
83                         docstring text = bformat(_("The file %1$s exists but is not "
84                                 "readable by the current user."),
85                                 from_utf8(filename.absFilename()));
86                         Alert::error(_("File not readable!"), text);
87                         return 0;
88                 }
89                 Buffer * b = theBufferList().newBuffer(filename.absFilename());
90                 if (!b) {
91                         // Buffer creation is not possible.
92                         return 0;
93                 }
94                 if (!b->loadLyXFile(filename)) {
95                         theBufferList().release(b);
96                         return 0;
97                 }
98                 return b;
99         }
100
101         docstring text = bformat(_("The document %1$s does not yet "
102                 "exist.\n\nDo you want to create a new document?"),
103                 from_utf8(filename.absFilename()));
104         if (!Alert::prompt(_("Create new document?"),
105                         text, 0, 1, _("&Create"), _("Cancel")))
106                 return newFile(filename.absFilename(), string(), true);
107
108         return 0;
109 }
110
111
112 // FIXME newFile() should probably be a member method of Application...
113 Buffer * newFile(string const & filename, string const & templatename,
114                  bool const isNamed)
115 {
116         // get a free buffer
117         Buffer * b = theBufferList().newBuffer(filename);
118         if (!b)
119                 // Buffer creation is not possible.
120                 return 0;
121
122         FileName tname;
123         // use defaults.lyx as a default template if it exists.
124         if (templatename.empty())
125                 tname = libFileSearch("templates", "defaults.lyx");
126         else
127                 tname = makeAbsPath(templatename);
128
129         if (!tname.empty()) {
130                 if (!b->readFile(tname)) {
131                         docstring const file = makeDisplayPath(tname.absFilename(), 50);
132                         docstring const text  = bformat(
133                                 _("The specified document template\n%1$s\ncould not be read."),
134                                 file);
135                         Alert::error(_("Could not read template"), text);
136                         theBufferList().release(b);
137                         return 0;
138                 }
139         }
140
141         if (!isNamed) {
142                 b->setUnnamed();
143                 b->setFileName(filename);
144         }
145
146         b->setReadonly(false);
147         b->setFullyLoaded(true);
148
149         return b;
150 }
151
152
153 Buffer * newUnnamedFile(string const & templatename, FileName const & path)
154 {
155         static int newfile_number;
156
157         FileName filename(path, 
158                 "newfile" + convert<string>(++newfile_number) + ".lyx");
159         while (theBufferList().exists(filename)
160                 || filename.isReadableFile()) {
161                 ++newfile_number;
162                 filename.set(path,
163                         "newfile" +     convert<string>(newfile_number) + ".lyx");
164         }
165         return newFile(filename.absFilename(), templatename, false);
166 }
167
168
169 int countWords(DocIterator const & from, DocIterator const & to)
170 {
171         int count = 0;
172         bool inword = false;
173         for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
174                 // Copied and adapted from isLetter() in ControlSpellChecker
175                 if (dit.inTexted()
176                     && dit.pos() != dit.lastpos()
177                     && dit.paragraph().isLetter(dit.pos())
178                     && !dit.paragraph().isDeleted(dit.pos())) {
179                         if (!inword) {
180                                 ++count;
181                                 inword = true;
182                         }
183                 } else if (inword)
184                         inword = false;
185         }
186
187         return count;
188 }
189
190
191 int countChars(DocIterator const & from, DocIterator const & to, bool with_blanks)
192 {
193         int chars = 0;
194         int blanks = 0;
195         for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
196
197                 if (!dit.inTexted()) continue;
198                 Paragraph const & par = dit.paragraph();
199                 pos_type const pos = dit.pos();
200
201                 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
202                         if (Inset const * ins = par.getInset(pos)) {
203                                 if (ins->isLetter())
204                                         ++chars;
205                                 else if (with_blanks && ins->isSpace())
206                                         ++blanks;
207                         } else {
208                                 char_type const c = par.getChar(pos);
209                                 if (isPrintableNonspace(c))
210                                         ++chars;
211                                 else if (isSpace(c) && with_blanks)
212                                         ++blanks;
213                         }
214                 }
215         }
216
217         return chars + blanks;
218 }
219
220 } // namespace lyx