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