+/// the type of outline operation
+enum OutlineOp {
+ OutlineUp, // Move this header with text down
+ OutlineDown, // Move this header with text up
+ OutlineIn, // Make this header deeper
+ OutlineOut // Make this header shallower
+};
+
+
+static void outline(OutlineOp mode, Cursor & cur)
+{
+ 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();
+
+ DocumentClass const & tc = buf.params().documentClass();
+
+ int const thistoclevel = start->layout().toclevel;
+ int toclevel;
+
+ // 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 != Layout::NOT_IN_TOC && toclevel <= thistoclevel) {
+ break;
+ }
+ }
+
+ switch (mode) {
+ case OutlineUp: {
+ if (start == pars.begin())
+ // Nothing to move.
+ return;
+ ParagraphList::iterator dest = start;
+ // Move out (up) from this header
+ if (dest == bgn)
+ return;
+ // Search previous same-level header above
+ do {
+ --dest;
+ toclevel = dest->layout().toclevel;
+ } while(dest != bgn
+ && (toclevel == Layout::NOT_IN_TOC
+ || toclevel > thistoclevel));
+ // Not found; do nothing
+ if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
+ return;
+ pit_type const newpit = distance(bgn, dest);
+ pit_type const len = distance(start, finish);
+ pit_type const deletepit = pit + len;
+ buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
+ pars.insert(dest, start, finish);
+ start = boost::next(pars.begin(), deletepit);
+ pit = newpit;
+ pars.erase(start, finish);
+ return;
+ }
+ case OutlineDown: {
+ if (finish == end)
+ // Nothing to move.
+ return;
+ // Go one down from *this* header:
+ ParagraphList::iterator dest = boost::next(finish, 1);
+ // Go further down to find header to insert in front of:
+ for (; dest != end; ++dest) {
+ toclevel = dest->layout().toclevel;
+ if (toclevel != Layout::NOT_IN_TOC
+ && toclevel <= thistoclevel) {
+ break;
+ }
+ }
+ // One such was found:
+ pit_type newpit = distance(bgn, dest);
+ pit_type const len = distance(start, finish);
+ buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
+ pars.insert(dest, start, finish);
+ start = boost::next(bgn, pit);
+ pit = newpit - len;
+ pars.erase(start, finish);
+ return;
+ }
+ case OutlineIn: {
+ pit_type const len = distance(start, finish);
+ buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
+ for (; start != finish; ++start) {
+ toclevel = start->layout().toclevel;
+ if (toclevel == Layout::NOT_IN_TOC)
+ continue;
+ DocumentClass::const_iterator lit = tc.begin();
+ DocumentClass::const_iterator len = tc.end();
+ for (; lit != len; ++lit) {
+ if (lit->toclevel == toclevel + 1 &&
+ start->layout().labeltype == lit->labeltype) {
+ start->setLayout(*lit);
+ break;
+ }
+ }
+ }
+ return;
+ }
+ case OutlineOut: {
+ pit_type const len = distance(start, finish);
+ buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
+ for (; start != finish; ++start) {
+ toclevel = start->layout().toclevel;
+ if (toclevel == Layout::NOT_IN_TOC)
+ continue;
+ DocumentClass::const_iterator lit = tc.begin();
+ DocumentClass::const_iterator len = tc.end();
+ for (; lit != len; ++lit) {
+ if (lit->toclevel == toclevel - 1 &&
+ start->layout().labeltype == lit->labeltype) {
+ start->setLayout(*lit);
+ break;
+ }
+ }
+ }
+ return;
+ }
+ }
+}
+
+