- TocList toclist;
- if (!buf)
- return toclist;
-
- LyXTextClass const & textclass = buf->params.getLyXTextClass();
-
- ParConstIterator pit = buf->par_iterator_begin();
- ParConstIterator end = buf->par_iterator_end();
- for (; pit != end; ++pit) {
-#ifdef WITH_WARNINGS
-#warning bogus type (Lgb)
-#endif
- char const labeltype = pit->layout()->labeltype;
-
- if (labeltype >= LABEL_COUNTER_CHAPTER
- && labeltype <= LABEL_COUNTER_CHAPTER + buf->params.tocdepth) {
- // insert this into the table of contents
- const int depth = max(0, labeltype - textclass.maxcounter());
- TocItem const item(pit->id(), depth,
- pit->asString(buf, true));
- toclist["TOC"].push_back(item);
+ Buffer * buf = & cur.buffer();
+ pit_type & pit = cur.pit();
+ ParagraphList & pars = buf->text().paragraphs();
+ ParagraphList::iterator bgn = pars.begin();
+ // The first paragraph of the area to be copied:
+ ParagraphList::iterator start = boost::next(bgn, pit);
+ // The final paragraph of area to be copied:
+ ParagraphList::iterator finish = start;
+ ParagraphList::iterator end = pars.end();
+
+ LyXTextClass::const_iterator lit =
+ buf->params().getLyXTextClass().begin();
+ LyXTextClass::const_iterator const lend =
+ buf->params().getLyXTextClass().end();
+
+ int const thistoclevel = start->layout()->toclevel;
+ int toclevel;
+ switch (mode) {
+ case Up: {
+ // Move out (down) from this section header
+ if (finish != end)
+ ++finish;
+ // Seek the one (on same level) below
+ for (; finish != end; ++finish) {
+ toclevel = finish->layout()->toclevel;
+ if (toclevel != LyXLayout::NOT_IN_TOC
+ && toclevel <= thistoclevel) {
+ break;
+ }
+ }
+ ParagraphList::iterator dest = start;
+ // Move out (up) from this header
+ if (dest != bgn)
+ --dest;
+ else
+ break;
+ // Search previous same-level header above
+ for (; dest != bgn; --dest) {
+ toclevel = dest->layout()->toclevel;
+ if (toclevel != LyXLayout::NOT_IN_TOC
+ && toclevel <= thistoclevel) {
+ break;
+ }
+ }
+ // Not found; do nothing
+ if (dest == bgn)
+ break;
+ pit_type const newpit = std::distance(bgn, dest);
+ pit_type const len = std::distance(start, finish);
+ pit += len;
+ pit = std::min(pit, cur.lastpit());
+ recordUndo(cur, Undo::ATOMIC, newpit, pit);
+ pars.insert(dest, start, finish);
+ start = boost::next(bgn, pit);
+ pit = newpit;
+ pars.erase(start, finish);
+ break;