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