]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
remove unused code
[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         if (!mathcursor) {
217                 lyxerr << "getCursorPos - should not happen";
218                 x = y = 0;
219                 return;
220         }
221         mathcursor->getPos(x, y);
222         //x -= xo_;
223         y -= yo_;
224         //lyxerr << "getCursorPos: " << x << " " << y << "\n";
225 }
226
227
228 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
229 {
230         //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
231         if (isCursorVisible())
232                 hideInsetCursor(bv);
233         else
234                 showInsetCursor(bv);
235 }
236
237
238 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
239 {
240         if (isCursorVisible())
241                 return;
242         fitInsetCursor(bv);
243         int x, y, asc, des;
244         getCursorPos(bv, x, y);
245         math_font_max_dim(font_, asc, des);
246         bv->showLockedInsetCursor(x, y, asc, des);
247         setCursorVisible(true);
248         //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
249 }
250
251
252 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
253 {
254         if (!isCursorVisible())
255                 return;
256         bv->hideLockedInsetCursor();
257         setCursorVisible(false);
258         //lyxerr << "hideInsetCursor: \n";
259 }
260
261
262 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
263 {
264         if (!mathcursor)
265                 return;
266         int x, y, asc, des;
267         math_font_max_dim(font_, asc, des);
268         getCursorPos(bv, x, y);
269         //y += yo_;
270         //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
271         bv->fitLockedInsetCursor(x, y, asc, des);
272 }
273
274
275 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
276 {
277         if (mathcursor)
278                 bv->updateInset(this, false);
279 }
280
281
282 vector<string> const InsetFormulaBase::getLabelList() const
283 {
284   return vector<string>();
285 }
286
287
288 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
289 {
290         bv->updateInset(this, dirty);
291 }
292
293
294 Inset::RESULT InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
295 {
296         if (!mathcursor)
297                 return UNDISPATCHED;
298
299         BufferView * bv = cmd.view();
300         hideInsetCursor(bv);
301         showInsetCursor(bv);
302         bv->updateInset(this, false);
303
304         if (cmd.button() == mouse_button::button3) {
305                 // try to dispatch to enclosed insets first
306                 if (mathcursor->dispatch(cmd) == MathInset::UNDISPATCHED) {     
307                         // launch math panel for right mouse button
308                         bv->owner()->getDialogs().showMathPanel();
309                 }
310                 return DISPATCHED;
311         }
312
313         if (cmd.button() == mouse_button::button1) {
314                 // try to dispatch to enclosed insets first
315                 mathcursor->dispatch(cmd);
316                 // try to set the cursor
317                 //delete mathcursor;
318                 //mathcursor = new MathCursor(this, x == 0);
319                 //metrics(bv);
320                 //mathcursor->setPos(x + xo_, y + yo_);
321                 return DISPATCHED;
322         }
323         return UNDISPATCHED;
324 }
325
326
327 Inset::RESULT InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
328 {
329         BufferView * bv = cmd.view();
330         releaseMathCursor(bv);
331         mathcursor = new MathCursor(this, cmd.x == 0);
332
333         if (cmd.button() == mouse_button::button1) {
334                 // just set the cursor here
335                 //lyxerr << "setting cursor\n";
336                 metrics(bv);
337                 first_x = cmd.x;
338                 first_y = cmd.y;
339                 mathcursor->selClear();
340                 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
341                 mathcursor->dispatch(cmd);
342                 return DISPATCHED;
343         }
344         if (cmd.button() == mouse_button::button3) {
345                 mathcursor->dispatch(cmd);
346                 //delete mathcursor;
347                 return DISPATCHED;
348         }
349         bv->updateInset(this, false);
350         return DISPATCHED;
351 }
352
353
354 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
355 {
356         if (!mathcursor)
357                 return DISPATCHED;
358
359         if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
360                 return DISPATCHED;
361
362         if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2) {
363                 //lyxerr << "insetMotionNotify: ignored\n";
364                 return DISPATCHED;
365         }
366         first_x = cmd.x;
367         first_y = cmd.y;
368
369         if (!mathcursor->selection())
370                 mathcursor->selStart();
371
372         BufferView * bv = cmd.view();
373         hideInsetCursor(bv);
374         mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
375         showInsetCursor(bv);
376         bv->updateInset(this, false);
377         return DISPATCHED;
378 }
379
380
381 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
382 {
383         //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
384         //      << " arg: '" << cmd.argument
385         //      << " x: '" << cmd.x
386         //      << " y: '" << cmd.y
387         //      << "' button: " << cmd.button() << "\n";
388
389         switch (cmd.action) {
390                 case LFUN_MOUSE_PRESS:
391                         return lfunMousePress(cmd);
392                 case LFUN_MOUSE_MOTION:
393                         return lfunMouseMotion(cmd);
394                 case LFUN_MOUSE_RELEASE:
395                         return lfunMouseRelease(cmd);
396                 default:
397                         break;
398         }
399
400         if (!mathcursor)
401                 return UNDISPATCHED;
402
403         BufferView * bv    = cmd.view();
404         string argument    = cmd.argument;
405         RESULT result      = DISPATCHED;
406         bool sel           = false;
407         bool was_macro     = mathcursor->inMacroMode();
408         bool was_selection = mathcursor->selection();
409
410         hideInsetCursor(bv);
411
412         mathcursor->normalize();
413         mathcursor->touch();
414
415         switch (cmd.action) {
416
417         case LFUN_MATH_MUTATE:
418         case LFUN_MATH_DISPLAY:
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(cmd);
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(cmd.argument.c_str());
530                 is >> x >> y;
531                 mathcursor->setPos(x, y);
532                 updateLocal(bv, false);
533                 break;
534         }
535
536 /*
537         case LFUN_PASTESELECTION: {
538                 string const clip = bv->getClipboard();
539                 if (!clip.empty())
540                         mathed_parse_normal(par_, clip);
541                 break;
542         }
543 */
544
545         case LFUN_PASTE:
546                 if (was_macro)
547                         mathcursor->macroModeClose();
548                 bv->lockedInsetStoreUndo(Undo::EDIT);
549                 mathcursor->selPaste();
550                 updateLocal(bv, true);
551                 break;
552
553         case LFUN_CUT:
554                 bv->lockedInsetStoreUndo(Undo::DELETE);
555                 mathcursor->selCut();
556                 updateLocal(bv, true);
557                 break;
558
559         case LFUN_COPY:
560                 mathcursor->selCopy();
561                 break;
562
563
564         // Special casing for superscript in case of LyX handling
565         // dead-keys:
566         case LFUN_CIRCUMFLEX:
567                 if (cmd.argument.empty()) {
568                         // do superscript if LyX handles
569                         // deadkeys
570                         bv->lockedInsetStoreUndo(Undo::EDIT);
571                         mathcursor->script(true);
572                         updateLocal(bv, true);
573                 }
574                 break;
575
576         case LFUN_UMLAUT:
577         case LFUN_ACUTE:
578         case LFUN_GRAVE:
579         case LFUN_BREVE:
580         case LFUN_DOT:
581         case LFUN_MACRON:
582         case LFUN_CARON:
583         case LFUN_TILDE:
584         case LFUN_CEDILLA:
585         case LFUN_CIRCLE:
586         case LFUN_UNDERDOT:
587         case LFUN_TIE:
588         case LFUN_OGONEK:
589         case LFUN_HUNG_UMLAUT:
590                 break;
591
592         //  Math fonts
593         case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
594         case LFUN_BOLD:         handleFont(bv, cmd.argument, "textbf"); break;
595         case LFUN_SANS:         handleFont(bv, cmd.argument, "textsf"); break;
596         case LFUN_EMPH:         handleFont(bv, cmd.argument, "mathcal"); break;
597         case LFUN_ROMAN:        handleFont(bv, cmd.argument, "mathrm"); break;
598         case LFUN_CODE:         handleFont(bv, cmd.argument, "texttt"); break;
599         case LFUN_FRAK:         handleFont(bv, cmd.argument, "mathfrak"); break;
600         case LFUN_ITAL:         handleFont(bv, cmd.argument, "mathit"); break;
601         case LFUN_NOUN:         handleFont(bv, cmd.argument, "mathbb"); break;
602         case LFUN_DEFAULT:      handleFont(bv, cmd.argument, "textnormal"); break;
603         case LFUN_FREE:         handleFont(bv, cmd.argument, "textrm"); break;
604
605         case LFUN_GREEK:
606                 handleFont(bv, cmd.argument, "lyxgreek1");
607                 if (cmd.argument.size())
608                         mathcursor->interpret(cmd.argument);
609                 break;
610
611         case LFUN_MATH_MODE:
612                 if (mathcursor->currentMode())
613                         handleFont(bv, cmd.argument, "textrm");
614                 else {
615                         mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
616                         updateLocal(bv, true);
617                 }
618                 //bv->owner()->message(_("math text mode toggled"));
619                 break;
620
621         case LFUN_MATH_LIMITS:
622                 bv->lockedInsetStoreUndo(Undo::EDIT);
623                 if (mathcursor->toggleLimits())
624                         updateLocal(bv, true);
625                 break;
626
627         case LFUN_MATH_SIZE:
628 #if 0
629                 if (!arg.empty()) {
630                         bv->lockedInsetStoreUndo(Undo::EDIT);
631                         mathcursor->setSize(arg);
632                         updateLocal(bv, true);
633                 }
634 #endif
635                 break;
636
637         case LFUN_INSERT_MATRIX:
638                 if (!cmd.argument.empty()) {
639                         bv->lockedInsetStoreUndo(Undo::EDIT);
640                         mathcursor->interpret("matrix " + cmd.argument);
641                         updateLocal(bv, true);
642                 }
643                 break;
644
645         case LFUN_SUPERSCRIPT:
646         case LFUN_SUBSCRIPT:
647         {
648                 bv->lockedInsetStoreUndo(Undo::EDIT);
649                 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
650                 updateLocal(bv, true);
651                 break;
652         }
653
654         case LFUN_MATH_DELIM:
655         {
656                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
657                 string ls;
658                 string rs = split(cmd.argument, ls, ' ');
659                 // Reasonable default values
660                 if (ls.empty())
661                         ls = '(';
662                 if (rs.empty())
663                         rs = ')';
664
665                 bv->lockedInsetStoreUndo(Undo::EDIT);
666                 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
667                 updateLocal(bv, true);
668                 break;
669         }
670
671         case LFUN_PROTECTEDSPACE:
672         case LFUN_MATH_SPACE:
673                 bv->lockedInsetStoreUndo(Undo::EDIT);
674                 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
675                 updateLocal(bv, true);
676                 break;
677
678         case LFUN_UNDO:
679                 bv->owner()->message(_("Invalid action in math mode!"));
680                 break;
681
682
683         case LFUN_EXEC_COMMAND:
684                 result = UNDISPATCHED;
685                 break;
686
687         case LFUN_INSET_ERT:
688                 // interpret this as if a backslash was typed
689                 bv->lockedInsetStoreUndo(Undo::EDIT);
690                 mathcursor->interpret('\\');
691                 updateLocal(bv, true);
692                 break;
693
694         case LFUN_BREAKPARAGRAPH:
695         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
696         case LFUN_BREAKPARAGRAPH_SKIP:
697                 argument = "\n";
698                 // fall through
699
700         case -1:
701         case LFUN_INSERT_MATH:
702         case LFUN_SELFINSERT:
703                 if (!argument.empty()) {
704                         bv->lockedInsetStoreUndo(Undo::EDIT);
705                         if (argument.size() == 1)
706                                 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
707                         else
708                                 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
709                         updateLocal(bv, true);
710                 }
711                 break;
712
713         case LFUN_MATH_PANEL:
714                 result = UNDISPATCHED;
715                 break;
716
717         case LFUN_ESCAPE:
718                 if (mathcursor->selection())
719                         mathcursor->selClear();
720                 else
721                         result = UNDISPATCHED;
722                 break;
723
724         case LFUN_INSET_TOGGLE:
725                 mathcursor->insetToggle();
726                 updateLocal(bv, true);
727                 break;
728
729         case LFUN_REF_INSERT:
730                 //if (argument.empty()) {
731                 //      InsetCommandParams p("ref");
732                 //      owner_->getDialogs().createRef(p.getAsString());
733                 //} else {
734                 //      InsetCommandParams p;
735                 //      p.setFromString(argument);
736
737                 //      InsetRef * inset = new InsetRef(p, *buffer_);
738                 //      if (!insertInset(inset))
739                 //              delete inset;
740                 //      else
741                 //              updateInset(inset, true);
742                 //}
743                 //
744                 if (cmd.argument.empty()) {
745                         InsetCommandParams p("ref");
746                         bv->owner()->getDialogs().createRef(p.getAsString());
747                 } else {
748                         //mathcursor->handleNest(new InsetRef2);
749                         //mathcursor->insert(arg);
750                         mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
751                 }
752                 updateLocal(bv, true);
753                 break;
754
755         default:
756                 result = UNDISPATCHED;
757         }
758
759         mathcursor->normalize();
760         mathcursor->touch();
761
762         lyx::Assert(mathcursor);
763
764         if (mathcursor->selection() || was_selection)
765                 toggleInsetSelection(bv);
766
767         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
768             result == UNDISPATCHED) {
769                 fitInsetCursor(bv);
770                 showInsetCursor(bv);
771                 revealCodes(bv);
772         } else {
773                 releaseMathCursor(bv);
774                 bv->unlockInset(this);
775         }
776
777         return result;  // original version
778 }
779
780
781 void InsetFormulaBase::revealCodes(BufferView * bv) const
782 {
783         if (!mathcursor)
784                 return;
785         bv->owner()->message(mathcursor->info());
786
787 #if 0
788         // write something to the minibuffer
789         // translate to latex
790         mathcursor->markInsert();
791         ostringstream os;
792         write(NULL, os);
793         string str = os.str();
794         mathcursor->markErase();
795         string::size_type pos = 0;
796         string res;
797         for (string::iterator it = str.begin(); it != str.end(); ++it) {
798                 if (*it == '\n')
799                         res += ' ';
800                 else if (*it == '\0') {
801                         res += "  -X-  ";
802                         pos = it - str.begin();
803                 }
804                 else
805                         res += *it;
806         }
807         if (pos > 30)
808                 res = res.substr(pos - 30);
809         if (res.size() > 60)
810                 res = res.substr(0, 60);
811         bv->owner()->message(res);
812 #endif
813 }
814
815
816 Inset::Code InsetFormulaBase::lyxCode() const
817 {
818         return Inset::MATH_CODE;
819 }
820
821
822 int InsetFormulaBase::ylow() const
823 {
824         return yo_ - ascent(view(), font_);
825 }
826
827
828 int InsetFormulaBase::yhigh() const
829 {
830         return yo_ + descent(view(), font_);
831 }
832
833
834 int InsetFormulaBase::xlow() const
835 {
836         return xo_;
837 }
838
839
840 int InsetFormulaBase::xhigh() const
841 {
842         return xo_ + width(view(), font_);
843 }
844
845
846 /////////////////////////////////////////////////////////////////////
847
848
849 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
850                                      bool, bool)
851 {
852 #ifdef WITH_WARNINGS
853 #warning pretty ugly
854 #endif
855         static InsetFormulaBase * lastformula = 0;
856         static MathIterator current = MathIterator(ibegin(par().nucleus()));
857         static MathArray ar;
858         static string laststr;
859
860         if (lastformula != this || laststr != str) {
861                 //lyxerr << "reset lastformula to " << this << "\n";
862                 lastformula = this;
863                 laststr = str;
864                 current = ibegin(par().nucleus());
865                 ar.clear();
866                 mathed_parse_cell(ar, str);
867         } else {
868                 ++current;
869         }
870         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
871
872         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
873                 if (it.cell().matchpart(ar, it.back().pos_)) {
874                         bv->unlockInset(bv->theLockingInset());
875                         if (!bv->lockInset(this)) {
876                                 lyxerr << "Cannot lock inset" << endl;
877                                 return false;
878                         }
879                         delete mathcursor;
880                         mathcursor = new MathCursor(this, true);
881                         metrics(bv);
882                         mathcursor->setSelection(it, ar.size());
883                         current = it;
884                         it.jump(ar.size());
885                         updateLocal(bv, false);
886                         return true;
887                 }
888         }
889
890         //lyxerr << "not found!\n";
891         lastformula = 0;
892         return false;
893 }
894
895
896 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
897                                       bool a, bool b)
898 {
899         lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
900         return searchForward(bv, what, a, b);
901 }
902
903
904 bool InsetFormulaBase::display() const
905 {
906         return par()->asHullInset() && par()->asHullInset()->display();
907 }
908
909
910 /////////////////////////////////////////////////////////////////////
911
912
913 void mathDispatchCreation(FuncRequest const & cmd, bool display)
914 {
915         BufferView * bv = cmd.view();
916         // use selection if available..
917         //string sel;
918         //if (action == LFUN_MATH_IMPORT_SELECTION)
919         //      sel = "";
920         //else
921
922         string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
923
924         if (sel.empty()) {
925                 InsetFormula * f = new InsetFormula(bv);
926                 if (openNewInset(bv, f)) {
927                         bv->theLockingInset()->
928                                 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
929                         // don't do that also for LFUN_MATH_MODE unless you want end up with
930                         // always changing to mathrm when opening an inlined inset
931                         // -- I really hate "LyXfunc overloading"...
932                         if (display)
933                                 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
934                         f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
935                 }
936         } else {
937                 // create a macro if we see "\\newcommand" somewhere, and an ordinary
938                 // formula otherwise
939                 InsetFormulaBase * f;
940                 if (sel.find("\\newcommand") == string::npos &&
941                                 sel.find("\\def") == string::npos)
942                         f = new InsetFormula(sel);
943                 else
944                         f = new InsetFormulaMacro(sel);
945                 bv->getLyXText()->cutSelection(bv);
946                 openNewInset(bv, f);
947         }
948         bv->owner()->getLyXFunc().setMessage(N_("Math editor mode"));
949 }
950
951
952 void mathDispatch(FuncRequest const & cmd)
953 {
954         BufferView * bv = cmd.view();
955         if (!bv->available())
956                 return;
957
958         switch (cmd.action) {
959
960                 case LFUN_MATH_DISPLAY:
961                         mathDispatchCreation(cmd, true);
962                         break;
963
964                 case LFUN_MATH_MODE:
965                         mathDispatchCreation(cmd, false);
966                         break;
967
968                 case LFUN_MATH_IMPORT_SELECTION:
969                         mathDispatchCreation(cmd, false);
970                         break;
971
972                 case LFUN_MATH_MACRO:
973                         if (cmd.argument.empty())
974                                 bv->owner()->getLyXFunc().setErrorMessage(N_("Missing argument"));
975                         else {
976                                 string s = cmd.argument;
977                                 string const s1 = token(s, ' ', 1);
978                                 int const na = s1.empty() ? 0 : lyx::atoi(s1);
979                                 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
980                         }
981                         break;
982
983                 case LFUN_GREEK: 
984                 case LFUN_INSERT_MATH: 
985                 case LFUN_INSERT_MATRIX: 
986                 case LFUN_MATH_DELIM: {
987                         InsetFormula * f = new InsetFormula(bv);
988                         if (openNewInset(bv, f)) {
989                                 bv->theLockingInset()-> 
990                                         localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
991                                 bv->theLockingInset()->localDispatch(cmd);
992                         }
993                         break;
994                 }
995                 default:
996                         break;
997         }
998 }