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