]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettext.C
Alfredo's double-dialog fix
[lyx.git] / src / insets / insettext.C
index 03c2764251af6248e7c5ea7243f1646f562a618e..e24e992051a4137bbda81690911caa01a45287d2 100644 (file)
@@ -1,11 +1,11 @@
-/* This file is part of
- * ======================================================
+/**
+ * \file insettext.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *           LyX, The Document Processor
+ * \author Jürgen Vigna
  *
- *           Copyright 1998-2001 The LyX Team.
- *
- * ======================================================
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
@@ -37,6 +37,8 @@
 #include "trans_mgr.h"
 #include "undo_funcs.h"
 #include "WordLangTuple.h"
+#include "paragraph_funcs.h"
+#include "sgml.h"
 
 #include "frontends/Alert.h"
 #include "frontends/Dialogs.h"
@@ -50,6 +52,8 @@
 #include "support/lstrings.h"
 #include "support/lyxalgo.h" // lyx::count
 
+#include <boost/bind.hpp>
+
 #include <fstream>
 #include <algorithm>
 #include <cstdlib>
@@ -63,6 +67,7 @@ using std::max;
 using std::make_pair;
 using std::vector;
 using std::pair;
+using std::for_each;
 
 using lyx::pos_type;
 using lyx::textclass_type;
@@ -77,14 +82,14 @@ extern int greek_kb_flag;
 void InsetText::saveLyXTextState(LyXText * t) const
 {
        // check if my paragraphs are still valid
-       Paragraph * p = &*(paragraphs.begin());
-       while (p) {
-               if (p == t->cursor.par())
+       ParagraphList::iterator it = paragraphs.begin();
+       ParagraphList::iterator end = paragraphs.end();
+       for (; it != end; ++it) {
+               if (&*it == t->cursor.par())
                        break;
-               p = p->next();
        }
 
-       if (p && t->cursor.pos() <= p->size()) {
+       if (it != end && t->cursor.pos() <= it->size()) {
                sstate.lpar = t->cursor.par();
                sstate.pos = t->cursor.pos();
                sstate.boundary = t->cursor.boundary();
@@ -170,11 +175,9 @@ void InsetText::init(InsetText const * ins, bool same_id)
                if (same_id)
                        id_ = ins->id_;
        } else {
-               Paragraph * p = &*(paragraphs.begin());
-               while (p) {
-                       p->setInsetOwner(this);
-                       p = p->next();
-               }
+               for_each(paragraphs.begin(), paragraphs.end(),
+                        boost::bind(&Paragraph::setInsetOwner, _1, this));
+
                the_locking_inset = 0;
                drawFrame_ = NEVER;
                frame_color = LColor::insetframe;
@@ -201,10 +204,6 @@ void InsetText::init(InsetText const * ins, bool same_id)
 
 InsetText::~InsetText()
 {
-       cached_bview = 0;
-
-       // NOTE
-
        paragraphs.clear();
 }
 
@@ -239,7 +238,12 @@ void InsetText::write(Buffer const * buf, ostream & os) const
 
 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
 {
-       paragraphs.begin()->writeFile(buf, os, buf->params, 0);
+       ParagraphList::iterator it = paragraphs.begin();
+       ParagraphList::iterator end = paragraphs.end();
+       Paragraph::depth_type dth = 0;
+       for (; it != end; ++it) {
+               it->write(buf, os, buf->params, dth);
+       }
 }
 
 
@@ -259,14 +263,11 @@ void InsetText::read(Buffer const * buf, LyXLex & lex)
                if (token.empty())
                        continue;
                if (token == "\\end_inset") {
-#ifndef NO_COMPABILITY
-                       const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
-#endif
                        break;
                }
 
                Paragraph * tmp = &*(paragraphs.begin());
-               
+
                if (const_cast<Buffer*>(buf)->
                        parseSingleLyXformat2Token(lex, tmp, return_par,
                                                   token, pos, depth, font)) {
@@ -274,6 +275,7 @@ void InsetText::read(Buffer const * buf, LyXLex & lex)
                        lex.printError("\\the_end read in inset! Error in document!");
                        return;
                }
+               paragraphs.set(tmp);
        }
        if (!return_par)
                return_par = &*(paragraphs.begin());
@@ -741,11 +743,12 @@ void InsetText::edit(BufferView * bv, bool front)
        if (front)
                lt->setCursor(bv, &*(paragraphs.begin()), 0);
        else {
-               Paragraph * p = &*(paragraphs.begin());
-               while (p->next())
-                       p = p->next();
+               ParagraphList::iterator it = paragraphs.begin();
+               ParagraphList::iterator end = paragraphs.end();
+               while (boost::next(it) != end)
+                       ++it;
 //             int const pos = (p->size() ? p->size()-1 : p->size());
-               lt->setCursor(bv, p, p->size());
+               lt->setCursor(bv, &*it, it->size());
        }
        lt->clearSelection();
        finishUndo();
@@ -985,29 +988,29 @@ bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
 }
 
 
-void InsetText::insetButtonPress(BufferView * bv,
-       int x, int y, mouse_button::state button)
+void InsetText::lfunMousePress(FuncRequest const & cmd)
 {
        no_selection = true;
 
        // use this to check mouse motion for selection!
-       mouse_x = x;
-       mouse_y = y;
+       mouse_x = cmd.x;
+       mouse_y = cmd.y;
 
+       BufferView * bv = cmd.view();
+       FuncRequest cmd1 = cmd;
+       cmd1.x -= inset_x;
+       cmd1.y -= inset_y;
        if (!locked)
                lockInset(bv);
 
-       int tmp_x = x - drawTextXOffset;
-       int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
-       Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
+       int tmp_x = cmd.x - drawTextXOffset;
+       int tmp_y = cmd.y + insetAscent - getLyXText(bv)->first_y;
+       Inset * inset = getLyXText(bv)->checkInsetHit(bv, tmp_x, tmp_y);
 
        hideInsetCursor(bv);
        if (the_locking_inset) {
                if (the_locking_inset == inset) {
-                       the_locking_inset->insetButtonPress(bv,
-                                                           x - inset_x,
-                                                           y - inset_y,
-                                                           button);
+                       the_locking_inset->localDispatch(cmd1);
                        return;
                }
 #if 0
@@ -1017,8 +1020,7 @@ void InsetText::insetButtonPress(BufferView * bv,
                        inset_x = cix(bv) - top_x + drawTextXOffset;
                        inset_y = ciy(bv) + drawTextYOffset;
                        the_locking_inset = 0;
-                       inset->insetButtonPress(bv, x - inset_x,
-                                               y - inset_y, button);
+                       inset->localDispatch(cmd1);
 //                     inset->edit(bv, x - inset_x, y - inset_y, button);
                        if (the_locking_inset)
                                updateLocal(bv, CURSOR, false);
@@ -1040,7 +1042,7 @@ void InsetText::insetButtonPress(BufferView * bv,
                        if (!bv->lockInset(uinset)) {
                                lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
                        }
-                       inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
+                       inset->localDispatch(cmd1);
                        if (the_locking_inset)
                                updateLocal(bv, CURSOR, false);
                        return;
@@ -1048,7 +1050,7 @@ void InsetText::insetButtonPress(BufferView * bv,
        }
        if (!inset) { // && (button == mouse_button::button2)) {
                bool paste_internally = false;
-               if ((button == mouse_button::button2) && getLyXText(bv)->selection.set()) {
+               if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
                        localDispatch(FuncRequest(bv, LFUN_COPY));
                        paste_internally = true;
                }
@@ -1059,8 +1061,8 @@ void InsetText::insetButtonPress(BufferView * bv,
                }
                int old_first_y = lt->first_y;
 
-               lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
-                                            y + insetAscent);
+               lt->setCursorFromCoordinates(bv, cmd.x - drawTextXOffset,
+                                            cmd.y + insetAscent);
                // set the selection cursor!
                lt->selection.cursor = lt->cursor;
                lt->cursor.x_fix(lt->cursor.x());
@@ -1086,7 +1088,7 @@ void InsetText::insetButtonPress(BufferView * bv,
                // Insert primary selection with middle mouse
                // if there is a local selection in the current buffer,
                // insert this
-               if (button == mouse_button::button2) {
+               if (cmd.button() == mouse_button::button2) {
                        if (paste_internally)
                                localDispatch(FuncRequest(bv, LFUN_PASTE));
                        else
@@ -1099,30 +1101,33 @@ void InsetText::insetButtonPress(BufferView * bv,
 }
 
 
-bool InsetText::insetButtonRelease(BufferView * bv,
-       int x, int y, mouse_button::state button)
+bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
 {
+       BufferView * bv = cmd.view();
+       FuncRequest cmd1 = cmd;
+       cmd1.x -= inset_x;
+       cmd1.y -= inset_y;
+
        no_selection = true;
-       if (the_locking_inset) {
-               return the_locking_inset->insetButtonRelease(bv,
-                                                            x - inset_x, y - inset_y,
-                                                            button);
-       }
-       int tmp_x = x - drawTextXOffset;
-       int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
-       Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
+       if (the_locking_inset)
+               return the_locking_inset->localDispatch(cmd1);
+
+       int tmp_x = cmd.x - drawTextXOffset;
+       int tmp_y = cmd.y + insetAscent - getLyXText(bv)->first_y;
+       Inset * inset = getLyXText(bv)->checkInsetHit(bv, tmp_x, tmp_y);
        bool ret = false;
        if (inset) {
-               if (isHighlyEditableInset(inset)) {
-                       ret = inset->insetButtonRelease(bv, x - inset_x,
-                                                       y - inset_y, button);
-               } else {
+               if (isHighlyEditableInset(inset))
+                       ret = inset->localDispatch(cmd1);
+               else {
                        inset_x = cix(bv) - top_x + drawTextXOffset;
                        inset_y = ciy(bv) + drawTextYOffset;
-                       ret = inset->insetButtonRelease(bv, x - inset_x,
-                                                       y - inset_y, button);
-                       inset->edit(bv, x - inset_x,
-                                   y - inset_y, button);
+                       cmd1.x = cmd.x - inset_x;
+                       cmd1.y = cmd.x - inset_y;
+// note that we should do ret = inset->localDispatch(cmd1) 
+// and fix this instead (Alfredo);
+                       ret = true;
+                       inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
                }
                updateLocal(bv, CURSOR_PAR, false);
        }
@@ -1130,17 +1135,21 @@ bool InsetText::insetButtonRelease(BufferView * bv,
 }
 
 
-void InsetText::insetMotionNotify(BufferView * bv, int x, int y, mouse_button::state state)
+void InsetText::lfunMouseMotion(FuncRequest const & cmd)
 {
+       FuncRequest cmd1 = cmd;
+       cmd1.x -= inset_x;
+       cmd1.y -= inset_y;
+
        if (the_locking_inset) {
-               the_locking_inset->insetMotionNotify(bv, x - inset_x,
-                                                    y - inset_y,state);
+               the_locking_inset->localDispatch(cmd1);
                return;
        }
 
-       if (no_selection || ((mouse_x == x) && (mouse_y == y)))
+       if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
                return;
 
+       BufferView * bv = cmd.view();
        bool clear = false;
        if (!lt) {
                lt = getLyXText(bv);
@@ -1148,7 +1157,8 @@ void InsetText::insetMotionNotify(BufferView * bv, int x, int y, mouse_button::s
        }
        hideInsetCursor(bv);
        LyXCursor cur = lt->cursor;
-       lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
+       lt->setCursorFromCoordinates
+               (bv, cmd.x - drawTextXOffset, cmd.y + insetAscent);
        lt->cursor.x_fix(lt->cursor.x());
        if (cur == lt->cursor) {
                if (clear)
@@ -1167,13 +1177,25 @@ void InsetText::insetMotionNotify(BufferView * bv, int x, int y, mouse_button::s
 }
 
 
-UpdatableInset::RESULT
-InsetText::localDispatch(FuncRequest const & ev)
+Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
 {
        BufferView * bv = ev.view();
+       switch (ev.action) {
+               case LFUN_MOUSE_PRESS:
+                       lfunMousePress(ev);
+                       return DISPATCHED;
+               case LFUN_MOUSE_MOTION:
+                       lfunMouseMotion(ev);
+                       return DISPATCHED;
+               case LFUN_MOUSE_RELEASE:
+                       return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
+               default:
+                       break;
+       }
+
        bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
        no_selection = false;
-       RESULT result= UpdatableInset::localDispatch(ev);
+       RESULT result = UpdatableInset::localDispatch(ev);
        if (result != UNDISPATCHED)
                return DISPATCHED;
 
@@ -1234,6 +1256,7 @@ InsetText::localDispatch(FuncRequest const & ev)
        int updwhat = 0;
        int updflag = false;
        switch (ev.action) {
+
        // Normal chars
        case LFUN_SELFINSERT:
                if (bv->buffer()->isReadonly()) {
@@ -1246,9 +1269,11 @@ InsetText::localDispatch(FuncRequest const & ev)
                         * typed in now. Depends on lyxrc settings
                         * "auto_region_delete", which defaults to
                         * true (on). */
