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"
31 #include "LayoutPtr.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 Buffer * checkAndLoadLyXFile(FileName const & filename)
78 Buffer * checkBuffer = theBufferList().getBuffer(filename.absFilename());
80 if (checkBuffer->isClean())
82 docstring const file = makeDisplayPath(filename.absFilename(), 20);
83 docstring text = bformat(_(
84 "The document %1$s is already loaded and has unsaved changes.\n"
85 "Do you want to abandon your changes and reload the version on disk?"), file);
86 if (Alert::prompt(_("Reload saved document?"),
87 text, 0, 1, _("&Reload"), _("&Keep Changes")))
90 // FIXME: should be LFUN_REVERT
91 if (theBufferList().close(checkBuffer, false))
93 return checkAndLoadLyXFile(filename);
94 // The file could not be closed.
98 if (filename.isReadable()) {
99 Buffer * b = theBufferList().newBuffer(filename.absFilename());
100 if (!b->loadLyXFile(filename)) {
101 theBufferList().release(b);
107 docstring text = bformat(_("The document %1$s does not yet "
108 "exist.\n\nDo you want to create a new document?"),
109 from_utf8(filename.absFilename()));
110 if (!Alert::prompt(_("Create new document?"),
111 text, 0, 1, _("&Create"), _("Cancel")))
112 return newFile(filename.absFilename(), string(), true);
118 // FIXME newFile() should probably be a member method of Application...
119 Buffer * newFile(string const & filename, string const & templatename,
123 Buffer * b = theBufferList().newBuffer(filename);
127 // use defaults.lyx as a default template if it exists.
128 if (templatename.empty())
129 tname = libFileSearch("templates", "defaults.lyx");
131 tname = makeAbsPath(templatename);
133 if (!tname.empty()) {
134 if (!b->readFile(tname)) {
135 docstring const file = makeDisplayPath(tname.absFilename(), 50);
136 docstring const text = bformat(
137 _("The specified document template\n%1$s\ncould not be read."),
139 Alert::error(_("Could not read template"), text);
140 theBufferList().release(b);
147 b->setFileName(filename);
150 b->setReadonly(false);
151 b->setFullyLoaded(true);
157 int countWords(DocIterator const & from, DocIterator const & to)
161 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
162 // Copied and adapted from isLetter() in ControlSpellChecker
164 && dit.pos() != dit.lastpos()
165 && dit.paragraph().isLetter(dit.pos())
166 && !dit.paragraph().isDeleted(dit.pos())) {
181 depth_type getDepth(DocIterator const & it)
183 depth_type depth = 0;
184 for (size_t i = 0 ; i < it.depth() ; ++i)
185 if (!it[i].inset().inMathed())
186 depth += it[i].paragraph().getDepth() + 1;
187 // remove 1 since the outer inset does not count
191 depth_type getItemDepth(ParIterator const & it)
193 Paragraph const & par = *it;
194 LabelType const labeltype = par.layout()->labeltype;
196 if (labeltype != LABEL_ENUMERATE && labeltype != LABEL_ITEMIZE)
199 // this will hold the lowest depth encountered up to now.
200 depth_type min_depth = getDepth(it);
201 ParIterator prev_it = it;
204 --prev_it.top().pit();
206 // start of nested inset: go to outer par
208 if (prev_it.empty()) {
209 // start of document: nothing to do
214 // We search for the first paragraph with same label
215 // that is not more deeply nested.
216 Paragraph & prev_par = *prev_it;
217 depth_type const prev_depth = getDepth(prev_it);
218 if (labeltype == prev_par.layout()->labeltype) {
219 if (prev_depth < min_depth) {
220 return prev_par.itemdepth + 1;
222 else if (prev_depth == min_depth) {
223 return prev_par.itemdepth;
226 min_depth = std::min(min_depth, prev_depth);
227 // small optimization: if we are at depth 0, we won't
228 // find anything else
229 if (prev_depth == 0) {
236 bool needEnumCounterReset(ParIterator const & it)
238 Paragraph const & par = *it;
239 BOOST_ASSERT(par.layout()->labeltype == LABEL_ENUMERATE);
240 depth_type const cur_depth = par.getDepth();
241 ParIterator prev_it = it;
242 while (prev_it.pit()) {
243 --prev_it.top().pit();
244 Paragraph const & prev_par = *prev_it;
245 if (prev_par.getDepth() <= cur_depth)
246 return prev_par.layout()->labeltype != LABEL_ENUMERATE;
248 // start of nested inset: reset
253 // set the label of a paragraph. This includes the counters.
254 void setLabel(Buffer const & buf, ParIterator & it)
256 TextClass const & textclass = buf.params().getTextClass();
257 Paragraph & par = it.paragraph();
258 LayoutPtr const & layout = par.layout();
259 Counters & counters = textclass.counters();
261 if (par.params().startOfAppendix()) {
262 // FIXME: only the counter corresponding to toplevel
263 // sectionning should be reset
265 counters.appendix(true);
267 par.params().appendix(counters.appendix());
269 // Compute the item depth of the paragraph
270 par.itemdepth = getItemDepth(it);
272 if (layout->margintype == MARGIN_MANUAL) {
273 if (par.params().labelWidthString().empty())
274 par.params().labelWidthString(par.translateIfPossible(layout->labelstring(), buf.params()));
276 par.params().labelWidthString(docstring());
279 switch(layout->labeltype) {
281 if (layout->toclevel <= buf.params().secnumdepth
282 && (layout->latextype != LATEX_ENVIRONMENT
283 || isFirstInSequence(it.pit(), it.plist()))) {
284 counters.step(layout->counter);
285 par.params().labelString(
286 par.expandLabel(layout, buf.params()));
288 par.params().labelString(docstring());
291 case LABEL_ITEMIZE: {
292 // At some point of time we should do something more
293 // clever here, like:
294 // par.params().labelString(
295 // buf.params().user_defined_bullet(par.itemdepth).getText());
296 // for now, use a simple hardcoded label
298 switch (par.itemdepth) {
300 itemlabel = char_type(0x2022);
303 itemlabel = char_type(0x2013);
306 itemlabel = char_type(0x2217);
309 itemlabel = char_type(0x2219); // or 0x00b7
312 par.params().labelString(itemlabel);
316 case LABEL_ENUMERATE: {
317 // FIXME: Yes I know this is a really, really! bad solution
319 docstring enumcounter = from_ascii("enum");
321 switch (par.itemdepth) {
333 // not a valid enumdepth...
337 // Maybe we have to reset the enumeration counter.
338 if (needEnumCounterReset(it))
339 counters.reset(enumcounter);
341 counters.step(enumcounter);
345 switch (par.itemdepth) {
347 format = N_("\\arabic{enumi}.");
350 format = N_("(\\alph{enumii})");
353 format = N_("\\roman{enumiii}.");
356 format = N_("\\Alph{enumiv}.");
359 // not a valid enumdepth...
363 par.params().labelString(counters.counterLabel(
364 par.translateIfPossible(from_ascii(format), buf.params())));
369 case LABEL_SENSITIVE: {
370 string const & type = counters.current_float();
371 docstring full_label;
373 full_label = buf.B_("Senseless!!! ");
375 docstring name = buf.B_(textclass.floats().getType(type).name());
376 if (counters.hasCounter(from_utf8(type))) {
377 counters.step(from_utf8(type));
378 full_label = bformat(from_ascii("%1$s %2$s:"),
380 counters.theCounter(from_utf8(type)));
382 full_label = bformat(from_ascii("%1$s #:"), name);
384 par.params().labelString(full_label);
389 par.params().labelString(docstring());
393 case LABEL_TOP_ENVIRONMENT:
394 case LABEL_CENTERED_TOP_ENVIRONMENT:
397 par.params().labelString(
398 par.translateIfPossible(layout->labelstring(),
406 void updateLabels(Buffer const & buf, ParIterator & parit)
408 BOOST_ASSERT(parit.pit() == 0);
410 depth_type maxdepth = 0;
411 pit_type const lastpit = parit.lastpit();
412 for ( ; parit.pit() <= lastpit ; ++parit.pit()) {
413 // reduce depth if necessary
414 parit->params().depth(min(parit->params().depth(), maxdepth));
415 maxdepth = parit->getMaxDepthAfter();
417 // set the counter for this paragraph
418 setLabel(buf, parit);
421 InsetList::const_iterator iit = parit->insetList().begin();
422 InsetList::const_iterator end = parit->insetList().end();
423 for (; iit != end; ++iit) {
424 parit.pos() = iit->pos;
425 iit->inset->updateLabels(buf, parit);
432 // FIXME: buf should should be const because updateLabels() modifies
433 // the contents of the paragraphs.
434 void updateLabels(Buffer const & buf, bool childonly)
436 Buffer const * const master = buf.masterBuffer();
437 // Use the master text class also for child documents
438 TextClass const & textclass = master->params().getTextClass();
441 // If this is a child document start with the master
442 if (master != &buf) {
443 updateLabels(*master);
447 // start over the counters
448 textclass.counters().reset();
451 Buffer & cbuf = const_cast<Buffer &>(buf);
453 if (buf.text().empty()) {
454 // FIXME: we don't call continue with updateLabels()
455 // here because it crashes on newly created documents.
456 // But the TocBackend needs to be initialised
457 // nonetheless so we update the tocBackend manually.
458 cbuf.tocBackend().update();
463 ParIterator parit = par_iterator_begin(buf.inset());
464 updateLabels(buf, parit);
466 cbuf.tocBackend().update();
468 cbuf.structureChanged();
472 void checkBufferStructure(Buffer & buffer, ParIterator const & par_it)
474 if (par_it->layout()->toclevel != Layout::NOT_IN_TOC) {
475 Buffer * master = buffer.masterBuffer();
476 master->tocBackend().updateItem(par_it);
477 master->structureChanged();