]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacroTemplate.cpp
Make Buffer argument mandatory in most of the InsetMath based class... boring work...
[lyx.git] / src / mathed / MathMacroTemplate.cpp
1 /**
2  * \file MathMacroTemplate.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Stefan Schimanski
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "MathMacroTemplate.h"
15
16 #include "DocIterator.h"
17 #include "LaTeXFeatures.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathChar.h"
20 #include "InsetMathSqrt.h"
21 #include "MathMacro.h"
22 #include "MathMacroArgument.h"
23 #include "MathStream.h"
24 #include "MathParser.h"
25 #include "MathSupport.h"
26 #include "MathMacroArgument.h"
27
28 #include "Buffer.h"
29 #include "BufferView.h"
30 #include "Color.h"
31 #include "Cursor.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
35 #include "Lexer.h"
36 #include "Undo.h"
37
38 #include "frontends/Painter.h"
39
40 #include "support/lassert.h"
41 #include "support/convert.h"
42 #include "support/debug.h"
43 #include "support/gettext.h"
44 #include "support/docstream.h"
45 #include "support/lstrings.h"
46
47 #include <sstream>
48
49 using namespace std;
50
51 namespace lyx {
52
53 using support::bformat;
54
55 //////////////////////////////////////////////////////////////////////
56
57 class InsetLabelBox : public InsetMathNest {
58 public:
59         ///
60         InsetLabelBox(Buffer * buf, MathAtom const & atom, docstring label,
61                       MathMacroTemplate const & parent, bool frame = false);
62         InsetLabelBox(Buffer * buf, docstring label, MathMacroTemplate const & parent,
63                       bool frame = false);
64         ///
65         void metrics(MetricsInfo & mi, Dimension & dim) const;
66         ///
67         void draw(PainterInfo &, int x, int y) const;
68
69 protected:
70         ///
71         MathMacroTemplate const & parent_;
72         ///
73         Inset * clone() const;
74         ///
75         docstring const label_;
76         ///
77         bool frame_;
78 };
79
80
81 InsetLabelBox::InsetLabelBox(Buffer * buf, MathAtom const & atom, docstring label,
82         MathMacroTemplate const & parent, bool frame)
83         : InsetMathNest(buf, 1), parent_(parent), label_(label), frame_(frame)
84 {
85         cell(0).insert(0, atom);
86 }
87
88
89 InsetLabelBox::InsetLabelBox(Buffer * buf, docstring label,
90                              MathMacroTemplate const & parent, bool frame)
91         : InsetMathNest(buf, 1), parent_(parent), label_(label), frame_(frame)
92 {
93 }
94
95
96 Inset * InsetLabelBox::clone() const
97 {
98         return new InsetLabelBox(*this);
99 }
100
101
102 void InsetLabelBox::metrics(MetricsInfo & mi, Dimension & dim) const
103 {
104         // kernel
105         cell(0).metrics(mi, dim);
106
107         // frame
108         if (frame_) {
109                 dim.wid += 6;
110                 dim.asc += 5;
111                 dim.des += 5;
112         }
113
114         // adjust to common height in main metrics phase
115         if (!parent_.premetrics()) {
116                 dim.asc = max(dim.asc, parent_.commonLabelBoxAscent());
117                 dim.des = max(dim.des, parent_.commonLabelBoxDescent());
118         }
119
120         // label
121         if (parent_.editing(mi.base.bv) && label_.length() > 0) {
122                 // grey
123                 FontInfo font = sane_font;
124                 font.setSize(FONT_SIZE_TINY);
125                 font.setColor(Color_mathmacrolabel);
126
127                 // make space for label and box
128                 int lwid = mathed_string_width(font, label_);
129                 int maxasc;
130                 int maxdes;
131                 math_font_max_dim(font, maxasc, maxdes);
132
133                 dim.wid = max(dim.wid, lwid + 2);
134
135                 // space for the label
136                 if (!parent_.premetrics())
137                         dim.des += maxasc + maxdes + 1;
138         }
139 }
140
141
142 void InsetLabelBox::draw(PainterInfo & pi, int x, int y) const
143 {
144         Dimension const dim = dimension(*pi.base.bv);
145         Dimension const cdim = cell(0).dimension(*pi.base.bv);
146
147         // kernel
148         cell(0).draw(pi, x + (dim.wid - cdim.wid) / 2, y);
149
150         // label
151         if (parent_.editing(pi.base.bv) && label_.length() > 0) {
152                 // grey
153                 FontInfo font = sane_font;
154                 font.setSize(FONT_SIZE_TINY);
155                 font.setColor(Color_mathmacrolabel);
156
157                 // make space for label and box
158                 int lwid = mathed_string_width(font, label_);
159                 int maxasc;
160                 int maxdes;
161                 math_font_max_dim(font, maxasc, maxdes);
162
163                 if (lwid < dim.wid)
164                         pi.pain.text(x + (dim.wid - lwid) / 2, y + dim.des - maxdes, label_, font);
165                 else
166                         pi.pain.text(x, y + dim.des - maxdes, label_, font);
167         }
168
169         // draw frame
170         int boxHeight = parent_.commonLabelBoxAscent() + parent_.commonLabelBoxDescent();
171         if (frame_) {
172                 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
173                                   dim.wid - 2, boxHeight - 2,
174                                   Color_mathline);
175         }
176 }
177
178
179 //////////////////////////////////////////////////////////////////////
180
181 class DisplayLabelBox : public InsetLabelBox {
182 public:
183         ///
184         DisplayLabelBox(Buffer * buf, MathAtom const & atom, docstring label,
185                         MathMacroTemplate const & parent);
186
187         ///
188         void metrics(MetricsInfo & mi, Dimension & dim) const;
189         ///
190         void draw(PainterInfo &, int x, int y) const;
191
192 protected:
193         ///
194         Inset * clone() const;
195 };
196
197
198 DisplayLabelBox::DisplayLabelBox(Buffer * buf, MathAtom const & atom,
199                                  docstring label,
200                                  MathMacroTemplate const & parent)
201         : InsetLabelBox(buf, atom, label, parent, true)
202 {
203 }
204
205
206
207 Inset * DisplayLabelBox::clone() const
208 {
209         return new DisplayLabelBox(*this);
210 }
211
212
213 void DisplayLabelBox::metrics(MetricsInfo & mi, Dimension & dim) const
214 {
215         InsetLabelBox::metrics(mi, dim);
216         if (!parent_.editing(mi.base.bv)
217             && parent_.cell(parent_.displayIdx()).empty()) {
218                 dim.wid = 0;
219                 dim.asc = 0;
220                 dim.des = 0;
221         }
222 }
223
224
225 void DisplayLabelBox::draw(PainterInfo & pi, int x, int y) const
226 {
227         if (parent_.editing(pi.base.bv)
228             || !parent_.cell(parent_.displayIdx()).empty()) {
229                 InsetLabelBox::draw(pi, x, y);
230         } else {
231                 bool enabled = pi.pain.isDrawingEnabled();
232                 pi.pain.setDrawingEnabled(false);
233                 InsetLabelBox::draw(pi, x, y);
234                 pi.pain.setDrawingEnabled(enabled);
235         }
236 }
237
238
239 //////////////////////////////////////////////////////////////////////
240
241 class InsetMathWrapper : public InsetMath {
242 public:
243         ///
244         InsetMathWrapper(MathData const * value) : value_(value) {}
245         ///
246         void metrics(MetricsInfo & mi, Dimension & dim) const;
247         ///
248         void draw(PainterInfo &, int x, int y) const;
249
250 private:
251         ///
252         Inset * clone() const;
253         ///
254         MathData const * value_;
255 };
256
257
258 Inset * InsetMathWrapper::clone() const
259 {
260         return new InsetMathWrapper(*this);
261 }
262
263
264 void InsetMathWrapper::metrics(MetricsInfo & mi, Dimension & dim) const
265 {
266         value_->metrics(mi, dim);
267         //metricsMarkers2(dim);
268 }
269
270
271 void InsetMathWrapper::draw(PainterInfo & pi, int x, int y) const
272 {
273         value_->draw(pi, x, y);
274         //drawMarkers(pi, x, y);
275 }
276
277
278 ///////////////////////////////////////////////////////////////////////
279 class InsetColoredCell : public InsetMathNest {
280 public:
281         ///
282         InsetColoredCell(Buffer * buf, ColorCode min, ColorCode max);
283         ///
284         InsetColoredCell(Buffer * buf, ColorCode min, ColorCode max, MathAtom const & atom);
285         ///
286         void draw(PainterInfo &, int x, int y) const;
287         ///
288         void metrics(MetricsInfo & mi, Dimension & dim) const;
289
290 protected:
291         ///
292         Inset * clone() const;
293         ///
294         ColorCode min_;
295         ///
296         ColorCode max_;
297 };
298
299
300 InsetColoredCell::InsetColoredCell(Buffer * buf, ColorCode min, ColorCode max)
301         : InsetMathNest(buf, 1), min_(min), max_(max)
302 {
303 }
304
305
306 InsetColoredCell::InsetColoredCell(Buffer * buf, ColorCode min, ColorCode max, MathAtom const & atom)
307         : InsetMathNest(buf, 1), min_(min), max_(max)
308 {
309         cell(0).insert(0, atom);
310 }
311
312
313 Inset * InsetColoredCell::clone() const
314 {
315         return new InsetColoredCell(*this);
316 }
317
318
319 void InsetColoredCell::metrics(MetricsInfo & mi, Dimension & dim) const
320 {
321         cell(0).metrics(mi, dim);
322 }
323
324
325 void InsetColoredCell::draw(PainterInfo & pi, int x, int y) const
326 {
327         pi.pain.enterMonochromeMode(min_, max_);
328         cell(0).draw(pi, x, y);
329         pi.pain.leaveMonochromeMode();
330 }
331
332
333 ///////////////////////////////////////////////////////////////////////
334
335 class InsetNameWrapper : public InsetMathWrapper {
336 public:
337         ///
338         InsetNameWrapper(MathData const * value, MathMacroTemplate const & parent);
339         ///
340         void metrics(MetricsInfo & mi, Dimension & dim) const;
341         ///
342         void draw(PainterInfo &, int x, int y) const;
343
344 private:
345         ///
346         MathMacroTemplate const & parent_;
347         ///
348         Inset * clone() const;
349 };
350
351
352 InsetNameWrapper::InsetNameWrapper(MathData const * value,
353                                    MathMacroTemplate const & parent)
354         : InsetMathWrapper(value), parent_(parent)
355 {
356 }
357
358
359 Inset * InsetNameWrapper::clone() const
360 {
361         return new InsetNameWrapper(*this);
362 }
363
364
365 void InsetNameWrapper::metrics(MetricsInfo & mi, Dimension & dim) const
366 {
367         InsetMathWrapper::metrics(mi, dim);
368         dim.wid += mathed_string_width(mi.base.font, from_ascii("\\"));
369 }
370
371
372 void InsetNameWrapper::draw(PainterInfo & pi, int x, int y) const
373 {
374         // create fonts
375         PainterInfo namepi = pi;
376         if (parent_.validMacro())
377                 namepi.base.font.setColor(Color_latex);
378         else
379                 namepi.base.font.setColor(Color_error);
380
381         // draw backslash
382         pi.pain.text(x, y, from_ascii("\\"), namepi.base.font);
383         x += mathed_string_width(namepi.base.font, from_ascii("\\"));
384
385         // draw name
386         InsetMathWrapper::draw(namepi, x, y);
387 }
388
389
390 ///////////////////////////////////////////////////////////////////////
391
392
393 MathMacroTemplate::MathMacroTemplate(Buffer * buf)
394         : InsetMathNest(buf, 3), numargs_(0), argsInLook_(0), optionals_(0),
395           type_(MacroTypeNewcommand), lookOutdated_(true)
396 {
397         initMath();
398 }
399
400
401 MathMacroTemplate::MathMacroTemplate(Buffer * buf, docstring const & name, int numargs,
402         int optionals, MacroType type, vector<MathData> const & optionalValues,
403         MathData const & def, MathData const & display)
404         : InsetMathNest(buf, optionals + 3), numargs_(numargs), argsInLook_(numargs),
405           optionals_(optionals), optionalValues_(optionalValues),
406           type_(type), lookOutdated_(true)
407 {
408         initMath();
409
410         if (numargs_ > 9)
411                 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
412                         << numargs_ << endl;
413
414         asArray(name, cell(0));
415         optionalValues_.resize(9);
416         for (int i = 0; i < optionals_; ++i)
417                 cell(optIdx(i)) = optionalValues_[i];
418         cell(defIdx()) = def;
419         cell(displayIdx()) = display;
420
421         updateLook();
422 }
423
424
425 MathMacroTemplate::MathMacroTemplate(Buffer * buf, docstring const & str)
426         : InsetMathNest(buf, 3), numargs_(0), optionals_(0),
427         type_(MacroTypeNewcommand), lookOutdated_(true)
428 {
429         buffer_ = buf;
430         initMath();
431
432         MathData ar(buf);
433         mathed_parse_cell(ar, str, Parse::NORMAL);
434         if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
435                 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
436                 asArray(from_ascii("invalidmacro"), cell(0));
437                 // FIXME: The macro template does not make sense after this.
438                 // The whole parsing should not be in a constructor which
439                 // has no chance to report failure.
440                 return;
441         }
442         operator=( *(ar[0]->asMacroTemplate()) );
443
444         updateLook();
445 }
446
447
448 Inset * MathMacroTemplate::clone() const
449 {
450         MathMacroTemplate * inset = new MathMacroTemplate(*this);
451         // the parent pointers of the proxy insets above will point to
452         // to the old template. Hence, the look must be updated.
453         inset->updateLook();
454         return inset;
455 }
456
457
458 docstring MathMacroTemplate::name() const
459 {
460         return asString(cell(0));
461 }
462
463
464 void MathMacroTemplate::updateToContext(MacroContext const & mc) const
465 {
466         redefinition_ = mc.get(name()) != 0;
467 }
468
469
470 void MathMacroTemplate::updateLook() const
471 {
472         lookOutdated_ = true;
473 }
474
475
476 void MathMacroTemplate::createLook(int args) const
477 {
478         look_.clear();
479         argsInLook_ = args;
480
481         // \foo
482         look_.push_back(MathAtom(
483                 new InsetLabelBox(buffer_, _("Name"), *this, false)));
484         MathData & nameData = look_[look_.size() - 1].nucleus()->cell(0);
485         nameData.push_back(MathAtom(new InsetNameWrapper(&cell(0), *this)));
486
487         // [#1][#2]
488         int i = 0;
489         if (optionals_ > 0) {
490                 look_.push_back(MathAtom(
491                         new InsetLabelBox(buffer_, _("optional"), *this, false)));
492                 
493                 MathData * optData = &look_[look_.size() - 1].nucleus()->cell(0);
494                 for (; i < optionals_; ++i) {
495                         // color it light grey, if it is to be removed when the cursor leaves
496                         if (i == argsInLook_) {
497                                 optData->push_back(MathAtom(
498                                         new InsetColoredCell(buffer_, Color_mathbg, Color_mathmacrooldarg)));
499                                 optData = &(*optData)[optData->size() - 1].nucleus()->cell(0);
500                         }
501
502                         optData->push_back(MathAtom(new InsetMathChar('[')));
503                         optData->push_back(MathAtom(new InsetMathWrapper(&cell(1 + i))));
504                         optData->push_back(MathAtom(new InsetMathChar(']')));
505                 }
506         }
507
508         // {#3}{#4}
509         for (; i < numargs_; ++i) {
510                 MathData arg;
511                 arg.push_back(MathAtom(new MathMacroArgument(i + 1)));
512                 if (i >= argsInLook_) {
513                         look_.push_back(MathAtom(new InsetColoredCell(buffer_,
514                                 Color_mathbg, Color_mathmacrooldarg,
515                                 MathAtom(new InsetMathBrace(arg)))));
516                 } else
517                         look_.push_back(MathAtom(new InsetMathBrace(arg)));
518         }
519         for (; i < argsInLook_; ++i) {
520                 MathData arg;
521                 arg.push_back(MathAtom(new MathMacroArgument(i + 1)));
522                 look_.push_back(MathAtom(new InsetColoredCell(buffer_,
523                         Color_mathbg, Color_mathmacronewarg,
524                         MathAtom(new InsetMathBrace(arg)))));
525         }
526         
527         // :=
528         look_.push_back(MathAtom(new InsetMathChar(':')));
529         look_.push_back(MathAtom(new InsetMathChar('=')));
530
531         // definition
532         look_.push_back(MathAtom(
533                 new InsetLabelBox(buffer_, MathAtom(
534                         new InsetMathWrapper(&cell(defIdx()))), _("TeX"), *this,        true)));
535
536         // display
537         look_.push_back(MathAtom(
538                 new DisplayLabelBox(buffer_, MathAtom(
539                         new InsetMathWrapper(&cell(displayIdx()))), _("LyX"), *this)));
540 }
541
542
543 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
544 {
545         FontSetChanger dummy1(mi.base, from_ascii("mathnormal"));
546         StyleChanger dummy2(mi.base, LM_ST_TEXT);
547
548         // valid macro?
549         MacroData const * macro = 0;
550         if (validName()) {
551                 macro = mi.macrocontext.get(name());
552
553                 // updateToContext() - avoids another lookup
554                 redefinition_ = macro != 0;
555         }
556
557         // update look?
558         int argsInDef = maxArgumentInDefinition();
559         if (lookOutdated_ || argsInDef != argsInLook_) {
560                 lookOutdated_ = false;
561                 createLook(argsInDef);
562         }
563
564         /// metrics for inset contents
565         if (macro)
566                 macro->lock();
567
568         // first phase, premetric:
569         premetrics_ = true;
570         look_.metrics(mi, dim);
571         labelBoxAscent_ = dim.asc;
572         labelBoxDescent_ = dim.des;
573
574         // second phase, main metric:
575         premetrics_ = false;
576         look_.metrics(mi, dim);
577
578         if (macro)
579                 macro->unlock();
580
581         dim.wid += 6;
582         dim.des += 2;
583         dim.asc += 2;
584
585         setDimCache(mi, dim);
586 }
587
588
589 void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
590 {
591         FontSetChanger dummy1(pi.base, from_ascii("mathnormal"));
592         StyleChanger dummy2(pi.base, LM_ST_TEXT);
593
594         setPosCache(pi, x, y);
595         Dimension const dim = dimension(*pi.base.bv);
596
597         // draw outer frame
598         int const a = y - dim.asc + 1;
599         int const w = dim.wid - 2;
600         int const h = dim.height() - 2;
601         pi.pain.rectangle(x, a, w, h, Color_mathframe);
602
603         // just to be sure: set some dummy values for coord cache
604         for (idx_type i = 0; i < nargs(); ++i)
605                 cell(i).setXY(*pi.base.bv, x, y);
606
607         // draw contents
608         look_.draw(pi, x + 3, y);
609 }
610
611
612 void MathMacroTemplate::edit(Cursor & cur, bool front, EntryDirection entry_from)
613 {
614         updateLook();
615         cur.updateFlags(Update::SinglePar);
616         InsetMathNest::edit(cur, front, entry_from);
617 }
618
619
620 bool MathMacroTemplate::notifyCursorLeaves(Cursor const & old, Cursor & cur)
621 {
622         // find this in cursor old
623         Cursor insetCur = old;
624         int scriptSlice = insetCur.find(this);
625         LASSERT(scriptSlice != -1, /**/);
626         insetCur.cutOff(scriptSlice);
627         
628         commitEditChanges(insetCur);
629         updateLook();
630         cur.updateFlags(Update::Force);
631         return InsetMathNest::notifyCursorLeaves(old, cur);
632 }
633
634
635 void MathMacroTemplate::removeArguments(Cursor & cur, int from, int to)
636 {
637         for (DocIterator it = doc_iterator_begin(&buffer(), this); it; it.forwardChar()) {
638                 if (!it.nextInset())
639                         continue;
640                 if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE)
641                         continue;
642                 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
643                 int n = arg->number() - 1;
644                 if (from <= n && n <= to) {
645                         int cellSlice = cur.find(it.cell());
646                         if (cellSlice != -1 && cur[cellSlice].pos() > it.pos())
647                                 --cur[cellSlice].pos();
648
649                         it.cell().erase(it.pos());
650                 }
651         }
652
653         updateLook();
654 }
655
656
657 void MathMacroTemplate::shiftArguments(size_t from, int by)
658 {
659         for (DocIterator it = doc_iterator_begin(&buffer(), this); it; it.forwardChar()) {
660                 if (!it.nextInset())
661                         continue;
662                 if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE)
663                         continue;
664                 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
665                 if (arg->number() >= int(from) + 1)
666                         arg->setNumber(arg->number() + by);
667         }
668
669         updateLook();
670 }
671
672
673 int MathMacroTemplate::maxArgumentInDefinition() const
674 {
675         // We don't have a buffer when pasting from the clipboard (bug 6014).
676         Buffer const * macro_buffer = this->isBufferValid() ? &buffer() : 0;
677         int maxArg = 0;
678         DocIterator it = doc_iterator_begin(macro_buffer, this);
679         it.idx() = defIdx();
680         for (; it; it.forwardChar()) {
681                 if (!it.nextInset())
682                         continue;
683                 if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE)
684                         continue;
685                 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
686                 maxArg = std::max(int(arg->number()), maxArg);
687         }
688         return maxArg;
689 }
690
691
692 void MathMacroTemplate::insertMissingArguments(int maxArg)
693 {
694         bool found[9] = { false, false, false, false, false, false, false, false, false };
695         idx_type idx = cell(displayIdx()).empty() ? defIdx() : displayIdx();
696
697         // search for #n macros arguments
698         DocIterator it = doc_iterator_begin(&buffer(), this);
699         it.idx() = idx;
700         for (; it && it[0].idx() == idx; it.forwardChar()) {
701                 if (!it.nextInset())
702                         continue;
703                 if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE)
704                         continue;
705                 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
706                 found[arg->number() - 1] = true;
707         }
708
709         // add missing ones
710         for (int i = 0; i < maxArg; ++i) {
711                 if (found[i])
712                         continue;
713
714                 cell(idx).push_back(MathAtom(new MathMacroArgument(i + 1)));
715         }
716 }
717
718
719 void MathMacroTemplate::changeArity(Cursor & cur, int newNumArg)
720 {
721         // remove parameter which do not appear anymore in the definition
722         for (int i = numargs_; i > newNumArg; --i)
723                 removeParameter(cur, numargs_ - 1, false);
724         
725         // add missing parameter
726         for (int i = numargs_; i < newNumArg; ++i)
727                 insertParameter(cur, numargs_, false, false);
728 }
729
730
731 void MathMacroTemplate::commitEditChanges(Cursor & cur)
732 {
733         int argsInDef = maxArgumentInDefinition();
734         if (argsInDef != numargs_) {
735                 cur.recordUndoFullDocument();
736                 changeArity(cur, argsInDef);
737         }
738         insertMissingArguments(argsInDef);
739 }
740
741
742 // FIXME: factorize those functions here with a functional style, maybe using Boost's function
743 // objects?
744
745 void fixMacroInstancesAddRemove(Cursor const & from, docstring const & name, int n, bool insert) {
746         Cursor dit = from;
747
748         for (; dit; dit.forwardPos()) {
749                 // only until a macro is redefined
750                 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
751                         MathMacroTemplate const & macroTemplate
752                         = static_cast<MathMacroTemplate const &>(dit.inset());
753                         if (macroTemplate.name() == name)
754                                 break;
755                 }
756
757                 // in front of macro instance?
758                 Inset * inset = dit.nextInset();
759                 if (!inset)
760                         continue;
761                 InsetMath * insetMath = inset->asInsetMath();
762                 if (!insetMath)
763                         continue;
764
765                 MathMacro * macro = insetMath->asMacro();
766                 if (macro && macro->name() == name && macro->folded()) {
767                         // found macro instance
768                         if (insert)
769                                 macro->insertArgument(n);
770                         else
771                                 macro->removeArgument(n);
772                 }
773         }
774 }
775
776
777 void fixMacroInstancesOptional(Cursor const & from, docstring const & name, int optionals) {
778         Cursor dit = from;
779
780         for (; dit; dit.forwardPos()) {
781                 // only until a macro is redefined
782                 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
783                         MathMacroTemplate const & macroTemplate
784                         = static_cast<MathMacroTemplate const &>(dit.inset());
785                         if (macroTemplate.name() == name)
786                                 break;
787                 }
788
789                 // in front of macro instance?
790                 Inset * inset = dit.nextInset();
791                 if (!inset)
792                         continue;
793                 InsetMath * insetMath = inset->asInsetMath();
794                 if (!insetMath)
795                         continue;
796                 MathMacro * macro = insetMath->asMacro();
797                 if (macro && macro->name() == name && macro->folded()) {
798                         // found macro instance
799                         macro->setOptionals(optionals);
800                 }
801         }
802 }
803
804
805 template<class F>
806 void fixMacroInstancesFunctional(Cursor const & from,
807         docstring const & name, F & fix) {
808         Cursor dit = from;
809
810         for (; dit; dit.forwardPos()) {
811                 // only until a macro is redefined
812                 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
813                         MathMacroTemplate const & macroTemplate
814                         = static_cast<MathMacroTemplate const &>(dit.inset());
815                         if (macroTemplate.name() == name)
816                                 break;
817                 }
818
819                 // in front of macro instance?
820                 Inset * inset = dit.nextInset();
821                 if (!inset)
822                         continue;
823                 InsetMath * insetMath = inset->asInsetMath();
824                 if (!insetMath)
825                         continue;
826                 MathMacro * macro = insetMath->asMacro();
827                 if (macro && macro->name() == name && macro->folded())
828                         F(macro);
829         }
830 }
831
832
833 void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy, bool addarg)
834 {
835         if (pos <= numargs_ && pos >= optionals_ && numargs_ < 9) {
836                 ++numargs_;
837                 
838                 // append example #n
839                 if (addarg) {
840                         shiftArguments(pos, 1);
841
842                         cell(defIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
843                         if (!cell(displayIdx()).empty())
844                                 cell(displayIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
845                 }
846
847                 if (!greedy) {
848                         Cursor dit = cur;
849                         dit.leaveInset(*this);
850                         // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
851                         dit.top().forwardPos();
852
853                         // fix macro instances
854                         fixMacroInstancesAddRemove(dit, name(), pos, true);
855                 }
856         }
857
858         updateLook();
859 }
860
861
862 void MathMacroTemplate::removeParameter(Cursor & cur, int pos, bool greedy)
863 {
864         if (pos < numargs_ && pos >= 0) {
865                 --numargs_;
866                 removeArguments(cur, pos, pos);
867                 shiftArguments(pos + 1, -1);
868
869                 // removed optional parameter?
870                 if (pos < optionals_) {
871                         --optionals_;
872                         optionalValues_[pos] = cell(optIdx(pos));
873                         cells_.erase(cells_.begin() + optIdx(pos));
874
875                         // fix cursor
876                         int macroSlice = cur.find(this);
877                         if (macroSlice != -1) {
878                                 if (cur[macroSlice].idx() == optIdx(pos)) {
879                                         cur.cutOff(macroSlice);
880                                         cur[macroSlice].idx() = 1;
881                                         cur[macroSlice].pos() = 0;
882                                 } else if (cur[macroSlice].idx() > optIdx(pos))
883                                         --cur[macroSlice].idx();
884                         }
885                 }
886
887                 if (!greedy) {
888                         // fix macro instances
889                         //boost::function<void(MathMacro *)> fix = _1->insertArgument(n);
890                         //fixMacroInstancesFunctional(dit, name(), fix);
891                         Cursor dit = cur;
892                         dit.leaveInset(*this);
893                         // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
894                         dit.top().forwardPos();
895                         fixMacroInstancesAddRemove(dit, name(), pos, false);
896                 }
897         }
898
899         updateLook();
900 }
901
902
903 void MathMacroTemplate::makeOptional(Cursor & cur) {
904         if (numargs_ > 0 && optionals_ < numargs_) {
905                 ++optionals_;
906                 cells_.insert(cells_.begin() + optIdx(optionals_ - 1), optionalValues_[optionals_ - 1]);
907                 // fix cursor
908                 int macroSlice = cur.find(this);
909                 if (macroSlice != -1 && cur[macroSlice].idx() >= optIdx(optionals_ - 1))
910                         ++cur[macroSlice].idx();
911
912                 // fix macro instances
913                 Cursor dit = cur;
914                 dit.leaveInset(*this);
915                 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
916                 dit.top().forwardPos();
917                 fixMacroInstancesOptional(dit, name(), optionals_);
918         }
919
920         updateLook();
921 }
922
923
924 void MathMacroTemplate::makeNonOptional(Cursor & cur) {
925         if (numargs_ > 0 && optionals_ > 0) {
926                 --optionals_;
927
928                 // store default value for later if the user changes his mind
929                 optionalValues_[optionals_] = cell(optIdx(optionals_));
930                 cells_.erase(cells_.begin() + optIdx(optionals_));
931
932                 // fix cursor
933                 int macroSlice = cur.find(this);
934                 if (macroSlice != -1) {
935                         if (cur[macroSlice].idx() > optIdx(optionals_))
936                                 --cur[macroSlice].idx();
937                         else if (cur[macroSlice].idx() == optIdx(optionals_)) {
938                                 cur.cutOff(macroSlice);
939                                 cur[macroSlice].idx() = optIdx(optionals_);
940                                 cur[macroSlice].pos() = 0;
941                         }
942                 }
943
944                 // fix macro instances
945                 Cursor dit = cur;
946                 dit.leaveInset(*this);
947                 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
948                 dit.top().forwardPos();
949                 fixMacroInstancesOptional(dit, name(), optionals_);
950         }
951
952         updateLook();
953 }
954
955
956 void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
957 {
958         string const arg = to_utf8(cmd.argument());
959         switch (cmd.action) {
960
961         case LFUN_MATH_MACRO_ADD_PARAM:
962                 if (numargs_ < 9) {
963                         commitEditChanges(cur);
964                         cur.recordUndoFullDocument();
965                         size_t pos = numargs_;
966                         if (arg.size() != 0)
967                                 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
968                         insertParameter(cur, pos);
969                 }
970                 break;
971
972
973         case LFUN_MATH_MACRO_REMOVE_PARAM:
974                 if (numargs_ > 0) {
975                         commitEditChanges(cur);
976                         cur.recordUndoFullDocument();
977                         size_t pos = numargs_ - 1;
978                         if (arg.size() != 0)
979                                 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
980                         removeParameter(cur, pos);
981                 }
982                 break;
983
984         case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
985                 if (numargs_ < 9) {
986                         commitEditChanges(cur);
987                         cur.recordUndoFullDocument();
988                         insertParameter(cur, numargs_, true);
989                 }
990                 break;
991
992         case LFUN_MATH_MACRO_REMOVE_GREEDY_PARAM:
993                 if (numargs_ > 0) {
994                         commitEditChanges(cur);
995                         cur.recordUndoFullDocument();
996                         removeParameter(cur, numargs_ - 1, true);
997                 }
998                 break;
999
1000         case LFUN_MATH_MACRO_MAKE_OPTIONAL:
1001                 commitEditChanges(cur);
1002                 cur.recordUndoFullDocument();
1003                 makeOptional(cur);
1004                 break;
1005
1006         case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
1007                 commitEditChanges(cur);
1008                 cur.recordUndoFullDocument();
1009                 makeNonOptional(cur);
1010                 break;
1011
1012         case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
1013                 if (numargs_ < 9) {
1014                         commitEditChanges(cur);
1015                         cur.recordUndoFullDocument();
1016                         insertParameter(cur, optionals_);
1017                         makeOptional(cur);
1018                 }
1019                 break;
1020
1021         case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
1022                 if (optionals_ > 0) {
1023                         commitEditChanges(cur);
1024                         cur.recordUndoFullDocument();
1025                         removeParameter(cur, optionals_ - 1);
1026                 } break;
1027
1028         case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
1029                 if (numargs_ == optionals_) {
1030                         commitEditChanges(cur);
1031                         cur.recordUndoFullDocument();
1032                         insertParameter(cur, 0, true);
1033                         makeOptional(cur);
1034                 }
1035                 break;
1036
1037         default:
1038                 InsetMathNest::doDispatch(cur, cmd);
1039                 break;
1040         }
1041 }
1042
1043
1044 bool MathMacroTemplate::getStatus(Cursor & /*cur*/, FuncRequest const & cmd,
1045         FuncStatus & flag) const
1046 {
1047         bool ret = true;
1048         string const arg = to_utf8(cmd.argument());
1049         switch (cmd.action) {
1050                 case LFUN_MATH_MACRO_ADD_PARAM: {
1051                         int num = numargs_ + 1;
1052                         if (arg.size() != 0)
1053                                 num = convert<int>(arg);
1054                         bool on = (num >= optionals_
1055                                    && numargs_ < 9 && num <= numargs_ + 1);
1056                         flag.setEnabled(on);
1057                         break;
1058                 }
1059
1060                 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
1061                         flag.setEnabled(numargs_ < 9);
1062                         break;
1063
1064                 case LFUN_MATH_MACRO_REMOVE_PARAM: {
1065                         int num = numargs_;
1066                         if (arg.size() != 0)
1067                                 num = convert<int>(arg);
1068                         flag.setEnabled(num >= 1 && num <= numargs_);
1069                         break;
1070                 }
1071
1072                 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
1073                         flag.setEnabled(numargs_ > 0
1074                                      && optionals_ < numargs_
1075                                      && type_ != MacroTypeDef);
1076                         break;
1077
1078                 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
1079                         flag.setEnabled(optionals_ > 0
1080                                      && type_ != MacroTypeDef);
1081                         break;
1082
1083                 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
1084                         flag.setEnabled(numargs_ < 9);
1085                         break;
1086
1087                 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
1088                         flag.setEnabled(optionals_ > 0);
1089                         break;
1090
1091                 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
1092                         flag.setEnabled(numargs_ == 0
1093                                      && type_ != MacroTypeDef);
1094                         break;
1095
1096                 case LFUN_IN_MATHMACROTEMPLATE:
1097                         flag.setEnabled(true);
1098                         break;
1099
1100                 default:
1101                         ret = false;
1102                         break;
1103         }
1104         return ret;
1105 }
1106
1107
1108 void MathMacroTemplate::read(Lexer & lex)
1109 {
1110         MathData ar(buffer_);
1111         mathed_parse_cell(ar, lex.getStream(), Parse::NORMAL);
1112         if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
1113                 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
1114                 lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
1115                 return;
1116         }
1117         operator=( *(ar[0]->asMacroTemplate()) );
1118
1119         updateLook();
1120 }
1121
1122
1123 void MathMacroTemplate::write(ostream & os) const
1124 {
1125         odocstringstream oss;
1126         WriteStream wi(oss, false, false, WriteStream::wsDefault);
1127         oss << "FormulaMacro\n";
1128         write(wi);
1129         os << to_utf8(oss.str());
1130 }
1131
1132
1133 void MathMacroTemplate::write(WriteStream & os) const
1134 {
1135         write(os, false);
1136 }
1137
1138
1139 void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
1140 {
1141         if (os.latex()) {
1142                 if (optionals_ > 0) {
1143                         // macros with optionals use the xargs package, e.g.:
1144                         // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
1145                         // \long is implicit by xargs
1146                         if (redefinition_ && !overwriteRedefinition)
1147                                 os << "\\renewcommandx";
1148                         else
1149                                 os << "\\newcommandx";
1150
1151                         os << "\\" << name()
1152                            << "[" << numargs_ << "]"
1153                            << "[usedefault, addprefix=\\global";
1154                         for (int i = 0; i < optionals_; ++i) {
1155                                 docstring optValue = asString(cell(optIdx(i)));
1156                                 if (optValue.find(']') != docstring::npos
1157                                     || optValue.find(',') != docstring::npos)
1158                                         os << ", " << i + 1 << "="
1159                                         << "{" << cell(optIdx(i)) << "}";
1160                                 else
1161                                         os << ", " << i + 1 << "="
1162                                         << cell(optIdx(i));
1163                         }
1164                         os << "]";
1165                 } else {
1166                         // Macros without optionals use standard _global_ \def macros:
1167                         //   \global\def\long\foo#1#2{#1,#2}
1168                         // We use the \long prefix as this is the equivalent to \newcommand.
1169                         // We cannot use \newcommand directly because \global does not work with it.
1170                         os << "\\global\\long\\def\\" << name();
1171                         docstring param = from_ascii("#0");
1172                         for (int i = 1; i <= numargs_; ++i) { 
1173                                 param[1] = '0' + i;
1174                                 os << param;
1175                         }
1176                 }
1177         } else {
1178                 // in LyX output we use some pseudo syntax which is implementation
1179                 // independent, e.g.
1180                 // \newcommand{\foo}[2][default]{#1,#2}
1181                 if (redefinition_ && !overwriteRedefinition)
1182                         os << "\\renewcommand";
1183                 else
1184                         os << "\\newcommand";
1185                 os << "{\\" << name() << '}';
1186                 if (numargs_ > 0)
1187                         os << '[' << numargs_ << ']';
1188
1189                 for (int i = 0; i < optionals_; ++i) {
1190                         docstring optValue = asString(cell(optIdx(i)));
1191                         if (optValue.find(']') != docstring::npos)
1192                                 os << "[{" << cell(optIdx(i)) << "}]";
1193                         else
1194                                 os << "[" << cell(optIdx(i)) << "]";
1195                 }
1196         }
1197
1198         os << "{" << cell(defIdx()) << "}";
1199
1200         if (os.latex()) {
1201                 // writing .tex. done.
1202                 os << "\n";
1203         } else {
1204                 // writing .lyx, write special .tex export only if necessary
1205                 if (!cell(displayIdx()).empty())
1206                         os << "\n{" << cell(displayIdx()) << '}';
1207         }
1208 }
1209
1210
1211 int MathMacroTemplate::plaintext(odocstream & os,
1212                                  OutputParams const &) const
1213 {
1214         static docstring const str = '[' + buffer().B_("math macro") + ']';
1215
1216         os << str;
1217         return str.size();
1218 }
1219
1220
1221 bool MathMacroTemplate::validName() const
1222 {
1223         docstring n = name();
1224
1225         // empty name?
1226         if (n.size() == 0)
1227                 return false;
1228
1229         // converting back and force doesn't swallow anything?
1230         /*MathData ma;
1231         asArray(n, ma);
1232         if (asString(ma) != n)
1233                 return false;*/
1234
1235         // valid characters?
1236         for (size_t i = 0; i < n.size(); ++i) {
1237                 if (!(n[i] >= 'a' && n[i] <= 'z')
1238                     && !(n[i] >= 'A' && n[i] <= 'Z')
1239                     && n[i] != '*')
1240                         return false;
1241         }
1242
1243         return true;
1244 }
1245
1246
1247 bool MathMacroTemplate::validMacro() const
1248 {
1249         return validName();
1250 }
1251
1252
1253 bool MathMacroTemplate::fixNameAndCheckIfValid()
1254 {
1255         // check all the characters/insets in the name cell
1256         size_t i = 0;
1257         MathData & data = cell(0);
1258         while (i < data.size()) {
1259                 InsetMathChar const * cinset = data[i]->asCharInset();
1260                 if (cinset) {
1261                         // valid character in [a-zA-Z]?
1262                         char_type c = cinset->getChar();
1263                         if ((c >= 'a' && c <= 'z')
1264                             || (c >= 'A' && c <= 'Z')) {
1265                                 ++i;
1266                                 continue;
1267                         }
1268                 }
1269
1270                 // throw cell away
1271                 data.erase(i);
1272         }
1273
1274         // now it should be valid if anything in the name survived
1275         return data.size() > 0;
1276 }
1277
1278         
1279 void MathMacroTemplate::validate(LaTeXFeatures & features) const
1280 {
1281         // we need global optional macro arguments. They are not available 
1282         // with \def, and \newcommand does not support global macros. So we
1283         // are bound to xargs also for the single-optional-parameter case.
1284         if (optionals_ > 0)
1285                 features.require("xargs");
1286 }
1287
1288 void MathMacroTemplate::getDefaults(vector<docstring> & defaults) const
1289 {
1290         defaults.resize(numargs_);
1291         for (int i = 0; i < optionals_; ++i)
1292                 defaults[i] = asString(cell(optIdx(i)));
1293 }
1294
1295
1296 docstring MathMacroTemplate::definition() const
1297 {
1298         return asString(cell(defIdx()));
1299 }
1300
1301
1302 docstring MathMacroTemplate::displayDefinition() const
1303 {
1304         return asString(cell(displayIdx()));
1305 }
1306
1307
1308 size_t MathMacroTemplate::numArgs() const
1309 {
1310         return numargs_;
1311 }
1312
1313
1314 size_t MathMacroTemplate::numOptionals() const
1315 {
1316         return optionals_;
1317 }
1318
1319
1320 void MathMacroTemplate::infoize(odocstream & os) const
1321 {
1322         os << "Math Macro: \\" << name();
1323 }
1324
1325
1326 docstring MathMacroTemplate::contextMenu(BufferView const &, int, int) const
1327 {
1328         return from_ascii("context-math-macro-definition");
1329 }
1330
1331 } // namespace lyx