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