]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
a step towards world domination^H^H^H
[lyx.git] / src / mathed / formulabase.C
1 /*
2 *  File:        formulabase.C
3 *  Purpose:     Implementation of common parts of the LyX  math insets
4 *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 *  Created:     January 1996
6 *
7 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
8 *
9 *  Version: 0.4, Lyx project.
10 *
11 *   You are free to use and modify this code under the terms of
12 *   the GNU General Public Licence version 2 or later.
13 */
14
15 #include <config.h>
16 #include <fstream>
17
18 #include "Lsstream.h"
19 #include "support/LAssert.h"
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "formula.h"
26 #include "formulamacro.h"
27 #include "lyxrc.h"
28 #include "commandtags.h"
29 #include "BufferView.h"
30 #include "lyxtext.h"
31 #include "lyxfunc.h"
32 #include "gettext.h"
33 #include "LaTeXFeatures.h"
34 #include "debug.h"
35 #include "math_support.h"
36 #include "support/lstrings.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/font_metrics.h"
39 #include "frontends/mouse_state.h"
40 #include "Lsstream.h"
41 #include "math_arrayinset.h"
42 #include "math_charinset.h"
43 #include "math_deliminset.h"
44 #include "math_cursor.h"
45 #include "math_factory.h"
46 #include "math_fontinset.h"
47 #include "math_hullinset.h"
48 #include "math_iterator.h"
49 #include "math_macrotable.h"
50 #include "math_parser.h"
51 #include "math_pos.h"
52 #include "math_spaceinset.h"
53 #include "undo_funcs.h"
54 #include "textpainter.h"
55 #include "frontends/Dialogs.h"
56 #include "intl.h"
57 #include "insets/insetcommandparams.h"
58 #include "ref_inset.h"
59
60 using std::endl;
61 using std::ostream;
62 using std::vector;
63 using std::abs;
64
65 MathCursor * mathcursor = 0;
66
67
68 namespace {
69
70 // local global
71 int first_x;
72 int first_y;
73
74
75
76 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
77 {
78         if (!bv->insertInset(new_inset)) {
79                 delete new_inset;
80                 return false;
81         }
82         new_inset->edit(bv, true);
83         return true;
84 }
85
86
87 } // namespace anon
88
89
90
91 InsetFormulaBase::InsetFormulaBase()
92         : view_(0), font_(), xo_(0), yo_(0)
93 {
94         // This is needed as long the math parser is not re-entrant
95         initMath();
96         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
97         //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
98         //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
99         //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
100 }
101
102
103 // simply scrap this function if you want
104 void InsetFormulaBase::mutateToText()
105 {
106 #if 0
107         // translate to latex
108         ostringstream os;
109         latex(NULL, os, false, false);
110         string str = os.str();
111
112         // insert this text
113         LyXText * lt = view_->getLyXText();
114         string::const_iterator cit = str.begin();
115         string::const_iterator end = str.end();
116         for (; cit != end; ++cit)
117                 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
118
119         // remove ourselves
120         //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
121 #endif
122 }
123
124
125 void InsetFormulaBase::handleFont
126         (BufferView * bv, string const & arg, string const & font)
127 {
128         bv->lockedInsetStoreUndo(Undo::EDIT);
129         bool sel = mathcursor->selection();
130         if (sel)
131                 updateLocal(bv, true);
132         mathcursor->handleNest(createMathInset(font));
133         mathcursor->insert(arg);
134         if (!sel)
135                 updateLocal(bv, false);
136 }
137
138
139 // Check if uses AMS macros
140 void InsetFormulaBase::validate(LaTeXFeatures &) const
141 {}
142
143
144 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
145 {
146         font_ = f;
147         metrics(bv);
148 }
149
150
151 void InsetFormulaBase::metrics(BufferView * bv) const
152 {
153         if (bv)
154                 view_ = bv;
155         MathMetricsInfo mi;
156         mi.view       = view_;
157         //mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
158         mi.base.style = LM_ST_TEXT;
159         mi.base.font  = font_;
160         mi.base.font.setColor(LColor::math);
161         par()->metrics(mi);
162 }
163
164
165 string const InsetFormulaBase::editMessage() const
166 {
167         return _("Math editor mode");
168 }
169
170
171 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
172 {
173         if (!bv->lockInset(this))
174                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
175         releaseMathCursor(bv);
176         mathcursor = new MathCursor(this, true);
177         metrics(bv);
178         mathcursor->setPos(x, y);
179         //lyxerr << "setting pos to " << x << "," << y << "\n";
180
181         // if that is removed, we won't get the magenta box when entering an
182         // inset for the first time
183         bv->updateInset(this, false);
184 }
185
186
187 void InsetFormulaBase::edit(BufferView * bv, bool front)
188 {
189         if (!bv->lockInset(this))
190                 lyxerr << "Cannot lock math inset in edit call!\n";
191         releaseMathCursor(bv);
192         mathcursor = new MathCursor(this, front);
193         metrics(bv);
194         bv->updateInset(this, false);
195 }
196
197
198 void InsetFormulaBase::insetUnlock(BufferView * bv)
199 {
200         if (mathcursor) {
201                 if (mathcursor->inMacroMode()) {
202                         mathcursor->macroModeClose();
203                         updateLocal(bv, true);
204                 }
205                 releaseMathCursor(bv);
206         }
207         bv->updateInset(this, false);
208 }
209
210
211 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
212 {
213         // calling metrics here destroys the cached xo,yo positions e.g. in
214         // MathParboxinset. And it would be too expensive anyway...
215         //metrics(bv);
216         mathcursor->getPos(x, y);
217         //x -= xo_;
218         y -= yo_;
219         //lyxerr << "getCursorPos: " << x << " " << y << "\n";
220 }
221
222
223 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
224 {
225         //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
226         if (isCursorVisible())
227                 hideInsetCursor(bv);
228         else
229                 showInsetCursor(bv);
230 }
231
232
233 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
234 {
235         if (isCursorVisible())
236                 return;
237         fitInsetCursor(bv);
238         int x, y, asc, des;
239         getCursorPos(bv, x, y);
240         math_font_max_dim(font_, asc, des);
241         bv->showLockedInsetCursor(x, y, asc, des);
242         setCursorVisible(true);
243         //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
244 }
245
246
247 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
248 {
249         if (!isCursorVisible())
250                 return;
251         bv->hideLockedInsetCursor();
252         setCursorVisible(false);
253         //lyxerr << "hideInsetCursor: \n";
254 }
255
256
257 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
258 {
259         if (!mathcursor)
260                 return;
261         int x, y, asc, des;
262         math_font_max_dim(font_, asc, des);
263         getCursorPos(bv, x, y);
264         //y += yo_;
265         //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
266         bv->fitLockedInsetCursor(x, y, asc, des);
267 }
268
269
270 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
271 {
272         if (mathcursor)
273                 bv->updateInset(this, false);
274 }
275
276
277 vector<string> const InsetFormulaBase::getLabelList() const
278 {
279   return vector<string>();
280 }
281
282
283 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
284 {
285         metrics(bv);
286         if (mathcursor)
287                 bv->fitCursor();
288         bv->updateInset(this, dirty);
289 }
290
291
292 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
293         int /*x*/, int /*y*/, mouse_button::state button)
294 {
295         if (!mathcursor)
296                 return false;
297
298         //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
299         hideInsetCursor(bv);
300         showInsetCursor(bv);
301         bv->updateInset(this, false);
302
303         if (button == mouse_button::button3) {
304                 // try to dispatch to enclosed insets first
305                 if (mathcursor->dispatch("mouse-3-release"))
306                         return true;
307
308                 // launch math panel for right mouse button
309                 bv->owner()->getDialogs()->showMathPanel();
310                 return true;
311         }
312
313         if (button == mouse_button::button1) {
314                 // try to dispatch to enclosed insets first
315                 if (mathcursor->dispatch("mouse-1-release"))
316                         return true;
317
318                 // try to set the cursor
319                 //delete mathcursor;
320                 //mathcursor = new MathCursor(this, x == 0);
321                 //metrics(bv);
322                 //mathcursor->setPos(x + xo_, y + yo_);
323                 return true;
324         }
325         return false;
326 }
327
328
329 void InsetFormulaBase::insetButtonPress(BufferView * bv,
330                                         int x, int y, mouse_button::state button)
331 {
332         //lyxerr << "insetButtonPress: "
333         //      << x << " " << y << " but: " << button << "\n";
334         //lyxerr << "formula: ";
335         par()->dump();
336
337         releaseMathCursor(bv);
338         mathcursor = new MathCursor(this, x == 0);
339
340         if (button == mouse_button::button1) {
341                 // just set the cursor here
342                 lyxerr << "setting cursor\n";
343                 metrics(bv);
344                 first_x = x;
345                 first_y = y;
346                 mathcursor->selClear();
347                 mathcursor->setPos(x + xo_, y + yo_);
348
349                 if (mathcursor->dispatch("mouse-1-press")) {
350                         //delete mathcursor;
351                         return;
352                 }
353
354         }
355         if (button == mouse_button::button3) {
356                 if (mathcursor->dispatch("mouse-3-press")) {
357                         //delete mathcursor;
358                         return;
359                 }
360         }
361         bv->updateInset(this, false);
362 }
363
364
365 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
366         int x, int y, mouse_button::state button)
367 {
368         if (!mathcursor)
369                 return;
370
371         if (button == mouse_button::button1)
372                 if (mathcursor->dispatch("mouse-1-motion"))
373                         return;
374
375         if (button == mouse_button::button3)
376                 if (mathcursor->dispatch("mouse-3-motion"))
377                         return;
378
379         if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
380                 //lyxerr << "insetMotionNotify: ignored\n";
381                 return;
382         }
383         first_x = x;
384         first_y = y;
385
386         if (!mathcursor->selection())
387                 mathcursor->selStart();
388
389         //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
390         //      << ' ' << button << "\n";
391         hideInsetCursor(bv);
392         mathcursor->setPos(x + xo_, y + yo_);
393         showInsetCursor(bv);
394         bv->updateInset(this, false);
395 }
396
397
398 UpdatableInset::RESULT
399 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
400                             string const & arg)
401 {
402         //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
403         //      << " arg: '" << arg
404         //      << "' cursor: " << mathcursor << "\n";
405
406         if (!mathcursor)
407                 return UNDISPATCHED;
408         string argument  = arg;
409
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 (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(arg.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 (arg.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, arg, "lyxgreek"); break;
582         case LFUN_BOLD:         handleFont(bv, arg, "textbf"); break;
583         case LFUN_SANS:         handleFont(bv, arg, "textsf"); break;
584         case LFUN_EMPH:         handleFont(bv, arg, "mathcal"); break;
585         case LFUN_ROMAN:        handleFont(bv, arg, "mathrm"); break;
586         case LFUN_CODE:         handleFont(bv, arg, "texttt"); break;
587         case LFUN_FRAK:         handleFont(bv, arg, "mathfrak"); break;
588         case LFUN_ITAL:         handleFont(bv, arg, "mathit"); break;
589         case LFUN_NOUN:         handleFont(bv, arg, "mathbb"); break;
590         case LFUN_DEFAULT:      handleFont(bv, arg, "textnormal"); break;
591         case LFUN_FREE:         handleFont(bv, arg, "textrm"); break;
592
593         case LFUN_GREEK:
594                 handleFont(bv, arg, "lyxgreek1");
595                 if (arg.size())
596                         mathcursor->interpret(arg);
597                 break;
598
599         case LFUN_MATH_MODE:
600                 if (mathcursor->currentMode()) {
601                         handleFont(bv, arg, "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 (!arg.empty()) {
627                         bv->lockedInsetStoreUndo(Undo::EDIT);
628                         mathcursor->interpret("matrix " + arg);
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(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(arg, 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 = arg.size() ? arg[0] : 'c';
684                         switch (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 (arg.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(arg)));
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, LFUN_MATH_DISPLAY, string());
944                         f->localDispatch(bv, 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, 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, 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, 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, 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 {}