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