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)
63 Buffer * checkBuffer = theBufferList().getBuffer(filename);
65 if (checkBuffer->isClean())
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")))
75 // FIXME: should be LFUN_REVERT
76 theBufferList().release(checkBuffer);
78 return checkAndLoadLyXFile(filename);
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);
89 Buffer * b = theBufferList().newBuffer(filename.absFilename());
91 // Buffer creation is not possible.
94 if (!b->loadLyXFile(filename)) {
95 theBufferList().release(b);
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);
112 // FIXME newFile() should probably be a member method of Application...
113 Buffer * newFile(string const & filename, string const & templatename,
117 Buffer * b = theBufferList().newBuffer(filename);
119 // Buffer creation is not possible.
123 // use defaults.lyx as a default template if it exists.
124 if (templatename.empty())
125 tname = libFileSearch("templates", "defaults.lyx");
127 tname = makeAbsPath(templatename);
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."),
135 Alert::error(_("Could not read template"), text);
136 theBufferList().release(b);
143 b->setFileName(filename);
146 b->setReadonly(false);
147 b->setFullyLoaded(true);
153 Buffer * newUnnamedFile(string const & templatename, FileName const & path)
155 static int newfile_number;
157 FileName filename(path,
158 "newfile" + convert<string>(++newfile_number) + ".lyx");
159 while (theBufferList().exists(filename)
160 || filename.isReadableFile()) {
163 "newfile" + convert<string>(newfile_number) + ".lyx");
165 return newFile(filename.absFilename(), templatename, false);
169 int countWords(DocIterator const & from, DocIterator const & to)
173 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
174 // Copied and adapted from isLetter() in ControlSpellChecker
176 && dit.pos() != dit.lastpos()
177 && dit.paragraph().isLetter(dit.pos())
178 && !dit.paragraph().isDeleted(dit.pos())) {
191 int countChars(DocIterator const & from, DocIterator const & to, bool with_blanks)
195 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
197 if (!dit.inTexted()) continue;
198 Paragraph const & par = dit.paragraph();
199 pos_type const pos = dit.pos();
201 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
202 if (Inset const * ins = par.getInset(pos)) {
205 else if (with_blanks && ins->isSpace())
208 char_type const c = par.getChar(pos);
209 if (isPrintableNonspace(c))
211 else if (isSpace(c) && with_blanks)
217 return chars + blanks;