-Buffer * newUnnamedFile(string const & templatename, FileName const & path)
-{
- static int newfile_number;
-
- FileName filename(path,
- "newfile" + convert<string>(++newfile_number) + ".lyx");
- while (theBufferList().exists(filename)
- || filename.isReadableFile()) {
- ++newfile_number;
- filename.set(path,
- "newfile" + convert<string>(newfile_number) + ".lyx");
- }
- return newFile(filename.absFilename(), templatename, false);
-}
-
-
-int countWords(DocIterator const & from, DocIterator const & to)
-{
- int count = 0;
- bool inword = false;
- for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
- // Copied and adapted from isLetter() in ControlSpellChecker
- if (dit.inTexted()
- && dit.pos() != dit.lastpos()
- && dit.paragraph().isLetter(dit.pos())
- && !dit.paragraph().isDeleted(dit.pos())) {
- if (!inword) {
- ++count;
- inword = true;
- }
- } else if (inword)
- inword = false;
- }
-
- return count;
-}
-
-
-int countChars(DocIterator const & from, DocIterator const & to, bool with_blanks)
-{
- int chars = 0;
- int blanks = 0;
- for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
-
- if (!dit.inTexted()) continue;
- Paragraph const & par = dit.paragraph();
- pos_type const pos = dit.pos();
-
- if (pos != dit.lastpos() && !par.isDeleted(pos)) {
- if (Inset const * ins = par.getInset(pos)) {
- if (ins->isLetter())
- ++chars;
- else if (with_blanks && ins->isSpace())
- ++blanks;
- } else {
- char_type const c = par.getChar(pos);
- if (isPrintableNonspace(c))
- ++chars;
- else if (isSpace(c) && with_blanks)
- ++blanks;
- }
- }
- }
-
- return chars + blanks;
-}
-
-
-namespace {
-
-depth_type getDepth(DocIterator const & it)
-{
- depth_type depth = 0;
- for (size_t i = 0 ; i < it.depth() ; ++i)
- if (!it[i].inset().inMathed())
- depth += it[i].paragraph().getDepth() + 1;
- // remove 1 since the outer inset does not count
- return depth - 1;
-}
-
-depth_type getItemDepth(ParIterator const & it)
-{
- Paragraph const & par = *it;
- LabelType const labeltype = par.layout().labeltype;
-
- if (labeltype != LABEL_ENUMERATE && labeltype != LABEL_ITEMIZE)
- return 0;
-
- // this will hold the lowest depth encountered up to now.
- depth_type min_depth = getDepth(it);
- ParIterator prev_it = it;
- while (true) {
- if (prev_it.pit())
- --prev_it.top().pit();
- else {
- // start of nested inset: go to outer par
- prev_it.pop_back();
- if (prev_it.empty()) {
- // start of document: nothing to do
- return 0;
- }
- }
-
- // We search for the first paragraph with same label
- // that is not more deeply nested.
- Paragraph & prev_par = *prev_it;
- depth_type const prev_depth = getDepth(prev_it);
- if (labeltype == prev_par.layout().labeltype) {
- if (prev_depth < min_depth)
- return prev_par.itemdepth + 1;
- if (prev_depth == min_depth)
- return prev_par.itemdepth;
- }
- min_depth = min(min_depth, prev_depth);
- // small optimization: if we are at depth 0, we won't
- // find anything else
- if (prev_depth == 0)
- return 0;
- }
-}
-
-
-bool needEnumCounterReset(ParIterator const & it)
-{
- Paragraph const & par = *it;
- LASSERT(par.layout().labeltype == LABEL_ENUMERATE, /**/);
- depth_type const cur_depth = par.getDepth();
- ParIterator prev_it = it;
- while (prev_it.pit()) {
- --prev_it.top().pit();
- Paragraph const & prev_par = *prev_it;
- if (prev_par.getDepth() <= cur_depth)
- return prev_par.layout().labeltype != LABEL_ENUMERATE;
- }
- // start of nested inset: reset
- return true;
-}
-
-
-// set the label of a paragraph. This includes the counters.
-void setLabel(Buffer const & buf, ParIterator & it)