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