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"
25 #include "InsetIterator.h"
29 #include "LayoutPtr.h"
31 #include "TextClass.h"
32 #include "TextClassList.h"
33 #include "Paragraph.h"
34 #include "paragraph_funcs.h"
35 #include "ParagraphList.h"
36 #include "ParagraphParameters.h"
37 #include "ParIterator.h"
40 #include "TocBackend.h"
42 #include "frontends/alert.h"
44 #include "insets/InsetBibitem.h"
45 #include "insets/InsetInclude.h"
47 #include "support/debug.h"
48 #include "support/filetools.h"
49 #include "support/gettext.h"
50 #include "support/lstrings.h"
51 #include "support/lyxlib.h"
58 using support::bformat;
59 using support::FileName;
60 using support::libFileSearch;
61 using support::makeAbsPath;
62 using support::makeDisplayPath;
63 using support::onlyFilename;
64 using support::onlyPath;
66 namespace Alert = frontend::Alert;
69 Buffer * checkAndLoadLyXFile(FileName const & filename)
72 Buffer * checkBuffer = theBufferList().getBuffer(filename.absFilename());
74 if (checkBuffer->isClean())
76 docstring const file = makeDisplayPath(filename.absFilename(), 20);
77 docstring text = bformat(_(
78 "The document %1$s is already loaded and has unsaved changes.\n"
79 "Do you want to abandon your changes and reload the version on disk?"), file);
80 if (Alert::prompt(_("Reload saved document?"),
81 text, 0, 1, _("&Reload"), _("&Keep Changes")))
84 // FIXME: should be LFUN_REVERT
85 if (theBufferList().close(checkBuffer, false))
87 return checkAndLoadLyXFile(filename);
88 // The file could not be closed.
92 if (filename.isReadableFile()) {
93 Buffer * b = theBufferList().newBuffer(filename.absFilename());
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);
121 // use defaults.lyx as a default template if it exists.
122 if (templatename.empty())
123 tname = libFileSearch("templates", "defaults.lyx");
125 tname = makeAbsPath(templatename);
127 if (!tname.empty()) {
128 if (!b->readFile(tname)) {
129 docstring const file = makeDisplayPath(tname.absFilename(), 50);
130 docstring const text = bformat(
131 _("The specified document template\n%1$s\ncould not be read."),
133 Alert::error(_("Could not read template"), text);
134 theBufferList().release(b);
141 b->setFileName(filename);
144 b->setReadonly(false);
145 b->setFullyLoaded(true);
151 int countWords(DocIterator const & from, DocIterator const & to)
155 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
156 // Copied and adapted from isLetter() in ControlSpellChecker
158 && dit.pos() != dit.lastpos()
159 && dit.paragraph().isLetter(dit.pos())
160 && !dit.paragraph().isDeleted(dit.pos())) {
175 depth_type getDepth(DocIterator const & it)
177 depth_type depth = 0;
178 for (size_t i = 0 ; i < it.depth() ; ++i)
179 if (!it[i].inset().inMathed())
180 depth += it[i].paragraph().getDepth() + 1;
181 // remove 1 since the outer inset does not count
185 depth_type getItemDepth(ParIterator const & it)
187 Paragraph const & par = *it;
188 LabelType const labeltype = par.layout()->labeltype;
190 if (labeltype != LABEL_ENUMERATE && labeltype != LABEL_ITEMIZE)
193 // this will hold the lowest depth encountered up to now.
194 depth_type min_depth = getDepth(it);
195 ParIterator prev_it = it;
198 --prev_it.top().pit();
200 // start of nested inset: go to outer par
202 if (prev_it.empty()) {
203 // start of document: nothing to do
208 // We search for the first paragraph with same label
209 // that is not more deeply nested.
210 Paragraph & prev_par = *prev_it;
211 depth_type const prev_depth = getDepth(prev_it);
212 if (labeltype == prev_par.layout()->labeltype) {
213 if (prev_depth < min_depth)
214 return prev_par.itemdepth + 1;
215 if (prev_depth == min_depth)
216 return prev_par.itemdepth;
218 min_depth = std::min(min_depth, prev_depth);
219 // small optimization: if we are at depth 0, we won't
220 // find anything else
227 bool needEnumCounterReset(ParIterator const & it)
229 Paragraph const & par = *it;
230 BOOST_ASSERT(par.layout()->labeltype == LABEL_ENUMERATE);
231 depth_type const cur_depth = par.getDepth();
232 ParIterator prev_it = it;
233 while (prev_it.pit()) {
234 --prev_it.top().pit();
235 Paragraph const & prev_par = *prev_it;
236 if (prev_par.getDepth() <= cur_depth)
237 return prev_par.layout()->labeltype != LABEL_ENUMERATE;
239 // start of nested inset: reset
244 // set the label of a paragraph. This includes the counters.
245 void setLabel(Buffer const & buf, ParIterator & it)
247 TextClass const & textclass = buf.params().getTextClass();
248 Paragraph & par = it.paragraph();
249 LayoutPtr const & layout = par.layout();
250 Counters & counters = textclass.counters();
252 if (par.params().startOfAppendix()) {
253 // FIXME: only the counter corresponding to toplevel
254 // sectionning should be reset
256 counters.appendix(true);
258 par.params().appendix(counters.appendix());
260 // Compute the item depth of the paragraph
261 par.itemdepth = getItemDepth(it);
263 if (layout->margintype == MARGIN_MANUAL) {
264 if (par.params().labelWidthString().empty())
265 par.params().labelWidthString(par.translateIfPossible(layout->labelstring(), buf.params()));
267 par.params().labelWidthString(docstring());
270 switch(layout->labeltype) {
272 if (layout->toclevel <= buf.params().secnumdepth
273 && (layout->latextype != LATEX_ENVIRONMENT
274 || isFirstInSequence(it.pit(), it.plist()))) {
275 counters.step(layout->counter);
276 par.params().labelString(
277 par.expandLabel(layout, buf.params()));
279 par.params().labelString(docstring());
282 case LABEL_ITEMIZE: {
283 // At some point of time we should do something more
284 // clever here, like:
285 // par.params().labelString(
286 // buf.params().user_defined_bullet(par.itemdepth).getText());
287 // for now, use a simple hardcoded label
289 switch (par.itemdepth) {
291 itemlabel = char_type(0x2022);
294 itemlabel = char_type(0x2013);
297 itemlabel = char_type(0x2217);
300 itemlabel = char_type(0x2219); // or 0x00b7
303 par.params().labelString(itemlabel);
307 case LABEL_ENUMERATE: {
308 // FIXME: Yes I know this is a really, really! bad solution
310 docstring enumcounter = from_ascii("enum");
312 switch (par.itemdepth) {
324 // not a valid enumdepth...
328 // Maybe we have to reset the enumeration counter.
329 if (needEnumCounterReset(it))
330 counters.reset(enumcounter);
332 counters.step(enumcounter);
336 switch (par.itemdepth) {
338 format = N_("\\arabic{enumi}.");
341 format = N_("(\\alph{enumii})");
344 format = N_("\\roman{enumiii}.");
347 format = N_("\\Alph{enumiv}.");
350 // not a valid enumdepth...
354 par.params().labelString(counters.counterLabel(
355 par.translateIfPossible(from_ascii(format), buf.params())));
360 case LABEL_SENSITIVE: {
361 string const & type = counters.current_float();
362 docstring full_label;
364 full_label = buf.B_("Senseless!!! ");
366 docstring name = buf.B_(textclass.floats().getType(type).name());
367 if (counters.hasCounter(from_utf8(type))) {
368 counters.step(from_utf8(type));
369 full_label = bformat(from_ascii("%1$s %2$s:"),
371 counters.theCounter(from_utf8(type)));
373 full_label = bformat(from_ascii("%1$s #:"), name);
375 par.params().labelString(full_label);
380 par.params().labelString(docstring());
384 case LABEL_TOP_ENVIRONMENT:
385 case LABEL_CENTERED_TOP_ENVIRONMENT:
388 par.params().labelString(
389 par.translateIfPossible(layout->labelstring(),
397 void updateLabels(Buffer const & buf, ParIterator & parit)
399 BOOST_ASSERT(parit.pit() == 0);
401 depth_type maxdepth = 0;
402 pit_type const lastpit = parit.lastpit();
403 for ( ; parit.pit() <= lastpit ; ++parit.pit()) {
404 // reduce depth if necessary
405 parit->params().depth(min(parit->params().depth(), maxdepth));
406 maxdepth = parit->getMaxDepthAfter();
408 // set the counter for this paragraph
409 setLabel(buf, parit);
412 InsetList::const_iterator iit = parit->insetList().begin();
413 InsetList::const_iterator end = parit->insetList().end();
414 for (; iit != end; ++iit) {
415 parit.pos() = iit->pos;
416 iit->inset->updateLabels(buf, parit);
423 // FIXME: buf should should be const because updateLabels() modifies
424 // the contents of the paragraphs.
425 void updateLabels(Buffer const & buf, bool childonly)
427 Buffer const * const master = buf.masterBuffer();
428 // Use the master text class also for child documents
429 TextClass const & textclass = master->params().getTextClass();
432 // If this is a child document start with the master
433 if (master != &buf) {
434 updateLabels(*master);
438 // start over the counters
439 textclass.counters().reset();
442 Buffer & cbuf = const_cast<Buffer &>(buf);
444 if (buf.text().empty()) {
445 // FIXME: we don't call continue with updateLabels()
446 // here because it crashes on newly created documents.
447 // But the TocBackend needs to be initialised
448 // nonetheless so we update the tocBackend manually.
449 cbuf.tocBackend().update();
454 ParIterator parit = par_iterator_begin(buf.inset());
455 updateLabels(buf, parit);
457 cbuf.tocBackend().update();
459 cbuf.structureChanged();
463 void checkBufferStructure(Buffer & buffer, ParIterator const & par_it)
465 if (par_it->layout()->toclevel != Layout::NOT_IN_TOC) {
466 Buffer const * master = buffer.masterBuffer();
467 master->tocBackend().updateItem(par_it);
468 master->structureChanged();