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