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.
93 if (!b->loadLyXFile(filename)) {
94 theBufferList().release(b);
100 docstring text = bformat(_("The document %1$s does not yet "
101 "exist.\n\nDo you want to create a new document?"),
102 from_utf8(filename.absFilename()));
103 if (!Alert::prompt(_("Create new document?"),
104 text, 0, 1, _("&Create"), _("Cancel")))
105 return newFile(filename.absFilename(), string(), true);
111 // FIXME newFile() should probably be a member method of Application...
112 Buffer * newFile(string const & filename, string const & templatename,
116 Buffer * b = theBufferList().newBuffer(filename);
118 // Buffer creation is not possible.
122 // use defaults.lyx as a default template if it exists.
123 if (templatename.empty())
124 tname = libFileSearch("templates", "defaults.lyx");
126 tname = makeAbsPath(templatename);
128 if (!tname.empty()) {
129 if (!b->readFile(tname)) {
130 docstring const file = makeDisplayPath(tname.absFilename(), 50);
131 docstring const text = bformat(
132 _("The specified document template\n%1$s\ncould not be read."),
134 Alert::error(_("Could not read template"), text);
135 theBufferList().release(b);
142 b->setFileName(filename);
145 b->setReadonly(false);
146 b->setFullyLoaded(true);
152 Buffer * newUnnamedFile(string const & templatename, FileName const & path)
154 static int newfile_number;
156 FileName filename(path,
157 "newfile" + convert<string>(++newfile_number) + ".lyx");
158 while (theBufferList().exists(filename)
159 || filename.isReadableFile()) {
162 "newfile" + convert<string>(newfile_number) + ".lyx");
164 return newFile(filename.absFilename(), templatename, false);
168 int countWords(DocIterator const & from, DocIterator const & to)
172 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
173 // Copied and adapted from isLetter() in ControlSpellChecker
175 && dit.pos() != dit.lastpos()
176 && dit.paragraph().isLetter(dit.pos())
177 && !dit.paragraph().isDeleted(dit.pos())) {
190 int countChars(DocIterator const & from, DocIterator const & to, bool with_blanks)
194 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
196 if (!dit.inTexted()) continue;
197 Paragraph const & par = dit.paragraph();
198 pos_type const pos = dit.pos();
200 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
201 if (Inset const * ins = par.getInset(pos)) {
204 else if (with_blanks && ins->isSpace())
207 char_type const c = par.getChar(pos);
208 if (isPrintableNonspace(c))
210 else if (isSpace(c) && with_blanks)
216 return chars + blanks;
222 depth_type getDepth(DocIterator const & it)
224 depth_type depth = 0;
225 for (size_t i = 0 ; i < it.depth() ; ++i)
226 if (!it[i].inset().inMathed())
227 depth += it[i].paragraph().getDepth() + 1;
228 // remove 1 since the outer inset does not count
232 depth_type getItemDepth(ParIterator const & it)
234 Paragraph const & par = *it;
235 LabelType const labeltype = par.layout().labeltype;
237 if (labeltype != LABEL_ENUMERATE && labeltype != LABEL_ITEMIZE)
240 // this will hold the lowest depth encountered up to now.
241 depth_type min_depth = getDepth(it);
242 ParIterator prev_it = it;
245 --prev_it.top().pit();
247 // start of nested inset: go to outer par
249 if (prev_it.empty()) {
250 // start of document: nothing to do
255 // We search for the first paragraph with same label
256 // that is not more deeply nested.
257 Paragraph & prev_par = *prev_it;
258 depth_type const prev_depth = getDepth(prev_it);
259 if (labeltype == prev_par.layout().labeltype) {
260 if (prev_depth < min_depth)
261 return prev_par.itemdepth + 1;
262 if (prev_depth == min_depth)
263 return prev_par.itemdepth;
265 min_depth = min(min_depth, prev_depth);
266 // small optimization: if we are at depth 0, we won't
267 // find anything else
274 bool needEnumCounterReset(ParIterator const & it)
276 Paragraph const & par = *it;
277 LASSERT(par.layout().labeltype == LABEL_ENUMERATE, /**/);
278 depth_type const cur_depth = par.getDepth();
279 ParIterator prev_it = it;
280 while (prev_it.pit()) {
281 --prev_it.top().pit();
282 Paragraph const & prev_par = *prev_it;
283 if (prev_par.getDepth() <= cur_depth)
284 return prev_par.layout().labeltype != LABEL_ENUMERATE;
286 // start of nested inset: reset
291 // set the label of a paragraph. This includes the counters.
292 void setLabel(Buffer const & buf, ParIterator & it)
294 BufferParams const & bp = buf.masterBuffer()->params();
295 DocumentClass const & textclass = bp.documentClass();
296 Paragraph & par = it.paragraph();
297 Layout const & layout = par.layout();
298 Counters & counters = textclass.counters();
300 if (par.params().startOfAppendix()) {
301 // FIXME: only the counter corresponding to toplevel
302 // sectionning should be reset
304 counters.appendix(true);
306 par.params().appendix(counters.appendix());
308 // Compute the item depth of the paragraph
309 par.itemdepth = getItemDepth(it);
311 if (layout.margintype == MARGIN_MANUAL) {
312 if (par.params().labelWidthString().empty())
313 par.params().labelWidthString(par.translateIfPossible(layout.labelstring(), bp));
315 par.params().labelWidthString(docstring());
318 switch(layout.labeltype) {
320 if (layout.toclevel <= bp.secnumdepth
321 && (layout.latextype != LATEX_ENVIRONMENT
322 || isFirstInSequence(it.pit(), it.plist()))) {
323 counters.step(layout.counter);
324 par.params().labelString(
325 par.expandLabel(layout, bp));
327 par.params().labelString(docstring());
330 case LABEL_ITEMIZE: {
331 // At some point of time we should do something more
332 // clever here, like:
333 // par.params().labelString(
334 // bp.user_defined_bullet(par.itemdepth).getText());
335 // for now, use a simple hardcoded label
337 switch (par.itemdepth) {
339 itemlabel = char_type(0x2022);
342 itemlabel = char_type(0x2013);
345 itemlabel = char_type(0x2217);
348 itemlabel = char_type(0x2219); // or 0x00b7
351 par.params().labelString(itemlabel);
355 case LABEL_ENUMERATE: {
356 // FIXME: Yes I know this is a really, really! bad solution
358 docstring enumcounter = from_ascii("enum");
360 switch (par.itemdepth) {
372 // not a valid enumdepth...
376 // Maybe we have to reset the enumeration counter.
377 if (needEnumCounterReset(it))
378 counters.reset(enumcounter);
380 counters.step(enumcounter);
384 switch (par.itemdepth) {
386 format = N_("\\arabic{enumi}.");
389 format = N_("(\\alph{enumii})");
392 format = N_("\\roman{enumiii}.");
395 format = N_("\\Alph{enumiv}.");
398 // not a valid enumdepth...
402 par.params().labelString(counters.counterLabel(
403 par.translateIfPossible(from_ascii(format), bp)));
408 case LABEL_SENSITIVE: {
409 string const & type = counters.current_float();
410 docstring full_label;
412 full_label = buf.B_("Senseless!!! ");
414 docstring name = buf.B_(textclass.floats().getType(type).name());
415 if (counters.hasCounter(from_utf8(type))) {
416 counters.step(from_utf8(type));
417 full_label = bformat(from_ascii("%1$s %2$s:"),
419 counters.theCounter(from_utf8(type)));
421 full_label = bformat(from_ascii("%1$s #:"), name);
423 par.params().labelString(full_label);
428 par.params().labelString(docstring());
432 case LABEL_TOP_ENVIRONMENT:
433 case LABEL_CENTERED_TOP_ENVIRONMENT:
436 par.params().labelString(
437 par.translateIfPossible(layout.labelstring(), bp));
444 void updateLabels(Buffer const & buf, ParIterator & parit)
446 LASSERT(parit.pit() == 0, /**/);
448 // set the position of the text in the buffer to be able
449 // to resolve macros in it. This has nothing to do with
450 // labels, but by putting it here we avoid implementing
451 // a whole bunch of traversal routines just for this call.
452 parit.text()->setMacrocontextPosition(parit);
454 depth_type maxdepth = 0;
455 pit_type const lastpit = parit.lastpit();
456 for ( ; parit.pit() <= lastpit ; ++parit.pit()) {
457 // reduce depth if necessary
458 parit->params().depth(min(parit->params().depth(), maxdepth));
459 maxdepth = parit->getMaxDepthAfter();
461 // set the counter for this paragraph
462 setLabel(buf, parit);
465 InsetList::const_iterator iit = parit->insetList().begin();
466 InsetList::const_iterator end = parit->insetList().end();
467 for (; iit != end; ++iit) {
468 parit.pos() = iit->pos;
469 iit->inset->updateLabels(parit);
475 // FIXME: buf should should be const because updateLabels() modifies
476 // the contents of the paragraphs.
477 void updateLabels(Buffer const & buf, bool childonly)
479 Buffer const * const master = buf.masterBuffer();
480 // Use the master text class also for child documents
481 DocumentClass const & textclass = master->params().documentClass();
484 // If this is a child document start with the master
485 if (master != &buf) {
486 updateLabels(*master);
490 // start over the counters
491 textclass.counters().reset();
492 buf.clearReferenceCache();
493 buf.inset().setBuffer(const_cast<Buffer &>(buf));
497 Buffer & cbuf = const_cast<Buffer &>(buf);
499 BOOST_ASSERT(!buf.text().paragraphs().empty());
502 ParIterator parit = par_iterator_begin(buf.inset());
503 updateLabels(buf, parit);
506 // TocBackend update will be done later.
509 cbuf.tocBackend().update();
511 cbuf.structureChanged();