]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hullinset.C
soem more IU for \mbox
[lyx.git] / src / mathed / math_hullinset.C
1 /**
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.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
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"
20
21 #include "BufferView.h"
22 #include "cursor.h"
23 #include "dispatchresult.h"
24 #include "debug.h"
25 #include "funcrequest.h"
26 #include "gettext.h"
27 #include "LaTeXFeatures.h"
28 #include "LColor.h"
29 #include "lyxrc.h"
30 #include "outputparams.h"
31 #include "textpainter.h"
32 #include "undo.h"
33
34 #include "frontends/Alert.h"
35
36 #include "support/std_sstream.h"
37
38
39 using std::endl;
40 using std::max;
41 using std::string;
42 using std::ostream;
43 using std::auto_ptr;
44 using std::istringstream;
45 using std::ostream;
46 using std::ostringstream;
47 using std::pair;
48 using std::swap;
49 using std::vector;
50
51
52 namespace {
53
54         int getCols(string const & type)
55         {
56                 if (type == "eqnarray")
57                         return 3;
58                 if (type == "align")
59                         return 2;
60                 if (type == "flalign")
61                         return 2;
62                 if (type == "alignat")
63                         return 2;
64                 if (type == "xalignat")
65                         return 2;
66                 if (type == "xxalignat")
67                         return 2;
68                 return 1;
69         }
70
71
72         // returns position of first relation operator in the array
73         // used for "intelligent splitting"
74         size_t firstRelOp(MathArray const & ar)
75         {
76                 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
77                         if ((*it)->isRelOp())
78                                 return it - ar.begin();
79                 return ar.size();
80         }
81
82
83         char const * star(bool numbered)
84         {
85                 return numbered ? "" : "*";
86         }
87
88
89         int typecode(string const & s)
90         {
91                 if (s == "none")      return 0;
92                 if (s == "simple")    return 1;
93                 if (s == "equation")  return 2;
94                 if (s == "eqnarray")  return 3;
95                 if (s == "align")     return 4;
96                 if (s == "alignat")   return 5;
97                 if (s == "xalignat")  return 6;
98                 if (s == "xxalignat") return 7;
99                 if (s == "multline")  return 8;
100                 if (s == "gather")    return 9;
101                 if (s == "flalign")   return 10;
102                 lyxerr << "unknown hull type '" << s << "'" << endl;
103                 return 0;
104         }
105
106         bool smaller(string const & s, string const & t)
107         {
108                 return typecode(s) < typecode(t);
109         }
110
111
112 } // end anon namespace
113
114
115
116 MathHullInset::MathHullInset()
117         : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
118 {
119         // This is needed as long the math parser is not re-entrant
120         initMath();
121         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
122         //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
123         //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
124         //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
125         setDefaults();
126 }
127
128
129 MathHullInset::MathHullInset(string const & type)
130         : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
131 {
132         setDefaults();
133 }
134
135
136 auto_ptr<InsetBase> MathHullInset::clone() const
137 {
138         return auto_ptr<InsetBase>(new MathHullInset(*this));
139 }
140
141
142 MathInset::mode_type MathHullInset::currentMode() const
143 {
144         if (type_ == "none")
145                 return UNDECIDED_MODE;
146         // definitely math mode ...
147         return MATH_MODE;
148 }
149
150
151 bool MathHullInset::idxFirst(LCursor & cur) const
152 {
153         cur.idx() = 0;
154         cur.pos() = 0;
155         return true;
156 }
157
158
159 bool MathHullInset::idxLast(LCursor & cur) const
160 {
161         cur.idx() = nargs() - 1;
162         cur.pos() = cur.lastpos();
163         return true;
164 }
165
166
167 char MathHullInset::defaultColAlign(col_type col)
168 {
169         if (type_ == "eqnarray")
170                 return "rcl"[col];
171         if (typecode(type_) >= typecode("align"))
172                 return "rl"[col & 1];
173         return 'c';
174 }
175
176
177 int MathHullInset::defaultColSpace(col_type col)
178 {
179         if (type_ == "align" || type_ == "alignat")
180                 return 0;
181         if (type_ == "xalignat")
182                 return (col & 1) ? 20 : 0;
183         if (type_ == "xxalignat" || type_ == "flalign")
184                 return (col & 1) ? 40 : 0;
185         return 0;
186 }
187
188
189 char const * MathHullInset::standardFont() const
190 {
191         if (type_ == "none")
192                 return "lyxnochange";
193         return "mathnormal";
194 }
195
196
197 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
198 {
199         FontSetChanger dummy1(mi.base, standardFont());
200         StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
201
202         // let the cells adjust themselves
203         MathGridInset::metrics(mi, dim);
204
205         if (display()) {
206                 dim.asc += 12;
207                 dim.des += 12;
208         }
209
210         if (numberedType()) {
211                 FontSetChanger dummy(mi.base, "mathbf");
212                 int l = 0;
213                 for (row_type row = 0; row < nrows(); ++row)
214                         l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
215
216                 if (l)
217                         dim.wid += 30 + l;
218         }
219
220         // make it at least as high as the current font
221         int asc = 0;
222         int des = 0;
223         math_font_max_dim(mi.base.font, asc, des);
224         dim.asc = max(dim.asc, asc);
225         dim.des = max(dim.des, des);
226
227         dim_ = dim;
228 }
229
230
231 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
232 {
233         FontSetChanger dummy1(pi.base, standardFont());
234         StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
235         MathGridInset::draw(pi, x + 1, y);
236
237         if (numberedType()) {
238                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
239                 for (row_type row = 0; row < nrows(); ++row) {
240                         int const yy = y + rowinfo_[row].offset_;
241                         FontSetChanger dummy(pi.base, "mathrm");
242                         drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
243                 }
244         }
245         setPosCache(pi, x, y);
246 }
247
248
249 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
250 {
251         if (display()) {
252                 MathGridInset::metricsT(mi, dim);
253         } else {
254                 ostringstream os;
255                 WriteStream wi(os, false, true);
256                 write(wi);
257                 dim.wid = os.str().size();
258                 dim.asc = 1;
259                 dim.des = 0;
260         }
261 }
262
263
264 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
265 {
266         if (display()) {
267                 MathGridInset::drawT(pain, x, y);
268         } else {
269                 ostringstream os;
270                 WriteStream wi(os, false, true);
271                 write(wi);
272                 pain.draw(x, y, os.str().c_str());
273         }
274 }
275
276
277 string MathHullInset::label(row_type row) const
278 {
279         row_type n = nrows();
280         BOOST_ASSERT(row < n);
281         return label_[row];
282 }
283
284
285 void MathHullInset::label(row_type row, string const & label)
286 {
287         //lyxerr << "setting label '" << label << "' for row " << row << endl;
288         label_[row] = label;
289 }
290
291
292 void MathHullInset::numbered(row_type row, bool num)
293 {
294         nonum_[row] = !num;
295 }
296
297
298 bool MathHullInset::numbered(row_type row) const
299 {
300         return !nonum_[row];
301 }
302
303
304 bool MathHullInset::ams() const
305 {
306         return
307                 type_ == "align" ||
308                 type_ == "flalign" ||
309                 type_ == "multline" ||
310                 type_ == "gather" ||
311                 type_ == "alignat" ||
312                 type_ == "xalignat" ||
313                 type_ == "xxalignat";
314 }
315
316
317 bool MathHullInset::display() const
318 {
319         return type_ != "simple" && type_ != "none";
320 }
321
322
323 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
324 {
325         for (row_type row = 0; row < nrows(); ++row)
326                 if (!label_[row].empty() && nonum_[row] != 1)
327                         labels.push_back(label_[row]);
328 }
329
330
331 bool MathHullInset::numberedType() const
332 {
333         if (type_ == "none")
334                 return false;
335         if (type_ == "simple")
336                 return false;
337         if (type_ == "xxalignat")
338                 return false;
339         for (row_type row = 0; row < nrows(); ++row)
340                 if (!nonum_[row])
341                         return true;
342         return false;
343 }
344
345
346 void MathHullInset::validate(LaTeXFeatures & features) const
347 {
348         if (ams())
349                 features.require("amsmath");
350
351
352         // Validation is necessary only if not using AMS math.
353         // To be safe, we will always run mathedvalidate.
354         //if (features.amsstyle)
355         //  return;
356
357         features.require("boldsymbol");
358         //features.binom      = true;
359
360         MathGridInset::validate(features);
361 }
362
363
364 void MathHullInset::header_write(WriteStream & os) const
365 {
366         bool n = numberedType();
367
368         if (type_ == "none")
369                 ;
370
371         else if (type_ == "simple") {
372                 os << '$';
373                 if (cell(0).empty())
374                         os << ' ';
375         }
376
377         else if (type_ == "equation") {
378                 if (n)
379                         os << "\\begin{equation" << star(n) << "}\n";
380                 else
381                         os << "\\[\n";
382         }
383
384         else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
385                  || type_ == "gather" || type_ == "multline")
386                         os << "\\begin{" << type_ << star(n) << "}\n";
387
388         else if (type_ == "alignat" || type_ == "xalignat")
389                 os << "\\begin{" << type_ << star(n) << '}'
390                   << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
391
392         else if (type_ == "xxalignat")
393                 os << "\\begin{" << type_ << '}'
394                   << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
395
396         else
397                 os << "\\begin{unknown" << star(n) << '}';
398 }
399
400
401 void MathHullInset::footer_write(WriteStream & os) const
402 {
403         bool n = numberedType();
404
405         if (type_ == "none")
406                 os << "\n";
407
408         else if (type_ == "simple")
409                 os << '$';
410
411         else if (type_ == "equation")
412                 if (n)
413                         os << "\\end{equation" << star(n) << "}\n";
414                 else
415                         os << "\\]\n";
416
417         else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
418                  || type_ == "alignat" || type_ == "xalignat"
419                  || type_ == "gather" || type_ == "multline")
420                 os << "\\end{" << type_ << star(n) << "}\n";
421
422         else if (type_ == "xxalignat")
423                 os << "\\end{" << type_ << "}\n";
424
425         else
426                 os << "\\end{unknown" << star(n) << '}';
427 }
428
429
430 bool MathHullInset::colChangeOK() const
431 {
432         return
433                 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
434                 type_ == "xalignat" || type_ == "xxalignat";
435 }
436
437
438 void MathHullInset::addRow(row_type row)
439 {
440         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
441         label_.insert(label_.begin() + row + 1, string());
442         MathGridInset::addRow(row);
443 }
444
445
446 void MathHullInset::swapRow(row_type row)
447 {
448         if (nrows() == 1)
449                 return;
450         if (row + 1 == nrows())
451                 --row;
452         swap(nonum_[row], nonum_[row + 1]);
453         swap(label_[row], label_[row + 1]);
454         MathGridInset::swapRow(row);
455 }
456
457
458 void MathHullInset::delRow(row_type row)
459 {
460         if (nrows() <= 1)
461                 return;
462         MathGridInset::delRow(row);
463         nonum_.erase(nonum_.begin() + row);
464         label_.erase(label_.begin() + row);
465 }
466
467
468 void MathHullInset::addCol(col_type col)
469 {
470         if (colChangeOK())
471                 MathGridInset::addCol(col);
472         else
473                 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
474 }
475
476
477 void MathHullInset::delCol(col_type col)
478 {
479         if (colChangeOK())
480                 MathGridInset::delCol(col);
481         else
482                 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
483 }
484
485
486 string MathHullInset::nicelabel(row_type row) const
487 {
488         if (nonum_[row])
489                 return string();
490         if (label_[row].empty())
491                 return string("(#)");
492         return '(' + label_[row] + ')';
493 }
494
495
496 void MathHullInset::glueall()
497 {
498         MathArray ar;
499         for (idx_type i = 0; i < nargs(); ++i)
500                 ar.append(cell(i));
501         *this = MathHullInset("simple");
502         cell(0) = ar;
503         setDefaults();
504 }
505
506
507 string const & MathHullInset::getType() const
508 {
509         return type_;
510 }
511
512
513 void MathHullInset::setType(string const & type)
514 {
515         type_ = type;
516         setDefaults();
517 }
518
519
520
521 void MathHullInset::mutate(string const & newtype)
522 {
523         lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
524
525         // we try to move along the chain
526         // none <-> simple <-> equation <-> eqnarray
527
528         if (newtype == "dump") {
529                 dump();
530         }
531
532         else if (newtype == type_) {
533                 // done
534         }
535
536         else if (type_ == "none") {
537                 setType("simple");
538                 numbered(0, false);
539                 mutate(newtype);
540         }
541
542         else if (type_ == "simple") {
543                 if (newtype == "none") {
544                         setType("none");
545                 } else {
546                         setType("equation");
547                         numbered(0, false);
548                         mutate(newtype);
549                 }
550         }
551
552         else if (type_ == "equation") {
553                 if (smaller(newtype, type_)) {
554                         setType("simple");
555                         mutate(newtype);
556                 } else if (newtype == "eqnarray") {
557                         MathGridInset::addCol(1);
558                         MathGridInset::addCol(1);
559
560                         // split it "nicely" on the firest relop
561                         pos_type pos = firstRelOp(cell(0));
562                         cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
563                         cell(0).erase(pos, cell(0).size());
564
565                         if (cell(1).size()) {
566                                 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
567                                 cell(1).erase(1, cell(1).size());
568                         }
569                         setType("eqnarray");
570                         mutate(newtype);
571                 } else if (newtype == "multline" || newtype == "gather") {
572                         setType(newtype);
573                         numbered(0, false);
574                 } else {
575                         MathGridInset::addCol(1);
576                         // split it "nicely"
577                         pos_type pos = firstRelOp(cell(0));
578                         cell(1) = cell(0);
579                         cell(0).erase(pos, cell(0).size());
580                         cell(1).erase(0, pos);
581                         setType("align");
582                         mutate(newtype);
583                 }
584         }
585
586         else if (type_ == "eqnarray") {
587                 if (smaller(newtype, type_)) {
588                         // set correct (no)numbering
589                         bool allnonum = true;
590                         for (row_type row = 0; row < nrows(); ++row)
591                                 if (!nonum_[row])
592                                         allnonum = false;
593
594                         // set first non-empty label
595                         string label;
596                         for (row_type row = 0; row < nrows(); ++row) {
597                                 if (!label_[row].empty()) {
598                                         label = label_[row];
599                                         break;
600                                 }
601                         }
602
603                         glueall();
604                         nonum_[0] = allnonum;
605                         label_[0] = label;
606                         mutate(newtype);
607                 } else { // align & Co.
608                         for (row_type row = 0; row < nrows(); ++row) {
609                                 idx_type c = 3 * row + 1;
610                                 cell(c).append(cell(c + 1));
611                         }
612                         MathGridInset::delCol(2);
613                         setType("align");
614                         mutate(newtype);
615                 }
616         }
617
618         else if (type_ == "align") {
619                 if (smaller(newtype, type_)) {
620                         MathGridInset::addCol(1);
621                         setType("eqnarray");
622                         mutate(newtype);
623                 } else {
624                         setType(newtype);
625                 }
626         }
627
628         else if (type_ == "multline") {
629                 if (newtype == "gather" || newtype == "align" ||
630                     newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
631                         setType(newtype);
632                 else if (newtype == "eqnarray") {
633                         MathGridInset::addCol(1);
634                         MathGridInset::addCol(1);
635                         setType("eqnarray");
636                 } else {
637                         lyxerr << "mutation from '" << type_
638                                 << "' to '" << newtype << "' not implemented" << endl;
639                 }
640         }
641
642         else if (type_ == "gather") {
643                 if (newtype == "multline") {
644                         setType("multline");
645                 } else {
646                         lyxerr << "mutation from '" << type_
647                                 << "' to '" << newtype << "' not implemented" << endl;
648                 }
649         }
650
651         else {
652                 lyxerr << "mutation from '" << type_
653                                          << "' to '" << newtype << "' not implemented" << endl;
654         }
655 }
656
657
658 string MathHullInset::eolString(row_type row, bool fragile) const
659 {
660         string res;
661         if (numberedType()) {
662                 if (!label_[row].empty() && !nonum_[row])
663                         res += "\\label{" + label_[row] + '}';
664                 if (nonum_[row] && (type_ != "multline"))
665                         res += "\\nonumber ";
666         }
667         return res + MathGridInset::eolString(row, fragile);
668 }
669
670
671 void MathHullInset::write(WriteStream & os) const
672 {
673         header_write(os);
674         MathGridInset::write(os);
675         footer_write(os);
676 }
677
678
679 void MathHullInset::normalize(NormalStream & os) const
680 {
681         os << "[formula " << type_ << ' ';
682         MathGridInset::normalize(os);
683         os << "] ";
684 }
685
686
687 void MathHullInset::mathmlize(MathMLStream & os) const
688 {
689         MathGridInset::mathmlize(os);
690 }
691
692
693 void MathHullInset::infoize(ostream & os) const
694 {
695         os << "Type: " << type_;
696 }
697
698
699 void MathHullInset::check() const
700 {
701         BOOST_ASSERT(nonum_.size() == nrows());
702         BOOST_ASSERT(label_.size() == nrows());
703 }
704
705
706 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
707 {
708         string lang;
709         string extra;
710         istringstream iss(func.argument.c_str());
711         iss >> lang >> extra;
712         if (extra.empty())
713                 extra = "noextra";
714
715 #ifdef WITH_WARNINGS
716 #warning temporarily disabled
717         //if (cur.selection()) {
718         //      MathArray ar;
719         //      selGet(cur.ar);
720         //      lyxerr << "use selection: " << ar << endl;
721         //      insert(pipeThroughExtern(lang, extra, ar));
722         //      return;
723         //}
724 #endif
725
726         MathArray eq;
727         eq.push_back(MathAtom(new MathCharInset('=')));
728
729         // go to first item in line
730         cur.idx() -= cur.idx() % ncols();
731         cur.pos() = 0;
732
733         if (getType() == "simple") {
734                 size_type pos = cur.cell().find_last(eq);
735                 MathArray ar;
736                 if (cur.inMathed() && cur.selection()) {
737                         asArray(cur.grabAndEraseSelection(), ar);
738                 } else if (pos == cur.cell().size()) {
739                         ar = cur.cell();
740                         lyxerr << "use whole cell: " << ar << endl;
741                 } else {
742                         ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
743                         lyxerr << "use partial cell form pos: " << pos << endl;
744                 }
745                 cur.cell().append(eq);
746                 cur.cell().append(pipeThroughExtern(lang, extra, ar));
747                 cur.pos() = cur.lastpos();
748                 return;
749         }
750
751         if (getType() == "equation") {
752                 lyxerr << "use equation inset" << endl;
753                 mutate("eqnarray");
754                 MathArray & ar = cur.cell();
755                 lyxerr << "use cell: " << ar << endl;
756                 ++cur.idx();
757                 cur.cell() = eq;
758                 ++cur.idx();
759                 cur.cell() = pipeThroughExtern(lang, extra, ar);
760                 // move to end of line
761                 cur.pos() = cur.lastpos();
762                 return;
763         }
764
765         {
766                 lyxerr << "use eqnarray" << endl;
767                 cur.idx() += 2 - cur.idx() % ncols();
768                 cur.pos() = 0;
769                 MathArray ar = cur.cell();
770                 lyxerr << "use cell: " << ar << endl;
771 #ifdef WITH_WARNINGS
772 #warning temporarily disabled
773 #endif
774                 addRow(cur.row());
775                 ++cur.idx();
776                 ++cur.idx();
777                 cur.cell() = eq;
778                 ++cur.idx();
779                 cur.cell() = pipeThroughExtern(lang, extra, ar);
780                 cur.pos() = cur.lastpos();
781         }
782 }
783
784
785 void MathHullInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
786 {
787         //lyxerr << "*** MathHullInset: request: " << cmd << endl;
788         switch (cmd.action) {
789
790         case LFUN_BREAKLINE:
791                 if (type_ == "simple" || type_ == "equation") {
792                         mutate("eqnarray");
793                         cur.idx() = 1;
794                         cur.pos() = 0;
795                         //cur.dispatched(FINISHED);
796                         break;
797                 }
798                 MathGridInset::priv_dispatch(cur, cmd);
799                 break;
800
801         case LFUN_MATH_NUMBER:
802                 //lyxerr << "toggling all numbers" << endl;
803                 if (display()) {
804                         recordUndo(cur);
805                         bool old = numberedType();
806                         if (type_ == "multline")
807                                 numbered(nrows() - 1, !old);
808                         else
809                                 for (row_type row = 0; row < nrows(); ++row)
810                                         numbered(row, !old);
811                         cur.message(old ? _("No number") : _("Number"));
812                 }
813                 break;
814
815         case LFUN_MATH_NONUMBER:
816                 if (display()) {
817                         row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
818                         recordUndo(cur);
819                         bool old = numbered(r);
820                         cur.message(old ? _("No number") : _("Number"));
821                         numbered(r, !old);
822                 }
823                 break;
824
825         case LFUN_INSERT_LABEL: {
826                 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
827                 string old_label = label(r);
828                 string new_label = cmd.argument;
829
830                 if (new_label.empty()) {
831                         string const default_label =
832                                 (lyxrc.label_init_length >= 0) ? "eq:" : "";
833                         pair<bool, string> const res = old_label.empty()
834                                 ? Alert::askForText(_("Enter new label to insert:"), default_label)
835                                 : Alert::askForText(_("Enter label:"), old_label);
836                         new_label = lyx::support::trim(res.second);
837                 }
838
839                 if (!new_label.empty())
840                         numbered(r, true);
841                 label(r, new_label);
842                 break;
843         }
844
845         case LFUN_MATH_EXTERN:
846                 doExtern(cur, cmd);
847                 //cur.dispatched(FINISHED);
848                 break;
849
850         case LFUN_MATH_MUTATE: {
851                 lyxerr << "Hull: MUTATE: " << cmd.argument << endl;
852                 row_type row = cur.row();
853                 col_type col = cur.col();
854                 mutate(cmd.argument);
855                 cur.idx() = row * ncols() + col;
856                 if (cur.idx() > cur.lastidx()) {
857                         cur.idx() = cur.lastidx();
858                         cur.pos() = cur.lastpos();
859                 }
860                 if (cur.pos() > cur.lastpos())
861                         cur.pos() = cur.lastpos();
862                 //cur.dispatched(FINISHED);
863                 break;
864         }
865
866         case LFUN_MATH_DISPLAY: {
867                 mutate(type_ == "simple" ? "equation" : "simple");
868                 cur.idx() = 0;
869                 cur.pos() = cur.lastpos();
870                 //cur.dispatched(FINISHED);
871                 break;
872         }
873
874         default:
875                 MathGridInset::priv_dispatch(cur, cmd);
876                 break;
877         }
878 }
879
880
881 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
882                 FuncStatus & flag) const
883 {
884         switch (cmd.action) {
885         case LFUN_BREAKLINE:
886         case LFUN_MATH_NUMBER:
887         case LFUN_MATH_NONUMBER:
888         case LFUN_INSERT_LABEL:
889         case LFUN_MATH_EXTERN:
890         case LFUN_MATH_MUTATE:
891         case LFUN_MATH_DISPLAY:
892                 // we handle these
893                 return true;
894         default:
895                 return MathGridInset::getStatus(cur, cmd, flag);
896         }
897 }
898
899
900 /////////////////////////////////////////////////////////////////////
901
902 #include "formulamacro.h"
903 #include "math_arrayinset.h"
904 #include "math_deliminset.h"
905 #include "math_factory.h"
906 #include "math_parser.h"
907 #include "math_spaceinset.h"
908 #include "ref_inset.h"
909
910 #include "bufferview_funcs.h"
911 #include "lyxtext.h"
912 #include "undo.h"
913
914 #include "frontends/LyXView.h"
915 #include "frontends/Dialogs.h"
916
917 #include "support/std_sstream.h"
918 #include "support/lstrings.h"
919 #include "support/lyxlib.h"
920
921
922 // simply scrap this function if you want
923 void MathHullInset::mutateToText()
924 {
925 #if 0
926         // translate to latex
927         ostringstream os;
928         latex(NULL, os, false, false);
929         string str = os.str();
930
931         // insert this text
932         LyXText * lt = view_->getLyXText();
933         string::const_iterator cit = str.begin();
934         string::const_iterator end = str.end();
935         for (; cit != end; ++cit)
936                 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
937
938         // remove ourselves
939         //view_->owner()->dispatch(LFUN_ESCAPE);
940 #endif
941 }
942
943
944 void MathHullInset::handleFont(LCursor & cur, string const & arg,
945         string const & font)
946 {
947         // this whole function is a hack and won't work for incremental font
948         // changes...
949         recordUndo(cur);
950         if (cur.inset().asMathInset()->name() == font)
951                 cur.handleFont(font);
952         else {
953                 cur.handleNest(createMathInset(font));
954                 cur.insert(arg);
955         }
956 }
957
958
959 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
960 {
961         recordUndo(cur);
962         LyXFont font;
963         bool b;
964         bv_funcs::string2font(arg, font, b);
965         if (font.color() != LColor::inherit) {
966                 MathAtom at = createMathInset("color");
967                 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
968                 cur.handleNest(at, 1);
969         }
970 }
971
972
973 void MathHullInset::edit(LCursor & cur, bool left)
974 {
975         lyxerr << "MathHullInset: edit left/right" << endl;
976         cur.push(*this);
977 }
978
979
980 string const MathHullInset::editMessage() const
981 {
982         return _("Math editor mode");
983 }
984
985
986 void MathHullInset::getCursorDim(int & asc, int & desc) const
987 {
988         asc = 10;
989         desc = 2;
990         //math_font_max_dim(font_, asc, des);
991 }
992
993
994 void MathHullInset::revealCodes(LCursor & cur) const
995 {
996         if (!cur.inMathed())
997                 return;
998         ostringstream os;
999         cur.info(os);
1000         cur.message(os.str());
1001 /*
1002         // write something to the minibuffer
1003         // translate to latex
1004         cur.markInsert(bv);
1005         ostringstream os;
1006         write(NULL, os);
1007         string str = os.str();
1008         cur.markErase(bv);
1009         string::size_type pos = 0;
1010         string res;
1011         for (string::iterator it = str.begin(); it != str.end(); ++it) {
1012                 if (*it == '\n')
1013                         res += ' ';
1014                 else if (*it == '\0') {
1015                         res += "  -X-  ";
1016                         pos = it - str.begin();
1017                 }
1018                 else
1019                         res += *it;
1020         }
1021         if (pos > 30)
1022                 res = res.substr(pos - 30);
1023         if (res.size() > 60)
1024                 res = res.substr(0, 60);
1025         cur.message(res);
1026 */
1027 }
1028
1029
1030 InsetBase::Code MathHullInset::lyxCode() const
1031 {
1032         return MATH_CODE;
1033 }
1034
1035
1036 /////////////////////////////////////////////////////////////////////
1037
1038
1039 #if 1
1040 bool MathHullInset::searchForward(BufferView *, string const &, bool, bool)
1041 {
1042         return false;
1043 }
1044
1045 #else
1046 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1047                                      bool, bool)
1048 {
1049         return false;
1050 #ifdef WITH_WARNINGS
1051 #warning pretty ugly
1052 #endif
1053         static MathHullInset * lastformula = 0;
1054         static CursorBase current = DocIterator(ibegin(nucleus()));
1055         static MathArray ar;
1056         static string laststr;
1057
1058         if (lastformula != this || laststr != str) {
1059                 //lyxerr << "reset lastformula to " << this << endl;
1060                 lastformula = this;
1061                 laststr = str;
1062                 current = ibegin(nucleus());
1063                 ar.clear();
1064                 mathed_parse_cell(ar, str);
1065         } else {
1066                 increment(current);
1067         }
1068         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1069
1070         for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1071                 CursorSlice & top = it.back();
1072                 MathArray const & a = top.asMathInset()->cell(top.idx_);
1073                 if (a.matchpart(ar, top.pos_)) {
1074                         bv->cursor().setSelection(it, ar.size());
1075                         current = it;
1076                         top.pos_ += ar.size();
1077                         bv->update();
1078                         return true;
1079                 }
1080         }
1081
1082         //lyxerr << "not found!" << endl;
1083         lastformula = 0;
1084         return false;
1085 }
1086 #endif
1087
1088
1089 bool MathHullInset::searchBackward(BufferView * bv, string const & what,
1090                                       bool a, bool b)
1091 {
1092         lyxerr[Debug::MATHED]
1093                 << "searching backward not implemented in mathed" << endl;
1094         return searchForward(bv, what, a, b);
1095 }
1096
1097
1098 void MathHullInset::write(Buffer const &, std::ostream & os) const
1099 {
1100         WriteStream wi(os, false, false);
1101         os << "Formula ";
1102         write(wi);
1103 }
1104
1105
1106 void MathHullInset::read(Buffer const &, LyXLex & lex)
1107 {
1108         MathAtom at;
1109         mathed_parse_normal(at, lex);
1110         operator=(*at->asHullInset());
1111 }
1112
1113
1114 int MathHullInset::plaintext(Buffer const &, ostream & os,
1115                         OutputParams const &) const
1116 {
1117         if (0 && display()) {
1118                 Dimension dim;
1119                 TextMetricsInfo mi;
1120                 metricsT(mi, dim);
1121                 TextPainter tpain(dim.width(), dim.height());
1122                 drawT(tpain, 0, dim.ascent());
1123                 tpain.show(os, 3);
1124                 // reset metrics cache to "real" values
1125                 //metrics();
1126                 return tpain.textheight();
1127         } else {
1128                 WriteStream wi(os, false, true);
1129                 wi << ' ' << cell(0) << ' ';
1130                 return wi.line();
1131         }
1132 }
1133
1134
1135 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1136                            OutputParams const & runparams) const
1137 {
1138         return docbook(buf, os, runparams);
1139 }
1140
1141
1142 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1143                           OutputParams const & runparams) const
1144 {
1145         MathMLStream ms(os);
1146         ms << MTag("equation");
1147         ms <<   MTag("alt");
1148         ms <<    "<[CDATA[";
1149         int res = plaintext(buf, ms.os(), runparams);
1150         ms <<    "]]>";
1151         ms <<   ETag("alt");
1152         ms <<   MTag("math");
1153         MathGridInset::mathmlize(ms);
1154         ms <<   ETag("math");
1155         ms << ETag("equation");
1156         return ms.line() + res;
1157 }