2 * \file math_hullinset.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "math_charinset.h"
14 #include "math_data.h"
15 #include "math_extern.h"
16 #include "math_hullinset.h"
17 #include "math_mathmlstream.h"
18 #include "math_streamstr.h"
19 #include "math_support.h"
21 #include "BufferView.h"
22 #include "CutAndPaste.h"
24 #include "LaTeXFeatures.h"
27 #include "dispatchresult.h"
28 #include "funcrequest.h"
32 #include "outputparams.h"
34 #include "textpainter.h"
37 #include "insets/render_preview.h"
39 #include "frontends/Alert.h"
41 #include "graphics/PreviewImage.h"
42 #include "graphics/PreviewLoader.h"
44 #include "support/lstrings.h"
46 #include <boost/bind.hpp>
50 using lyx::cap::grabAndEraseSelection;
51 using lyx::support::subst;
58 using std::istringstream;
60 using std::ostringstream;
68 int getCols(string const & type)
70 if (type == "eqnarray")
74 if (type == "flalign")
76 if (type == "alignat")
78 if (type == "xalignat")
80 if (type == "xxalignat")
86 // returns position of first relation operator in the array
87 // used for "intelligent splitting"
88 size_t firstRelOp(MathArray const & ar)
90 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
92 return it - ar.begin();
97 char const * star(bool numbered)
99 return numbered ? "" : "*";
103 int typecode(string const & s)
105 if (s == "none") return 0;
106 if (s == "simple") return 1;
107 if (s == "equation") return 2;
108 if (s == "eqnarray") return 3;
109 if (s == "align") return 4;
110 if (s == "alignat") return 5;
111 if (s == "xalignat") return 6;
112 if (s == "xxalignat") return 7;
113 if (s == "multline") return 8;
114 if (s == "gather") return 9;
115 if (s == "flalign") return 10;
116 lyxerr << "unknown hull type '" << s << "'" << endl;
120 bool smaller(string const & s, string const & t)
122 return typecode(s) < typecode(t);
126 } // end anon namespace
130 MathHullInset::MathHullInset()
131 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1),
132 preview_(new RenderPreview(this))
134 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
135 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
136 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
137 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
142 MathHullInset::MathHullInset(string const & type)
143 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1),
144 preview_(new RenderPreview(this))
150 MathHullInset::MathHullInset(MathHullInset const & other)
151 : MathGridInset(other),
152 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
153 preview_(new RenderPreview(this))
157 MathHullInset::~MathHullInset()
161 auto_ptr<InsetBase> MathHullInset::doClone() const
163 return auto_ptr<InsetBase>(new MathHullInset(*this));
167 MathHullInset & MathHullInset::operator=(MathHullInset const & other)
171 *static_cast<MathGridInset*>(this) = MathGridInset(other);
173 nonum_ = other.nonum_;
174 label_ = other.label_;
175 preview_.reset(new RenderPreview(*other.preview_, this));
181 MathInset::mode_type MathHullInset::currentMode() const
184 return UNDECIDED_MODE;
185 // definitely math mode ...
190 bool MathHullInset::idxFirst(LCursor & cur) const
198 bool MathHullInset::idxLast(LCursor & cur) const
200 cur.idx() = nargs() - 1;
201 cur.pos() = cur.lastpos();
206 char MathHullInset::defaultColAlign(col_type col)
208 if (type_ == "eqnarray")
210 if (typecode(type_) >= typecode("align"))
211 return "rl"[col & 1];
216 int MathHullInset::defaultColSpace(col_type col)
218 if (type_ == "align" || type_ == "alignat")
220 if (type_ == "xalignat")
221 return (col & 1) ? 20 : 0;
222 if (type_ == "xxalignat" || type_ == "flalign")
223 return (col & 1) ? 40 : 0;
228 char const * MathHullInset::standardFont() const
231 return "lyxnochange";
236 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
238 BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
240 bool use_preview = false;
241 if (!editing(mi.base.bv) &&
242 RenderPreview::status() == LyXRC::PREVIEW_ON) {
243 lyx::graphics::PreviewImage const * pimage =
244 preview_->getPreviewImage(*mi.base.bv->buffer());
245 use_preview = pimage && pimage->image();
249 preview_->metrics(mi, dim);
250 // insert a one pixel gap in front of the formula
258 FontSetChanger dummy1(mi.base, standardFont());
259 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
261 // let the cells adjust themselves
262 MathGridInset::metrics(mi, dim);
269 if (numberedType()) {
270 FontSetChanger dummy(mi.base, "mathbf");
272 for (row_type row = 0; row < nrows(); ++row)
273 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
279 // make it at least as high as the current font
282 math_font_max_dim(mi.base.font, asc, des);
283 dim.asc = max(dim.asc, asc);
284 dim.des = max(dim.des, des);
290 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
292 BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
294 bool use_preview = false;
295 if (!editing(pi.base.bv) &&
296 RenderPreview::status() == LyXRC::PREVIEW_ON) {
297 lyx::graphics::PreviewImage const * pimage =
298 preview_->getPreviewImage(*pi.base.bv->buffer());
299 use_preview = pimage && pimage->image();
303 // one pixel gap in front
304 preview_->draw(pi, x + 1, y);
305 setPosCache(pi, x, y);
309 FontSetChanger dummy1(pi.base, standardFont());
310 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
311 MathGridInset::draw(pi, x + 1, y);
313 if (numberedType()) {
314 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
315 for (row_type row = 0; row < nrows(); ++row) {
316 int const yy = y + rowinfo_[row].offset_;
317 FontSetChanger dummy(pi.base, "mathrm");
318 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
321 setPosCache(pi, x, y);
325 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
328 MathGridInset::metricsT(mi, dim);
331 WriteStream wi(os, false, true);
333 dim.wid = os.str().size();
340 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
343 MathGridInset::drawT(pain, x, y);
346 WriteStream wi(os, false, true);
348 pain.draw(x, y, os.str().c_str());
355 string const latex_string(MathHullInset const & inset)
358 WriteStream wi(ls, false, false);
366 void MathHullInset::addPreview(lyx::graphics::PreviewLoader & ploader) const
368 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
369 string const snippet = latex_string(*this);
370 preview_->addPreview(snippet, ploader);
375 void MathHullInset::notifyCursorLeaves(LCursor & cur)
377 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
378 Buffer const & buffer = cur.buffer();
379 string const snippet = latex_string(*this);
380 preview_->addPreview(snippet, buffer);
381 preview_->startLoading(buffer);
386 string MathHullInset::label(row_type row) const
388 row_type n = nrows();
389 BOOST_ASSERT(row < n);
394 void MathHullInset::label(row_type row, string const & label)
396 //lyxerr << "setting label '" << label << "' for row " << row << endl;
401 void MathHullInset::numbered(row_type row, bool num)
407 bool MathHullInset::numbered(row_type row) const
413 bool MathHullInset::ams() const
417 type_ == "flalign" ||
418 type_ == "multline" ||
420 type_ == "alignat" ||
421 type_ == "xalignat" ||
422 type_ == "xxalignat";
426 bool MathHullInset::display() const
428 return type_ != "simple" && type_ != "none";
432 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
434 for (row_type row = 0; row < nrows(); ++row)
435 if (!label_[row].empty() && nonum_[row] != 1)
436 labels.push_back(label_[row]);
440 bool MathHullInset::numberedType() const
444 if (type_ == "simple")
446 if (type_ == "xxalignat")
448 for (row_type row = 0; row < nrows(); ++row)
455 void MathHullInset::validate(LaTeXFeatures & features) const
458 features.require("amsmath");
461 // Validation is necessary only if not using AMS math.
462 // To be safe, we will always run mathedvalidate.
463 //if (features.amsstyle)
466 features.require("boldsymbol");
467 //features.binom = true;
469 MathGridInset::validate(features);
473 void MathHullInset::header_write(WriteStream & os) const
475 bool n = numberedType();
480 else if (type_ == "simple") {
486 else if (type_ == "equation") {
488 os << "\\begin{equation" << star(n) << "}\n";
493 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
494 || type_ == "gather" || type_ == "multline")
495 os << "\\begin{" << type_ << star(n) << "}\n";
497 else if (type_ == "alignat" || type_ == "xalignat")
498 os << "\\begin{" << type_ << star(n) << '}'
499 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
501 else if (type_ == "xxalignat")
502 os << "\\begin{" << type_ << '}'
503 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
506 os << "\\begin{unknown" << star(n) << '}';
510 void MathHullInset::footer_write(WriteStream & os) const
512 bool n = numberedType();
517 else if (type_ == "simple")
520 else if (type_ == "equation")
522 os << "\\end{equation" << star(n) << "}\n";
526 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
527 || type_ == "alignat" || type_ == "xalignat"
528 || type_ == "gather" || type_ == "multline")
529 os << "\\end{" << type_ << star(n) << "}\n";
531 else if (type_ == "xxalignat")
532 os << "\\end{" << type_ << "}\n";
535 os << "\\end{unknown" << star(n) << '}';
539 bool MathHullInset::rowChangeOK() const
542 type_ == "eqnarray" || type_ == "align" ||
543 type_ == "flalign" || type_ == "alignat" ||
544 type_ == "xalignat" || type_ == "xxalignat" ||
545 type_ == "gather" || type_ == "multline";
549 bool MathHullInset::colChangeOK() const
552 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
553 type_ == "xalignat" || type_ == "xxalignat";
557 void MathHullInset::addRow(row_type row)
561 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
562 label_.insert(label_.begin() + row + 1, string());
563 MathGridInset::addRow(row);
567 void MathHullInset::swapRow(row_type row)
571 if (row + 1 == nrows())
573 swap(nonum_[row], nonum_[row + 1]);
574 swap(label_[row], label_[row + 1]);
575 MathGridInset::swapRow(row);
579 void MathHullInset::delRow(row_type row)
581 if (nrows() <= 1 || !rowChangeOK())
583 MathGridInset::delRow(row);
584 nonum_.erase(nonum_.begin() + row);
585 label_.erase(label_.begin() + row);
589 void MathHullInset::addCol(col_type col)
593 MathGridInset::addCol(col);
597 void MathHullInset::delCol(col_type col)
599 if (ncols() <= 1 || !colChangeOK())
601 MathGridInset::delCol(col);
605 string MathHullInset::nicelabel(row_type row) const
609 if (label_[row].empty())
610 return string("(#)");
611 return '(' + label_[row] + ')';
615 void MathHullInset::glueall()
618 for (idx_type i = 0; i < nargs(); ++i)
620 *this = MathHullInset("simple");
626 void MathHullInset::splitTo2Cols()
628 BOOST_ASSERT(ncols() == 1);
629 MathGridInset::addCol(1);
630 for (row_type row = 0; row < nrows(); ++row) {
631 idx_type const i = 2 * row;
632 pos_type pos = firstRelOp(cell(i));
633 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
634 cell(i).erase(pos, cell(i).size());
639 void MathHullInset::splitTo3Cols()
641 BOOST_ASSERT(ncols() < 3);
644 MathGridInset::addCol(1);
645 for (row_type row = 0; row < nrows(); ++row) {
646 idx_type const i = 3 * row + 1;
647 if (cell(i).size()) {
648 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
649 cell(i).erase(1, cell(i).size());
655 void MathHullInset::changeCols(col_type cols)
659 else if (ncols() < cols) {
665 while (ncols() < cols)
666 MathGridInset::addCol(ncols() - 1);
672 for (row_type row = 0; row < nrows(); ++row) {
673 idx_type const i = row * ncols();
674 for (col_type col = cols; col < ncols(); ++col) {
675 cell(i + cols - 1).append(cell(i + col));
679 while (ncols() > cols) {
680 MathGridInset::delCol(ncols() - 1);
685 string const & MathHullInset::getType() const
691 void MathHullInset::setType(string const & type)
699 void MathHullInset::mutate(string const & newtype)
701 lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
703 // we try to move along the chain
704 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
706 // +-------------------------------------+
707 // we use eqnarray as intermediate type for mutations that are not
708 // directly supported because it handles labels and numbering for
711 if (newtype == "dump") {
715 else if (newtype == type_) {
719 else if (typecode(newtype) < 0) {
723 else if (type_ == "none") {
729 else if (type_ == "simple") {
730 if (newtype == "none") {
740 else if (type_ == "equation") {
741 if (smaller(newtype, type_)) {
745 } else if (newtype == "eqnarray") {
746 // split it "nicely" on the first relop
749 } else if (newtype == "multline" || newtype == "gather") {
759 else if (type_ == "eqnarray") {
760 if (smaller(newtype, type_)) {
761 // set correct (no)numbering
762 bool allnonum = true;
763 for (row_type row = 0; row < nrows(); ++row)
767 // set first non-empty label
769 for (row_type row = 0; row < nrows(); ++row) {
770 if (!label_[row].empty()) {
777 nonum_[0] = allnonum;
780 } else { // align & Co.
787 else if (type_ == "align" || type_ == "alignat" ||
788 type_ == "xalignat" || type_ == "flalign") {
789 if (smaller(newtype, "align")) {
793 } else if (newtype == "gather" || newtype == "multline") {
796 } else if (newtype == "xxalignat") {
797 for (row_type row = 0; row < nrows(); ++row)
798 numbered(row, false);
805 else if (type_ == "xxalignat") {
806 for (row_type row = 0; row < nrows(); ++row)
807 numbered(row, false);
808 if (smaller(newtype, "align")) {
812 } else if (newtype == "gather" || newtype == "multline") {
820 else if (type_ == "multline" || type_ == "gather") {
821 if (newtype == "gather" || newtype == "multline")
823 else if (newtype == "align" || newtype == "flalign" ||
824 newtype == "alignat" || newtype == "xalignat") {
827 } else if (newtype == "xxalignat") {
829 for (row_type row = 0; row < nrows(); ++row)
830 numbered(row, false);
840 lyxerr << "mutation from '" << type_
841 << "' to '" << newtype << "' not implemented" << endl;
846 string MathHullInset::eolString(row_type row, bool fragile) const
849 if (numberedType()) {
850 if (!label_[row].empty() && !nonum_[row])
851 res += "\\label{" + label_[row] + '}';
852 if (nonum_[row] && (type_ != "multline"))
853 res += "\\nonumber ";
855 return res + MathGridInset::eolString(row, fragile);
859 void MathHullInset::write(WriteStream & os) const
862 MathGridInset::write(os);
867 void MathHullInset::normalize(NormalStream & os) const
869 os << "[formula " << type_ << ' ';
870 MathGridInset::normalize(os);
875 void MathHullInset::mathmlize(MathMLStream & os) const
877 MathGridInset::mathmlize(os);
881 void MathHullInset::infoize(ostream & os) const
883 os << "Type: " << type_;
887 void MathHullInset::check() const
889 BOOST_ASSERT(nonum_.size() == nrows());
890 BOOST_ASSERT(label_.size() == nrows());
894 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
898 istringstream iss(func.argument);
899 iss >> lang >> extra;
904 #warning temporarily disabled
905 //if (cur.selection()) {
908 // lyxerr << "use selection: " << ar << endl;
909 // insert(pipeThroughExtern(lang, extra, ar));
915 eq.push_back(MathAtom(new MathCharInset('=')));
917 // go to first item in line
918 cur.idx() -= cur.idx() % ncols();
921 if (getType() == "simple") {
922 size_type pos = cur.cell().find_last(eq);
924 if (cur.inMathed() && cur.selection()) {
925 asArray(grabAndEraseSelection(cur), ar);
926 } else if (pos == cur.cell().size()) {
928 lyxerr << "use whole cell: " << ar << endl;
930 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
931 lyxerr << "use partial cell form pos: " << pos << endl;
933 cur.cell().append(eq);
934 cur.cell().append(pipeThroughExtern(lang, extra, ar));
935 cur.pos() = cur.lastpos();
939 if (getType() == "equation") {
940 lyxerr << "use equation inset" << endl;
942 MathArray & ar = cur.cell();
943 lyxerr << "use cell: " << ar << endl;
947 cur.cell() = pipeThroughExtern(lang, extra, ar);
948 // move to end of line
949 cur.pos() = cur.lastpos();
954 lyxerr << "use eqnarray" << endl;
955 cur.idx() += 2 - cur.idx() % ncols();
957 MathArray ar = cur.cell();
958 lyxerr << "use cell: " << ar << endl;
960 #warning temporarily disabled
967 cur.cell() = pipeThroughExtern(lang, extra, ar);
968 cur.pos() = cur.lastpos();
973 void MathHullInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
975 switch (cmd.action) {
977 case LFUN_FINISHED_LEFT:
978 case LFUN_FINISHED_RIGHT:
979 case LFUN_FINISHED_UP:
980 case LFUN_FINISHED_DOWN:
981 MathGridInset::priv_dispatch(cur, cmd);
982 notifyCursorLeaves(cur);
985 case LFUN_BREAKPARAGRAPH:
990 // some magic for the common case
991 if (type_ == "simple" || type_ == "equation") {
992 recordUndoInset(cur);
995 cur.pos() = cur.lastpos();
997 MathGridInset::priv_dispatch(cur, cmd);
1000 case LFUN_MATH_NUMBER:
1001 //lyxerr << "toggling all numbers" << endl;
1003 recordUndoInset(cur);
1004 bool old = numberedType();
1005 if (type_ == "multline")
1006 numbered(nrows() - 1, !old);
1008 for (row_type row = 0; row < nrows(); ++row)
1009 numbered(row, !old);
1010 cur.message(old ? _("No number") : _("Number"));
1014 case LFUN_MATH_NONUMBER:
1016 recordUndoInset(cur);
1017 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1018 bool old = numbered(r);
1019 cur.message(old ? _("No number") : _("Number"));
1024 case LFUN_INSERT_LABEL: {
1025 recordUndoInset(cur);
1026 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1027 string old_label = label(r);
1028 string new_label = cmd.argument;
1030 if (new_label.empty()) {
1031 string const default_label =
1032 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1033 pair<bool, string> const res = old_label.empty()
1034 ? Alert::askForText(_("Enter new label to insert:"), default_label)
1035 : Alert::askForText(_("Enter label:"), old_label);
1037 new_label = lyx::support::trim(res.second);
1039 new_label = old_label;
1042 if (!new_label.empty())
1044 label(r, new_label);
1048 case LFUN_MATH_EXTERN:
1049 recordUndoInset(cur);
1053 case LFUN_MATH_MUTATE: {
1054 recordUndoInset(cur);
1055 row_type row = cur.row();
1056 col_type col = cur.col();
1057 mutate(cmd.argument);
1058 cur.idx() = row * ncols() + col;
1059 if (cur.idx() > cur.lastidx()) {
1060 cur.idx() = cur.lastidx();
1061 cur.pos() = cur.lastpos();
1063 if (cur.pos() > cur.lastpos())
1064 cur.pos() = cur.lastpos();
1065 //cur.dispatched(FINISHED);
1069 case LFUN_MATH_DISPLAY: {
1070 recordUndoInset(cur);
1071 mutate(type_ == "simple" ? "equation" : "simple");
1073 cur.pos() = cur.lastpos();
1074 //cur.dispatched(FINISHED);
1079 MathGridInset::priv_dispatch(cur, cmd);
1085 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1086 FuncStatus & flag) const
1088 switch (cmd.action) {
1089 case LFUN_BREAKLINE:
1090 case LFUN_MATH_NUMBER:
1091 case LFUN_MATH_NONUMBER:
1092 case LFUN_INSERT_LABEL:
1093 case LFUN_MATH_EXTERN:
1094 case LFUN_MATH_MUTATE:
1095 case LFUN_MATH_DISPLAY:
1098 case LFUN_TABULAR_FEATURE: {
1099 istringstream is(cmd.argument);
1103 && (s == "append-row"
1104 || s == "delete-row"
1105 || s == "copy-row"))
1108 && (s == "delete-row" || s == "swap-row"))
1111 && (s == "append-column"
1112 || s == "delete-column"
1113 || s == "copy-column"))
1116 && (s == "delete-column" || s == "swap-column"))
1118 return MathGridInset::getStatus(cur, cmd, flag);
1121 return MathGridInset::getStatus(cur, cmd, flag);
1126 /////////////////////////////////////////////////////////////////////
1128 #include "math_arrayinset.h"
1129 #include "math_deliminset.h"
1130 #include "math_factory.h"
1131 #include "math_parser.h"
1132 #include "math_spaceinset.h"
1133 #include "ref_inset.h"
1135 #include "bufferview_funcs.h"
1136 #include "lyxtext.h"
1138 #include "frontends/LyXView.h"
1139 #include "frontends/Dialogs.h"
1141 #include "support/lyxlib.h"
1144 // simply scrap this function if you want
1145 void MathHullInset::mutateToText()
1148 // translate to latex
1150 latex(NULL, os, false, false);
1151 string str = os.str();
1154 LyXText * lt = view_->getLyXText();
1155 string::const_iterator cit = str.begin();
1156 string::const_iterator end = str.end();
1157 for (; cit != end; ++cit)
1158 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1161 //view_->owner()->dispatch(LFUN_ESCAPE);
1166 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1167 string const & font)
1169 // this whole function is a hack and won't work for incremental font
1172 if (cur.inset().asMathInset()->name() == font)
1173 cur.handleFont(font);
1175 cur.handleNest(createMathInset(font));
1181 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1186 bv_funcs::string2font(arg, font, b);
1187 if (font.color() != LColor::inherit) {
1188 MathAtom at = createMathInset("color");
1189 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
1190 cur.handleNest(at, 1);
1195 void MathHullInset::edit(LCursor & cur, bool left)
1198 left ? idxFirst(cur) : idxLast(cur);
1202 string const MathHullInset::editMessage() const
1204 return _("Math editor mode");
1208 void MathHullInset::getCursorDim(int & asc, int & desc) const
1212 //math_font_max_dim(font_, asc, des);
1216 void MathHullInset::revealCodes(LCursor & cur) const
1218 if (!cur.inMathed())
1222 cur.message(os.str());
1224 // write something to the minibuffer
1225 // translate to latex
1229 string str = os.str();
1231 string::size_type pos = 0;
1233 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1236 else if (*it == '\0') {
1238 pos = it - str.begin();
1244 res = res.substr(pos - 30);
1245 if (res.size() > 60)
1246 res = res.substr(0, 60);
1252 InsetBase::Code MathHullInset::lyxCode() const
1258 /////////////////////////////////////////////////////////////////////
1262 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1265 #ifdef WITH_WARNINGS
1266 #warning completely broken
1268 static MathHullInset * lastformula = 0;
1269 static CursorBase current = DocIterator(ibegin(nucleus()));
1270 static MathArray ar;
1271 static string laststr;
1273 if (lastformula != this || laststr != str) {
1274 //lyxerr << "reset lastformula to " << this << endl;
1277 current = ibegin(nucleus());
1279 mathed_parse_cell(ar, str);
1283 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1285 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1286 CursorSlice & top = it.back();
1287 MathArray const & a = top.asMathInset()->cell(top.idx_);
1288 if (a.matchpart(ar, top.pos_)) {
1289 bv->cursor().setSelection(it, ar.size());
1291 top.pos_ += ar.size();
1297 //lyxerr << "not found!" << endl;
1304 void MathHullInset::write(Buffer const &, std::ostream & os) const
1306 WriteStream wi(os, false, false);
1312 void MathHullInset::read(Buffer const &, LyXLex & lex)
1315 mathed_parse_normal(at, lex);
1316 operator=(*at->asHullInset());
1320 int MathHullInset::plaintext(Buffer const &, ostream & os,
1321 OutputParams const &) const
1323 if (0 && display()) {
1327 TextPainter tpain(dim.width(), dim.height());
1328 drawT(tpain, 0, dim.ascent());
1330 // reset metrics cache to "real" values
1332 return tpain.textheight();
1334 WriteStream wi(os, false, true);
1341 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1342 OutputParams const & runparams) const
1344 return docbook(buf, os, runparams);
1348 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1349 OutputParams const & runparams) const
1351 MathMLStream ms(os);
1354 if (getType() == "simple")
1355 name= "inlineequation";
1357 name = "informalequation";
1359 string bname = name;
1360 if (!label(0).empty())
1361 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1362 ms << MTag(bname.c_str());
1365 if (runparams.flavor == OutputParams::XML) {
1366 ms << MTag("alt role=\"tex\" ");
1367 // Workaround for db2latex: db2latex always includes equations with
1368 // \ensuremath{} or \begin{display}\end{display}
1369 // so we strip LyX' math environment
1370 WriteStream wi(ls, false, false);
1371 MathGridInset::write(wi);
1372 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1375 MathGridInset::mathmlize(ms);
1378 ms << MTag("alt role=\"tex\"");
1379 res = latex(buf, ls, runparams);
1380 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1384 ms << "<graphic fileref=\"eqn/";
1385 if ( !label(0).empty())
1386 ms << sgml::cleanID(buf, runparams, label(0));
1388 ms << sgml::uniqueID("anon");
1390 if (runparams.flavor == OutputParams::XML)
1395 ms << ETag(name.c_str());
1396 return ms.line() + res;