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