]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.cpp
add onoff support for "inset-modify changetype xxx" in include inset
[lyx.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  * FIXME : merge with countChars. The structures of the two functions
173  * are similar but, unfortunately, they seem to have a different
174  * notion of what to count. Since nobody ever complained about that,
175  * this proves (again) that any number beats no number ! (JMarc)
176  */
177 int countWords(DocIterator const & from, DocIterator const & to)
178 {
179         int count = 0;
180         bool inword = false;
181         for (DocIterator dit = from ; dit != to ; ) {
182                 if (!dit.inTexted()) {
183                         dit.forwardPos();
184                         continue;
185                 }
186                 
187                 Paragraph const & par = dit.paragraph();
188                 pos_type const pos = dit.pos();
189                 
190                 // Copied and adapted from isWordSeparator() in Paragraph
191                 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
192                         Inset const * ins = par.getInset(pos);
193                         if (ins && !ins->producesOutput()) {
194                                 //skip this inset
195                                 ++dit.top().pos();
196                                 continue;
197                         }
198                         if (par.isWordSeparator(pos)) 
199                                 inword = false;
200                         else if (!inword) {
201                                 ++count;
202                                 inword = true;
203                         }
204                 }
205                 dit.forwardPos();
206         }
207
208         return count;
209 }
210
211
212 int countChars(DocIterator const & from, DocIterator const & to, 
213                bool with_blanks)
214 {
215         int chars = 0;
216         int blanks = 0;
217         for (DocIterator dit = from ; dit != to ; ) {
218                 if (!dit.inTexted()) {
219                         dit.forwardPos();
220                         continue;
221                 }
222                 
223                 Paragraph const & par = dit.paragraph();
224                 pos_type const pos = dit.pos();
225                 
226                 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
227                         if (Inset const * ins = par.getInset(pos)) {
228                                 if (!ins->producesOutput()) {
229                                         //skip this inset
230                                         ++dit.top().pos();
231                                         continue;
232                                 }
233                                 if (ins->isLetter())
234                                         ++chars;
235                                 else if (with_blanks && ins->isSpace())
236                                         ++blanks;
237                         } else {
238                                 char_type const c = par.getChar(pos);
239                                 if (isPrintableNonspace(c))
240                                         ++chars;
241                                 else if (isSpace(c) && with_blanks)
242                                         ++blanks;
243                         }
244                 }
245                 dit.forwardPos();
246         }
247
248         return chars + blanks;
249 }
250
251 } // namespace lyx