-
+#if 0
+                       // This should not be needed here and is also WRONG!
                        setUndo(bv, Undo::INSERT,
                                lt->cursor.par(), lt->cursor.par()->next());
+#endif
                        bv->switchKeyMap();
                        if (lyxrc.auto_region_delete) {
                                if (lt->selection.set()) {
@@ -1320,9 +1345,25 @@ InsetText::localDispatch(FuncRequest const & ev)
                lt->cursorEnd(bv);
                updwhat = CURSOR;
                break;
+       case LFUN_PRIOR:
+               if (!crow(bv)->previous())
+                       result = FINISHED_UP;
+               else {
+                       lt->cursorPrevious(bv);
+                       result = DISPATCHED_NOUPDATE;
+               }
+               updwhat = CURSOR;
+               break;
+       case LFUN_NEXT:
+               if (!crow(bv)->next())
+                       result = FINISHED_DOWN;
+               else {
+                       lt->cursorNext(bv);
+                       result = DISPATCHED_NOUPDATE;
+               }
+               updwhat = CURSOR;
+               break;
        case LFUN_BACKSPACE: {
-               setUndo(bv, Undo::DELETE,
-                       lt->cursor.par(), lt->cursor.par()->next());
                if (lt->selection.set())
                        lt->cutSelection(bv, true, false);
                else
@@ -1333,8 +1374,6 @@ InsetText::localDispatch(FuncRequest const & ev)
        break;
 
        case LFUN_DELETE: {
-               setUndo(bv, Undo::DELETE,
-                       lt->cursor.par(), lt->cursor.par()->next());
                if (lt->selection.set()) {
                        lt->cutSelection(bv, true, false);
                } else {
@@ -1346,8 +1385,6 @@ InsetText::localDispatch(FuncRequest const & ev)
        break;
 
        case LFUN_CUT: {
-               setUndo(bv, Undo::DELETE,
-                       lt->cursor.par(), lt->cursor.par()->next());
                lt->cutSelection(bv);
                updwhat = CURSOR_PAR;
                updflag = true;
@@ -1387,8 +1424,11 @@ InsetText::localDispatch(FuncRequest const & ev)
                                break;
                        }
                }
+#if 0
+               // This should not be needed here and is also WRONG!
                setUndo(bv, Undo::INSERT,
                        lt->cursor.par(), lt->cursor.par()->next());
+#endif
                lt->pasteSelection(bv);
                // bug 393
                lt->clearSelection();
@@ -1421,8 +1461,11 @@ InsetText::localDispatch(FuncRequest const & ev)
                        result = DISPATCHED;
                        break;
                }
+#if 0
+               // This should not be needed here and is also WRONG!
                setUndo(bv, Undo::INSERT,
                        lt->cursor.par(), lt->cursor.par()->next());
+#endif
                lt->insertChar(bv, Paragraph::META_NEWLINE);
                updwhat = CURSOR | CURSOR_PAR;
                updflag = true;
@@ -1542,24 +1585,27 @@ InsetText::localDispatch(FuncRequest const & ev)
 }
 
 
-int InsetText::latex(Buffer const * buf, ostream & os, bool moving_arg, bool) const
+int InsetText::latex(Buffer const * buf, ostream & os,
+                    bool moving_arg, bool) const
 {
        TexRow texrow;
-       buf->latexParagraphs(os, &*(paragraphs.begin()), 0, texrow, moving_arg);
+       buf->latexParagraphs(os, &*(paragraphs.begin()), 0,
+                            texrow, moving_arg);
        return texrow.rows();
 }
 
 
 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
 {
-       Paragraph * p = &*(paragraphs.begin());
        unsigned int lines = 0;
 
-       while (p) {
-               string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
+       ParagraphList::iterator beg = paragraphs.begin();
+       ParagraphList::iterator end = paragraphs.end();
+       ParagraphList::iterator it = beg;
+       for (; it != end; ++it) {
+               string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
                lines += lyx::count(tmp.begin(), tmp.end(), '\n');
                os << tmp;
-               p = p->next();
        }
        return lines;
 }
@@ -1588,11 +1634,11 @@ int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
                for (; depth > p->params().depth(); --depth) {
                        if (environment_inner[depth] != "!-- --") {
                                item_name = "listitem";
-                               lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
+                               lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
                                if (environment_inner[depth] == "varlistentry")
-                                       lines += buf->sgmlCloseTag(os, depth+command_depth, mixcont, environment_inner[depth]);
+                                       lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
                        }
-                       lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
+                       lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
                        environment_stack[depth].erase();
                        environment_inner[depth].erase();
                }
@@ -1602,12 +1648,12 @@ int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
                   && !environment_stack[depth].empty()) {
                        if (environment_inner[depth] != "!-- --") {
                                item_name= "listitem";
-                               lines += buf->sgmlCloseTag(os, command_depth+depth, mixcont, item_name);
+                               lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
                                if (environment_inner[depth] == "varlistentry")
-                                       lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
+                                       lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
                        }
 
-                       lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
+                       lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
 
                        environment_stack[depth].erase();
                        environment_inner[depth].erase();
@@ -1616,7 +1662,7 @@ int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
                // Write opening SGML tags.
                switch (style->latextype) {
                case LATEX_PARAGRAPH:
-                       lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
+                       lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
                        break;
 
                case LATEX_COMMAND:
@@ -1638,13 +1684,13 @@ int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
                                }
                                environment_stack[depth] = style->latexname();
                                environment_inner[depth] = "!-- --";
-                               lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, environment_stack[depth]);
+                               lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
                        } else {
                                if (environment_inner[depth] != "!-- --") {
                                        item_name= "listitem";
-                                       lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
+                                       lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
                                        if (environment_inner[depth] == "varlistentry")
-                                               lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
+                                               lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
                                }
                        }
 
@@ -1653,22 +1699,22 @@ int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
                                        if (style->latexparam() == "CDATA")
                                                os << "<![CDATA[";
                                        else
-                                         lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexparam());
+                                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
                                }
                                break;
                        }
 
                        desc_on = (style->labeltype == LABEL_MANUAL);
 
