]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
Martin's space patch.
[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 "gettext.h"
32 #include "LaTeXFeatures.h"
33 #include "debug.h"
34 #include "math_support.h"
35 #include "math_metricsinfo.h"
36 #include "support/lstrings.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/font_metrics.h"
39 #include "frontends/mouse_state.h"
40 #include "Lsstream.h"
41 #include "math_arrayinset.h"
42 #include "math_charinset.h"
43 #include "math_deliminset.h"
44 #include "math_cursor.h"
45 #include "math_factory.h"
46 #include "math_fontinset.h"
47 #include "math_hullinset.h"
48 #include "math_iterator.h"
49 #include "math_macrotable.h"
50 #include "math_parser.h"
51 #include "math_pos.h"
52 #include "math_spaceinset.h"
53 #include "undo_funcs.h"
54 #include "textpainter.h"
55 #include "frontends/Dialogs.h"
56 #include "intl.h"
57 #include "insets/insetcommandparams.h"
58 #include "ref_inset.h"
59
60 using std::endl;
61 using std::ostream;
62 using std::vector;
63 using std::abs;
64
65 MathCursor * mathcursor = 0;
66
67
68 namespace {
69
70 // local global
71 int first_x;
72 int first_y;
73
74
75
76 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
77 {
78         if (!bv->insertInset(new_inset)) {
79                 delete new_inset;
80                 return false;
81         }
82         new_inset->edit(bv, true);
83         return true;
84 }
85
86
87 } // namespace anon
88
89
90
91 InsetFormulaBase::InsetFormulaBase()
92         : font_(), xo_(0), yo_(0)
93 {
94         // This is needed as long the math parser is not re-entrant
95         initMath();
96         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
97         //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
98         //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
99         //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
100 }
101
102
103 // simply scrap this function if you want
104 void InsetFormulaBase::mutateToText()
105 {
106 #if 0
107         // translate to latex
108         ostringstream os;
109         latex(NULL, os, false, false);
110         string str = os.str();
111
112         // insert this text
113         LyXText * lt = view_->getLyXText();
114         string::const_iterator cit = str.begin();
115         string::const_iterator end = str.end();
116         for (; cit != end; ++cit)
117                 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
118
119         // remove ourselves
120         //view_->owner()->dispatch(LFUN_ESCAPE);
121 #endif
122 }
123
124
125 void InsetFormulaBase::handleFont
126         (BufferView * bv, string const & arg, string const & font)
127 {
128         bv->lockedInsetStoreUndo(Undo::EDIT);
129         bool sel = mathcursor->selection();
130         if (sel)
131                 updateLocal(bv, true);
132         mathcursor->handleNest(createMathInset(font));
133         mathcursor->insert(arg);
134         if (!sel)
135                 updateLocal(bv, false);
136 }
137
138
139 // Check if uses AMS macros
140 void InsetFormulaBase::validate(LaTeXFeatures &) const
141 {}
142
143
144 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
145 {
146         font_ = f;
147         metrics(bv);
148 }
149
150
151 void InsetFormulaBase::metrics(BufferView * bv) const
152 {
153         if (bv)
154                 view_ = bv->owner()->view();
155         MathMetricsInfo mi;
156         mi.base.style = LM_ST_TEXT;
157         mi.base.font  = font_;
158         mi.base.font.setColor(LColor::math);
159         par()->metrics(mi);
160 }
161
162
163 string const InsetFormulaBase::editMessage() const
164 {
165         return _("Math editor mode");
166 }
167
168
169 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
170 {
171         if (!bv->lockInset(this))
172                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
173         releaseMathCursor(bv);
174         mathcursor = new MathCursor(this, true);
175         metrics(bv);
176         mathcursor->setPos(x + xo_, y + yo_);
177         // if that is removed, we won't get the magenta box when entering an
178         // inset for the first time
179         bv->updateInset(this, false);
180 }
181
182
183 void InsetFormulaBase::edit(BufferView * bv, bool front)
184 {
185         if (!bv->lockInset(this))
186                 lyxerr << "Cannot lock math inset in edit call!\n";
187         releaseMathCursor(bv);
188         mathcursor = new MathCursor(this, front);
189         metrics(bv);
190         bv->updateInset(this, false);
191 }
192
193
194 void InsetFormulaBase::insetUnlock(BufferView * bv)
195 {
196         if (mathcursor) {
197                 if (mathcursor->inMacroMode()) {
198                         mathcursor->macroModeClose();
199                         updateLocal(bv, true);
200                 }
201                 releaseMathCursor(bv);
202         }
203         generatePreview();
204         bv->updateInset(this, false);
205 }
206
207
208 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
209 {
210         // calling metrics here destroys the cached xo,yo positions e.g. in
211         // MathParboxinset. And it would be too expensive anyway...
212         //metrics(bv);
213         if (!mathcursor) {
214                 lyxerr << "getCursorPos - should not happen";
215                 x = y = 0;
216                 return;
217         }
218         mathcursor->getPos(x, y);
219         x = mathcursor->targetX();
220         x -= xo_;
221         y -= yo_;
222         //lyxerr << "getCursorPos: " << x << " " << y << "\n";
223 }
224
225
226 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
227 {
228         //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
229         if (isCursorVisible())
230                 hideInsetCursor(bv);
231         else
232                 showInsetCursor(bv);
233 }
234
235
236 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
237 {
238         if (isCursorVisible())
239                 return;
240         fitInsetCursor(bv);
241         int x, y, asc, des;
242         mathcursor->getPos(x, y);
243         math_font_max_dim(font_, asc, des);
244         bv->showLockedInsetCursor(x, y - yo_, asc, des);
245         setCursorVisible(true);
246         //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
247 }
248
249
250 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
251 {
252         if (!isCursorVisible())
253                 return;
254         bv->hideLockedInsetCursor();
255         setCursorVisible(false);
256         //lyxerr << "hideInsetCursor: \n";
257 }
258
259
260 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
261 {
262         if (!mathcursor)
263                 return;
264         int x, y, asc, des;
265         math_font_max_dim(font_, asc, des);
266         getCursorPos(bv, x, y);
267         //y += yo_;
268         //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
269         bv->fitLockedInsetCursor(x, y, asc, des);
270 }
271
272
273 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
274 {
275         if (mathcursor)
276                 bv->updateInset(this, false);
277 }
278
279
280 vector<string> const InsetFormulaBase::getLabelList() const
281 {
282   return vector<string>();
283 }
284
285
286 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
287 {
288         bv->updateInset(this, dirty);
289 }
290
291
292 Inset::RESULT InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
293 {
294         if (!mathcursor)
295                 return UNDISPATCHED;
296
297         BufferView * bv = cmd.view();
298         hideInsetCursor(bv);
299         showInsetCursor(bv);
300         bv->updateInset(this, false);
301
302         if (cmd.button() == mouse_button::button3) {
303                 // try to dispatch to enclosed insets first
304                 if (mathcursor->dispatch(cmd) == MathInset::UNDISPATCHED) {     
305                         // launch math panel for right mouse button
306                         bv->owner()->getDialogs().showMathPanel();
307                 }
308                 return DISPATCHED;
309         }
310
311         if (cmd.button() == mouse_button::button1) {
312                 // try to dispatch to enclosed insets first
313                 mathcursor->dispatch(cmd);
314                 // try to set the cursor
315                 //delete mathcursor;
316                 //mathcursor = new MathCursor(this, x == 0);
317                 //metrics(bv);
318                 //mathcursor->setPos(x + xo_, y + yo_);
319                 return DISPATCHED;
320         }
321         return UNDISPATCHED;
322 }
323
324
325 Inset::RESULT InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
326 {
327         BufferView * bv = cmd.view();
328         releaseMathCursor(bv);
329         mathcursor = new MathCursor(this, cmd.x == 0);
330
331         if (cmd.button() == mouse_button::button1) {
332                 // just set the cursor here
333                 //lyxerr << "setting cursor\n";
334                 metrics(bv);
335                 first_x = cmd.x;
336                 first_y = cmd.y;
337                 mathcursor->selClear();
338                 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
339                 mathcursor->dispatch(cmd);
340                 return DISPATCHED;
341         }
342         if (cmd.button() == mouse_button::button3) {
343                 mathcursor->dispatch(cmd);
344                 //delete mathcursor;
345                 return DISPATCHED;
346         }
347         bv->updateInset(this, false);
348         return DISPATCHED;
349 }
350
351
352 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
353 {
354         if (!mathcursor)
355                 return DISPATCHED;
356
357         if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
358                 return DISPATCHED;
359
360         if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2) {
361                 //lyxerr << "insetMotionNotify: ignored\n";
362                 return DISPATCHED;
363         }
364         first_x = cmd.x;
365         first_y = cmd.y;
366
367         if (!mathcursor->selection())
368                 mathcursor->selStart();
369
370         BufferView * bv = cmd.view();
371         hideInsetCursor(bv);
372         mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
373         showInsetCursor(bv);
374         bv->updateInset(this, false);
375         return DISPATCHED;
376 }
377
378
379 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
380 {
381         //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
382         //      << " arg: '" << cmd.argument
383         //      << " x: '" << cmd.x
384         //      << " y: '" << cmd.y
385         //      << "' button: " << cmd.button() << "\n";
386
387         switch (cmd.action) {
388                 case LFUN_MOUSE_PRESS:
389                         return lfunMousePress(cmd);
390                 case LFUN_MOUSE_MOTION:
391                         return lfunMouseMotion(cmd);
392                 case LFUN_MOUSE_RELEASE:
393                         return lfunMouseRelease(cmd);
394                 case LFUN_MOUSE_DOUBLE:
395                         //lyxerr << "Mouse double\n";
396                         return localDispatch(FuncRequest(LFUN_WORDSEL));
397                 default:
398                         break;
399         }
400
401         if (!mathcursor)
402                 return UNDISPATCHED;
403
404         BufferView * bv    = cmd.view();
405         string argument    = cmd.argument;
406         RESULT result      = DISPATCHED;
407         bool sel           = false;
408         bool was_macro     = mathcursor->inMacroMode();
409         bool was_selection = mathcursor->selection();
410
411         hideInsetCursor(bv);
412
413         mathcursor->normalize();
414         mathcursor->touch();
415
416         switch (cmd.action) {
417
418         case LFUN_MATH_MUTATE:
419         case LFUN_MATH_DISPLAY:
420         case LFUN_MATH_NUMBER:
421         case LFUN_MATH_NONUMBER:
422         case LFUN_TABINSERT:
423         case LFUN_BREAKLINE:
424         case LFUN_DELETE_LINE_FORWARD:
425         case LFUN_INSERT_LABEL:
426         case LFUN_MATH_EXTERN:
427         case LFUN_TABULAR_FEATURE:
428         case LFUN_PASTESELECTION:
429         case LFUN_MATH_LIMITS:
430                 bv->lockedInsetStoreUndo(Undo::EDIT);
431                 mathcursor->dispatch(cmd);
432                 updateLocal(bv, true);
433                 break;
434
435         case LFUN_WORDRIGHTSEL:
436         case LFUN_RIGHTSEL:
437                 sel = true; // fall through...
438         case LFUN_RIGHT:
439                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
440                 //lyxerr << "calling scroll 20\n";
441                 //scroll(bv, 20);
442                 updateLocal(bv, false);
443                 // write something to the minibuffer
444                 //bv->owner()->message(mathcursor->info());
445                 break;
446
447         case LFUN_WORDLEFTSEL:
448         case LFUN_LEFTSEL:
449                 sel = true; // fall through
450         case LFUN_LEFT:
451                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
452                 updateLocal(bv, false);
453                 break;
454
455         case LFUN_UPSEL:
456                 sel = true; // fall through
457         case LFUN_UP:
458                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
459                 updateLocal(bv, false);
460                 break;
461
462         case LFUN_DOWNSEL:
463                 sel = true; // fall through
464         case LFUN_DOWN:
465                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
466                 updateLocal(bv, false);
467                 break;
468
469         case LFUN_WORDSEL:
470                 mathcursor->home(false);
471                 mathcursor->end(true);
472                 updateLocal(bv, false);
473                 break;
474
475         case LFUN_HOMESEL:
476                 sel = true; // fall through
477         case LFUN_HOME:
478                 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
479                 updateLocal(bv, false);
480                 break;
481
482         case LFUN_ENDSEL:
483                 sel = true; // fall through
484         case LFUN_END:
485                 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
486                 updateLocal(bv, false);
487                 break;
488
489         case LFUN_PRIORSEL:
490         case LFUN_PRIOR:
491                 result = FINISHED_UP;
492                 updateLocal(bv, false);
493                 break;
494
495         case LFUN_NEXTSEL:
496         case LFUN_NEXT:
497                 result = FINISHED_DOWN;
498                 updateLocal(bv, false);
499                 break;
500
501         case LFUN_TAB:
502                 mathcursor->idxNext();
503                 updateLocal(bv, false);
504                 break;
505
506         case LFUN_SHIFT_TAB:
507                 mathcursor->idxPrev();
508                 updateLocal(bv, false);
509                 break;
510
511         case LFUN_DELETE_WORD_BACKWARD:
512         case LFUN_BACKSPACE:
513                 bv->lockedInsetStoreUndo(Undo::DELETE);
514                 mathcursor->backspace();
515                 updateLocal(bv, true);
516                 break;
517
518         case LFUN_DELETE_WORD_FORWARD:
519         case LFUN_DELETE:
520                 bv->lockedInsetStoreUndo(Undo::DELETE);
521                 mathcursor->erase();
522                 bv->updateInset(this, true);
523                 break;
524
525         //    case LFUN_GETXY:
526         //      sprintf(dispatch_buffer, "%d %d",);
527         //      dispatch_result = dispatch_buffer;
528         //      break;
529         case LFUN_SETXY: {
530                 lyxerr << "LFUN_SETXY broken!\n";
531                 int x = 0;
532                 int y = 0;
533                 istringstream is(cmd.argument.c_str());
534                 is >> x >> y;
535                 mathcursor->setPos(x, y);
536                 updateLocal(bv, false);
537                 break;
538         }
539
540         case LFUN_PASTE:
541                 if (was_macro)
542                         mathcursor->macroModeClose();
543                 bv->lockedInsetStoreUndo(Undo::EDIT);
544                 mathcursor->selPaste();
545                 updateLocal(bv, true);
546                 break;
547
548         case LFUN_CUT:
549                 bv->lockedInsetStoreUndo(Undo::DELETE);
550                 mathcursor->selCut();
551                 updateLocal(bv, true);
552                 break;
553
554         case LFUN_COPY:
555                 mathcursor->selCopy();
556                 break;
557
558
559         // Special casing for superscript in case of LyX handling
560         // dead-keys:
561         case LFUN_CIRCUMFLEX:
562                 if (cmd.argument.empty()) {
563                         // do superscript if LyX handles
564                         // deadkeys
565                         bv->lockedInsetStoreUndo(Undo::EDIT);
566                         mathcursor->script(true);
567                         updateLocal(bv, true);
568                 }
569                 break;
570
571         case LFUN_UMLAUT:
572         case LFUN_ACUTE:
573         case LFUN_GRAVE:
574         case LFUN_BREVE:
575         case LFUN_DOT:
576         case LFUN_MACRON:
577         case LFUN_CARON:
578         case LFUN_TILDE:
579         case LFUN_CEDILLA:
580         case LFUN_CIRCLE:
581         case LFUN_UNDERDOT:
582         case LFUN_TIE:
583         case LFUN_OGONEK:
584         case LFUN_HUNG_UMLAUT:
585                 break;
586
587         //  Math fonts
588         case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
589         case LFUN_BOLD:         handleFont(bv, cmd.argument, "textbf"); break;
590         case LFUN_SANS:         handleFont(bv, cmd.argument, "textsf"); break;
591         case LFUN_EMPH:         handleFont(bv, cmd.argument, "mathcal"); break;
592         case LFUN_ROMAN:        handleFont(bv, cmd.argument, "mathrm"); break;
593         case LFUN_CODE:         handleFont(bv, cmd.argument, "texttt"); break;
594         case LFUN_FRAK:         handleFont(bv, cmd.argument, "mathfrak"); break;
595         case LFUN_ITAL:         handleFont(bv, cmd.argument, "mathit"); break;
596         case LFUN_NOUN:         handleFont(bv, cmd.argument, "mathbb"); break;
597         case LFUN_FREE:         handleFont(bv, cmd.argument, "textrm"); break;
598         case LFUN_DEFAULT:      handleFont(bv, cmd.argument, "textnormal"); break;
599
600         case LFUN_GREEK:
601                 handleFont(bv, cmd.argument, "lyxgreek1");
602                 if (cmd.argument.size())
603                         mathcursor->interpret(cmd.argument);
604                 break;
605
606         case LFUN_MATH_MODE:
607                 if (mathcursor->currentMode())
608                         handleFont(bv, cmd.argument, "textrm");
609                 else {
610                         mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
611                         updateLocal(bv, true);
612                 }
613                 //bv->owner()->message(_("math text mode toggled"));
614                 break;
615
616         case LFUN_MATH_SIZE:
617 #if 0
618                 if (!arg.empty()) {
619                         bv->lockedInsetStoreUndo(Undo::EDIT);
620                         mathcursor->setSize(arg);
621                         updateLocal(bv, true);
622                 }
623 #endif
624                 break;
625
626         case LFUN_INSERT_MATRIX:
627                 if (!cmd.argument.empty()) {
628                         bv->lockedInsetStoreUndo(Undo::EDIT);
629                         mathcursor->interpret("matrix " + cmd.argument);
630                         updateLocal(bv, true);
631                 }
632                 break;
633
634         case LFUN_SUPERSCRIPT:
635         case LFUN_SUBSCRIPT:
636         {
637                 bv->lockedInsetStoreUndo(Undo::EDIT);
638                 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
639                 updateLocal(bv, true);
640                 break;
641         }
642
643         case LFUN_MATH_DELIM:
644         {
645                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
646                 string ls;
647                 string rs = split(cmd.argument, ls, ' ');
648                 // Reasonable default values
649                 if (ls.empty())
650                         ls = '(';
651                 if (rs.empty())
652                         rs = ')';
653
654                 bv->lockedInsetStoreUndo(Undo::EDIT);
655                 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
656                 updateLocal(bv, true);
657                 break;
658         }
659
660         case LFUN_PROTECTEDSPACE:
661         case LFUN_MATH_SPACE:
662                 bv->lockedInsetStoreUndo(Undo::EDIT);
663                 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
664                 updateLocal(bv, true);
665                 break;
666
667         case LFUN_UNDO:
668                 bv->owner()->message(_("Invalid action in math mode!"));
669                 break;
670
671
672         case LFUN_EXEC_COMMAND:
673                 result = UNDISPATCHED;
674                 break;
675
676         case LFUN_INSET_ERT:
677                 // interpret this as if a backslash was typed
678                 bv->lockedInsetStoreUndo(Undo::EDIT);
679                 mathcursor->interpret('\\');
680                 updateLocal(bv, true);
681                 break;
682
683         case LFUN_BREAKPARAGRAPH:
684         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
685         case LFUN_BREAKPARAGRAPH_SKIP:
686                 argument = "\n";
687                 // fall through
688
689         case -1:
690         case LFUN_INSERT_MATH:
691         case LFUN_SELFINSERT:
692                 if (!argument.empty()) {
693                         bv->lockedInsetStoreUndo(Undo::EDIT);
694                         if (argument.size() == 1)
695                                 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
696                         else
697                                 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
698                         updateLocal(bv, true);
699                 }
700                 break;
701
702         case LFUN_MATH_PANEL:
703                 result = UNDISPATCHED;
704                 break;
705
706         case LFUN_ESCAPE:
707                 if (mathcursor->selection())
708                         mathcursor->selClear();
709                 else
710                         result = UNDISPATCHED;
711                 break;
712
713         case LFUN_INSET_TOGGLE:
714                 mathcursor->insetToggle();
715                 updateLocal(bv, true);
716                 break;
717
718         case LFUN_REF_INSERT:
719                 //if (argument.empty()) {
720                 //      InsetCommandParams p("ref");
721                 //      owner_->getDialogs().createRef(p.getAsString());
722                 //} else {
723                 //      InsetCommandParams p;
724                 //      p.setFromString(argument);
725
726                 //      InsetRef * inset = new InsetRef(p, *buffer_);
727                 //      if (!insertInset(inset))
728                 //              delete inset;
729                 //      else
730                 //              updateInset(inset, true);
731                 //}
732                 //
733                 if (cmd.argument.empty()) {
734                         InsetCommandParams p("ref");
735                         bv->owner()->getDialogs().createRef(p.getAsString());
736                 } else {
737                         //mathcursor->handleNest(new InsetRef2);
738                         //mathcursor->insert(arg);
739                         mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
740                 }
741                 updateLocal(bv, true);
742                 break;
743
744         default:
745                 result = UNDISPATCHED;
746         }
747
748         mathcursor->normalize();
749         mathcursor->touch();
750
751         lyx::Assert(mathcursor);
752
753         if (mathcursor->selection() || was_selection)
754                 toggleInsetSelection(bv);
755
756         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
757             result == UNDISPATCHED) {
758                 fitInsetCursor(bv);
759                 showInsetCursor(bv);
760                 revealCodes(bv);
761         } else {
762                 releaseMathCursor(bv);
763                 bv->unlockInset(this);
764         }
765
766         return result;  // original version
767 }
768
769
770 void InsetFormulaBase::revealCodes(BufferView * bv) const
771 {
772         if (!mathcursor)
773                 return;
774         bv->owner()->message(mathcursor->info());
775
776 #if 0
777         // write something to the minibuffer
778         // translate to latex
779         mathcursor->markInsert();
780         ostringstream os;
781         write(NULL, os);
782         string str = os.str();
783         mathcursor->markErase();
784         string::size_type pos = 0;
785         string res;
786         for (string::iterator it = str.begin(); it != str.end(); ++it) {
787                 if (*it == '\n')
788                         res += ' ';
789                 else if (*it == '\0') {
790                         res += "  -X-  ";
791                         pos = it - str.begin();
792                 }
793                 else
794                         res += *it;
795         }
796         if (pos > 30)
797                 res = res.substr(pos - 30);
798         if (res.size() > 60)
799                 res = res.substr(0, 60);
800         bv->owner()->message(res);
801 #endif
802 }
803
804
805 Inset::Code InsetFormulaBase::lyxCode() const
806 {
807         return Inset::MATH_CODE;
808 }
809
810
811 int InsetFormulaBase::ylow() const
812 {
813         return yo_ - ascent(view(), font_);
814 }
815
816
817 int InsetFormulaBase::yhigh() const
818 {
819         return yo_ + descent(view(), font_);
820 }
821
822
823 int InsetFormulaBase::xlow() const
824 {
825         return xo_;
826 }
827
828
829 int InsetFormulaBase::xhigh() const
830 {
831         return xo_ + width(view(), font_);
832 }
833
834
835 /////////////////////////////////////////////////////////////////////
836
837
838 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
839                                      bool, bool)
840 {
841 #ifdef WITH_WARNINGS
842 #warning pretty ugly
843 #endif
844         static InsetFormulaBase * lastformula = 0;
845         static MathIterator current = MathIterator(ibegin(par().nucleus()));
846         static MathArray ar;
847         static string laststr;
848
849         if (lastformula != this || laststr != str) {
850                 //lyxerr << "reset lastformula to " << this << "\n";
851                 lastformula = this;
852                 laststr = str;
853                 current = ibegin(par().nucleus());
854                 ar.clear();
855                 mathed_parse_cell(ar, str);
856         } else {
857                 ++current;
858         }
859         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
860
861         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
862                 if (it.cell().matchpart(ar, it.back().pos_)) {
863                         bv->unlockInset(bv->theLockingInset());
864                         if (!bv->lockInset(this)) {
865                                 lyxerr << "Cannot lock inset" << endl;
866                                 return false;
867                         }
868                         delete mathcursor;
869                         mathcursor = new MathCursor(this, true);
870                         metrics(bv);
871                         mathcursor->setSelection(it, ar.size());
872                         current = it;
873                         it.jump(ar.size());
874                         updateLocal(bv, false);
875                         return true;
876                 }
877         }
878
879         //lyxerr << "not found!\n";
880         lastformula = 0;
881         return false;
882 }
883
884
885 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
886                                       bool a, bool b)
887 {
888         lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
889         return searchForward(bv, what, a, b);
890 }
891
892
893 bool InsetFormulaBase::display() const
894 {
895         return par()->asHullInset() && par()->asHullInset()->display();
896 }
897
898
899 /////////////////////////////////////////////////////////////////////
900
901
902 void mathDispatchCreation(FuncRequest const & cmd, bool display)
903 {
904         BufferView * bv = cmd.view();
905         // use selection if available..
906         //string sel;
907         //if (action == LFUN_MATH_IMPORT_SELECTION)
908         //      sel = "";
909         //else
910
911         string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
912
913         if (sel.empty()) {
914                 InsetFormula * f = new InsetFormula(bv);
915                 if (openNewInset(bv, f)) {
916                         bv->theLockingInset()->
917                                 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
918                         // don't do that also for LFUN_MATH_MODE unless you want end up with
919                         // always changing to mathrm when opening an inlined inset
920                         // -- I really hate "LyXfunc overloading"...
921                         if (display)
922                                 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
923                         f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
924                 }
925         } else {
926                 // create a macro if we see "\\newcommand" somewhere, and an ordinary
927                 // formula otherwise
928                 InsetFormulaBase * f;
929                 if (sel.find("\\newcommand") == string::npos &&
930                                 sel.find("\\def") == string::npos)
931                         f = new InsetFormula(sel);
932                 else
933                         f = new InsetFormulaMacro(sel);
934                 bv->getLyXText()->cutSelection(bv);
935                 openNewInset(bv, f);
936         }
937         cmd.message(N_("Math editor mode"));
938 }
939
940
941 void mathDispatch(FuncRequest const & cmd)
942 {
943         BufferView * bv = cmd.view();
944         if (!bv->available())
945                 return;
946
947         switch (cmd.action) {
948
949                 case LFUN_MATH_DISPLAY:
950                         mathDispatchCreation(cmd, true);
951                         break;
952
953                 case LFUN_MATH_MODE:
954                         mathDispatchCreation(cmd, false);
955                         break;
956
957                 case LFUN_MATH_IMPORT_SELECTION:
958                         mathDispatchCreation(cmd, false);
959                         break;
960
961                 case LFUN_MATH_MACRO:
962                         if (cmd.argument.empty())
963                                 cmd.errorMessage(N_("Missing argument"));
964                         else {
965                                 string s = cmd.argument;
966                                 string const s1 = token(s, ' ', 1);
967                                 int const na = s1.empty() ? 0 : lyx::atoi(s1);
968                                 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
969                         }
970                         break;
971
972                 case LFUN_GREEK: 
973                 case LFUN_INSERT_MATH: 
974                 case LFUN_INSERT_MATRIX: 
975                 case LFUN_MATH_DELIM: {
976                         InsetFormula * f = new InsetFormula(bv);
977                         if (openNewInset(bv, f)) {
978                                 bv->theLockingInset()-> 
979                                         localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
980                                 bv->theLockingInset()->localDispatch(cmd);
981                         }
982                         break;
983                 }
984                 default:
985                         break;
986         }
987 }