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.
6 * \author Lars Gullik Bjønnes
7 * \author Alfredo Braunstein
9 * Full author contact details are available in file CREDITS.
15 #include "buffer_funcs.h"
17 #include "BufferList.h"
18 #include "BufferParams.h"
19 #include "DocIterator.h"
21 #include "ErrorList.h"
23 #include "FloatList.h"
24 #include "InsetList.h"
29 #include "TextClass.h"
30 #include "Paragraph.h"
31 #include "ParagraphList.h"
32 #include "ParagraphParameters.h"
33 #include "ParIterator.h"
36 #include "TocBackend.h"
38 #include "frontends/alert.h"
40 #include "insets/InsetBibitem.h"
41 #include "insets/InsetInclude.h"
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"
52 using namespace lyx::support;
56 namespace Alert = frontend::Alert;
59 Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
62 Buffer * checkBuffer = theBufferList().getBuffer(filename);
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())
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())
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);
92 Buffer * b = theBufferList().newBuffer(filename.absFileName());
94 // Buffer creation is not possible.
97 if (!b->loadLyXFile(filename)) {
98 // do not save an emergency file when releasing the buffer
100 theBufferList().release(b);
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);
117 // FIXME newFile() should probably be a member method of Application...
118 Buffer * newFile(string const & filename, string const & templatename,
122 Buffer * b = theBufferList().newBuffer(filename);
124 // Buffer creation is not possible.
128 // use defaults.lyx as a default template if it exists.
129 if (templatename.empty())
130 tname = libFileSearch("templates", "defaults.lyx");
132 tname = makeAbsPath(templatename);
134 if (!tname.empty()) {
135 if (!b->readFile(tname)) {
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."),
140 Alert::error(_("Could not read template"), text);
141 theBufferList().release(b);
148 b->setFileName(filename);
150 // in this case, the user chose the filename, so we assume that she
151 // really does want this file.
154 b->setReadonly(false);
155 b->setFullyLoaded(true);
161 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
162 string const & templatename)
164 static map<string, int> file_number;
170 prefix + convert<string>(++file_number[prefix]) + ".lyx");
172 while (theBufferList().exists(filename) || filename.isReadableFile());
174 return newFile(filename.absFileName(), templatename, false);
179 * FIXME : merge with countChars. The structures of the two functions
180 * are similar but, unfortunately, they seem to have a different
181 * notion of what to count. Since nobody ever complained about that,
182 * this proves (again) that any number beats no number ! (JMarc)
184 int countWords(DocIterator const & from, DocIterator const & to)
188 for (DocIterator dit = from ; dit != to ; ) {
189 if (!dit.inTexted()) {
194 Paragraph const & par = dit.paragraph();
195 pos_type const pos = dit.pos();
197 // Copied and adapted from isWordSeparator() in Paragraph
198 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
199 Inset const * ins = par.getInset(pos);
200 if (ins && !ins->producesOutput()) {
205 if (par.isWordSeparator(pos))
219 int countChars(DocIterator const & from, DocIterator const & to,
224 for (DocIterator dit = from ; dit != to ; ) {
225 if (!dit.inTexted()) {
230 Paragraph const & par = dit.paragraph();
231 pos_type const pos = dit.pos();
233 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
234 if (Inset const * ins = par.getInset(pos)) {
235 if (!ins->producesOutput()) {
242 else if (with_blanks && ins->isSpace())
245 char_type const c = par.getChar(pos);
246 if (isPrintableNonspace(c))
248 else if (isSpace(c) && with_blanks)
255 return chars + blanks;
259 Buffer * loadIfNeeded(FileName const & fname)
261 Buffer * buffer = theBufferList().getBuffer(fname);
266 buffer = theBufferList().newBuffer(fname.absFileName());
268 // Buffer creation is not possible.
271 if (!buffer->loadLyXFile(fname)) {
272 //close the buffer we just opened
273 theBufferList().release(buffer);