]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
layout as layout
[lyx.git] / src / mathed / formulabase.C
1 /*
2 *  File:        formulabase.C
3 *  Purpose:     Implementation of common parts of the LyX  math insets
4 *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 *  Created:     January 1996
6 *
7 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
8 *
9 *  Version: 0.4, Lyx project.
10 *
11 *   You are free to use and modify this code under the terms of
12 *   the GNU General Public Licence version 2 or later.
13 */
14
15 #include <config.h>
16 #include <fstream>
17
18 #include "Lsstream.h"
19 #include "support/LAssert.h"
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "formula.h"
26 #include "formulamacro.h"
27 #include "commandtags.h"
28 #include "BufferView.h"
29 #include "lyxtext.h"
30 #include "lyxfunc.h"
31 #include "gettext.h"
32 #include "LaTeXFeatures.h"
33 #include "debug.h"
34 #include "math_support.h"
35 #include "support/lstrings.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/font_metrics.h"
38 #include "frontends/mouse_state.h"
39 #include "Lsstream.h"
40 #include "math_arrayinset.h"
41 #include "math_charinset.h"
42 #include "math_cursor.h"
43 #include "math_factory.h"
44 #include "math_fontinset.h"
45 #include "math_hullinset.h"
46 #include "math_iterator.h"
47 #include "math_macrotable.h"
48 #include "math_parser.h"
49 #include "math_pos.h"
50 #include "math_spaceinset.h"
51 #include "undo_funcs.h"
52 #include "textpainter.h"
53 #include "frontends/Dialogs.h"
54 #include "intl.h"
55 #include "../insets/insetcommand.h"
56
57 #include "ref_inset.h"
58
59 using std::endl;
60 using std::ostream;
61 using std::vector;
62 using std::abs;
63
64 MathCursor * mathcursor = 0;
65
66
67 namespace {
68
69 // local global
70 int first_x;
71 int first_y;
72
73
74
75 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
76 {
77         if (!bv->insertInset(new_inset)) {
78                 delete new_inset;
79                 return false;
80         }
81         new_inset->edit(bv, 0, 0, mouse_button::none);
82         return true;
83 }
84
85
86 } // namespace anon
87
88
89
90 InsetFormulaBase::InsetFormulaBase()
91         : view_(0), font_(), xo_(0), yo_(0)
92 {
93         // This is needed as long the math parser is not re-entrant
94         MathMacroTable::builtinMacros();
95         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
96         //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
97         //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
98         //lyxerr << "sizeof(LyXFont): " << sizeof(LyXFont) << "\n";
99 }
100
101
102 // simply scrap this function if you want
103 void InsetFormulaBase::mutateToText()
104 {
105 #if 0
106         // translate to latex
107         ostringstream os;
108         latex(NULL, os, false, false);
109         string str = os.str();
110
111         // insert this text
112         LyXText * lt = view_->getLyXText();
113         string::const_iterator cit = str.begin();
114         string::const_iterator end = str.end();
115         for (; cit != end; ++cit)
116                 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
117
118         // remove ourselves
119         //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
120 #endif
121 }
122
123
124 void InsetFormulaBase::handleFont
125         (BufferView * bv, string const & arg, string const & font)
126 {
127         bv->lockedInsetStoreUndo(Undo::EDIT);
128         bool sel = mathcursor->selection();
129         if (sel)
130                 updateLocal(bv, true);
131         mathcursor->handleNest(new MathFontInset(font));
132         mathcursor->insert(arg);
133         if (!sel)
134                 updateLocal(bv, false);
135 }
136
137
138 // Check if uses AMS macros
139 void InsetFormulaBase::validate(LaTeXFeatures &) const
140 {}
141
142
143 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
144 {
145         font_ = f;
146         metrics(bv);
147 }
148
149
150 void InsetFormulaBase::metrics(BufferView * bv) const
151 {
152         if (bv)
153                 view_ = bv;
154         MathMetricsInfo mi;
155         mi.view       = view_;
156         mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
157         mi.base.font  = font_;
158         mi.base.font.setColor(LColor::math);
159         par()->metrics(mi);
160 }
161
162
163 string const InsetFormulaBase::editMessage() const
164 {
165         return _("Math editor mode");
166 }
167
168
169 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
170 {
171         if (!bv->lockInset(this))
172                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
173         delete mathcursor;
174         mathcursor = new MathCursor(this, true);
175         metrics(bv);
176         mathcursor->setPos(x, y);
177         //lyxerr << "setting pos to " << x << "," << y << "\n";
178
179         // if that is removed, we won't get the magenta box when entering an
180         // inset for the first time
181         bv->updateInset(this, false);
182 }
183
184
185 void InsetFormulaBase::edit(BufferView * bv, bool front)
186 {
187         if (!bv->lockInset(this))
188                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
189         delete mathcursor;
190         mathcursor = new MathCursor(this, front);
191         metrics(bv);
192         bv->updateInset(this, false);
193 }
194
195
196 void InsetFormulaBase::insetUnlock(BufferView * bv)
197 {
198         if (mathcursor) {
199                 if (mathcursor->inMacroMode()) {
200                         mathcursor->macroModeClose();
201                         updateLocal(bv, true);
202                 }
203                 delete mathcursor;
204                 mathcursor = 0;
205         }
206         bv->updateInset(this, false);
207 }
208
209
210 void InsetFormulaBase::getCursorPos(BufferView * bv, int & x, int & y) const
211 {
212         metrics(bv);
213         mathcursor->getPos(x, y);
214         //x -= xo_;
215         y -= yo_;
216         //lyxerr << "getCursorPos: " << x << " " << y << "\n";
217 }
218
219
220 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
221 {
222         if (!mathcursor)
223                 return;
224
225         if (isCursorVisible())
226                 bv->hideLockedInsetCursor();
227         else {
228                 metrics(bv);
229                 int x;
230                 int y;
231                 mathcursor->getPos(x, y);
232                 y -= yo_;
233                 int asc = 0;
234                 int des = 0;
235                 math_font_max_dim(font_, asc, des);
236                 bv->showLockedInsetCursor(x, y, asc, des);
237                 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
238         }
239
240         toggleCursorVisible();
241 }
242
243
244 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
245 {
246         if (isCursorVisible())
247                 return;
248         if (mathcursor) {
249                 metrics(bv);
250                 int x;
251                 int y;
252                 mathcursor->getPos(x, y);
253                 y -= yo_;
254                 int asc = 0;
255                 int des = 0;
256                 math_font_max_dim(font_, asc, des);
257                 bv->fitLockedInsetCursor(x, y, asc, des);
258                 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
259         }
260         toggleInsetCursor(bv);
261 }
262
263
264 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
265 {
266         if (isCursorVisible())
267                 toggleInsetCursor(bv);
268 }
269
270
271 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
272 {
273         if (!mathcursor)
274                 return;
275
276         int const asc = font_metrics::maxAscent(font_);
277         int const desc = font_metrics::maxDescent(font_);
278         int x, y;
279
280         getCursorPos(bv, x, y);
281         bv->fitLockedInsetCursor(x, y, asc, desc);
282 }
283
284
285 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
286 {
287         if (mathcursor)
288                 bv->updateInset(this, false);
289 }
290
291
292 vector<string> const InsetFormulaBase::getLabelList() const
293 {
294   return vector<string>();
295 }
296
297
298 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
299 {
300         metrics(bv);
301         if (mathcursor)
302                 bv->fitCursor();
303         bv->updateInset(this, dirty);
304 }
305
306
307 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
308         int /*x*/, int /*y*/, mouse_button::state button)
309 {
310         //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
311
312         if (!mathcursor)
313                 return false;
314         hideInsetCursor(bv);
315         showInsetCursor(bv);
316         bv->updateInset(this, false);
317
318         if (button == mouse_button::button3) {
319                 // try to dispatch to enclosed insets first
320                 if (mathcursor->dispatch("mouse 3"))
321                         return true;
322
323                 // launch math panel for right mouse button
324                 bv->owner()->getDialogs()->showMathPanel();
325                 return true;
326         }
327         return false;
328 }
329
330
331 void InsetFormulaBase::insetButtonPress(BufferView * bv,
332                                         int x, int y, mouse_button::state button)
333 {
334         //lyxerr << "insetButtonPress: "
335         //      << x << " " << y << " but: " << button << "\n";
336 #if 0
337         switch (button) {
338                 default:
339                 case 1:
340                         // left click
341                         delete mathcursor;
342                         mathcursor = new MathCursor(this, x == 0);
343                         metrics(bv);
344                         first_x = x;
345                         first_y = y;
346                         mathcursor->selClear();
347                         mathcursor->setPos(x + xo_, y + yo_);
348                         break;
349 /*
350                 case 2:
351                         lyxerr << "insetButtonPress: 2\n";
352                         // insert stuff
353                         if (mathcursor) {
354                                 bv->lockedInsetStoreUndo(Undo::EDIT);
355                                 MathArray ar;
356                                 mathcursor->selGet(ar);
357                                 mathcursor->setPos(x + xo_, y + yo_);
358                                 string sel =
359                                         bv->getLyXText()->selectionAsString(bv->buffer(), false);
360                                 mathed_parse_cell(ar, sel);
361                                 mathcursor->insert(ar);
362                         }
363                         break;
364 */
365                 case 3:
366                         // launch math panel for right mouse button
367                         bv->owner()->getDialogs()->showMathPanel();
368                         break;
369         }
370 #else
371         if (button == mouse_button::button1 || !mathcursor) {
372                 delete mathcursor;
373                 mathcursor = new MathCursor(this, x == 0);
374                 metrics(bv);
375                 first_x = x;
376                 first_y = y;
377                 mathcursor->selClear();
378                 mathcursor->setPos(x + xo_, y + yo_);
379         }
380 #if 0
381 #warning Never launch a Dialog on "Press" event ONLY on "Release" event!
382         if (button == 3) {
383                 // launch math panel for right mouse button
384                 bv->owner()->getDialogs()->showMathPanel();
385         }
386 #endif
387 #endif
388         bv->updateInset(this, false);
389 }
390
391
392 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
393         int x, int y, mouse_button::state)
394 {
395         if (!mathcursor)
396                 return;
397
398         if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
399                 //lyxerr << "insetMotionNotify: ignored\n";
400                 return;
401         }
402         first_x = x;
403         first_y = y;
404
405         if (!mathcursor->selection())
406                 mathcursor->selStart();
407
408         //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
409         //      << ' ' << button << "\n";
410         hideInsetCursor(bv);
411         mathcursor->setPos(x + xo_, y + yo_);
412         showInsetCursor(bv);
413         bv->updateInset(this, false);
414 }
415
416
417 UpdatableInset::RESULT
418 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
419                             string const & arg)
420 {
421         //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
422         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
423
424         if (!mathcursor)
425                 return UNDISPATCHED;
426
427         RESULT result      = DISPATCHED;
428         bool sel           = false;
429         bool was_macro     = mathcursor->inMacroMode();
430         bool was_selection = mathcursor->selection();
431
432         hideInsetCursor(bv);
433
434         mathcursor->normalize();
435         mathcursor->touch();
436
437         switch (action) {
438
439         // --- Cursor Movements ---------------------------------------------
440
441         case LFUN_RIGHTSEL:
442                 sel = true; // fall through...
443
444         case LFUN_RIGHT:
445                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
446                 //lyxerr << "calling scroll 20\n";
447                 //scroll(bv, 20);
448                 updateLocal(bv, false);
449                 // write something to the minibuffer
450                 //bv->owner()->message(mathcursor->info());
451                 break;
452
453
454         case LFUN_LEFTSEL:
455                 sel = true; // fall through
456
457         case LFUN_LEFT:
458                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
459                 updateLocal(bv, false);
460                 break;
461
462
463         case LFUN_UPSEL:
464                 sel = true;
465
466         case LFUN_UP:
467                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
468                 updateLocal(bv, false);
469                 break;
470
471
472         case LFUN_DOWNSEL:
473                 sel = true;
474
475         case LFUN_DOWN:
476                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
477                 updateLocal(bv, false);
478                 break;
479
480         case LFUN_HOMESEL:
481                 sel = true;
482
483         case LFUN_HOME:
484                 mathcursor->home(sel);
485                 updateLocal(bv, false);
486                 break;
487
488         case LFUN_ENDSEL:
489                 sel = true;
490
491         case LFUN_END:
492                 mathcursor->end(sel);
493                 updateLocal(bv, false);
494                 break;
495
496         case LFUN_DELETE_LINE_FORWARD:
497                 bv->lockedInsetStoreUndo(Undo::DELETE);
498                 mathcursor->delLine();
499                 updateLocal(bv, true);
500                 break;
501
502         case LFUN_TAB:
503                 mathcursor->idxNext();
504                 updateLocal(bv, false);
505                 break;
506
507         case LFUN_SHIFT_TAB:
508                 mathcursor->idxPrev();
509                 updateLocal(bv, false);
510                 break;
511
512         case LFUN_TABINSERT:
513                 bv->lockedInsetStoreUndo(Undo::EDIT);
514                 mathcursor->splitCell();
515                 updateLocal(bv, true);
516                 break;
517
518         case LFUN_DELETE_WORD_BACKWARD:
519         case LFUN_BACKSPACE:
520                 bv->lockedInsetStoreUndo(Undo::DELETE);
521                 mathcursor->backspace();
522                 bv->updateInset(this, true);
523                 break;
524
525         case LFUN_DELETE_WORD_FORWARD:
526         case LFUN_DELETE:
527                 bv->lockedInsetStoreUndo(Undo::DELETE);
528                 mathcursor->erase();
529                 bv->updateInset(this, true);
530                 break;
531
532         //    case LFUN_GETXY:
533         //      sprintf(dispatch_buffer, "%d %d",);
534         //      dispatch_result = dispatch_buffer;
535         //      break;
536         case LFUN_SETXY: {
537                 lyxerr << "LFUN_SETXY broken!\n";
538                 int x = 0;
539                 int y = 0;
540                 istringstream is(arg.c_str());
541                 is >> x >> y;
542                 mathcursor->setPos(x, y);
543                 updateLocal(bv, false);
544                 break;
545         }
546
547         case LFUN_PASTE:
548                 if (was_macro)
549                         mathcursor->macroModeClose();
550                 bv->lockedInsetStoreUndo(Undo::EDIT);
551                 mathcursor->selPaste();
552                 updateLocal(bv, true);
553                 break;
554
555         case LFUN_CUT:
556                 bv->lockedInsetStoreUndo(Undo::DELETE);
557                 mathcursor->selCut();
558                 updateLocal(bv, true);
559                 break;
560
561         case LFUN_COPY:
562                 mathcursor->selCopy();
563                 break;
564
565         case LFUN_WORDRIGHTSEL:
566         case LFUN_WORDLEFTSEL:
567                 break;
568
569         // Special casing for superscript in case of LyX handling
570         // dead-keys:
571         case LFUN_CIRCUMFLEX:
572                 if (arg.empty()) {
573                         // do superscript if LyX handles
574                         // deadkeys
575                         bv->lockedInsetStoreUndo(Undo::EDIT);
576                         mathcursor->script(true);
577                         updateLocal(bv, true);
578                 }
579                 break;
580         case LFUN_UMLAUT:
581         case LFUN_ACUTE:
582         case LFUN_GRAVE:
583         case LFUN_BREVE:
584         case LFUN_DOT:
585         case LFUN_MACRON:
586         case LFUN_CARON:
587         case LFUN_TILDE:
588         case LFUN_CEDILLA:
589         case LFUN_CIRCLE:
590         case LFUN_UNDERDOT:
591         case LFUN_TIE:
592         case LFUN_OGONEK:
593         case LFUN_HUNG_UMLAUT:
594                 break;
595
596         //  Math fonts
597         case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
598         case LFUN_BOLD:         handleFont(bv, arg, "textbf"); break;
599         case LFUN_SANS:         handleFont(bv, arg, "textsf"); break;
600         case LFUN_EMPH:         handleFont(bv, arg, "mathcal"); break;
601         case LFUN_ROMAN:        handleFont(bv, arg, "mathrm"); break;
602         case LFUN_CODE:         handleFont(bv, arg, "texttt"); break;
603         case LFUN_FRAK:         handleFont(bv, arg, "mathfrak"); break;
604         case LFUN_ITAL:         handleFont(bv, arg, "mathit"); break;
605         case LFUN_NOUN:         handleFont(bv, arg, "mathbb"); break;
606         case LFUN_DEFAULT:      handleFont(bv, arg, "textnormal"); break;
607         case LFUN_FREE:         handleFont(bv, arg, "textrm"); break;
608
609         case LFUN_GREEK:
610                 handleFont(bv, arg, "lyxgreek1");
611                 if (arg.size())
612                         mathcursor->interpret(arg);
613                 break;
614
615         case LFUN_MATH_MODE:
616                 if (mathcursor->inMathMode()) {
617                         handleFont(bv, arg, "textrm");
618                 } else {
619                         mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
620                         updateLocal(bv, true);
621                 }
622                 //bv->owner()->message(_("math text mode toggled"));
623                 break;
624
625         case LFUN_MATH_LIMITS:
626                 bv->lockedInsetStoreUndo(Undo::EDIT);
627                 if (mathcursor->toggleLimits())
628                         updateLocal(bv, true);
629                 break;
630
631         case LFUN_MATH_SIZE:
632 #if 0
633                 if (!arg.empty()) {
634                         bv->lockedInsetStoreUndo(Undo::EDIT);
635                         mathcursor->setSize(arg);
636                         updateLocal(bv, true);
637                 }
638 #endif
639                 break;
640
641         case LFUN_INSERT_MATRIX:
642                 if (!arg.empty()) {
643                         bv->lockedInsetStoreUndo(Undo::EDIT);
644                         mathcursor->interpret("matrix " + arg);
645                         updateLocal(bv, true);
646                 }
647                 break;
648
649         case LFUN_SUPERSCRIPT:
650         case LFUN_SUBSCRIPT:
651         {
652                 bv->lockedInsetStoreUndo(Undo::EDIT);
653                 mathcursor->script(action == LFUN_SUPERSCRIPT);
654                 updateLocal(bv, true);
655                 break;
656         }
657
658         case LFUN_MATH_DELIM:
659         {
660                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
661                 string ls;
662                 string rs = split(arg, ls, ' ');
663                 // Reasonable default values
664                 if (ls.empty())
665                         ls = '(';
666                 if (rs.empty())
667                         rs = ')';
668
669                 bv->lockedInsetStoreUndo(Undo::EDIT);
670                 mathcursor->handleDelim(ls, rs);
671                 updateLocal(bv, true);
672                 break;
673         }
674
675         case LFUN_PROTECTEDSPACE:
676         case LFUN_MATH_SPACE:
677                 bv->lockedInsetStoreUndo(Undo::EDIT);
678                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
679                 updateLocal(bv, true);
680                 break;
681
682         case LFUN_UNDO:
683                 bv->owner()->message(_("Invalid action in math mode!"));
684                 break;
685
686
687         case LFUN_MATH_HALIGN:
688         case LFUN_MATH_VALIGN:
689         case LFUN_MATH_ROW_INSERT:
690         case LFUN_MATH_ROW_DELETE:
691         case LFUN_MATH_COLUMN_INSERT:
692         case LFUN_MATH_COLUMN_DELETE:
693         {
694                 MathInset::idx_type idx = 0;
695                 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
696                 if (p) {
697                         mathcursor->popToEnclosingGrid();
698                         bv->lockedInsetStoreUndo(Undo::EDIT);
699                         char align = arg.size() ? arg[0] : 'c';
700                         switch (action) {
701                                 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
702                                 case LFUN_MATH_VALIGN: p->valign(align); break;
703                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
704                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
705                                 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
706                                 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
707                                 default: ;
708                         }
709                         updateLocal(bv, true);
710                 }
711                 break;
712         }
713
714         case LFUN_EXEC_COMMAND:
715                 result = UNDISPATCHED;
716                 break;
717
718         case LFUN_BREAKPARAGRAPH:
719         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
720                 //lyxerr << "LFUN ignored\n";
721                 break;
722
723         case LFUN_INSET_ERT:
724                 // interpret this as if a backslash was typed
725                 bv->lockedInsetStoreUndo(Undo::EDIT);
726                 mathcursor->interpret('\\');
727                 updateLocal(bv, true);
728                 break;
729
730         case -1:
731         case LFUN_INSERT_MATH:
732         case LFUN_SELFINSERT:
733                 if (!arg.empty()) {
734                         bv->lockedInsetStoreUndo(Undo::EDIT);
735                         if (arg.size() == 1)
736                                 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
737                         else
738                                 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
739                         updateLocal(bv, true);
740                 }
741                 break;
742
743         case LFUN_MATH_PANEL:
744                 result = UNDISPATCHED;
745                 break;
746
747         case LFUN_ESCAPE:
748                 if (mathcursor->selection())
749                         mathcursor->selClear();
750                 else
751                         result = UNDISPATCHED;
752                 break;
753
754         case LFUN_INSET_TOGGLE:
755                 mathcursor->insetToggle();
756                 updateLocal(bv, true);
757                 break;
758
759         case LFUN_REF_INSERT:
760                 //if (argument.empty()) {
761                 //      InsetCommandParams p("ref");
762                 //      owner_->getDialogs()->createRef(p.getAsString());
763                 //} else {
764                 //      InsetCommandParams p;
765                 //      p.setFromString(argument);
766
767                 //      InsetRef * inset = new InsetRef(p, *buffer_);
768                 //      if (!insertInset(inset))
769                 //              delete inset;
770                 //      else
771                 //              updateInset(inset, true);
772                 //}
773                 //
774                 if (arg.empty()) {
775                         InsetCommandParams p("ref");
776                         bv->owner()->getDialogs()->createRef(p.getAsString());
777                 } else {
778                         //mathcursor->handleNest(new RefInset);
779                         //mathcursor->insert(arg);
780                         mathcursor->insert(MathAtom(new RefInset(arg)));
781                 }
782                 updateLocal(bv, true);
783                 break;
784
785         default:
786                 result = UNDISPATCHED;
787         }
788
789         mathcursor->normalize();
790         mathcursor->touch();
791
792         lyx::Assert(mathcursor);
793
794         if (mathcursor->selection() || was_selection)
795                 toggleInsetSelection(bv);
796
797         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
798             result == UNDISPATCHED)
799                 showInsetCursor(bv);
800         else
801                 bv->unlockInset(this);
802
803         revealCodes(bv);
804
805         return result;  // original version
806 }
807
808
809 void InsetFormulaBase::revealCodes(BufferView * bv) const
810 {
811         if (!mathcursor)
812                 return;
813         bv->owner()->message(mathcursor->info());
814
815 #if 0
816         // write something to the minibuffer
817         // translate to latex
818         mathcursor->markInsert();
819         ostringstream os;
820         write(NULL, os);
821         string str = os.str();
822         mathcursor->markErase();
823         string::size_type pos = 0;
824         string res;
825         for (string::iterator it = str.begin(); it != str.end(); ++it) {
826                 if (*it == '\n')
827                         res += ' ';
828                 else if (*it == '\0') {
829                         res += "  -X-  ";
830                         pos = it - str.begin();
831                 }
832                 else
833                         res += *it;
834         }
835         if (pos > 30)
836                 res = res.substr(pos - 30);
837         if (res.size() > 60)
838                 res = res.substr(0, 60);
839         bv->owner()->message(res);
840 #endif
841 }
842
843
844 Inset::Code InsetFormulaBase::lyxCode() const
845 {
846         return Inset::MATH_CODE;
847 }
848
849
850 int InsetFormulaBase::ylow() const
851 {
852         return yo_ - ascent(view_, font_);
853 }
854
855
856 int InsetFormulaBase::yhigh() const
857 {
858         return yo_ + descent(view_, font_);
859 }
860
861
862 int InsetFormulaBase::xlow() const
863 {
864         return xo_;
865 }
866
867
868 int InsetFormulaBase::xhigh() const
869 {
870         return xo_ + width(view_, font_);
871 }
872
873
874 /////////////////////////////////////////////////////////////////////
875
876
877 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
878                                      bool, bool)
879 {
880 #ifdef WITH_WARNINGS
881 #warning pretty ugly
882 #endif
883         static InsetFormulaBase * lastformula = 0;
884         static MathIterator current = MathIterator(ibegin(par().nucleus()));
885         static MathArray ar;
886         static string laststr;
887
888         if (lastformula != this || laststr != str) {
889                 //lyxerr << "reset lastformula to " << this << "\n";
890                 lastformula = this;
891                 laststr = str;
892                 current = ibegin(par().nucleus());
893                 ar.clear();
894                 mathed_parse_cell(ar, str);
895         } else {
896                 ++current;
897         }
898         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
899
900         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
901                 if (it.cell().matchpart(ar, it.position().pos_)) {
902                         bv->unlockInset(bv->theLockingInset());
903                         if (!bv->lockInset(this)) {
904                                 lyxerr << "Cannot lock inset" << endl;
905                                 return false;
906                         }
907                         delete mathcursor;
908                         mathcursor = new MathCursor(this, true);
909                         metrics(bv);
910                         mathcursor->setSelection(it.cursor(), ar.size());
911                         current = it;
912                         it.jump(ar.size());
913                         updateLocal(bv, false);
914                         return true;
915                 }
916         }
917
918         //lyxerr << "not found!\n";
919         lastformula = 0;
920         return false;
921 }
922
923
924 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
925                                       bool a, bool b)
926 {
927         lyxerr << "searching backward not implemented in mathed" << endl;
928         return searchForward(bv, what, a, b);
929 }
930
931
932 /////////////////////////////////////////////////////////////////////
933
934
935 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
936 {
937         if (bv->available()) {
938                 // use selection if available..
939                 //string sel;
940                 //if (action == LFUN_MATH_IMPORT_SELECTION)
941                 //      sel = "";
942                 //else
943
944                 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
945
946                 InsetFormulaBase * f;
947                 if (sel.empty()) {
948                         f = new InsetFormula(LM_OT_SIMPLE);
949                         if (openNewInset(bv, f)) {
950                                 // don't do that also for LFUN_MATH_MODE unless you want end up with
951                                 // always changing to mathrm when opening an inlined inset
952                                 // -- I really hate "LyXfunc overloading"...
953                                 if (display)
954                                         f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
955                                 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
956                         }
957                 } else {
958                         // create a macro if we see "\\newcommand" somewhere, and an ordinary
959                         // formula otherwise
960                         if (sel.find("\\newcommand") == string::npos &&
961                                   sel.find("\\def") == string::npos)
962                         {
963                                 f = new InsetFormula(sel);
964                         } else {
965                                 string name;
966                                 if (!mathed_parse_macro(name, sel))
967                                         return;
968                                 f = new InsetFormulaMacro(sel);
969                         }
970                         bv->getLyXText()->cutSelection(bv);
971                         openNewInset(bv, f);
972                 }
973         }
974         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
975 }
976
977
978 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
979 {
980         mathDispatchCreation(bv, arg, true);
981 }
982
983
984 void mathDispatchMathMode(BufferView * bv, string const & arg)
985 {
986         mathDispatchCreation(bv, arg, false);
987 }
988
989
990 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
991 {
992         mathDispatchCreation(bv, arg, true);
993 }
994
995
996 void mathDispatchMathMacro(BufferView * bv, string const & arg)
997 {
998         if (bv->available()) {
999                 if (arg.empty())
1000                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1001                 else {
1002                         string s = arg;
1003                         string const s1 = token(s, ' ', 1);
1004                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
1005                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1006                 }
1007         }
1008 }
1009
1010
1011 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1012 {
1013         if (bv->available()) {
1014                 if (openNewInset(bv, new InsetFormula(LM_OT_SIMPLE)))
1015                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1016         }
1017 }
1018
1019
1020 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1021 {
1022         if (bv->available()) {
1023                 if (openNewInset(bv, new InsetFormula(LM_OT_SIMPLE)))
1024                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1025         }
1026 }
1027
1028
1029 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1030 {
1031         if (bv->available()) {
1032                 if (arg.size() && arg[0] == '\\') {
1033                         InsetFormula * f = new InsetFormula(arg);
1034                         if (!bv->insertInset(f))
1035                                 delete f;
1036                         else if (!mathcursor) // hotfix
1037                                 bv->getLyXText()->cursorRight(bv);
1038                 } else {
1039                         mathDispatchMathMode(bv, arg);
1040                 }
1041         }
1042 }
1043
1044
1045 void mathDispatchGreek(BufferView * bv, string const & arg)
1046 {
1047         if (bv->available()) {
1048                 InsetFormula * f = new InsetFormula(LM_OT_SIMPLE);
1049                 if (openNewInset(bv, f)) {
1050                         bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1051                         bv->unlockInset(f);
1052                 }
1053         }
1054 }
1055
1056
1057 void mathDispatch(BufferView *, kb_action, string const &)
1058 {}
1059
1060
1061 void mathDispatch(BufferView *, string const &)
1062 {}