+ Paragraph * par = inset.paragraph();
+ int lines = 0;
+ while (par) {
+ pos_type siz = par->size();
+ for (pos_type i = 0; i < siz; ++i) {
+ if (par->isNewline(i)) {
+ os << '\n';
+ ++lines;
+ } else {
+ os << par->getChar(i);
+ }
+ }
+ par = par->next();
+ if (par) {
+ os << "\n";
+ lines ++;
+ }
+ }
+
+ return lines;
+}
+
+
+Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
+{
+ Inset::RESULT result = UNDISPATCHED;
+ BufferView * bv = cmd.view();
+
+ if (inset.paragraph()->empty()) {
+ set_latex_font(bv);
+ }
+
+ switch (cmd.action) {
+ case LFUN_INSET_MODIFY: {
+ InsetERT::ERTStatus status_;
+ InsetERTMailer::string2params(cmd.argument, status_);
+
+ status(bv, status_);
+
+ /* FIXME: I refuse to believe we have to live
+ * with ugliness like this ! Note that this
+ * rebreak *is* needed. Consider a change from
+ * Open (needfullrow) to Inlined (only the space
+ * taken by the text).
+ */
+ LyXText * t = inset.getLyXText(cmd.view());
+ t->need_break_row = t->rows().begin();
+ t->fullRebreak();
+ t->setCursorIntern(t->cursor.par(), t->cursor.pos());
+ inset.update(cmd.view(), true);
+ bv->updateInset(this);
+ result = DISPATCHED;
+ }
+ break;
+
+ case LFUN_MOUSE_PRESS:
+ lfunMousePress(cmd);
+ result = DISPATCHED;
+ break;
+
+ case LFUN_MOUSE_MOTION:
+ lfunMouseMotion(cmd);
+ result = DISPATCHED;
+ break;
+
+ case LFUN_MOUSE_RELEASE:
+ lfunMouseRelease(cmd);
+ result = DISPATCHED;
+ break;
+
+ case LFUN_LAYOUT:
+ bv->owner()->setLayout(inset.paragraph()->layout()->name());
+ result = DISPATCHED_NOUPDATE;
+ break;
+
+ default:
+ result = InsetCollapsable::localDispatch(cmd);
+ }
+
+ switch (cmd.action) {
+ case LFUN_BREAKPARAGRAPH:
+ case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
+ case LFUN_BACKSPACE:
+ case LFUN_BACKSPACE_SKIP:
+ case LFUN_DELETE:
+ case LFUN_DELETE_SKIP:
+ case LFUN_DELETE_LINE_FORWARD:
+ case LFUN_CUT:
+ set_latex_font(bv);
+ break;
+
+ default:
+ break;
+ }
+ return result;
+}
+
+
+string const InsetERT::get_new_label() const
+{
+ string la;
+ pos_type const max_length = 15;
+ pos_type const p_siz = inset.paragraph()->size();
+ pos_type const n = min(max_length, p_siz);
+ pos_type i = 0;
+ pos_type j = 0;
+ for(; i < n && j < p_siz; ++j) {
+ if (inset.paragraph()->isInset(j))
+ continue;
+ la += inset.paragraph()->getChar(j);
+ ++i;
+ }
+ if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
+ la += "...";
+ }
+ if (la.empty()) {
+ la = _("ERT");
+ }
+ return la;
+}
+
+
+void InsetERT::setButtonLabel() const
+{
+ if (status_ == Collapsed) {
+ setLabel(get_new_label());
+ } else {
+ setLabel(_("ERT"));
+ }
+}
+
+
+bool InsetERT::checkInsertChar(LyXFont & /* font */)
+{
+#ifdef SET_HARD_FONT
+ LyXFont f(LyXFont::ALL_INHERIT, latex_language);
+ font = f;
+ font.setFamily(LyXFont::TYPEWRITER_FAMILY);
+ font.setColor(LColor::latex);
+#endif
+ return true;
+}
+
+
+int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
+{
+ if (!inlined())
+ return InsetCollapsable::ascent(bv, font);
+
+ return inset.ascent(bv, font);
+}
+
+
+int InsetERT::descent(BufferView * bv, LyXFont const & font) const
+{
+ if (!inlined())
+ return InsetCollapsable::descent(bv, font);
+
+ return inset.descent(bv, font);
+}
+
+
+int InsetERT::width(BufferView * bv, LyXFont const & font) const
+{
+ if (!inlined())
+ return InsetCollapsable::width(bv, font);
+
+ return inset.width(bv, font);
+}
+
+
+void InsetERT::draw(BufferView * bv, LyXFont const & f,
+ int baseline, float & x) const
+{
+ InsetCollapsable::draw(bv, f, baseline, x, inlined());
+}
+
+
+void InsetERT::set_latex_font(BufferView * /* bv */)
+{
+#ifdef SET_HARD_FONT
+ LyXFont font(LyXFont::ALL_INHERIT, latex_language);
+
+ font.setFamily(LyXFont::TYPEWRITER_FAMILY);
+ font.setColor(LColor::latex);
+
+ inset.getLyXText(bv)->setFont(bv, font, false);
+#endif
+}
+
+
+// attention this function can be called with bv == 0
+void InsetERT::status(BufferView * bv, ERTStatus const st) const
+{
+ if (st != status_) {
+ status_ = st;
+ switch (st) {
+ case Inlined:
+ if (bv)
+ inset.setUpdateStatus(bv, InsetText::INIT);
+ break;
+ case Open:
+ collapsed_ = false;
+ setButtonLabel();
+ break;
+ case Collapsed:
+ collapsed_ = true;
+ setButtonLabel();
+ if (bv)
+ bv->unlockInset(const_cast<InsetERT *>(this));
+ break;
+ }
+ if (bv) {
+ bv->updateInset(const_cast<InsetERT *>(this));
+ bv->buffer()->markDirty();
+ }
+ }
+}
+
+
+bool InsetERT::showInsetDialog(BufferView * bv) const
+{
+ InsetERTMailer mailer(const_cast<InsetERT &>(*this));
+ mailer.showDialog(bv);
+ return true;
+}
+
+
+void InsetERT::open(BufferView * bv)
+{
+ if (!collapsed_)
+ return;
+ status(bv, Open);
+}
+
+
+void InsetERT::close(BufferView * bv) const
+{
+ if (status_ == Collapsed || status_ == Inlined)
+ return;
+
+ status(bv, Collapsed);
+}
+
+
+WordLangTuple const
+InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
+{
+ bv->unlockInset(const_cast<InsetERT *>(this));
+ return WordLangTuple();
+}
+
+
+void InsetERT::getDrawFont(LyXFont & font) const
+{
+ LyXFont f(LyXFont::ALL_INHERIT, latex_language);
+ font = f;
+ font.setFamily(LyXFont::TYPEWRITER_FAMILY);
+ font.setColor(LColor::latex);
+}
+
+
+int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
+{
+ int w = InsetCollapsable::getMaxWidth(bv, in);
+ if (status_ != Inlined || w < 0)
+ return w;
+ LyXText * text = inset.getLyXText(bv);
+ int rw = text->rows().begin()->width();
+ if (!rw)
+ rw = w;
+ rw += 40;
+ if (text->rows().size() == 1 && rw < w)
+ return -1;
+ return w;
+}
+
+
+void InsetERT::update(BufferView * bv, bool reinit)
+{
+ if (inset.need_update & InsetText::INIT ||
+ inset.need_update & InsetText::FULL) {
+ setButtonLabel();
+ }
+
+ InsetCollapsable::update(bv, reinit);
+}
+
+
+string const InsetERTMailer::name_("ert");
+
+InsetERTMailer::InsetERTMailer(InsetERT & inset)
+ : inset_(inset)
+{}
+
+
+string const InsetERTMailer::inset2string() const
+{
+ return params2string(inset_.status());
+}
+
+
+void InsetERTMailer::string2params(string const & in,
+ InsetERT::ERTStatus & status)
+{
+ status = InsetERT::Collapsed;
+
+ string name;
+ string body = split(in, name, ' ');
+
+ if (body.empty())
+ return;
+
+ status = static_cast<InsetERT::ERTStatus>(strToInt(body));
+}
+
+
+string const
+InsetERTMailer::params2string(InsetERT::ERTStatus status)
+{
+ return name_ + ' ' + tostr(status);