]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacro.cpp
8197444dffedb737007d2939a5c9455ca86efdec
[lyx.git] / src / mathed / MathMacro.cpp
1 /**
2  * \file MathMacro.cpp
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  * \author Stefan Schimanski
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "MathMacro.h"
16
17 #include "MathCompletionList.h"
18 #include "MathExtern.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
21
22 #include "Buffer.h"
23 #include "BufferView.h"
24 #include "CoordCache.h"
25 #include "Cursor.h"
26 #include "FuncStatus.h"
27 #include "FuncRequest.h"
28 #include "LaTeXFeatures.h"
29 #include "LyXFunc.h"
30 #include "LyXRC.h"
31 #include "Undo.h"
32
33 #include "frontends/Painter.h"
34
35 #include "support/debug.h"
36
37 #include <ostream>
38 #include <vector>
39
40 using namespace std;
41
42 namespace lyx {
43
44
45 /// A proxy for the macro values
46 class ArgumentProxy : public InsetMath {
47 public:
48         ///
49         ArgumentProxy(MathMacro & mathMacro, size_t idx) 
50                 : mathMacro_(mathMacro), idx_(idx) {}
51         ///
52         ArgumentProxy(MathMacro & mathMacro, size_t idx, docstring const & def) 
53                 : mathMacro_(mathMacro), idx_(idx) 
54         {
55                         asArray(def, def_);
56         }
57         ///
58         void metrics(MetricsInfo & mi, Dimension & dim) const {
59                 mathMacro_.macro()->unlock();
60                 if (!mathMacro_.editMetrics(mi.base.bv) 
61                     && mathMacro_.cell(idx_).empty())
62                         def_.metrics(mi, dim);
63                 else {
64                         CoordCache & coords = mi.base.bv->coordCache();
65                         dim = coords.arrays().dim(&mathMacro_.cell(idx_));
66                 }
67                 mathMacro_.macro()->lock();
68         }
69         ///
70         void draw(PainterInfo & pi, int x, int y) const {
71                 if (mathMacro_.editMetrics(pi.base.bv)) {
72                         // The only way a ArgumentProxy can appear is in a cell of the 
73                         // MathMacro. Moreover the cells are only drawn in the DISPLAY_FOLDED 
74                         // mode and then, if the macro is edited the monochrome 
75                         // mode is entered by the MathMacro before calling the cells' draw
76                         // method. Then eventually this code is reached and the proxy leaves
77                         // monochrome mode temporarely. Hence, if it is not in monochrome 
78                         // here (and the assert triggers in pain.leaveMonochromeMode()) 
79                         // it's a bug.
80                         pi.pain.leaveMonochromeMode();
81                         mathMacro_.cell(idx_).draw(pi, x, y);
82                         pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
83                 } else if (mathMacro_.cell(idx_).empty()) {
84                         mathMacro_.cell(idx_).setXY(*pi.base.bv, x, y);
85                         def_.draw(pi, x, y);
86                 } else
87                         mathMacro_.cell(idx_).draw(pi, x, y);
88         }
89         ///
90         size_t idx() const { return idx_; }
91         ///
92         int kerning(BufferView const * bv) const
93         { 
94                 if (mathMacro_.editMetrics(bv)
95                     || !mathMacro_.cell(idx_).empty())
96                         return mathMacro_.cell(idx_).kerning(bv); 
97                 else
98                         return def_.kerning(bv);
99         }
100
101 private:
102         ///
103         Inset * clone() const 
104         {
105                 return new ArgumentProxy(*this);
106         }
107         ///
108         MathMacro & mathMacro_;
109         ///
110         size_t idx_;
111         ///
112         MathData def_;
113 };
114
115
116 MathMacro::MathMacro(docstring const & name)
117         : InsetMathNest(0), name_(name), displayMode_(DISPLAY_INIT),
118                 attachedArgsNum_(0), optionals_(0), nextFoldMode_(true),
119                 macro_(0), needsUpdate_(false), appetite_(9)
120 {}
121
122
123 Inset * MathMacro::clone() const
124 {
125         MathMacro * copy = new MathMacro(*this);
126         copy->needsUpdate_ = true;
127         copy->expanded_.cell(0).clear();
128         return copy;
129 }
130
131
132 docstring MathMacro::name() const
133 {
134         if (displayMode_ == DISPLAY_UNFOLDED)
135                 return asString(cell(0));
136         else
137                 return name_;
138 }
139
140
141 void MathMacro::cursorPos(BufferView const & bv,
142                 CursorSlice const & sl, bool boundary, int & x, int & y) const
143 {
144         // We may have 0 arguments, but InsetMathNest requires at least one.
145         if (nargs() > 0)
146                 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
147 }
148
149
150 bool MathMacro::editMode(BufferView const * bv) const {
151         // find this in cursor trace
152         Cursor const & cur = bv->cursor();
153         for (size_t i = 0; i != cur.depth(); ++i)
154                 if (&cur[i].inset() == this) {
155                         // look if there is no other macro in edit mode above
156                         ++i;
157                         for (; i != cur.depth(); ++i) {
158                                 MathMacro const * macro = dynamic_cast<MathMacro const *>(&cur[i].inset());
159                                 if (macro && macro->displayMode() == DISPLAY_NORMAL)
160                                         return false;
161                         }
162
163                         // ok, none found, I am the highest one
164                         return true;
165                 }
166
167         return false;
168 }
169
170
171 bool MathMacro::editMetrics(BufferView const * bv) const
172 {
173         return editing_[bv];
174 }
175
176
177 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
178 {
179         // set edit mode for which we will have calculated metrics. But only
180         editing_[mi.base.bv] = editMode(mi.base.bv);
181
182         // calculate new metrics according to display mode
183         if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {
184                 mathed_string_dim(mi.base.font, from_ascii("\\") + name(), dim);
185         } else if (displayMode_ == DISPLAY_UNFOLDED) {
186                 cell(0).metrics(mi, dim);
187                 Dimension bsdim;
188                 mathed_string_dim(mi.base.font, from_ascii("\\"), bsdim);
189                 dim.wid += bsdim.width() + 1;
190                 dim.asc = max(bsdim.ascent(), dim.ascent());
191                 dim.des = max(bsdim.descent(), dim.descent());
192                 metricsMarkers(dim);
193         } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST 
194                    && editing_[mi.base.bv]) {
195                 // Macro will be edited in a old-style list mode here:
196
197                 BOOST_ASSERT(macro_ != 0);
198                 Dimension fontDim;
199                 FontInfo labelFont = sane_font;
200                 math_font_max_dim(labelFont, fontDim.asc, fontDim.des);
201                 
202                 // get dimension of components of list view
203                 Dimension nameDim;
204                 nameDim.wid = mathed_string_width(mi.base.font, from_ascii("Macro \\") + name() + ": ");
205                 nameDim.asc = fontDim.asc;
206                 nameDim.des = fontDim.des;
207
208                 Dimension argDim;
209                 argDim.wid = mathed_string_width(labelFont, from_ascii("#9: "));
210                 argDim.asc = fontDim.asc;
211                 argDim.des = fontDim.des;
212                 
213                 Dimension defDim;
214                 definition_.metrics(mi, defDim);
215                 
216                 // add them up
217                 dim.wid = nameDim.wid + defDim.wid;
218                 dim.asc = max(nameDim.asc, defDim.asc);
219                 dim.des = max(nameDim.des, defDim.des);
220                 
221                 for (idx_type i = 0; i < nargs(); ++i) {
222                         Dimension cdim;
223                         cell(i).metrics(mi, cdim);
224                         dim.des += max(argDim.height(), cdim.height()) + 1;
225                         dim.wid = max(dim.wid, argDim.wid + cdim.wid);
226                 }
227                 
228                 // make space for box and markers, 2 pixels
229                 dim.asc += 1;
230                 dim.des += 1;
231                 dim.wid += 2;
232                 metricsMarkers2(dim);
233         } else {
234                 BOOST_ASSERT(macro_ != 0);
235
236                 // metrics are computed here for the cells,
237                 // in the proxy we will then use the dim from the cache
238                 InsetMathNest::metrics(mi);
239                 
240                 // calculate metrics finally
241                 macro_->lock();
242                 expanded_.cell(0).metrics(mi, dim);
243                 macro_->unlock();
244
245                 // calculate dimension with label while editing
246                 if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX 
247                     && editing_[mi.base.bv]) {
248                         FontInfo font = mi.base.font;
249                         augmentFont(font, from_ascii("lyxtex"));
250                         Dimension namedim;
251                         mathed_string_dim(font, name(), namedim);
252 #if 0
253                         dim.wid += 2 + namedim.wid + 2 + 2;
254                         dim.asc = max(dim.asc, namedim.asc) + 2;
255                         dim.des = max(dim.des, namedim.des) + 2;
256 #endif
257                         dim.wid = max(1 + namedim.wid + 1, 2 + dim.wid + 2);
258                         dim.asc += 1 + namedim.height() + 1;
259                         dim.des += 2;
260                 }
261          
262         }
263 }
264
265
266 int MathMacro::kerning(BufferView const * bv) const {
267         if (displayMode_ == DISPLAY_NORMAL && !editing_[bv])
268                 return expanded_.kerning(bv);
269         else
270                 return 0;
271 }
272
273
274 void MathMacro::updateMacro(MacroContext const & mc) 
275 {
276         if (validName()) {
277                 macro_ = mc.get(name());            
278                 if (macro_ && macroBackup_ != *macro_) {
279                         macroBackup_ = *macro_;
280                         needsUpdate_ = true;
281                 }
282         } else {
283                 macro_ = 0;
284         }
285 }
286
287
288 void MathMacro::updateRepresentation()
289 {
290         // known macro?
291         if (macro_ == 0)
292                 return;
293
294         // update requires
295         requires_ = macro_->requires();
296         
297         // non-normal mode? We are done!
298         if (displayMode_ != DISPLAY_NORMAL)
299                 return;
300
301         // macro changed?
302         if (!needsUpdate_)
303                 return;
304         
305         needsUpdate_ = false;
306         
307         // get default values of macro
308         vector<docstring> const & defaults = macro_->defaults();
309         
310         // create MathMacroArgumentValue objects pointing to the cells of the macro
311         vector<MathData> values(nargs());
312         for (size_t i = 0; i < nargs(); ++i) {
313                 ArgumentProxy * proxy;
314                 if (i < defaults.size()) 
315                         proxy = new ArgumentProxy(*this, i, defaults[i]);
316                 else
317                         proxy = new ArgumentProxy(*this, i);
318                 values[i].insert(0, MathAtom(proxy));
319         }
320         
321         // expanding macro with the values
322         macro_->expand(values, expanded_.cell(0));
323                 // get definition for list edit mode
324         docstring const & display = macro_->display();
325         asArray(display.empty() ? macro_->definition() : display, definition_);
326 }
327
328
329 void MathMacro::draw(PainterInfo & pi, int x, int y) const
330 {
331         Dimension const dim = dimension(*pi.base.bv);
332
333         setPosCache(pi, x, y);
334         int expx = x;
335         int expy = y;
336
337         if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {         
338                 PainterInfo pi2(pi.base.bv, pi.pain);
339                 pi2.base.font.setColor(macro_ ? Color_latex : Color_error);
340                 //pi2.base.style = LM_ST_TEXT;
341                 pi2.pain.text(x, y, from_ascii("\\") + name(), pi2.base.font);
342         } else if (displayMode_ == DISPLAY_UNFOLDED) {
343                 PainterInfo pi2(pi.base.bv, pi.pain);
344                 pi2.base.font.setColor(macro_ ? Color_latex : Color_error);
345                 //pi2.base.style = LM_ST_TEXT;
346                 pi2.pain.text(x, y, from_ascii("\\"), pi2.base.font);
347                 x += mathed_string_width(pi2.base.font, from_ascii("\\")) + 1;
348                 cell(0).draw(pi2, x, y);
349                 drawMarkers(pi2, expx, expy);
350         } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST
351                    && editing_[pi.base.bv]) {
352                 // Macro will be edited in a old-style list mode here:
353                 
354                 CoordCache & coords = pi.base.bv->coordCache();
355                 FontInfo const & labelFont = sane_font;
356                 
357                 // markers and box needs two pixels
358                 x += 2;
359                 
360                 // get maximal font height
361                 Dimension fontDim;
362                 math_font_max_dim(pi.base.font, fontDim.asc, fontDim.des);
363                 
364                 // draw label
365                 docstring label = from_ascii("Macro \\") + name() + from_ascii(": ");
366                 pi.pain.text(x, y, label, labelFont);
367                 x += mathed_string_width(labelFont, label);
368
369                 // draw definition
370                 definition_.draw(pi, x, y);
371                 Dimension defDim
372                 = coords.arrays().dim(&definition_);
373                 y += max(fontDim.des, defDim.des);
374                                 
375                 // draw parameters
376                 docstring str = from_ascii("#9");
377                 int strw1 = mathed_string_width(labelFont, from_ascii("#9"));
378                 int strw2 = mathed_string_width(labelFont, from_ascii(": "));
379                 
380                 for (idx_type i = 0; i < nargs(); ++i) {
381                         // position of label
382                         Dimension cdim
383                         = coords.arrays().dim(&cell(i));
384                         x = expx + 2;
385                         y += max(fontDim.asc, cdim.asc) + 1;
386                         
387                         // draw label
388                         str[1] = '1' + i;
389                         pi.pain.text(x, y, str, labelFont);
390                         x += strw1;
391                         pi.pain.text(x, y, from_ascii(":"), labelFont);
392                         x += strw2;
393                         
394                         // draw paramter
395                         cell(i).draw(pi, x, y);
396                         
397                         // next line
398                         y += max(fontDim.des, cdim.des);
399                 }
400                 
401                 pi.pain.rectangle(expx + 1, expy - dim.asc + 1, dim.wid - 3, 
402                                   dim.height() - 2, Color_mathmacroframe);
403                 drawMarkers2(pi, expx, expy);
404         } else {
405                 bool drawBox = lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX;
406                 
407                 // warm up cells
408                 for (size_t i = 0; i < nargs(); ++i)
409                         cell(i).setXY(*pi.base.bv, x, y);
410
411                 if (drawBox && editing_[pi.base.bv]) {
412                         // draw header and rectangle around
413                         FontInfo font = pi.base.font;
414                         augmentFont(font, from_ascii("lyxtex"));
415                         font.setSize(FONT_SIZE_TINY);
416                         font.setColor(Color_mathmacrolabel);
417                         Dimension namedim;
418                         mathed_string_dim(font, name(), namedim);
419
420                         pi.pain.fillRectangle(x, y - dim.asc, dim.wid, 1 + namedim.height() + 1, Color_mathmacrobg);
421                         pi.pain.text(x + 1, y - dim.asc + namedim.asc + 2, name(), font);
422                         expx += (dim.wid - expanded_.cell(0).dimension(*pi.base.bv).width()) / 2;
423                 }
424
425                 if (editing_[pi.base.bv]) {
426                         pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
427                         expanded_.cell(0).draw(pi, expx, expy);
428                         pi.pain.leaveMonochromeMode();
429
430                         if (drawBox)
431                                 pi.pain.rectangle(x, y - dim.asc, dim.wid, 
432                                                   dim.height(), Color_mathmacroframe);
433                 } else
434                         expanded_.cell(0).draw(pi, expx, expy);
435
436                 if (!drawBox)
437                         drawMarkers(pi, x, y);
438         }
439
440         // edit mode changed?
441         if (editing_[pi.base.bv] != editMode(pi.base.bv))
442                 pi.base.bv->cursor().updateFlags(Update::SinglePar);
443 }
444
445
446 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
447 {
448         // We may have 0 arguments, but InsetMathNest requires at least one.
449         if (cells_.size() > 0)
450                 InsetMathNest::drawSelection(pi, x, y);
451 }
452
453
454 void MathMacro::setDisplayMode(MathMacro::DisplayMode mode, int appetite)
455 {
456         if (displayMode_ != mode) {             
457                 // transfer name if changing from or to DISPLAY_UNFOLDED
458                 if (mode == DISPLAY_UNFOLDED) {
459                         cells_.resize(1);
460                         asArray(name_, cell(0));
461                 } else if (displayMode_ == DISPLAY_UNFOLDED) {
462                         name_ = asString(cell(0));
463                         cells_.resize(0);
464                 }
465
466                 displayMode_ = mode;
467                 needsUpdate_ = true;
468         }
469         
470         // the interactive init mode is non-greedy by default
471         if (appetite == -1)
472                 appetite_ = (mode == DISPLAY_INTERACTIVE_INIT) ? 0 : 9;
473         else
474                 appetite_ = size_t(appetite);
475 }
476
477
478 MathMacro::DisplayMode MathMacro::computeDisplayMode() const
479 {
480         if (nextFoldMode_ == true && macro_ && !macro_->locked())
481                 return DISPLAY_NORMAL;
482         else
483                 return DISPLAY_UNFOLDED;
484 }
485
486
487 bool MathMacro::validName() const
488 {
489         docstring n = name();
490
491         // empty name?
492         if (n.size() == 0)
493                 return false;
494
495         // converting back and force doesn't swallow anything?
496         /*MathData ma;
497         asArray(n, ma);
498         if (asString(ma) != n)
499                 return false;*/
500
501         // valid characters?
502         for (size_t i = 0; i<n.size(); ++i) {
503                 if (!(n[i] >= 'a' && n[i] <= 'z')
504                     && !(n[i] >= 'A' && n[i] <= 'Z')
505                     && n[i] != '*') 
506                         return false;
507         }
508
509         return true;
510 }
511
512
513 void MathMacro::validate(LaTeXFeatures & features) const
514 {
515         if (!requires_.empty())
516                 features.require(requires_);
517
518         if (name() == "binom" || name() == "mathcircumflex")
519                 features.require(to_utf8(name()));
520 }
521
522
523 void MathMacro::edit(Cursor & cur, bool front, EntryDirection entry_from)
524 {
525         cur.updateFlags(Update::SinglePar);
526         InsetMathNest::edit(cur, front, entry_from);
527 }
528
529
530 Inset * MathMacro::editXY(Cursor & cur, int x, int y)
531 {
532         // We may have 0 arguments, but InsetMathNest requires at least one.
533         if (nargs() > 0) {
534                 cur.updateFlags(Update::SinglePar);
535                 return InsetMathNest::editXY(cur, x, y);                
536         } else
537                 return this;
538 }
539
540
541 void MathMacro::removeArgument(Inset::pos_type pos) {
542         if (displayMode_ == DISPLAY_NORMAL) {
543                 BOOST_ASSERT(size_t(pos) < cells_.size());
544                 cells_.erase(cells_.begin() + pos);
545                 if (size_t(pos) < attachedArgsNum_)
546                         --attachedArgsNum_;
547                 if (size_t(pos) < optionals_) {
548                         --optionals_;
549                 }
550
551                 needsUpdate_ = true;
552         }
553 }
554
555
556 void MathMacro::insertArgument(Inset::pos_type pos) {
557         if (displayMode_ == DISPLAY_NORMAL) {
558                 BOOST_ASSERT(size_t(pos) <= cells_.size());
559                 cells_.insert(cells_.begin() + pos, MathData());
560                 if (size_t(pos) < attachedArgsNum_)
561                         ++attachedArgsNum_;
562                 if (size_t(pos) < optionals_)
563                         ++optionals_;
564
565                 needsUpdate_ = true;
566         }
567 }
568
569
570 void MathMacro::detachArguments(vector<MathData> & args, bool strip)
571 {
572         BOOST_ASSERT(displayMode_ == DISPLAY_NORMAL);   
573         args = cells_;
574
575         // strip off empty cells, but not more than arity-attachedArgsNum_
576         if (strip) {
577                 size_t i;
578                 for (i = cells_.size(); i > attachedArgsNum_; --i)
579                         if (!cell(i - 1).empty()) break;
580                 args.resize(i);
581         }
582
583         attachedArgsNum_ = 0;
584         expanded_.cell(0) = MathData();
585         cells_.resize(0);
586
587         needsUpdate_ = true;
588 }
589
590
591 void MathMacro::attachArguments(vector<MathData> const & args, size_t arity, int optionals)
592 {
593         BOOST_ASSERT(displayMode_ == DISPLAY_NORMAL);
594         cells_ = args;
595         attachedArgsNum_ = args.size();
596         cells_.resize(arity);
597         expanded_.cell(0) = MathData();
598         optionals_ = optionals;
599
600         needsUpdate_ = true;
601 }
602
603
604 bool MathMacro::idxFirst(Cursor & cur) const 
605 {
606         cur.updateFlags(Update::SinglePar);
607         return InsetMathNest::idxFirst(cur);
608 }
609
610
611 bool MathMacro::idxLast(Cursor & cur) const 
612 {
613         cur.updateFlags(Update::SinglePar);
614         return InsetMathNest::idxLast(cur);
615 }
616
617
618 bool MathMacro::notifyCursorLeaves(Cursor const & old, Cursor & cur)
619 {
620         cur.updateFlags(Update::Force);
621         return InsetMathNest::notifyCursorLeaves(old, cur);
622 }
623
624
625 void MathMacro::fold(Cursor & cur)
626 {
627         if (!nextFoldMode_) {
628                 nextFoldMode_ = true;
629                 cur.updateFlags(Update::SinglePar);
630         }
631 }
632
633
634 void MathMacro::unfold(Cursor & cur)
635 {
636         if (nextFoldMode_) {
637                 nextFoldMode_ = false;
638                 cur.updateFlags(Update::SinglePar);
639         }
640 }
641
642
643 bool MathMacro::folded() const
644 {
645         return nextFoldMode_;
646 }
647
648
649 void MathMacro::write(WriteStream & os) const
650 {
651         // non-normal mode
652         if (displayMode_ != DISPLAY_NORMAL) {
653                 os << "\\" << name() << " ";
654                 os.pendingSpace(true);
655                 return;
656         }
657
658         // normal mode
659         BOOST_ASSERT(macro_);
660
661         // optional arguments make macros fragile
662         if (optionals_ > 0 && os.fragile())
663                 os << "\\protect";
664         
665         os << "\\" << name();
666         bool first = true;
667         
668         // Optional arguments:
669         // First find last non-empty optional argument
670         idx_type emptyOptFrom = 0;
671         idx_type i = 0;
672         for (; i < cells_.size() && i < optionals_; ++i) {
673                 if (!cell(i).empty())
674                         emptyOptFrom = i + 1;
675         }
676         
677         // print out optionals
678         for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) {
679                 first = false;
680                 os << "[" << cell(i) << "]";
681         }
682         
683         // skip the tailing empty optionals
684         i = optionals_;
685         
686         // Print remaining macros 
687         for (; i < cells_.size(); ++i) {
688                 if (cell(i).size() == 1 
689                          && cell(i)[0].nucleus()->asCharInset()) {
690                         if (first)
691                                 os << " ";
692                         os << cell(i);
693                 } else
694                         os << "{" << cell(i) << "}";
695                 first = false;
696         }
697
698         // add space if there was no argument
699         if (first)
700                 os.pendingSpace(true);
701 }
702
703
704 void MathMacro::maple(MapleStream & os) const
705 {
706         lyx::maple(expanded_.cell(0), os);
707 }
708
709
710 void MathMacro::mathmlize(MathStream & os) const
711 {
712         lyx::mathmlize(expanded_.cell(0), os);
713 }
714
715
716 void MathMacro::octave(OctaveStream & os) const
717 {
718         lyx::octave(expanded_.cell(0), os);
719 }
720
721
722 void MathMacro::infoize(odocstream & os) const
723 {
724         os << "Macro: " << name();
725 }
726
727
728 void MathMacro::infoize2(odocstream & os) const
729 {
730         os << "Macro: " << name();
731
732 }
733
734
735 bool MathMacro::completionSupported(Cursor const & cur) const
736 {
737         if (displayMode() != DISPLAY_UNFOLDED)
738                 return InsetMathNest::completionSupported(cur);
739
740         return lyxrc.completion_popup_math
741                 && displayMode() == DISPLAY_UNFOLDED
742                 && cur.bv().cursor().pos() == int(name().size());
743 }
744
745
746 bool MathMacro::inlineCompletionSupported(Cursor const & cur) const
747 {
748         if (displayMode() != DISPLAY_UNFOLDED)
749                 return InsetMathNest::inlineCompletionSupported(cur);
750
751         return lyxrc.completion_inline_math
752                 && displayMode() == DISPLAY_UNFOLDED
753                 && cur.bv().cursor().pos() == int(name().size());
754 }
755
756
757 bool MathMacro::automaticInlineCompletion() const
758 {
759         if (displayMode() != DISPLAY_UNFOLDED)
760                 return InsetMathNest::automaticInlineCompletion();
761
762         return lyxrc.completion_inline_math;
763 }
764
765
766 bool MathMacro::automaticPopupCompletion() const
767 {
768         if (displayMode() != DISPLAY_UNFOLDED)
769                 return InsetMathNest::automaticPopupCompletion();
770
771         return lyxrc.completion_popup_math;
772 }
773
774
775 CompletionList const * 
776 MathMacro::createCompletionList(Cursor const & cur) const
777 {
778         if (displayMode() != DISPLAY_UNFOLDED)
779                 return InsetMathNest::createCompletionList(cur);
780
781         return new MathCompletionList(cur.bv().cursor());
782 }
783
784
785 docstring MathMacro::completionPrefix(Cursor const & cur) const
786 {
787         if (displayMode() != DISPLAY_UNFOLDED)
788                 return InsetMathNest::completionPrefix(cur);
789
790         if (!completionSupported(cur))
791                 return docstring();
792         
793         return "\\" + name();
794 }
795
796
797 bool MathMacro::insertCompletion(Cursor & cur, docstring const & s,
798                                         bool finished)
799 {
800         if (displayMode() != DISPLAY_UNFOLDED)
801                 return InsetMathNest::insertCompletion(cur, s, finished);
802
803         if (!completionSupported(cur))
804                 return false;
805
806         // append completion
807         docstring newName = name() + s;
808         asArray(newName, cell(0));
809         cur.bv().cursor().pos() = name().size();
810         cur.updateFlags(Update::SinglePar);
811         
812         // finish macro
813         if (finished) {
814                 cur.bv().cursor().pop();
815                 ++cur.bv().cursor().pos();
816                 cur.updateFlags(Update::SinglePar);
817         }
818         
819         return true;
820 }
821
822
823 void MathMacro::completionPosAndDim(Cursor const & cur, int & x, int & y,
824         Dimension & dim) const
825 {
826         if (displayMode() != DISPLAY_UNFOLDED)
827                 InsetMathNest::completionPosAndDim(cur, x, y, dim);
828         
829         // get inset dimensions
830         dim = cur.bv().coordCache().insets().dim(this);
831         // FIXME: these 3 are no accurate, but should depend on the font.
832         // Now the popup jumps down if you enter a char with descent > 0.
833         dim.des += 3;
834         dim.asc += 3;
835         
836         // and position
837         Point xy
838         = cur.bv().coordCache().insets().xy(this);
839         x = xy.x_;
840         y = xy.y_;
841 }
842
843
844 } // namespace lyx