]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
macro handling cleanup
[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 "lyxrc.h"
28 #include "commandtags.h"
29 #include "BufferView.h"
30 #include "lyxtext.h"
31 #include "lyxfunc.h"
32 #include "gettext.h"
33 #include "LaTeXFeatures.h"
34 #include "debug.h"
35 #include "math_support.h"
36 #include "support/lstrings.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/font_metrics.h"
39 #include "frontends/mouse_state.h"
40 #include "Lsstream.h"
41 #include "math_arrayinset.h"
42 #include "math_charinset.h"
43 #include "math_cursor.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/insetcommandparams.h"
56 #include "ref_inset.h"
57
58 using std::endl;
59 using std::ostream;
60 using std::vector;
61 using std::abs;
62
63 MathCursor * mathcursor = 0;
64
65
66 namespace {
67
68 // local global
69 int first_x;
70 int first_y;
71
72
73
74 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
75 {
76         if (!bv->insertInset(new_inset)) {
77                 delete new_inset;
78                 return false;
79         }
80         new_inset->edit(bv, true);
81         return true;
82 }
83
84
85 } // namespace anon
86
87
88
89 InsetFormulaBase::InsetFormulaBase()
90         : view_(0), font_(), xo_(0), yo_(0)
91 {
92         // This is needed as long the math parser is not re-entrant
93         initMath();
94         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
95         //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
96         //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
97         //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
98 }
99
100
101 // simply scrap this function if you want
102 void InsetFormulaBase::mutateToText()
103 {
104 #if 0
105         // translate to latex
106         ostringstream os;
107         latex(NULL, os, false, false);
108         string str = os.str();
109
110         // insert this text
111         LyXText * lt = view_->getLyXText();
112         string::const_iterator cit = str.begin();
113         string::const_iterator end = str.end();
114         for (; cit != end; ++cit)
115                 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
116
117         // remove ourselves
118         //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
119 #endif
120 }
121
122
123 void InsetFormulaBase::handleFont
124         (BufferView * bv, string const & arg, string const & font)
125 {
126         bv->lockedInsetStoreUndo(Undo::EDIT);
127         bool sel = mathcursor->selection();
128         if (sel)
129                 updateLocal(bv, true);
130         mathcursor->handleNest(new MathFontInset(font));
131         mathcursor->insert(arg);
132         if (!sel)
133                 updateLocal(bv, false);
134 }
135
136
137 // Check if uses AMS macros
138 void InsetFormulaBase::validate(LaTeXFeatures &) const
139 {}
140
141
142 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
143 {
144         font_ = f;
145         metrics(bv);
146 }
147
148
149 void InsetFormulaBase::metrics(BufferView * bv) const
150 {
151         if (bv)
152                 view_ = bv;
153         MathMetricsInfo mi;
154         mi.view       = view_;
155         //mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
156         mi.base.style = 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 = 0;
230                 int y = 0;
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         if (!mathcursor)
311                 return false;
312
313         //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
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-release"))
321                         return true;
322
323                 // launch math panel for right mouse button
324                 bv->owner()->getDialogs()->showMathPanel();
325                 return true;
326         }
327
328         if (button == mouse_button::button1) {
329                 // try to dispatch to enclosed insets first
330                 if (mathcursor->dispatch("mouse-1-release"))
331                         return true;
332
333                 // try to set the cursor
334                 //delete mathcursor;
335                 //mathcursor = new MathCursor(this, x == 0);
336                 //metrics(bv);
337                 //mathcursor->setPos(x + xo_, y + yo_);
338                 return true;
339         }
340         return false;
341 }
342
343
344 void InsetFormulaBase::insetButtonPress(BufferView * bv,
345                                         int x, int y, mouse_button::state button)
346 {
347         lyxerr << "insetButtonPress: "
348                 << x << " " << y << " but: " << button << "\n";
349         lyxerr << "formula: ";
350         par()->dump();
351
352         delete mathcursor;
353         mathcursor = new MathCursor(this, x == 0);
354
355         if (button == mouse_button::button1) {
356                 // just set the cursor here
357                 lyxerr << "setting cursor\n";
358                 metrics(bv);
359                 first_x = x;
360                 first_y = y;
361                 mathcursor->selClear();
362                 mathcursor->setPos(x + xo_, y + yo_);
363
364                 if (mathcursor->dispatch("mouse-1-press")) {
365                         //delete mathcursor;
366                         return;
367                 }
368         
369         }
370         if (button == mouse_button::button3) { 
371                 if (mathcursor->dispatch("mouse-3-press")) {
372                         //delete mathcursor;
373                         return;
374                 }
375         }
376         bv->updateInset(this, false);
377 }
378
379
380 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
381         int x, int y, mouse_button::state button)
382 {
383         if (!mathcursor)
384                 return;
385
386         if (button == mouse_button::button1) 
387                 if (mathcursor->dispatch("mouse-1-motion"))
388                         return;
389
390         if (button == mouse_button::button3) 
391                 if (mathcursor->dispatch("mouse-3-motion"))
392                         return;
393
394         if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
395                 //lyxerr << "insetMotionNotify: ignored\n";
396                 return;
397         }
398         first_x = x;
399         first_y = y;
400
401         if (!mathcursor->selection())
402                 mathcursor->selStart();
403
404         //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
405         //      << ' ' << button << "\n";
406         hideInsetCursor(bv);
407         mathcursor->setPos(x + xo_, y + yo_);
408         showInsetCursor(bv);
409         bv->updateInset(this, false);
410 }
411
412
413 UpdatableInset::RESULT
414 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
415                             string const & arg)
416 {
417         //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
418         //      << " arg: '" << arg
419         //      << "' cursor: " << mathcursor
420         //      << "\n";
421
422         if (!mathcursor)
423                 return UNDISPATCHED;
424
425         RESULT result      = DISPATCHED;
426         bool sel           = false;
427         bool was_macro     = mathcursor->inMacroMode();
428         bool was_selection = mathcursor->selection();
429
430         hideInsetCursor(bv);
431
432         mathcursor->normalize();
433         mathcursor->touch();
434
435         switch (action) {
436
437         // --- Cursor Movements ---------------------------------------------
438
439         case LFUN_RIGHTSEL:
440                 sel = true; // fall through...
441
442         case LFUN_RIGHT:
443                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
444                 //lyxerr << "calling scroll 20\n";
445                 //scroll(bv, 20);
446                 updateLocal(bv, false);
447                 // write something to the minibuffer
448                 //bv->owner()->message(mathcursor->info());
449                 break;
450
451
452         case LFUN_LEFTSEL:
453                 sel = true; // fall through
454
455         case LFUN_LEFT:
456                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
457                 updateLocal(bv, false);
458                 break;
459
460
461         case LFUN_UPSEL:
462                 sel = true;
463
464         case LFUN_UP:
465                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
466                 updateLocal(bv, false);
467                 break;
468
469
470         case LFUN_DOWNSEL:
471                 sel = true;
472
473         case LFUN_DOWN:
474                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
475                 updateLocal(bv, false);
476                 break;
477
478         case LFUN_HOMESEL:
479                 sel = true;
480
481         case LFUN_HOME:
482                 mathcursor->home(sel);
483                 updateLocal(bv, false);
484                 break;
485
486         case LFUN_ENDSEL:
487                 sel = true;
488
489         case LFUN_END:
490                 mathcursor->end(sel);
491                 updateLocal(bv, false);
492                 break;
493
494         case LFUN_DELETE_LINE_FORWARD:
495                 bv->lockedInsetStoreUndo(Undo::DELETE);
496                 mathcursor->delLine();
497                 updateLocal(bv, true);
498                 break;
499
500         case LFUN_TAB:
501                 mathcursor->idxNext();
502                 updateLocal(bv, false);
503                 break;
504
505         case LFUN_SHIFT_TAB:
506                 mathcursor->idxPrev();
507                 updateLocal(bv, false);
508                 break;
509
510         case LFUN_TABINSERT:
511                 bv->lockedInsetStoreUndo(Undo::EDIT);
512                 mathcursor->splitCell();
513                 updateLocal(bv, true);
514                 break;
515
516         case LFUN_DELETE_WORD_BACKWARD:
517         case LFUN_BACKSPACE:
518                 bv->lockedInsetStoreUndo(Undo::DELETE);
519                 mathcursor->backspace();
520                 updateLocal(bv, true);
521                 break;
522
523         case LFUN_DELETE_WORD_FORWARD:
524         case LFUN_DELETE:
525                 bv->lockedInsetStoreUndo(Undo::DELETE);
526                 mathcursor->erase();
527                 bv->updateInset(this, true);
528                 break;
529
530         //    case LFUN_GETXY:
531         //      sprintf(dispatch_buffer, "%d %d",);
532         //      dispatch_result = dispatch_buffer;
533         //      break;
534         case LFUN_SETXY: {
535                 lyxerr << "LFUN_SETXY broken!\n";
536                 int x = 0;
537                 int y = 0;
538                 istringstream is(arg.c_str());
539                 is >> x >> y;
540                 mathcursor->setPos(x, y);
541                 updateLocal(bv, false);
542                 break;
543         }
544
545         case LFUN_PASTE:
546                 if (was_macro)
547                         mathcursor->macroModeClose();
548                 bv->lockedInsetStoreUndo(Undo::EDIT);
549                 mathcursor->selPaste();
550                 updateLocal(bv, true);
551                 break;
552
553         case LFUN_CUT:
554                 bv->lockedInsetStoreUndo(Undo::DELETE);
555                 mathcursor->selCut();
556                 updateLocal(bv, true);
557                 break;
558
559         case LFUN_COPY:
560                 mathcursor->selCopy();
561                 break;
562
563         case LFUN_WORDRIGHTSEL:
564         case LFUN_WORDLEFTSEL:
565                 break;
566
567         // Special casing for superscript in case of LyX handling
568         // dead-keys:
569         case LFUN_CIRCUMFLEX:
570                 if (arg.empty()) {
571                         // do superscript if LyX handles
572                         // deadkeys
573                         bv->lockedInsetStoreUndo(Undo::EDIT);
574                         mathcursor->script(true);
575                         updateLocal(bv, true);
576                 }
577                 break;
578         case LFUN_UMLAUT:
579         case LFUN_ACUTE:
580         case LFUN_GRAVE:
581         case LFUN_BREVE:
582         case LFUN_DOT:
583         case LFUN_MACRON:
584         case LFUN_CARON:
585         case LFUN_TILDE:
586         case LFUN_CEDILLA:
587         case LFUN_CIRCLE:
588         case LFUN_UNDERDOT:
589         case LFUN_TIE:
590         case LFUN_OGONEK:
591         case LFUN_HUNG_UMLAUT:
592                 break;
593
594         //  Math fonts
595         case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
596         case LFUN_BOLD:         handleFont(bv, arg, "textbf"); break;
597         case LFUN_SANS:         handleFont(bv, arg, "textsf"); break;
598         case LFUN_EMPH:         handleFont(bv, arg, "mathcal"); break;
599         case LFUN_ROMAN:        handleFont(bv, arg, "mathrm"); break;
600         case LFUN_CODE:         handleFont(bv, arg, "texttt"); break;
601         case LFUN_FRAK:         handleFont(bv, arg, "mathfrak"); break;
602         case LFUN_ITAL:         handleFont(bv, arg, "mathit"); break;
603         case LFUN_NOUN:         handleFont(bv, arg, "mathbb"); break;
604         case LFUN_DEFAULT:      handleFont(bv, arg, "textnormal"); break;
605         case LFUN_FREE:         handleFont(bv, arg, "textrm"); break;
606
607         case LFUN_GREEK:
608                 handleFont(bv, arg, "lyxgreek1");
609                 if (arg.size())
610                         mathcursor->interpret(arg);
611                 break;
612
613         case LFUN_MATH_MODE:
614                 if (mathcursor->inMathMode()) {
615                         handleFont(bv, arg, "textrm");
616                 } else {
617                         mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
618                         updateLocal(bv, true);
619                 }
620                 //bv->owner()->message(_("math text mode toggled"));
621                 break;
622
623         case LFUN_MATH_LIMITS:
624                 bv->lockedInsetStoreUndo(Undo::EDIT);
625                 if (mathcursor->toggleLimits())
626                         updateLocal(bv, true);
627                 break;
628
629         case LFUN_MATH_SIZE:
630 #if 0
631                 if (!arg.empty()) {
632                         bv->lockedInsetStoreUndo(Undo::EDIT);
633                         mathcursor->setSize(arg);
634                         updateLocal(bv, true);
635                 }
636 #endif
637                 break;
638
639         case LFUN_INSERT_MATRIX:
640                 if (!arg.empty()) {
641                         bv->lockedInsetStoreUndo(Undo::EDIT);
642                         mathcursor->interpret("matrix " + arg);
643                         updateLocal(bv, true);
644                 }
645                 break;
646
647         case LFUN_SUPERSCRIPT:
648         case LFUN_SUBSCRIPT:
649         {
650                 bv->lockedInsetStoreUndo(Undo::EDIT);
651                 mathcursor->script(action == LFUN_SUPERSCRIPT);
652                 updateLocal(bv, true);
653                 break;
654         }
655
656         case LFUN_MATH_DELIM:
657         {
658                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
659                 string ls;
660                 string rs = split(arg, ls, ' ');
661                 // Reasonable default values
662                 if (ls.empty())
663                         ls = '(';
664                 if (rs.empty())
665                         rs = ')';
666
667                 bv->lockedInsetStoreUndo(Undo::EDIT);
668                 mathcursor->handleDelim(ls, rs);
669                 updateLocal(bv, true);
670                 break;
671         }
672
673         case LFUN_PROTECTEDSPACE:
674         case LFUN_MATH_SPACE:
675                 bv->lockedInsetStoreUndo(Undo::EDIT);
676                 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
677                 updateLocal(bv, true);
678                 break;
679
680         case LFUN_UNDO:
681                 bv->owner()->message(_("Invalid action in math mode!"));
682                 break;
683
684
685         case LFUN_MATH_HALIGN:
686         case LFUN_MATH_VALIGN:
687         case LFUN_MATH_ROW_INSERT:
688         case LFUN_MATH_ROW_DELETE:
689         case LFUN_MATH_COLUMN_INSERT:
690         case LFUN_MATH_COLUMN_DELETE:
691         {
692                 MathInset::idx_type idx = 0;
693                 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
694                 if (p) {
695                         mathcursor->popToEnclosingGrid();
696                         bv->lockedInsetStoreUndo(Undo::EDIT);
697                         char align = arg.size() ? arg[0] : 'c';
698                         switch (action) {
699                                 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
700                                 case LFUN_MATH_VALIGN: p->valign(align); break;
701                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
702                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
703                                 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
704                                 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
705                                 default: ;
706                         }
707                         updateLocal(bv, true);
708                 }
709                 break;
710         }
711
712         case LFUN_EXEC_COMMAND:
713                 result = UNDISPATCHED;
714                 break;
715
716         case LFUN_BREAKPARAGRAPH:
717         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
718                 //lyxerr << "LFUN ignored\n";
719                 break;
720
721         case LFUN_INSET_ERT:
722                 // interpret this as if a backslash was typed
723                 bv->lockedInsetStoreUndo(Undo::EDIT);
724                 mathcursor->interpret('\\');
725                 updateLocal(bv, true);
726                 break;
727
728         case -1:
729         case LFUN_INSERT_MATH:
730         case LFUN_SELFINSERT:
731                 if (!arg.empty()) {
732                         bv->lockedInsetStoreUndo(Undo::EDIT);
733                         if (arg.size() == 1)
734                                 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
735                         else
736                                 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
737                         updateLocal(bv, true);
738                 }
739                 break;
740
741         case LFUN_MATH_PANEL:
742                 result = UNDISPATCHED;
743                 break;
744
745         case LFUN_ESCAPE:
746                 if (mathcursor->selection())
747                         mathcursor->selClear();
748                 else
749                         result = UNDISPATCHED;
750                 break;
751
752         case LFUN_INSET_TOGGLE:
753                 mathcursor->insetToggle();
754                 updateLocal(bv, true);
755                 break;
756
757         case LFUN_REF_INSERT:
758                 //if (argument.empty()) {
759                 //      InsetCommandParams p("ref");
760                 //      owner_->getDialogs()->createRef(p.getAsString());
761                 //} else {
762                 //      InsetCommandParams p;
763                 //      p.setFromString(argument);
764
765                 //      InsetRef * inset = new InsetRef(p, *buffer_);
766                 //      if (!insertInset(inset))
767                 //              delete inset;
768                 //      else
769                 //              updateInset(inset, true);
770                 //}
771                 //
772                 if (arg.empty()) {
773                         InsetCommandParams p("ref");
774                         bv->owner()->getDialogs()->createRef(p.getAsString());
775                 } else {
776                         //mathcursor->handleNest(new InsetRef2);
777                         //mathcursor->insert(arg);
778                         mathcursor->insert(MathAtom(new RefInset(arg)));
779                 }
780                 updateLocal(bv, true);
781                 break;
782
783         default:
784                 result = UNDISPATCHED;
785         }
786
787         mathcursor->normalize();
788         mathcursor->touch();
789
790         lyx::Assert(mathcursor);
791
792         if (mathcursor->selection() || was_selection)
793                 toggleInsetSelection(bv);
794
795         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
796             result == UNDISPATCHED)
797                 showInsetCursor(bv);
798         else
799                 bv->unlockInset(this);
800
801         revealCodes(bv);
802
803         return result;  // original version
804 }
805
806
807 void InsetFormulaBase::revealCodes(BufferView * bv) const
808 {
809         if (!mathcursor)
810                 return;
811         bv->owner()->message(mathcursor->info());
812
813 #if 0
814         // write something to the minibuffer
815         // translate to latex
816         mathcursor->markInsert();
817         ostringstream os;
818         write(NULL, os);
819         string str = os.str();
820         mathcursor->markErase();
821         string::size_type pos = 0;
822         string res;
823         for (string::iterator it = str.begin(); it != str.end(); ++it) {
824                 if (*it == '\n')
825                         res += ' ';
826                 else if (*it == '\0') {
827                         res += "  -X-  ";
828                         pos = it - str.begin();
829                 }
830                 else
831                         res += *it;
832         }
833         if (pos > 30)
834                 res = res.substr(pos - 30);
835         if (res.size() > 60)
836                 res = res.substr(0, 60);
837         bv->owner()->message(res);
838 #endif
839 }
840
841
842 Inset::Code InsetFormulaBase::lyxCode() const
843 {
844         return Inset::MATH_CODE;
845 }
846
847
848 int InsetFormulaBase::ylow() const
849 {
850         return yo_ - ascent(view_, font_);
851 }
852
853
854 int InsetFormulaBase::yhigh() const
855 {
856         return yo_ + descent(view_, font_);
857 }
858
859
860 int InsetFormulaBase::xlow() const
861 {
862         return xo_;
863 }
864
865
866 int InsetFormulaBase::xhigh() const
867 {
868         return xo_ + width(view_, font_);
869 }
870
871
872 /////////////////////////////////////////////////////////////////////
873
874
875 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
876                                      bool, bool)
877 {
878 #ifdef WITH_WARNINGS
879 #warning pretty ugly
880 #endif
881         static InsetFormulaBase * lastformula = 0;
882         static MathIterator current = MathIterator(ibegin(par().nucleus()));
883         static MathArray ar;
884         static string laststr;
885
886         if (lastformula != this || laststr != str) {
887                 //lyxerr << "reset lastformula to " << this << "\n";
888                 lastformula = this;
889                 laststr = str;
890                 current = ibegin(par().nucleus());
891                 ar.clear();
892                 mathed_parse_cell(ar, str);
893         } else {
894                 ++current;
895         }
896         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
897
898         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
899                 if (it.cell().matchpart(ar, it.position().pos_)) {
900                         bv->unlockInset(bv->theLockingInset());
901                         if (!bv->lockInset(this)) {
902                                 lyxerr << "Cannot lock inset" << endl;
903                                 return false;
904                         }
905                         delete mathcursor;
906                         mathcursor = new MathCursor(this, true);
907                         metrics(bv);
908                         mathcursor->setSelection(it.cursor(), ar.size());
909                         current = it;
910                         it.jump(ar.size());
911                         updateLocal(bv, false);
912                         return true;
913                 }
914         }
915
916         //lyxerr << "not found!\n";
917         lastformula = 0;
918         return false;
919 }
920
921
922 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
923                                       bool a, bool b)
924 {
925         lyxerr << "searching backward not implemented in mathed" << endl;
926         return searchForward(bv, what, a, b);
927 }
928
929
930 /////////////////////////////////////////////////////////////////////
931
932
933 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
934 {
935         if (!bv->available())
936                 return;
937
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         if (sel.empty()) {
947                 InsetFormula * f = new InsetFormula(bv);
948                 if (openNewInset(bv, f)) {
949                         f->mutate("simple");
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                 InsetFormulaBase * f;
961                 if (sel.find("\\newcommand") == string::npos &&
962                                 sel.find("\\def") == string::npos)
963                         f = new InsetFormula(sel);
964                 else
965                         f = new InsetFormulaMacro(sel);
966                 bv->getLyXText()->cutSelection(bv);
967                 openNewInset(bv, f);
968         }
969         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
970 }
971
972
973 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
974 {
975         mathDispatchCreation(bv, arg, true);
976 }
977
978
979 void mathDispatchMathMode(BufferView * bv, string const & arg)
980 {
981         mathDispatchCreation(bv, arg, false);
982 }
983
984
985 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
986 {
987         mathDispatchCreation(bv, arg, true);
988 }
989
990
991 void mathDispatchMathMacro(BufferView * bv, string const & arg)
992 {
993         if (!bv->available())
994                 return;
995         if (arg.empty())
996                 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
997         else {
998                 string s = arg;
999                 string const s1 = token(s, ' ', 1);
1000                 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1001                 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1002         }
1003 }
1004
1005
1006 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1007 {
1008         if (!bv->available())
1009                 return;
1010         InsetFormula * f = new InsetFormula(bv);
1011         if (openNewInset(bv, f)) {
1012                 f->mutate("simple");
1013                 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1014         }
1015 }
1016
1017
1018 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1019 {
1020         if (!bv->available())
1021                 return;
1022         InsetFormula * f = new InsetFormula(bv);
1023         if (openNewInset(bv, f)) {
1024                 f->mutate("simple");
1025                 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1026         }
1027 }
1028
1029
1030 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1031 {
1032         if (!bv->available())
1033                 return;
1034         InsetFormula * f = new InsetFormula(bv);
1035         if (openNewInset(bv, f)) {
1036                 f->mutate("simple");
1037     bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATH, arg);
1038         }
1039 }
1040
1041
1042 void mathDispatchGreek(BufferView * bv, string const & arg)
1043 {
1044         if (!bv->available())
1045                 return;
1046         InsetFormula * f = new InsetFormula(bv);
1047         if (openNewInset(bv, f)) {
1048                 f->mutate("simple");
1049                 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1050                 bv->unlockInset(f);
1051         }
1052 }
1053
1054
1055 void mathDispatch(BufferView *, kb_action, string const &)
1056 {}
1057
1058
1059 void mathDispatch(BufferView *, string const &)
1060 {}