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