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