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