-                       environment_inner[depth] = desc_on?"varlistentry":"listitem";
-                       lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
+                       environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
+                       lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
 
-                       item_name = desc_on?"term":"para";
-                       lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, item_name);
+                       item_name = desc_on ? "term" : "para";
+                       lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
 
                        break;
                default:
-                       lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
+                       lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
                        break;
                }
 
@@ -1683,19 +1729,19 @@ int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
                                if (style->latexparam() == "CDATA")
                                        os << "]]>";
                                else
-                                       lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexparam());
+                                       lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
                        }
                        break;
                case LATEX_ITEM_ENVIRONMENT:
                        if (desc_on == 1) break;
                        end_tag= "para";
-                       lines += buf->sgmlCloseTag(os, depth + 1 + command_depth, mixcont, end_tag);
+                       lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
                        break;
                case LATEX_PARAGRAPH:
-                       lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
+                       lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
                        break;
                default:
-                       lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
+                       lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
                        break;
                }
        }
@@ -1705,12 +1751,12 @@ int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
                if (!environment_stack[depth].empty()) {
                        if (environment_inner[depth] != "!-- --") {
                                item_name = "listitem";
-                               lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
+                               lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
                               if (environment_inner[depth] == "varlistentry")
-                                      lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
+                                      lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
                        }
 
-                       lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
+                       lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
                }
        }
 
