]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
Rename strip to rtrim
[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_WORDRIGHTSEL:
420         case LFUN_RIGHTSEL:
421                 sel = true; // fall through...
422         case LFUN_RIGHT:
423                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
424                 //lyxerr << "calling scroll 20\n";
425                 //scroll(bv, 20);
426                 updateLocal(bv, false);
427                 // write something to the minibuffer
428                 //bv->owner()->message(mathcursor->info());
429                 break;
430
431         case LFUN_WORDLEFTSEL:
432         case LFUN_LEFTSEL:
433                 sel = true; // fall through
434         case LFUN_LEFT:
435                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
436                 updateLocal(bv, false);
437                 break;
438
439         case LFUN_UPSEL:
440                 sel = true; // fall through
441         case LFUN_UP:
442                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
443                 updateLocal(bv, false);
444                 break;
445
446         case LFUN_DOWNSEL:
447                 sel = true; // fall through
448         case LFUN_DOWN:
449                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
450                 updateLocal(bv, false);
451                 break;
452
453         case LFUN_HOMESEL:
454                 sel = true; // fall through
455         case LFUN_HOME:
456                 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
457                 updateLocal(bv, false);
458                 break;
459
460         case LFUN_ENDSEL:
461                 sel = true; // fall through
462         case LFUN_END:
463                 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
464                 updateLocal(bv, false);
465                 break;
466
467         case LFUN_PRIORSEL:
468         case LFUN_PRIOR:
469                 result = FINISHED_UP;
470                 updateLocal(bv, false);
471                 break;
472
473         case LFUN_NEXTSEL:
474         case LFUN_NEXT:
475                 result = FINISHED_DOWN;
476                 updateLocal(bv, false);
477                 break;
478
479         case LFUN_DELETE_LINE_FORWARD:
480                 bv->lockedInsetStoreUndo(Undo::DELETE);
481                 mathcursor->delLine();
482                 updateLocal(bv, true);
483                 break;
484
485         case LFUN_TAB:
486                 mathcursor->idxNext();
487                 updateLocal(bv, false);
488                 break;
489
490         case LFUN_SHIFT_TAB:
491                 mathcursor->idxPrev();
492                 updateLocal(bv, false);
493                 break;
494
495         case LFUN_TABINSERT:
496                 bv->lockedInsetStoreUndo(Undo::EDIT);
497                 mathcursor->splitCell();
498                 updateLocal(bv, true);
499                 break;
500
501         case LFUN_DELETE_WORD_BACKWARD:
502         case LFUN_BACKSPACE:
503                 bv->lockedInsetStoreUndo(Undo::DELETE);
504                 mathcursor->backspace();
505                 updateLocal(bv, true);
506                 break;
507
508         case LFUN_DELETE_WORD_FORWARD:
509         case LFUN_DELETE:
510                 bv->lockedInsetStoreUndo(Undo::DELETE);
511                 mathcursor->erase();
512                 bv->updateInset(this, true);
513                 break;
514
515         //    case LFUN_GETXY:
516         //      sprintf(dispatch_buffer, "%d %d",);
517         //      dispatch_result = dispatch_buffer;
518         //      break;
519         case LFUN_SETXY: {
520                 lyxerr << "LFUN_SETXY broken!\n";
521                 int x = 0;
522                 int y = 0;
523                 istringstream is(arg.c_str());
524                 is >> x >> y;
525                 mathcursor->setPos(x, y);
526                 updateLocal(bv, false);
527                 break;
528         }
529
530         case LFUN_PASTE:
531                 if (was_macro)
532                         mathcursor->macroModeClose();
533                 bv->lockedInsetStoreUndo(Undo::EDIT);
534                 mathcursor->selPaste();
535                 updateLocal(bv, true);
536                 break;
537
538         case LFUN_CUT:
539                 bv->lockedInsetStoreUndo(Undo::DELETE);
540                 mathcursor->selCut();
541                 updateLocal(bv, true);
542                 break;
543
544         case LFUN_COPY:
545                 mathcursor->selCopy();
546                 break;
547
548
549         // Special casing for superscript in case of LyX handling
550         // dead-keys:
551         case LFUN_CIRCUMFLEX:
552                 if (arg.empty()) {
553                         // do superscript if LyX handles
554                         // deadkeys
555                         bv->lockedInsetStoreUndo(Undo::EDIT);
556                         mathcursor->script(true);
557                         updateLocal(bv, true);
558                 }
559                 break;
560
561         case LFUN_UMLAUT:
562         case LFUN_ACUTE:
563         case LFUN_GRAVE:
564         case LFUN_BREVE:
565         case LFUN_DOT:
566         case LFUN_MACRON:
567         case LFUN_CARON:
568         case LFUN_TILDE:
569         case LFUN_CEDILLA:
570         case LFUN_CIRCLE:
571         case LFUN_UNDERDOT:
572         case LFUN_TIE:
573         case LFUN_OGONEK:
574         case LFUN_HUNG_UMLAUT:
575                 break;
576
577         //  Math fonts
578         case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
579         case LFUN_BOLD:         handleFont(bv, arg, "textbf"); break;
580         case LFUN_SANS:         handleFont(bv, arg, "textsf"); break;
581         case LFUN_EMPH:         handleFont(bv, arg, "mathcal"); break;
582         case LFUN_ROMAN:        handleFont(bv, arg, "mathrm"); break;
583         case LFUN_CODE:         handleFont(bv, arg, "texttt"); break;
584         case LFUN_FRAK:         handleFont(bv, arg, "mathfrak"); break;
585         case LFUN_ITAL:         handleFont(bv, arg, "mathit"); break;
586         case LFUN_NOUN:         handleFont(bv, arg, "mathbb"); break;
587         case LFUN_DEFAULT:      handleFont(bv, arg, "textnormal"); break;
588         case LFUN_FREE:         handleFont(bv, arg, "textrm"); break;
589
590         case LFUN_GREEK:
591                 handleFont(bv, arg, "lyxgreek1");
592                 if (arg.size())
593                         mathcursor->interpret(arg);
594                 break;
595
596         case LFUN_MATH_MODE:
597                 if (mathcursor->currentMode()) {
598                         handleFont(bv, arg, "textrm");
599                 } else {
600                         mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
601                         updateLocal(bv, true);
602                 }
603                 //bv->owner()->message(_("math text mode toggled"));
604                 break;
605
606         case LFUN_MATH_LIMITS:
607                 bv->lockedInsetStoreUndo(Undo::EDIT);
608                 if (mathcursor->toggleLimits())
609                         updateLocal(bv, true);
610                 break;
611
612         case LFUN_MATH_SIZE:
613 #if 0
614                 if (!arg.empty()) {
615                         bv->lockedInsetStoreUndo(Undo::EDIT);
616                         mathcursor->setSize(arg);
617                         updateLocal(bv, true);
618                 }
619 #endif
620                 break;
621
622         case LFUN_INSERT_MATRIX:
623                 if (!arg.empty()) {
624                         bv->lockedInsetStoreUndo(Undo::EDIT);
625                         mathcursor->interpret("matrix " + arg);
626                         updateLocal(bv, true);
627                 }
628                 break;
629
630         case LFUN_SUPERSCRIPT:
631         case LFUN_SUBSCRIPT:
632         {
633                 bv->lockedInsetStoreUndo(Undo::EDIT);
634                 mathcursor->script(action == LFUN_SUPERSCRIPT);
635                 updateLocal(bv, true);
636                 break;
637         }
638
639         case LFUN_MATH_DELIM:
640         {
641                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
642                 string ls;
643                 string rs = split(arg, ls, ' ');
644                 // Reasonable default values
645                 if (ls.empty())
646                         ls = '(';
647                 if (rs.empty())
648                         rs = ')';
649
650                 bv->lockedInsetStoreUndo(Undo::EDIT);
651                 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
652                 updateLocal(bv, true);
653                 break;
654         }
655
656         case LFUN_PROTECTEDSPACE:
657         case LFUN_MATH_SPACE:
658                 bv->lockedInsetStoreUndo(Undo::EDIT);
659                 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
660                 updateLocal(bv, true);
661                 break;
662
663         case LFUN_UNDO:
664                 bv->owner()->message(_("Invalid action in math mode!"));
665                 break;
666
667
668         case LFUN_MATH_HALIGN:
669         case LFUN_MATH_VALIGN:
670         case LFUN_MATH_ROW_INSERT:
671         case LFUN_MATH_ROW_DELETE:
672         case LFUN_MATH_COLUMN_INSERT:
673         case LFUN_MATH_COLUMN_DELETE:
674         {
675                 MathInset::idx_type idx = 0;
676                 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
677                 if (p) {
678                         mathcursor->popToEnclosingGrid();
679                         bv->lockedInsetStoreUndo(Undo::EDIT);
680                         char align = arg.size() ? arg[0] : 'c';
681                         switch (action) {
682                                 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
683                                 case LFUN_MATH_VALIGN: p->valign(align); break;
684                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
685                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
686                                 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
687                                 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
688                                 default: ;
689                         }
690                         updateLocal(bv, true);
691                 }
692                 break;
693         }
694
695         case LFUN_EXEC_COMMAND:
696                 result = UNDISPATCHED;
697                 break;
698
699         case LFUN_BREAKPARAGRAPH:
700         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
701                 //lyxerr << "LFUN ignored\n";
702                 break;
703
704         case LFUN_INSET_ERT:
705                 // interpret this as if a backslash was typed
706                 bv->lockedInsetStoreUndo(Undo::EDIT);
707                 mathcursor->interpret('\\');
708                 updateLocal(bv, true);
709                 break;
710
711         case -1:
712         case LFUN_INSERT_MATH:
713         case LFUN_SELFINSERT:
714                 if (!arg.empty()) {
715                         bv->lockedInsetStoreUndo(Undo::EDIT);
716                         if (arg.size() == 1)
717                                 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
718                         else
719                                 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
720                         updateLocal(bv, true);
721                 }
722                 break;
723
724         case LFUN_MATH_PANEL:
725                 result = UNDISPATCHED;
726                 break;
727
728         case LFUN_ESCAPE:
729                 if (mathcursor->selection())
730                         mathcursor->selClear();
731                 else
732                         result = UNDISPATCHED;
733                 break;
734
735         case LFUN_INSET_TOGGLE:
736                 mathcursor->insetToggle();
737                 updateLocal(bv, true);
738                 break;
739
740         case LFUN_REF_INSERT:
741                 //if (argument.empty()) {
742                 //      InsetCommandParams p("ref");
743                 //      owner_->getDialogs()->createRef(p.getAsString());
744                 //} else {
745                 //      InsetCommandParams p;
746                 //      p.setFromString(argument);
747
748                 //      InsetRef * inset = new InsetRef(p, *buffer_);
749                 //      if (!insertInset(inset))
750                 //              delete inset;
751                 //      else
752                 //              updateInset(inset, true);
753                 //}
754                 //
755                 if (arg.empty()) {
756                         InsetCommandParams p("ref");
757                         bv->owner()->getDialogs()->createRef(p.getAsString());
758                 } else {
759                         //mathcursor->handleNest(new InsetRef2);
760                         //mathcursor->insert(arg);
761                         mathcursor->insert(MathAtom(new RefInset(arg)));
762                 }
763                 updateLocal(bv, true);
764                 break;
765
766         default:
767                 result = UNDISPATCHED;
768         }
769
770         mathcursor->normalize();
771         mathcursor->touch();
772
773         lyx::Assert(mathcursor);
774
775         if (mathcursor->selection() || was_selection)
776                 toggleInsetSelection(bv);
777
778         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
779             result == UNDISPATCHED) {
780                 fitInsetCursor(bv);
781                 showInsetCursor(bv);
782                 revealCodes(bv);
783         } else {
784                 releaseMathCursor(bv);
785                 bv->unlockInset(this);
786         }
787
788         return result;  // original version
789 }
790
791
792 void InsetFormulaBase::revealCodes(BufferView * bv) const
793 {
794         if (!mathcursor)
795                 return;
796         bv->owner()->message(mathcursor->info());
797
798 #if 0
799         // write something to the minibuffer
800         // translate to latex
801         mathcursor->markInsert();
802         ostringstream os;
803         write(NULL, os);
804         string str = os.str();
805         mathcursor->markErase();
806         string::size_type pos = 0;
807         string res;
808         for (string::iterator it = str.begin(); it != str.end(); ++it) {
809                 if (*it == '\n')
810                         res += ' ';
811                 else if (*it == '\0') {
812                         res += "  -X-  ";
813                         pos = it - str.begin();
814                 }
815                 else
816                         res += *it;
817         }
818         if (pos > 30)
819                 res = res.substr(pos - 30);
820         if (res.size() > 60)
821                 res = res.substr(0, 60);
822         bv->owner()->message(res);
823 #endif
824 }
825
826
827 Inset::Code InsetFormulaBase::lyxCode() const
828 {
829         return Inset::MATH_CODE;
830 }
831
832
833 int InsetFormulaBase::ylow() const
834 {
835         return yo_ - ascent(view_, font_);
836 }
837
838
839 int InsetFormulaBase::yhigh() const
840 {
841         return yo_ + descent(view_, font_);
842 }
843
844
845 int InsetFormulaBase::xlow() const
846 {
847         return xo_;
848 }
849
850
851 int InsetFormulaBase::xhigh() const
852 {
853         return xo_ + width(view_, font_);
854 }
855
856
857 /////////////////////////////////////////////////////////////////////
858
859
860 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
861                                      bool, bool)
862 {
863 #ifdef WITH_WARNINGS
864 #warning pretty ugly
865 #endif
866         static InsetFormulaBase * lastformula = 0;
867         static MathIterator current = MathIterator(ibegin(par().nucleus()));
868         static MathArray ar;
869         static string laststr;
870
871         if (lastformula != this || laststr != str) {
872                 //lyxerr << "reset lastformula to " << this << "\n";
873                 lastformula = this;
874                 laststr = str;
875                 current = ibegin(par().nucleus());
876                 ar.clear();
877                 mathed_parse_cell(ar, str);
878         } else {
879                 ++current;
880         }
881         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
882
883         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
884                 if (it.cell().matchpart(ar, it.back().pos_)) {
885                         bv->unlockInset(bv->theLockingInset());
886                         if (!bv->lockInset(this)) {
887                                 lyxerr << "Cannot lock inset" << endl;
888                                 return false;
889                         }
890                         delete mathcursor;
891                         mathcursor = new MathCursor(this, true);
892                         metrics(bv);
893                         mathcursor->setSelection(it, ar.size());
894                         current = it;
895                         it.jump(ar.size());
896                         updateLocal(bv, false);
897                         return true;
898                 }
899         }
900
901         //lyxerr << "not found!\n";
902         lastformula = 0;
903         return false;
904 }
905
906
907 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
908                                       bool a, bool b)
909 {
910         lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
911         return searchForward(bv, what, a, b);
912 }
913
914
915 /////////////////////////////////////////////////////////////////////
916
917
918 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
919 {
920         if (!bv->available())
921                 return;
922
923         // use selection if available..
924         //string sel;
925         //if (action == LFUN_MATH_IMPORT_SELECTION)
926         //      sel = "";
927         //else
928
929         string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
930
931         if (sel.empty()) {
932                 InsetFormula * f = new InsetFormula(bv);
933                 if (openNewInset(bv, f)) {
934                         f->mutate("simple");
935                         // don't do that also for LFUN_MATH_MODE unless you want end up with
936                         // always changing to mathrm when opening an inlined inset
937                         // -- I really hate "LyXfunc overloading"...
938                         if (display)
939                                 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
940                         f->localDispatch(bv, LFUN_INSERT_MATH, arg);
941                 }
942         } else {
943                 // create a macro if we see "\\newcommand" somewhere, and an ordinary
944                 // formula otherwise
945                 InsetFormulaBase * f;
946                 if (sel.find("\\newcommand") == string::npos &&
947                                 sel.find("\\def") == string::npos)
948                         f = new InsetFormula(sel);
949                 else
950                         f = new InsetFormulaMacro(sel);
951                 bv->getLyXText()->cutSelection(bv);
952                 openNewInset(bv, f);
953         }
954         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
955 }
956
957
958 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
959 {
960         mathDispatchCreation(bv, arg, true);
961 }
962
963
964 void mathDispatchMathMode(BufferView * bv, string const & arg)
965 {
966         mathDispatchCreation(bv, arg, false);
967 }
968
969
970 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
971 {
972         mathDispatchCreation(bv, arg, true);
973 }
974
975
976 void mathDispatchMathMacro(BufferView * bv, string const & arg)
977 {
978         if (!bv->available())
979                 return;
980         if (arg.empty())
981                 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
982         else {
983                 string s = arg;
984                 string const s1 = token(s, ' ', 1);
985                 int const na = s1.empty() ? 0 : lyx::atoi(s1);
986                 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
987         }
988 }
989
990
991 void mathDispatchMathDelim(BufferView * bv, string const & arg)
992 {
993         if (!bv->available())
994                 return;
995         InsetFormula * f = new InsetFormula(bv);
996         if (openNewInset(bv, f)) {
997                 f->mutate("simple");
998                 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
999         }
1000 }
1001
1002
1003 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1004 {
1005         if (!bv->available())
1006                 return;
1007         InsetFormula * f = new InsetFormula(bv);
1008         if (openNewInset(bv, f)) {
1009                 f->mutate("simple");
1010                 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1011         }
1012 }
1013
1014
1015 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1016 {
1017         if (!bv->available())
1018                 return;
1019         InsetFormula * f = new InsetFormula(bv);
1020         if (openNewInset(bv, f)) {
1021                 f->mutate("simple");
1022     bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATH, arg);
1023         }
1024 }
1025
1026
1027 void mathDispatchGreek(BufferView * bv, string const & arg)
1028 {
1029         if (!bv->available())
1030                 return;
1031         InsetFormula * f = new InsetFormula(bv);
1032         if (openNewInset(bv, f)) {
1033                 f->mutate("simple");
1034                 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1035                 bv->unlockInset(f);
1036         }
1037 }
1038
1039
1040 void mathDispatch(BufferView *, kb_action, string const &)
1041 {}
1042
1043
1044 void mathDispatch(BufferView *, string const &)
1045 {}