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"
20 #include "DocIterator.h"
22 #include "ErrorList.h"
24 #include "FloatList.h"
26 #include "InsetList.h"
27 #include "InsetIterator.h"
32 #include "lyxlayout_ptr_fwd.h"
33 #include "TextClass.h"
34 #include "TextClassList.h"
35 #include "Paragraph.h"
36 #include "paragraph_funcs.h"
37 #include "ParagraphList.h"
38 #include "ParagraphParameters.h"
39 #include "ParIterator.h"
42 #include "TocBackend.h"
44 #include "frontends/alert.h"
46 #include "insets/InsetBibitem.h"
47 #include "insets/InsetInclude.h"
49 #include "support/filetools.h"
50 #include "support/fs_extras.h"
51 #include "support/lyxlib.h"
53 #include <boost/bind.hpp>
63 using support::bformat;
64 using support::FileName;
65 using support::libFileSearch;
66 using support::makeAbsPath;
67 using support::makeDisplayPath;
68 using support::onlyFilename;
69 using support::onlyPath;
70 using support::unlink;
72 namespace Alert = frontend::Alert;
75 bool checkIfLoaded(FileName const & fn)
77 return theBufferList().getBuffer(fn.absFilename());
81 Buffer * checkAndLoadLyXFile(FileName const & filename)
84 Buffer * checkBuffer = theBufferList().getBuffer(filename.absFilename());
86 if (checkBuffer->isClean())
88 docstring const file = makeDisplayPath(filename.absFilename(), 20);
89 docstring text = bformat(_(
90 "The document %1$s is already loaded and has unsaved changes.\n"
91 "Do you want to abandon your changes and reload the version on disk?"), file);
92 if (Alert::prompt(_("Reload saved document?"),
93 text, 0, 1, _("&Reload"), _("&Keep Changes")))
96 // FIXME: should be LFUN_REVERT
97 if (theBufferList().close(checkBuffer, false))
99 return checkAndLoadLyXFile(filename);
101 // The file could not be closed.
105 if (filename.isReadable()) {
106 Buffer * b = theBufferList().newBuffer(filename.absFilename());
107 if (!b->loadLyXFile(filename)) {
108 theBufferList().release(b);
114 docstring text = bformat(_("The document %1$s does not yet "
115 "exist.\n\nDo you want to create a new document?"),
116 from_utf8(filename.absFilename()));
117 if (!Alert::prompt(_("Create new document?"),
118 text, 0, 1, _("&Create"), _("Cancel")))
119 return newFile(filename.absFilename(), string(), true);
125 // FIXME newFile() should probably be a member method of Application...
126 Buffer * newFile(string const & filename, string const & templatename,
130 Buffer * b = theBufferList().newBuffer(filename);
134 // use defaults.lyx as a default template if it exists.
135 if (templatename.empty())
136 tname = libFileSearch("templates", "defaults.lyx");
138 tname = makeAbsPath(templatename);
140 if (!tname.empty()) {
141 if (!b->readFile(tname)) {
142 docstring const file = makeDisplayPath(tname.absFilename(), 50);
143 docstring const text = bformat(
144 _("The specified document template\n%1$s\ncould not be read."),
146 Alert::error(_("Could not read template"), text);
147 theBufferList().release(b);
154 b->setFileName(filename);
157 b->setReadonly(false);
158 b->setFullyLoaded(true);
164 int countWords(DocIterator const & from, DocIterator const & to)
168 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
169 // Copied and adapted from isLetter() in ControlSpellChecker
171 && dit.pos() != dit.lastpos()
172 && dit.paragraph().isLetter(dit.pos())
173 && !dit.paragraph().isDeleted(dit.pos())) {
188 depth_type getDepth(DocIterator const & it)
190 depth_type depth = 0;
191 for (size_t i = 0 ; i < it.depth() ; ++i)
192 if (!it[i].inset().inMathed())
193 depth += it[i].paragraph().getDepth() + 1;
194 // remove 1 since the outer inset does not count
198 depth_type getItemDepth(ParIterator const & it)
200 Paragraph const & par = *it;
201 LabelType const labeltype = par.layout()->labeltype;
203 if (labeltype != LABEL_ENUMERATE && labeltype != LABEL_ITEMIZE)
206 // this will hold the lowest depth encountered up to now.
207 depth_type min_depth = getDepth(it);
208 ParIterator prev_it = it;
211 --prev_it.top().pit();
213 // start of nested inset: go to outer par
215 if (prev_it.empty()) {
216 // start of document: nothing to do
221 // We search for the first paragraph with same label
222 // that is not more deeply nested.
223 Paragraph & prev_par = *prev_it;
224 depth_type const prev_depth = getDepth(prev_it);
225 if (labeltype == prev_par.layout()->labeltype) {
226 if (prev_depth < min_depth) {
227 return prev_par.itemdepth + 1;
229 else if (prev_depth == min_depth) {
230 return prev_par.itemdepth;
233 min_depth = std::min(min_depth, prev_depth);
234 // small optimization: if we are at depth 0, we won't
235 // find anything else
236 if (prev_depth == 0) {
243 bool needEnumCounterReset(ParIterator const & it)
245 Paragraph const & par = *it;
246 BOOST_ASSERT(par.layout()->labeltype == LABEL_ENUMERATE);
247 depth_type const cur_depth = par.getDepth();
248 ParIterator prev_it = it;
249 while (prev_it.pit()) {
250 --prev_it.top().pit();
251 Paragraph const & prev_par = *prev_it;
252 if (prev_par.getDepth() <= cur_depth)
253 return prev_par.layout()->labeltype != LABEL_ENUMERATE;
255 // start of nested inset: reset
260 // set the label of a paragraph. This includes the counters.
261 void setLabel(Buffer const & buf, ParIterator & it)
263 TextClass const & textclass = buf.params().getTextClass();
264 Paragraph & par = it.paragraph();
265 LayoutPtr const & layout = par.layout();
266 Counters & counters = textclass.counters();
268 if (par.params().startOfAppendix()) {
269 // FIXME: only the counter corresponding to toplevel
270 // sectionning should be reset
272 counters.appendix(true);
274 par.params().appendix(counters.appendix());
276 // Compute the item depth of the paragraph
277 par.itemdepth = getItemDepth(it);
279 if (layout->margintype == MARGIN_MANUAL) {
280 if (par.params().labelWidthString().empty())
281 par.params().labelWidthString(par.translateIfPossible(layout->labelstring(), buf.params()));
283 par.params().labelWidthString(docstring());
286 switch(layout->labeltype) {
288 if (layout->toclevel <= buf.params().secnumdepth
289 && (layout->latextype != LATEX_ENVIRONMENT
290 || isFirstInSequence(it.pit(), it.plist()))) {
291 counters.step(layout->counter);
292 par.params().labelString(
293 par.expandLabel(layout, buf.params()));
295 par.params().labelString(docstring());
298 case LABEL_ITEMIZE: {
299 // At some point of time we should do something more
300 // clever here, like:
301 // par.params().labelString(
302 // buf.params().user_defined_bullet(par.itemdepth).getText());
303 // for now, use a simple hardcoded label
305 switch (par.itemdepth) {
307 itemlabel = char_type(0x2022);
310 itemlabel = char_type(0x2013);
313 itemlabel = char_type(0x2217);
316 itemlabel = char_type(0x2219); // or 0x00b7
319 par.params().labelString(itemlabel);
323 case LABEL_ENUMERATE: {
324 // FIXME: Yes I know this is a really, really! bad solution
326 docstring enumcounter = from_ascii("enum");
328 switch (par.itemdepth) {
340 // not a valid enumdepth...
344 // Maybe we have to reset the enumeration counter.
345 if (needEnumCounterReset(it))
346 counters.reset(enumcounter);
348 counters.step(enumcounter);
352 switch (par.itemdepth) {
354 format = N_("\\arabic{enumi}.");
357 format = N_("(\\alph{enumii})");
360 format = N_("\\roman{enumiii}.");
363 format = N_("\\Alph{enumiv}.");
366 // not a valid enumdepth...
370 par.params().labelString(counters.counterLabel(
371 par.translateIfPossible(from_ascii(format), buf.params())));
376 case LABEL_SENSITIVE: {
377 string const & type = counters.current_float();
378 docstring full_label;
380 full_label = buf.B_("Senseless!!! ");
382 docstring name = buf.B_(textclass.floats().getType(type).name());
383 if (counters.hasCounter(from_utf8(type))) {
384 counters.step(from_utf8(type));
385 full_label = bformat(from_ascii("%1$s %2$s:"),
387 counters.theCounter(from_utf8(type)));
389 full_label = bformat(from_ascii("%1$s #:"), name);
391 par.params().labelString(full_label);
396 par.params().labelString(docstring());
400 case LABEL_TOP_ENVIRONMENT:
401 case LABEL_CENTERED_TOP_ENVIRONMENT:
404 par.params().labelString(
405 par.translateIfPossible(layout->labelstring(),
413 void updateLabels(Buffer const & buf, ParIterator & parit)
415 BOOST_ASSERT(parit.pit() == 0);
417 depth_type maxdepth = 0;
418 pit_type const lastpit = parit.lastpit();
419 for ( ; parit.pit() <= lastpit ; ++parit.pit()) {
420 // reduce depth if necessary
421 parit->params().depth(min(parit->params().depth(), maxdepth));
422 maxdepth = parit->getMaxDepthAfter();
424 // set the counter for this paragraph
425 setLabel(buf, parit);
428 InsetList::const_iterator iit = parit->insetList().begin();
429 InsetList::const_iterator end = parit->insetList().end();
430 for (; iit != end; ++iit) {
431 parit.pos() = iit->pos;
432 iit->inset->updateLabels(buf, parit);
439 // FIXME: buf should should be const because updateLabels() modifies
440 // the contents of the paragraphs.
441 void updateLabels(Buffer const & buf, bool childonly)
443 Buffer const * const master = buf.masterBuffer();
444 // Use the master text class also for child documents
445 TextClass const & textclass = master->params().getTextClass();
448 // If this is a child document start with the master
449 if (master != &buf) {
450 updateLabels(*master);
454 // start over the counters
455 textclass.counters().reset();
458 Buffer & cbuf = const_cast<Buffer &>(buf);
460 if (buf.text().empty()) {
461 // FIXME: we don't call continue with updateLabels()
462 // here because it crashes on newly created documents.
463 // But the TocBackend needs to be initialised
464 // nonetheless so we update the tocBackend manually.
465 cbuf.tocBackend().update();
470 ParIterator parit = par_iterator_begin(buf.inset());
471 updateLabels(buf, parit);
473 cbuf.tocBackend().update();
475 cbuf.structureChanged();
477 // the embedding signal is emitted with structureChanged signal
478 // this is inaccurate so these two will be separated later.
479 //cbuf.embeddedFiles().update();
480 //cbuf.embeddingChanged();
484 void checkBufferStructure(Buffer & buffer, ParIterator const & par_it)
486 if (par_it->layout()->toclevel != Layout::NOT_IN_TOC) {
487 Buffer * master = buffer.masterBuffer();
488 master->tocBackend().updateItem(par_it);
489 master->structureChanged();