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 "paragraph_funcs.h"
32 #include "ParagraphList.h"
33 #include "ParagraphParameters.h"
34 #include "ParIterator.h"
37 #include "TocBackend.h"
39 #include "frontends/alert.h"
41 #include "insets/InsetBibitem.h"
42 #include "insets/InsetInclude.h"
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"
53 using namespace lyx::support;
57 namespace Alert = frontend::Alert;
60 Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
63 Buffer * checkBuffer = theBufferList().getBuffer(filename);
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)
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")))
78 // FIXME: should be LFUN_REVERT
79 theBufferList().release(checkBuffer);
81 return checkAndLoadLyXFile(filename);
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 theBufferList().release(b);
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);
115 // FIXME newFile() should probably be a member method of Application...
116 Buffer * newFile(string const & filename, string const & templatename,
120 Buffer * b = theBufferList().newBuffer(filename);
122 // Buffer creation is not possible.
126 // use defaults.lyx as a default template if it exists.
127 if (templatename.empty())
128 tname = libFileSearch("templates", "defaults.lyx");
130 tname = makeAbsPath(templatename);
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."),
138 Alert::error(_("Could not read template"), text);
139 theBufferList().release(b);
146 b->setFileName(filename);
149 b->setReadonly(false);
150 b->setFullyLoaded(true);
156 Buffer * newUnnamedFile(string const & templatename, FileName const & path)
158 static int newfile_number;
160 FileName filename(path,
161 "newfile" + convert<string>(++newfile_number) + ".lyx");
162 while (theBufferList().exists(filename)
163 || filename.isReadableFile()) {
166 "newfile" + convert<string>(newfile_number) + ".lyx");
168 return newFile(filename.absFilename(), templatename, false);
172 int countWords(DocIterator const & from, DocIterator const & to)
176 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
177 // Copied and adapted from isLetter() in ControlSpellChecker
179 && dit.pos() != dit.lastpos()
180 && dit.paragraph().isLetter(dit.pos())
181 && !dit.paragraph().isDeleted(dit.pos())) {
194 int countChars(DocIterator const & from, DocIterator const & to, bool with_blanks)
198 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
200 if (!dit.inTexted()) continue;
201 Paragraph const & par = dit.paragraph();
202 pos_type const pos = dit.pos();
204 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
205 if (Inset const * ins = par.getInset(pos)) {
208 else if (with_blanks && ins->isSpace())
211 char_type const c = par.getChar(pos);
212 if (isPrintableNonspace(c))
214 else if (isSpace(c) && with_blanks)
220 return chars + blanks;