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