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