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