@@ -1720,11 +1766,8 @@ int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
 
 void InsetText::validate(LaTeXFeatures & features) const
 {
-       Paragraph * p = &*(paragraphs.begin());
-       while (p) {
-               p->validate(features);
-               p = p->next();
-       }
+       for_each(paragraphs.begin(), paragraphs.end(),
+                boost::bind(&Paragraph::validate, _1, boost::ref(features)));
 }
 
 
@@ -1826,7 +1869,7 @@ void InsetText::fitInsetCursor(BufferView * bv) const
 }
 
 
-UpdatableInset::RESULT
+Inset::RESULT
 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
 {
        if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
@@ -1836,7 +1879,7 @@ InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
 }
 
 
-UpdatableInset::RESULT
+Inset::RESULT
 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
 {
        if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
@@ -1846,7 +1889,7 @@ InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
 }
 
 
-UpdatableInset::RESULT
+Inset::RESULT
 InsetText::moveRightIntern(BufferView * bv, bool front,
                           bool activate_inset, bool selecting)
 {
@@ -1861,7 +1904,7 @@ InsetText::moveRightIntern(BufferView * bv, bool front,
 }
 
 
-UpdatableInset::RESULT
+Inset::RESULT
 InsetText::moveLeftIntern(BufferView * bv, bool front,
                          bool activate_inset, bool selecting)
 {
@@ -1876,8 +1919,7 @@ InsetText::moveLeftIntern(BufferView * bv, bool front,
 }
 
 
-UpdatableInset::RESULT
-InsetText::moveUp(BufferView * bv)
+Inset::RESULT InsetText::moveUp(BufferView * bv)
 {
        if (!crow(bv)->previous())
                return FINISHED_UP;
@@ -1886,8 +1928,7 @@ InsetText::moveUp(BufferView * bv)
 }
 
 
-UpdatableInset::RESULT
-InsetText::moveDown(BufferView * bv)
+Inset::RESULT InsetText::moveDown(BufferView * bv)
 {
        if (!crow(bv)->next())
                return FINISHED_DOWN;
@@ -1960,15 +2001,15 @@ vector<string> const InsetText::getLabelList() const
 {
        vector<string> label_list;
 
-       Paragraph * tpar = &*(paragraphs.begin());
-       while (tpar) {
-               InsetList::iterator beg = tpar->insetlist.begin();
-               InsetList::iterator end = tpar->insetlist.end();
+       ParagraphList::iterator pit = paragraphs.begin();
+       ParagraphList::iterator pend = paragraphs.end();
+       for (; pit != pend; ++pit) {
+               InsetList::iterator beg = pit->insetlist.begin();
+               InsetList::iterator end = pit->insetlist.end();
                for (; beg != end; ++beg) {
                        vector<string> const l = beg.getInset()->getLabelList();
                        label_list.insert(label_list.end(), l.begin(), l.end());
                }
-               tpar = tpar->next();
        }
        return label_list;
 }
@@ -2032,7 +2073,7 @@ bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
        x -= drawTextXOffset;
        int dummyx = x;
        int dummyy = y + insetAscent;
-       Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
+       Inset * inset = getLyXText(bv)->checkInsetHit(bv, dummyx, dummyy);
        // we only do the edit() call if the inset was hit by the mouse
        // or if it is a highly editable inset. So we should call this
        // function from our own edit with button < 0.
@@ -2286,10 +2327,8 @@ void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
        it->second.remove = true;
        if (recursive) {
                /// then remove all LyXText in text-insets
-               Paragraph * p = &*(paragraphs.begin());
-               for (; p; p = p->next()) {
-                       p->deleteInsetsLyXText(bv);
-               }
+               for_each(paragraphs.begin(), paragraphs.end(),
+                        boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
        }
 }
 
@@ -2324,9 +2363,10 @@ void InsetText::resizeLyXText(BufferView * bv, bool force) const
 
        LyXText * t = it->second.text.get();
        saveLyXTextState(t);
-       for (Paragraph * p = &*(paragraphs.begin()); p; p = p->next()) {
-               p->resizeInsetsLyXText(bv);
-       }
+
+       for_each(paragraphs.begin(), paragraphs.end(),
+                boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
+
        t->init(bv, true);
        restoreLyXTextState(bv, t);
        if (the_locking_inset) {
@@ -2356,17 +2396,17 @@ void InsetText::reinitLyXText() const
        do_reinit = false;
        do_resize = 0;
 //     lyxerr << "InsetText::reinitLyXText\n";
-       for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
+       for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
                lyx::Assert(it->second.text.get());
 
                LyXText * t = it->second.text.get();
                BufferView * bv = it->first;
 
                saveLyXTextState(t);
-               for (Paragraph * p = &*(paragraphs.begin());
-                    p; p = p->next()) {
-                       p->resizeInsetsLyXText(bv);
-               }
+
+               for_each(paragraphs.begin(), paragraphs.end(),
+                        boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
+
                t->init(bv, true);
                restoreLyXTextState(bv, t);
                if (the_locking_inset) {
@@ -2389,11 +2429,13 @@ void InsetText::removeNewlines()
 {
        bool changed = false;
 
-       for (Paragraph * p = &*(paragraphs.begin()); p; p = p->next()) {
-               for (int i = 0; i < p->size(); ++i) {
-                       if (p->getChar(i) == Paragraph::META_NEWLINE) {
+       ParagraphList::iterator it = paragraphs.begin();
+       ParagraphList::iterator end = paragraphs.end();
+       for (; it != end; ++it) {
+               for (int i = 0; i < it->size(); ++i) {
+                       if (it->getChar(i) == Paragraph::META_NEWLINE) {
                                changed = true;
-                               p->erase(i);
+                               it->erase(i);
                        }
                }
        }
@@ -2467,29 +2509,18 @@ void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
 
 Paragraph * InsetText::getParFromID(int id) const
 {
-#if 0
-       Paragraph * result = par;
-       Paragraph * ires = 0;
-       while (result && result->id() != id) {
-               if ((ires = result->getParFromID(id)))
-                       return ires;
-               result = result->next();
-       }
-       return result;
-#else
-       Paragraph * tmp = &*(paragraphs.begin());
-       while (tmp) {
-               if (tmp->id() == id) {
-                       return tmp;
+       ParagraphList::iterator it = paragraphs.begin();
+       ParagraphList::iterator end = paragraphs.end();
+       for (; it != end; ++it) {
+               if (it->id() == id) {
+                       return &*(it);
                }
-               Paragraph * tmp2 = tmp->getParFromID(id);
+               Paragraph * tmp2 = it->getParFromID(id);
                if (tmp2 != 0) {
                        return tmp2;
                }
-               tmp = tmp->next();
        }
        return 0;
-#endif
 }
 
 
@@ -2532,11 +2563,9 @@ void InsetText::paragraph(Paragraph * p)
        // and are done!
        paragraphs.set(p);
        // set ourself as owner for all the paragraphs inserted!
-       Paragraph * np = &*(paragraphs.begin());
-       while (np) {
-               np->setInsetOwner(this);
-               np = np->next();
-       }
+       for_each(paragraphs.begin(), paragraphs.end(),
+                boost::bind(&Paragraph::setInsetOwner, _1, this));
+
        reinitLyXText();
        // redraw myself when asked for
        need_update = INIT;
@@ -2548,20 +2577,18 @@ Inset * InsetText::getInsetFromID(int id_arg) const
        if (id_arg == id())
                return const_cast<InsetText *>(this);
 
-       Paragraph * lp = &*(paragraphs.begin());
-
-       while (lp) {
-               for (InsetList::iterator it = lp->insetlist.begin(),
-                        en = lp->insetlist.end();
-                        it != en; ++it)
-               {
+       ParagraphList::iterator pit = paragraphs.begin();
+       ParagraphList::iterator pend = paragraphs.end();
+       for (; pit != pend; ++pit) {
+               InsetList::iterator it = pit->insetlist.begin();
+               InsetList::iterator end = pit->insetlist.end();
+               for (; it != end; ++it) {
                        if (it.getInset()->id() == id_arg)
                                return it.getInset();
                        Inset * in = it.getInset()->getInsetFromID(id_arg);
                        if (in)
                                return in;
                }
-               lp = lp->next();
        }
        return 0;
 }
@@ -2739,7 +2766,7 @@ void InsetText::collapseParagraphs(BufferView * bv) const
                                        llt->selection.end.pos() + paragraphs.begin()->size());
                        }
                }
-               paragraphs.begin()->pasteParagraph(bparams);
+               mergeParagraph(bparams, &*paragraphs.begin());
        }
        reinitLyXText();
 }
@@ -2781,7 +2808,7 @@ void InsetText::appendParagraphs(BufferParams const & bparams,
        // paste it!
        lastbuffer->next(buf);
        buf->previous(lastbuffer);
-       lastbuffer->pasteParagraph(bparams);
+       mergeParagraph(bparams, lastbuffer);
 
        reinitLyXText();
 }