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